arnaucube
arnaucube/circomlibPublic Self-hosted repository
circomlib/test/sign.js
test/sign.jsRaw
 1const path = require("path");
 2const bigInt = require("big-integer");
 3const tester = require("circom").tester;
 4
 5function print(circuit, w, s) {
 6    console.log(s + ": " + w[circuit.getSignalIdx(s)]);
 7}
 8
 9function getBits(v, n) {
10    const res = [];
11    for (let i=0; i<n; i++) {
12        if (v.shiftRight(i).isOdd()) {
13            res.push(bigInt.one);
14        } else {
15            res.push(bigInt.zero);
16        }
17    }
18    return res;
19}
20
21const q = bigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617");
22
23describe("Sign test", function() {
24    let circuit;
25    this.timeout(100000);
26
27    before( async() => {
28        circuit = await tester(path.join(__dirname, "circuits", "sign_test.circom"));
29    });
30
31    it("Sign of 0", async () => {
32        const inp = getBits(bigInt.zero, 254);
33        const w = await circuit.calculateWitness({in: inp}, true);
34
35        await circuit.assertOut(w, {sign: 0});
36    });
37
38    it("Sign of 3", async () => {
39        const inp = getBits(bigInt(3), 254);
40        const w = await circuit.calculateWitness({in: inp}, true);
41
42        await circuit.assertOut(w, {sign: 0});
43    });
44
45    it("Sign of q/2", async () => {
46        const inp = getBits(q.shiftRight(bigInt.one), 254);
47        const w = await circuit.calculateWitness({in: inp}, true);
48
49        await circuit.assertOut(w, {sign: 0});
50    });
51
52    it("Sign of q/2+1", async () => {
53        const inp = getBits(q.shiftRight(bigInt.one).add(bigInt.one), 254);
54        const w = await circuit.calculateWitness({in: inp}, true);
55
56        await circuit.assertOut(w, {sign: 1});
57    });
58
59    it("Sign of q-1", async () => {
60        const inp = getBits(q.minus(bigInt.one), 254);
61        const w = await circuit.calculateWitness({in: inp}, true);
62
63        await circuit.assertOut(w, {sign: 1});
64    });
65
66    it("Sign of q", async () => {
67        const inp = getBits(q, 254);
68        const w = await circuit.calculateWitness({in: inp}, true);
69
70        await circuit.assertOut(w, {sign: 1});
71    });
72
73    it("Sign of all ones", async () => {
74        const inp = getBits(bigInt(1).shiftLeft(254).minus(bigInt(1)), 254);
75        const w = await circuit.calculateWitness({in: inp}, true);
76
77        await circuit.assertOut(w, {sign: 1});
78    });
79});