diff --git a/ecc/bls12-377/fr/gkr/gkr.go b/ecc/bls12-377/fr/gkr/gkr.go index 19f4e9bb2..5eb0e2c61 100644 --- a/ecc/bls12-377/fr/gkr/gkr.go +++ b/ecc/bls12-377/fr/gkr/gkr.go @@ -884,10 +884,10 @@ func (g AddGate) Evaluate(x ...fr.Element) (res fr.Element) { // set zero case 1: res.Set(&x[0]) - case 2: + 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 @@ -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-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.go b/ecc/bls12-378/fr/gkr/gkr.go index c576d7aca..ceada9579 100644 --- a/ecc/bls12-378/fr/gkr/gkr.go +++ b/ecc/bls12-378/fr/gkr/gkr.go @@ -884,10 +884,10 @@ func (g AddGate) Evaluate(x ...fr.Element) (res fr.Element) { // set zero case 1: res.Set(&x[0]) - case 2: + 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 @@ -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_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.go b/ecc/bls12-381/fr/gkr/gkr.go index 64b48da06..b4d322ad1 100644 --- a/ecc/bls12-381/fr/gkr/gkr.go +++ b/ecc/bls12-381/fr/gkr/gkr.go @@ -884,10 +884,10 @@ func (g AddGate) Evaluate(x ...fr.Element) (res fr.Element) { // set zero case 1: res.Set(&x[0]) - case 2: + 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 @@ -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_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.go b/ecc/bls24-315/fr/gkr/gkr.go index ca1f4c833..49d6a0554 100644 --- a/ecc/bls24-315/fr/gkr/gkr.go +++ b/ecc/bls24-315/fr/gkr/gkr.go @@ -884,10 +884,10 @@ func (g AddGate) Evaluate(x ...fr.Element) (res fr.Element) { // set zero case 1: res.Set(&x[0]) - case 2: + 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 @@ -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_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.go b/ecc/bls24-317/fr/gkr/gkr.go index 0dc5b4883..fde5babfd 100644 --- a/ecc/bls24-317/fr/gkr/gkr.go +++ b/ecc/bls24-317/fr/gkr/gkr.go @@ -884,10 +884,10 @@ func (g AddGate) Evaluate(x ...fr.Element) (res fr.Element) { // set zero case 1: res.Set(&x[0]) - case 2: + 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 @@ -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_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.go b/ecc/bn254/fr/gkr/gkr.go index df189b19d..94a2735b9 100644 --- a/ecc/bn254/fr/gkr/gkr.go +++ b/ecc/bn254/fr/gkr/gkr.go @@ -884,10 +884,10 @@ func (g AddGate) Evaluate(x ...fr.Element) (res fr.Element) { // set zero case 1: res.Set(&x[0]) - case 2: + 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 @@ -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_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.go b/ecc/bw6-633/fr/gkr/gkr.go index 888c2c3c5..899729723 100644 --- a/ecc/bw6-633/fr/gkr/gkr.go +++ b/ecc/bw6-633/fr/gkr/gkr.go @@ -884,10 +884,10 @@ func (g AddGate) Evaluate(x ...fr.Element) (res fr.Element) { // set zero case 1: res.Set(&x[0]) - case 2: + 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 @@ -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_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.go b/ecc/bw6-756/fr/gkr/gkr.go index 5fe30d6d2..ae9a3ecfa 100644 --- a/ecc/bw6-756/fr/gkr/gkr.go +++ b/ecc/bw6-756/fr/gkr/gkr.go @@ -884,10 +884,10 @@ func (g AddGate) Evaluate(x ...fr.Element) (res fr.Element) { // set zero case 1: res.Set(&x[0]) - case 2: + 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 @@ -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_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.go b/ecc/bw6-761/fr/gkr/gkr.go index c98a8ab05..fda102869 100644 --- a/ecc/bw6-761/fr/gkr/gkr.go +++ b/ecc/bw6-761/fr/gkr/gkr.go @@ -884,10 +884,10 @@ func (g AddGate) Evaluate(x ...fr.Element) (res fr.Element) { // set zero case 1: res.Set(&x[0]) - case 2: + 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 @@ -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_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.go.tmpl b/internal/generator/gkr/template/gkr.go.tmpl index 049702508..e346ae932 100644 --- a/internal/generator/gkr/template/gkr.go.tmpl +++ b/internal/generator/gkr/template/gkr.go.tmpl @@ -869,10 +869,10 @@ func (g AddGate) Evaluate(x ...{{.ElementType}}) (res {{.ElementType}}) { // set zero case 1: res.Set(&x[0]) - case 2: + 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 @@ -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/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) 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..d0dae64a6 100644 --- a/internal/generator/test_vector_utils/small_rational/gkr/gkr.go +++ b/internal/generator/test_vector_utils/small_rational/gkr/gkr.go @@ -884,10 +884,10 @@ func (g AddGate) Evaluate(x ...small_rational.SmallRational) (res small_rational // set zero case 1: res.Set(&x[0]) - case 2: + 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 @@ -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