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.

67 lines
2.3 KiB

5 years ago
  1. const chai = require("chai");
  2. const path = require("path");
  3. const snarkjs = require("snarkjs");
  4. const bigInt = snarkjs.bigInt;
  5. const compiler = require("../index.js");
  6. const assert = chai.assert;
  7. async function assertThrowsAsync(fn, regExp) {
  8. let f = () => {};
  9. try {
  10. await fn();
  11. } catch(e) {
  12. f = () => { throw e; };
  13. } finally {
  14. assert.throws(f, regExp);
  15. }
  16. }
  17. describe("Sum test", () => {
  18. it("Should compile a code with an undefined if", async () => {
  19. await compiler(path.join(__dirname, "circuits", "undefinedif.circom"));
  20. });
  21. it("Should compile a code with vars inside a for", async () => {
  22. const cirDef = await compiler(path.join(__dirname, "circuits", "forvariables.circom"));
  23. const circuit = new snarkjs.Circuit(cirDef);
  24. const witness = circuit.calculateWitness({ "in": 111});
  25. assert(witness[0].equals(bigInt(1)));
  26. assert(witness[1].equals(bigInt(114)));
  27. assert(witness[2].equals(bigInt(111)));
  28. });
  29. it("Should compile a code with an undefined if", async () => {
  30. const cirDef = await compiler(path.join(__dirname, "circuits", "mixvarsignal.circom"));
  31. const circuit = new snarkjs.Circuit(cirDef);
  32. const witness = circuit.calculateWitness({ "i": 111});
  33. assert(witness[0].equals(bigInt(1)));
  34. assert(witness[1].equals(bigInt(111*111)));
  35. assert(witness[2].equals(bigInt(111)));
  36. });
  37. // it("Should assign signal ERROR", async () => {
  38. // await assertThrowsAsync(async () => {
  39. // await compiler(path.join(__dirname, "circuits", "assignsignal.circom"));
  40. // }, /Cannot assign to a signal .*/);
  41. // });
  42. it("Should compile a code with compute", async () => {
  43. const cirDef = await compiler(path.join(__dirname, "circuits", "compute.circom"));
  44. const circuit = new snarkjs.Circuit(cirDef);
  45. const witness = circuit.calculateWitness({ "x": 6});
  46. assert(witness[0].equals(bigInt(1)));
  47. assert(witness[1].equals(bigInt(37)));
  48. assert(witness[2].equals(bigInt(6)));
  49. });
  50. it("Should compile a code with compute", async () => {
  51. const cirDef = await compiler(path.join(__dirname, "circuits", "inout.circom"));
  52. assert.equal(cirDef.constraints.length, 1);
  53. });
  54. });