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.

42 lines
1.1 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. const chai = require("chai");
  2. const path = require("path");
  3. const snarkjs = require("snarkjs");
  4. const crypto = require("crypto");
  5. const compiler = require("../index.js");
  6. const assert = chai.assert;
  7. const sha256 = require("./helpers/sha256");
  8. // const printSignal = require("./helpers/printsignal");
  9. describe("SHA256 test", () => {
  10. it("Should calculate a hash", async () => {
  11. const cirDef = await compiler(path.join(__dirname, "circuits", "sha256_2_test.circom"));
  12. const circuit = new snarkjs.Circuit(cirDef);
  13. console.log("Vars: "+circuit.nVars);
  14. console.log("Constraints: "+circuit.nConstraints);
  15. const witness = circuit.calculateWitness({ "a": "1", "b": "2" });
  16. const b = new Buffer.alloc(54);
  17. b[26] = 1;
  18. b[53] = 2;
  19. const hash = crypto.createHash("sha256")
  20. .update(b)
  21. .digest("hex");
  22. const r = "0x" + hash.slice(10);
  23. const hash2 = sha256.hash(b.toString("hex"), {msgFormat: "hex-bytes"});
  24. assert.equal(hash, hash2);
  25. assert(witness[1].equals(snarkjs.bigInt(r)));
  26. }).timeout(1000000);
  27. });