diff --git a/circuits/array.circom b/circuits/array.circom new file mode 100644 index 00000000..7897afbc --- /dev/null +++ b/circuits/array.circom @@ -0,0 +1,53 @@ +/* + Copyright 2018 0KIMS association. + + This file is part of circom (Zero Knowledge Circuit Compiler). + + circom is a free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + circom is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public + License for more details. + + You should have received a copy of the GNU General Public License + along with circom. If not, see . +*/ +pragma circom 2.0.0; + +include "comparators.circom"; + +template Sum(n) { + signal input in[n]; + signal output out; + + signal cumulative[n]; + cumulative[0] <== in[0]; + + for (var i = 1; i < n; i++) { + cumulative[i] <== cumulative[i - 1] + in[i]; + } + + out <== cumulative[n - 1]; +} + +template Contains(n) { + signal input in[n]; + signal input value; + signal cumulative[n]; + signal output out; + + cumulative[0] <== in[0] - value; + + for (var i = 1; i < n; i++) { + cumulative[i] <== cumulative[i - 1] * (in[i] - value); + } + + component isZero = IsZero(); + isZero.in <== cumulative[n - 1]; + + out <== isZero.out; +} \ No newline at end of file diff --git a/test/array.js b/test/array.js new file mode 100644 index 00000000..e341a5b3 --- /dev/null +++ b/test/array.js @@ -0,0 +1,49 @@ +const chai = require("chai"); +const path = require("path"); +const F1Field = require("ffjavascript").F1Field; +const Scalar = require("ffjavascript").Scalar; +exports.p = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617"); +const Fr = new F1Field(exports.p); + +const wasm_tester = require("circom_tester").wasm; + +const assert = chai.assert; + +describe.only("Array test", function () { + + this.timeout(100000); + + it("Should create a sum circuit", async() => { + const circuit = await wasm_tester(path.join(__dirname, "circuits", "array_sum_test.circom")); + + let witness; + witness = await circuit.calculateWitness({ "in": [7, 3, 6, 4]}, true); + assert(Fr.eq(Fr.e(witness[0]), Fr.e(1))); + assert(Fr.eq(Fr.e(witness[1]), Fr.e(20))); + + witness = await circuit.calculateWitness({ "in": [2, 1, 5, 4]}, true); + assert(Fr.eq(Fr.e(witness[0]), Fr.e(1))); + assert(Fr.eq(Fr.e(witness[1]), Fr.e(12))); + }); + + it("Should create a contains circuit", async() => { + const circuit = await wasm_tester(path.join(__dirname, "circuits", "array_contains_test.circom")); + + let witness; + witness = await circuit.calculateWitness({ "in": [7, 3, 6, 4], "value": 3}, true); + assert(Fr.eq(Fr.e(witness[0]), Fr.e(1))); + assert(Fr.eq(Fr.e(witness[1]), Fr.e(1))); + + witness = await circuit.calculateWitness({ "in": [7, 3, 7, 4], "value": 7}, true); + assert(Fr.eq(Fr.e(witness[0]), Fr.e(1))); + assert(Fr.eq(Fr.e(witness[1]), Fr.e(1))); + + witness = await circuit.calculateWitness({ "in": [7, 3, 6, 4], "value": 5}, true); + assert(Fr.eq(Fr.e(witness[0]), Fr.e(1))); + assert(Fr.eq(Fr.e(witness[1]), Fr.e(0))); + + witness = await circuit.calculateWitness({ "in": [7, 5, 6, 4], "value": 3}, true); + assert(Fr.eq(Fr.e(witness[0]), Fr.e(1))); + assert(Fr.eq(Fr.e(witness[1]), Fr.e(0))); + }); +}); diff --git a/test/circuits/array_contains_test.circom b/test/circuits/array_contains_test.circom new file mode 100644 index 00000000..362e1960 --- /dev/null +++ b/test/circuits/array_contains_test.circom @@ -0,0 +1,5 @@ +pragma circom 2.0.0; + +include "../../circuits/array.circom"; + +component main = Contains(4); diff --git a/test/circuits/array_sum_test.circom b/test/circuits/array_sum_test.circom new file mode 100644 index 00000000..780be8b6 --- /dev/null +++ b/test/circuits/array_sum_test.circom @@ -0,0 +1,5 @@ +pragma circom 2.0.0; + +include "../../circuits/array.circom"; + +component main = Sum(4);