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