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 assert = chai.assert;
  7
  8const sha256 = require("./helpers/sha256");
  9
 10const c_tester = require("circom").c_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"));
 50    });
 51
 52    it("Should calculate a hash of 1 compressor", async () => {
 53        const cirDef = await compiler(path.join(__dirname, "circuits", "sha256_2_test.circom"));
 54        const circuit = new snarkjs.Circuit(cirDef);
 55
 56        console.log("Vars: "+circuit.nVars);
 57        console.log("Constraints: "+circuit.nConstraints);
 58
 59        const witness = circuit.calculateWitness({ "a": "1", "b": "2" });
 60
 61        const b = new Buffer.alloc(54);
 62        b[26] = 1;
 63        b[53] = 2;
 64
 65        const hash = crypto.createHash("sha256")
 66            .update(b)
 67            .digest("hex");
 68        const r = "0x" + hash.slice(10);
 69
 70        const hash2 = sha256.hash(b.toString("hex"), {msgFormat: "hex-bytes"});
 71
 72        assert.equal(hash, hash2);
 73
 74        assert(witness[1].equals(snarkjs.bigInt(r)));
 75    }).timeout(1000000);
 76
 77    it("Should calculate a hash of 2 compressor", async () => {
 78        const cirDef = await compiler(path.join(__dirname, "circuits", "sha256_test512.circom"),  {reduceConstraints:false}  );
 79        const circuit = new snarkjs.Circuit(cirDef);
 80
 81        console.log("Vars: "+circuit.nVars);
 82        console.log("Constraints: "+circuit.nConstraints);
 83
 84        // const testStr = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
 85
 86        // const b = Buffer.from(testStr, 'utf8');
 87        const b = new Buffer.alloc(64);
 88        for (let i=0; i<64; i++) {
 89            b[i] = i+1;
 90        }
 91
 92        const hash = crypto.createHash("sha256")
 93            .update(b)
 94            .digest("hex");
 95
 96        const arrIn = buffer2bitArray(b);
 97        const witness = circuit.calculateWitness({ "in": arrIn } , {logOutput: false} );
 98
 99        const arrOut = witness.slice(1, 257);
100        const hash2 = bitArray2buffer(arrOut).toString("hex");
101
102        assert.equal(hash, hash2);
103
104    }).timeout(1000000);
105
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
115        const testStr = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
116
117        const b = Buffer.from(testStr, 'utf8');
118        //  for (let i=0; i<64; i++) {
119        //    b[i] = i+1;
120        //  }
121
122        const hash = crypto.createHash("sha256")
123            .update(b)
124            .digest("hex");
125
126        const arrIn = buffer2bitArray(b);
127        const witness = circuit.calculateWitness({ "in": arrIn }, {logOutput: false});
128
129        const arrOut = witness.slice(1, 257);
130        const hash2 = bitArray2buffer(arrOut).toString("hex");
131
132        assert.equal(hash, hash2);
133
134    }).timeout(1000000);
135*/
136    it ("Should calculate a hash of 2 compressor", async () => {
137        const cir = await c_tester(path.join(__dirname, "circuits", "sha256_test448.circom"));
138
139        const testStr = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
140
141        const b = Buffer.from(testStr, "utf8");
142
143        const hash = crypto.createHash("sha256")
144            .update(b)
145            .digest("hex");
146
147        const arrIn = buffer2bitArray(b);
148
149        console.log(JSON.stringify({ "in": arrIn }));
150        const witness = await cir.calculateWitness({ "in": arrIn });
151
152        const arrOut = witness.slice(1, 257);
153        const hash2 = bitArray2buffer(arrOut).toString("hex");
154
155        assert.equal(hash, hash2);
156    });
157
158});