zkjson 0.7.1 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,445 @@
1
+ export default function buildPoseidonWasm(module) {
2
+ const F = new ffjavascript.F1Field(
3
+ ffjavascript.Scalar.e(
4
+ "21888242871839275222246405745257275088548364400416034343698204186575808495617",
5
+ ),
6
+ )
7
+ const N_ROUNDS_P = [
8
+ 56, 57, 56, 60, 60, 63, 64, 63, 60, 66, 60, 65, 70, 60, 64, 68,
9
+ ]
10
+ const NSizes = poseidonConstants$1.C.length
11
+ const buffIdx = new Uint8Array(NSizes * 5 * 4)
12
+ const buffIdx32 = new Uint32Array(buffIdx.buffer)
13
+ for (let i = 0; i < NSizes; i++) {
14
+ buffIdx32[i * 5] = N_ROUNDS_P[i]
15
+ const buffC = new Uint8Array(32 * poseidonConstants$1.C[i].length)
16
+ for (let j = 0; j < poseidonConstants$1.C[i].length; j++) {
17
+ F.toRprLEM(buffC, j * 32, F.e(poseidonConstants$1.C[i][j]))
18
+ }
19
+ buffIdx32[i * 5 + 1] = module.alloc(buffC)
20
+
21
+ const buffS = new Uint8Array(32 * poseidonConstants$1.S[i].length)
22
+ for (let j = 0; j < poseidonConstants$1.S[i].length; j++) {
23
+ F.toRprLEM(buffS, j * 32, F.e(poseidonConstants$1.S[i][j]))
24
+ }
25
+ buffIdx32[i * 5 + 2] = module.alloc(buffS)
26
+
27
+ const N = poseidonConstants$1.M[i].length
28
+ const buffM = new Uint8Array(32 * N * N)
29
+ for (let j = 0; j < N; j++) {
30
+ for (let k = 0; k < N; k++) {
31
+ F.toRprLEM(buffM, (j * N + k) * 32, F.e(poseidonConstants$1.M[i][k][j]))
32
+ }
33
+ }
34
+ buffIdx32[i * 5 + 3] = module.alloc(buffM)
35
+
36
+ const buffP = new Uint8Array(32 * N * N)
37
+ for (let j = 0; j < N; j++) {
38
+ for (let k = 0; k < N; k++) {
39
+ F.toRprLEM(buffP, (j * N + k) * 32, F.e(poseidonConstants$1.P[i][k][j]))
40
+ }
41
+ }
42
+ buffIdx32[i * 5 + 4] = module.alloc(buffP)
43
+ }
44
+
45
+ const pConstants = module.alloc(buffIdx)
46
+ const pState = module.alloc(32 * ((NSizes + 1) * 32))
47
+
48
+ function buildAddConstant() {
49
+ const f = module.addFunction("poseidon_addConstant")
50
+ f.addParam("t", "i32")
51
+ f.addParam("pC", "i32")
52
+ f.setReturnType("i32")
53
+ f.addLocal("i", "i32")
54
+ f.addLocal("pState", "i32")
55
+
56
+ const c = f.getCodeBuilder()
57
+
58
+ f.addCode(
59
+ c.setLocal("pState", c.i32_const(pState)),
60
+ c.setLocal("i", c.i32_const(0)),
61
+ c.block(
62
+ c.loop(
63
+ c.br_if(1, c.i32_eq(c.getLocal("i"), c.getLocal("t"))),
64
+ c.call(
65
+ "frm_add",
66
+ c.getLocal("pC"),
67
+ c.getLocal("pState"),
68
+ c.getLocal("pState"),
69
+ ),
70
+ c.setLocal("pC", c.i32_add(c.getLocal("pC"), c.i32_const(32))),
71
+ c.setLocal(
72
+ "pState",
73
+ c.i32_add(c.getLocal("pState"), c.i32_const(32)),
74
+ ),
75
+ c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))),
76
+ c.br(0),
77
+ ),
78
+ ),
79
+ c.ret(c.getLocal("pC")),
80
+ )
81
+ }
82
+
83
+ function buildPower5() {
84
+ const f = module.addFunction("poseidon_power5")
85
+ f.addParam("p", "i32")
86
+
87
+ const c = f.getCodeBuilder()
88
+
89
+ const AUX = c.i32_const(module.alloc(32))
90
+
91
+ f.addCode(
92
+ c.call("frm_square", c.getLocal("p"), AUX),
93
+ c.call("frm_square", AUX, AUX),
94
+ c.call("frm_mul", c.getLocal("p"), AUX, c.getLocal("p")),
95
+ )
96
+ }
97
+
98
+ function buildPower5all() {
99
+ const f = module.addFunction("poseidon_power5all")
100
+ f.addParam("t", "i32")
101
+ f.addLocal("i", "i32")
102
+ f.addLocal("pState", "i32")
103
+
104
+ const c = f.getCodeBuilder()
105
+
106
+ f.addCode(
107
+ c.setLocal("pState", c.i32_const(pState)),
108
+ c.setLocal("i", c.i32_const(0)),
109
+ c.block(
110
+ c.loop(
111
+ c.br_if(1, c.i32_eq(c.getLocal("i"), c.getLocal("t"))),
112
+ c.call("poseidon_power5", c.getLocal("pState")),
113
+ c.setLocal(
114
+ "pState",
115
+ c.i32_add(c.getLocal("pState"), c.i32_const(32)),
116
+ ),
117
+ c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))),
118
+ c.br(0),
119
+ ),
120
+ ),
121
+ )
122
+ }
123
+
124
+ function buildApplyMatrix() {
125
+ const f = module.addFunction("poseidon_applyMatrix")
126
+ f.addParam("t", "i32")
127
+ f.addParam("pM", "i32")
128
+ f.addLocal("i", "i32")
129
+ f.addLocal("j", "i32")
130
+ f.addLocal("pState", "i32")
131
+ f.addLocal("pStateAux", "i32")
132
+
133
+ const c = f.getCodeBuilder()
134
+
135
+ const pStateAux = module.alloc(32 * ((NSizes + 1) * 32))
136
+
137
+ const pAux = module.alloc(32)
138
+
139
+ f.addCode(
140
+ c.setLocal("pStateAux", c.i32_const(pStateAux)),
141
+ c.setLocal("i", c.i32_const(0)),
142
+ c.block(
143
+ c.loop(
144
+ c.br_if(1, c.i32_eq(c.getLocal("i"), c.getLocal("t"))),
145
+ c.call("frm_zero", c.getLocal("pStateAux")),
146
+ c.setLocal("pState", c.i32_const(pState)),
147
+ c.setLocal("j", c.i32_const(0)),
148
+ c.block(
149
+ c.loop(
150
+ c.br_if(1, c.i32_eq(c.getLocal("j"), c.getLocal("t"))),
151
+ c.call(
152
+ "frm_mul",
153
+ c.getLocal("pState"),
154
+ c.getLocal("pM"),
155
+ c.i32_const(pAux),
156
+ ),
157
+ c.call(
158
+ "frm_add",
159
+ c.i32_const(pAux),
160
+ c.getLocal("pStateAux"),
161
+ c.getLocal("pStateAux"),
162
+ ),
163
+ c.setLocal("pM", c.i32_add(c.getLocal("pM"), c.i32_const(32))),
164
+ c.setLocal(
165
+ "pState",
166
+ c.i32_add(c.getLocal("pState"), c.i32_const(32)),
167
+ ),
168
+ c.setLocal("j", c.i32_add(c.getLocal("j"), c.i32_const(1))),
169
+ c.br(0),
170
+ ),
171
+ ),
172
+ c.setLocal(
173
+ "pStateAux",
174
+ c.i32_add(c.getLocal("pStateAux"), c.i32_const(32)),
175
+ ),
176
+ c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))),
177
+ c.br(0),
178
+ ),
179
+ ),
180
+ c.setLocal("pStateAux", c.i32_const(pStateAux)),
181
+ c.setLocal("pState", c.i32_const(pState)),
182
+ c.setLocal("i", c.i32_const(0)),
183
+ c.block(
184
+ c.loop(
185
+ c.br_if(1, c.i32_eq(c.getLocal("i"), c.getLocal("t"))),
186
+ c.call("frm_copy", c.getLocal("pStateAux"), c.getLocal("pState")),
187
+ c.setLocal(
188
+ "pState",
189
+ c.i32_add(c.getLocal("pState"), c.i32_const(32)),
190
+ ),
191
+ c.setLocal(
192
+ "pStateAux",
193
+ c.i32_add(c.getLocal("pStateAux"), c.i32_const(32)),
194
+ ),
195
+ c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))),
196
+ c.br(0),
197
+ ),
198
+ ),
199
+ )
200
+ }
201
+
202
+ function buildApplySMatrix() {
203
+ const f = module.addFunction("poseidon_applySMatrix")
204
+ f.addParam("t", "i32")
205
+ f.addParam("pS", "i32")
206
+ f.setReturnType("i32")
207
+ f.addLocal("i", "i32")
208
+ f.addLocal("pState", "i32")
209
+
210
+ const c = f.getCodeBuilder()
211
+
212
+ const pS0 = module.alloc(32)
213
+ const pAux = module.alloc(32)
214
+
215
+ f.addCode(
216
+ c.call("frm_zero", c.i32_const(pS0)),
217
+ c.setLocal("pState", c.i32_const(pState)),
218
+ c.setLocal("i", c.i32_const(0)),
219
+ c.block(
220
+ c.loop(
221
+ c.br_if(1, c.i32_eq(c.getLocal("i"), c.getLocal("t"))),
222
+ c.call(
223
+ "frm_mul",
224
+ c.getLocal("pState"),
225
+ c.getLocal("pS"),
226
+ c.i32_const(pAux),
227
+ ),
228
+ c.call(
229
+ "frm_add",
230
+ c.i32_const(pS0),
231
+ c.i32_const(pAux),
232
+ c.i32_const(pS0),
233
+ ),
234
+ c.setLocal("pS", c.i32_add(c.getLocal("pS"), c.i32_const(32))),
235
+ c.setLocal(
236
+ "pState",
237
+ c.i32_add(c.getLocal("pState"), c.i32_const(32)),
238
+ ),
239
+ c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))),
240
+ c.br(0),
241
+ ),
242
+ ),
243
+
244
+ c.setLocal("pState", c.i32_const(pState + 32)),
245
+ c.setLocal("i", c.i32_const(1)),
246
+ c.block(
247
+ c.loop(
248
+ c.br_if(1, c.i32_eq(c.getLocal("i"), c.getLocal("t"))),
249
+ c.call(
250
+ "frm_mul",
251
+ c.i32_const(pState),
252
+ c.getLocal("pS"),
253
+ c.i32_const(pAux),
254
+ ),
255
+ c.call(
256
+ "frm_add",
257
+ c.getLocal("pState"),
258
+ c.i32_const(pAux),
259
+ c.getLocal("pState"),
260
+ ),
261
+ c.setLocal("pS", c.i32_add(c.getLocal("pS"), c.i32_const(32))),
262
+ c.setLocal(
263
+ "pState",
264
+ c.i32_add(c.getLocal("pState"), c.i32_const(32)),
265
+ ),
266
+ c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))),
267
+ c.br(0),
268
+ ),
269
+ ),
270
+ c.call("frm_copy", c.i32_const(pS0), c.i32_const(pState)),
271
+ c.ret(c.getLocal("pS")),
272
+ )
273
+ }
274
+
275
+ function buildPoseidon() {
276
+ const f = module.addFunction("poseidon")
277
+ f.addParam("pInitState", "i32")
278
+ f.addParam("pIn", "i32")
279
+ f.addParam("n", "i32")
280
+ f.addParam("pOut", "i32")
281
+ f.addParam("nOut", "i32")
282
+ f.addLocal("pC", "i32")
283
+ f.addLocal("pS", "i32")
284
+ f.addLocal("pM", "i32")
285
+ f.addLocal("pP", "i32")
286
+ f.addLocal("t", "i32")
287
+ f.addLocal("i", "i32")
288
+ f.addLocal("nRoundsP", "i32")
289
+ f.addLocal("pAux", "i32")
290
+
291
+ const c = f.getCodeBuilder()
292
+
293
+ f.addCode(
294
+ c.setLocal("t", c.i32_add(c.getLocal("n"), c.i32_const(1))),
295
+ c.setLocal(
296
+ "pAux",
297
+ c.i32_add(
298
+ c.i32_const(pConstants),
299
+ c.i32_mul(
300
+ c.i32_sub(c.getLocal("n"), c.i32_const(1)),
301
+ c.i32_const(20),
302
+ ),
303
+ ),
304
+ ),
305
+ c.setLocal("nRoundsP", c.i32_load(c.getLocal("pAux"))),
306
+ c.setLocal(
307
+ "pC",
308
+ c.i32_load(c.i32_add(c.getLocal("pAux"), c.i32_const(4))),
309
+ ),
310
+ c.setLocal(
311
+ "pS",
312
+ c.i32_load(c.i32_add(c.getLocal("pAux"), c.i32_const(8))),
313
+ ),
314
+ c.setLocal(
315
+ "pM",
316
+ c.i32_load(c.i32_add(c.getLocal("pAux"), c.i32_const(12))),
317
+ ),
318
+ c.setLocal(
319
+ "pP",
320
+ c.i32_load(c.i32_add(c.getLocal("pAux"), c.i32_const(16))),
321
+ ),
322
+
323
+ // Initialize state
324
+ c.call("frm_zero", c.i32_const(pState)),
325
+ c.call("frm_copy", c.getLocal("pInitState"), c.i32_const(pState)),
326
+ c.setLocal("i", c.i32_const(1)),
327
+ c.block(
328
+ c.loop(
329
+ c.call(
330
+ "frm_copy",
331
+ c.i32_add(
332
+ c.getLocal("pIn"),
333
+ c.i32_mul(
334
+ c.i32_sub(c.getLocal("i"), c.i32_const(1)),
335
+ c.i32_const(32),
336
+ ),
337
+ ),
338
+ c.i32_add(
339
+ c.i32_const(pState),
340
+ c.i32_mul(c.getLocal("i"), c.i32_const(32)),
341
+ ),
342
+ ),
343
+ c.br_if(1, c.i32_eq(c.getLocal("i"), c.getLocal("n"))),
344
+ c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))),
345
+ c.br(0),
346
+ ),
347
+ ),
348
+
349
+ // Initialize state
350
+ c.setLocal(
351
+ "pC",
352
+ c.call("poseidon_addConstant", c.getLocal("t"), c.getLocal("pC")),
353
+ ),
354
+ // First full rounds
355
+ c.setLocal("i", c.i32_const(0)),
356
+ c.block(
357
+ c.loop(
358
+ c.br_if(1, c.i32_eq(c.getLocal("i"), c.i32_const(3))),
359
+ c.call("poseidon_power5all", c.getLocal("t")),
360
+ c.setLocal(
361
+ "pC",
362
+ c.call("poseidon_addConstant", c.getLocal("t"), c.getLocal("pC")),
363
+ ),
364
+ c.call("poseidon_applyMatrix", c.getLocal("t"), c.getLocal("pM")),
365
+ c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))),
366
+ c.br(0),
367
+ ),
368
+ ),
369
+
370
+ c.call("poseidon_power5all", c.getLocal("t")),
371
+ c.setLocal(
372
+ "pC",
373
+ c.call("poseidon_addConstant", c.getLocal("t"), c.getLocal("pC")),
374
+ ),
375
+ c.call("poseidon_applyMatrix", c.getLocal("t"), c.getLocal("pP")),
376
+
377
+ c.setLocal("i", c.i32_const(0)),
378
+ c.block(
379
+ c.loop(
380
+ c.br_if(1, c.i32_eq(c.getLocal("i"), c.getLocal("nRoundsP"))),
381
+ c.call("poseidon_power5", c.i32_const(pState)),
382
+ c.call(
383
+ "frm_add",
384
+ c.i32_const(pState),
385
+ c.getLocal("pC"),
386
+ c.i32_const(pState),
387
+ ),
388
+ c.setLocal("pC", c.i32_add(c.getLocal("pC"), c.i32_const(32))),
389
+ c.setLocal(
390
+ "pS",
391
+ c.call("poseidon_applySMatrix", c.getLocal("t"), c.getLocal("pS")),
392
+ ),
393
+ c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))),
394
+ c.br(0),
395
+ ),
396
+ ),
397
+
398
+ c.setLocal("i", c.i32_const(0)),
399
+ c.block(
400
+ c.loop(
401
+ c.br_if(1, c.i32_eq(c.getLocal("i"), c.i32_const(3))),
402
+ c.call("poseidon_power5all", c.getLocal("t")),
403
+ c.setLocal(
404
+ "pC",
405
+ c.call("poseidon_addConstant", c.getLocal("t"), c.getLocal("pC")),
406
+ ),
407
+ c.call("poseidon_applyMatrix", c.getLocal("t"), c.getLocal("pM")),
408
+ c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))),
409
+ c.br(0),
410
+ ),
411
+ ),
412
+ c.call("poseidon_power5all", c.getLocal("t")),
413
+ c.call("poseidon_applyMatrix", c.getLocal("t"), c.getLocal("pM")),
414
+
415
+ c.setLocal("i", c.i32_const(0)),
416
+ c.block(
417
+ c.loop(
418
+ c.br_if(1, c.i32_eq(c.getLocal("i"), c.getLocal("nOut"))),
419
+ c.call(
420
+ "frm_copy",
421
+ c.i32_add(
422
+ c.i32_const(pState),
423
+ c.i32_mul(c.getLocal("i"), c.i32_const(32)),
424
+ ),
425
+ c.i32_add(
426
+ c.getLocal("pOut"),
427
+ c.i32_mul(c.getLocal("i"), c.i32_const(32)),
428
+ ),
429
+ ),
430
+ c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))),
431
+ c.br(0),
432
+ ),
433
+ ),
434
+ )
435
+ }
436
+
437
+ buildAddConstant()
438
+ buildPower5()
439
+ buildPower5all()
440
+ buildApplyMatrix()
441
+ buildApplySMatrix()
442
+
443
+ buildPoseidon()
444
+ module.exportFunction("poseidon")
445
+ }