Skip to content

Commit

Permalink
Move bls-tests back to bls package
Browse files Browse the repository at this point in the history
  • Loading branch information
richardpringle committed Jan 14, 2025
1 parent 3c1ce6c commit ad10cb2
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 98 deletions.
Original file line number Diff line number Diff line change
@@ -1,44 +1,23 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package blstest
package bls

import (
"crypto/rand"
"strconv"
"testing"

"github.com/stretchr/testify/require"

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

blst "github.com/supranational/blst/bindings/go"
"github.com/ava-labs/avalanchego/utils/crypto/bls/blstest"
)

func newKey(require *require.Assertions) *blst.SecretKey {
var ikm [32]byte
_, err := rand.Read(ikm[:])
require.NoError(err)
sk := blst.KeyGen(ikm[:])
ikm = [32]byte{} // zero out the ikm

return sk
}

func sign(sk *blst.SecretKey, msg []byte) *bls.Signature {
return new(bls.Signature).Sign(sk, msg, bls.CiphersuiteSignature)
}

func publicKey(sk *blst.SecretKey) *bls.PublicKey {
return new(bls.PublicKey).From(sk)
}

func BenchmarkVerify(b *testing.B) {
privateKey := newKey(require.New(b))
publicKey := publicKey(privateKey)

for _, messageSize := range BenchmarkSizes {
for _, messageSize := range blstest.BenchmarkSizes {
b.Run(strconv.Itoa(messageSize), func(b *testing.B) {

Check failure on line 21 in utils/crypto/bls/bls_benchmark_test.go

View workflow job for this annotation

GitHub Actions / Lint

empty-lines: extra empty line at the start of a block (revive)

Check failure on line 21 in utils/crypto/bls/bls_benchmark_test.go

View workflow job for this annotation

GitHub Actions / Lint

unnecessary leading newline (whitespace)

message := utils.RandomBytes(messageSize)
Expand All @@ -47,25 +26,25 @@ func BenchmarkVerify(b *testing.B) {
b.ResetTimer()

for n := 0; n < b.N; n++ {
require.True(b, bls.Verify(publicKey, signature, message))
require.True(b, Verify(publicKey, signature, message))
}
})
}
}

func BenchmarkAggregatePublicKeys(b *testing.B) {
keys := make([]*bls.PublicKey, BiggestBenchmarkSize)
keys := make([]*PublicKey, blstest.BiggestBenchmarkSize)

for i := range keys {
privateKey := newKey(require.New(b))

keys[i] = publicKey(privateKey)
}

for _, size := range BenchmarkSizes {
for _, size := range blstest.BenchmarkSizes {
b.Run(strconv.Itoa(size), func(b *testing.B) {
for n := 0; n < b.N; n++ {
_, err := bls.AggregatePublicKeys(keys[:size])
_, err := AggregatePublicKeys(keys[:size])
require.NoError(b, err)
}
})
Expand All @@ -79,19 +58,19 @@ func BenchmarkPublicKeyToCompressedBytes(b *testing.B) {

b.ResetTimer()
for range b.N {
bls.PublicKeyToCompressedBytes(pk)
PublicKeyToCompressedBytes(pk)
}
}

func BenchmarkPublicKeyFromCompressedBytes(b *testing.B) {
sk := newKey(require.New(b))

pk := publicKey(sk)
pkBytes := bls.PublicKeyToCompressedBytes(pk)
pkBytes := PublicKeyToCompressedBytes(pk)

b.ResetTimer()
for range b.N {
_, _ = bls.PublicKeyFromCompressedBytes(pkBytes)
_, _ = PublicKeyFromCompressedBytes(pkBytes)
}
}

Expand All @@ -102,19 +81,19 @@ func BenchmarkPublicKeyToUncompressedBytes(b *testing.B) {

b.ResetTimer()
for range b.N {
bls.PublicKeyToUncompressedBytes(pk)
PublicKeyToUncompressedBytes(pk)
}
}

func BenchmarkPublicKeyFromValidUncompressedBytes(b *testing.B) {
sk := newKey(require.New(b))

pk := publicKey(sk)
pkBytes := bls.PublicKeyToUncompressedBytes(pk)
pkBytes := PublicKeyToUncompressedBytes(pk)

b.ResetTimer()
for range b.N {
_ = bls.PublicKeyFromValidUncompressedBytes(pkBytes)
_ = PublicKeyFromValidUncompressedBytes(pkBytes)
}
}

Expand All @@ -123,10 +102,10 @@ func BenchmarkSignatureFromBytes(b *testing.B) {

message := utils.RandomBytes(32)
signature := sign(sk, message)
signatureBytes := bls.SignatureToBytes(signature)
signatureBytes := SignatureToBytes(signature)

b.ResetTimer()
for range b.N {
_, _ = bls.SignatureFromBytes(signatureBytes)
_, _ = SignatureFromBytes(signatureBytes)
}
}
13 changes: 0 additions & 13 deletions utils/crypto/bls/bls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,6 @@ func sign(sk *blst.SecretKey, msg []byte) *Signature {
return new(Signature).Sign(sk, msg, CiphersuiteSignature)
}

func AggregateAndVerify(publicKeys []*PublicKey, signatures []*Signature, message []byte) (bool, error) {
aggSig, err := AggregateSignatures(signatures)
if err != nil {
return false, err
}
aggPK, err := AggregatePublicKeys(publicKeys)
if err != nil {
return false, err
}

return Verify(aggPK, aggSig, message), nil
}

func TestAggregationThreshold(t *testing.T) {
require := require.New(t)

Expand Down
21 changes: 0 additions & 21 deletions utils/crypto/bls/blstest/blstest.go

This file was deleted.

13 changes: 13 additions & 0 deletions utils/crypto/bls/signature.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,16 @@ func AggregateSignatures(sigs []*Signature) (*Signature, error) {
}
return agg.ToAffine(), nil
}

func AggregateAndVerify(publicKeys []*PublicKey, signatures []*Signature, message []byte) (bool, error) {
aggSig, err := AggregateSignatures(signatures)
if err != nil {
return false, err
}
aggPK, err := AggregatePublicKeys(publicKeys)
if err != nil {
return false, err
}

return Verify(aggPK, aggSig, message), nil
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

package blstest
package bls

import (
"testing"

"github.com/stretchr/testify/require"

"github.com/ava-labs/avalanchego/utils"
"github.com/ava-labs/avalanchego/utils/crypto/bls"
)

func TestSignatureBytes(t *testing.T) {
Expand All @@ -19,11 +18,11 @@ func TestSignatureBytes(t *testing.T) {

sk := newKey(require)
sig := sign(sk, msg)
sigBytes := bls.SignatureToBytes(sig)
sigBytes := SignatureToBytes(sig)

sig2, err := bls.SignatureFromBytes(sigBytes)
sig2, err := SignatureFromBytes(sigBytes)
require.NoError(err)
sig2Bytes := bls.SignatureToBytes(sig2)
sig2Bytes := SignatureToBytes(sig2)

require.Equal(sig, sig2)
require.Equal(sigBytes, sig2Bytes)
Expand All @@ -36,12 +35,12 @@ func TestAggregateSignaturesNoop(t *testing.T) {

sk := newKey(require)
sig := sign(sk, msg)
sigBytes := bls.SignatureToBytes(sig)
sigBytes := SignatureToBytes(sig)

aggSig, err := bls.AggregateSignatures([]*bls.Signature{sig})
aggSig, err := AggregateSignatures([]*Signature{sig})
require.NoError(err)

aggSigBytes := bls.SignatureToBytes(aggSig)
aggSigBytes := SignatureToBytes(aggSig)
require.NoError(err)

require.Equal(sig, aggSig)
Expand Down
19 changes: 9 additions & 10 deletions utils/crypto/bls/signers/json-rpc/bls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/stretchr/testify/require"

"github.com/ava-labs/avalanchego/utils/crypto/bls"
"github.com/ava-labs/avalanchego/utils/crypto/bls/blstest"
"github.com/ava-labs/avalanchego/utils/crypto/bls/signers/localsigner"
)

Expand Down Expand Up @@ -106,7 +105,7 @@ func TestValidAggregation(t *testing.T) {
signer3.Sign(msg),
}

isValid, err := blstest.AggregateAndVerify(pks, sigs, msg)
isValid, err := bls.AggregateAndVerify(pks, sigs, msg)
require.NoError(err)
require.True(isValid)
}
Expand All @@ -122,7 +121,7 @@ func TestSingleKeyAggregation(t *testing.T) {

sig := signer.Sign(msg)

isValid, err := blstest.AggregateAndVerify(pks, []*bls.Signature{sig}, msg)
isValid, err := bls.AggregateAndVerify(pks, []*bls.Signature{sig}, msg)
require.NoError(err)
require.True(isValid)
}
Expand All @@ -145,7 +144,7 @@ func TestIncorrectMessageAggregation(t *testing.T) {
sk3.Sign(msg),
}

isValid, err := blstest.AggregateAndVerify(pks, signatures, []byte("a different message"))
isValid, err := bls.AggregateAndVerify(pks, signatures, []byte("a different message"))
require.NoError(err)
require.False(isValid)
}
Expand All @@ -169,7 +168,7 @@ func TestOneDifferentMessageAggregation(t *testing.T) {
sk3.Sign(differentMsg),
}

isValid, err := blstest.AggregateAndVerify(pks, signatures, msg)
isValid, err := bls.AggregateAndVerify(pks, signatures, msg)
require.NoError(err)
require.False(isValid)
}
Expand All @@ -193,7 +192,7 @@ func TestOneIncorrectPubKeyAggregation(t *testing.T) {
sk3.Sign(msg),
}

isValid, err := blstest.AggregateAndVerify(pks, signatures, msg)
isValid, err := bls.AggregateAndVerify(pks, signatures, msg)
require.NoError(err)
require.False(isValid)
}
Expand All @@ -214,7 +213,7 @@ func TestMorePubkeysThanSignaturesAggregation(t *testing.T) {
sk2.Sign(msg),
}

isValid, err := blstest.AggregateAndVerify(pks, signatures, msg)
isValid, err := bls.AggregateAndVerify(pks, signatures, msg)
require.NoError(err)
require.False(isValid)
}
Expand All @@ -236,7 +235,7 @@ func TestMoreSignaturesThanPubkeysAggregation(t *testing.T) {
sk3.Sign(msg),
}

isValid, err := blstest.AggregateAndVerify(pks, signatures, msg)
isValid, err := bls.AggregateAndVerify(pks, signatures, msg)
require.NoError(err)
require.False(isValid)
}
Expand All @@ -256,7 +255,7 @@ func TestNoPubkeysAggregation(t *testing.T) {
sk3.Sign(msg),
}

isValid, err := blstest.AggregateAndVerify(nil, signatures, msg)
isValid, err := bls.AggregateAndVerify(nil, signatures, msg)
require.ErrorIs(err, bls.ErrNoPublicKeys)
require.False(isValid)
}
Expand All @@ -272,7 +271,7 @@ func TestNoSignaturesAggregation(t *testing.T) {

msg := []byte("TestNoSignaturesAggregation json-rpc")

isValid, err := blstest.AggregateAndVerify(pks, nil, msg)
isValid, err := bls.AggregateAndVerify(pks, nil, msg)
require.ErrorIs(err, bls.ErrNoSignatures)
require.False(isValid)
}
Expand Down
Loading

0 comments on commit ad10cb2

Please sign in to comment.