arnaucube
arnaucube/circomlibPublic Self-hosted repository
circomlib/test/sign.js
test/sign.jsRaw
 1const chai = require("chai");
 2const path = require("path");
 3const snarkjs = require("snarkjs");
 4const compiler = require("circom");
 5
 6const assert = chai.assert;
 7
 8const bigInt = snarkjs.bigInt;
 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.shr(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("Sign test", () => {
29    let circuit;
30    before( async() => {
31        const cirDef = await compiler(path.join(__dirname, "circuits", "sign_test.circom"));
32
33        circuit = new snarkjs.Circuit(cirDef);
34
35        console.log("NConstrains: " + circuit.nConstraints);
36    });
37
38    it("Sign of 0", async () => {
39        const inp = getBits(bigInt.zero, 254);
40        const w = circuit.calculateWitness({in: inp});
41
42        assert( w[circuit.getSignalIdx("main.sign")].equals(bigInt(0)) );
43    });
44
45    it("Sign of 3", async () => {
46        const inp = getBits(bigInt(3), 254);
47        const w = circuit.calculateWitness({in: inp});
48
49        assert( w[circuit.getSignalIdx("main.sign")].equals(bigInt(0)) );
50    });
51
52    it("Sign of q/2", async () => {
53        const inp = getBits(q.shr(bigInt.one), 254);
54        const w = circuit.calculateWitness({in: inp});
55
56        assert( w[circuit.getSignalIdx("main.sign")].equals(bigInt(0)) );
57    });
58
59    it("Sign of q/2+1", async () => {
60        const inp = getBits(q.shr(bigInt.one).add(bigInt.one), 254);
61        const w = circuit.calculateWitness({in: inp});
62
63        assert( w[circuit.getSignalIdx("main.sign")].equals(bigInt(1)) );
64    });
65
66    it("Sign of q-1", async () => {
67        const inp = getBits(q.sub(bigInt.one), 254);
68        const w = circuit.calculateWitness({in: inp});
69
70        assert( w[circuit.getSignalIdx("main.sign")].equals(bigInt(1)) );
71    });
72
73    it("Sign of q", async () => {
74        const inp = getBits(q, 254);
75        const w = circuit.calculateWitness({in: inp});
76
77        assert( w[circuit.getSignalIdx("main.sign")].equals(bigInt(1)) );
78    });
79
80    it("Sign of all ones", async () => {
81        const inp = getBits(bigInt(1).shl(254).sub(bigInt(1)), 254);
82        const w = circuit.calculateWitness({in: inp});
83
84        assert( w[circuit.getSignalIdx("main.sign")].equals(bigInt(1)) );
85    });
86
87
88});