2018-11-27 18:03:57 +03:00
|
|
|
const path = require("path");
|
2020-04-18 23:33:59 +03:00
|
|
|
const Fr = require("ffjavascript").bn128.Fr;
|
|
|
|
const Scalar = require("ffjavascript").Scalar;
|
2019-12-14 22:32:45 +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(Fr.one);
|
2018-11-27 18:03:57 +03:00
|
|
|
} else {
|
2020-04-18 23:33:59 +03:00
|
|
|
res.push(Fr.zero);
|
2018-11-27 18:03:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2020-04-18 23:33:59 +03:00
|
|
|
const q = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617");
|
2018-11-27 18:03:57 +03:00
|
|
|
|
2019-12-14 22:32:45 +03:00
|
|
|
describe("Sign test", function() {
|
2018-11-27 18:03:57 +03:00
|
|
|
let circuit;
|
2019-12-14 22:32:45 +03:00
|
|
|
this.timeout(100000);
|
2018-11-27 18:03:57 +03:00
|
|
|
|
2019-12-14 22:32:45 +03:00
|
|
|
before( async() => {
|
|
|
|
circuit = await tester(path.join(__dirname, "circuits", "sign_test.circom"));
|
2018-11-27 18:03:57 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it("Sign of 0", async () => {
|
2020-04-18 23:33:59 +03:00
|
|
|
const inp = getBits(Scalar.e(0), 254);
|
2020-03-26 21:24:20 +03:00
|
|
|
const w = await circuit.calculateWitness({in: inp}, true);
|
2018-11-27 18:03:57 +03:00
|
|
|
|
2019-12-14 22:32:45 +03:00
|
|
|
await circuit.assertOut(w, {sign: 0});
|
2018-11-27 18:03:57 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it("Sign of 3", async () => {
|
2020-04-18 23:33:59 +03:00
|
|
|
const inp = getBits(Scalar.e(3), 254);
|
2020-03-26 21:24:20 +03:00
|
|
|
const w = await circuit.calculateWitness({in: inp}, true);
|
2018-11-27 18:03:57 +03:00
|
|
|
|
2019-12-14 22:32:45 +03:00
|
|
|
await circuit.assertOut(w, {sign: 0});
|
2018-11-27 18:03:57 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it("Sign of q/2", async () => {
|
2020-04-18 23:33:59 +03:00
|
|
|
const inp = getBits(Scalar.shr(q, 1), 254);
|
2020-03-26 21:24:20 +03:00
|
|
|
const w = await circuit.calculateWitness({in: inp}, true);
|
2018-11-27 18:03:57 +03:00
|
|
|
|
2019-12-14 22:32:45 +03:00
|
|
|
await circuit.assertOut(w, {sign: 0});
|
2018-11-27 18:03:57 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it("Sign of q/2+1", async () => {
|
2020-04-18 23:33:59 +03:00
|
|
|
const inp = getBits(Scalar.add(Scalar.shr(q, 1), 1) , 254);
|
2020-03-26 21:24:20 +03:00
|
|
|
const w = await circuit.calculateWitness({in: inp}, true);
|
2018-11-27 18:03:57 +03:00
|
|
|
|
2019-12-14 22:32:45 +03:00
|
|
|
await circuit.assertOut(w, {sign: 1});
|
2018-11-27 18:03:57 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it("Sign of q-1", async () => {
|
2020-04-18 23:33:59 +03:00
|
|
|
const inp = getBits(Scalar.sub(q, 1), 254);
|
2020-03-26 21:24:20 +03:00
|
|
|
const w = await circuit.calculateWitness({in: inp}, true);
|
2018-11-27 18:03:57 +03:00
|
|
|
|
2019-12-14 22:32:45 +03:00
|
|
|
await circuit.assertOut(w, {sign: 1});
|
2018-11-27 18:03:57 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it("Sign of q", async () => {
|
|
|
|
const inp = getBits(q, 254);
|
2020-03-26 21:24:20 +03:00
|
|
|
const w = await circuit.calculateWitness({in: inp}, true);
|
2018-11-27 18:03:57 +03:00
|
|
|
|
2019-12-14 22:32:45 +03:00
|
|
|
await circuit.assertOut(w, {sign: 1});
|
2018-11-27 18:03:57 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it("Sign of all ones", async () => {
|
2020-04-18 23:33:59 +03:00
|
|
|
const inp = getBits(Scalar.sub(Scalar.shl(1,254),1), 254);
|
2020-03-26 21:24:20 +03:00
|
|
|
const w = await circuit.calculateWitness({in: inp}, true);
|
2018-11-27 18:03:57 +03:00
|
|
|
|
2019-12-14 22:32:45 +03:00
|
|
|
await circuit.assertOut(w, {sign: 1});
|
2018-11-27 18:03:57 +03:00
|
|
|
});
|
|
|
|
});
|