2018-11-27 18:03:57 +03:00
|
|
|
const chai = require("chai");
|
|
|
|
const path = require("path");
|
|
|
|
|
|
|
|
const assert = chai.assert;
|
|
|
|
|
2020-04-18 23:33:59 +03:00
|
|
|
const Scalar = require("ffjavascript").Scalar;
|
|
|
|
const F1Field = require("ffjavascript").F1Field;
|
|
|
|
const utils = require("ffjavascript").utils;
|
|
|
|
const q = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617");
|
|
|
|
const F = new F1Field(q);
|
2019-12-12 18:49:58 +03:00
|
|
|
|
|
|
|
const tester = require("circom").tester;
|
2018-11-27 18:03:57 +03:00
|
|
|
|
|
|
|
function print(circuit, w, s) {
|
|
|
|
console.log(s + ": " + w[circuit.getSignalIdx(s)]);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getBits(v, n) {
|
|
|
|
const res = [];
|
|
|
|
for (let i=0; i<n; i++) {
|
2020-04-18 23:33:59 +03:00
|
|
|
if (Scalar.isOdd(Scalar.shr(v,i))) {
|
|
|
|
res.push(F.one);
|
2018-11-27 18:03:57 +03:00
|
|
|
} else {
|
2020-04-18 23:33:59 +03:00
|
|
|
res.push(F.zero);
|
2018-11-27 18:03:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-12 18:49:58 +03:00
|
|
|
describe("Aliascheck test", function () {
|
|
|
|
this.timeout(100000);
|
2018-11-27 18:03:57 +03:00
|
|
|
|
2019-12-12 18:49:58 +03:00
|
|
|
let cir;
|
|
|
|
before( async() => {
|
2018-11-27 18:03:57 +03:00
|
|
|
|
2019-12-12 18:49:58 +03:00
|
|
|
cir = await tester(path.join(__dirname, "circuits", "aliascheck_test.circom"));
|
2018-11-27 18:03:57 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it("Satisfy the aliastest 0", async () => {
|
2020-04-18 23:33:59 +03:00
|
|
|
const inp = getBits(0, 254);
|
2020-03-26 21:24:20 +03:00
|
|
|
await cir.calculateWitness({in: inp}, true);
|
2018-11-27 18:03:57 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it("Satisfy the aliastest 3", async () => {
|
2020-04-18 23:33:59 +03:00
|
|
|
const inp = getBits(3, 254);
|
2020-03-26 21:24:20 +03:00
|
|
|
await cir.calculateWitness({in: inp}, true);
|
2018-11-27 18:03:57 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it("Satisfy the aliastest q-1", async () => {
|
2020-04-18 23:33:59 +03:00
|
|
|
const inp = getBits(F.minusone, 254);
|
|
|
|
// console.log(JSON.stringify(utils.stringifyBigInts(inp)));
|
2020-03-26 21:24:20 +03:00
|
|
|
await cir.calculateWitness({in: inp}, true);
|
2018-11-27 18:03:57 +03:00
|
|
|
});
|
|
|
|
|
2019-12-12 18:49:58 +03:00
|
|
|
it("Should not satisfy an input of q", async () => {
|
2018-11-27 18:03:57 +03:00
|
|
|
const inp = getBits(q, 254);
|
|
|
|
try {
|
2020-03-26 21:24:20 +03:00
|
|
|
await cir.calculateWitness({in: inp}, true);
|
2018-11-27 18:03:57 +03:00
|
|
|
assert(false);
|
|
|
|
} catch(err) {
|
2019-12-04 23:57:02 +03:00
|
|
|
assert(/Constraint\sdoesn't\smatch(.*)1\s!=\s0/.test(err.message) );
|
2018-11-27 18:03:57 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-12-12 18:49:58 +03:00
|
|
|
it("Should not satisfy all ones", async () => {
|
2018-11-27 18:03:57 +03:00
|
|
|
|
2020-04-18 23:33:59 +03:00
|
|
|
const inp = getBits(Scalar.sub(Scalar.shl(1, 254) , 1) , 254);
|
2018-11-27 18:03:57 +03:00
|
|
|
try {
|
2020-03-26 21:24:20 +03:00
|
|
|
await cir.calculateWitness({in: inp}, true);
|
2018-11-27 18:03:57 +03:00
|
|
|
assert(false);
|
|
|
|
} catch(err) {
|
2019-12-04 23:57:02 +03:00
|
|
|
assert(/Constraint\sdoesn't\smatch(.*)1\s!=\s0/.test(err.message) );
|
2018-11-27 18:03:57 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|