keccak256-circom/test/keccak-intermediate.js
test/keccak-intermediate.jsRaw
1const path = require("path");
2
3const chai = require("chai");
4const assert = chai.assert;
5
6const wasm_tester = require("circom_tester").wasm;
7const c_tester = require("circom_tester").c;
8
9const utils = require("./utils");
10
11describe("Keccak-Pad test", function () {
12 this.timeout(100000);
13
14 it ("Pad (testvector generated from go)", async () => {
15 const cir = await wasm_tester(path.join(__dirname, "circuits", "pad_test.circom"));
16
17 const input =
18 [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31];
19 const expectedOut = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
20 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
21 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
22 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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, 0, 0, 0, 0, 0, 0,
24 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
25 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128];
26
27 const stateIn = utils.bytesToBits(input);
28
29 const witness = await cir.calculateWitness({ "in": stateIn }, true);
30
31 const stateOut = witness.slice(1, 1+(136*8));
32 const stateOutBytes = utils.bitsToBytes(stateOut);
33 // console.log(stateOutBytes, expectedOut);
34 assert.deepEqual(stateOutBytes, expectedOut);
35 });
36});
37
38describe("absorb test", function () {
39 this.timeout(100000);
40
41 let cir;
42 before(async () => {
43 // const cir = await wasm_tester(path.join(__dirname, "circuits", "keccakf_test.circom"));
44 cir = await c_tester(path.join(__dirname, "circuits", "absorb_test.circom"));
45 await cir.loadConstraints();
46 console.log("n_constraints", cir.constraints.length);
47 });
48
49 it ("absorb 1 (testvector generated from go)", async () => {
50 const s = utils.strsToBigInts(["0", "1", "2", "3", "4", "5", "6", "7",
51 "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18",
52 "19", "20", "21", "22", "23", "24"]);
53
54 const block = utils.strsToBigInts(["0", "1", "2", "3", "4", "5", "6",
55 "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17",
56 "18", "19", "20", "21", "22", "23", "24", "25", "26", "27",
57 "28","29", "30", "31", "1", "0", "0", "0", "0", "0", "0", "0", "0",
58 "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
59 "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
60 "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
61 "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
62 "0","0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
63 "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
64 "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
65 "0", "0", "0", "128"]);
66
67 const expectedOut = utils.strsToBigInts(["8342348566319207042",
68 "319359607942176202", "14410076088654599075",
69 "15666111399434436772", "9558421567405313402",
70 "3396178318116504023", "794353847439963108",
71 "12717011319735989377", "3503398863218919239",
72 "5517201702366862678", "15999361614129160496",
73 "1325524015888689985", "11971708408118944333",
74 "14874486179441062217", "12554876384974234666",
75 "11129975558302206043", "11257826431949606534",
76 "2740710607956478714", "15000019752453010167",
77 "15593606854132419294",
78 "2598425978562809333","8872504799797239246", "1212062965004664308",
79 "5443427421087086722", "10946808592826700411"]);
80
81 const sIn = utils.u64ArrayToBits(s);
82 const blockIn = utils.bytesToBits(block);
83 const expectedOutBits = utils.u64ArrayToBits(expectedOut);
84
85 const witness = await cir.calculateWitness({ "s": sIn, "block": blockIn }, true);
86
87 const stateOut = witness.slice(1, 1+(25*64));
88 const stateOutU64 = utils.bitsToU64Array(stateOut);
89 // console.log(stateOutU64, expectedOut);
90 assert.deepEqual(stateOutU64, expectedOut);
91 });
92
93 it ("absorb 2 (testvector generated from go)", async () => {
94 const s = utils.strsToBigInts(["8342348566319207042",
95 "319359607942176202", "14410076088654599075",
96 "15666111399434436772", "9558421567405313402",
97 "3396178318116504023", "794353847439963108",
98 "12717011319735989377", "3503398863218919239",
99 "5517201702366862678", "15999361614129160496",
100 "1325524015888689985", "11971708408118944333",
101 "14874486179441062217", "12554876384974234666",
102 "11129975558302206043", "11257826431949606534",
103 "2740710607956478714", "15000019752453010167",
104 "15593606854132419294",
105 "2598425978562809333","8872504799797239246", "1212062965004664308",
106 "5443427421087086722", "10946808592826700411"]);
107
108 const block = utils.strsToBigInts(["0", "1", "2", "3", "4", "5", "6",
109 "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17",
110 "18", "19", "20", "21", "22", "23", "24", "25", "26", "27",
111 "28","29", "30", "31", "1", "0", "0", "0", "0", "0", "0", "0", "0",
112 "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
113 "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
114 "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
115 "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
116 "0","0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
117 "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
118 "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
119 "0", "0", "0", "128"]);
120
121 const expectedOut = utils.strsToBigInts(["8909243822027471379",
122 "1111840847970088140", "12093072708540612559",
123 "11255033638786021658", "2082116894939842214",
124 "12821085060245261575", "6901785969834988344",
125 "3182430130277914993", "2164708585929408975",
126 "14402143231999718904", "16231444410553803968",
127 "1850945423480060493", "12856855675247400303",
128 "1137248620532111171", "7389129221921446308",
129 "12932467982741614601", "1350606937385760406",
130 "10983682292859713641", "10305595434820307765",
131 "13958651111365489854", "17206620388135196198",
132 "4238113785249530092", "7230868147643218103", "603011106238724524",
133 "16480095441097880488"]);
134
135 const sIn = utils.u64ArrayToBits(s);
136 const blockIn = utils.bytesToBits(block);
137 const expectedOutBits = utils.u64ArrayToBits(expectedOut);
138
139 const witness = await cir.calculateWitness({ "s": sIn, "block": blockIn }, true);
140
141 const stateOut = witness.slice(1, 1+(25*64));
142 const stateOutU64 = utils.bitsToU64Array(stateOut);
143 // console.log(stateOutU64, expectedOut);
144 assert.deepEqual(stateOutU64, expectedOut);
145 });
146});
147
148describe("Keccak-Final test", function () {
149 this.timeout(100000);
150
151 let cir;
152 before(async () => {
153 cir = await c_tester(path.join(__dirname, "circuits", "final_test.circom"));
154 await cir.loadConstraints();
155 console.log("n_constraints", cir.constraints.length);
156 });
157
158 it ("Final 1 (testvector generated from go)", async () => {
159 const input =
160 [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31];
161 const expectedOut = utils.strsToBigInts(["16953415415620100490",
162 "7495738965189503699", "12723370805759944158",
163 "3295955328722933810", "12121371508560456016",
164 "174876831679863147", "15944933357501475584",
165 "7502339663607726274", "12048918224562833898",
166 "16715284461100269102", "15582559130083209842",
167 "1743886467337678829", "2424196198791253761",
168 "1116417308245482383", "10367365997906434042",
169 "1849801549382613906", "13294939539683415102",
170 "4478091053375708790", "2969967870313332958",
171 "14618962068930014237", "2721742233407503451",
172 "12003265593030191290", "8109318293656735684",
173 "6346795302983965746", "12210038122000333046"]);
174
175 const inIn = utils.bytesToBits(input);
176 const expectedOutBits = utils.u64ArrayToBits(expectedOut);
177
178 const witness = await cir.calculateWitness({ "in": inIn }, true);
179
180 const stateOut = witness.slice(1, 1+(25*64));
181 const stateOutU64 = utils.bitsToU64Array(stateOut);
182 // console.log(stateOutU64, expectedOut);
183 assert.deepEqual(stateOutU64, expectedOut);
184 });
185
186 it ("Final 2 (testvector generated from go)", async () => {
187 const input = utils.strsToBigInts(["254", "254", "254", "254", "254",
188 "254", "254", "254", "254", "254", "254", "254", "254", "254",
189 "254", "254", "254", "254", "254", "254", "254", "254", "254",
190 "254", "254", "254", "254", "254", "254", "254", "254", "254"]);
191 const expectedOut = utils.strsToBigInts(["16852464862333879129",
192 "9588646233186836430", "693207875935078627", "6545910230963382296",
193 "3599194178366828471", "13130606490077331384",
194 "10374798023615518933", "7285576075118720444",
195 "4097382401500492461", "3968685317688314807",
196 "3350659309646210303", "640023485234837464", "2550030127986774041",
197 "8948768022010378840", "10678227883444996205",
198 "1395278318096830339", "2744077813166753978",
199 "13362598477502046010", "14601579319881128511",
200 "4070707967569603186", "16833768365875755098",
201 "1486295134719870048", "9161068934282437999",
202 "8245604251371175619", "8421994351908003183"]);
203
204 const inIn = utils.bytesToBits(input);
205 const expectedOutBits = utils.u64ArrayToBits(expectedOut);
206
207 const witness = await cir.calculateWitness({ "in": inIn }, true);
208
209 const stateOut = witness.slice(1, 1+(25*64));
210 const stateOutU64 = utils.bitsToU64Array(stateOut);
211 // console.log(stateOutU64, expectedOut);
212 assert.deepEqual(stateOutU64, expectedOut);
213 });
214});
215
216describe("Keccak-Squeeze test", function () {
217 this.timeout(100000);
218
219 let cir;
220 before(async () => {
221 cir = await c_tester(path.join(__dirname, "circuits", "squeeze_test.circom"));
222 await cir.loadConstraints();
223 console.log("n_constraints", cir.constraints.length);
224 });
225
226 it ("Squeeze 1 (testvector generated from go)", async () => {
227 const input = utils.strsToBigInts(["16852464862333879129",
228 "9588646233186836430", "693207875935078627", "6545910230963382296",
229 "3599194178366828471", "13130606490077331384",
230 "10374798023615518933", "7285576075118720444",
231 "4097382401500492461", "3968685317688314807",
232 "3350659309646210303", "640023485234837464", "2550030127986774041",
233 "8948768022010378840", "10678227883444996205",
234 "1395278318096830339", "2744077813166753978",
235 "13362598477502046010", "14601579319881128511",
236 "4070707967569603186", "16833768365875755098",
237 "1486295134719870048", "9161068934282437999",
238 "8245604251371175619", "8421994351908003183"]);
239
240 const expectedOut = [89, 195, 41, 13, 129, 251, 223, 233, 206, 31, 253,
241 61, 242, 182, 17, 133, 227, 8, 157, 240, 227, 196, 158, 9, 24, 232,
242 42, 96, 172, 190, 215, 90];
243
244 const inIn = utils.u64ArrayToBits(input);
245 const expectedOutBits = utils.bytesToBits(expectedOut);
246
247 const witness = await cir.calculateWitness({ "s": inIn }, true);
248
249 const stateOut = witness.slice(1, 1+(32*8));
250 const stateOutBytes = utils.bitsToBytes(stateOut);
251 // console.log(stateOutBytes, expectedOut);
252 assert.deepEqual(stateOutBytes, expectedOut);
253 });
254 it ("Squeeze 2 (testvector generated from go)", async () => {
255 const input = utils.strsToBigInts(["16953415415620100490",
256 "7495738965189503699", "12723370805759944158",
257 "3295955328722933810", "12121371508560456016",
258 "174876831679863147", "15944933357501475584",
259 "7502339663607726274", "12048918224562833898",
260 "16715284461100269102", "15582559130083209842",
261 "1743886467337678829", "2424196198791253761",
262 "1116417308245482383", "10367365997906434042",
263 "1849801549382613906", "13294939539683415102",
264 "4478091053375708790", "2969967870313332958",
265 "14618962068930014237", "2721742233407503451",
266 "12003265593030191290", "8109318293656735684",
267 "6346795302983965746", "12210038122000333046"]);
268
269 const expectedOut = [138, 225, 170, 89, 127, 161, 70, 235, 211, 170,
270 44, 237, 223, 54, 6, 104, 222, 165, 229, 38, 86, 126, 146, 176, 50,
271 24, 22, 164, 232, 149, 189, 45];
272
273 const inIn = utils.u64ArrayToBits(input);
274 const expectedOutBits = utils.bytesToBits(expectedOut);
275
276 const witness = await cir.calculateWitness({ "s": inIn }, true);
277
278 const stateOut = witness.slice(1, 1+(32*8));
279 const stateOutBytes = utils.bitsToBytes(stateOut);
280 // console.log(stateOutBytes, expectedOut);
281 assert.deepEqual(stateOutBytes, expectedOut);
282 });
283});