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.

52 lines
1.5 KiB

  1. const path = require("path");
  2. const Scalar = require("ffjavascript").Scalar;
  3. const wasm_tester = require("circom_tester").wasm;
  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=Scalar.e(_a);
  9. let b=Scalar.e(_b);
  10. if (Scalar.lt(a, 0)) a = Scalar.add(a, Scalar.shl(1, 16));
  11. if (Scalar.lt(b, 0)) b = Scalar.add(b, Scalar.shl(1, 16));
  12. const w = await circuit.calculateWitness({a: a, b: b}, true);
  13. let res = Scalar.sub(a, b);
  14. if (Scalar.lt(res, 0)) res = Scalar.add(res, Scalar.shl(1, 16));
  15. await circuit.assertOut(w, {out: res});
  16. }
  17. describe("BinSub test", function () {
  18. this.timeout(100000);
  19. let circuit;
  20. before( async() => {
  21. circuit = await wasm_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. });