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