arnaucube
arnaucube/circomlibPublic Self-hosted repository
circomlib/test/aliascheck.js
test/aliascheck.jsRaw
 1const chai = require("chai");
 2const path = require("path");
 3
 4const assert = chai.assert;
 5
 6const bigInt = require("big-integer");
 7
 8const tester = require("circom").tester;
 9
10function print(circuit, w, s) {
11    console.log(s + ": " + w[circuit.getSignalIdx(s)]);
12}
13
14function getBits(v, n) {
15    const res = [];
16    for (let i=0; i<n; i++) {
17        if (v.shiftRight(i).isOdd()) {
18            res.push(bigInt.one);
19        } else {
20            res.push(bigInt.zero);
21        }
22    }
23    return res;
24}
25
26const q = bigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617");
27
28describe("Aliascheck test", function () {
29    this.timeout(100000);
30
31    let cir;
32    before( async() => {
33
34        cir = await tester(path.join(__dirname, "circuits", "aliascheck_test.circom"));
35    });
36
37    it("Satisfy the aliastest 0", async () => {
38        const inp = getBits(bigInt.zero, 254);
39        await cir.calculateWitness({in: inp}, true);
40    });
41
42    it("Satisfy the aliastest 3", async () => {
43        const inp = getBits(bigInt(3), 254);
44        await cir.calculateWitness({in: inp}, true);
45    });
46
47    it("Satisfy the aliastest q-1", async () => {
48        const inp = getBits(q.minus(bigInt.one), 254);
49        await cir.calculateWitness({in: inp}, true);
50    });
51
52    it("Should not satisfy an input of q", async () => {
53        const inp = getBits(q, 254);
54        try {
55            await cir.calculateWitness({in: inp}, true);
56            assert(false);
57        } catch(err) {
58            assert(/Constraint\sdoesn't\smatch(.*)1\s!=\s0/.test(err.message) );
59        }
60    });
61
62    it("Should not satisfy all ones", async () => {
63
64        const inp = getBits(bigInt(1).shiftLeft(254).minus(bigInt.one), 254);
65        try {
66            await cir.calculateWitness({in: inp}, true);
67            assert(false);
68        } catch(err) {
69            assert(/Constraint\sdoesn't\smatch(.*)1\s!=\s0/.test(err.message) );
70        }
71    });
72
73});