circomlib/test/binsub.js
test/binsub.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 checkSub(_a,_b, circuit) {
15 let a=bigInt(_a);
16 let b=bigInt(_b);
17 if (a.lesser(bigInt.zero)) a = a.add(bigInt.one.shl(16));
18 if (b.lesser(bigInt.zero)) b = b.add(bigInt.one.shl(16));
19 const w = circuit.calculateWitness({a: a, b: b});
20
21 let res = a.sub(b);
22 if (res.lesser(bigInt.zero)) res = res.add(bigInt.one.shl(16));
23 assert( w[circuit.getSignalIdx("main.out")].equals(bigInt(res)) );
24}
25
26describe("BinSub test", () => {
27 let circuit;
28 before( async() => {
29 const cirDef = await compiler(path.join(__dirname, "circuits", "binsub_test.circom"));
30
31 circuit = new snarkjs.Circuit(cirDef);
32
33 console.log("NConstrains BinSub: " + circuit.nConstraints);
34 });
35
36 it("Should check variuos ege cases", async () => {
37 checkSub(0,0, circuit);
38 checkSub(1,0, circuit);
39 checkSub(-1,0, circuit);
40 checkSub(2,1, circuit);
41 checkSub(2,2, circuit);
42 checkSub(2,3, circuit);
43 checkSub(2,-1, circuit);
44 checkSub(2,-2, circuit);
45 checkSub(2,-3, circuit);
46 checkSub(-2,-3, circuit);
47 checkSub(-2,-2, circuit);
48 checkSub(-2,-1, circuit);
49 checkSub(-2,0, circuit);
50 checkSub(-2,1, circuit);
51 checkSub(-2,2, circuit);
52 checkSub(-2,3, circuit);
53 });
54
55
56});