2018-11-27 18:03:57 +03:00
|
|
|
const chai = require("chai");
|
|
|
|
const path = require("path");
|
2023-07-21 13:10:01 +03:00
|
|
|
const snarkjs = require("@tornado/snarkjs");
|
2018-11-27 18:03:57 +03:00
|
|
|
const compiler = require("circom");
|
|
|
|
|
|
|
|
const assert = chai.assert;
|
|
|
|
|
|
|
|
const bigInt = snarkjs.bigInt;
|
|
|
|
|
|
|
|
function print(circuit, w, s) {
|
|
|
|
console.log(s + ": " + w[circuit.getSignalIdx(s)]);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getBits(v, n) {
|
|
|
|
const res = [];
|
2023-07-21 13:10:01 +03:00
|
|
|
for (let i = 0; i < n; i++) {
|
2018-11-27 18:03:57 +03:00
|
|
|
if (v.shr(i).isOdd()) {
|
|
|
|
res.push(bigInt.one);
|
|
|
|
} else {
|
|
|
|
res.push(bigInt.zero);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
const q = bigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617");
|
|
|
|
|
|
|
|
describe("Aliascheck test", () => {
|
|
|
|
let circuit;
|
2023-07-21 13:10:01 +03:00
|
|
|
before(async () => {
|
2018-11-27 18:03:57 +03:00
|
|
|
const cirDef = await compiler(path.join(__dirname, "circuits", "aliascheck_test.circom"));
|
|
|
|
|
|
|
|
circuit = new snarkjs.Circuit(cirDef);
|
|
|
|
|
|
|
|
console.log("NConstrains: " + circuit.nConstraints);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Satisfy the aliastest 0", async () => {
|
|
|
|
const inp = getBits(bigInt.zero, 254);
|
2023-07-21 13:10:01 +03:00
|
|
|
circuit.calculateWitness({ in: inp });
|
2018-11-27 18:03:57 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it("Satisfy the aliastest 3", async () => {
|
|
|
|
const inp = getBits(bigInt(3), 254);
|
2023-07-21 13:10:01 +03:00
|
|
|
circuit.calculateWitness({ in: inp });
|
2018-11-27 18:03:57 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it("Satisfy the aliastest q-1", async () => {
|
|
|
|
const inp = getBits(q.sub(bigInt.one), 254);
|
2023-07-21 13:10:01 +03:00
|
|
|
circuit.calculateWitness({ in: inp });
|
2018-11-27 18:03:57 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it("Nhot not satisfy an input of q", async () => {
|
|
|
|
const inp = getBits(q, 254);
|
|
|
|
try {
|
2023-07-21 13:10:01 +03:00
|
|
|
circuit.calculateWitness({ in: inp });
|
2018-11-27 18:03:57 +03:00
|
|
|
assert(false);
|
2023-07-21 13:10:01 +03:00
|
|
|
} catch (err) {
|
|
|
|
assert(/Constraint\sdoesn't\smatch(.*)1\s!=\s0/.test(err.message));
|
2019-11-23 22:19:47 +03:00
|
|
|
assert(err.message.indexOf("1 != 0") >= 0);
|
2018-11-27 18:03:57 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Nhot not satisfy all ones", async () => {
|
|
|
|
const inp = getBits(bigInt(1).shl(254).sub(bigInt(1)), 254);
|
|
|
|
try {
|
2023-07-21 13:10:01 +03:00
|
|
|
circuit.calculateWitness({ in: inp });
|
2018-11-27 18:03:57 +03:00
|
|
|
assert(false);
|
2023-07-21 13:10:01 +03:00
|
|
|
} catch (err) {
|
|
|
|
assert(/Constraint\sdoesn't\smatch(.*)1\s!=\s0/.test(err.message));
|
2019-11-23 22:19:47 +03:00
|
|
|
assert(err.message.indexOf("1 != 0") >= 0);
|
2018-11-27 18:03:57 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|