circomlib/test/sha256.js
test/sha256.jsRaw
1const chai = require("chai");
2const path = require("path");
3const crypto = require("crypto");
4const Fr = require("ffjavascript").bn128.Fr;
5
6const assert = chai.assert;
7
8const sha256 = require("./helpers/sha256");
9
10const tester = require("circom").tester;
11
12// const printSignal = require("./helpers/printsignal");
13
14
15function buffer2bitArray(b) {
16 const res = [];
17 for (let i=0; i<b.length; i++) {
18 for (let j=0; j<8; j++) {
19 res.push((b[i] >> (7-j) &1));
20 }
21 }
22 return res;
23}
24
25function bitArray2buffer(a) {
26 const len = Math.floor((a.length -1 )/8)+1;
27 const b = new Buffer.alloc(len);
28
29 for (let i=0; i<a.length; i++) {
30 const p = Math.floor(i/8);
31 b[p] = b[p] | (Number(a[i]) << ( 7 - (i%8) ));
32 }
33 return b;
34}
35
36
37describe("SHA256 test", function () {
38 this.timeout(100000);
39
40
41 it("Should work bits to array and array to bits", async () => {
42 const b = new Buffer.alloc(64);
43 for (let i=0; i<64; i++) {
44 b[i] = i+1;
45 }
46 const a = buffer2bitArray(b);
47 const b2 = bitArray2buffer(a);
48
49 assert.equal(b.toString("hex"), b2.toString("hex"), true);
50 });
51
52 it("Should calculate a hash of 1 compressor", async () => {
53 const cir = await tester(path.join(__dirname, "circuits", "sha256_2_test.circom"));
54
55 const witness = await cir.calculateWitness({ "a": "1", "b": "2" }, true);
56
57 const b = new Buffer.alloc(54);
58 b[26] = 1;
59 b[53] = 2;
60
61 const hash = crypto.createHash("sha256")
62 .update(b)
63 .digest("hex");
64 const r = "0x" + hash.slice(10);
65
66 const hash2 = sha256.hash(b.toString("hex"), {msgFormat: "hex-bytes"});
67
68 assert.equal(hash, hash2);
69
70 assert(Fr.eq(witness[1], Fr.e(r)));
71 }).timeout(1000000);
72
73 it("Should calculate a hash of 2 compressor", async () => {
74 const cir = await tester(path.join(__dirname, "circuits", "sha256_test512.circom"));
75
76 const b = new Buffer.alloc(64);
77 for (let i=0; i<64; i++) {
78 b[i] = i+1;
79 }
80
81 const hash = crypto.createHash("sha256")
82 .update(b)
83 .digest("hex");
84
85 const arrIn = buffer2bitArray(b);
86 const witness = await cir.calculateWitness({ "in": arrIn }, true);
87
88 const arrOut = witness.slice(1, 257);
89 const hash2 = bitArray2buffer(arrOut).toString("hex");
90
91 assert.equal(hash, hash2);
92
93 }).timeout(1000000);
94 it ("Should calculate a hash of 2 compressor", async () => {
95 const cir = await tester(path.join(__dirname, "circuits", "sha256_test448.circom"));
96
97 const testStr = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
98
99 const b = Buffer.from(testStr, "utf8");
100
101 const hash = crypto.createHash("sha256")
102 .update(b)
103 .digest("hex");
104
105 const arrIn = buffer2bitArray(b);
106
107 const witness = await cir.calculateWitness({ "in": arrIn }, true);
108
109 const arrOut = witness.slice(1, 257);
110 const hash2 = bitArray2buffer(arrOut).toString("hex");
111
112 assert.equal(hash, hash2);
113 });
114
115});