Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Perf: Poseidon2 GKR circuit #1410

Open
wants to merge 52 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
6025aa5
refactor: power
Tabaie Jan 22, 2025
952ac57
feat: gkr gates
Tabaie Jan 23, 2025
3af41cb
cleanup
Tabaie Jan 23, 2025
c8d5327
feat bls12377 gates for poseidon2
Tabaie Jan 24, 2025
f9d6f56
fix: fr gates
Tabaie Jan 24, 2025
a4c633e
feat: gkr permutations
Tabaie Jan 29, 2025
134e640
fix: no y round keys in partial rounds
Tabaie Jan 29, 2025
35a1045
feat: passing proof
Tabaie Jan 29, 2025
d7c49f9
fix: max
Tabaie Jan 29, 2025
909adbf
refactor: public GkrPermutations
Tabaie Jan 29, 2025
89de33f
fix real round constants
Tabaie Jan 30, 2025
5e00636
feat: merkle damgard and poseidon2
Tabaie Jan 31, 2025
63038e6
fix bad use of h.Permutation, and test
Tabaie Jan 31, 2025
a8e353f
docs: remove incomplete comment
Tabaie Feb 3, 2025
1b57817
test: against gnark-crypto
Tabaie Feb 3, 2025
d9abb7d
remove zero keys
Tabaie Feb 3, 2025
e137111
refactor: move bls12377 code to gnark-crypto
Tabaie Feb 3, 2025
8f5e88e
perf: break up sbox
Tabaie Feb 4, 2025
e5fdd2b
fix broken up s box works
Tabaie Feb 4, 2025
a022a5a
feat: test gate degrees
Tabaie Feb 4, 2025
5f8eee0
chore: share sbox gates
Tabaie Feb 4, 2025
a216fee
refactor: simplify sbox function
Tabaie Feb 4, 2025
37a79fa
chore: integrate gnark-crypto refactors
Tabaie Feb 5, 2025
2473dea
refactor: rename NewHash to NewPermutation
Tabaie Feb 5, 2025
bdc849e
refactor: downstream from gnark-crypto Poseidon2 changes
Tabaie Feb 5, 2025
5032ce6
Merge branch 'master' into feat/poseidon2-hash
Tabaie Feb 11, 2025
254dd75
refactor: align implementation with gnark-crypto
ivokub Feb 12, 2025
cc342a0
refactor: align naming with gnark-crypto
ivokub Feb 12, 2025
4b9aaf2
feat: take default parameters from gnark-crypto
ivokub Feb 12, 2025
ffdba7f
chore: rename file
ivokub Feb 12, 2025
d6fcd5d
refactor: rename constructor to align with gnark-crypto
ivokub Feb 12, 2025
5c46d09
fix: add danger when calling permute for t=2,3 for now
ivokub Feb 13, 2025
3ed26b0
Merge branch 'master' into perf/gkr-poseidon2-breakup-sbox
Tabaie Feb 13, 2025
2a94b12
build: update deps
Tabaie Feb 13, 2025
e344adc
revert: bring back algo_utils.Map
Tabaie Feb 13, 2025
c1ecda2
fix: reflect gate naming changes
Tabaie Feb 13, 2025
6ea4c1e
Merge remote-tracking branch 'origin/feat/poseidon2-hash' into perf/g…
ivokub Feb 13, 2025
7c5f296
chore: PR feedback
Tabaie Feb 16, 2025
7a488cb
docs: poseidon2 GKR
Tabaie Feb 17, 2025
1ee4644
test: SolveInTestEngine
Tabaie Feb 17, 2025
12a8dcb
chore: solidify RegisterGKRGates
Tabaie Feb 17, 2025
429e64f
refactor: break up `finalize`
Tabaie Feb 17, 2025
dee2881
refactor: move var def near use
Tabaie Feb 17, 2025
b1ed2dd
chore: remove gkr API Println
Tabaie Feb 17, 2025
791c5e3
Merge branch 'master' into perf/gkr-poseidon2-breakup-sbox
Tabaie Feb 17, 2025
883c46f
docs: incorporate @ivokub's suggestions in SolveAll doc
Tabaie Feb 17, 2025
836ece8
Merge branch 'master' into perf/gkr-poseidon2-breakup-sbox
Tabaie Feb 17, 2025
4f862ce
build: update gnark-crypto deps
Tabaie Feb 17, 2025
e2cfc55
Merge branch 'master' into perf/gkr-poseidon2-breakup-sbox
Tabaie Feb 18, 2025
f8f8497
chore: remove separated feature
ivokub Feb 19, 2025
e99bfb5
test: use test.CheckCircuit instead of manual Groth16/PLONK
ivokub Feb 19, 2025
8329f58
chore: go fmt
ivokub Feb 19, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ toolchain go1.22.6
require (
github.com/bits-and-blooms/bitset v1.20.0
github.com/blang/semver/v4 v4.0.0
github.com/consensys/bavard v0.1.27
github.com/consensys/bavard v0.1.29
github.com/consensys/compress v0.2.5
github.com/consensys/gnark-crypto v0.16.1-0.20250205153847-10a243d332ca
github.com/consensys/gnark-crypto v0.16.1-0.20250217214835-5ed804970f85
github.com/fxamacker/cbor/v2 v2.7.0
github.com/google/go-cmp v0.6.0
github.com/google/pprof v0.0.0-20240727154555-813a5fbdbec8
Expand All @@ -19,9 +19,9 @@ require (
github.com/ronanh/intcomp v1.1.0
github.com/rs/zerolog v1.33.0
github.com/stretchr/testify v1.10.0
golang.org/x/crypto v0.32.0
golang.org/x/crypto v0.33.0
golang.org/x/exp v0.0.0-20240823005443-9b4947da3948
golang.org/x/sync v0.10.0
golang.org/x/sync v0.11.0
)

require (
Expand All @@ -31,7 +31,7 @@ require (
github.com/mmcloughlin/addchain v0.4.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/x448/float16 v0.8.4 // indirect
golang.org/x/sys v0.29.0 // indirect
golang.org/x/sys v0.30.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
rsc.io/tmplfunc v0.0.3 // indirect
)
20 changes: 10 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDk
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
github.com/cncf/udpa/go v0.0.0-20200629203442-efcf912fb354/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
github.com/consensys/bavard v0.1.27 h1:j6hKUrGAy/H+gpNrpLU3I26n1yc+VMGmd6ID5+gAhOs=
github.com/consensys/bavard v0.1.27/go.mod h1:k/zVjHHC4B+PQy1Pg7fgvG3ALicQw540Crag8qx+dZs=
github.com/consensys/bavard v0.1.29 h1:fobxIYksIQ+ZSrTJUuQgu+HIJwclrAPcdXqd7H2hh1k=
github.com/consensys/bavard v0.1.29/go.mod h1:k/zVjHHC4B+PQy1Pg7fgvG3ALicQw540Crag8qx+dZs=
github.com/consensys/compress v0.2.5 h1:gJr1hKzbOD36JFsF1AN8lfXz1yevnJi1YolffY19Ntk=
github.com/consensys/compress v0.2.5/go.mod h1:pyM+ZXiNUh7/0+AUjUf9RKUM6vSH7T/fsn5LLS0j1Tk=
github.com/consensys/gnark-crypto v0.16.1-0.20250205153847-10a243d332ca h1:u6iXwMBfbXODF+hDSwKSTBg6yfD3+eMX6o3PILAK474=
github.com/consensys/gnark-crypto v0.16.1-0.20250205153847-10a243d332ca/go.mod h1:Ke3j06ndtPTVvo++PhGNgvm+lgpLvzbcE2MqljY7diU=
github.com/consensys/gnark-crypto v0.16.1-0.20250217214835-5ed804970f85 h1:3ht4gGH3smFGVLFhpFTKvDbEdagC6eSaPXnHjCQGh94=
github.com/consensys/gnark-crypto v0.16.1-0.20250217214835-5ed804970f85/go.mod h1:A2URlMHUT81ifJ0UlLzSlm7TmnE3t7VxEThApdMukJw=
github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
Expand Down Expand Up @@ -304,8 +304,8 @@ golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8U
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.32.0 h1:euUpcYgM8WcP71gNpTqQCn6rC2t6ULUPiOzfWaXVVfc=
golang.org/x/crypto v0.32.0/go.mod h1:ZnnJkOaASj8g0AjIduWNlq2NRxL0PlBrbKVyZ6V/Ugc=
golang.org/x/crypto v0.33.0 h1:IOBPskki6Lysi0lo9qQvbxiQ+FvsCC/YWOecCHAixus=
golang.org/x/crypto v0.33.0/go.mod h1:bVdXmD7IV/4GdElGPozy6U7lWdRXA4qyRVGJV57uQ5M=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=
Expand Down Expand Up @@ -410,8 +410,8 @@ golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sync v0.11.0 h1:GGz8+XQP4FvTTrjZPzNKTMFtSXH80RAzG+5ghFPgK9w=
golang.org/x/sync v0.11.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181026203630-95b1ffbd15a5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand Down Expand Up @@ -462,8 +462,8 @@ golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
Expand Down
5 changes: 0 additions & 5 deletions std/gkr/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,3 @@ func (api *API) Sub(i1, i2 constraint.GkrVariable, in ...constraint.GkrVariable)
func (api *API) Mul(i1, i2 constraint.GkrVariable, in ...constraint.GkrVariable) constraint.GkrVariable {
return api.namedGate2PlusIn("mul", i1, i2, in...)
}

// TODO @Tabaie This can be useful
func (api *API) Println(a ...constraint.GkrVariable) {
panic("not implemented")
}
142 changes: 74 additions & 68 deletions std/gkr/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ import (
bw6761 "github.com/consensys/gnark/constraint/bw6-761"
"github.com/consensys/gnark/test"

"github.com/consensys/gnark-crypto/kzg"
"github.com/consensys/gnark/backend/plonk"
bn254 "github.com/consensys/gnark/constraint/bn254"
"github.com/stretchr/testify/require"

Expand All @@ -26,15 +24,12 @@ 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/internal/test_vectors_utils"
"github.com/consensys/gnark/test/unsafekzg"
)

// compressThreshold --> if linear expressions are larger than this, the frontend will introduce
Expand Down Expand Up @@ -69,6 +64,7 @@ func (c *doubleNoDependencyCircuit) Define(api frontend.API) error {
}

func TestDoubleNoDependencyCircuit(t *testing.T) {
assert := test.NewAssert(t)

xValuess := [][]frontend.Variable{
{1, 1},
Expand All @@ -77,12 +73,13 @@ func TestDoubleNoDependencyCircuit(t *testing.T) {

hashes := []string{"-1", "-20"}

for _, xValues := range xValuess {
for i, xValues := range xValuess {
for _, hashName := range hashes {
assignment := doubleNoDependencyCircuit{X: xValues}
circuit := doubleNoDependencyCircuit{X: make([]frontend.Variable, len(xValues)), hashName: hashName}

test.NewAssert(t).CheckCircuit(&circuit, test.WithValidAssignment(&assignment))
assert.Run(func(assert *test.Assert) {
assert.CheckCircuit(&circuit, test.WithValidAssignment(&assignment), test.WithCurves(ecc.BN254))
}, fmt.Sprintf("xValue=%d/hash=%s", i, hashName))

}
}
Expand Down Expand Up @@ -115,6 +112,7 @@ func (c *sqNoDependencyCircuit) Define(api frontend.API) error {
}

func TestSqNoDependencyCircuit(t *testing.T) {
assert := test.NewAssert(t)

xValuess := [][]frontend.Variable{
{1, 1},
Expand All @@ -123,12 +121,13 @@ func TestSqNoDependencyCircuit(t *testing.T) {

hashes := []string{"-1", "-20"}

for _, xValues := range xValuess {
for i, xValues := range xValuess {
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)
assert.Run(func(assert *test.Assert) {
assert.CheckCircuit(&circuit, test.WithValidAssignment(&assignment), test.WithCurves(ecc.BN254))
}, fmt.Sprintf("xValues=%d/hash=%s", i, hashName))
}
}
}
Expand Down Expand Up @@ -168,6 +167,7 @@ func (c *mulNoDependencyCircuit) Define(api frontend.API) error {
}

func TestMulNoDependency(t *testing.T) {
assert := test.NewAssert(t)
xValuess := [][]frontend.Variable{
{1, 2},
}
Expand All @@ -189,9 +189,9 @@ func TestMulNoDependency(t *testing.T) {
Y: make([]frontend.Variable, len(yValuess[i])),
hashName: hashName,
}

testGroth16(t, &circuit, &assignment)
testPlonk(t, &circuit, &assignment)
assert.Run(func(assert *test.Assert) {
assert.CheckCircuit(&circuit, test.WithValidAssignment(&assignment), test.WithCurves(ecc.BN254))
}, fmt.Sprintf("xValues=%d/hash=%s", i, hashName))
}
}
}
Expand Down Expand Up @@ -240,14 +240,13 @@ func (c *mulWithDependencyCircuit) Define(api frontend.API) error {
}

func TestSolveMulWithDependency(t *testing.T) {
assert := test.NewAssert(t)
assignment := mulWithDependencyCircuit{
XLast: 1,
Y: []frontend.Variable{3, 2},
}
circuit := mulWithDependencyCircuit{Y: make([]frontend.Variable, len(assignment.Y)), hashName: "-20"}

testGroth16(t, &circuit, &assignment)
testPlonk(t, &circuit, &assignment)
assert.CheckCircuit(&circuit, test.WithValidAssignment(&assignment), test.WithCurves(ecc.BN254))
}

func TestApiMul(t *testing.T) {
Expand Down Expand Up @@ -387,53 +386,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, srsLagrange, err := unsafekzg.NewSRS(cs)
require.NoError(t, err)
pk, vk, err = plonk.Setup(cs, kzgSrs, srsLagrange)
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() {
bn254.RegisterHashBuilder("mimc", func() hash.Hash {
return bn254MiMC.NewMiMC()
Expand Down Expand Up @@ -645,19 +597,21 @@ func BenchmarkMiMCNoGkrFullDepthSolve(b *testing.B) {
}

func TestMiMCFullDepthNoDepSolve(t *testing.T) {
assert := test.NewAssert(t)
registerMiMC()
for i := 0; i < 100; i++ {
circuit, assignment := mimcNoDepCircuits(5, 1<<2, "-20")
testGroth16(t, circuit, assignment)
testPlonk(t, circuit, assignment)
assert.Run(func(assert *test.Assert) {
assert.CheckCircuit(circuit, test.WithValidAssignment(assignment), test.WithCurves(ecc.BN254))
}, fmt.Sprintf("i=%d", i))
}
}

func TestMiMCFullDepthNoDepSolveWithMiMCHash(t *testing.T) {
assert := test.NewAssert(t)
registerMiMC()
circuit, assignment := mimcNoDepCircuits(5, 1<<2, "mimc")
testGroth16(t, circuit, assignment)
testPlonk(t, circuit, assignment)
assert.CheckCircuit(circuit, test.WithValidAssignment(assignment), test.WithCurves(ecc.BN254))
}

func mimcNoGkrCircuits(mimcDepth, nbInstances int) (circuit, assignment frontend.Circuit) {
Expand All @@ -678,3 +632,55 @@ func mimcNoGkrCircuits(mimcDepth, nbInstances int) (circuit, assignment frontend
}
return
}

func TestSolveInTestEngine(t *testing.T) {
assignment := testSolveInTestEngineCircuit{
X: []frontend.Variable{2, 3, 4, 5, 6, 7, 8, 9},
}
circuit := testSolveInTestEngineCircuit{
X: make([]frontend.Variable, len(assignment.X)),
}

require.NoError(t, test.IsSolved(&circuit, &assignment, ecc.BN254.ScalarField()))
require.NoError(t, test.IsSolved(&circuit, &assignment, ecc.BLS24_315.ScalarField()))
require.NoError(t, test.IsSolved(&circuit, &assignment, ecc.BLS12_381.ScalarField()))
require.NoError(t, test.IsSolved(&circuit, &assignment, ecc.BLS24_317.ScalarField()))
require.NoError(t, test.IsSolved(&circuit, &assignment, ecc.BW6_633.ScalarField()))
require.NoError(t, test.IsSolved(&circuit, &assignment, ecc.BW6_761.ScalarField()))
require.NoError(t, test.IsSolved(&circuit, &assignment, ecc.BLS12_377.ScalarField()))
}

type testSolveInTestEngineCircuit struct {
X []frontend.Variable
}

func (c *testSolveInTestEngineCircuit) Define(api frontend.API) error {
gkr := NewApi()
x, err := gkr.Import(c.X)
if err != nil {
return err
}
Y := make([]frontend.Variable, len(c.X))
Y[0] = 1
y, err := gkr.Import(Y)
if err != nil {
return err
}

z := gkr.Mul(x, y)

for i := range len(c.X) - 1 {
gkr.Series(y, z, i+1, i)
}

assignments := gkr.SolveInTestEngine(api)

product := frontend.Variable(1)
for i := range c.X {
api.AssertIsEqual(assignments[y][i], product)
product = api.Mul(product, c.X[i])
api.AssertIsEqual(assignments[z][i], product)
}

return nil
}
Loading
Loading