2019-02-19 15:09:50 +00:00
|
|
|
package bls_test
|
2019-02-15 18:31:07 +00:00
|
|
|
|
|
|
|
import (
|
2019-02-19 15:09:50 +00:00
|
|
|
"bytes"
|
|
|
|
"crypto/rand"
|
2019-02-15 18:31:07 +00:00
|
|
|
"testing"
|
2019-02-19 15:09:50 +00:00
|
|
|
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/bls"
|
2019-03-03 17:31:29 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/bytesutil"
|
2019-02-15 18:31:07 +00:00
|
|
|
)
|
|
|
|
|
2019-02-19 15:09:50 +00:00
|
|
|
func TestMarshalUnmarshal(t *testing.T) {
|
|
|
|
b := []byte("hi")
|
|
|
|
b32 := bytesutil.ToBytes32(b)
|
|
|
|
pk, err := bls.SecretKeyFromBytes(b32[:])
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2019-02-15 18:31:07 +00:00
|
|
|
}
|
2019-02-19 15:09:50 +00:00
|
|
|
pk2, err := bls.SecretKeyFromBytes(b32[:])
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2019-02-15 18:31:07 +00:00
|
|
|
}
|
2019-02-19 15:09:50 +00:00
|
|
|
if !bytes.Equal(pk.Marshal(), pk2.Marshal()) {
|
|
|
|
t.Errorf("Keys not equal, received %#x == %#x", pk.Marshal(), pk2.Marshal())
|
2019-02-15 18:31:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-19 15:09:50 +00:00
|
|
|
func TestSignVerify(t *testing.T) {
|
|
|
|
priv, _ := bls.RandKey(rand.Reader)
|
|
|
|
pub := priv.PublicKey()
|
|
|
|
msg := []byte("hello")
|
|
|
|
sig := priv.Sign(msg, 0)
|
2019-03-11 00:02:47 +00:00
|
|
|
if !sig.Verify(msg, pub, 0) {
|
2019-02-19 15:09:50 +00:00
|
|
|
t.Error("Signature did not verify")
|
2019-02-15 18:31:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-19 15:09:50 +00:00
|
|
|
func TestVerifyAggregate(t *testing.T) {
|
|
|
|
pubkeys := make([]*bls.PublicKey, 0, 100)
|
|
|
|
sigs := make([]*bls.Signature, 0, 100)
|
|
|
|
msg := []byte("hello")
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
priv, _ := bls.RandKey(rand.Reader)
|
|
|
|
pub := priv.PublicKey()
|
|
|
|
sig := priv.Sign(msg, 0)
|
|
|
|
pubkeys = append(pubkeys, pub)
|
|
|
|
sigs = append(sigs, sig)
|
2019-02-15 18:31:07 +00:00
|
|
|
}
|
2019-02-19 15:09:50 +00:00
|
|
|
aggSig := bls.AggregateSignatures(sigs)
|
|
|
|
if !aggSig.VerifyAggregate(pubkeys, msg, 0) {
|
|
|
|
t.Error("Signature did not verify")
|
2019-02-15 18:31:07 +00:00
|
|
|
}
|
|
|
|
}
|