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