2019-08-06 00:11:38 +00:00
|
|
|
package bls_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2019-12-03 20:29:05 +00:00
|
|
|
bls2 "github.com/herumi/bls-eth-go-binary/bls"
|
2019-08-06 00:11:38 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/bls"
|
2019-12-03 20:29:05 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/hashutil"
|
2019-08-06 00:11:38 +00:00
|
|
|
)
|
|
|
|
|
2019-12-03 20:29:05 +00:00
|
|
|
func BenchmarkPairing(b *testing.B) {
|
2020-04-14 16:41:09 +00:00
|
|
|
if err := bls2.Init(bls2.BLS12_381); err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
2020-05-07 12:05:53 +00:00
|
|
|
if err := bls2.SetETHmode(bls2.EthModeDraft05); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-12-03 20:29:05 +00:00
|
|
|
newGt := &bls2.GT{}
|
|
|
|
newG1 := &bls2.G1{}
|
|
|
|
newG2 := &bls2.G2{}
|
|
|
|
|
|
|
|
newGt.SetInt64(10)
|
|
|
|
hash := hashutil.Hash([]byte{})
|
|
|
|
err := newG1.HashAndMapTo(hash[:])
|
2019-11-15 16:27:23 +00:00
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
2019-12-03 20:29:05 +00:00
|
|
|
err = newG2.HashAndMapTo(hash[:])
|
|
|
|
if err != nil {
|
|
|
|
b.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
bls2.Pairing(newGt, newG1, newG2)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
func BenchmarkSignature_Verify(b *testing.B) {
|
|
|
|
sk := bls.RandKey()
|
|
|
|
|
2019-08-06 00:11:38 +00:00
|
|
|
msg := []byte("Some msg")
|
2020-05-07 12:05:53 +00:00
|
|
|
sig := sk.Sign(msg)
|
2019-08-06 00:11:38 +00:00
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2020-05-25 21:08:12 +00:00
|
|
|
if !sig.Verify(sk.PublicKey(), msg) {
|
2019-08-06 00:11:38 +00:00
|
|
|
b.Fatal("could not verify sig")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-07 12:05:53 +00:00
|
|
|
func BenchmarkSignature_AggregateVerify(b *testing.B) {
|
2019-08-06 00:11:38 +00:00
|
|
|
sigN := 128 // MAX_ATTESTATIONS per block.
|
|
|
|
|
|
|
|
var pks []*bls.PublicKey
|
2020-05-07 12:05:53 +00:00
|
|
|
var sigs []*bls.Signature
|
|
|
|
var msgs [][32]byte
|
2019-08-06 00:11:38 +00:00
|
|
|
for i := 0; i < sigN; i++ {
|
2020-05-07 12:05:53 +00:00
|
|
|
msg := [32]byte{'s', 'i', 'g', 'n', 'e', 'd', byte(i)}
|
2019-12-03 20:29:05 +00:00
|
|
|
sk := bls.RandKey()
|
2020-05-07 12:05:53 +00:00
|
|
|
sig := sk.Sign(msg[:])
|
2019-08-06 00:11:38 +00:00
|
|
|
pks = append(pks, sk.PublicKey())
|
2020-05-07 12:05:53 +00:00
|
|
|
sigs = append(sigs, sig)
|
|
|
|
msgs = append(msgs, msg)
|
2019-08-06 00:11:38 +00:00
|
|
|
}
|
2020-05-07 12:05:53 +00:00
|
|
|
aggregated := bls.Aggregate(sigs)
|
2019-08-06 00:11:38 +00:00
|
|
|
|
|
|
|
b.ResetTimer()
|
2019-10-07 07:50:58 +00:00
|
|
|
b.ReportAllocs()
|
2019-08-06 00:11:38 +00:00
|
|
|
for i := 0; i < b.N; i++ {
|
2020-05-07 12:05:53 +00:00
|
|
|
if !aggregated.AggregateVerify(pks, msgs) {
|
2019-08-06 00:11:38 +00:00
|
|
|
b.Fatal("could not verify aggregate sig")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-22 23:40:39 +00:00
|
|
|
|
|
|
|
func BenchmarkSecretKey_Marshal(b *testing.B) {
|
|
|
|
key := bls.RandKey()
|
|
|
|
d := key.Marshal()
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2020-05-07 12:05:53 +00:00
|
|
|
_, err := bls.SecretKeyFromBytes(d)
|
|
|
|
_ = err
|
2020-03-22 23:40:39 +00:00
|
|
|
}
|
|
|
|
}
|