package herumi_test import ( "bytes" "errors" "testing" "github.com/prysmaticlabs/prysm/shared/bls/herumi" "github.com/prysmaticlabs/prysm/shared/testutil/assert" ) func TestPublicKeyFromBytes(t *testing.T) { tests := []struct { name string input []byte err error }{ { name: "Nil", err: errors.New("public key must be 48 bytes"), }, { name: "Empty", input: []byte{}, err: errors.New("public key must be 48 bytes"), }, { name: "Short", 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}, err: errors.New("public key must be 48 bytes"), }, { name: "Long", 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}, err: errors.New("public key must be 48 bytes"), }, { name: "Bad", 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}, err: errors.New("could not unmarshal bytes into public key: err blsPublicKeyDeserialize 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"), }, { name: "Good", 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}, }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { res, err := herumi.PublicKeyFromBytes(test.input) if test.err != nil { assert.ErrorContains(t, test.err.Error(), err) } else { assert.NoError(t, err) assert.DeepEqual(t, test.input, res.Marshal()) } }) } } func TestPublicKey_Copy(t *testing.T) { pubkeyA := herumi.RandKey().PublicKey() pubkeyBytes := pubkeyA.Marshal() pubkeyB := pubkeyA.Copy() pubkeyB.Aggregate(herumi.RandKey().PublicKey()) if !bytes.Equal(pubkeyA.Marshal(), pubkeyBytes) { t.Fatal("Pubkey was mutated after copy") } }