arnaucube
arnaucube/circomlibPublic Self-hosted repository
circomlib/test/binsub.js
test/binsub.jsRaw
 1const path = require("path");
 2
 3const Fr = require("ffjavascript").bn128.Fr;
 4const Scalar = require("ffjavascript").Scalar;
 5const tester = require("circom").tester;
 6
 7function print(circuit, w, s) {
 8    console.log(s + ": " + w[circuit.getSignalIdx(s)]);
 9}
10
11async function checkSub(_a,_b, circuit) {
12    let a=Scalar.e(_a);
13    let b=Scalar.e(_b);
14    if (Scalar.lt(a, 0)) a = Scalar.add(a, Scalar.shl(1, 16));
15    if (Scalar.lt(b, 0)) b = Scalar.add(b, Scalar.shl(1, 16));
16    const w = await circuit.calculateWitness({a: a, b: b}, true);
17
18    let res = Scalar.sub(a, b);
19    if (Scalar.lt(res, 0)) res = Scalar.add(res, Scalar.shl(1, 16));
20
21    await circuit.assertOut(w, {out: res});
22}
23
24describe("BinSub test", function () {
25
26    this.timeout(100000);
27
28    let circuit;
29    before( async() => {
30        circuit = await tester(path.join(__dirname, "circuits", "binsub_test.circom"));
31    });
32
33    it("Should check variuos ege cases", async () => {
34        await checkSub(0,0, circuit);
35        await checkSub(1,0, circuit);
36        await checkSub(-1,0, circuit);
37        await checkSub(2,1, circuit);
38        await checkSub(2,2, circuit);
39        await checkSub(2,3, circuit);
40        await checkSub(2,-1, circuit);
41        await checkSub(2,-2, circuit);
42        await checkSub(2,-3, circuit);
43        await checkSub(-2,-3, circuit);
44        await checkSub(-2,-2, circuit);
45        await checkSub(-2,-1, circuit);
46        await checkSub(-2,0, circuit);
47        await checkSub(-2,1, circuit);
48        await checkSub(-2,2, circuit);
49        await checkSub(-2,3, circuit);
50    });
51
52
53});