Skip to content

Commit

Permalink
chore: add benchmark for bls package
Browse files Browse the repository at this point in the history
  • Loading branch information
Ja7ad committed Sep 28, 2024
1 parent c8dda2d commit dbfe34f
Showing 1 changed file with 93 additions and 0 deletions.
93 changes: 93 additions & 0 deletions crypto/bls/bls_bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package bls_test

import (
"crypto/rand"
"github.com/pactus-project/pactus/crypto/bls"
"testing"
)

func BenchmarkEncode(b *testing.B) {
b.ReportAllocs()

buf := make([]byte, bls.PrivateKeySize)
_, err := rand.Read(buf)
if err != nil {
b.Fatal(err)
}
prv, _ := bls.PrivateKeyFromBytes(buf)
pub := prv.PublicKeyNative()

b.ResetTimer()

for i := 0; i < b.N; i++ {
_ = pub.Bytes()
}
}

func BenchmarkDecodeSign(b *testing.B) {
b.ReportAllocs()

buf := make([]byte, bls.PrivateKeySize)
_, err := rand.Read(buf)
if err != nil {
b.Fatal(err)
}
prv, _ := bls.PrivateKeyFromBytes(buf)
bufMsg := []byte("pactus")
sig := prv.Sign(bufMsg)
sigBytes := sig.Bytes()

b.ResetTimer()

for i := 0; i < b.N; i++ {
_, err := bls.SignatureFromBytes(sigBytes)
if err != nil {
b.Fatal(err)
}
}
}

func BenchmarkVerify(b *testing.B) {
b.ReportAllocs()

buf := make([]byte, bls.PrivateKeySize)
_, err := rand.Read(buf)
if err != nil {
b.Fatal(err)
}
prv, _ := bls.PrivateKeyFromBytes(buf)
pub := prv.PublicKeyNative()

bufMsg := []byte("pactus")
sig1 := prv.Sign(bufMsg)

b.ResetTimer()

for i := 0; i < b.N; i++ {
err = pub.Verify(bufMsg, sig1)
if err != nil {
b.Fatal(err)
}
}
}

func BenchmarkDecode(b *testing.B) {
b.ReportAllocs()

buf := make([]byte, bls.PrivateKeySize)
_, err := rand.Read(buf)
if err != nil {
b.Fatal(err)
}
prv, _ := bls.PrivateKeyFromBytes(buf)
pub := prv.PublicKeyNative()
pubBytes := pub.Bytes()
b.ResetTimer()

for i := 0; i < b.N; i++ {
_, err := bls.PublicKeyFromBytes(pubBytes)
if err != nil {
b.Fatal(err)
}
}
}

0 comments on commit dbfe34f

Please sign in to comment.