/ test / aliascheck.js
aliascheck.js
 1  const chai = require("chai");
 2  const path = require("path");
 3  
 4  const assert = chai.assert;
 5  
 6  const Scalar = require("ffjavascript").Scalar;
 7  const F1Field = require("ffjavascript").F1Field;
 8  const utils = require("ffjavascript").utils;
 9  const q = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617");
10  const F = new F1Field(q);
11  
12  const wasm_tester = require("circom_tester").wasm;
13  
14  function print(circuit, w, s) {
15      console.log(s + ": " + w[circuit.getSignalIdx(s)]);
16  }
17  
18  function getBits(v, n) {
19      const res = [];
20      for (let i=0; i<n; i++) {
21          if (Scalar.isOdd(Scalar.shr(v,i))) {
22              res.push(F.one);
23          } else {
24              res.push(F.zero);
25          }
26      }
27      return res;
28  }
29  
30  
31  describe("Aliascheck test", function () {
32      this.timeout(100000);
33  
34      let cir;
35      before( async() => {
36  
37          cir = await wasm_tester(path.join(__dirname, "circuits", "aliascheck_test.circom"));
38      });
39  
40      it("Satisfy the aliastest 0", async () => {
41          const inp = getBits(0, 254);
42          await cir.calculateWitness({in: inp}, true);
43      });
44  
45      it("Satisfy the aliastest 3", async () => {
46          const inp = getBits(3, 254);
47          await cir.calculateWitness({in: inp}, true);
48      });
49  
50      it("Satisfy the aliastest q-1", async () => {
51          const inp = getBits(F.e(-1), 254);
52          // console.log(JSON.stringify(utils.stringifyBigInts(inp)));
53          await cir.calculateWitness({in: inp}, true);
54      });
55  
56      it("Should not satisfy an input of q", async () => {
57          const inp = getBits(q, 254);
58          try {
59              await cir.calculateWitness({in: inp}, true);
60              assert(false);
61          } catch(err) {
62              assert(err.message.includes("Assert Failed"));
63          }
64      });
65  
66      it("Should not satisfy all ones", async () => {
67  
68          const inp = getBits(Scalar.sub(Scalar.shl(1, 254) , 1) , 254);
69          try {
70              await cir.calculateWitness({in: inp}, true);
71              assert(false);
72          } catch(err) {
73              assert(err.message.includes("Assert Failed"));
74          }
75      });
76  
77  });