From 30a179f7e886f69535c017e0ab7f22e78aec71b6 Mon Sep 17 00:00:00 2001 From: BeratOz01 Date: Mon, 4 Mar 2024 00:32:51 +0300 Subject: [PATCH 1/4] fix: add gate evaluation bug for gkr --- ecc/bls12-377/fr/gkr/gkr.go | 4 +++- ecc/bls12-378/fr/gkr/gkr.go | 4 +++- ecc/bls12-381/fr/gkr/gkr.go | 4 +++- ecc/bls24-315/fr/gkr/gkr.go | 4 +++- ecc/bls24-317/fr/gkr/gkr.go | 4 +++- ecc/bn254/fr/gkr/gkr.go | 4 +++- ecc/bw6-633/fr/gkr/gkr.go | 4 +++- ecc/bw6-756/fr/gkr/gkr.go | 4 +++- ecc/bw6-761/fr/gkr/gkr.go | 4 +++- internal/generator/gkr/template/gkr.go.tmpl | 4 +++- .../generator/test_vector_utils/small_rational/gkr/gkr.go | 4 +++- 11 files changed, 33 insertions(+), 11 deletions(-) diff --git a/ecc/bls12-377/fr/gkr/gkr.go b/ecc/bls12-377/fr/gkr/gkr.go index 19f4e9bb2..ff5f60f1d 100644 --- a/ecc/bls12-377/fr/gkr/gkr.go +++ b/ecc/bls12-377/fr/gkr/gkr.go @@ -886,8 +886,10 @@ func (g AddGate) Evaluate(x ...fr.Element) (res fr.Element) { res.Set(&x[0]) case 2: res.Add(&x[0], &x[1]) + default: + res.Add(&x[0], &x[1]) for i := 2; i < len(x); i++ { - res.Add(&res, &x[2]) + res.Add(&res, &x[i]) } } return diff --git a/ecc/bls12-378/fr/gkr/gkr.go b/ecc/bls12-378/fr/gkr/gkr.go index c576d7aca..5241117e6 100644 --- a/ecc/bls12-378/fr/gkr/gkr.go +++ b/ecc/bls12-378/fr/gkr/gkr.go @@ -886,8 +886,10 @@ func (g AddGate) Evaluate(x ...fr.Element) (res fr.Element) { res.Set(&x[0]) case 2: res.Add(&x[0], &x[1]) + default: + res.Add(&x[0], &x[1]) for i := 2; i < len(x); i++ { - res.Add(&res, &x[2]) + res.Add(&res, &x[i]) } } return diff --git a/ecc/bls12-381/fr/gkr/gkr.go b/ecc/bls12-381/fr/gkr/gkr.go index 64b48da06..eda514b61 100644 --- a/ecc/bls12-381/fr/gkr/gkr.go +++ b/ecc/bls12-381/fr/gkr/gkr.go @@ -886,8 +886,10 @@ func (g AddGate) Evaluate(x ...fr.Element) (res fr.Element) { res.Set(&x[0]) case 2: res.Add(&x[0], &x[1]) + default: + res.Add(&x[0], &x[1]) for i := 2; i < len(x); i++ { - res.Add(&res, &x[2]) + res.Add(&res, &x[i]) } } return diff --git a/ecc/bls24-315/fr/gkr/gkr.go b/ecc/bls24-315/fr/gkr/gkr.go index ca1f4c833..881134cc7 100644 --- a/ecc/bls24-315/fr/gkr/gkr.go +++ b/ecc/bls24-315/fr/gkr/gkr.go @@ -886,8 +886,10 @@ func (g AddGate) Evaluate(x ...fr.Element) (res fr.Element) { res.Set(&x[0]) case 2: res.Add(&x[0], &x[1]) + default: + res.Add(&x[0], &x[1]) for i := 2; i < len(x); i++ { - res.Add(&res, &x[2]) + res.Add(&res, &x[i]) } } return diff --git a/ecc/bls24-317/fr/gkr/gkr.go b/ecc/bls24-317/fr/gkr/gkr.go index 0dc5b4883..aab3a0226 100644 --- a/ecc/bls24-317/fr/gkr/gkr.go +++ b/ecc/bls24-317/fr/gkr/gkr.go @@ -886,8 +886,10 @@ func (g AddGate) Evaluate(x ...fr.Element) (res fr.Element) { res.Set(&x[0]) case 2: res.Add(&x[0], &x[1]) + default: + res.Add(&x[0], &x[1]) for i := 2; i < len(x); i++ { - res.Add(&res, &x[2]) + res.Add(&res, &x[i]) } } return diff --git a/ecc/bn254/fr/gkr/gkr.go b/ecc/bn254/fr/gkr/gkr.go index df189b19d..ef83b72ab 100644 --- a/ecc/bn254/fr/gkr/gkr.go +++ b/ecc/bn254/fr/gkr/gkr.go @@ -886,8 +886,10 @@ func (g AddGate) Evaluate(x ...fr.Element) (res fr.Element) { res.Set(&x[0]) case 2: res.Add(&x[0], &x[1]) + default: + res.Add(&x[0], &x[1]) for i := 2; i < len(x); i++ { - res.Add(&res, &x[2]) + res.Add(&res, &x[i]) } } return diff --git a/ecc/bw6-633/fr/gkr/gkr.go b/ecc/bw6-633/fr/gkr/gkr.go index 888c2c3c5..71106ecc3 100644 --- a/ecc/bw6-633/fr/gkr/gkr.go +++ b/ecc/bw6-633/fr/gkr/gkr.go @@ -886,8 +886,10 @@ func (g AddGate) Evaluate(x ...fr.Element) (res fr.Element) { res.Set(&x[0]) case 2: res.Add(&x[0], &x[1]) + default: + res.Add(&x[0], &x[1]) for i := 2; i < len(x); i++ { - res.Add(&res, &x[2]) + res.Add(&res, &x[i]) } } return diff --git a/ecc/bw6-756/fr/gkr/gkr.go b/ecc/bw6-756/fr/gkr/gkr.go index 5fe30d6d2..8ea9c04a2 100644 --- a/ecc/bw6-756/fr/gkr/gkr.go +++ b/ecc/bw6-756/fr/gkr/gkr.go @@ -886,8 +886,10 @@ func (g AddGate) Evaluate(x ...fr.Element) (res fr.Element) { res.Set(&x[0]) case 2: res.Add(&x[0], &x[1]) + default: + res.Add(&x[0], &x[1]) for i := 2; i < len(x); i++ { - res.Add(&res, &x[2]) + res.Add(&res, &x[i]) } } return diff --git a/ecc/bw6-761/fr/gkr/gkr.go b/ecc/bw6-761/fr/gkr/gkr.go index c98a8ab05..d0247f47e 100644 --- a/ecc/bw6-761/fr/gkr/gkr.go +++ b/ecc/bw6-761/fr/gkr/gkr.go @@ -886,8 +886,10 @@ func (g AddGate) Evaluate(x ...fr.Element) (res fr.Element) { res.Set(&x[0]) case 2: res.Add(&x[0], &x[1]) + default: + res.Add(&x[0], &x[1]) for i := 2; i < len(x); i++ { - res.Add(&res, &x[2]) + res.Add(&res, &x[i]) } } return diff --git a/internal/generator/gkr/template/gkr.go.tmpl b/internal/generator/gkr/template/gkr.go.tmpl index 049702508..d0b978072 100644 --- a/internal/generator/gkr/template/gkr.go.tmpl +++ b/internal/generator/gkr/template/gkr.go.tmpl @@ -871,8 +871,10 @@ func (g AddGate) Evaluate(x ...{{.ElementType}}) (res {{.ElementType}}) { res.Set(&x[0]) case 2: res.Add(&x[0], &x[1]) + default: + res.Add(&x[0], &x[1]) for i := 2; i < len(x); i++ { - res.Add(&res, &x[2]) + res.Add(&res, &x[i]) } } return diff --git a/internal/generator/test_vector_utils/small_rational/gkr/gkr.go b/internal/generator/test_vector_utils/small_rational/gkr/gkr.go index a2bef7d1e..0b04a0b46 100644 --- a/internal/generator/test_vector_utils/small_rational/gkr/gkr.go +++ b/internal/generator/test_vector_utils/small_rational/gkr/gkr.go @@ -886,8 +886,10 @@ func (g AddGate) Evaluate(x ...small_rational.SmallRational) (res small_rational res.Set(&x[0]) case 2: res.Add(&x[0], &x[1]) + default: + res.Add(&x[0], &x[1]) for i := 2; i < len(x); i++ { - res.Add(&res, &x[2]) + res.Add(&res, &x[i]) } } return From 76174a72c841e14c94a195d460e86d6cfc03aaae Mon Sep 17 00:00:00 2001 From: BeratOz01 Date: Mon, 4 Mar 2024 18:01:13 +0300 Subject: [PATCH 2/4] test(gkr): add gate tests added --- ecc/bls12-377/fr/gkr/gkr_test.go | 27 +++++++++++++++++++ ecc/bls12-378/fr/gkr/gkr_test.go | 27 +++++++++++++++++++ ecc/bls12-381/fr/gkr/gkr_test.go | 27 +++++++++++++++++++ ecc/bls24-315/fr/gkr/gkr_test.go | 27 +++++++++++++++++++ ecc/bls24-317/fr/gkr/gkr_test.go | 27 +++++++++++++++++++ ecc/bn254/fr/gkr/gkr_test.go | 27 +++++++++++++++++++ ecc/bw6-633/fr/gkr/gkr_test.go | 27 +++++++++++++++++++ ecc/bw6-756/fr/gkr/gkr_test.go | 27 +++++++++++++++++++ ecc/bw6-761/fr/gkr/gkr_test.go | 27 +++++++++++++++++++ .../generator/gkr/template/gkr.test.go.tmpl | 27 +++++++++++++++++++ 10 files changed, 270 insertions(+) diff --git a/ecc/bls12-377/fr/gkr/gkr_test.go b/ecc/bls12-377/fr/gkr/gkr_test.go index 15ff485d9..d80d1e39f 100644 --- a/ecc/bls12-377/fr/gkr/gkr_test.go +++ b/ecc/bls12-377/fr/gkr/gkr_test.go @@ -45,6 +45,14 @@ func TestNoGate(t *testing.T) { testManyInstances(t, 1, testNoGate) } +func TestSingleAddGateTwoInstances(t *testing.T) { + testSingleAddGate(t, []fr.Element{four, three}, []fr.Element{two, three}) +} + +func TestSingleAddGate(t *testing.T) { + testManyInstances(t, 2, testSingleAddGate) +} + func TestSingleMulGateTwoInstances(t *testing.T) { testSingleMulGate(t, []fr.Element{four, three}, []fr.Element{two, three}) } @@ -203,6 +211,25 @@ func testNoGate(t *testing.T, inputAssignments ...[]fr.Element) { assert.NoError(t, err, "proof rejected") } +func testSingleAddGate(t *testing.T, inputAssignments ...[]fr.Element) { + c := make(Circuit, 3) + c[2] = Wire{ + Gate: Gates["add"], + Inputs: []*Wire{&c[0], &c[1]}, + } + + assignment := WireAssignment{&c[0]: inputAssignments[0], &c[1]: inputAssignments[1]}.Complete(c) + + proof, err := Prove(c, assignment, fiatshamir.WithHash(test_vector_utils.NewMessageCounter(1, 1))) + assert.NoError(t, err) + + err = Verify(c, assignment, proof, fiatshamir.WithHash(test_vector_utils.NewMessageCounter(1, 1))) + assert.NoError(t, err, "proof rejected") + + err = Verify(c, assignment, proof, fiatshamir.WithHash(test_vector_utils.NewMessageCounter(0, 1))) + assert.NotNil(t, err, "bad proof accepted") +} + func testSingleMulGate(t *testing.T, inputAssignments ...[]fr.Element) { c := make(Circuit, 3) diff --git a/ecc/bls12-378/fr/gkr/gkr_test.go b/ecc/bls12-378/fr/gkr/gkr_test.go index 462ee5fdd..aef3b7b07 100644 --- a/ecc/bls12-378/fr/gkr/gkr_test.go +++ b/ecc/bls12-378/fr/gkr/gkr_test.go @@ -45,6 +45,14 @@ func TestNoGate(t *testing.T) { testManyInstances(t, 1, testNoGate) } +func TestSingleAddGateTwoInstances(t *testing.T) { + testSingleAddGate(t, []fr.Element{four, three}, []fr.Element{two, three}) +} + +func TestSingleAddGate(t *testing.T) { + testManyInstances(t, 2, testSingleAddGate) +} + func TestSingleMulGateTwoInstances(t *testing.T) { testSingleMulGate(t, []fr.Element{four, three}, []fr.Element{two, three}) } @@ -203,6 +211,25 @@ func testNoGate(t *testing.T, inputAssignments ...[]fr.Element) { assert.NoError(t, err, "proof rejected") } +func testSingleAddGate(t *testing.T, inputAssignments ...[]fr.Element) { + c := make(Circuit, 3) + c[2] = Wire{ + Gate: Gates["add"], + Inputs: []*Wire{&c[0], &c[1]}, + } + + assignment := WireAssignment{&c[0]: inputAssignments[0], &c[1]: inputAssignments[1]}.Complete(c) + + proof, err := Prove(c, assignment, fiatshamir.WithHash(test_vector_utils.NewMessageCounter(1, 1))) + assert.NoError(t, err) + + err = Verify(c, assignment, proof, fiatshamir.WithHash(test_vector_utils.NewMessageCounter(1, 1))) + assert.NoError(t, err, "proof rejected") + + err = Verify(c, assignment, proof, fiatshamir.WithHash(test_vector_utils.NewMessageCounter(0, 1))) + assert.NotNil(t, err, "bad proof accepted") +} + func testSingleMulGate(t *testing.T, inputAssignments ...[]fr.Element) { c := make(Circuit, 3) diff --git a/ecc/bls12-381/fr/gkr/gkr_test.go b/ecc/bls12-381/fr/gkr/gkr_test.go index bf6877cb5..1c3ee3d23 100644 --- a/ecc/bls12-381/fr/gkr/gkr_test.go +++ b/ecc/bls12-381/fr/gkr/gkr_test.go @@ -45,6 +45,14 @@ func TestNoGate(t *testing.T) { testManyInstances(t, 1, testNoGate) } +func TestSingleAddGateTwoInstances(t *testing.T) { + testSingleAddGate(t, []fr.Element{four, three}, []fr.Element{two, three}) +} + +func TestSingleAddGate(t *testing.T) { + testManyInstances(t, 2, testSingleAddGate) +} + func TestSingleMulGateTwoInstances(t *testing.T) { testSingleMulGate(t, []fr.Element{four, three}, []fr.Element{two, three}) } @@ -203,6 +211,25 @@ func testNoGate(t *testing.T, inputAssignments ...[]fr.Element) { assert.NoError(t, err, "proof rejected") } +func testSingleAddGate(t *testing.T, inputAssignments ...[]fr.Element) { + c := make(Circuit, 3) + c[2] = Wire{ + Gate: Gates["add"], + Inputs: []*Wire{&c[0], &c[1]}, + } + + assignment := WireAssignment{&c[0]: inputAssignments[0], &c[1]: inputAssignments[1]}.Complete(c) + + proof, err := Prove(c, assignment, fiatshamir.WithHash(test_vector_utils.NewMessageCounter(1, 1))) + assert.NoError(t, err) + + err = Verify(c, assignment, proof, fiatshamir.WithHash(test_vector_utils.NewMessageCounter(1, 1))) + assert.NoError(t, err, "proof rejected") + + err = Verify(c, assignment, proof, fiatshamir.WithHash(test_vector_utils.NewMessageCounter(0, 1))) + assert.NotNil(t, err, "bad proof accepted") +} + func testSingleMulGate(t *testing.T, inputAssignments ...[]fr.Element) { c := make(Circuit, 3) diff --git a/ecc/bls24-315/fr/gkr/gkr_test.go b/ecc/bls24-315/fr/gkr/gkr_test.go index 7fd419d28..fd14819be 100644 --- a/ecc/bls24-315/fr/gkr/gkr_test.go +++ b/ecc/bls24-315/fr/gkr/gkr_test.go @@ -45,6 +45,14 @@ func TestNoGate(t *testing.T) { testManyInstances(t, 1, testNoGate) } +func TestSingleAddGateTwoInstances(t *testing.T) { + testSingleAddGate(t, []fr.Element{four, three}, []fr.Element{two, three}) +} + +func TestSingleAddGate(t *testing.T) { + testManyInstances(t, 2, testSingleAddGate) +} + func TestSingleMulGateTwoInstances(t *testing.T) { testSingleMulGate(t, []fr.Element{four, three}, []fr.Element{two, three}) } @@ -203,6 +211,25 @@ func testNoGate(t *testing.T, inputAssignments ...[]fr.Element) { assert.NoError(t, err, "proof rejected") } +func testSingleAddGate(t *testing.T, inputAssignments ...[]fr.Element) { + c := make(Circuit, 3) + c[2] = Wire{ + Gate: Gates["add"], + Inputs: []*Wire{&c[0], &c[1]}, + } + + assignment := WireAssignment{&c[0]: inputAssignments[0], &c[1]: inputAssignments[1]}.Complete(c) + + proof, err := Prove(c, assignment, fiatshamir.WithHash(test_vector_utils.NewMessageCounter(1, 1))) + assert.NoError(t, err) + + err = Verify(c, assignment, proof, fiatshamir.WithHash(test_vector_utils.NewMessageCounter(1, 1))) + assert.NoError(t, err, "proof rejected") + + err = Verify(c, assignment, proof, fiatshamir.WithHash(test_vector_utils.NewMessageCounter(0, 1))) + assert.NotNil(t, err, "bad proof accepted") +} + func testSingleMulGate(t *testing.T, inputAssignments ...[]fr.Element) { c := make(Circuit, 3) diff --git a/ecc/bls24-317/fr/gkr/gkr_test.go b/ecc/bls24-317/fr/gkr/gkr_test.go index 35288f1d0..2d6134f87 100644 --- a/ecc/bls24-317/fr/gkr/gkr_test.go +++ b/ecc/bls24-317/fr/gkr/gkr_test.go @@ -45,6 +45,14 @@ func TestNoGate(t *testing.T) { testManyInstances(t, 1, testNoGate) } +func TestSingleAddGateTwoInstances(t *testing.T) { + testSingleAddGate(t, []fr.Element{four, three}, []fr.Element{two, three}) +} + +func TestSingleAddGate(t *testing.T) { + testManyInstances(t, 2, testSingleAddGate) +} + func TestSingleMulGateTwoInstances(t *testing.T) { testSingleMulGate(t, []fr.Element{four, three}, []fr.Element{two, three}) } @@ -203,6 +211,25 @@ func testNoGate(t *testing.T, inputAssignments ...[]fr.Element) { assert.NoError(t, err, "proof rejected") } +func testSingleAddGate(t *testing.T, inputAssignments ...[]fr.Element) { + c := make(Circuit, 3) + c[2] = Wire{ + Gate: Gates["add"], + Inputs: []*Wire{&c[0], &c[1]}, + } + + assignment := WireAssignment{&c[0]: inputAssignments[0], &c[1]: inputAssignments[1]}.Complete(c) + + proof, err := Prove(c, assignment, fiatshamir.WithHash(test_vector_utils.NewMessageCounter(1, 1))) + assert.NoError(t, err) + + err = Verify(c, assignment, proof, fiatshamir.WithHash(test_vector_utils.NewMessageCounter(1, 1))) + assert.NoError(t, err, "proof rejected") + + err = Verify(c, assignment, proof, fiatshamir.WithHash(test_vector_utils.NewMessageCounter(0, 1))) + assert.NotNil(t, err, "bad proof accepted") +} + func testSingleMulGate(t *testing.T, inputAssignments ...[]fr.Element) { c := make(Circuit, 3) diff --git a/ecc/bn254/fr/gkr/gkr_test.go b/ecc/bn254/fr/gkr/gkr_test.go index 429998b12..422ddb997 100644 --- a/ecc/bn254/fr/gkr/gkr_test.go +++ b/ecc/bn254/fr/gkr/gkr_test.go @@ -45,6 +45,14 @@ func TestNoGate(t *testing.T) { testManyInstances(t, 1, testNoGate) } +func TestSingleAddGateTwoInstances(t *testing.T) { + testSingleAddGate(t, []fr.Element{four, three}, []fr.Element{two, three}) +} + +func TestSingleAddGate(t *testing.T) { + testManyInstances(t, 2, testSingleAddGate) +} + func TestSingleMulGateTwoInstances(t *testing.T) { testSingleMulGate(t, []fr.Element{four, three}, []fr.Element{two, three}) } @@ -203,6 +211,25 @@ func testNoGate(t *testing.T, inputAssignments ...[]fr.Element) { assert.NoError(t, err, "proof rejected") } +func testSingleAddGate(t *testing.T, inputAssignments ...[]fr.Element) { + c := make(Circuit, 3) + c[2] = Wire{ + Gate: Gates["add"], + Inputs: []*Wire{&c[0], &c[1]}, + } + + assignment := WireAssignment{&c[0]: inputAssignments[0], &c[1]: inputAssignments[1]}.Complete(c) + + proof, err := Prove(c, assignment, fiatshamir.WithHash(test_vector_utils.NewMessageCounter(1, 1))) + assert.NoError(t, err) + + err = Verify(c, assignment, proof, fiatshamir.WithHash(test_vector_utils.NewMessageCounter(1, 1))) + assert.NoError(t, err, "proof rejected") + + err = Verify(c, assignment, proof, fiatshamir.WithHash(test_vector_utils.NewMessageCounter(0, 1))) + assert.NotNil(t, err, "bad proof accepted") +} + func testSingleMulGate(t *testing.T, inputAssignments ...[]fr.Element) { c := make(Circuit, 3) diff --git a/ecc/bw6-633/fr/gkr/gkr_test.go b/ecc/bw6-633/fr/gkr/gkr_test.go index 5a683825f..7b2abc9dc 100644 --- a/ecc/bw6-633/fr/gkr/gkr_test.go +++ b/ecc/bw6-633/fr/gkr/gkr_test.go @@ -45,6 +45,14 @@ func TestNoGate(t *testing.T) { testManyInstances(t, 1, testNoGate) } +func TestSingleAddGateTwoInstances(t *testing.T) { + testSingleAddGate(t, []fr.Element{four, three}, []fr.Element{two, three}) +} + +func TestSingleAddGate(t *testing.T) { + testManyInstances(t, 2, testSingleAddGate) +} + func TestSingleMulGateTwoInstances(t *testing.T) { testSingleMulGate(t, []fr.Element{four, three}, []fr.Element{two, three}) } @@ -203,6 +211,25 @@ func testNoGate(t *testing.T, inputAssignments ...[]fr.Element) { assert.NoError(t, err, "proof rejected") } +func testSingleAddGate(t *testing.T, inputAssignments ...[]fr.Element) { + c := make(Circuit, 3) + c[2] = Wire{ + Gate: Gates["add"], + Inputs: []*Wire{&c[0], &c[1]}, + } + + assignment := WireAssignment{&c[0]: inputAssignments[0], &c[1]: inputAssignments[1]}.Complete(c) + + proof, err := Prove(c, assignment, fiatshamir.WithHash(test_vector_utils.NewMessageCounter(1, 1))) + assert.NoError(t, err) + + err = Verify(c, assignment, proof, fiatshamir.WithHash(test_vector_utils.NewMessageCounter(1, 1))) + assert.NoError(t, err, "proof rejected") + + err = Verify(c, assignment, proof, fiatshamir.WithHash(test_vector_utils.NewMessageCounter(0, 1))) + assert.NotNil(t, err, "bad proof accepted") +} + func testSingleMulGate(t *testing.T, inputAssignments ...[]fr.Element) { c := make(Circuit, 3) diff --git a/ecc/bw6-756/fr/gkr/gkr_test.go b/ecc/bw6-756/fr/gkr/gkr_test.go index 2632d06f1..7f24681ed 100644 --- a/ecc/bw6-756/fr/gkr/gkr_test.go +++ b/ecc/bw6-756/fr/gkr/gkr_test.go @@ -45,6 +45,14 @@ func TestNoGate(t *testing.T) { testManyInstances(t, 1, testNoGate) } +func TestSingleAddGateTwoInstances(t *testing.T) { + testSingleAddGate(t, []fr.Element{four, three}, []fr.Element{two, three}) +} + +func TestSingleAddGate(t *testing.T) { + testManyInstances(t, 2, testSingleAddGate) +} + func TestSingleMulGateTwoInstances(t *testing.T) { testSingleMulGate(t, []fr.Element{four, three}, []fr.Element{two, three}) } @@ -203,6 +211,25 @@ func testNoGate(t *testing.T, inputAssignments ...[]fr.Element) { assert.NoError(t, err, "proof rejected") } +func testSingleAddGate(t *testing.T, inputAssignments ...[]fr.Element) { + c := make(Circuit, 3) + c[2] = Wire{ + Gate: Gates["add"], + Inputs: []*Wire{&c[0], &c[1]}, + } + + assignment := WireAssignment{&c[0]: inputAssignments[0], &c[1]: inputAssignments[1]}.Complete(c) + + proof, err := Prove(c, assignment, fiatshamir.WithHash(test_vector_utils.NewMessageCounter(1, 1))) + assert.NoError(t, err) + + err = Verify(c, assignment, proof, fiatshamir.WithHash(test_vector_utils.NewMessageCounter(1, 1))) + assert.NoError(t, err, "proof rejected") + + err = Verify(c, assignment, proof, fiatshamir.WithHash(test_vector_utils.NewMessageCounter(0, 1))) + assert.NotNil(t, err, "bad proof accepted") +} + func testSingleMulGate(t *testing.T, inputAssignments ...[]fr.Element) { c := make(Circuit, 3) diff --git a/ecc/bw6-761/fr/gkr/gkr_test.go b/ecc/bw6-761/fr/gkr/gkr_test.go index 4120b3aff..976fb661d 100644 --- a/ecc/bw6-761/fr/gkr/gkr_test.go +++ b/ecc/bw6-761/fr/gkr/gkr_test.go @@ -45,6 +45,14 @@ func TestNoGate(t *testing.T) { testManyInstances(t, 1, testNoGate) } +func TestSingleAddGateTwoInstances(t *testing.T) { + testSingleAddGate(t, []fr.Element{four, three}, []fr.Element{two, three}) +} + +func TestSingleAddGate(t *testing.T) { + testManyInstances(t, 2, testSingleAddGate) +} + func TestSingleMulGateTwoInstances(t *testing.T) { testSingleMulGate(t, []fr.Element{four, three}, []fr.Element{two, three}) } @@ -203,6 +211,25 @@ func testNoGate(t *testing.T, inputAssignments ...[]fr.Element) { assert.NoError(t, err, "proof rejected") } +func testSingleAddGate(t *testing.T, inputAssignments ...[]fr.Element) { + c := make(Circuit, 3) + c[2] = Wire{ + Gate: Gates["add"], + Inputs: []*Wire{&c[0], &c[1]}, + } + + assignment := WireAssignment{&c[0]: inputAssignments[0], &c[1]: inputAssignments[1]}.Complete(c) + + proof, err := Prove(c, assignment, fiatshamir.WithHash(test_vector_utils.NewMessageCounter(1, 1))) + assert.NoError(t, err) + + err = Verify(c, assignment, proof, fiatshamir.WithHash(test_vector_utils.NewMessageCounter(1, 1))) + assert.NoError(t, err, "proof rejected") + + err = Verify(c, assignment, proof, fiatshamir.WithHash(test_vector_utils.NewMessageCounter(0, 1))) + assert.NotNil(t, err, "bad proof accepted") +} + func testSingleMulGate(t *testing.T, inputAssignments ...[]fr.Element) { c := make(Circuit, 3) diff --git a/internal/generator/gkr/template/gkr.test.go.tmpl b/internal/generator/gkr/template/gkr.test.go.tmpl index 525abd6c9..b553c2366 100644 --- a/internal/generator/gkr/template/gkr.test.go.tmpl +++ b/internal/generator/gkr/template/gkr.test.go.tmpl @@ -31,6 +31,14 @@ func TestNoGate(t *testing.T) { testManyInstances(t, 1, testNoGate) } +func TestSingleAddGateTwoInstances(t *testing.T) { + testSingleAddGate(t, []{{.ElementType}}{four, three}, []{{.ElementType}}{two, three}) +} + +func TestSingleAddGate(t *testing.T) { + testManyInstances(t, 2, testSingleAddGate) +} + func TestSingleMulGateTwoInstances(t *testing.T) { testSingleMulGate(t, []{{.ElementType}}{four, three}, []{{.ElementType}}{two, three}) } @@ -193,6 +201,25 @@ func testNoGate(t *testing.T, inputAssignments ...[]{{.ElementType}}) { assert.NoError(t, err, "proof rejected") } +func testSingleAddGate(t *testing.T, inputAssignments ...[]{{.ElementType}}) { + c := make(Circuit, 3) + c[2] = Wire{ + Gate: Gates["add"], + Inputs: []*Wire{&c[0], &c[1]}, + } + + assignment := WireAssignment{&c[0]: inputAssignments[0], &c[1]: inputAssignments[1]}.Complete(c) + + proof, err := Prove(c, assignment, fiatshamir.WithHash(test_vector_utils.NewMessageCounter(1, 1))) + assert.NoError(t,err) + + err = Verify(c, assignment, proof, fiatshamir.WithHash(test_vector_utils.NewMessageCounter(1, 1))) + assert.NoError(t, err, "proof rejected") + + err = Verify(c, assignment, proof, fiatshamir.WithHash(test_vector_utils.NewMessageCounter(0, 1))) + assert.NotNil(t, err, "bad proof accepted") +} + func testSingleMulGate(t *testing.T, inputAssignments ...[]{{.ElementType}}) { c := make(Circuit, 3) From a43222ad0e24a96c78b99a801d2c1e6d2dd7cb69 Mon Sep 17 00:00:00 2001 From: BeratOz01 Date: Mon, 4 Mar 2024 18:39:51 +0300 Subject: [PATCH 3/4] feat: refactor add gate evaluate --- ecc/bls12-377/fr/gkr/gkr.go | 2 -- ecc/bls12-378/fr/gkr/gkr.go | 2 -- ecc/bls12-381/fr/gkr/gkr.go | 2 -- ecc/bls24-315/fr/gkr/gkr.go | 2 -- ecc/bls24-317/fr/gkr/gkr.go | 2 -- ecc/bn254/fr/gkr/gkr.go | 2 -- ecc/bw6-633/fr/gkr/gkr.go | 2 -- ecc/bw6-756/fr/gkr/gkr.go | 2 -- ecc/bw6-761/fr/gkr/gkr.go | 2 -- internal/generator/gkr/template/gkr.go.tmpl | 2 -- internal/generator/test_vector_utils/small_rational/gkr/gkr.go | 2 -- 11 files changed, 22 deletions(-) diff --git a/ecc/bls12-377/fr/gkr/gkr.go b/ecc/bls12-377/fr/gkr/gkr.go index ff5f60f1d..d3e8f4173 100644 --- a/ecc/bls12-377/fr/gkr/gkr.go +++ b/ecc/bls12-377/fr/gkr/gkr.go @@ -884,8 +884,6 @@ func (g AddGate) Evaluate(x ...fr.Element) (res fr.Element) { // set zero case 1: res.Set(&x[0]) - case 2: - res.Add(&x[0], &x[1]) default: res.Add(&x[0], &x[1]) for i := 2; i < len(x); i++ { diff --git a/ecc/bls12-378/fr/gkr/gkr.go b/ecc/bls12-378/fr/gkr/gkr.go index 5241117e6..77b0602d7 100644 --- a/ecc/bls12-378/fr/gkr/gkr.go +++ b/ecc/bls12-378/fr/gkr/gkr.go @@ -884,8 +884,6 @@ func (g AddGate) Evaluate(x ...fr.Element) (res fr.Element) { // set zero case 1: res.Set(&x[0]) - case 2: - res.Add(&x[0], &x[1]) default: res.Add(&x[0], &x[1]) for i := 2; i < len(x); i++ { diff --git a/ecc/bls12-381/fr/gkr/gkr.go b/ecc/bls12-381/fr/gkr/gkr.go index eda514b61..fb9080f1a 100644 --- a/ecc/bls12-381/fr/gkr/gkr.go +++ b/ecc/bls12-381/fr/gkr/gkr.go @@ -884,8 +884,6 @@ func (g AddGate) Evaluate(x ...fr.Element) (res fr.Element) { // set zero case 1: res.Set(&x[0]) - case 2: - res.Add(&x[0], &x[1]) default: res.Add(&x[0], &x[1]) for i := 2; i < len(x); i++ { diff --git a/ecc/bls24-315/fr/gkr/gkr.go b/ecc/bls24-315/fr/gkr/gkr.go index 881134cc7..43a557077 100644 --- a/ecc/bls24-315/fr/gkr/gkr.go +++ b/ecc/bls24-315/fr/gkr/gkr.go @@ -884,8 +884,6 @@ func (g AddGate) Evaluate(x ...fr.Element) (res fr.Element) { // set zero case 1: res.Set(&x[0]) - case 2: - res.Add(&x[0], &x[1]) default: res.Add(&x[0], &x[1]) for i := 2; i < len(x); i++ { diff --git a/ecc/bls24-317/fr/gkr/gkr.go b/ecc/bls24-317/fr/gkr/gkr.go index aab3a0226..ba20ca746 100644 --- a/ecc/bls24-317/fr/gkr/gkr.go +++ b/ecc/bls24-317/fr/gkr/gkr.go @@ -884,8 +884,6 @@ func (g AddGate) Evaluate(x ...fr.Element) (res fr.Element) { // set zero case 1: res.Set(&x[0]) - case 2: - res.Add(&x[0], &x[1]) default: res.Add(&x[0], &x[1]) for i := 2; i < len(x); i++ { diff --git a/ecc/bn254/fr/gkr/gkr.go b/ecc/bn254/fr/gkr/gkr.go index ef83b72ab..5470fe33a 100644 --- a/ecc/bn254/fr/gkr/gkr.go +++ b/ecc/bn254/fr/gkr/gkr.go @@ -884,8 +884,6 @@ func (g AddGate) Evaluate(x ...fr.Element) (res fr.Element) { // set zero case 1: res.Set(&x[0]) - case 2: - res.Add(&x[0], &x[1]) default: res.Add(&x[0], &x[1]) for i := 2; i < len(x); i++ { diff --git a/ecc/bw6-633/fr/gkr/gkr.go b/ecc/bw6-633/fr/gkr/gkr.go index 71106ecc3..1e73e7c6a 100644 --- a/ecc/bw6-633/fr/gkr/gkr.go +++ b/ecc/bw6-633/fr/gkr/gkr.go @@ -884,8 +884,6 @@ func (g AddGate) Evaluate(x ...fr.Element) (res fr.Element) { // set zero case 1: res.Set(&x[0]) - case 2: - res.Add(&x[0], &x[1]) default: res.Add(&x[0], &x[1]) for i := 2; i < len(x); i++ { diff --git a/ecc/bw6-756/fr/gkr/gkr.go b/ecc/bw6-756/fr/gkr/gkr.go index 8ea9c04a2..060e10e6c 100644 --- a/ecc/bw6-756/fr/gkr/gkr.go +++ b/ecc/bw6-756/fr/gkr/gkr.go @@ -884,8 +884,6 @@ func (g AddGate) Evaluate(x ...fr.Element) (res fr.Element) { // set zero case 1: res.Set(&x[0]) - case 2: - res.Add(&x[0], &x[1]) default: res.Add(&x[0], &x[1]) for i := 2; i < len(x); i++ { diff --git a/ecc/bw6-761/fr/gkr/gkr.go b/ecc/bw6-761/fr/gkr/gkr.go index d0247f47e..05b06c041 100644 --- a/ecc/bw6-761/fr/gkr/gkr.go +++ b/ecc/bw6-761/fr/gkr/gkr.go @@ -884,8 +884,6 @@ func (g AddGate) Evaluate(x ...fr.Element) (res fr.Element) { // set zero case 1: res.Set(&x[0]) - case 2: - res.Add(&x[0], &x[1]) default: res.Add(&x[0], &x[1]) for i := 2; i < len(x); i++ { diff --git a/internal/generator/gkr/template/gkr.go.tmpl b/internal/generator/gkr/template/gkr.go.tmpl index d0b978072..20f6c968a 100644 --- a/internal/generator/gkr/template/gkr.go.tmpl +++ b/internal/generator/gkr/template/gkr.go.tmpl @@ -869,8 +869,6 @@ func (g AddGate) Evaluate(x ...{{.ElementType}}) (res {{.ElementType}}) { // set zero case 1: res.Set(&x[0]) - case 2: - res.Add(&x[0], &x[1]) default: res.Add(&x[0], &x[1]) for i := 2; i < len(x); i++ { diff --git a/internal/generator/test_vector_utils/small_rational/gkr/gkr.go b/internal/generator/test_vector_utils/small_rational/gkr/gkr.go index 0b04a0b46..e1007f824 100644 --- a/internal/generator/test_vector_utils/small_rational/gkr/gkr.go +++ b/internal/generator/test_vector_utils/small_rational/gkr/gkr.go @@ -884,8 +884,6 @@ func (g AddGate) Evaluate(x ...small_rational.SmallRational) (res small_rational // set zero case 1: res.Set(&x[0]) - case 2: - res.Add(&x[0], &x[1]) default: res.Add(&x[0], &x[1]) for i := 2; i < len(x); i++ { From 1e21286765539c7bee069a3d18414fe69e526da7 Mon Sep 17 00:00:00 2001 From: Ivo Kubjas Date: Tue, 5 Mar 2024 09:55:26 +0000 Subject: [PATCH 4/4] fix: mul gate input --- ecc/bls12-377/fr/gkr/gkr.go | 2 +- ecc/bls12-378/fr/gkr/gkr.go | 2 +- ecc/bls12-381/fr/gkr/gkr.go | 2 +- ecc/bls24-315/fr/gkr/gkr.go | 2 +- ecc/bls24-317/fr/gkr/gkr.go | 2 +- ecc/bn254/fr/gkr/gkr.go | 2 +- ecc/bw6-633/fr/gkr/gkr.go | 2 +- ecc/bw6-756/fr/gkr/gkr.go | 2 +- ecc/bw6-761/fr/gkr/gkr.go | 2 +- internal/generator/gkr/template/gkr.go.tmpl | 2 +- internal/generator/test_vector_utils/small_rational/gkr/gkr.go | 2 +- 11 files changed, 11 insertions(+), 11 deletions(-) diff --git a/ecc/bls12-377/fr/gkr/gkr.go b/ecc/bls12-377/fr/gkr/gkr.go index d3e8f4173..5eb0e2c61 100644 --- a/ecc/bls12-377/fr/gkr/gkr.go +++ b/ecc/bls12-377/fr/gkr/gkr.go @@ -909,7 +909,7 @@ func (g MulGate) Evaluate(x ...fr.Element) (res fr.Element) { default: res.Mul(&x[0], &x[1]) for i := 2; i < len(x); i++ { - res.Mul(&res, &x[2]) + res.Mul(&res, &x[i]) } } return diff --git a/ecc/bls12-378/fr/gkr/gkr.go b/ecc/bls12-378/fr/gkr/gkr.go index 77b0602d7..ceada9579 100644 --- a/ecc/bls12-378/fr/gkr/gkr.go +++ b/ecc/bls12-378/fr/gkr/gkr.go @@ -909,7 +909,7 @@ func (g MulGate) Evaluate(x ...fr.Element) (res fr.Element) { default: res.Mul(&x[0], &x[1]) for i := 2; i < len(x); i++ { - res.Mul(&res, &x[2]) + res.Mul(&res, &x[i]) } } return diff --git a/ecc/bls12-381/fr/gkr/gkr.go b/ecc/bls12-381/fr/gkr/gkr.go index fb9080f1a..b4d322ad1 100644 --- a/ecc/bls12-381/fr/gkr/gkr.go +++ b/ecc/bls12-381/fr/gkr/gkr.go @@ -909,7 +909,7 @@ func (g MulGate) Evaluate(x ...fr.Element) (res fr.Element) { default: res.Mul(&x[0], &x[1]) for i := 2; i < len(x); i++ { - res.Mul(&res, &x[2]) + res.Mul(&res, &x[i]) } } return diff --git a/ecc/bls24-315/fr/gkr/gkr.go b/ecc/bls24-315/fr/gkr/gkr.go index 43a557077..49d6a0554 100644 --- a/ecc/bls24-315/fr/gkr/gkr.go +++ b/ecc/bls24-315/fr/gkr/gkr.go @@ -909,7 +909,7 @@ func (g MulGate) Evaluate(x ...fr.Element) (res fr.Element) { default: res.Mul(&x[0], &x[1]) for i := 2; i < len(x); i++ { - res.Mul(&res, &x[2]) + res.Mul(&res, &x[i]) } } return diff --git a/ecc/bls24-317/fr/gkr/gkr.go b/ecc/bls24-317/fr/gkr/gkr.go index ba20ca746..fde5babfd 100644 --- a/ecc/bls24-317/fr/gkr/gkr.go +++ b/ecc/bls24-317/fr/gkr/gkr.go @@ -909,7 +909,7 @@ func (g MulGate) Evaluate(x ...fr.Element) (res fr.Element) { default: res.Mul(&x[0], &x[1]) for i := 2; i < len(x); i++ { - res.Mul(&res, &x[2]) + res.Mul(&res, &x[i]) } } return diff --git a/ecc/bn254/fr/gkr/gkr.go b/ecc/bn254/fr/gkr/gkr.go index 5470fe33a..94a2735b9 100644 --- a/ecc/bn254/fr/gkr/gkr.go +++ b/ecc/bn254/fr/gkr/gkr.go @@ -909,7 +909,7 @@ func (g MulGate) Evaluate(x ...fr.Element) (res fr.Element) { default: res.Mul(&x[0], &x[1]) for i := 2; i < len(x); i++ { - res.Mul(&res, &x[2]) + res.Mul(&res, &x[i]) } } return diff --git a/ecc/bw6-633/fr/gkr/gkr.go b/ecc/bw6-633/fr/gkr/gkr.go index 1e73e7c6a..899729723 100644 --- a/ecc/bw6-633/fr/gkr/gkr.go +++ b/ecc/bw6-633/fr/gkr/gkr.go @@ -909,7 +909,7 @@ func (g MulGate) Evaluate(x ...fr.Element) (res fr.Element) { default: res.Mul(&x[0], &x[1]) for i := 2; i < len(x); i++ { - res.Mul(&res, &x[2]) + res.Mul(&res, &x[i]) } } return diff --git a/ecc/bw6-756/fr/gkr/gkr.go b/ecc/bw6-756/fr/gkr/gkr.go index 060e10e6c..ae9a3ecfa 100644 --- a/ecc/bw6-756/fr/gkr/gkr.go +++ b/ecc/bw6-756/fr/gkr/gkr.go @@ -909,7 +909,7 @@ func (g MulGate) Evaluate(x ...fr.Element) (res fr.Element) { default: res.Mul(&x[0], &x[1]) for i := 2; i < len(x); i++ { - res.Mul(&res, &x[2]) + res.Mul(&res, &x[i]) } } return diff --git a/ecc/bw6-761/fr/gkr/gkr.go b/ecc/bw6-761/fr/gkr/gkr.go index 05b06c041..fda102869 100644 --- a/ecc/bw6-761/fr/gkr/gkr.go +++ b/ecc/bw6-761/fr/gkr/gkr.go @@ -909,7 +909,7 @@ func (g MulGate) Evaluate(x ...fr.Element) (res fr.Element) { default: res.Mul(&x[0], &x[1]) for i := 2; i < len(x); i++ { - res.Mul(&res, &x[2]) + res.Mul(&res, &x[i]) } } return diff --git a/internal/generator/gkr/template/gkr.go.tmpl b/internal/generator/gkr/template/gkr.go.tmpl index 20f6c968a..e346ae932 100644 --- a/internal/generator/gkr/template/gkr.go.tmpl +++ b/internal/generator/gkr/template/gkr.go.tmpl @@ -894,7 +894,7 @@ func (g MulGate) Evaluate(x ...{{.ElementType}}) (res {{.ElementType}}) { default: res.Mul(&x[0], &x[1]) for i := 2; i < len(x); i++ { - res.Mul(&res, &x[2]) + res.Mul(&res, &x[i]) } } return diff --git a/internal/generator/test_vector_utils/small_rational/gkr/gkr.go b/internal/generator/test_vector_utils/small_rational/gkr/gkr.go index e1007f824..d0dae64a6 100644 --- a/internal/generator/test_vector_utils/small_rational/gkr/gkr.go +++ b/internal/generator/test_vector_utils/small_rational/gkr/gkr.go @@ -909,7 +909,7 @@ func (g MulGate) Evaluate(x ...small_rational.SmallRational) (res small_rational default: res.Mul(&x[0], &x[1]) for i := 2; i < len(x); i++ { - res.Mul(&res, &x[2]) + res.Mul(&res, &x[i]) } } return