Skip to content

Commit

Permalink
added benchmarks for HashToG2/BLS12-381
Browse files Browse the repository at this point in the history
  • Loading branch information
weijiguo committed Jan 30, 2024
1 parent 86d12ce commit 78bdcf6
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions std/algebra/emulated/sw_bls12381/hash_to_g2_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package sw_bls12381

import (
"bytes"
"encoding/hex"
"testing"

"github.com/consensys/gnark-crypto/ecc"
bls12381 "github.com/consensys/gnark-crypto/ecc/bls12-381"
bls12381fp "github.com/consensys/gnark-crypto/ecc/bls12-381/fp"
"github.com/consensys/gnark/constraint"
"github.com/consensys/gnark/frontend"
"github.com/consensys/gnark/frontend/cs/r1cs"
"github.com/consensys/gnark/frontend/cs/scs"
"github.com/consensys/gnark/std/algebra/emulated/fields_bls12381"
"github.com/consensys/gnark/std/hash/tofield"
"github.com/consensys/gnark/std/math/emulated"
Expand Down Expand Up @@ -188,3 +192,75 @@ func TestHashToG2TestSolve(t *testing.T) {
assert.NoError(err)
}
}

type hashToG2BenchCircuit struct {
Msg []byte
Dst []byte
}

func (c *hashToG2BenchCircuit) Define(api frontend.API) error {
_, e := HashToG2(api, uints.NewU8Array(c.Msg), c.Dst)
return e
}

func BenchmarkHashToG2(b *testing.B) {

dst := getDst()

msg := "abcd"
witness := hashToG2BenchCircuit{
Msg: []uint8(msg),
Dst: dst,
}
w, err := frontend.NewWitness(&witness, ecc.BN254.ScalarField())
if err != nil {
b.Fatal(err)
}
var ccs constraint.ConstraintSystem
b.Run("compile scs", func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
if ccs, err = frontend.Compile(ecc.BN254.ScalarField(), scs.NewBuilder, &hashToG2BenchCircuit{}); err != nil {
b.Fatal(err)
}
}
})
var buf bytes.Buffer
_, err = ccs.WriteTo(&buf)
if err != nil {
b.Fatal(err)
}
b.Logf("scs size: %d (bytes), nb constraints %d, nbInstructions: %d", buf.Len(), ccs.GetNbConstraints(), ccs.GetNbInstructions())
b.Run("solve scs", func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
if _, err := ccs.Solve(w); err != nil {
b.Fatal(err)
}
}
})
b.Run("compile r1cs", func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
if ccs, err = frontend.Compile(ecc.BN254.ScalarField(), r1cs.NewBuilder, &hashToG2BenchCircuit{}); err != nil {
b.Fatal(err)
}
}
})
buf.Reset()
_, err = ccs.WriteTo(&buf)
if err != nil {
b.Fatal(err)
}
b.Logf("r1cs size: %d (bytes), nb constraints %d, nbInstructions: %d", buf.Len(), ccs.GetNbConstraints(), ccs.GetNbInstructions())

b.Run("solve r1cs", func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
if _, err := ccs.Solve(w); err != nil {
b.Fatal(err)
}
}
})

}

0 comments on commit 78bdcf6

Please sign in to comment.