diff --git a/snow/validators/gvalidators/validator_state_client.go b/snow/validators/gvalidators/validator_state_client.go index ae09b749d3ca..60c7a17cd9c8 100644 --- a/snow/validators/gvalidators/validator_state_client.go +++ b/snow/validators/gvalidators/validator_state_client.go @@ -76,11 +76,11 @@ func (c *Client) GetValidatorSet( } var publicKey *bls.PublicKey if len(validator.PublicKey) > 0 { - // This is a performance optimization to avoid the cost of - // compression and key re-verification with - // PublicKeyFromCompressedBytes. We can safely assume that the BLS - // Public Keys are verified before being added to the P-Chain and - // served by the gRPC server. + // PublicKeyFromValidUncompressedBytes is used rather than + // PublicKeyFromCompressedBytes because it is significantly faster + // due to the avoidance of decompression and key re-verification. 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 = bls.PublicKeyFromValidUncompressedBytes(validator.PublicKey) if publicKey == nil { return nil, errFailedPublicKeyDeserialize diff --git a/snow/validators/gvalidators/validator_state_server.go b/snow/validators/gvalidators/validator_state_server.go index 0550eba0b9b3..5eec541af589 100644 --- a/snow/validators/gvalidators/validator_state_server.go +++ b/snow/validators/gvalidators/validator_state_server.go @@ -70,8 +70,9 @@ func (s *Server) GetValidatorSet(ctx context.Context, req *pb.GetValidatorSetReq Weight: vdr.Weight, } if vdr.PublicKey != nil { - // This is a performance optimization to avoid the cost of compression - // from PublicKeyToCompressedBytes. + // Passing in the uncompressed bytes is a performance optimization + // to avoid the cost of calling PublicKeyFromCompressedBytes on the + // client side. vdrPB.PublicKey = bls.PublicKeyToUncompressedBytes(vdr.PublicKey) } resp.Validators[i] = vdrPB diff --git a/utils/crypto/bls/bls_benchmark_test.go b/utils/crypto/bls/bls_benchmark_test.go index b9648b43c04e..ca2c70182777 100644 --- a/utils/crypto/bls/bls_benchmark_test.go +++ b/utils/crypto/bls/bls_benchmark_test.go @@ -80,3 +80,53 @@ func BenchmarkAggregatePublicKeys(b *testing.B) { }) } } + +func BenchmarkPublicKeyToCompressedBytes(b *testing.B) { + sk, err := NewSecretKey() + require.NoError(b, err) + + pk := PublicFromSecretKey(sk) + + b.ResetTimer() + for range b.N { + PublicKeyToCompressedBytes(pk) + } +} + +func BenchmarkPublicKeyFromCompressedBytes(b *testing.B) { + sk, err := NewSecretKey() + require.NoError(b, err) + + pk := PublicFromSecretKey(sk) + pkBytes := PublicKeyToCompressedBytes(pk) + + b.ResetTimer() + for range b.N { + _, _ = PublicKeyFromCompressedBytes(pkBytes) + } +} + +func BenchmarkPublicKeyToUncompressedBytes(b *testing.B) { + sk, err := NewSecretKey() + require.NoError(b, err) + + pk := PublicFromSecretKey(sk) + + b.ResetTimer() + for range b.N { + PublicKeyToUncompressedBytes(pk) + } +} + +func BenchmarkPublicKeyFromValidUncompressedBytes(b *testing.B) { + sk, err := NewSecretKey() + require.NoError(b, err) + + pk := PublicFromSecretKey(sk) + pkBytes := PublicKeyToUncompressedBytes(pk) + + b.ResetTimer() + for range b.N { + _ = PublicKeyFromValidUncompressedBytes(pkBytes) + } +}