From d0b9a923f2ae32a22dc33a66928f2d7654ecb336 Mon Sep 17 00:00:00 2001 From: Arya Tabaie Date: Thu, 29 Jun 2023 18:31:02 -0500 Subject: [PATCH 1/5] refactor: use same function for hint replacement --- backend/groth16/bls12-377/prove.go | 5 +---- backend/groth16/bls12-381/prove.go | 5 +---- backend/groth16/bls24-315/prove.go | 5 +---- backend/groth16/bls24-317/prove.go | 5 +---- backend/groth16/bn254/prove.go | 5 +---- backend/groth16/bw6-633/prove.go | 5 +---- backend/groth16/bw6-761/prove.go | 5 +---- backend/plonk/bls12-377/prove.go | 5 +---- backend/plonk/bls12-381/prove.go | 5 +---- backend/plonk/bls24-315/prove.go | 5 +---- backend/plonk/bls24-317/prove.go | 5 +---- backend/plonk/bn254/prove.go | 5 +---- backend/plonk/bw6-633/prove.go | 5 +---- backend/plonk/bw6-761/prove.go | 5 +---- .../template/zkpschemes/groth16/groth16.prove.go.tmpl | 5 +---- .../backend/template/zkpschemes/plonk/plonk.prove.go.tmpl | 5 +---- 16 files changed, 16 insertions(+), 64 deletions(-) diff --git a/backend/groth16/bls12-377/prove.go b/backend/groth16/bls12-377/prove.go index ed7124a557..33b02400df 100644 --- a/backend/groth16/bls12-377/prove.go +++ b/backend/groth16/bls12-377/prove.go @@ -95,10 +95,7 @@ func Prove(r1cs *cs.R1CS, pk *ProvingKey, fullWitness witness.Witness, opts ...b } if r1cs.GkrInfo.Is() { - var gkrData cs.GkrSolvingData - solverOpts = append(solverOpts, - solver.OverrideHint(r1cs.GkrInfo.SolveHintID, cs.GkrSolveHint(r1cs.GkrInfo, &gkrData)), - solver.OverrideHint(r1cs.GkrInfo.ProveHintID, cs.GkrProveHint(r1cs.GkrInfo.HashName, &gkrData))) + cs.ReplaceGkrHints(&solverOpts, r1cs.GkrInfo) } _solution, err := r1cs.Solve(fullWitness, solverOpts...) diff --git a/backend/groth16/bls12-381/prove.go b/backend/groth16/bls12-381/prove.go index 6e4c0a5227..c42f1a1765 100644 --- a/backend/groth16/bls12-381/prove.go +++ b/backend/groth16/bls12-381/prove.go @@ -95,10 +95,7 @@ func Prove(r1cs *cs.R1CS, pk *ProvingKey, fullWitness witness.Witness, opts ...b } if r1cs.GkrInfo.Is() { - var gkrData cs.GkrSolvingData - solverOpts = append(solverOpts, - solver.OverrideHint(r1cs.GkrInfo.SolveHintID, cs.GkrSolveHint(r1cs.GkrInfo, &gkrData)), - solver.OverrideHint(r1cs.GkrInfo.ProveHintID, cs.GkrProveHint(r1cs.GkrInfo.HashName, &gkrData))) + cs.ReplaceGkrHints(&solverOpts, r1cs.GkrInfo) } _solution, err := r1cs.Solve(fullWitness, solverOpts...) diff --git a/backend/groth16/bls24-315/prove.go b/backend/groth16/bls24-315/prove.go index c464544ad0..92399a8d84 100644 --- a/backend/groth16/bls24-315/prove.go +++ b/backend/groth16/bls24-315/prove.go @@ -95,10 +95,7 @@ func Prove(r1cs *cs.R1CS, pk *ProvingKey, fullWitness witness.Witness, opts ...b } if r1cs.GkrInfo.Is() { - var gkrData cs.GkrSolvingData - solverOpts = append(solverOpts, - solver.OverrideHint(r1cs.GkrInfo.SolveHintID, cs.GkrSolveHint(r1cs.GkrInfo, &gkrData)), - solver.OverrideHint(r1cs.GkrInfo.ProveHintID, cs.GkrProveHint(r1cs.GkrInfo.HashName, &gkrData))) + cs.ReplaceGkrHints(&solverOpts, r1cs.GkrInfo) } _solution, err := r1cs.Solve(fullWitness, solverOpts...) diff --git a/backend/groth16/bls24-317/prove.go b/backend/groth16/bls24-317/prove.go index 10f38c5f77..76a60a3e3f 100644 --- a/backend/groth16/bls24-317/prove.go +++ b/backend/groth16/bls24-317/prove.go @@ -95,10 +95,7 @@ func Prove(r1cs *cs.R1CS, pk *ProvingKey, fullWitness witness.Witness, opts ...b } if r1cs.GkrInfo.Is() { - var gkrData cs.GkrSolvingData - solverOpts = append(solverOpts, - solver.OverrideHint(r1cs.GkrInfo.SolveHintID, cs.GkrSolveHint(r1cs.GkrInfo, &gkrData)), - solver.OverrideHint(r1cs.GkrInfo.ProveHintID, cs.GkrProveHint(r1cs.GkrInfo.HashName, &gkrData))) + cs.ReplaceGkrHints(&solverOpts, r1cs.GkrInfo) } _solution, err := r1cs.Solve(fullWitness, solverOpts...) diff --git a/backend/groth16/bn254/prove.go b/backend/groth16/bn254/prove.go index 42ec4de8b9..3abdccbfd9 100644 --- a/backend/groth16/bn254/prove.go +++ b/backend/groth16/bn254/prove.go @@ -95,10 +95,7 @@ func Prove(r1cs *cs.R1CS, pk *ProvingKey, fullWitness witness.Witness, opts ...b } if r1cs.GkrInfo.Is() { - var gkrData cs.GkrSolvingData - solverOpts = append(solverOpts, - solver.OverrideHint(r1cs.GkrInfo.SolveHintID, cs.GkrSolveHint(r1cs.GkrInfo, &gkrData)), - solver.OverrideHint(r1cs.GkrInfo.ProveHintID, cs.GkrProveHint(r1cs.GkrInfo.HashName, &gkrData))) + cs.ReplaceGkrHints(&solverOpts, r1cs.GkrInfo) } _solution, err := r1cs.Solve(fullWitness, solverOpts...) diff --git a/backend/groth16/bw6-633/prove.go b/backend/groth16/bw6-633/prove.go index b92dbb6943..9d20837acc 100644 --- a/backend/groth16/bw6-633/prove.go +++ b/backend/groth16/bw6-633/prove.go @@ -95,10 +95,7 @@ func Prove(r1cs *cs.R1CS, pk *ProvingKey, fullWitness witness.Witness, opts ...b } if r1cs.GkrInfo.Is() { - var gkrData cs.GkrSolvingData - solverOpts = append(solverOpts, - solver.OverrideHint(r1cs.GkrInfo.SolveHintID, cs.GkrSolveHint(r1cs.GkrInfo, &gkrData)), - solver.OverrideHint(r1cs.GkrInfo.ProveHintID, cs.GkrProveHint(r1cs.GkrInfo.HashName, &gkrData))) + cs.ReplaceGkrHints(&solverOpts, r1cs.GkrInfo) } _solution, err := r1cs.Solve(fullWitness, solverOpts...) diff --git a/backend/groth16/bw6-761/prove.go b/backend/groth16/bw6-761/prove.go index 3ee6b9ad0f..000f28fc50 100644 --- a/backend/groth16/bw6-761/prove.go +++ b/backend/groth16/bw6-761/prove.go @@ -95,10 +95,7 @@ func Prove(r1cs *cs.R1CS, pk *ProvingKey, fullWitness witness.Witness, opts ...b } if r1cs.GkrInfo.Is() { - var gkrData cs.GkrSolvingData - solverOpts = append(solverOpts, - solver.OverrideHint(r1cs.GkrInfo.SolveHintID, cs.GkrSolveHint(r1cs.GkrInfo, &gkrData)), - solver.OverrideHint(r1cs.GkrInfo.ProveHintID, cs.GkrProveHint(r1cs.GkrInfo.HashName, &gkrData))) + cs.ReplaceGkrHints(&solverOpts, r1cs.GkrInfo) } _solution, err := r1cs.Solve(fullWitness, solverOpts...) diff --git a/backend/plonk/bls12-377/prove.go b/backend/plonk/bls12-377/prove.go index 3f767215ba..ef004c4704 100644 --- a/backend/plonk/bls12-377/prove.go +++ b/backend/plonk/bls12-377/prove.go @@ -128,10 +128,7 @@ func Prove(spr *cs.SparseR1CS, pk *ProvingKey, fullWitness witness.Witness, opts } if spr.GkrInfo.Is() { - var gkrData cs.GkrSolvingData - opt.SolverOpts = append(opt.SolverOpts, - solver.OverrideHint(spr.GkrInfo.SolveHintID, cs.GkrSolveHint(spr.GkrInfo, &gkrData)), - solver.OverrideHint(spr.GkrInfo.ProveHintID, cs.GkrProveHint(spr.GkrInfo.HashName, &gkrData))) + cs.ReplaceGkrHints(&opt.SolverOpts, spr.GkrInfo) } // query l, r, o in Lagrange basis, not blinded diff --git a/backend/plonk/bls12-381/prove.go b/backend/plonk/bls12-381/prove.go index 17627cd176..6e2f4b20fe 100644 --- a/backend/plonk/bls12-381/prove.go +++ b/backend/plonk/bls12-381/prove.go @@ -128,10 +128,7 @@ func Prove(spr *cs.SparseR1CS, pk *ProvingKey, fullWitness witness.Witness, opts } if spr.GkrInfo.Is() { - var gkrData cs.GkrSolvingData - opt.SolverOpts = append(opt.SolverOpts, - solver.OverrideHint(spr.GkrInfo.SolveHintID, cs.GkrSolveHint(spr.GkrInfo, &gkrData)), - solver.OverrideHint(spr.GkrInfo.ProveHintID, cs.GkrProveHint(spr.GkrInfo.HashName, &gkrData))) + cs.ReplaceGkrHints(&opt.SolverOpts, spr.GkrInfo) } // query l, r, o in Lagrange basis, not blinded diff --git a/backend/plonk/bls24-315/prove.go b/backend/plonk/bls24-315/prove.go index 7ed6145ebb..ad48f92aa1 100644 --- a/backend/plonk/bls24-315/prove.go +++ b/backend/plonk/bls24-315/prove.go @@ -128,10 +128,7 @@ func Prove(spr *cs.SparseR1CS, pk *ProvingKey, fullWitness witness.Witness, opts } if spr.GkrInfo.Is() { - var gkrData cs.GkrSolvingData - opt.SolverOpts = append(opt.SolverOpts, - solver.OverrideHint(spr.GkrInfo.SolveHintID, cs.GkrSolveHint(spr.GkrInfo, &gkrData)), - solver.OverrideHint(spr.GkrInfo.ProveHintID, cs.GkrProveHint(spr.GkrInfo.HashName, &gkrData))) + cs.ReplaceGkrHints(&opt.SolverOpts, spr.GkrInfo) } // query l, r, o in Lagrange basis, not blinded diff --git a/backend/plonk/bls24-317/prove.go b/backend/plonk/bls24-317/prove.go index 0cdc878d8d..6784bbb0c2 100644 --- a/backend/plonk/bls24-317/prove.go +++ b/backend/plonk/bls24-317/prove.go @@ -128,10 +128,7 @@ func Prove(spr *cs.SparseR1CS, pk *ProvingKey, fullWitness witness.Witness, opts } if spr.GkrInfo.Is() { - var gkrData cs.GkrSolvingData - opt.SolverOpts = append(opt.SolverOpts, - solver.OverrideHint(spr.GkrInfo.SolveHintID, cs.GkrSolveHint(spr.GkrInfo, &gkrData)), - solver.OverrideHint(spr.GkrInfo.ProveHintID, cs.GkrProveHint(spr.GkrInfo.HashName, &gkrData))) + cs.ReplaceGkrHints(&opt.SolverOpts, spr.GkrInfo) } // query l, r, o in Lagrange basis, not blinded diff --git a/backend/plonk/bn254/prove.go b/backend/plonk/bn254/prove.go index 40b120a21f..58a885d7ae 100644 --- a/backend/plonk/bn254/prove.go +++ b/backend/plonk/bn254/prove.go @@ -128,10 +128,7 @@ func Prove(spr *cs.SparseR1CS, pk *ProvingKey, fullWitness witness.Witness, opts } if spr.GkrInfo.Is() { - var gkrData cs.GkrSolvingData - opt.SolverOpts = append(opt.SolverOpts, - solver.OverrideHint(spr.GkrInfo.SolveHintID, cs.GkrSolveHint(spr.GkrInfo, &gkrData)), - solver.OverrideHint(spr.GkrInfo.ProveHintID, cs.GkrProveHint(spr.GkrInfo.HashName, &gkrData))) + cs.ReplaceGkrHints(&opt.SolverOpts, spr.GkrInfo) } // query l, r, o in Lagrange basis, not blinded diff --git a/backend/plonk/bw6-633/prove.go b/backend/plonk/bw6-633/prove.go index 1ea6ccf02d..e2b52f853f 100644 --- a/backend/plonk/bw6-633/prove.go +++ b/backend/plonk/bw6-633/prove.go @@ -128,10 +128,7 @@ func Prove(spr *cs.SparseR1CS, pk *ProvingKey, fullWitness witness.Witness, opts } if spr.GkrInfo.Is() { - var gkrData cs.GkrSolvingData - opt.SolverOpts = append(opt.SolverOpts, - solver.OverrideHint(spr.GkrInfo.SolveHintID, cs.GkrSolveHint(spr.GkrInfo, &gkrData)), - solver.OverrideHint(spr.GkrInfo.ProveHintID, cs.GkrProveHint(spr.GkrInfo.HashName, &gkrData))) + cs.ReplaceGkrHints(&opt.SolverOpts, spr.GkrInfo) } // query l, r, o in Lagrange basis, not blinded diff --git a/backend/plonk/bw6-761/prove.go b/backend/plonk/bw6-761/prove.go index 603a95cf31..d0134d3798 100644 --- a/backend/plonk/bw6-761/prove.go +++ b/backend/plonk/bw6-761/prove.go @@ -128,10 +128,7 @@ func Prove(spr *cs.SparseR1CS, pk *ProvingKey, fullWitness witness.Witness, opts } if spr.GkrInfo.Is() { - var gkrData cs.GkrSolvingData - opt.SolverOpts = append(opt.SolverOpts, - solver.OverrideHint(spr.GkrInfo.SolveHintID, cs.GkrSolveHint(spr.GkrInfo, &gkrData)), - solver.OverrideHint(spr.GkrInfo.ProveHintID, cs.GkrProveHint(spr.GkrInfo.HashName, &gkrData))) + cs.ReplaceGkrHints(&opt.SolverOpts, spr.GkrInfo) } // query l, r, o in Lagrange basis, not blinded diff --git a/internal/generator/backend/template/zkpschemes/groth16/groth16.prove.go.tmpl b/internal/generator/backend/template/zkpschemes/groth16/groth16.prove.go.tmpl index fa72568220..93d77e9234 100644 --- a/internal/generator/backend/template/zkpschemes/groth16/groth16.prove.go.tmpl +++ b/internal/generator/backend/template/zkpschemes/groth16/groth16.prove.go.tmpl @@ -78,10 +78,7 @@ func Prove(r1cs *cs.R1CS, pk *ProvingKey, fullWitness witness.Witness, opts ...b } if r1cs.GkrInfo.Is() { - var gkrData cs.GkrSolvingData - solverOpts = append(solverOpts, - solver.OverrideHint(r1cs.GkrInfo.SolveHintID, cs.GkrSolveHint(r1cs.GkrInfo, &gkrData)), - solver.OverrideHint(r1cs.GkrInfo.ProveHintID, cs.GkrProveHint(r1cs.GkrInfo.HashName, &gkrData))) + cs.ReplaceGkrHints(&solverOpts, r1cs.GkrInfo) } _solution, err := r1cs.Solve(fullWitness, solverOpts...) diff --git a/internal/generator/backend/template/zkpschemes/plonk/plonk.prove.go.tmpl b/internal/generator/backend/template/zkpschemes/plonk/plonk.prove.go.tmpl index def44c21ec..426924c305 100644 --- a/internal/generator/backend/template/zkpschemes/plonk/plonk.prove.go.tmpl +++ b/internal/generator/backend/template/zkpschemes/plonk/plonk.prove.go.tmpl @@ -106,10 +106,7 @@ func Prove(spr *cs.SparseR1CS, pk *ProvingKey, fullWitness witness.Witness, opts } if spr.GkrInfo.Is() { - var gkrData cs.GkrSolvingData - opt.SolverOpts = append(opt.SolverOpts, - solver.OverrideHint(spr.GkrInfo.SolveHintID, cs.GkrSolveHint(spr.GkrInfo, &gkrData)), - solver.OverrideHint(spr.GkrInfo.ProveHintID, cs.GkrProveHint(spr.GkrInfo.HashName, &gkrData))) + cs.ReplaceGkrHints(&opt.SolverOpts, spr.GkrInfo) } // query l, r, o in Lagrange basis, not blinded From 45613f53f766be4c717adce8a91da02dfe94db1c Mon Sep 17 00:00:00 2001 From: Arya Tabaie Date: Thu, 29 Jun 2023 19:00:40 -0500 Subject: [PATCH 2/5] feat: common ReplaceGkrHints function --- constraint/bls12-377/gkr.go | 7 +++++++ constraint/bls12-381/gkr.go | 7 +++++++ constraint/bls24-315/gkr.go | 7 +++++++ constraint/bls24-317/gkr.go | 7 +++++++ constraint/bn254/gkr.go | 7 +++++++ constraint/bw6-633/gkr.go | 7 +++++++ constraint/bw6-761/gkr.go | 7 +++++++ .../generator/backend/template/representations/gkr.go.tmpl | 7 +++++++ 8 files changed, 56 insertions(+) diff --git a/constraint/bls12-377/gkr.go b/constraint/bls12-377/gkr.go index 0a5bcdebef..d555469921 100644 --- a/constraint/bls12-377/gkr.go +++ b/constraint/bls12-377/gkr.go @@ -191,5 +191,12 @@ func GkrProveHint(hashName string, data *GkrSolvingData) hint.Hint { } } +func ReplaceGkrHints(solverOpts *[]hint.Option, gkrInfo constraint.GkrInfo) { + var gkrData GkrSolvingData + *solverOpts = append(*solverOpts, + hint.OverrideHint(gkrInfo.SolveHintID, GkrSolveHint(gkrInfo, &gkrData)), + hint.OverrideHint(gkrInfo.ProveHintID, GkrProveHint(gkrInfo.HashName, &gkrData))) +} + // TODO: Move to gnark-crypto var HashBuilderRegistry = make(map[string]func() hash.Hash) diff --git a/constraint/bls12-381/gkr.go b/constraint/bls12-381/gkr.go index bd55673cd1..006a450a1a 100644 --- a/constraint/bls12-381/gkr.go +++ b/constraint/bls12-381/gkr.go @@ -191,5 +191,12 @@ func GkrProveHint(hashName string, data *GkrSolvingData) hint.Hint { } } +func ReplaceGkrHints(solverOpts *[]hint.Option, gkrInfo constraint.GkrInfo) { + var gkrData GkrSolvingData + *solverOpts = append(*solverOpts, + hint.OverrideHint(gkrInfo.SolveHintID, GkrSolveHint(gkrInfo, &gkrData)), + hint.OverrideHint(gkrInfo.ProveHintID, GkrProveHint(gkrInfo.HashName, &gkrData))) +} + // TODO: Move to gnark-crypto var HashBuilderRegistry = make(map[string]func() hash.Hash) diff --git a/constraint/bls24-315/gkr.go b/constraint/bls24-315/gkr.go index 61925e2be8..a219acf0db 100644 --- a/constraint/bls24-315/gkr.go +++ b/constraint/bls24-315/gkr.go @@ -191,5 +191,12 @@ func GkrProveHint(hashName string, data *GkrSolvingData) hint.Hint { } } +func ReplaceGkrHints(solverOpts *[]hint.Option, gkrInfo constraint.GkrInfo) { + var gkrData GkrSolvingData + *solverOpts = append(*solverOpts, + hint.OverrideHint(gkrInfo.SolveHintID, GkrSolveHint(gkrInfo, &gkrData)), + hint.OverrideHint(gkrInfo.ProveHintID, GkrProveHint(gkrInfo.HashName, &gkrData))) +} + // TODO: Move to gnark-crypto var HashBuilderRegistry = make(map[string]func() hash.Hash) diff --git a/constraint/bls24-317/gkr.go b/constraint/bls24-317/gkr.go index 4b18be9567..9b7fbca313 100644 --- a/constraint/bls24-317/gkr.go +++ b/constraint/bls24-317/gkr.go @@ -191,5 +191,12 @@ func GkrProveHint(hashName string, data *GkrSolvingData) hint.Hint { } } +func ReplaceGkrHints(solverOpts *[]hint.Option, gkrInfo constraint.GkrInfo) { + var gkrData GkrSolvingData + *solverOpts = append(*solverOpts, + hint.OverrideHint(gkrInfo.SolveHintID, GkrSolveHint(gkrInfo, &gkrData)), + hint.OverrideHint(gkrInfo.ProveHintID, GkrProveHint(gkrInfo.HashName, &gkrData))) +} + // TODO: Move to gnark-crypto var HashBuilderRegistry = make(map[string]func() hash.Hash) diff --git a/constraint/bn254/gkr.go b/constraint/bn254/gkr.go index a3730d97d3..db89cc7012 100644 --- a/constraint/bn254/gkr.go +++ b/constraint/bn254/gkr.go @@ -191,5 +191,12 @@ func GkrProveHint(hashName string, data *GkrSolvingData) hint.Hint { } } +func ReplaceGkrHints(solverOpts *[]hint.Option, gkrInfo constraint.GkrInfo) { + var gkrData GkrSolvingData + *solverOpts = append(*solverOpts, + hint.OverrideHint(gkrInfo.SolveHintID, GkrSolveHint(gkrInfo, &gkrData)), + hint.OverrideHint(gkrInfo.ProveHintID, GkrProveHint(gkrInfo.HashName, &gkrData))) +} + // TODO: Move to gnark-crypto var HashBuilderRegistry = make(map[string]func() hash.Hash) diff --git a/constraint/bw6-633/gkr.go b/constraint/bw6-633/gkr.go index db66fd871d..b08dcf1465 100644 --- a/constraint/bw6-633/gkr.go +++ b/constraint/bw6-633/gkr.go @@ -191,5 +191,12 @@ func GkrProveHint(hashName string, data *GkrSolvingData) hint.Hint { } } +func ReplaceGkrHints(solverOpts *[]hint.Option, gkrInfo constraint.GkrInfo) { + var gkrData GkrSolvingData + *solverOpts = append(*solverOpts, + hint.OverrideHint(gkrInfo.SolveHintID, GkrSolveHint(gkrInfo, &gkrData)), + hint.OverrideHint(gkrInfo.ProveHintID, GkrProveHint(gkrInfo.HashName, &gkrData))) +} + // TODO: Move to gnark-crypto var HashBuilderRegistry = make(map[string]func() hash.Hash) diff --git a/constraint/bw6-761/gkr.go b/constraint/bw6-761/gkr.go index 2c3ddabbc0..9f10a52378 100644 --- a/constraint/bw6-761/gkr.go +++ b/constraint/bw6-761/gkr.go @@ -191,5 +191,12 @@ func GkrProveHint(hashName string, data *GkrSolvingData) hint.Hint { } } +func ReplaceGkrHints(solverOpts *[]hint.Option, gkrInfo constraint.GkrInfo) { + var gkrData GkrSolvingData + *solverOpts = append(*solverOpts, + hint.OverrideHint(gkrInfo.SolveHintID, GkrSolveHint(gkrInfo, &gkrData)), + hint.OverrideHint(gkrInfo.ProveHintID, GkrProveHint(gkrInfo.HashName, &gkrData))) +} + // TODO: Move to gnark-crypto var HashBuilderRegistry = make(map[string]func() hash.Hash) diff --git a/internal/generator/backend/template/representations/gkr.go.tmpl b/internal/generator/backend/template/representations/gkr.go.tmpl index 9f6735ab4c..5261138f8c 100644 --- a/internal/generator/backend/template/representations/gkr.go.tmpl +++ b/internal/generator/backend/template/representations/gkr.go.tmpl @@ -173,5 +173,12 @@ func GkrProveHint(hashName string, data *GkrSolvingData) hint.Hint { } } +func ReplaceGkrHints(solverOpts *[]hint.Option, gkrInfo constraint.GkrInfo) { + var gkrData GkrSolvingData + *solverOpts = append(*solverOpts, + hint.OverrideHint(gkrInfo.SolveHintID, GkrSolveHint(gkrInfo, &gkrData)), + hint.OverrideHint(gkrInfo.ProveHintID, GkrProveHint(gkrInfo.HashName, &gkrData))) +} + // TODO: Move to gnark-crypto var HashBuilderRegistry = make(map[string]func() hash.Hash) \ No newline at end of file From 2ca348e76a7a9f412f6354d5cc6a49db951ef7e0 Mon Sep 17 00:00:00 2001 From: Arya Tabaie Date: Thu, 29 Jun 2023 19:01:13 -0500 Subject: [PATCH 3/5] feat: random GKR hint IDs --- std/gkr/compile.go | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/std/gkr/compile.go b/std/gkr/compile.go index 7f8dc97925..79c03aa820 100644 --- a/std/gkr/compile.go +++ b/std/gkr/compile.go @@ -1,6 +1,7 @@ package gkr import ( + "crypto/rand" "fmt" "github.com/consensys/gnark/constraint" "github.com/consensys/gnark/constraint/solver" @@ -8,7 +9,6 @@ import ( fiatshamir "github.com/consensys/gnark/std/fiat-shamir" "github.com/consensys/gnark/std/hash" "github.com/consensys/gnark/std/utils/algo_utils" - "math/big" "math/bits" ) @@ -90,6 +90,14 @@ func appendNonNil(dst *[]frontend.Variable, src []frontend.Variable) { } } +func randHintID() solver.HintID { + bytes := make([]byte, 4) + if _, err := rand.Read(bytes); err != nil { + panic("could not generate random hint ID") + } + return solver.HintID(bytes[0]) | solver.HintID(bytes[1])<<8 | solver.HintID(bytes[2])<<16 | solver.HintID(bytes[3])<<24 +} + // Solve finalizes the GKR circuit and returns the output variables in the order created func (api *API) Solve(parentApi frontend.API) (Solution, error) { @@ -123,8 +131,8 @@ func (api *API) Solve(parentApi frontend.API) (Solution, error) { } } - outsSerialized, err := parentApi.Compiler().NewHint(SolveHintPlaceholder, solveHintNOut, ins...) - api.toStore.SolveHintID = solver.GetHintID(SolveHintPlaceholder) + api.toStore.SolveHintID = randHintID() // Possible danger of confusing with other hints? + outsSerialized, err := parentApi.Compiler().NewHintForId(api.toStore.SolveHintID, solveHintNOut, ins...) if err != nil { return Solution{}, err } @@ -175,11 +183,11 @@ func (s Solution) Verify(hashName string, initialChallenges ...frontend.Variable } copy(hintIns[1:], initialChallenges) - if proofSerialized, err = s.parentApi.Compiler().NewHint( - ProveHintPlaceholder, ProofSize(forSnark.circuit, logNbInstances), hintIns...); err != nil { + s.toStore.ProveHintID = randHintID() // Possible danger of confusing with other hints? + if proofSerialized, err = s.parentApi.Compiler().NewHintForId( + s.toStore.ProveHintID, ProofSize(forSnark.circuit, logNbInstances), hintIns...); err != nil { return err } - s.toStore.ProveHintID = solver.GetHintID(ProveHintPlaceholder) forSnarkSorted := algo_utils.MapRange(0, len(s.toStore.Circuit), slicePtrAt(forSnark.circuit)) @@ -201,14 +209,6 @@ func (s Solution) Verify(hashName string, initialChallenges ...frontend.Variable return s.parentApi.Compiler().SetGkrInfo(s.toStore) } -func SolveHintPlaceholder(*big.Int, []*big.Int, []*big.Int) error { // TODO @Tabaie Add implementation for testing - return fmt.Errorf("placeholder - not meant to be called") -} - -func ProveHintPlaceholder(*big.Int, []*big.Int, []*big.Int) error { // TODO @Tabaie Add implementation for testing - return fmt.Errorf("placeholder - not meant to be called") -} - func slicePtrAt[T any](slice []T) func(int) *T { return func(i int) *T { return &slice[i] From 2505c13033fc5446db6308f4a2426c3c95fc223d Mon Sep 17 00:00:00 2001 From: Arya Tabaie Date: Thu, 29 Jun 2023 19:42:50 -0500 Subject: [PATCH 4/5] feat: usable placeholder hints for bn254 --- constraint/solver/hint_registry.go | 6 +++ std/gkr/compile.go | 6 +++ std/gkr/placeholder_hints.go | 82 ++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+) create mode 100644 std/gkr/placeholder_hints.go diff --git a/constraint/solver/hint_registry.go b/constraint/solver/hint_registry.go index 7021de5024..398d6132e3 100644 --- a/constraint/solver/hint_registry.go +++ b/constraint/solver/hint_registry.go @@ -46,6 +46,12 @@ func RegisterNamedHint(hintFn Hint, key HintID) { registry[key] = hintFn } +func RemoveNamedHint(key HintID) { + registryM.Lock() + defer registryM.Unlock() + delete(registry, key) +} + // GetRegisteredHints returns all registered hint functions. func GetRegisteredHints() []Hint { registryM.RLock() diff --git a/std/gkr/compile.go b/std/gkr/compile.go index 79c03aa820..d0f77d467f 100644 --- a/std/gkr/compile.go +++ b/std/gkr/compile.go @@ -164,6 +164,9 @@ func (s Solution) Export(v frontend.Variable) []frontend.Variable { } // Verify encodes the verification circuitry for the GKR circuit +// hashName denotes the random oracle used to make GKR non-interactive (Fiat-Shamir) +// initialChallenges are initially fed to the Fiat-Shamir hash. If there is many of them, use api.(Committer).Commit to +// consolidate them into a few values func (s Solution) Verify(hashName string, initialChallenges ...frontend.Variable) error { var ( err error @@ -206,6 +209,9 @@ func (s Solution) Verify(hashName string, initialChallenges ...frontend.Variable return err } + solver.RegisterNamedHint(SolveHintPlaceholderGenerator(s.toStore.SolveHintID, s.toStore), s.toStore.SolveHintID) + solver.RegisterNamedHint(ProveHintPlaceholderGenerator(hashName, s.toStore.SolveHintID, s.toStore.ProveHintID), s.toStore.ProveHintID) + return s.parentApi.Compiler().SetGkrInfo(s.toStore) } diff --git a/std/gkr/placeholder_hints.go b/std/gkr/placeholder_hints.go new file mode 100644 index 0000000000..22375e510f --- /dev/null +++ b/std/gkr/placeholder_hints.go @@ -0,0 +1,82 @@ +package gkr + +import ( + "errors" + "github.com/consensys/gnark-crypto/ecc" + "github.com/consensys/gnark/constraint" + bls12_377 "github.com/consensys/gnark/constraint/bls12-377" + bls12_381 "github.com/consensys/gnark/constraint/bls12-381" + bls24_315 "github.com/consensys/gnark/constraint/bls24-315" + bls24_317 "github.com/consensys/gnark/constraint/bls24-317" + bn254 "github.com/consensys/gnark/constraint/bn254" + bw6_633 "github.com/consensys/gnark/constraint/bw6-633" + bw6_761 "github.com/consensys/gnark/constraint/bw6-761" + + "github.com/consensys/gnark/constraint/solver" + "github.com/consensys/gnark/internal/utils" + "math/big" +) + +// TODO @Tabaie Autogen this and possibly move to another package + +var placeholderGkrSolvingData map[solver.HintID]any + +func SolveHintPlaceholderGenerator(hintId solver.HintID, gkrInfo constraint.GkrInfo) func(*big.Int, []*big.Int, []*big.Int) error { + return func(mod *big.Int, in []*big.Int, out []*big.Int) (err error) { + solver.RemoveNamedHint(hintId) + + curve := utils.FieldToCurve(mod) + switch curve { + case ecc.BLS12_377: + var data bls12_377.GkrSolvingData + placeholderGkrSolvingData[hintId] = &data + err = bls12_377.GkrSolveHint(gkrInfo, &data)(mod, in, out) + case ecc.BLS12_381: + var data bls12_381.GkrSolvingData + placeholderGkrSolvingData[hintId] = &data + err = bls12_381.GkrSolveHint(gkrInfo, &data)(mod, in, out) + case ecc.BLS24_315: + var data bls24_315.GkrSolvingData + placeholderGkrSolvingData[hintId] = &data + err = bls24_315.GkrSolveHint(gkrInfo, &data)(mod, in, out) + case ecc.BLS24_317: + var data bls24_317.GkrSolvingData + placeholderGkrSolvingData[hintId] = &data + err = bls24_317.GkrSolveHint(gkrInfo, &data)(mod, in, out) + case ecc.BN254: + var data bn254.GkrSolvingData + placeholderGkrSolvingData[hintId] = &data + err = bn254.GkrSolveHint(gkrInfo, &data)(mod, in, out) + case ecc.BW6_633: + var data bw6_633.GkrSolvingData + placeholderGkrSolvingData[hintId] = &data + err = bw6_633.GkrSolveHint(gkrInfo, &data)(mod, in, out) + case ecc.BW6_761: + var data bw6_761.GkrSolvingData + placeholderGkrSolvingData[hintId] = &data + err = bw6_761.GkrSolveHint(gkrInfo, &data)(mod, in, out) + default: + err = errors.New("unsupported curve") + } + + return err + } +} + +func ProveHintPlaceholderGenerator(hashName string, solveHintId, proveHintId solver.HintID) func(*big.Int, []*big.Int, []*big.Int) error { + return func(mod *big.Int, in []*big.Int, out []*big.Int) (err error) { + solver.RemoveNamedHint(proveHintId) + + curve := utils.FieldToCurve(mod) + switch curve { + case ecc.BN254: + err = bn254.GkrProveHint(hashName, placeholderGkrSolvingData[solveHintId].(*bn254.GkrSolvingData))(mod, in, out) + default: + err = errors.New("unsupported curve") + } + + delete(placeholderGkrSolvingData, solveHintId) + + return err + } +} From fe5af22193f090361ba9851441e933c529d17232 Mon Sep 17 00:00:00 2001 From: Arya Tabaie Date: Fri, 30 Jun 2023 14:28:39 -0500 Subject: [PATCH 5/5] refactor: move gkr hint replacement from prover --- backend/groth16/bls12-377/prove.go | 4 ---- backend/groth16/bls12-381/prove.go | 4 ---- backend/groth16/bls24-315/prove.go | 4 ---- backend/groth16/bls24-317/prove.go | 4 ---- backend/groth16/bn254/prove.go | 4 ---- backend/groth16/bw6-633/prove.go | 4 ---- backend/groth16/bw6-761/prove.go | 4 ---- backend/plonk/bls12-377/prove.go | 4 ---- backend/plonk/bls12-381/prove.go | 4 ---- backend/plonk/bls24-315/prove.go | 4 ---- backend/plonk/bls24-317/prove.go | 4 ---- backend/plonk/bn254/prove.go | 4 ---- backend/plonk/bw6-633/prove.go | 4 ---- backend/plonk/bw6-761/prove.go | 4 ---- constraint/bls12-377/system.go | 4 ++++ constraint/bls12-381/system.go | 4 ++++ constraint/bls24-315/system.go | 4 ++++ constraint/bls24-317/system.go | 4 ++++ constraint/bn254/system.go | 4 ++++ constraint/bw6-633/system.go | 4 ++++ constraint/bw6-761/system.go | 4 ++++ .../backend/template/representations/system.go.tmpl | 8 ++++++-- .../template/zkpschemes/groth16/groth16.prove.go.tmpl | 4 ---- .../backend/template/zkpschemes/plonk/plonk.prove.go.tmpl | 4 ---- 24 files changed, 34 insertions(+), 66 deletions(-) diff --git a/backend/groth16/bls12-377/prove.go b/backend/groth16/bls12-377/prove.go index 33b02400df..9b4ff35e3f 100644 --- a/backend/groth16/bls12-377/prove.go +++ b/backend/groth16/bls12-377/prove.go @@ -94,10 +94,6 @@ func Prove(r1cs *cs.R1CS, pk *ProvingKey, fullWitness witness.Witness, opts ...b }(i))) } - if r1cs.GkrInfo.Is() { - cs.ReplaceGkrHints(&solverOpts, r1cs.GkrInfo) - } - _solution, err := r1cs.Solve(fullWitness, solverOpts...) if err != nil { return nil, err diff --git a/backend/groth16/bls12-381/prove.go b/backend/groth16/bls12-381/prove.go index c42f1a1765..b92f8e744f 100644 --- a/backend/groth16/bls12-381/prove.go +++ b/backend/groth16/bls12-381/prove.go @@ -94,10 +94,6 @@ func Prove(r1cs *cs.R1CS, pk *ProvingKey, fullWitness witness.Witness, opts ...b }(i))) } - if r1cs.GkrInfo.Is() { - cs.ReplaceGkrHints(&solverOpts, r1cs.GkrInfo) - } - _solution, err := r1cs.Solve(fullWitness, solverOpts...) if err != nil { return nil, err diff --git a/backend/groth16/bls24-315/prove.go b/backend/groth16/bls24-315/prove.go index 92399a8d84..79cc5d8690 100644 --- a/backend/groth16/bls24-315/prove.go +++ b/backend/groth16/bls24-315/prove.go @@ -94,10 +94,6 @@ func Prove(r1cs *cs.R1CS, pk *ProvingKey, fullWitness witness.Witness, opts ...b }(i))) } - if r1cs.GkrInfo.Is() { - cs.ReplaceGkrHints(&solverOpts, r1cs.GkrInfo) - } - _solution, err := r1cs.Solve(fullWitness, solverOpts...) if err != nil { return nil, err diff --git a/backend/groth16/bls24-317/prove.go b/backend/groth16/bls24-317/prove.go index 76a60a3e3f..7f2c01f516 100644 --- a/backend/groth16/bls24-317/prove.go +++ b/backend/groth16/bls24-317/prove.go @@ -94,10 +94,6 @@ func Prove(r1cs *cs.R1CS, pk *ProvingKey, fullWitness witness.Witness, opts ...b }(i))) } - if r1cs.GkrInfo.Is() { - cs.ReplaceGkrHints(&solverOpts, r1cs.GkrInfo) - } - _solution, err := r1cs.Solve(fullWitness, solverOpts...) if err != nil { return nil, err diff --git a/backend/groth16/bn254/prove.go b/backend/groth16/bn254/prove.go index 3abdccbfd9..03b93c4d68 100644 --- a/backend/groth16/bn254/prove.go +++ b/backend/groth16/bn254/prove.go @@ -94,10 +94,6 @@ func Prove(r1cs *cs.R1CS, pk *ProvingKey, fullWitness witness.Witness, opts ...b }(i))) } - if r1cs.GkrInfo.Is() { - cs.ReplaceGkrHints(&solverOpts, r1cs.GkrInfo) - } - _solution, err := r1cs.Solve(fullWitness, solverOpts...) if err != nil { return nil, err diff --git a/backend/groth16/bw6-633/prove.go b/backend/groth16/bw6-633/prove.go index 9d20837acc..eea524dd62 100644 --- a/backend/groth16/bw6-633/prove.go +++ b/backend/groth16/bw6-633/prove.go @@ -94,10 +94,6 @@ func Prove(r1cs *cs.R1CS, pk *ProvingKey, fullWitness witness.Witness, opts ...b }(i))) } - if r1cs.GkrInfo.Is() { - cs.ReplaceGkrHints(&solverOpts, r1cs.GkrInfo) - } - _solution, err := r1cs.Solve(fullWitness, solverOpts...) if err != nil { return nil, err diff --git a/backend/groth16/bw6-761/prove.go b/backend/groth16/bw6-761/prove.go index 000f28fc50..4f31c19b20 100644 --- a/backend/groth16/bw6-761/prove.go +++ b/backend/groth16/bw6-761/prove.go @@ -94,10 +94,6 @@ func Prove(r1cs *cs.R1CS, pk *ProvingKey, fullWitness witness.Witness, opts ...b }(i))) } - if r1cs.GkrInfo.Is() { - cs.ReplaceGkrHints(&solverOpts, r1cs.GkrInfo) - } - _solution, err := r1cs.Solve(fullWitness, solverOpts...) if err != nil { return nil, err diff --git a/backend/plonk/bls12-377/prove.go b/backend/plonk/bls12-377/prove.go index ef004c4704..b2d3804587 100644 --- a/backend/plonk/bls12-377/prove.go +++ b/backend/plonk/bls12-377/prove.go @@ -127,10 +127,6 @@ func Prove(spr *cs.SparseR1CS, pk *ProvingKey, fullWitness witness.Witness, opts bsb22ComputeCommitmentHint(spr, pk, proof, cCommitments, &commitmentVal[i], i))) } - if spr.GkrInfo.Is() { - cs.ReplaceGkrHints(&opt.SolverOpts, spr.GkrInfo) - } - // query l, r, o in Lagrange basis, not blinded _solution, err := spr.Solve(fullWitness, opt.SolverOpts...) if err != nil { diff --git a/backend/plonk/bls12-381/prove.go b/backend/plonk/bls12-381/prove.go index 6e2f4b20fe..04af58ed95 100644 --- a/backend/plonk/bls12-381/prove.go +++ b/backend/plonk/bls12-381/prove.go @@ -127,10 +127,6 @@ func Prove(spr *cs.SparseR1CS, pk *ProvingKey, fullWitness witness.Witness, opts bsb22ComputeCommitmentHint(spr, pk, proof, cCommitments, &commitmentVal[i], i))) } - if spr.GkrInfo.Is() { - cs.ReplaceGkrHints(&opt.SolverOpts, spr.GkrInfo) - } - // query l, r, o in Lagrange basis, not blinded _solution, err := spr.Solve(fullWitness, opt.SolverOpts...) if err != nil { diff --git a/backend/plonk/bls24-315/prove.go b/backend/plonk/bls24-315/prove.go index ad48f92aa1..650e71714c 100644 --- a/backend/plonk/bls24-315/prove.go +++ b/backend/plonk/bls24-315/prove.go @@ -127,10 +127,6 @@ func Prove(spr *cs.SparseR1CS, pk *ProvingKey, fullWitness witness.Witness, opts bsb22ComputeCommitmentHint(spr, pk, proof, cCommitments, &commitmentVal[i], i))) } - if spr.GkrInfo.Is() { - cs.ReplaceGkrHints(&opt.SolverOpts, spr.GkrInfo) - } - // query l, r, o in Lagrange basis, not blinded _solution, err := spr.Solve(fullWitness, opt.SolverOpts...) if err != nil { diff --git a/backend/plonk/bls24-317/prove.go b/backend/plonk/bls24-317/prove.go index 6784bbb0c2..01eebfb5d6 100644 --- a/backend/plonk/bls24-317/prove.go +++ b/backend/plonk/bls24-317/prove.go @@ -127,10 +127,6 @@ func Prove(spr *cs.SparseR1CS, pk *ProvingKey, fullWitness witness.Witness, opts bsb22ComputeCommitmentHint(spr, pk, proof, cCommitments, &commitmentVal[i], i))) } - if spr.GkrInfo.Is() { - cs.ReplaceGkrHints(&opt.SolverOpts, spr.GkrInfo) - } - // query l, r, o in Lagrange basis, not blinded _solution, err := spr.Solve(fullWitness, opt.SolverOpts...) if err != nil { diff --git a/backend/plonk/bn254/prove.go b/backend/plonk/bn254/prove.go index 58a885d7ae..6058692704 100644 --- a/backend/plonk/bn254/prove.go +++ b/backend/plonk/bn254/prove.go @@ -127,10 +127,6 @@ func Prove(spr *cs.SparseR1CS, pk *ProvingKey, fullWitness witness.Witness, opts bsb22ComputeCommitmentHint(spr, pk, proof, cCommitments, &commitmentVal[i], i))) } - if spr.GkrInfo.Is() { - cs.ReplaceGkrHints(&opt.SolverOpts, spr.GkrInfo) - } - // query l, r, o in Lagrange basis, not blinded _solution, err := spr.Solve(fullWitness, opt.SolverOpts...) if err != nil { diff --git a/backend/plonk/bw6-633/prove.go b/backend/plonk/bw6-633/prove.go index e2b52f853f..abfc80c1ad 100644 --- a/backend/plonk/bw6-633/prove.go +++ b/backend/plonk/bw6-633/prove.go @@ -127,10 +127,6 @@ func Prove(spr *cs.SparseR1CS, pk *ProvingKey, fullWitness witness.Witness, opts bsb22ComputeCommitmentHint(spr, pk, proof, cCommitments, &commitmentVal[i], i))) } - if spr.GkrInfo.Is() { - cs.ReplaceGkrHints(&opt.SolverOpts, spr.GkrInfo) - } - // query l, r, o in Lagrange basis, not blinded _solution, err := spr.Solve(fullWitness, opt.SolverOpts...) if err != nil { diff --git a/backend/plonk/bw6-761/prove.go b/backend/plonk/bw6-761/prove.go index d0134d3798..1aba64a32e 100644 --- a/backend/plonk/bw6-761/prove.go +++ b/backend/plonk/bw6-761/prove.go @@ -127,10 +127,6 @@ func Prove(spr *cs.SparseR1CS, pk *ProvingKey, fullWitness witness.Witness, opts bsb22ComputeCommitmentHint(spr, pk, proof, cCommitments, &commitmentVal[i], i))) } - if spr.GkrInfo.Is() { - cs.ReplaceGkrHints(&opt.SolverOpts, spr.GkrInfo) - } - // query l, r, o in Lagrange basis, not blinded _solution, err := spr.Solve(fullWitness, opt.SolverOpts...) if err != nil { diff --git a/constraint/bls12-377/system.go b/constraint/bls12-377/system.go index 49ba1525ef..ebd5eecbe6 100644 --- a/constraint/bls12-377/system.go +++ b/constraint/bls12-377/system.go @@ -67,6 +67,10 @@ func (cs *system) Solve(witness witness.Witness, opts ...csolver.Option) (any, e v := witness.Vector().(fr.Vector) + if cs.GkrInfo.Is() { + ReplaceGkrHints(&opts, cs.GkrInfo) + } + // init the solver solver, err := newSolver(cs, v, opts...) if err != nil { diff --git a/constraint/bls12-381/system.go b/constraint/bls12-381/system.go index 5bcb7d44ee..54f904e700 100644 --- a/constraint/bls12-381/system.go +++ b/constraint/bls12-381/system.go @@ -67,6 +67,10 @@ func (cs *system) Solve(witness witness.Witness, opts ...csolver.Option) (any, e v := witness.Vector().(fr.Vector) + if cs.GkrInfo.Is() { + ReplaceGkrHints(&opts, cs.GkrInfo) + } + // init the solver solver, err := newSolver(cs, v, opts...) if err != nil { diff --git a/constraint/bls24-315/system.go b/constraint/bls24-315/system.go index 01ea069cb3..4a92f8b6a4 100644 --- a/constraint/bls24-315/system.go +++ b/constraint/bls24-315/system.go @@ -67,6 +67,10 @@ func (cs *system) Solve(witness witness.Witness, opts ...csolver.Option) (any, e v := witness.Vector().(fr.Vector) + if cs.GkrInfo.Is() { + ReplaceGkrHints(&opts, cs.GkrInfo) + } + // init the solver solver, err := newSolver(cs, v, opts...) if err != nil { diff --git a/constraint/bls24-317/system.go b/constraint/bls24-317/system.go index 06633648ce..b5887ee9cf 100644 --- a/constraint/bls24-317/system.go +++ b/constraint/bls24-317/system.go @@ -67,6 +67,10 @@ func (cs *system) Solve(witness witness.Witness, opts ...csolver.Option) (any, e v := witness.Vector().(fr.Vector) + if cs.GkrInfo.Is() { + ReplaceGkrHints(&opts, cs.GkrInfo) + } + // init the solver solver, err := newSolver(cs, v, opts...) if err != nil { diff --git a/constraint/bn254/system.go b/constraint/bn254/system.go index a96fe5d5a2..37e746f8ca 100644 --- a/constraint/bn254/system.go +++ b/constraint/bn254/system.go @@ -67,6 +67,10 @@ func (cs *system) Solve(witness witness.Witness, opts ...csolver.Option) (any, e v := witness.Vector().(fr.Vector) + if cs.GkrInfo.Is() { + ReplaceGkrHints(&opts, cs.GkrInfo) + } + // init the solver solver, err := newSolver(cs, v, opts...) if err != nil { diff --git a/constraint/bw6-633/system.go b/constraint/bw6-633/system.go index 7cd6843fc5..fcb79fadba 100644 --- a/constraint/bw6-633/system.go +++ b/constraint/bw6-633/system.go @@ -67,6 +67,10 @@ func (cs *system) Solve(witness witness.Witness, opts ...csolver.Option) (any, e v := witness.Vector().(fr.Vector) + if cs.GkrInfo.Is() { + ReplaceGkrHints(&opts, cs.GkrInfo) + } + // init the solver solver, err := newSolver(cs, v, opts...) if err != nil { diff --git a/constraint/bw6-761/system.go b/constraint/bw6-761/system.go index 75252436e8..6fa72c6617 100644 --- a/constraint/bw6-761/system.go +++ b/constraint/bw6-761/system.go @@ -67,6 +67,10 @@ func (cs *system) Solve(witness witness.Witness, opts ...csolver.Option) (any, e v := witness.Vector().(fr.Vector) + if cs.GkrInfo.Is() { + ReplaceGkrHints(&opts, cs.GkrInfo) + } + // init the solver solver, err := newSolver(cs, v, opts...) if err != nil { diff --git a/internal/generator/backend/template/representations/system.go.tmpl b/internal/generator/backend/template/representations/system.go.tmpl index dc5b4a98d3..ab3438dcbe 100644 --- a/internal/generator/backend/template/representations/system.go.tmpl +++ b/internal/generator/backend/template/representations/system.go.tmpl @@ -48,10 +48,15 @@ func newSystem(capacity int, t constraint.SystemType) *system { func (cs *system) Solve(witness witness.Witness, opts ...csolver.Option) (any, error) { log := logger.Logger().With().Int("nbConstraints", cs.GetNbConstraints()).Logger() start := time.Now() - v := witness.Vector().(fr.Vector) + {{if ne .CurveID "UNKNOWN"}} {{/* TODO @Tabaie add tinyfield GKR support */}} + if cs.GkrInfo.Is() { + ReplaceGkrHints(&opts, cs.GkrInfo) + } + {{- end}} + // init the solver solver, err := newSolver(cs, v, opts...) if err != nil { @@ -98,7 +103,6 @@ func (cs *system) IsSolved(witness witness.Witness, opts ...csolver.Option) erro return err } - // GetR1Cs return the list of R1C func (cs *system) GetR1Cs() []constraint.R1C { toReturn := make([]constraint.R1C, 0, cs.GetNbConstraints()) diff --git a/internal/generator/backend/template/zkpschemes/groth16/groth16.prove.go.tmpl b/internal/generator/backend/template/zkpschemes/groth16/groth16.prove.go.tmpl index 93d77e9234..760cf22d24 100644 --- a/internal/generator/backend/template/zkpschemes/groth16/groth16.prove.go.tmpl +++ b/internal/generator/backend/template/zkpschemes/groth16/groth16.prove.go.tmpl @@ -77,10 +77,6 @@ func Prove(r1cs *cs.R1CS, pk *ProvingKey, fullWitness witness.Witness, opts ...b }(i))) } - if r1cs.GkrInfo.Is() { - cs.ReplaceGkrHints(&solverOpts, r1cs.GkrInfo) - } - _solution, err := r1cs.Solve(fullWitness, solverOpts...) if err != nil { return nil, err diff --git a/internal/generator/backend/template/zkpschemes/plonk/plonk.prove.go.tmpl b/internal/generator/backend/template/zkpschemes/plonk/plonk.prove.go.tmpl index 426924c305..a869528cf9 100644 --- a/internal/generator/backend/template/zkpschemes/plonk/plonk.prove.go.tmpl +++ b/internal/generator/backend/template/zkpschemes/plonk/plonk.prove.go.tmpl @@ -105,10 +105,6 @@ func Prove(spr *cs.SparseR1CS, pk *ProvingKey, fullWitness witness.Witness, opts bsb22ComputeCommitmentHint(spr, pk, proof, cCommitments, &commitmentVal[i], i))) } - if spr.GkrInfo.Is() { - cs.ReplaceGkrHints(&opt.SolverOpts, spr.GkrInfo) - } - // query l, r, o in Lagrange basis, not blinded _solution, err := spr.Solve(fullWitness, opt.SolverOpts...) if err != nil {