Module: Eth::Bls

Defined in:
lib/eth/bls.rb

Overview

Helper methods for interacting with BLS12-381 points and signatures

Class Method Summary collapse

Class Method Details

.decode_public_key(hex) ⇒ BLS::PointG1

Decode a compressed G1 public key from hex.

Parameters:

  • hex (String)

    a compressed G1 point

Returns:

  • (BLS::PointG1)


13
14
15
# File 'lib/eth/bls.rb', line 13

def decode_public_key(hex)
  BLS::PointG1.from_hex Util.remove_hex_prefix(hex)
end

.decode_signature(hex) ⇒ BLS::PointG2

Decode a compressed G2 signature from hex.

Parameters:

  • hex (String)

    a compressed G2 point

Returns:

  • (BLS::PointG2)


27
28
29
# File 'lib/eth/bls.rb', line 27

def decode_signature(hex)
  BLS::PointG2.from_hex Util.remove_hex_prefix(hex)
end

.encode_public_key(point) ⇒ String

Encode a G1 public key to compressed hex.

Parameters:

  • point (BLS::PointG1)

Returns:

  • (String)

    hex string prefixed with 0x



20
21
22
# File 'lib/eth/bls.rb', line 20

def encode_public_key(point)
  Util.prefix_hex point.to_hex(compressed: true)
end

.encode_signature(point) ⇒ String

Encode a G2 signature to compressed hex.

Parameters:

  • point (BLS::PointG2)

Returns:

  • (String)

    hex string prefixed with 0x



34
35
36
# File 'lib/eth/bls.rb', line 34

def encode_signature(point)
  Util.prefix_hex point.to_hex(compressed: true)
end

.get_public_key(priv_hex) ⇒ String

Derive a compressed public key from a private key.

Parameters:

  • priv_hex (String)

    private key as hex

Returns:

  • (String)

    compressed G1 public key (hex)



41
42
43
44
# File 'lib/eth/bls.rb', line 41

def get_public_key(priv_hex)
  key = BLS.get_public_key Util.remove_hex_prefix(priv_hex)
  encode_public_key key
end

.sign(message, priv_hex) ⇒ String

Sign a message digest with the given private key.

Parameters:

  • message (String)

    message digest (hex)

  • priv_hex (String)

    private key as hex

Returns:

  • (String)

    compressed G2 signature (hex)



50
51
52
53
54
# File 'lib/eth/bls.rb', line 50

def sign(message, priv_hex)
  sig = BLS.sign Util.remove_hex_prefix(message),
                 Util.remove_hex_prefix(priv_hex)
  encode_signature sig
end

.verify(message, signature_hex, pubkey_hex) ⇒ Boolean

Verify a BLS signature using pairings. This mirrors the behaviour of the BLS12-381 pairing precompile.

Parameters:

  • message (String)

    message digest (hex)

  • signature_hex (String)

    compressed G2 signature (hex)

  • pubkey_hex (String)

    compressed G1 public key (hex)

Returns:

  • (Boolean)

    verification result



62
63
64
65
66
# File 'lib/eth/bls.rb', line 62

def verify(message, signature_hex, pubkey_hex)
  signature = decode_signature(signature_hex)
  pubkey = decode_public_key(pubkey_hex)
  BLS.verify(signature, Util.remove_hex_prefix(message), pubkey)
end