Skip to content

Commit

Permalink
Move all blst function usage to bls pkg (#2222)
Browse files Browse the repository at this point in the history
Signed-off-by: Dan Laine <[email protected]>
Co-authored-by: Dan Laine <[email protected]>
  • Loading branch information
dhrubabasu and Dan Laine authored Oct 26, 2023
1 parent 3b213fc commit cacbb9b
Show file tree
Hide file tree
Showing 11 changed files with 29 additions and 12 deletions.
2 changes: 1 addition & 1 deletion snow/validators/gvalidators/validator_state_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (c *Client) GetValidatorSet(
// and key re-verification with PublicKeyFromBytes. We can safely
// assume that the BLS Public Keys are verified before being added
// to the P-Chain and served by the gRPC server.
publicKey = new(bls.PublicKey).Deserialize(validator.PublicKey)
publicKey = bls.DeserializePublicKey(validator.PublicKey)
if publicKey == nil {
return nil, errFailedPublicKeyDeserialize
}
Expand Down
3 changes: 2 additions & 1 deletion snow/validators/gvalidators/validator_state_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/validators"
"github.com/ava-labs/avalanchego/utils/crypto/bls"

pb "github.com/ava-labs/avalanchego/proto/pb/validatorstate"
)
Expand Down Expand Up @@ -71,7 +72,7 @@ func (s *Server) GetValidatorSet(ctx context.Context, req *pb.GetValidatorSetReq
if vdr.PublicKey != nil {
// This is a performance optimization to avoid the cost of compression
// from PublicKeyToBytes.
vdrPB.PublicKey = vdr.PublicKey.Serialize()
vdrPB.PublicKey = bls.SerializePublicKey(vdr.PublicKey)
}
resp.Validators[i] = vdrPB
i++
Expand Down
4 changes: 2 additions & 2 deletions snow/validators/gvalidators/validator_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ func TestPublicKeyDeserialize(t *testing.T) {
require.NoError(err)
pk := bls.PublicFromSecretKey(sk)

pkBytes := pk.Serialize()
pkDe := new(bls.PublicKey).Deserialize(pkBytes)
pkBytes := bls.SerializePublicKey(pk)
pkDe := bls.DeserializePublicKey(pkBytes)
require.NotNil(pkDe)
require.Equal(pk, pkDe)
}
Expand Down
2 changes: 1 addition & 1 deletion tests/fixture/testnet/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func (nc *NodeConfig) EnsureBLSSigningKey() error {
if err != nil {
return fmt.Errorf("failed to generate staking signer key: %w", err)
}
nc.Flags[config.StakingSignerKeyContentKey] = base64.StdEncoding.EncodeToString(newKey.Serialize())
nc.Flags[config.StakingSignerKeyContentKey] = base64.StdEncoding.EncodeToString(bls.SerializeSecretKey(newKey))
return nil
}

Expand Down
8 changes: 8 additions & 0 deletions utils/crypto/bls/public.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,11 @@ func Verify(pk *PublicKey, sig *Signature, msg []byte) bool {
func VerifyProofOfPossession(pk *PublicKey, sig *Signature, msg []byte) bool {
return sig.Verify(false, pk, false, msg, ciphersuiteProofOfPossession)
}

func DeserializePublicKey(pkBytes []byte) *PublicKey {
return new(PublicKey).Deserialize(pkBytes)
}

func SerializePublicKey(key *PublicKey) []byte {
return key.Serialize()
}
8 changes: 8 additions & 0 deletions utils/crypto/bls/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,11 @@ func Sign(sk *SecretKey, msg []byte) *Signature {
func SignProofOfPossession(sk *SecretKey, msg []byte) *Signature {
return new(Signature).Sign(sk, msg, ciphersuiteProofOfPossession)
}

func DeserializeSecretKey(pkBytes []byte) *SecretKey {
return new(SecretKey).Deserialize(pkBytes)
}

func SerializeSecretKey(key *SecretKey) []byte {
return key.Serialize()
}
4 changes: 2 additions & 2 deletions vms/platformvm/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -1322,7 +1322,7 @@ func (s *state) ApplyValidatorPublicKeyDiffs(
continue
}

vdr.PublicKey = new(bls.PublicKey).Deserialize(pkBytes)
vdr.PublicKey = bls.DeserializePublicKey(pkBytes)
}

// Note: this does not fallback to the linkeddb index because the linkeddb
Expand Down Expand Up @@ -2060,7 +2060,7 @@ func (s *state) writeCurrentStakers(updateValidators bool, height uint64) error
// diffs.
err := s.flatValidatorPublicKeyDiffsDB.Put(
marshalDiffKey(constants.PrimaryNetworkID, height, nodeID),
staker.PublicKey.Serialize(),
bls.SerializePublicKey(staker.PublicKey),
)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion vms/platformvm/vm_regression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2255,7 +2255,7 @@ func checkValidatorBlsKeyIsSet(
return errors.New("unexpected BLS key")
case expectedBlsKey != nil && val.PublicKey == nil:
return errors.New("missing BLS key")
case !bytes.Equal(expectedBlsKey.Serialize(), val.PublicKey.Serialize()):
case !bytes.Equal(bls.SerializePublicKey(expectedBlsKey), bls.SerializePublicKey(val.PublicKey)):
return errors.New("incorrect BLS key")
default:
return nil
Expand Down
2 changes: 1 addition & 1 deletion vms/platformvm/warp/signature_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func newTestValidator() *testValidator {
sk: sk,
vdr: &Validator{
PublicKey: pk,
PublicKeyBytes: pk.Serialize(),
PublicKeyBytes: bls.SerializePublicKey(pk),
Weight: 3,
NodeIDs: []ids.NodeID{nodeID},
},
Expand Down
2 changes: 1 addition & 1 deletion vms/platformvm/warp/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func GetCanonicalValidatorSet(
continue
}

pkBytes := vdr.PublicKey.Serialize()
pkBytes := bls.SerializePublicKey(vdr.PublicKey)
uniqueVdr, ok := vdrs[string(pkBytes)]
if !ok {
uniqueVdr = &Validator{
Expand Down
4 changes: 2 additions & 2 deletions vms/platformvm/warp/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func TestFilterValidators(t *testing.T) {
pk0 := bls.PublicFromSecretKey(sk0)
vdr0 := &Validator{
PublicKey: pk0,
PublicKeyBytes: pk0.Serialize(),
PublicKeyBytes: bls.SerializePublicKey(pk0),
Weight: 1,
}

Expand All @@ -175,7 +175,7 @@ func TestFilterValidators(t *testing.T) {
pk1 := bls.PublicFromSecretKey(sk1)
vdr1 := &Validator{
PublicKey: pk1,
PublicKeyBytes: pk1.Serialize(),
PublicKeyBytes: bls.SerializePublicKey(pk1),
Weight: 2,
}

Expand Down

0 comments on commit cacbb9b

Please sign in to comment.