2018-12-16 13:27:29 +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-13 20:35:29 +03:00
|
|
|
const tester = require("circom").tester;
|
2018-12-16 13:27:29 +03:00
|
|
|
|
|
|
|
function print(circuit, w, s) {
|
|
|
|
console.log(s + ": " + w[circuit.getSignalIdx(s)]);
|
|
|
|
}
|
|
|
|
|
2019-12-13 20:35:29 +03:00
|
|
|
async function checkSub(_a,_b, circuit) {
|
2020-04-18 23:33:59 +03:00
|
|
|
let a=Scalar.e(_a);
|
|
|
|
let b=Scalar.e(_b);
|
|
|
|
if (Scalar.lt(a, 0)) a = Scalar.add(a, Scalar.shl(1, 16));
|
|
|
|
if (Scalar.lt(b, 0)) b = Scalar.add(b, Scalar.shl(1, 16));
|
2020-03-26 21:24:20 +03:00
|
|
|
const w = await circuit.calculateWitness({a: a, b: b}, true);
|
2018-12-16 13:27:29 +03:00
|
|
|
|
2020-04-18 23:33:59 +03:00
|
|
|
let res = Scalar.sub(a, b);
|
|
|
|
if (Scalar.lt(res, 0)) res = Scalar.add(res, Scalar.shl(1, 16));
|
|
|
|
|
|
|
|
await circuit.assertOut(w, {out: res});
|
2018-12-16 13:27:29 +03:00
|
|
|
}
|
|
|
|
|
2019-12-13 20:35:29 +03:00
|
|
|
describe("BinSub test", function () {
|
2018-12-16 13:27:29 +03:00
|
|
|
|
2019-12-13 20:35:29 +03:00
|
|
|
this.timeout(100000);
|
2018-12-16 13:27:29 +03:00
|
|
|
|
2019-12-13 20:35:29 +03:00
|
|
|
let circuit;
|
|
|
|
before( async() => {
|
|
|
|
circuit = await tester(path.join(__dirname, "circuits", "binsub_test.circom"));
|
2018-12-16 13:27:29 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it("Should check variuos ege cases", async () => {
|
2019-12-13 20:35:29 +03:00
|
|
|
await checkSub(0,0, circuit);
|
|
|
|
await checkSub(1,0, circuit);
|
|
|
|
await checkSub(-1,0, circuit);
|
|
|
|
await checkSub(2,1, circuit);
|
|
|
|
await checkSub(2,2, circuit);
|
|
|
|
await checkSub(2,3, circuit);
|
|
|
|
await checkSub(2,-1, circuit);
|
|
|
|
await checkSub(2,-2, circuit);
|
|
|
|
await checkSub(2,-3, circuit);
|
|
|
|
await checkSub(-2,-3, circuit);
|
|
|
|
await checkSub(-2,-2, circuit);
|
|
|
|
await checkSub(-2,-1, circuit);
|
|
|
|
await checkSub(-2,0, circuit);
|
|
|
|
await checkSub(-2,1, circuit);
|
|
|
|
await checkSub(-2,2, circuit);
|
|
|
|
await checkSub(-2,3, circuit);
|
2018-12-16 13:27:29 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
});
|