Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions circuits/array.circom
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/
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;
}
49 changes: 49 additions & 0 deletions test/array.js
Original file line number Diff line number Diff line change
@@ -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)));
});
});
5 changes: 5 additions & 0 deletions test/circuits/array_contains_test.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pragma circom 2.0.0;

include "../../circuits/array.circom";

component main = Contains(4);
5 changes: 5 additions & 0 deletions test/circuits/array_sum_test.circom
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pragma circom 2.0.0;

include "../../circuits/array.circom";

component main = Sum(4);