github.com/prysmaticlabs/prysm@v1.4.4/shared/bls/blst/secret_key_test.go (about) 1 // +build linux,amd64 linux,arm64 darwin,amd64 windows,amd64 2 // +build !blst_disabled 3 4 package blst_test 5 6 import ( 7 "bytes" 8 "crypto/rand" 9 "errors" 10 "testing" 11 12 "github.com/prysmaticlabs/prysm/shared/bls/blst" 13 "github.com/prysmaticlabs/prysm/shared/bls/common" 14 "github.com/prysmaticlabs/prysm/shared/bytesutil" 15 "github.com/prysmaticlabs/prysm/shared/testutil/assert" 16 "github.com/prysmaticlabs/prysm/shared/testutil/require" 17 ) 18 19 func TestMarshalUnmarshal(t *testing.T) { 20 priv, err := blst.RandKey() 21 require.NoError(t, err) 22 b := priv.Marshal() 23 b32 := bytesutil.ToBytes32(b) 24 pk, err := blst.SecretKeyFromBytes(b32[:]) 25 require.NoError(t, err) 26 pk2, err := blst.SecretKeyFromBytes(b32[:]) 27 require.NoError(t, err) 28 assert.DeepEqual(t, pk.Marshal(), pk2.Marshal(), "Keys not equal") 29 } 30 31 func TestSecretKeyFromBytes(t *testing.T) { 32 tests := []struct { 33 name string 34 input []byte 35 err error 36 }{ 37 { 38 name: "Nil", 39 err: errors.New("secret key must be 32 bytes"), 40 }, 41 { 42 name: "Empty", 43 input: []byte{}, 44 err: errors.New("secret key must be 32 bytes"), 45 }, 46 { 47 name: "Short", 48 input: []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 49 err: errors.New("secret key must be 32 bytes"), 50 }, 51 { 52 name: "Long", 53 input: []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 54 err: errors.New("secret key must be 32 bytes"), 55 }, 56 { 57 name: "Bad", 58 input: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 59 err: common.ErrSecretUnmarshal, 60 }, 61 { 62 name: "Good", 63 input: []byte{0x25, 0x29, 0x5f, 0x0d, 0x1d, 0x59, 0x2a, 0x90, 0xb3, 0x33, 0xe2, 0x6e, 0x85, 0x14, 0x97, 0x08, 0x20, 0x8e, 0x9f, 0x8e, 0x8b, 0xc1, 0x8f, 0x6c, 0x77, 0xbd, 0x62, 0xf8, 0xad, 0x7a, 0x68, 0x66}, 64 }, 65 } 66 67 for _, test := range tests { 68 t.Run(test.name, func(t *testing.T) { 69 res, err := blst.SecretKeyFromBytes(test.input) 70 if test.err != nil { 71 assert.NotEqual(t, nil, err, "No error returned") 72 assert.ErrorContains(t, test.err.Error(), err, "Unexpected error returned") 73 } else { 74 assert.NoError(t, err) 75 assert.DeepEqual(t, 0, bytes.Compare(res.Marshal(), test.input)) 76 } 77 }) 78 } 79 } 80 81 func TestSerialize(t *testing.T) { 82 rk, err := blst.RandKey() 83 require.NoError(t, err) 84 b := rk.Marshal() 85 86 _, err = blst.SecretKeyFromBytes(b) 87 assert.NoError(t, err) 88 } 89 90 func TestZeroKey(t *testing.T) { 91 // Is Zero 92 zKey := [32]byte{} 93 assert.Equal(t, true, blst.IsZero(zKey[:])) 94 95 // Is Not Zero 96 _, err := rand.Read(zKey[:]) 97 assert.NoError(t, err) 98 assert.Equal(t, false, blst.IsZero(zKey[:])) 99 }