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