prysm-pulse/shared/bls/bls_test.go
Nishant Das 24583864b4 Change BLS Library to Herumi (#3752)
* change to herumi's bls

* change alias

* change to better

* add benchmark

* build

* change to bazel fork

* fix prefix

* make it work with library

* update to latest

* change again

* add import

* update to latest

* add sha commit

* new static lib with groups swapped

* using herumis new lib

* fix dep paths in c headers

* update again

* new changes

* fix commit

* fix serialization

* comment

* fix test

* fix to herumis latest version

* fix test

* fix benchmarks

* add new workspace

* change commit and remove init

* get test to pass

* remove parameter

* remove reverse byte order

* make gazelle happy

* set pure to off

* fix failing tests

* remove old ref

* use HashWithDomain functions

* update to latest version

* clean up

* gaz

* add back removed code

* switch off pure
2019-11-14 09:51:42 -06:00

103 lines
2.6 KiB
Go

package bls_test
import (
"bytes"
"testing"
"github.com/prysmaticlabs/prysm/shared/bls"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
)
func TestMarshalUnmarshal(t *testing.T) {
b := bls.RandKey().Marshal()
b32 := bytesutil.ToBytes32(b)
pk, err := bls.SecretKeyFromBytes(b32[:])
if err != nil {
t.Fatal(err)
}
pk2, err := bls.SecretKeyFromBytes(b32[:])
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(pk.Marshal(), pk2.Marshal()) {
t.Errorf("Keys not equal, received %#x == %#x", pk.Marshal(), pk2.Marshal())
}
}
func TestSignVerify(t *testing.T) {
priv := bls.RandKey()
pub := priv.PublicKey()
msg := []byte("hello")
sig := priv.Sign(msg, 0)
if !sig.Verify(msg, pub, 0) {
t.Error("Signature did not verify")
}
}
func TestVerifyAggregate(t *testing.T) {
pubkeys := make([]*bls.PublicKey, 0, 100)
sigs := make([]*bls.Signature, 0, 100)
var msgs [][32]byte
for i := 0; i < 100; i++ {
msg := [32]byte{'h', 'e', 'l', 'l', 'o', byte(i)}
priv := bls.RandKey()
pub := priv.PublicKey()
sig := priv.Sign(msg[:], 0)
pubkeys = append(pubkeys, pub)
sigs = append(sigs, sig)
msgs = append(msgs, msg)
}
aggSig := bls.AggregateSignatures(sigs)
if !aggSig.VerifyAggregate(pubkeys, msgs, 0) {
t.Error("Signature did not verify")
}
}
func TestVerifyAggregateCommon(t *testing.T) {
pubkeys := make([]*bls.PublicKey, 0, 100)
sigs := make([]*bls.Signature, 0, 100)
msg := [32]byte{'h', 'e', 'l', 'l', 'o'}
for i := 0; i < 100; i++ {
priv := bls.RandKey()
pub := priv.PublicKey()
sig := priv.Sign(msg[:], 0)
pubkeys = append(pubkeys, pub)
sigs = append(sigs, sig)
}
aggSig := bls.AggregateSignatures(sigs)
if !aggSig.VerifyAggregateCommon(pubkeys, msg, 0) {
t.Error("Signature did not verify")
}
}
func TestVerifyAggregate_ReturnsFalseOnEmptyPubKeyList(t *testing.T) {
var pubkeys []*bls.PublicKey
sigs := make([]*bls.Signature, 0, 100)
msg := [32]byte{'h', 'e', 'l', 'l', 'o'}
aggSig := bls.AggregateSignatures(sigs)
if aggSig.VerifyAggregateCommon(pubkeys, msg, 0 /*domain*/) != false {
t.Error("Expected VerifyAggregate to return false with empty input " +
"of public keys.")
}
}
func TestComputeDomain_OK(t *testing.T) {
tests := []struct {
epoch uint64
domainType uint64
domain uint64
}{
{epoch: 1, domainType: 4, domain: 4},
{epoch: 2, domainType: 4, domain: 4},
{epoch: 2, domainType: 5, domain: 5},
{epoch: 3, domainType: 4, domain: 4},
{epoch: 3, domainType: 5, domain: 5},
}
for _, tt := range tests {
if bls.ComputeDomain(bytesutil.Bytes4(tt.domainType)) != tt.domain {
t.Errorf("wanted domain version: %d, got: %d", tt.domain, bls.ComputeDomain(bytesutil.Bytes4(tt.domainType)))
}
}
}