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.

54 lines
1.7 KiB

  1. const chai = require("chai");
  2. const path = require("path");
  3. const zkSnark = require("zksnark");
  4. const crypto = require("crypto");
  5. const compiler = require("../index.js");
  6. const assert = chai.assert;
  7. describe("SHA256 test", () => {
  8. it("Should create a constant circuit", async () => {
  9. const cirDef = await compiler(path.join(__dirname, "circuits", "constants_test.circom"));
  10. assert.equal(cirDef.nVars, 2);
  11. const circuit = new zkSnark.Circuit(cirDef);
  12. const witness = circuit.calculateWitness({ "in": "0xd807aa98" });
  13. assert(witness[0].equals(zkSnark.bigInt(1)));
  14. assert(witness[1].equals(zkSnark.bigInt("0xd807aa98")));
  15. });
  16. it("Should create a sum circuit", async () => {
  17. const cirDef = await compiler(path.join(__dirname, "circuits", "sum_test.circom"));
  18. assert.equal(cirDef.nVars, 101);
  19. const circuit = new zkSnark.Circuit(cirDef);
  20. const witness = circuit.calculateWitness({ "a": "111", "b": "222" });
  21. assert(witness[0].equals(zkSnark.bigInt(1)));
  22. assert(witness[1].equals(zkSnark.bigInt("333")));
  23. });
  24. it("Should calculate a hash", async () => {
  25. const cirDef = await compiler(path.join(__dirname, "circuits", "sha256_2_test.circom"));
  26. const circuit = new zkSnark.Circuit(cirDef);
  27. const witness = circuit.calculateWitness({ "a": "1", "b": "2" });
  28. const b = new Buffer.alloc(432);
  29. b[115] = 1;
  30. b[431] = 2;
  31. const hash = crypto.createHash("sha256")
  32. .update(b)
  33. .digest("hex");
  34. const r = "0x" + hash.slice(40);
  35. assert(witness[1].equals(zkSnark.bigInt(r)));
  36. });
  37. });