arnaucube
arnaucube/circomlibPublic Self-hosted repository
circomlib/test/sha256.js
test/sha256.jsRaw
  1const chai = require("chai");
  2const path = require("path");
  3const snarkjs = require("snarkjs");
  4const crypto = require("crypto");
  5
  6const compiler = require("circom");
  7
  8const assert = chai.assert;
  9
 10const sha256 = require("./helpers/sha256");
 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", () => {
 38
 39
 40    it("Should work bits to array and array to bits", async () => {
 41        const b = new Buffer.alloc(64);
 42        for (let i=0; i<64; i++) {
 43            b[i] = i+1;
 44        }
 45        const a = buffer2bitArray(b);
 46        const b2 = bitArray2buffer(a);
 47
 48        assert.equal(b.toString("hex"), b2.toString("hex"));
 49    });
 50
 51    it("Should calculate a hash of 1 compressor", async () => {
 52        const cirDef = await compiler(path.join(__dirname, "circuits", "sha256_2_test.circom"));
 53        const circuit = new snarkjs.Circuit(cirDef);
 54
 55        console.log("Vars: "+circuit.nVars);
 56        console.log("Constraints: "+circuit.nConstraints);
 57
 58        const witness = circuit.calculateWitness({ "a": "1", "b": "2" });
 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(witness[1].equals(snarkjs.bigInt(r)));
 74    }).timeout(1000000);
 75
 76    it("Should calculate a hash of 2 compressor", async () => {
 77        const cirDef = await compiler(path.join(__dirname, "circuits", "sha256_test512.circom"),  {reduceConstraints:false}  );
 78        const circuit = new snarkjs.Circuit(cirDef);
 79
 80        console.log("Vars: "+circuit.nVars);
 81        console.log("Constraints: "+circuit.nConstraints);
 82
 83
 84        // const testStr = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
 85
 86        // const b = Buffer.from(testStr, 'utf8');
 87
 88        const b = new Buffer.alloc(64);
 89        for (let i=0; i<64; i++) {
 90            b[i] = i+1;
 91        }
 92
 93        const hash = crypto.createHash("sha256")
 94            .update(b)
 95            .digest("hex");
 96
 97        const arrIn = buffer2bitArray(b);
 98        const witness = circuit.calculateWitness({ "in": arrIn }, {logOutput: false});
 99
100        const arrOut = witness.slice(1, 257);
101        const hash2 = bitArray2buffer(arrOut).toString("hex");
102
103        assert.equal(hash, hash2);
104
105    }).timeout(1000000);
106
107    it("Should calculate a hash of 2 compressor", async () => {
108        const cirDef = await compiler(path.join(__dirname, "circuits", "sha256_test448.circom"),  {reduceConstraints:false}  );
109        const circuit = new snarkjs.Circuit(cirDef);
110
111        console.log("Vars: "+circuit.nVars);
112        console.log("Constraints: "+circuit.nConstraints);
113
114        const testStr = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
115
116        const b = Buffer.from(testStr, "utf8");
117        // for (let i=0; i<64; i++) {
118        //    b[i] = i+1;
119        // }
120
121        const hash = crypto.createHash("sha256")
122            .update(b)
123            .digest("hex");
124
125        const arrIn = buffer2bitArray(b);
126        const witness = circuit.calculateWitness({ "in": arrIn } , {logOutput: false});
127
128        const arrOut = witness.slice(1, 257);
129        const hash2 = bitArray2buffer(arrOut).toString("hex");
130
131        assert.equal(hash, hash2);
132
133    }).timeout(1000000);
134});