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.

56 lines
1.6 KiB

6 years ago
  1. const chai = require("chai");
  2. const path = require("path");
  3. const snarkjs = require("snarkjs");
  4. const compiler = require("circom");
  5. const assert = chai.assert;
  6. const bigInt = snarkjs.bigInt;
  7. function print(circuit, w, s) {
  8. console.log(s + ": " + w[circuit.getSignalIdx(s)]);
  9. }
  10. function checkSub(_a,_b, circuit) {
  11. let a=bigInt(_a);
  12. let b=bigInt(_b);
  13. if (a.lesser(bigInt.zero)) a = a.add(bigInt.one.shl(16));
  14. if (b.lesser(bigInt.zero)) b = b.add(bigInt.one.shl(16));
  15. const w = circuit.calculateWitness({a: a, b: b});
  16. let res = a.sub(b);
  17. if (res.lesser(bigInt.zero)) res = res.add(bigInt.one.shl(16));
  18. assert( w[circuit.getSignalIdx("main.out")].equals(bigInt(res)) );
  19. }
  20. describe("BinSub test", () => {
  21. let circuit;
  22. before( async() => {
  23. const cirDef = await compiler(path.join(__dirname, "circuits", "binsub_test.circom"));
  24. circuit = new snarkjs.Circuit(cirDef);
  25. console.log("NConstrains BinSub: " + circuit.nConstraints);
  26. });
  27. it("Should check variuos ege cases", async () => {
  28. checkSub(0,0, circuit);
  29. checkSub(1,0, circuit);
  30. checkSub(-1,0, circuit);
  31. checkSub(2,1, circuit);
  32. checkSub(2,2, circuit);
  33. checkSub(2,3, circuit);
  34. checkSub(2,-1, circuit);
  35. checkSub(2,-2, circuit);
  36. checkSub(2,-3, circuit);
  37. checkSub(-2,-3, circuit);
  38. checkSub(-2,-2, circuit);
  39. checkSub(-2,-1, circuit);
  40. checkSub(-2,0, circuit);
  41. checkSub(-2,1, circuit);
  42. checkSub(-2,2, circuit);
  43. checkSub(-2,3, circuit);
  44. });
  45. });