From 966647635c15fc388e9da6fce12b539418f4066e Mon Sep 17 00:00:00 2001 From: Jimmy Hu Date: Mon, 12 Nov 2018 22:14:19 +0800 Subject: [PATCH] bls: add benchmarks --- bls/bls_test.go | 54 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/bls/bls_test.go b/bls/bls_test.go index a9d3c51..9af1a40 100644 --- a/bls/bls_test.go +++ b/bls/bls_test.go @@ -96,3 +96,57 @@ func TestKnownAnswer(t *testing.T) { t.Fatalf("got %s, want %s", actual, expected) } } + +func generateTestcases(n int) (msgs [][]byte, pubs []*PublicKey, prvs []*PrivateKey, sigs []Signature) { + msgs = make([][]byte, 0, n) + pubs = make([]*PublicKey, 0, n) + prvs = make([]*PrivateKey, 0, n) + sigs = make([]Signature, 0, n) + for i := 0; i < n; i++ { + msg := randomMessage() + msgs = append(msgs, msg) + pub, prv, err := GenerateKey(rand.Reader) + if err != nil { + panic(err) + } + pubs = append(pubs, pub) + prvs = append(prvs, prv) + + sig := Sign(prv, msg) + sigs = append(sigs, sig) + } + return +} + +func BenchmarkVerifyAggregateSig(b *testing.B) { + msgs, pubs, _, sigs := generateTestcases(100) + b.Run("AggregateSignature", func(b *testing.B) { + for i := 0; i < b.N; i++ { + Aggregate(sigs...) + } + }) + sig := Aggregate(sigs...) + b.Run("VerifyAggregateSignature", func(b *testing.B) { + for i := 0; i < b.N; i++ { + Verify(pubs, msgs, sig) + } + }) +} + +func BenchmarkVerifySig(b *testing.B) { + n := 100 + msgs, pubs, _, sigs := generateTestcases(n) + pk := make([][]*PublicKey, n) + msg := make([][][]byte, n) + for i := 0; i < n; i++ { + pk[i] = []*PublicKey{pubs[i]} + msg[i] = [][]byte{msgs[i]} + } + b.ResetTimer() + for i := 0; i < b.N; i++ { + for j := 0; j < n; j++ { + Verify(pk[j], msg[j], sigs[j]) + } + } + b.StopTimer() +}