arnaucube
arnaucube/keccak256-circomPublic Self-hosted repository
keccak256-circom/test/keccak.js
test/keccak.jsRaw
  1const path = require("path");
  2
  3const chai = require("chai");
  4const assert = chai.assert;
  5
  6const c_tester = require("circom_tester").c;
  7
  8const utils = require("./utils");
  9const keccak256 = require("keccak256");
 10
 11describe("Keccak full hash test", function () {
 12    this.timeout(100000);
 13
 14    let cir;
 15    before(async () => {
 16	cir = await c_tester(path.join(__dirname, "circuits", "keccak256_test.circom"));
 17	await cir.loadConstraints();
 18	console.log("n_constraints", cir.constraints.length);
 19    });
 20
 21    it ("Keccak 1 (testvector generated from go)", async () => {
 22	const input = [116, 101, 115, 116, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
 23	    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
 24	const expectedOut = [37, 17, 98, 135, 161, 178, 88, 97, 125, 150, 143,
 25	    65, 228, 211, 170, 133, 153, 9, 88, 212, 4, 212, 175, 238, 249,
 26	    210, 214, 116, 170, 85, 45, 21];
 27
 28	const inIn = utils.bytesToBits(input);
 29
 30	const witness = await cir.calculateWitness({ "in": inIn }, true);
 31
 32	const stateOut = witness.slice(1, 1+(32*8));
 33	const stateOutBytes = utils.bitsToBytes(stateOut);
 34	// console.log(stateOutBytes, expectedOut);
 35	assert.deepEqual(stateOutBytes, expectedOut);
 36    });
 37    it ("Keccak 2 (testvector generated from go)", async () => {
 38	const input = [37, 17, 98, 135, 161, 178, 88, 97, 125, 150, 143, 65,
 39	    228, 211, 170, 133, 153, 9, 88, 212, 4, 212, 175, 238, 249, 210,
 40	    214, 116, 170, 85, 45, 21];
 41	const expectedOut = [182, 104, 121, 2, 8, 48, 224, 11, 238, 244, 73,
 42	    142, 67, 205, 166, 27, 10, 223, 142, 209, 10, 46, 171, 110, 239,
 43	    68, 111, 116, 164, 127, 103, 141];
 44
 45	const inIn = utils.bytesToBits(input);
 46
 47	const witness = await cir.calculateWitness({ "in": inIn }, true);
 48
 49	const stateOut = witness.slice(1, 1+(32*8));
 50	const stateOutBytes = utils.bitsToBytes(stateOut);
 51	// console.log(stateOutBytes, expectedOut);
 52	assert.deepEqual(stateOutBytes, expectedOut);
 53    });
 54    it ("Keccak 3 (testvector generated from go)", async () => {
 55	const input = [182, 104, 121, 2, 8, 48, 224, 11, 238, 244, 73, 142, 67,
 56	    205, 166, 27, 10, 223, 142, 209, 10, 46, 171, 110, 239, 68, 111,
 57	    116, 164, 127, 103, 141];
 58	const expectedOut = [191, 235, 249, 254, 70, 24, 106, 244, 212, 163,
 59	    52, 240, 1, 128, 235, 61, 158, 52, 138, 60, 197, 80, 113, 36, 44,
 60	    217, 55, 211, 97, 231, 26, 7];
 61
 62	const inIn = utils.bytesToBits(input);
 63
 64	const witness = await cir.calculateWitness({ "in": inIn }, true);
 65
 66	const stateOut = witness.slice(1, 1+(32*8));
 67	const stateOutBytes = utils.bitsToBytes(stateOut);
 68	// console.log(stateOutBytes, expectedOut);
 69	assert.deepEqual(stateOutBytes, expectedOut);
 70    });
 71    it ("Keccak 4 (testvector generated from go)", async () => {
 72	const input = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
 73	    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
 74	const expectedOut = [41, 13, 236, 217, 84, 139, 98, 168, 214, 3, 69,
 75	    169, 136, 56, 111, 200, 75, 166, 188, 149, 72, 64, 8, 246, 54, 47,
 76	    147, 22, 14, 243, 229, 99];
 77
 78	const inIn = utils.bytesToBits(input);
 79
 80	const witness = await cir.calculateWitness({ "in": inIn }, true);
 81
 82	const stateOut = witness.slice(1, 1+(32*8));
 83	const stateOutBytes = utils.bitsToBytes(stateOut);
 84	// console.log(stateOutBytes, expectedOut);
 85	assert.deepEqual(stateOutBytes, expectedOut);
 86    });
 87
 88    describe("Keccak256 circuit check with js version", function () {
 89	this.timeout(100000);
 90
 91	it ("Keccak256 circom-js 1", async () => {
 92	    let input, inputBits, expectedOut, witness, stateOut, stateOutBytes;
 93	    input = Buffer.from("0000000000000000000000000000000000000000000000000000000000000000", "hex");
 94	    for(let i=0; i<10; i++) {
 95		inputBits = utils.bytesToBits(input);
 96
 97		let jsOutRaw = keccak256(input);
 98		expectedOut = utils.bufferToBytes(jsOutRaw);
 99		console.log(i, "in:", input.toString('hex'), "\n out:", jsOutRaw.toString('hex'));
100
101		witness = await cir.calculateWitness({ "in": inputBits }, true);
102		stateOut = witness.slice(1, 1+(32*8));
103		stateOutBytes = utils.bitsToBytes(stateOut);
104		assert.deepEqual(stateOutBytes, expectedOut);
105
106		// assign output into input for next iteration
107		input = jsOutRaw;
108	    }
109	});
110    });
111});