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.

53 lines
1.5 KiB

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