Skip to content

Commit

Permalink
Store BLS private key in hex instead of raw big int (#1973)
Browse files Browse the repository at this point in the history
  • Loading branch information
begmaroman committed Oct 11, 2023
1 parent 4d379fd commit e34c65e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 29 deletions.
2 changes: 1 addition & 1 deletion command/polybftsecrets/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func (ip *initParams) getResult(
return nil, err
}

res.BLSPrivateKey = hex.EncodeToString(blspk)
res.BLSPrivateKey = string(blspk)
}
}

Expand Down
4 changes: 1 addition & 3 deletions command/polybftsecrets/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,7 @@ func Test_getResult(t *testing.T) {
assert.Equal(t, sir.Address.String(), pubKey)

// Test BLS public key serialization
blsPrivKeyRaw, err := hex.DecodeString(sir.BLSPrivateKey)
require.NoError(t, err)
blsPrivKey, err := bls.UnmarshalPrivateKey(blsPrivKeyRaw)
blsPrivKey, err := bls.UnmarshalPrivateKey([]byte(sir.BLSPrivateKey))
require.NoError(t, err)

blsPubKey := hex.EncodeToString(blsPrivKey.PublicKey().Marshal())
Expand Down
46 changes: 33 additions & 13 deletions consensus/polybft/signer/private.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,56 @@ package bls

import (
"math/big"
"strings"

bn256 "github.com/umbracle/go-eth-bn256"

"github.com/0xPolygon/polygon-edge/helper/hex"
)

// PrivateKey holds private key for bls implementation
type PrivateKey struct {
s *big.Int
}

// NewZeroPrivateKey is the constructor of an empty PrivateKey
func NewZeroPrivateKey() *PrivateKey {
return &PrivateKey{
s: new(big.Int),
}
}

// UnmarshalPrivateKey unmarshals the private key from the given byte slice.
// This function supports both raw big int string and hex-encoded big int string.
func UnmarshalPrivateKey(data []byte) (*PrivateKey, error) {
pk := NewZeroPrivateKey()

// First trying to use a default unmarshaling function of big int.
// It works for the raw big int string format and for hex with 0x prefix.
if err := pk.s.UnmarshalText(data); err == nil {
return pk, nil
}

// Otherwise, trying to assume the given data is a hex encoded big int represented as a bytes array.
// This is needed in order to be compatible with the currently stored polybft BLS keys.
var err error
if pk.s, err = hex.DecodeHexToBig(string(data)); err != nil {
return nil, err
}

return pk, nil
}

// PublicKey returns the public key from the PrivateKey
func (p *PrivateKey) PublicKey() *PublicKey {
g2 := new(bn256.G2).ScalarMult(g2Point, p.s)

return &PublicKey{g2: g2}
}

// Marshal marshals private key to byte slice
// Marshal marshals private key hex (without 0x prefix) represented as a byte slice
func (p *PrivateKey) Marshal() ([]byte, error) {
return p.s.MarshalText()
return []byte(strings.TrimPrefix(hex.EncodeBig(p.s), "0x")), nil
}

// Sign generates a simple BLS signature of the given message
Expand All @@ -34,14 +65,3 @@ func (p *PrivateKey) Sign(message, domain []byte) (*Signature, error) {

return &Signature{g1: g1}, nil
}

// UnmarshalPrivateKey unmarshals the private key from the given byte slice
func UnmarshalPrivateKey(data []byte) (*PrivateKey, error) {
s := new(big.Int)

if err := s.UnmarshalText(data); err != nil {
return nil, err
}

return &PrivateKey{s: s}, nil
}
12 changes: 0 additions & 12 deletions crypto/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,18 +369,6 @@ func BLSSecretKeyToPubkeyBytes(key *bls_sig.SecretKey) ([]byte, error) {
return marshalled, nil
}

// BytesToBLSPublicKey decodes given hex string and returns BLS Public Key
func BytesToBLSPublicKey(input string) (*bls_sig.PublicKey, error) {
// The key file on disk should be encoded in Base64,
// so it must be decoded before it can be parsed by ParsePrivateKey
decoded, err := hex.DecodeString(input)
if err != nil {
return nil, err
}

return UnmarshalBLSPublicKey(decoded)
}

// UnmarshalBLSPublicKey unmarshal bytes data into BLS Public Key
func UnmarshalBLSPublicKey(input []byte) (*bls_sig.PublicKey, error) {
pk := &bls_sig.PublicKey{}
Expand Down

0 comments on commit e34c65e

Please sign in to comment.