Add quick bls benchmark (#3148)

This commit is contained in:
Preston Van Loon
2019-08-05 20:11:38 -04:00
committed by terence tsao
parent 9ec54ae432
commit cb5c920502
3 changed files with 74 additions and 0 deletions

View File

@@ -19,3 +19,24 @@ go_test(
embed = [":go_default_library"],
deps = ["//shared/bytesutil:go_default_library"],
)
# gazelle:exclude bls_benchmark_test.go
go_test(
name = "go_benchmark_test",
size = "small",
srcs = ["bls_benchmark_test.go"],
args = [
"-test.bench=.",
"-test.benchmem",
"-test.v",
],
local = True,
tags = [
"benchmark",
"manual",
"no-cache",
],
deps = [
"//shared/bls:go_default_library",
],
)

View File

@@ -124,6 +124,9 @@ func (s *Signature) Marshal() []byte {
func AggregateSignatures(sigs []*Signature) *Signature {
var ss []*g1.Signature
for _, v := range sigs {
if v == nil {
continue
}
ss = append(ss, v.val)
}
return &Signature{val: g1.AggregateSignatures(ss)}

View File

@@ -0,0 +1,50 @@
package bls_test
import (
"crypto/rand"
"testing"
"github.com/prysmaticlabs/prysm/shared/bls"
)
func BenchmarkSignature_Verify(b *testing.B) {
sk, err := bls.RandKey(rand.Reader)
if err != nil {
b.Fatal(err)
}
msg := []byte("Some msg")
domain := uint64(42)
sig := sk.Sign(msg, domain)
b.ResetTimer()
for i := 0; i < b.N; i++ {
if !sig.Verify(msg, sk.PublicKey(), domain) {
b.Fatal("could not verify sig")
}
}
}
func BenchmarkSignature_VerifyAggregate(b *testing.B) {
sigN := 128 // MAX_ATTESTATIONS per block.
msg := []byte("signed message")
domain := uint64(0)
var aggregated *bls.Signature
var pks []*bls.PublicKey
for i := 0; i < sigN; i++ {
sk, err := bls.RandKey(rand.Reader)
if err != nil {
b.Fatal(err)
}
sig := sk.Sign(msg, domain)
aggregated = bls.AggregateSignatures([]*bls.Signature{aggregated, sig})
pks = append(pks, sk.PublicKey())
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
if !aggregated.VerifyAggregate(pks, msg, domain) {
b.Fatal("could not verify aggregate sig")
}
}
}