github.com/prysmaticlabs/prysm@v1.4.4/shared/bls/blst/public_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 "errors" 9 "testing" 10 11 "github.com/prysmaticlabs/prysm/shared/bls/blst" 12 "github.com/prysmaticlabs/prysm/shared/testutil/assert" 13 "github.com/prysmaticlabs/prysm/shared/testutil/require" 14 ) 15 16 func TestPublicKeyFromBytes(t *testing.T) { 17 tests := []struct { 18 name string 19 input []byte 20 err error 21 }{ 22 { 23 name: "Nil", 24 err: errors.New("public key must be 48 bytes"), 25 }, 26 { 27 name: "Empty", 28 input: []byte{}, 29 err: errors.New("public key must be 48 bytes"), 30 }, 31 { 32 name: "Short", 33 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, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 34 err: errors.New("public key must be 48 bytes"), 35 }, 36 { 37 name: "Long", 38 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, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 39 err: errors.New("public key must be 48 bytes"), 40 }, 41 { 42 name: "Bad", 43 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, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 44 err: errors.New("could not unmarshal bytes into public key"), 45 }, 46 { 47 name: "Good", 48 input: []byte{0xa9, 0x9a, 0x76, 0xed, 0x77, 0x96, 0xf7, 0xbe, 0x22, 0xd5, 0xb7, 0xe8, 0x5d, 0xee, 0xb7, 0xc5, 0x67, 0x7e, 0x88, 0xe5, 0x11, 0xe0, 0xb3, 0x37, 0x61, 0x8f, 0x8c, 0x4e, 0xb6, 0x13, 0x49, 0xb4, 0xbf, 0x2d, 0x15, 0x3f, 0x64, 0x9f, 0x7b, 0x53, 0x35, 0x9f, 0xe8, 0xb9, 0x4a, 0x38, 0xe4, 0x4c}, 49 }, 50 } 51 52 for _, test := range tests { 53 t.Run(test.name, func(t *testing.T) { 54 res, err := blst.PublicKeyFromBytes(test.input) 55 if test.err != nil { 56 assert.NotEqual(t, nil, err, "No error returned") 57 assert.ErrorContains(t, test.err.Error(), err, "Unexpected error returned") 58 } else { 59 assert.NoError(t, err) 60 assert.DeepEqual(t, 0, bytes.Compare(res.Marshal(), test.input)) 61 } 62 }) 63 } 64 } 65 66 func TestPublicKey_Copy(t *testing.T) { 67 priv, err := blst.RandKey() 68 require.NoError(t, err) 69 pubkeyA := priv.PublicKey() 70 pubkeyBytes := pubkeyA.Marshal() 71 72 pubkeyB := pubkeyA.Copy() 73 priv2, err := blst.RandKey() 74 require.NoError(t, err) 75 pubkeyB.Aggregate(priv2.PublicKey()) 76 77 require.DeepEqual(t, pubkeyA.Marshal(), pubkeyBytes, "Pubkey was mutated after copy") 78 } 79 80 func TestPublicKeysEmpty(t *testing.T) { 81 pubs := [][]byte{} 82 _, err := blst.AggregatePublicKeys(pubs) 83 require.ErrorContains(t, "nil or empty public keys", err) 84 }