From ad10cb2a585fb059382e3fc456fd9e091ed9099e Mon Sep 17 00:00:00 2001 From: Richard Pringle Date: Tue, 14 Jan 2025 13:31:05 -0500 Subject: [PATCH] Move bls-tests back to bls package --- .../bls/{blstest => }/bls_benchmark_test.go | 51 ++++++------------- utils/crypto/bls/bls_test.go | 13 ----- utils/crypto/bls/blstest/blstest.go | 21 -------- utils/crypto/bls/signature.go | 13 +++++ .../bls/{blstest => }/signature_test.go | 15 +++--- utils/crypto/bls/signers/json-rpc/bls_test.go | 19 ++++--- .../bls/signers/localsigner/bls_test.go | 19 ++++--- 7 files changed, 53 insertions(+), 98 deletions(-) rename utils/crypto/bls/{blstest => }/bls_benchmark_test.go (57%) delete mode 100644 utils/crypto/bls/blstest/blstest.go rename utils/crypto/bls/{blstest => }/signature_test.go (67%) diff --git a/utils/crypto/bls/blstest/bls_benchmark_test.go b/utils/crypto/bls/bls_benchmark_test.go similarity index 57% rename from utils/crypto/bls/blstest/bls_benchmark_test.go rename to utils/crypto/bls/bls_benchmark_test.go index 751403b71a68..33caa49eeb59 100644 --- a/utils/crypto/bls/blstest/bls_benchmark_test.go +++ b/utils/crypto/bls/bls_benchmark_test.go @@ -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) { message := utils.RandomBytes(messageSize) @@ -47,14 +26,14 @@ 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)) @@ -62,10 +41,10 @@ func BenchmarkAggregatePublicKeys(b *testing.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) } }) @@ -79,7 +58,7 @@ func BenchmarkPublicKeyToCompressedBytes(b *testing.B) { b.ResetTimer() for range b.N { - bls.PublicKeyToCompressedBytes(pk) + PublicKeyToCompressedBytes(pk) } } @@ -87,11 +66,11 @@ 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) } } @@ -102,7 +81,7 @@ func BenchmarkPublicKeyToUncompressedBytes(b *testing.B) { b.ResetTimer() for range b.N { - bls.PublicKeyToUncompressedBytes(pk) + PublicKeyToUncompressedBytes(pk) } } @@ -110,11 +89,11 @@ 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) } } @@ -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) } } diff --git a/utils/crypto/bls/bls_test.go b/utils/crypto/bls/bls_test.go index 4365d0d87407..8ab0b50ad175 100644 --- a/utils/crypto/bls/bls_test.go +++ b/utils/crypto/bls/bls_test.go @@ -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) diff --git a/utils/crypto/bls/blstest/blstest.go b/utils/crypto/bls/blstest/blstest.go deleted file mode 100644 index 88bc20c4bc99..000000000000 --- a/utils/crypto/bls/blstest/blstest.go +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. -// See the file LICENSE for licensing terms. - -package blstest - -import ( - "github.com/ava-labs/avalanchego/utils/crypto/bls" -) - -func AggregateAndVerify(publicKeys []*bls.PublicKey, signatures []*bls.Signature, message []byte) (bool, error) { - aggSig, err := bls.AggregateSignatures(signatures) - if err != nil { - return false, err - } - aggPK, err := bls.AggregatePublicKeys(publicKeys) - if err != nil { - return false, err - } - - return bls.Verify(aggPK, aggSig, message), nil -} diff --git a/utils/crypto/bls/signature.go b/utils/crypto/bls/signature.go index 51a3c11f0da6..dd75e9df5078 100644 --- a/utils/crypto/bls/signature.go +++ b/utils/crypto/bls/signature.go @@ -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 +} diff --git a/utils/crypto/bls/blstest/signature_test.go b/utils/crypto/bls/signature_test.go similarity index 67% rename from utils/crypto/bls/blstest/signature_test.go rename to utils/crypto/bls/signature_test.go index 843f38844515..74847dfdc436 100644 --- a/utils/crypto/bls/blstest/signature_test.go +++ b/utils/crypto/bls/signature_test.go @@ -1,7 +1,7 @@ // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. // See the file LICENSE for licensing terms. -package blstest +package bls import ( "testing" @@ -9,7 +9,6 @@ import ( "github.com/stretchr/testify/require" "github.com/ava-labs/avalanchego/utils" - "github.com/ava-labs/avalanchego/utils/crypto/bls" ) func TestSignatureBytes(t *testing.T) { @@ -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) @@ -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) diff --git a/utils/crypto/bls/signers/json-rpc/bls_test.go b/utils/crypto/bls/signers/json-rpc/bls_test.go index 4cac9a714e24..05540cd71d39 100644 --- a/utils/crypto/bls/signers/json-rpc/bls_test.go +++ b/utils/crypto/bls/signers/json-rpc/bls_test.go @@ -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" ) @@ -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) } @@ -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) } @@ -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) } @@ -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) } @@ -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) } @@ -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) } @@ -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) } @@ -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) } @@ -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) } diff --git a/utils/crypto/bls/signers/localsigner/bls_test.go b/utils/crypto/bls/signers/localsigner/bls_test.go index 7602e7683d03..a02071b23601 100644 --- a/utils/crypto/bls/signers/localsigner/bls_test.go +++ b/utils/crypto/bls/signers/localsigner/bls_test.go @@ -9,7 +9,6 @@ import ( "github.com/stretchr/testify/require" "github.com/ava-labs/avalanchego/utils/crypto/bls" - "github.com/ava-labs/avalanchego/utils/crypto/bls/blstest" ) func NewKeyPair(require *require.Assertions) (*LocalSigner, *bls.PublicKey) { @@ -86,7 +85,7 @@ func TestValidAggregation(t *testing.T) { sk3.Sign(msg), } - isValid, err := blstest.AggregateAndVerify(pks, sigs, msg) + isValid, err := bls.AggregateAndVerify(pks, sigs, msg) require.NoError(err) require.True(isValid) } @@ -101,7 +100,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) } @@ -121,7 +120,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) } @@ -143,7 +142,7 @@ func TestDifferentMessageAggregation(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) } @@ -165,7 +164,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) } @@ -185,7 +184,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) } @@ -206,7 +205,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) } @@ -225,7 +224,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) } @@ -240,7 +239,7 @@ func TestNoSignaturesAggregation(t *testing.T) { msg := []byte("TestNoSignaturesAggregation local signer") - isValid, err := blstest.AggregateAndVerify(pks, nil, msg) + isValid, err := bls.AggregateAndVerify(pks, nil, msg) require.ErrorIs(err, bls.ErrNoSignatures) require.False(isValid) }