circomlib/test/aliascheck.js
test/aliascheck.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 getBits(v, n) {
15 const res = [];
16 for (let i=0; i<n; i++) {
17 if (v.shr(i).isOdd()) {
18 res.push(bigInt.one);
19 } else {
20 res.push(bigInt.zero);
21 }
22 }
23 return res;
24}
25
26const q = bigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617");
27
28describe("Aliascheck test", () => {
29 let circuit;
30 before( async() => {
31 const cirDef = await compiler(path.join(__dirname, "circuits", "aliascheck_test.circom"));
32
33 circuit = new snarkjs.Circuit(cirDef);
34
35 console.log("NConstrains: " + circuit.nConstraints);
36 });
37
38 it("Satisfy the aliastest 0", async () => {
39 const inp = getBits(bigInt.zero, 254);
40 circuit.calculateWitness({in: inp});
41 });
42
43 it("Satisfy the aliastest 3", async () => {
44 const inp = getBits(bigInt(3), 254);
45 circuit.calculateWitness({in: inp});
46 });
47
48 it("Satisfy the aliastest q-1", async () => {
49 const inp = getBits(q.sub(bigInt.one), 254);
50 circuit.calculateWitness({in: inp});
51 });
52
53 it("Nhot not satisfy an input of q", async () => {
54 const inp = getBits(q, 254);
55 try {
56 circuit.calculateWitness({in: inp});
57 assert(false);
58 } catch(err) {
59 assert(/Constraint\sdoesn't\smatch(.*)1\s!=\s0/.test(err.message) );
60 }
61 });
62
63 it("Nhot not satisfy all ones", async () => {
64
65 const inp = getBits(bigInt(1).shl(254).sub(bigInt(1)), 254);
66 try {
67 circuit.calculateWitness({in: inp});
68 assert(false);
69 } catch(err) {
70 assert(/Constraint\sdoesn't\smatch(.*)1\s!=\s0/.test(err.message) );
71 }
72 });
73
74});