mirror of
https://github.com/arnaucube/gnark-plonky2-verifier.git
synced 2026-01-12 00:51:33 +01:00
Added benchmark
This commit is contained in:
1
goldilocks/.gitignore
vendored
Normal file
1
goldilocks/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
gnark.pprof
|
||||||
@@ -77,12 +77,14 @@ func NegOne() GoldilocksVariable {
|
|||||||
|
|
||||||
// The chip used for Goldilocks field operations.
|
// The chip used for Goldilocks field operations.
|
||||||
type GoldilocksApi struct {
|
type GoldilocksApi struct {
|
||||||
api frontend.API
|
api frontend.API
|
||||||
|
rangeChecker frontend.Rangechecker
|
||||||
}
|
}
|
||||||
|
|
||||||
// Creates a new Goldilocks chip.
|
// Creates a new Goldilocks chip.
|
||||||
func NewGoldilocksApi(api frontend.API) *GoldilocksApi {
|
func NewGoldilocksApi(api frontend.API) *GoldilocksApi {
|
||||||
return &GoldilocksApi{api: api}
|
rangeChecker := rangecheck.New(api)
|
||||||
|
return &GoldilocksApi{api: api, rangeChecker: rangeChecker}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Adds two field elements such that x + y = z within the Golidlocks field.
|
// Adds two field elements such that x + y = z within the Golidlocks field.
|
||||||
@@ -182,8 +184,7 @@ func (p *GoldilocksApi) Reduce(x GoldilocksVariable) GoldilocksVariable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
quotient := result[0]
|
quotient := result[0]
|
||||||
rangeCheckNbBits := RANGE_CHECK_NB_BITS
|
p.rangeChecker.Check(quotient, RANGE_CHECK_NB_BITS)
|
||||||
p.api.ToBinary(quotient, rangeCheckNbBits)
|
|
||||||
|
|
||||||
remainder := NewVariable(result[1])
|
remainder := NewVariable(result[1])
|
||||||
p.RangeCheck(remainder)
|
p.RangeCheck(remainder)
|
||||||
@@ -205,7 +206,7 @@ func (p *GoldilocksApi) ReduceWithMaxBits(x GoldilocksVariable, maxNbBits uint64
|
|||||||
}
|
}
|
||||||
|
|
||||||
quotient := result[0]
|
quotient := result[0]
|
||||||
p.api.ToBinary(quotient, int(maxNbBits))
|
p.rangeChecker.Check(quotient, int(maxNbBits))
|
||||||
|
|
||||||
remainder := NewVariable(result[1])
|
remainder := NewVariable(result[1])
|
||||||
p.RangeCheck(remainder)
|
p.RangeCheck(remainder)
|
||||||
|
|||||||
@@ -1,12 +1,16 @@
|
|||||||
package goldilocks
|
package goldilocks
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"math/big"
|
"math/big"
|
||||||
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/consensys/gnark-crypto/ecc"
|
"github.com/consensys/gnark-crypto/ecc"
|
||||||
"github.com/consensys/gnark/backend"
|
"github.com/consensys/gnark/backend"
|
||||||
"github.com/consensys/gnark/frontend"
|
"github.com/consensys/gnark/frontend"
|
||||||
|
"github.com/consensys/gnark/frontend/cs/scs"
|
||||||
|
"github.com/consensys/gnark/profile"
|
||||||
"github.com/consensys/gnark/test"
|
"github.com/consensys/gnark/test"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -39,6 +43,43 @@ func TestGoldilocksRangeCheck(t *testing.T) {
|
|||||||
assert.ProverSucceeded(&circuit, &witness, test.WithCurves(ecc.BN254), test.WithBackends(backend.GROTH16))
|
assert.ProverSucceeded(&circuit, &witness, test.WithCurves(ecc.BN254), test.WithBackends(backend.GROTH16))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type TestGoldilocksRangeCheckBenchmarkCircuit struct {
|
||||||
|
X []frontend.Variable
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *TestGoldilocksRangeCheckBenchmarkCircuit) Define(api frontend.API) error {
|
||||||
|
glApi := NewGoldilocksApi(api)
|
||||||
|
for _, x := range c.X {
|
||||||
|
glApi.RangeCheck(NewVariable(x))
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func BenchmarkGoldilocksRangeCheck(b *testing.B) {
|
||||||
|
var sizes = []int{5, 10, 15}
|
||||||
|
for i := 0; i < len(sizes); i++ {
|
||||||
|
var circuit, witness TestGoldilocksRangeCheckBenchmarkCircuit
|
||||||
|
circuit.X = make([]frontend.Variable, 2<<sizes[i])
|
||||||
|
witness.X = make([]frontend.Variable, 2<<sizes[i])
|
||||||
|
for j := 0; j < len(circuit.X); j++ {
|
||||||
|
witness.X[j] = 1
|
||||||
|
}
|
||||||
|
p := profile.Start()
|
||||||
|
r1cs, err := frontend.Compile(ecc.BN254.ScalarField(), scs.NewBuilder, &circuit)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("error in building circuit", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
p.Stop()
|
||||||
|
p.Top()
|
||||||
|
println("r1cs.GetNbCoefficients(): ", r1cs.GetNbCoefficients())
|
||||||
|
println("r1cs.GetNbConstraints(): ", r1cs.GetNbConstraints())
|
||||||
|
println("r1cs.GetNbSecretVariables(): ", r1cs.GetNbSecretVariables())
|
||||||
|
println("r1cs.GetNbPublicVariables(): ", r1cs.GetNbPublicVariables())
|
||||||
|
println("r1cs.GetNbInternalVariables(): ", r1cs.GetNbInternalVariables())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type TestGoldilocksMulAddCircuit struct {
|
type TestGoldilocksMulAddCircuit struct {
|
||||||
X, Y, Z frontend.Variable
|
X, Y, Z frontend.Variable
|
||||||
ExpectedResult frontend.Variable
|
ExpectedResult frontend.Variable
|
||||||
|
|||||||
Reference in New Issue
Block a user