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 32bytes full hash test", function () {
12 this.timeout(100000);
13
14 let cir;
15 before(async () => {
16 cir = await c_tester(path.join(__dirname, "circuits", "keccak_256_256_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});
112
113describe("Keccak input: 4bytes, output: 32bytes, full hash test", function () {
114 this.timeout(100000);
115
116 let cir;
117 before(async () => {
118 cir = await c_tester(path.join(__dirname, "circuits", "keccak_32_256_test.circom"));
119 await cir.loadConstraints();
120 console.log("n_constraints", cir.constraints.length);
121 });
122
123 it ("Keccak inputSize==32bits: 1 (testvector generated from go)", async () => {
124 const input = [116, 101, 115, 116];
125 const expectedOut = [156, 34, 255, 95, 33, 240, 184, 27, 17, 62, 99,
126 247, 219, 109, 169, 79, 237, 239, 17, 178, 17, 155, 64, 136, 184,
127 150, 100, 251, 154, 60, 182, 88];
128
129 const inIn = utils.bytesToBits(input);
130
131 const witness = await cir.calculateWitness({ "in": inIn }, true);
132
133 const stateOut = witness.slice(1, 1+(32*8));
134 const stateOutBytes = utils.bitsToBytes(stateOut);
135 // console.log(stateOutBytes, expectedOut);
136 assert.deepEqual(stateOutBytes, expectedOut);
137 });
138
139 it ("Keccak256 inputSize==32bits, circom-js 1", async () => {
140 let input, inputBits, expectedOut, witness, stateOut, stateOutBytes;
141 input = Buffer.from("test");
142 for(let i=0; i<10; i++) {
143 inputBits = utils.bytesToBits(input);
144
145 let jsOutRaw = keccak256(input);
146 expectedOut = utils.bufferToBytes(jsOutRaw);
147 console.log(i, "in:", input.toString('hex'), "\n out:", jsOutRaw.toString('hex'));
148
149 witness = await cir.calculateWitness({ "in": inputBits }, true);
150 stateOut = witness.slice(1, 1+(32*8));
151 stateOutBytes = utils.bitsToBytes(stateOut);
152 assert.deepEqual(stateOutBytes, expectedOut);
153
154 // assign output[0:4] into input for next iteration
155 input = jsOutRaw.slice(0, 4);
156 }
157 });
158});