2018-12-06 19:32:49 +03:00
|
|
|
const chai = require("chai");
|
|
|
|
const path = require("path");
|
|
|
|
const crypto = require("crypto");
|
2020-04-18 23:33:59 +03:00
|
|
|
const Fr = require("ffjavascript").bn128.Fr;
|
2018-12-06 19:32:49 +03:00
|
|
|
|
|
|
|
const assert = chai.assert;
|
|
|
|
|
|
|
|
const sha256 = require("./helpers/sha256");
|
|
|
|
|
2019-12-12 18:49:58 +03:00
|
|
|
const tester = require("circom").tester;
|
2019-12-11 23:41:15 +03:00
|
|
|
|
2018-12-06 19:32:49 +03:00
|
|
|
// const printSignal = require("./helpers/printsignal");
|
|
|
|
|
|
|
|
|
2019-09-05 17:41:58 +03:00
|
|
|
function buffer2bitArray(b) {
|
|
|
|
const res = [];
|
|
|
|
for (let i=0; i<b.length; i++) {
|
|
|
|
for (let j=0; j<8; j++) {
|
|
|
|
res.push((b[i] >> (7-j) &1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
function bitArray2buffer(a) {
|
|
|
|
const len = Math.floor((a.length -1 )/8)+1;
|
|
|
|
const b = new Buffer.alloc(len);
|
|
|
|
|
|
|
|
for (let i=0; i<a.length; i++) {
|
|
|
|
const p = Math.floor(i/8);
|
|
|
|
b[p] = b[p] | (Number(a[i]) << ( 7 - (i%8) ));
|
|
|
|
}
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-11 23:41:15 +03:00
|
|
|
describe("SHA256 test", function () {
|
|
|
|
this.timeout(100000);
|
2019-09-05 17:41:58 +03:00
|
|
|
|
2019-12-12 15:04:02 +03:00
|
|
|
|
2019-09-05 17:41:58 +03:00
|
|
|
it("Should work bits to array and array to bits", async () => {
|
|
|
|
const b = new Buffer.alloc(64);
|
|
|
|
for (let i=0; i<64; i++) {
|
|
|
|
b[i] = i+1;
|
|
|
|
}
|
|
|
|
const a = buffer2bitArray(b);
|
|
|
|
const b2 = bitArray2buffer(a);
|
|
|
|
|
2020-03-26 21:24:20 +03:00
|
|
|
assert.equal(b.toString("hex"), b2.toString("hex"), true);
|
2019-09-05 17:41:58 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it("Should calculate a hash of 1 compressor", async () => {
|
2019-12-12 18:49:58 +03:00
|
|
|
const cir = await tester(path.join(__dirname, "circuits", "sha256_2_test.circom"));
|
2018-12-06 19:32:49 +03:00
|
|
|
|
2020-03-26 21:24:20 +03:00
|
|
|
const witness = await cir.calculateWitness({ "a": "1", "b": "2" }, true);
|
2018-12-06 19:32:49 +03:00
|
|
|
|
|
|
|
const b = new Buffer.alloc(54);
|
|
|
|
b[26] = 1;
|
|
|
|
b[53] = 2;
|
|
|
|
|
|
|
|
const hash = crypto.createHash("sha256")
|
|
|
|
.update(b)
|
|
|
|
.digest("hex");
|
|
|
|
const r = "0x" + hash.slice(10);
|
|
|
|
|
|
|
|
const hash2 = sha256.hash(b.toString("hex"), {msgFormat: "hex-bytes"});
|
|
|
|
|
|
|
|
assert.equal(hash, hash2);
|
|
|
|
|
2020-04-18 23:33:59 +03:00
|
|
|
assert(Fr.eq(witness[1], Fr.e(r)));
|
2018-12-06 19:32:49 +03:00
|
|
|
}).timeout(1000000);
|
|
|
|
|
2019-09-05 17:41:58 +03:00
|
|
|
it("Should calculate a hash of 2 compressor", async () => {
|
2019-12-12 18:49:58 +03:00
|
|
|
const cir = await tester(path.join(__dirname, "circuits", "sha256_test512.circom"));
|
2019-09-05 17:41:58 +03:00
|
|
|
|
|
|
|
const b = new Buffer.alloc(64);
|
|
|
|
for (let i=0; i<64; i++) {
|
|
|
|
b[i] = i+1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const hash = crypto.createHash("sha256")
|
|
|
|
.update(b)
|
|
|
|
.digest("hex");
|
|
|
|
|
|
|
|
const arrIn = buffer2bitArray(b);
|
2020-03-26 21:24:20 +03:00
|
|
|
const witness = await cir.calculateWitness({ "in": arrIn }, true);
|
2019-09-05 17:41:58 +03:00
|
|
|
|
|
|
|
const arrOut = witness.slice(1, 257);
|
|
|
|
const hash2 = bitArray2buffer(arrOut).toString("hex");
|
|
|
|
|
|
|
|
assert.equal(hash, hash2);
|
|
|
|
|
|
|
|
}).timeout(1000000);
|
2019-12-11 23:41:15 +03:00
|
|
|
it ("Should calculate a hash of 2 compressor", async () => {
|
2019-12-12 18:49:58 +03:00
|
|
|
const cir = await tester(path.join(__dirname, "circuits", "sha256_test448.circom"));
|
2019-12-11 23:41:15 +03:00
|
|
|
|
|
|
|
const testStr = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
|
|
|
|
|
|
|
|
const b = Buffer.from(testStr, "utf8");
|
|
|
|
|
|
|
|
const hash = crypto.createHash("sha256")
|
|
|
|
.update(b)
|
|
|
|
.digest("hex");
|
|
|
|
|
|
|
|
const arrIn = buffer2bitArray(b);
|
|
|
|
|
2020-03-26 21:24:20 +03:00
|
|
|
const witness = await cir.calculateWitness({ "in": arrIn }, true);
|
2019-12-11 23:41:15 +03:00
|
|
|
|
|
|
|
const arrOut = witness.slice(1, 257);
|
|
|
|
const hash2 = bitArray2buffer(arrOut).toString("hex");
|
|
|
|
|
|
|
|
assert.equal(hash, hash2);
|
|
|
|
});
|
|
|
|
|
2018-12-06 19:32:49 +03:00
|
|
|
});
|