2018-12-06 19:32:49 +03:00
|
|
|
const chai = require("chai");
|
|
|
|
const path = require("path");
|
2023-07-21 13:10:01 +03:00
|
|
|
const snarkjs = require("@tornado/snarkjs");
|
2018-12-06 19:32:49 +03:00
|
|
|
const crypto = require("crypto");
|
|
|
|
|
|
|
|
const compiler = require("circom");
|
|
|
|
|
|
|
|
const assert = chai.assert;
|
|
|
|
|
|
|
|
const sha256 = require("./helpers/sha256");
|
|
|
|
|
|
|
|
// const printSignal = require("./helpers/printsignal");
|
|
|
|
|
2019-09-05 17:41:58 +03:00
|
|
|
function buffer2bitArray(b) {
|
|
|
|
const res = [];
|
2023-07-21 13:10:01 +03:00
|
|
|
for (let i = 0; i < b.length; i++) {
|
|
|
|
for (let j = 0; j < 8; j++) {
|
|
|
|
res.push((b[i] >> (7 - j)) & 1);
|
2019-09-05 17:41:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
function bitArray2buffer(a) {
|
2023-07-21 13:10:01 +03:00
|
|
|
const len = Math.floor((a.length - 1) / 8) + 1;
|
2019-09-05 17:41:58 +03:00
|
|
|
const b = new Buffer.alloc(len);
|
|
|
|
|
2023-07-21 13:10:01 +03:00
|
|
|
for (let i = 0; i < a.length; i++) {
|
|
|
|
const p = Math.floor(i / 8);
|
|
|
|
b[p] = b[p] | (Number(a[i]) << (7 - (i % 8)));
|
2019-09-05 17:41:58 +03:00
|
|
|
}
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
2018-12-06 19:32:49 +03:00
|
|
|
describe("SHA256 test", () => {
|
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);
|
2023-07-21 13:10:01 +03:00
|
|
|
for (let i = 0; i < 64; i++) {
|
|
|
|
b[i] = i + 1;
|
2019-09-05 17:41:58 +03:00
|
|
|
}
|
|
|
|
const a = buffer2bitArray(b);
|
|
|
|
const b2 = bitArray2buffer(a);
|
|
|
|
|
|
|
|
assert.equal(b.toString("hex"), b2.toString("hex"));
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Should calculate a hash of 1 compressor", async () => {
|
2018-12-06 19:32:49 +03:00
|
|
|
const cirDef = await compiler(path.join(__dirname, "circuits", "sha256_2_test.circom"));
|
|
|
|
const circuit = new snarkjs.Circuit(cirDef);
|
|
|
|
|
2023-07-21 13:10:01 +03:00
|
|
|
console.log("Vars: " + circuit.nVars);
|
|
|
|
console.log("Constraints: " + circuit.nConstraints);
|
2018-12-06 19:32:49 +03:00
|
|
|
|
2023-07-21 13:10:01 +03:00
|
|
|
const witness = circuit.calculateWitness({ a: "1", b: "2" });
|
2018-12-06 19:32:49 +03:00
|
|
|
|
|
|
|
const b = new Buffer.alloc(54);
|
|
|
|
b[26] = 1;
|
|
|
|
b[53] = 2;
|
|
|
|
|
2023-07-21 13:10:01 +03:00
|
|
|
const hash = crypto.createHash("sha256").update(b).digest("hex");
|
2018-12-06 19:32:49 +03:00
|
|
|
const r = "0x" + hash.slice(10);
|
|
|
|
|
2023-07-21 13:10:01 +03:00
|
|
|
const hash2 = sha256.hash(b.toString("hex"), { msgFormat: "hex-bytes" });
|
2018-12-06 19:32:49 +03:00
|
|
|
|
|
|
|
assert.equal(hash, hash2);
|
|
|
|
|
|
|
|
assert(witness[1].equals(snarkjs.bigInt(r)));
|
|
|
|
}).timeout(1000000);
|
|
|
|
|
2019-09-05 17:41:58 +03:00
|
|
|
it("Should calculate a hash of 2 compressor", async () => {
|
2023-07-21 13:10:01 +03:00
|
|
|
const cirDef = await compiler(path.join(__dirname, "circuits", "sha256_test512.circom"), { reduceConstraints: false });
|
2019-09-05 17:41:58 +03:00
|
|
|
const circuit = new snarkjs.Circuit(cirDef);
|
|
|
|
|
2023-07-21 13:10:01 +03:00
|
|
|
console.log("Vars: " + circuit.nVars);
|
|
|
|
console.log("Constraints: " + circuit.nConstraints);
|
2019-09-05 17:41:58 +03:00
|
|
|
|
2023-07-21 13:10:01 +03:00
|
|
|
/*
|
2019-09-05 17:41:58 +03:00
|
|
|
const testStr = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
|
|
|
|
|
|
|
|
const b = Buffer.from(testStr, 'utf8');
|
|
|
|
*/
|
|
|
|
const b = new Buffer.alloc(64);
|
2023-07-21 13:10:01 +03:00
|
|
|
for (let i = 0; i < 64; i++) {
|
|
|
|
b[i] = i + 1;
|
2019-09-05 17:41:58 +03:00
|
|
|
}
|
|
|
|
|
2023-07-21 13:10:01 +03:00
|
|
|
const hash = crypto.createHash("sha256").update(b).digest("hex");
|
2019-09-05 17:41:58 +03:00
|
|
|
|
|
|
|
const arrIn = buffer2bitArray(b);
|
2023-07-21 13:10:01 +03:00
|
|
|
const witness = circuit.calculateWitness({ in: arrIn } /*, {logOutput: 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);
|
|
|
|
|
|
|
|
it("Should calculate a hash of 2 compressor", async () => {
|
2023-07-21 13:10:01 +03:00
|
|
|
const cirDef = await compiler(path.join(__dirname, "circuits", "sha256_test448.circom"), { reduceConstraints: false });
|
2019-09-05 17:41:58 +03:00
|
|
|
const circuit = new snarkjs.Circuit(cirDef);
|
|
|
|
|
2023-07-21 13:10:01 +03:00
|
|
|
console.log("Vars: " + circuit.nVars);
|
|
|
|
console.log("Constraints: " + circuit.nConstraints);
|
2019-09-05 17:41:58 +03:00
|
|
|
|
|
|
|
const testStr = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
|
2018-12-06 19:32:49 +03:00
|
|
|
|
2023-07-21 13:10:01 +03:00
|
|
|
const b = Buffer.from(testStr, "utf8");
|
|
|
|
for (let i = 0; i < 64; i++) {
|
|
|
|
b[i] = i + 1;
|
2019-09-05 17:41:58 +03:00
|
|
|
}
|
|
|
|
|
2023-07-21 13:10:01 +03:00
|
|
|
const hash = crypto.createHash("sha256").update(b).digest("hex");
|
2019-09-05 17:41:58 +03:00
|
|
|
|
|
|
|
const arrIn = buffer2bitArray(b);
|
2023-07-21 13:10:01 +03:00
|
|
|
const witness = circuit.calculateWitness({ in: arrIn } /*, {logOutput: 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);
|
2018-12-06 19:32:49 +03:00
|
|
|
});
|