2020-06-25 00:47:51 +00:00
package herumi_test
import (
"bytes"
"errors"
"testing"
"github.com/prysmaticlabs/prysm/shared/bls/herumi"
)
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 {
if err == nil {
t . Errorf ( "No error returned: expected %v" , test . err )
} else if test . err . Error ( ) != err . Error ( ) {
t . Errorf ( "Unexpected error returned: expected %v, received %v" , test . err , err )
}
} else {
if err != nil {
t . Errorf ( "Unexpected error returned: %v" , err )
2020-08-10 16:16:45 +00:00
} else if ! bytes . Equal ( res . Marshal ( ) , test . input ) {
t . Errorf ( "Unexpected result: expected %x, received %x" , test . input , res . Marshal ( ) )
2020-06-25 00:47:51 +00:00
}
}
} )
}
}
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" )
}
}