From 97045eea226bd3a5b08605632f92af6e7e11ad3f Mon Sep 17 00:00:00 2001 From: Arya Tabaie Date: Tue, 20 Jun 2023 11:58:47 -0500 Subject: [PATCH] refactor: compactify commitment tests --- backend/groth16/bls12-377/setup.go | 2 +- backend/groth16/bls12-381/setup.go | 2 +- backend/groth16/bls24-315/setup.go | 2 +- backend/groth16/bls24-317/setup.go | 2 +- backend/groth16/bn254/setup.go | 2 +- backend/groth16/bw6-633/setup.go | 2 +- backend/groth16/bw6-761/setup.go | 2 +- .../zkpschemes/groth16/groth16.setup.go.tmpl | 2 +- test/commitments_test.go | 74 +++++++------------ 9 files changed, 33 insertions(+), 57 deletions(-) diff --git a/backend/groth16/bls12-377/setup.go b/backend/groth16/bls12-377/setup.go index e25d18113c..2b6add4ba9 100644 --- a/backend/groth16/bls12-377/setup.go +++ b/backend/groth16/bls12-377/setup.go @@ -655,7 +655,7 @@ func (pk *ProvingKey) NbG2() int { return 2 + len(pk.G2.B) } -// bitRerverse permutation as in fft.BitReverse , but with []curve.G1Affine +// bitReverse permutation as in fft.BitReverse , but with []curve.G1Affine func bitReverse(a []curve.G1Affine) { n := uint(len(a)) nn := uint(bits.UintSize - bits.TrailingZeros(n)) diff --git a/backend/groth16/bls12-381/setup.go b/backend/groth16/bls12-381/setup.go index d9aec8fa02..eae8f007e3 100644 --- a/backend/groth16/bls12-381/setup.go +++ b/backend/groth16/bls12-381/setup.go @@ -655,7 +655,7 @@ func (pk *ProvingKey) NbG2() int { return 2 + len(pk.G2.B) } -// bitRerverse permutation as in fft.BitReverse , but with []curve.G1Affine +// bitReverse permutation as in fft.BitReverse , but with []curve.G1Affine func bitReverse(a []curve.G1Affine) { n := uint(len(a)) nn := uint(bits.UintSize - bits.TrailingZeros(n)) diff --git a/backend/groth16/bls24-315/setup.go b/backend/groth16/bls24-315/setup.go index caa03a0243..0780e815a9 100644 --- a/backend/groth16/bls24-315/setup.go +++ b/backend/groth16/bls24-315/setup.go @@ -655,7 +655,7 @@ func (pk *ProvingKey) NbG2() int { return 2 + len(pk.G2.B) } -// bitRerverse permutation as in fft.BitReverse , but with []curve.G1Affine +// bitReverse permutation as in fft.BitReverse , but with []curve.G1Affine func bitReverse(a []curve.G1Affine) { n := uint(len(a)) nn := uint(bits.UintSize - bits.TrailingZeros(n)) diff --git a/backend/groth16/bls24-317/setup.go b/backend/groth16/bls24-317/setup.go index 83038d9dba..8005edb029 100644 --- a/backend/groth16/bls24-317/setup.go +++ b/backend/groth16/bls24-317/setup.go @@ -655,7 +655,7 @@ func (pk *ProvingKey) NbG2() int { return 2 + len(pk.G2.B) } -// bitRerverse permutation as in fft.BitReverse , but with []curve.G1Affine +// bitReverse permutation as in fft.BitReverse , but with []curve.G1Affine func bitReverse(a []curve.G1Affine) { n := uint(len(a)) nn := uint(bits.UintSize - bits.TrailingZeros(n)) diff --git a/backend/groth16/bn254/setup.go b/backend/groth16/bn254/setup.go index 51ddb0d1fe..7c85a63e7b 100644 --- a/backend/groth16/bn254/setup.go +++ b/backend/groth16/bn254/setup.go @@ -655,7 +655,7 @@ func (pk *ProvingKey) NbG2() int { return 2 + len(pk.G2.B) } -// bitRerverse permutation as in fft.BitReverse , but with []curve.G1Affine +// bitReverse permutation as in fft.BitReverse , but with []curve.G1Affine func bitReverse(a []curve.G1Affine) { n := uint(len(a)) nn := uint(bits.UintSize - bits.TrailingZeros(n)) diff --git a/backend/groth16/bw6-633/setup.go b/backend/groth16/bw6-633/setup.go index f24acbdd03..5f4a3299f9 100644 --- a/backend/groth16/bw6-633/setup.go +++ b/backend/groth16/bw6-633/setup.go @@ -655,7 +655,7 @@ func (pk *ProvingKey) NbG2() int { return 2 + len(pk.G2.B) } -// bitRerverse permutation as in fft.BitReverse , but with []curve.G1Affine +// bitReverse permutation as in fft.BitReverse , but with []curve.G1Affine func bitReverse(a []curve.G1Affine) { n := uint(len(a)) nn := uint(bits.UintSize - bits.TrailingZeros(n)) diff --git a/backend/groth16/bw6-761/setup.go b/backend/groth16/bw6-761/setup.go index 9123efa717..e83fa0d489 100644 --- a/backend/groth16/bw6-761/setup.go +++ b/backend/groth16/bw6-761/setup.go @@ -655,7 +655,7 @@ func (pk *ProvingKey) NbG2() int { return 2 + len(pk.G2.B) } -// bitRerverse permutation as in fft.BitReverse , but with []curve.G1Affine +// bitReverse permutation as in fft.BitReverse , but with []curve.G1Affine func bitReverse(a []curve.G1Affine) { n := uint(len(a)) nn := uint(bits.UintSize - bits.TrailingZeros(n)) diff --git a/internal/generator/backend/template/zkpschemes/groth16/groth16.setup.go.tmpl b/internal/generator/backend/template/zkpschemes/groth16/groth16.setup.go.tmpl index 1d4e6bbf8f..8214b173a2 100644 --- a/internal/generator/backend/template/zkpschemes/groth16/groth16.setup.go.tmpl +++ b/internal/generator/backend/template/zkpschemes/groth16/groth16.setup.go.tmpl @@ -638,7 +638,7 @@ func (pk *ProvingKey) NbG2() int { return 2 + len(pk.G2.B) } -// bitRerverse permutation as in fft.BitReverse , but with []curve.G1Affine +// bitReverse permutation as in fft.BitReverse , but with []curve.G1Affine func bitReverse(a []curve.G1Affine) { n := uint(len(a)) nn := uint(bits.UintSize - bits.TrailingZeros(n)) diff --git a/test/commitments_test.go b/test/commitments_test.go index d0a1988862..ec3c3f17ac 100644 --- a/test/commitments_test.go +++ b/test/commitments_test.go @@ -1,6 +1,7 @@ package test import ( + "github.com/consensys/gnark/backend" "reflect" "testing" @@ -20,10 +21,6 @@ func (c *noCommitmentCircuit) Define(api frontend.API) error { return nil } -func TestNoCommitmentCircuit(t *testing.T) { - testAll(t, &noCommitmentCircuit{1}) -} - type commitmentCircuit struct { Public []frontend.Variable `gnark:",public"` X []frontend.Variable @@ -46,31 +43,6 @@ func (c *commitmentCircuit) Define(api frontend.API) error { return nil } -func TestSingleCommitment(t *testing.T) { - assignment := &commitmentCircuit{X: []frontend.Variable{1}, Public: []frontend.Variable{}} - testAll(t, assignment) -} - -func TestTwoCommitments(t *testing.T) { - assignment := &commitmentCircuit{X: []frontend.Variable{1, 2}, Public: []frontend.Variable{}} - testAll(t, assignment) -} - -func TestFiveCommitments(t *testing.T) { - assignment := &commitmentCircuit{X: []frontend.Variable{1, 2, 3, 4, 5}, Public: []frontend.Variable{}} - testAll(t, assignment) -} - -func TestSingleCommitmentSinglePublic(t *testing.T) { - assignment := &commitmentCircuit{X: []frontend.Variable{0}, Public: []frontend.Variable{1}} - testAll(t, assignment) -} - -func TestFiveCommitmentsFivePublic(t *testing.T) { - assignment := &commitmentCircuit{X: []frontend.Variable{0, 1, 2, 3, 4}, Public: []frontend.Variable{1, 2, 3, 4, 5}} - testAll(t, assignment) -} - type committedConstantCircuit struct { X frontend.Variable } @@ -84,10 +56,6 @@ func (c *committedConstantCircuit) Define(api frontend.API) error { return nil } -func TestCommittedConstant(t *testing.T) { - testAll(t, &committedConstantCircuit{1}) -} - type committedPublicCircuit struct { X frontend.Variable `gnark:",public"` } @@ -101,10 +69,6 @@ func (c *committedPublicCircuit) Define(api frontend.API) error { return nil } -func TestCommittedPublic(t *testing.T) { - testAll(t, &committedPublicCircuit{1}) -} - type independentCommitsCircuit struct { X []frontend.Variable } @@ -121,10 +85,6 @@ func (c *independentCommitsCircuit) Define(api frontend.API) error { return nil } -func TestTwoIndependentCommits(t *testing.T) { - testAll(t, &independentCommitsCircuit{X: []frontend.Variable{1, 1}}) -} - type twoCommitCircuit struct { X []frontend.Variable Y frontend.Variable @@ -143,10 +103,6 @@ func (c *twoCommitCircuit) Define(api frontend.API) error { return nil } -func TestTwoCommit(t *testing.T) { - testAll(t, &twoCommitCircuit{X: []frontend.Variable{1, 2}, Y: 3}) -} - type doubleCommitCircuit struct { X, Y frontend.Variable } @@ -164,10 +120,6 @@ func (c *doubleCommitCircuit) Define(api frontend.API) error { return nil } -func TestDoubleCommit(t *testing.T) { - testAll(t, &doubleCommitCircuit{X: 1, Y: 2}) -} - func TestHollow(t *testing.T) { run := func(c, expected frontend.Circuit) func(t *testing.T) { @@ -222,3 +174,27 @@ func TestCommitUniquenessZerosScs(t *testing.T) { // TODO @Tabaie Randomize Grot _, err = ccs.Solve(w) assert.NoError(t, err) } + +var commitmentTestCircuits []frontend.Circuit + +func init() { + commitmentTestCircuits = []frontend.Circuit{ + &noCommitmentCircuit{1}, + &commitmentCircuit{X: []frontend.Variable{1}, Public: []frontend.Variable{}}, // single commitment + &commitmentCircuit{X: []frontend.Variable{1, 2}, Public: []frontend.Variable{}}, // two commitments + &commitmentCircuit{X: []frontend.Variable{1, 2, 3, 4, 5}, Public: []frontend.Variable{}}, // five commitments + &commitmentCircuit{X: []frontend.Variable{0}, Public: []frontend.Variable{1}}, // single commitment single public + &commitmentCircuit{X: []frontend.Variable{0, 1, 2, 3, 4}, Public: []frontend.Variable{1, 2, 3, 4, 5}}, // five commitments five public + &committedConstantCircuit{1}, // single committed constant + &committedPublicCircuit{1}, // single committed public + &independentCommitsCircuit{X: []frontend.Variable{1, 1}}, // two independent commitments + &twoCommitCircuit{X: []frontend.Variable{1, 2}, Y: 3}, // two commitments, second depending on first + &doubleCommitCircuit{X: 1, Y: 2}, // double committing to the same variable + } +} + +func TestCommitment(t *testing.T) { + for _, assignment := range commitmentTestCircuits { + NewAssert(t).ProverSucceeded(hollow(assignment), assignment, WithBackends(backend.GROTH16, backend.PLONK)) + } +}