2020-10-30 19:06:33 +00:00
|
|
|
package bls
|
|
|
|
|
|
|
|
import (
|
2021-04-27 06:46:47 +00:00
|
|
|
"math/big"
|
2020-10-30 19:06:33 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/bls/common"
|
2021-04-27 06:46:47 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/rand"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
|
2020-10-30 19:06:33 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDisallowZeroSecretKeys(t *testing.T) {
|
|
|
|
t.Run("blst", func(t *testing.T) {
|
2020-11-17 04:12:23 +00:00
|
|
|
// Blst does a zero check on the key during deserialization.
|
2020-10-30 19:06:33 +00:00
|
|
|
_, err := SecretKeyFromBytes(common.ZeroSecretKey[:])
|
2020-11-17 04:12:23 +00:00
|
|
|
require.Equal(t, common.ErrSecretUnmarshal, err)
|
2020-10-30 19:06:33 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDisallowZeroPublicKeys(t *testing.T) {
|
|
|
|
t.Run("blst", func(t *testing.T) {
|
|
|
|
_, err := PublicKeyFromBytes(common.InfinitePublicKey[:])
|
|
|
|
require.Equal(t, common.ErrInfinitePubKey, err)
|
|
|
|
})
|
|
|
|
}
|
2020-11-17 04:12:23 +00:00
|
|
|
|
|
|
|
func TestDisallowZeroPublicKeys_AggregatePubkeys(t *testing.T) {
|
|
|
|
t.Run("blst", func(t *testing.T) {
|
|
|
|
_, err := AggregatePublicKeys([][]byte{common.InfinitePublicKey[:], common.InfinitePublicKey[:]})
|
|
|
|
require.Equal(t, common.ErrInfinitePubKey, err)
|
|
|
|
})
|
|
|
|
}
|
2021-04-27 06:46:47 +00:00
|
|
|
|
|
|
|
func TestValidateSecretKeyString(t *testing.T) {
|
|
|
|
t.Run("blst", func(t *testing.T) {
|
|
|
|
zeroNum := new(big.Int).SetUint64(0)
|
|
|
|
_, err := SecretKeyFromBigNum(zeroNum.String())
|
|
|
|
assert.ErrorContains(t, "provided big number string sets to a key unequal to 32 bytes", err)
|
|
|
|
|
|
|
|
rGen := rand.NewDeterministicGenerator()
|
|
|
|
|
|
|
|
randBytes := make([]byte, 40)
|
|
|
|
n, err := rGen.Read(randBytes)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, n, len(randBytes))
|
|
|
|
rBigNum := new(big.Int).SetBytes(randBytes)
|
|
|
|
|
|
|
|
// Expect larger than expected key size to fail.
|
|
|
|
_, err = SecretKeyFromBigNum(rBigNum.String())
|
|
|
|
assert.ErrorContains(t, "provided big number string sets to a key unequal to 32 bytes", err)
|
|
|
|
|
|
|
|
key, err := RandKey()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
rBigNum = new(big.Int).SetBytes(key.Marshal())
|
|
|
|
|
|
|
|
// Expect correct size to pass.
|
|
|
|
_, err = SecretKeyFromBigNum(rBigNum.String())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
}
|