github.com/prysmaticlabs/prysm@v1.4.4/shared/bls/bls_test.go (about)

     1  package bls
     2  
     3  import (
     4  	"math/big"
     5  	"testing"
     6  
     7  	"github.com/prysmaticlabs/prysm/shared/bls/common"
     8  	"github.com/prysmaticlabs/prysm/shared/rand"
     9  	"github.com/prysmaticlabs/prysm/shared/testutil/assert"
    10  	"github.com/prysmaticlabs/prysm/shared/testutil/require"
    11  )
    12  
    13  func TestDisallowZeroSecretKeys(t *testing.T) {
    14  	t.Run("blst", func(t *testing.T) {
    15  		// Blst does a zero check on the key during deserialization.
    16  		_, err := SecretKeyFromBytes(common.ZeroSecretKey[:])
    17  		require.Equal(t, common.ErrSecretUnmarshal, err)
    18  	})
    19  }
    20  
    21  func TestDisallowZeroPublicKeys(t *testing.T) {
    22  	t.Run("blst", func(t *testing.T) {
    23  		_, err := PublicKeyFromBytes(common.InfinitePublicKey[:])
    24  		require.Equal(t, common.ErrInfinitePubKey, err)
    25  	})
    26  }
    27  
    28  func TestDisallowZeroPublicKeys_AggregatePubkeys(t *testing.T) {
    29  	t.Run("blst", func(t *testing.T) {
    30  		_, err := AggregatePublicKeys([][]byte{common.InfinitePublicKey[:], common.InfinitePublicKey[:]})
    31  		require.Equal(t, common.ErrInfinitePubKey, err)
    32  	})
    33  }
    34  
    35  func TestValidateSecretKeyString(t *testing.T) {
    36  	t.Run("blst", func(t *testing.T) {
    37  		zeroNum := new(big.Int).SetUint64(0)
    38  		_, err := SecretKeyFromBigNum(zeroNum.String())
    39  		assert.ErrorContains(t, "provided big number string sets to a key unequal to 32 bytes", err)
    40  
    41  		rGen := rand.NewDeterministicGenerator()
    42  
    43  		randBytes := make([]byte, 40)
    44  		n, err := rGen.Read(randBytes)
    45  		assert.NoError(t, err)
    46  		assert.Equal(t, n, len(randBytes))
    47  		rBigNum := new(big.Int).SetBytes(randBytes)
    48  
    49  		// Expect larger than expected key size to fail.
    50  		_, err = SecretKeyFromBigNum(rBigNum.String())
    51  		assert.ErrorContains(t, "provided big number string sets to a key unequal to 32 bytes", err)
    52  
    53  		key, err := RandKey()
    54  		assert.NoError(t, err)
    55  		rBigNum = new(big.Int).SetBytes(key.Marshal())
    56  
    57  		// Expect correct size to pass.
    58  		_, err = SecretKeyFromBigNum(rBigNum.String())
    59  		assert.NoError(t, err)
    60  	})
    61  }