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