circomlib/test/aliascheck.js
test/aliascheck.jsRaw
1const chai = require("chai");
2const path = require("path");
3
4const assert = chai.assert;
5
6const Scalar = require("ffjavascript").Scalar;
7const F1Field = require("ffjavascript").F1Field;
8const utils = require("ffjavascript").utils;
9const q = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617");
10const F = new F1Field(q);
11
12const tester = require("circom").tester;
13
14function print(circuit, w, s) {
15 console.log(s + ": " + w[circuit.getSignalIdx(s)]);
16}
17
18function 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
31describe("Aliascheck test", function () {
32 this.timeout(100000);
33
34 let cir;
35 before( async() => {
36
37 cir = await 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.minusone, 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(/Constraint\sdoesn't\smatch(.*)1\s!=\s0/.test(err.message) );
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(/Constraint\sdoesn't\smatch(.*)1\s!=\s0/.test(err.message) );
74 }
75 });
76
77});