circomlib/test/comparators.js

186 lines
7.1 KiB
JavaScript
Raw Normal View History

2018-12-06 19:32:49 +03:00
const chai = require("chai");
const path = require("path");
2019-12-13 20:35:29 +03:00
const tester = require("circom").tester;
const Fr = require("ffjavascript").bn128.Fr;
2018-12-06 19:32:49 +03:00
const assert = chai.assert;
2020-02-04 21:19:11 +03:00
describe("Comparators test", function () {
2018-12-06 19:32:49 +03:00
2019-12-13 20:35:29 +03:00
this.timeout(100000);
it("Should create a iszero circuit", async() => {
const circuit = await tester(path.join(__dirname, "circuits", "iszero.circom"));
2018-12-06 19:32:49 +03:00
let witness;
2020-03-26 21:24:20 +03:00
witness = await circuit.calculateWitness({ "in": 111}, true);
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(0)));
2018-12-06 19:32:49 +03:00
2020-03-26 21:24:20 +03:00
witness = await circuit.calculateWitness({ "in": 0 }, true);
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(1)));
2018-12-06 19:32:49 +03:00
});
it("Should create a isequal circuit", async() => {
2019-12-13 20:35:29 +03:00
const circuit = await tester(path.join(__dirname, "circuits", "isequal.circom"));
2018-12-06 19:32:49 +03:00
let witness;
2020-03-26 21:24:20 +03:00
witness = await circuit.calculateWitness({ "in": [111,222] }, true);
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(0)));
2018-12-06 19:32:49 +03:00
2020-03-26 21:24:20 +03:00
witness = await circuit.calculateWitness({ "in": [444,444] }, true);
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(1)));
2018-12-06 19:32:49 +03:00
});
2019-07-08 15:08:04 +03:00
it("Should create a comparison lessthan", async() => {
2019-12-13 20:35:29 +03:00
const circuit = await tester(path.join(__dirname, "circuits", "lessthan.circom"));
2018-12-06 19:32:49 +03:00
let witness;
2020-03-26 21:24:20 +03:00
witness = await circuit.calculateWitness({ "in": [333,444] }), true;
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(1)));
2018-12-06 19:32:49 +03:00
2020-03-26 21:24:20 +03:00
witness = await circuit.calculateWitness({ "in":[1,1] }, true);
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(0)));
2018-12-06 19:32:49 +03:00
2020-03-26 21:24:20 +03:00
witness = await circuit.calculateWitness({ "in": [661, 660] }, true);
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(0)));
2018-12-06 19:32:49 +03:00
2020-03-26 21:24:20 +03:00
witness = await circuit.calculateWitness({ "in": [0, 1] }, true);
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(1)));
2018-12-06 19:32:49 +03:00
2020-03-26 21:24:20 +03:00
witness = await circuit.calculateWitness({ "in": [0, 444] }, true);
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(1)));
2018-12-06 19:32:49 +03:00
2020-03-26 21:24:20 +03:00
witness = await circuit.calculateWitness({ "in": [1, 0] }, true);
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(0)));
2018-12-06 19:32:49 +03:00
2020-03-26 21:24:20 +03:00
witness = await circuit.calculateWitness({ "in": [555, 0] }, true);
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(0)));
2018-12-06 19:32:49 +03:00
2020-03-26 21:24:20 +03:00
witness = await circuit.calculateWitness({ "in": [0, 0] }, true);
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(0)));
2018-12-06 19:32:49 +03:00
});
2019-07-08 15:08:04 +03:00
it("Should create a comparison lesseqthan", async() => {
2019-12-13 20:35:29 +03:00
const circuit = await tester(path.join(__dirname, "circuits", "lesseqthan.circom"));
2019-07-08 15:08:04 +03:00
let witness;
2020-03-26 21:24:20 +03:00
witness = await circuit.calculateWitness({ "in": [333,444] }, true);
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(1)));
2019-07-08 15:08:04 +03:00
2020-03-26 21:24:20 +03:00
witness = await circuit.calculateWitness({ "in":[1,1] }, true);
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(1)));
2019-07-08 15:08:04 +03:00
2020-03-26 21:24:20 +03:00
witness = await circuit.calculateWitness({ "in": [661, 660] }, true);
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(0)));
2019-07-08 15:08:04 +03:00
2020-03-26 21:24:20 +03:00
witness = await circuit.calculateWitness({ "in": [0, 1] }, true);
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(1)));
2019-07-08 15:08:04 +03:00
2020-03-26 21:24:20 +03:00
witness = await circuit.calculateWitness({ "in": [0, 444] }, true);
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(1)));
2019-07-08 15:08:04 +03:00
2020-03-26 21:24:20 +03:00
witness = await circuit.calculateWitness({ "in": [1, 0] }, true);
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(0)));
2019-07-08 15:08:04 +03:00
2020-03-26 21:24:20 +03:00
witness = await circuit.calculateWitness({ "in": [555, 0] }, true);
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(0)));
2019-07-08 15:08:04 +03:00
2020-03-26 21:24:20 +03:00
witness = await circuit.calculateWitness({ "in": [0, 0] }, true);
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(1)));
2019-07-08 15:08:04 +03:00
});
it("Should create a comparison greaterthan", async() => {
2019-12-13 20:35:29 +03:00
const circuit = await tester(path.join(__dirname, "circuits", "greaterthan.circom"));
2019-07-08 15:08:04 +03:00
let witness;
2020-03-26 21:24:20 +03:00
witness = await circuit.calculateWitness({ "in": [333,444] }, true);
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(0)));
2019-07-08 15:08:04 +03:00
2020-03-26 21:24:20 +03:00
witness = await circuit.calculateWitness({ "in":[1,1] }, true);
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(0)));
2019-07-08 15:08:04 +03:00
2020-03-26 21:24:20 +03:00
witness = await circuit.calculateWitness({ "in": [661, 660] }, true);
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(1)));
2019-07-08 15:08:04 +03:00
2020-03-26 21:24:20 +03:00
witness = await circuit.calculateWitness({ "in": [0, 1] }, true);
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(0)));
2019-07-08 15:08:04 +03:00
2020-03-26 21:24:20 +03:00
witness = await circuit.calculateWitness({ "in": [0, 444] }, true);
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(0)));
2019-07-08 15:08:04 +03:00
2020-03-26 21:24:20 +03:00
witness = await circuit.calculateWitness({ "in": [1, 0] }, true);
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(1)));
2019-07-08 15:08:04 +03:00
2020-03-26 21:24:20 +03:00
witness = await circuit.calculateWitness({ "in": [555, 0] }, true);
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(1)));
2019-07-08 15:08:04 +03:00
2020-03-26 21:24:20 +03:00
witness = await circuit.calculateWitness({ "in": [0, 0] }, true);
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(0)));
2019-07-08 15:08:04 +03:00
});
it("Should create a comparison greatereqthan", async() => {
2019-12-13 20:35:29 +03:00
const circuit = await tester(path.join(__dirname, "circuits", "greatereqthan.circom"));
2019-07-08 15:08:04 +03:00
let witness;
2020-03-26 21:24:20 +03:00
witness = await circuit.calculateWitness({ "in": [333,444] }, true);
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(0)));
2019-07-08 15:08:04 +03:00
2020-03-26 21:24:20 +03:00
witness = await circuit.calculateWitness({ "in":[1,1] }, true);
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(1)));
2019-07-08 15:08:04 +03:00
2020-03-26 21:24:20 +03:00
witness = await circuit.calculateWitness({ "in": [661, 660] }, true);
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(1)));
2019-07-08 15:08:04 +03:00
2020-03-26 21:24:20 +03:00
witness = await circuit.calculateWitness({ "in": [0, 1] }, true);
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(0)));
2019-07-08 15:08:04 +03:00
2020-03-26 21:24:20 +03:00
witness = await circuit.calculateWitness({ "in": [0, 444] }, true);
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(0)));
2019-07-08 15:08:04 +03:00
2020-03-26 21:24:20 +03:00
witness = await circuit.calculateWitness({ "in": [1, 0] }, true);
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(1)));
2019-07-08 15:08:04 +03:00
2020-03-26 21:24:20 +03:00
witness = await circuit.calculateWitness({ "in": [555, 0] }, true);
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(1)));
2019-07-08 15:08:04 +03:00
2020-03-26 21:24:20 +03:00
witness = await circuit.calculateWitness({ "in": [0, 0] }, true);
assert(Fr.eq(witness[0], Fr.e(1)));
assert(Fr.eq(witness[1], Fr.e(1)));
2019-07-08 15:08:04 +03:00
});
2018-12-06 19:32:49 +03:00
});