-
Notifications
You must be signed in to change notification settings - Fork 370
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: more precomputation in plonk/iop (#471)
* perf: batch convert to lagrange coset * refactor: revert previous commit and move tests in backend/plonk * perf: pre-allocate some slices capacity to avoid mem moves * perf: pre compute lagrange coset basis poly once * refactor: kill EvaluationPermutationBigDomainBitReversed unused * refactor: keeping up * fix: fix Vector pointer type update in witness package * build: latest gnark-crypto * build: update to gnark-crypto v0.9.1 * docs: update comment * build: re-ran gogenerate * build: update go.mod deps * build: update go.mod deps * fix: recompute lagrange poly after deserializing step plonk pk
- Loading branch information
Showing
44 changed files
with
1,365 additions
and
2,588 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
package plonk_test | ||
|
||
import ( | ||
"bytes" | ||
"math/big" | ||
"testing" | ||
|
||
"github.com/consensys/gnark" | ||
"github.com/consensys/gnark-crypto/ecc" | ||
"github.com/consensys/gnark-crypto/kzg" | ||
"github.com/consensys/gnark/backend/plonk" | ||
"github.com/consensys/gnark/constraint" | ||
"github.com/consensys/gnark/frontend" | ||
"github.com/consensys/gnark/frontend/cs/scs" | ||
"github.com/consensys/gnark/test" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
//--------------------// | ||
// benches // | ||
//--------------------// | ||
|
||
func TestProver(t *testing.T) { | ||
|
||
for _, curve := range getCurves() { | ||
t.Run(curve.String(), func(t *testing.T) { | ||
var b1, b2 bytes.Buffer | ||
assert := require.New(t) | ||
|
||
ccs, _solution, srs := referenceCircuit(curve) | ||
fullWitness, err := frontend.NewWitness(_solution, curve.ScalarField()) | ||
assert.NoError(err) | ||
|
||
publicWitness, err := fullWitness.Public() | ||
assert.NoError(err) | ||
|
||
pk, vk, err := plonk.Setup(ccs, srs) | ||
assert.NoError(err) | ||
|
||
// write the PK to ensure it is not mutated | ||
_, err = pk.WriteTo(&b1) | ||
assert.NoError(err) | ||
|
||
proof, err := plonk.Prove(ccs, pk, fullWitness) | ||
assert.NoError(err) | ||
|
||
// check pk | ||
_, err = pk.WriteTo(&b2) | ||
assert.NoError(err) | ||
|
||
assert.True(bytes.Equal(b1.Bytes(), b2.Bytes()), "plonk prover mutated the proving key") | ||
|
||
err = plonk.Verify(proof, vk, publicWitness) | ||
assert.NoError(err) | ||
|
||
}) | ||
|
||
} | ||
} | ||
|
||
func BenchmarkSetup(b *testing.B) { | ||
for _, curve := range getCurves() { | ||
b.Run(curve.String(), func(b *testing.B) { | ||
ccs, _, srs := referenceCircuit(curve) | ||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
_, _, _ = plonk.Setup(ccs, srs) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func BenchmarkProver(b *testing.B) { | ||
for _, curve := range getCurves() { | ||
b.Run(curve.String(), func(b *testing.B) { | ||
ccs, _solution, srs := referenceCircuit(curve) | ||
fullWitness, err := frontend.NewWitness(_solution, curve.ScalarField()) | ||
if err != nil { | ||
b.Fatal(err) | ||
} | ||
pk, _, err := plonk.Setup(ccs, srs) | ||
if err != nil { | ||
b.Fatal(err) | ||
} | ||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
_, _ = plonk.Prove(ccs, pk, fullWitness) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func BenchmarkVerifier(b *testing.B) { | ||
for _, curve := range getCurves() { | ||
b.Run(curve.String(), func(b *testing.B) { | ||
ccs, _solution, srs := referenceCircuit(curve) | ||
fullWitness, err := frontend.NewWitness(_solution, curve.ScalarField()) | ||
if err != nil { | ||
b.Fatal(err) | ||
} | ||
publicWitness, err := fullWitness.Public() | ||
if err != nil { | ||
b.Fatal(err) | ||
} | ||
|
||
pk, vk, err := plonk.Setup(ccs, srs) | ||
if err != nil { | ||
b.Fatal(err) | ||
} | ||
proof, err := plonk.Prove(ccs, pk, fullWitness) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
_ = plonk.Verify(proof, vk, publicWitness) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
type refCircuit struct { | ||
nbConstraints int | ||
X frontend.Variable | ||
Y frontend.Variable `gnark:",public"` | ||
} | ||
|
||
func (circuit *refCircuit) Define(api frontend.API) error { | ||
for i := 0; i < circuit.nbConstraints; i++ { | ||
circuit.X = api.Mul(circuit.X, circuit.X) | ||
} | ||
api.AssertIsEqual(circuit.X, circuit.Y) | ||
return nil | ||
} | ||
|
||
func referenceCircuit(curve ecc.ID) (constraint.ConstraintSystem, frontend.Circuit, kzg.SRS) { | ||
const nbConstraints = 40000 | ||
circuit := refCircuit{ | ||
nbConstraints: nbConstraints, | ||
} | ||
ccs, err := frontend.Compile(curve.ScalarField(), scs.NewBuilder, &circuit) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
var good refCircuit | ||
good.X = 2 | ||
|
||
// compute expected Y | ||
expectedY := new(big.Int).SetUint64(2) | ||
exp := big.NewInt(1) | ||
exp.Lsh(exp, nbConstraints) | ||
expectedY.Exp(expectedY, exp, curve.ScalarField()) | ||
|
||
good.Y = expectedY | ||
srs, err := test.NewKZGSRS(ccs) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return ccs, &good, srs | ||
} | ||
|
||
func getCurves() []ecc.ID { | ||
if testing.Short() { | ||
return []ecc.ID{ecc.BN254} | ||
} | ||
return gnark.Curves() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.