Skip to content

Commit

Permalink
build: generify system.Equal
Browse files Browse the repository at this point in the history
  • Loading branch information
Tabaie committed Jun 30, 2023
1 parent 9fc971f commit 9d44a4d
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 140 deletions.
14 changes: 4 additions & 10 deletions constraint/bls12-377/system.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 4 additions & 10 deletions constraint/bls12-381/system.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 4 additions & 10 deletions constraint/bls24-315/system.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 4 additions & 10 deletions constraint/bls24-317/system.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 4 additions & 10 deletions constraint/bw6-633/system.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 4 additions & 10 deletions constraint/bw6-761/system.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 4 additions & 10 deletions constraint/tinyfield/system.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 4 additions & 10 deletions internal/generator/backend/template/representations/system.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -379,18 +379,12 @@ func (s *system) AddGkr(gkr constraint.GkrInfo) error {
}

func (s *system) Equal(other constraint.ConstraintSystem) bool {
if !s.GkrInfo.Is() {
return reflect.DeepEqual(s, other) // fast track
}
if o, ok := other.(*system); !ok {
return false
} else {
oHints := o.MHintsDependencies

if match := constraint.HintsEqual(s.MHintsDependencies, oHints); !match {
return false
}

o.MHintsDependencies = s.MHintsDependencies
match := reflect.DeepEqual(s, o)
o.MHintsDependencies = oHints
return match
return reflect.DeepEqual(s.field, o.field) && reflect.DeepEqual(s.CoeffTable, o.CoeffTable) && constraint.SystemEqual(s.System, o.System)
}
}
64 changes: 4 additions & 60 deletions std/gkr/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package gkr

import (
"fmt"
"github.com/consensys/gnark-crypto/kzg"
"github.com/consensys/gnark/backend"
"github.com/consensys/gnark/backend/plonk"
bn254r1cs "github.com/consensys/gnark/constraint/bn254"
"github.com/consensys/gnark/test"
"github.com/stretchr/testify/require"
Expand All @@ -19,11 +17,9 @@ import (
"github.com/consensys/gnark-crypto/ecc/bn254/fr/gkr"
bn254MiMC "github.com/consensys/gnark-crypto/ecc/bn254/fr/mimc"
"github.com/consensys/gnark/backend/groth16"
"github.com/consensys/gnark/backend/witness"
"github.com/consensys/gnark/constraint"
"github.com/consensys/gnark/frontend"
"github.com/consensys/gnark/frontend/cs/r1cs"
"github.com/consensys/gnark/frontend/cs/scs"
stdHash "github.com/consensys/gnark/std/hash"
"github.com/consensys/gnark/std/hash/mimc"
test_vector_utils "github.com/consensys/gnark/std/utils/test_vectors_utils"
Expand Down Expand Up @@ -74,9 +70,7 @@ func TestDoubleNoDependencyCircuit(t *testing.T) {
assignment := doubleNoDependencyCircuit{X: xValues}
circuit := doubleNoDependencyCircuit{X: make([]frontend.Variable, len(xValues)), hashName: hashName}

test.NewAssert(t).SolvingSucceeded(&circuit, &assignment, test.WithBackends(backend.GROTH16), test.WithCurves(ecc.BN254))
//testGroth16(t, &circuit, &assignment)
//testPlonk(t, &circuit, &assignment)
test.NewAssert(t).ProverSucceeded(&circuit, &assignment, test.WithCurves(ecc.BN254))
}
}
}
Expand Down Expand Up @@ -120,8 +114,7 @@ func TestSqNoDependencyCircuit(t *testing.T) {
for _, hashName := range hashes {
assignment := sqNoDependencyCircuit{X: xValues}
circuit := sqNoDependencyCircuit{X: make([]frontend.Variable, len(xValues)), hashName: hashName}
testGroth16(t, &circuit, &assignment)
testPlonk(t, &circuit, &assignment)
test.NewAssert(t).ProverSucceeded(&circuit, &assignment, test.WithCurves(ecc.BN254))
}
}
}
Expand Down Expand Up @@ -183,8 +176,7 @@ func TestMulNoDependency(t *testing.T) {
hashName: hashName,
}

testGroth16(t, &circuit, &assignment)
testPlonk(t, &circuit, &assignment)
test.NewAssert(t).ProverSucceeded(&circuit, &assignment, test.WithCurves(ecc.BN254))
}
}
}
Expand Down Expand Up @@ -239,8 +231,7 @@ func TestSolveMulWithDependency(t *testing.T) {
}
circuit := mulWithDependencyCircuit{Y: make([]frontend.Variable, len(assignment.Y)), hashName: "-20"}

testGroth16(t, &circuit, &assignment)
testPlonk(t, &circuit, &assignment)
test.NewAssert(t).ProverSucceeded(&circuit, &assignment, test.WithCurves(ecc.BN254))
}

func TestApiMul(t *testing.T) {
Expand Down Expand Up @@ -380,53 +371,6 @@ func (c *benchMiMCMerkleTreeCircuit) Define(api frontend.API) error {
return solution.Verify("-20", challenge)
}

func testGroth16(t *testing.T, circuit, assignment frontend.Circuit) {
cs, err := frontend.Compile(ecc.BN254.ScalarField(), r1cs.NewBuilder, circuit, frontend.WithCompressThreshold(compressThreshold))
require.NoError(t, err)
var (
fullWitness witness.Witness
publicWitness witness.Witness
pk groth16.ProvingKey
vk groth16.VerifyingKey
proof groth16.Proof
)
fullWitness, err = frontend.NewWitness(assignment, ecc.BN254.ScalarField())
require.NoError(t, err)
publicWitness, err = fullWitness.Public()
require.NoError(t, err)
pk, vk, err = groth16.Setup(cs)
require.NoError(t, err)
proof, err = groth16.Prove(cs, pk, fullWitness)
require.NoError(t, err)
err = groth16.Verify(proof, vk, publicWitness)
require.NoError(t, err)
}

func testPlonk(t *testing.T, circuit, assignment frontend.Circuit) {
cs, err := frontend.Compile(ecc.BN254.ScalarField(), scs.NewBuilder, circuit, frontend.WithCompressThreshold(compressThreshold))
require.NoError(t, err)
var (
fullWitness witness.Witness
publicWitness witness.Witness
pk plonk.ProvingKey
vk plonk.VerifyingKey
proof plonk.Proof
kzgSrs kzg.SRS
)
fullWitness, err = frontend.NewWitness(assignment, ecc.BN254.ScalarField())
require.NoError(t, err)
publicWitness, err = fullWitness.Public()
require.NoError(t, err)
kzgSrs, err = test.NewKZGSRS(cs)
require.NoError(t, err)
pk, vk, err = plonk.Setup(cs, kzgSrs)
require.NoError(t, err)
proof, err = plonk.Prove(cs, pk, fullWitness)
require.NoError(t, err)
err = plonk.Verify(proof, vk, publicWitness)
require.NoError(t, err)
}

func registerMiMC() {
bn254r1cs.HashBuilderRegistry["mimc"] = bn254MiMC.NewMiMC
stdHash.BuilderRegistry["mimc"] = func(api frontend.API) (stdHash.FieldHasher, error) {
Expand Down
12 changes: 12 additions & 0 deletions std/gkr/placeholder_hints.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,20 @@ func ProveHintPlaceholderGenerator(hashName string, solveHintId, proveHintId sol

curve := utils.FieldToCurve(mod)
switch curve {
case ecc.BLS12_377:
err = bls12_377.GkrProveHint(hashName, placeholderGkrSolvingData[solveHintId].(*bls12_377.GkrSolvingData))(mod, in, out)
case ecc.BLS12_381:
err = bls12_381.GkrProveHint(hashName, placeholderGkrSolvingData[solveHintId].(*bls12_381.GkrSolvingData))(mod, in, out)
case ecc.BLS24_315:
err = bls24_315.GkrProveHint(hashName, placeholderGkrSolvingData[solveHintId].(*bls24_315.GkrSolvingData))(mod, in, out)
case ecc.BLS24_317:
err = bls24_317.GkrProveHint(hashName, placeholderGkrSolvingData[solveHintId].(*bls24_317.GkrSolvingData))(mod, in, out)
case ecc.BN254:
err = bn254.GkrProveHint(hashName, placeholderGkrSolvingData[solveHintId].(*bn254.GkrSolvingData))(mod, in, out)
case ecc.BW6_633:
err = bw6_633.GkrProveHint(hashName, placeholderGkrSolvingData[solveHintId].(*bw6_633.GkrSolvingData))(mod, in, out)
case ecc.BW6_761:
err = bw6_761.GkrProveHint(hashName, placeholderGkrSolvingData[solveHintId].(*bw6_761.GkrSolvingData))(mod, in, out)
default:
err = errors.New("unsupported curve")
}
Expand Down

0 comments on commit 9d44a4d

Please sign in to comment.