You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.5 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. const path = require("path");
  2. const bigInt = require("big-integer");
  3. const tester = require("circom").tester;
  4. function print(circuit, w, s) {
  5. console.log(s + ": " + w[circuit.getSignalIdx(s)]);
  6. }
  7. async function checkSub(_a,_b, circuit) {
  8. let a=bigInt(_a);
  9. let b=bigInt(_b);
  10. if (a.lesser(bigInt.zero)) a = a.add(bigInt.one.shiftLeft(16));
  11. if (b.lesser(bigInt.zero)) b = b.add(bigInt.one.shiftLeft(16));
  12. const w = await circuit.calculateWitness({a: a, b: b}, true);
  13. let res = a.minus(b);
  14. if (res.lesser(bigInt.zero)) res = res.add(bigInt.one.shiftLeft(16));
  15. await circuit.assertOut(w, {out: bigInt(res)});
  16. }
  17. describe("BinSub test", function () {
  18. this.timeout(100000);
  19. let circuit;
  20. before( async() => {
  21. circuit = await tester(path.join(__dirname, "circuits", "binsub_test.circom"));
  22. });
  23. it("Should check variuos ege cases", async () => {
  24. await checkSub(0,0, circuit);
  25. await checkSub(1,0, circuit);
  26. await checkSub(-1,0, circuit);
  27. await checkSub(2,1, circuit);
  28. await checkSub(2,2, circuit);
  29. await checkSub(2,3, circuit);
  30. await checkSub(2,-1, circuit);
  31. await checkSub(2,-2, circuit);
  32. await checkSub(2,-3, circuit);
  33. await checkSub(-2,-3, circuit);
  34. await checkSub(-2,-2, circuit);
  35. await checkSub(-2,-1, circuit);
  36. await checkSub(-2,0, circuit);
  37. await checkSub(-2,1, circuit);
  38. await checkSub(-2,2, circuit);
  39. await checkSub(-2,3, circuit);
  40. });
  41. });