circomlib/test/aliascheck.js
test/aliascheck.jsRaw
1const chai = require("chai");
2const path = require("path");
3const compiler = require("circom");
4
5const assert = chai.assert;
6
7const bigInt = require("big-integer");
8
9const tester = require("circom").tester;
10
11function print(circuit, w, s) {
12 console.log(s + ": " + w[circuit.getSignalIdx(s)]);
13}
14
15function getBits(v, n) {
16 const res = [];
17 for (let i=0; i<n; i++) {
18 if (v.shiftRight(i).isOdd()) {
19 res.push(bigInt.one);
20 } else {
21 res.push(bigInt.zero);
22 }
23 }
24 return res;
25}
26
27const q = bigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617");
28
29describe("Aliascheck test", function () {
30 this.timeout(100000);
31
32 let cir;
33 before( async() => {
34
35 cir = await tester(path.join(__dirname, "circuits", "aliascheck_test.circom"));
36 });
37
38 it("Satisfy the aliastest 0", async () => {
39 const inp = getBits(bigInt.zero, 254);
40 await cir.calculateWitness({in: inp});
41 });
42
43 it("Satisfy the aliastest 3", async () => {
44 const inp = getBits(bigInt(3), 254);
45 await cir.calculateWitness({in: inp});
46 });
47
48 it("Satisfy the aliastest q-1", async () => {
49 const inp = getBits(q.minus(bigInt.one), 254);
50 await cir.calculateWitness({in: inp});
51 });
52
53 it("Should not satisfy an input of q", async () => {
54 const inp = getBits(q, 254);
55 try {
56 await cir.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("Should not satisfy all ones", async () => {
64
65 const inp = getBits(bigInt(1).shiftLeft(254).minus(bigInt.one), 254);
66 try {
67 await cir.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});