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