circomlib/test/binsub.js

52 lines
1.5 KiB
JavaScript
Raw Normal View History

2018-12-16 13:27:29 +03:00
const path = require("path");
2019-12-13 20:35:29 +03:00
const bigInt = require("big-integer");
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) {
2018-12-16 13:27:29 +03:00
let a=bigInt(_a);
let b=bigInt(_b);
2019-12-13 20:35:29 +03:00
if (a.lesser(bigInt.zero)) a = a.add(bigInt.one.shiftLeft(16));
if (b.lesser(bigInt.zero)) b = b.add(bigInt.one.shiftLeft(16));
const w = await circuit.calculateWitness({a: a, b: b});
2018-12-16 13:27:29 +03:00
2019-12-13 20:35:29 +03:00
let res = a.minus(b);
if (res.lesser(bigInt.zero)) res = res.add(bigInt.one.shiftLeft(16));
await circuit.assertOut(w, {out: bigInt(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
});
});