Skip to content

Commit

Permalink
Add additional BLS benchmarks (#3538)
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenButtolph authored Nov 12, 2024
1 parent 31f52b0 commit 5fcf290
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 7 deletions.
10 changes: 5 additions & 5 deletions snow/validators/gvalidators/validator_state_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions snow/validators/gvalidators/validator_state_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
50 changes: 50 additions & 0 deletions utils/crypto/bls/bls_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 5fcf290

Please sign in to comment.