zkjson 0.7.0 → 0.7.2

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,515 @@
1
+ import * as ffjavascript from "ffjavascript"
2
+ import poseidonConstants$1 from "./poseidonConstants$1.js"
3
+
4
+ function buildPoseidonWasm(module) {
5
+ const F = new ffjavascript.F1Field(
6
+ ffjavascript.Scalar.e(
7
+ "21888242871839275222246405745257275088548364400416034343698204186575808495617",
8
+ ),
9
+ )
10
+ const N_ROUNDS_P = [
11
+ 56, 57, 56, 60, 60, 63, 64, 63, 60, 66, 60, 65, 70, 60, 64, 68,
12
+ ]
13
+ const NSizes = poseidonConstants$1.C.length
14
+ const buffIdx = new Uint8Array(NSizes * 5 * 4)
15
+ const buffIdx32 = new Uint32Array(buffIdx.buffer)
16
+ for (let i = 0; i < NSizes; i++) {
17
+ buffIdx32[i * 5] = N_ROUNDS_P[i]
18
+ const buffC = new Uint8Array(32 * poseidonConstants$1.C[i].length)
19
+ for (let j = 0; j < poseidonConstants$1.C[i].length; j++) {
20
+ F.toRprLEM(buffC, j * 32, F.e(poseidonConstants$1.C[i][j]))
21
+ }
22
+ buffIdx32[i * 5 + 1] = module.alloc(buffC)
23
+
24
+ const buffS = new Uint8Array(32 * poseidonConstants$1.S[i].length)
25
+ for (let j = 0; j < poseidonConstants$1.S[i].length; j++) {
26
+ F.toRprLEM(buffS, j * 32, F.e(poseidonConstants$1.S[i][j]))
27
+ }
28
+ buffIdx32[i * 5 + 2] = module.alloc(buffS)
29
+
30
+ const N = poseidonConstants$1.M[i].length
31
+ const buffM = new Uint8Array(32 * N * N)
32
+ for (let j = 0; j < N; j++) {
33
+ for (let k = 0; k < N; k++) {
34
+ F.toRprLEM(buffM, (j * N + k) * 32, F.e(poseidonConstants$1.M[i][k][j]))
35
+ }
36
+ }
37
+ buffIdx32[i * 5 + 3] = module.alloc(buffM)
38
+
39
+ const buffP = new Uint8Array(32 * N * N)
40
+ for (let j = 0; j < N; j++) {
41
+ for (let k = 0; k < N; k++) {
42
+ F.toRprLEM(buffP, (j * N + k) * 32, F.e(poseidonConstants$1.P[i][k][j]))
43
+ }
44
+ }
45
+ buffIdx32[i * 5 + 4] = module.alloc(buffP)
46
+ }
47
+
48
+ const pConstants = module.alloc(buffIdx)
49
+ const pState = module.alloc(32 * ((NSizes + 1) * 32))
50
+
51
+ function buildAddConstant() {
52
+ const f = module.addFunction("poseidon_addConstant")
53
+ f.addParam("t", "i32")
54
+ f.addParam("pC", "i32")
55
+ f.setReturnType("i32")
56
+ f.addLocal("i", "i32")
57
+ f.addLocal("pState", "i32")
58
+
59
+ const c = f.getCodeBuilder()
60
+
61
+ f.addCode(
62
+ c.setLocal("pState", c.i32_const(pState)),
63
+ c.setLocal("i", c.i32_const(0)),
64
+ c.block(
65
+ c.loop(
66
+ c.br_if(1, c.i32_eq(c.getLocal("i"), c.getLocal("t"))),
67
+ c.call(
68
+ "frm_add",
69
+ c.getLocal("pC"),
70
+ c.getLocal("pState"),
71
+ c.getLocal("pState"),
72
+ ),
73
+ c.setLocal("pC", c.i32_add(c.getLocal("pC"), c.i32_const(32))),
74
+ c.setLocal(
75
+ "pState",
76
+ c.i32_add(c.getLocal("pState"), c.i32_const(32)),
77
+ ),
78
+ c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))),
79
+ c.br(0),
80
+ ),
81
+ ),
82
+ c.ret(c.getLocal("pC")),
83
+ )
84
+ }
85
+
86
+ function buildPower5() {
87
+ const f = module.addFunction("poseidon_power5")
88
+ f.addParam("p", "i32")
89
+
90
+ const c = f.getCodeBuilder()
91
+
92
+ const AUX = c.i32_const(module.alloc(32))
93
+
94
+ f.addCode(
95
+ c.call("frm_square", c.getLocal("p"), AUX),
96
+ c.call("frm_square", AUX, AUX),
97
+ c.call("frm_mul", c.getLocal("p"), AUX, c.getLocal("p")),
98
+ )
99
+ }
100
+
101
+ function buildPower5all() {
102
+ const f = module.addFunction("poseidon_power5all")
103
+ f.addParam("t", "i32")
104
+ f.addLocal("i", "i32")
105
+ f.addLocal("pState", "i32")
106
+
107
+ const c = f.getCodeBuilder()
108
+
109
+ f.addCode(
110
+ c.setLocal("pState", c.i32_const(pState)),
111
+ c.setLocal("i", c.i32_const(0)),
112
+ c.block(
113
+ c.loop(
114
+ c.br_if(1, c.i32_eq(c.getLocal("i"), c.getLocal("t"))),
115
+ c.call("poseidon_power5", c.getLocal("pState")),
116
+ c.setLocal(
117
+ "pState",
118
+ c.i32_add(c.getLocal("pState"), c.i32_const(32)),
119
+ ),
120
+ c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))),
121
+ c.br(0),
122
+ ),
123
+ ),
124
+ )
125
+ }
126
+
127
+ function buildApplyMatrix() {
128
+ const f = module.addFunction("poseidon_applyMatrix")
129
+ f.addParam("t", "i32")
130
+ f.addParam("pM", "i32")
131
+ f.addLocal("i", "i32")
132
+ f.addLocal("j", "i32")
133
+ f.addLocal("pState", "i32")
134
+ f.addLocal("pStateAux", "i32")
135
+
136
+ const c = f.getCodeBuilder()
137
+
138
+ const pStateAux = module.alloc(32 * ((NSizes + 1) * 32))
139
+
140
+ const pAux = module.alloc(32)
141
+
142
+ f.addCode(
143
+ c.setLocal("pStateAux", c.i32_const(pStateAux)),
144
+ c.setLocal("i", c.i32_const(0)),
145
+ c.block(
146
+ c.loop(
147
+ c.br_if(1, c.i32_eq(c.getLocal("i"), c.getLocal("t"))),
148
+ c.call("frm_zero", c.getLocal("pStateAux")),
149
+ c.setLocal("pState", c.i32_const(pState)),
150
+ c.setLocal("j", c.i32_const(0)),
151
+ c.block(
152
+ c.loop(
153
+ c.br_if(1, c.i32_eq(c.getLocal("j"), c.getLocal("t"))),
154
+ c.call(
155
+ "frm_mul",
156
+ c.getLocal("pState"),
157
+ c.getLocal("pM"),
158
+ c.i32_const(pAux),
159
+ ),
160
+ c.call(
161
+ "frm_add",
162
+ c.i32_const(pAux),
163
+ c.getLocal("pStateAux"),
164
+ c.getLocal("pStateAux"),
165
+ ),
166
+ c.setLocal("pM", c.i32_add(c.getLocal("pM"), c.i32_const(32))),
167
+ c.setLocal(
168
+ "pState",
169
+ c.i32_add(c.getLocal("pState"), c.i32_const(32)),
170
+ ),
171
+ c.setLocal("j", c.i32_add(c.getLocal("j"), c.i32_const(1))),
172
+ c.br(0),
173
+ ),
174
+ ),
175
+ c.setLocal(
176
+ "pStateAux",
177
+ c.i32_add(c.getLocal("pStateAux"), c.i32_const(32)),
178
+ ),
179
+ c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))),
180
+ c.br(0),
181
+ ),
182
+ ),
183
+ c.setLocal("pStateAux", c.i32_const(pStateAux)),
184
+ c.setLocal("pState", c.i32_const(pState)),
185
+ c.setLocal("i", c.i32_const(0)),
186
+ c.block(
187
+ c.loop(
188
+ c.br_if(1, c.i32_eq(c.getLocal("i"), c.getLocal("t"))),
189
+ c.call("frm_copy", c.getLocal("pStateAux"), c.getLocal("pState")),
190
+ c.setLocal(
191
+ "pState",
192
+ c.i32_add(c.getLocal("pState"), c.i32_const(32)),
193
+ ),
194
+ c.setLocal(
195
+ "pStateAux",
196
+ c.i32_add(c.getLocal("pStateAux"), c.i32_const(32)),
197
+ ),
198
+ c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))),
199
+ c.br(0),
200
+ ),
201
+ ),
202
+ )
203
+ }
204
+
205
+ function buildApplySMatrix() {
206
+ const f = module.addFunction("poseidon_applySMatrix")
207
+ f.addParam("t", "i32")
208
+ f.addParam("pS", "i32")
209
+ f.setReturnType("i32")
210
+ f.addLocal("i", "i32")
211
+ f.addLocal("pState", "i32")
212
+
213
+ const c = f.getCodeBuilder()
214
+
215
+ const pS0 = module.alloc(32)
216
+ const pAux = module.alloc(32)
217
+
218
+ f.addCode(
219
+ c.call("frm_zero", c.i32_const(pS0)),
220
+ c.setLocal("pState", c.i32_const(pState)),
221
+ c.setLocal("i", c.i32_const(0)),
222
+ c.block(
223
+ c.loop(
224
+ c.br_if(1, c.i32_eq(c.getLocal("i"), c.getLocal("t"))),
225
+ c.call(
226
+ "frm_mul",
227
+ c.getLocal("pState"),
228
+ c.getLocal("pS"),
229
+ c.i32_const(pAux),
230
+ ),
231
+ c.call(
232
+ "frm_add",
233
+ c.i32_const(pS0),
234
+ c.i32_const(pAux),
235
+ c.i32_const(pS0),
236
+ ),
237
+ c.setLocal("pS", c.i32_add(c.getLocal("pS"), c.i32_const(32))),
238
+ c.setLocal(
239
+ "pState",
240
+ c.i32_add(c.getLocal("pState"), c.i32_const(32)),
241
+ ),
242
+ c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))),
243
+ c.br(0),
244
+ ),
245
+ ),
246
+
247
+ c.setLocal("pState", c.i32_const(pState + 32)),
248
+ c.setLocal("i", c.i32_const(1)),
249
+ c.block(
250
+ c.loop(
251
+ c.br_if(1, c.i32_eq(c.getLocal("i"), c.getLocal("t"))),
252
+ c.call(
253
+ "frm_mul",
254
+ c.i32_const(pState),
255
+ c.getLocal("pS"),
256
+ c.i32_const(pAux),
257
+ ),
258
+ c.call(
259
+ "frm_add",
260
+ c.getLocal("pState"),
261
+ c.i32_const(pAux),
262
+ c.getLocal("pState"),
263
+ ),
264
+ c.setLocal("pS", c.i32_add(c.getLocal("pS"), c.i32_const(32))),
265
+ c.setLocal(
266
+ "pState",
267
+ c.i32_add(c.getLocal("pState"), c.i32_const(32)),
268
+ ),
269
+ c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))),
270
+ c.br(0),
271
+ ),
272
+ ),
273
+ c.call("frm_copy", c.i32_const(pS0), c.i32_const(pState)),
274
+ c.ret(c.getLocal("pS")),
275
+ )
276
+ }
277
+
278
+ function buildPoseidon() {
279
+ const f = module.addFunction("poseidon")
280
+ f.addParam("pInitState", "i32")
281
+ f.addParam("pIn", "i32")
282
+ f.addParam("n", "i32")
283
+ f.addParam("pOut", "i32")
284
+ f.addParam("nOut", "i32")
285
+ f.addLocal("pC", "i32")
286
+ f.addLocal("pS", "i32")
287
+ f.addLocal("pM", "i32")
288
+ f.addLocal("pP", "i32")
289
+ f.addLocal("t", "i32")
290
+ f.addLocal("i", "i32")
291
+ f.addLocal("nRoundsP", "i32")
292
+ f.addLocal("pAux", "i32")
293
+
294
+ const c = f.getCodeBuilder()
295
+
296
+ f.addCode(
297
+ c.setLocal("t", c.i32_add(c.getLocal("n"), c.i32_const(1))),
298
+ c.setLocal(
299
+ "pAux",
300
+ c.i32_add(
301
+ c.i32_const(pConstants),
302
+ c.i32_mul(
303
+ c.i32_sub(c.getLocal("n"), c.i32_const(1)),
304
+ c.i32_const(20),
305
+ ),
306
+ ),
307
+ ),
308
+ c.setLocal("nRoundsP", c.i32_load(c.getLocal("pAux"))),
309
+ c.setLocal(
310
+ "pC",
311
+ c.i32_load(c.i32_add(c.getLocal("pAux"), c.i32_const(4))),
312
+ ),
313
+ c.setLocal(
314
+ "pS",
315
+ c.i32_load(c.i32_add(c.getLocal("pAux"), c.i32_const(8))),
316
+ ),
317
+ c.setLocal(
318
+ "pM",
319
+ c.i32_load(c.i32_add(c.getLocal("pAux"), c.i32_const(12))),
320
+ ),
321
+ c.setLocal(
322
+ "pP",
323
+ c.i32_load(c.i32_add(c.getLocal("pAux"), c.i32_const(16))),
324
+ ),
325
+
326
+ // Initialize state
327
+ c.call("frm_zero", c.i32_const(pState)),
328
+ c.call("frm_copy", c.getLocal("pInitState"), c.i32_const(pState)),
329
+ c.setLocal("i", c.i32_const(1)),
330
+ c.block(
331
+ c.loop(
332
+ c.call(
333
+ "frm_copy",
334
+ c.i32_add(
335
+ c.getLocal("pIn"),
336
+ c.i32_mul(
337
+ c.i32_sub(c.getLocal("i"), c.i32_const(1)),
338
+ c.i32_const(32),
339
+ ),
340
+ ),
341
+ c.i32_add(
342
+ c.i32_const(pState),
343
+ c.i32_mul(c.getLocal("i"), c.i32_const(32)),
344
+ ),
345
+ ),
346
+ c.br_if(1, c.i32_eq(c.getLocal("i"), c.getLocal("n"))),
347
+ c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))),
348
+ c.br(0),
349
+ ),
350
+ ),
351
+
352
+ // Initialize state
353
+ c.setLocal(
354
+ "pC",
355
+ c.call("poseidon_addConstant", c.getLocal("t"), c.getLocal("pC")),
356
+ ),
357
+ // First full rounds
358
+ c.setLocal("i", c.i32_const(0)),
359
+ c.block(
360
+ c.loop(
361
+ c.br_if(1, c.i32_eq(c.getLocal("i"), c.i32_const(3))),
362
+ c.call("poseidon_power5all", c.getLocal("t")),
363
+ c.setLocal(
364
+ "pC",
365
+ c.call("poseidon_addConstant", c.getLocal("t"), c.getLocal("pC")),
366
+ ),
367
+ c.call("poseidon_applyMatrix", c.getLocal("t"), c.getLocal("pM")),
368
+ c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))),
369
+ c.br(0),
370
+ ),
371
+ ),
372
+
373
+ c.call("poseidon_power5all", c.getLocal("t")),
374
+ c.setLocal(
375
+ "pC",
376
+ c.call("poseidon_addConstant", c.getLocal("t"), c.getLocal("pC")),
377
+ ),
378
+ c.call("poseidon_applyMatrix", c.getLocal("t"), c.getLocal("pP")),
379
+
380
+ c.setLocal("i", c.i32_const(0)),
381
+ c.block(
382
+ c.loop(
383
+ c.br_if(1, c.i32_eq(c.getLocal("i"), c.getLocal("nRoundsP"))),
384
+ c.call("poseidon_power5", c.i32_const(pState)),
385
+ c.call(
386
+ "frm_add",
387
+ c.i32_const(pState),
388
+ c.getLocal("pC"),
389
+ c.i32_const(pState),
390
+ ),
391
+ c.setLocal("pC", c.i32_add(c.getLocal("pC"), c.i32_const(32))),
392
+ c.setLocal(
393
+ "pS",
394
+ c.call("poseidon_applySMatrix", c.getLocal("t"), c.getLocal("pS")),
395
+ ),
396
+ c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))),
397
+ c.br(0),
398
+ ),
399
+ ),
400
+
401
+ c.setLocal("i", c.i32_const(0)),
402
+ c.block(
403
+ c.loop(
404
+ c.br_if(1, c.i32_eq(c.getLocal("i"), c.i32_const(3))),
405
+ c.call("poseidon_power5all", c.getLocal("t")),
406
+ c.setLocal(
407
+ "pC",
408
+ c.call("poseidon_addConstant", c.getLocal("t"), c.getLocal("pC")),
409
+ ),
410
+ c.call("poseidon_applyMatrix", c.getLocal("t"), c.getLocal("pM")),
411
+ c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))),
412
+ c.br(0),
413
+ ),
414
+ ),
415
+ c.call("poseidon_power5all", c.getLocal("t")),
416
+ c.call("poseidon_applyMatrix", c.getLocal("t"), c.getLocal("pM")),
417
+
418
+ c.setLocal("i", c.i32_const(0)),
419
+ c.block(
420
+ c.loop(
421
+ c.br_if(1, c.i32_eq(c.getLocal("i"), c.getLocal("nOut"))),
422
+ c.call(
423
+ "frm_copy",
424
+ c.i32_add(
425
+ c.i32_const(pState),
426
+ c.i32_mul(c.getLocal("i"), c.i32_const(32)),
427
+ ),
428
+ c.i32_add(
429
+ c.getLocal("pOut"),
430
+ c.i32_mul(c.getLocal("i"), c.i32_const(32)),
431
+ ),
432
+ ),
433
+ c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))),
434
+ c.br(0),
435
+ ),
436
+ ),
437
+ )
438
+ }
439
+
440
+ buildAddConstant()
441
+ buildPower5()
442
+ buildPower5all()
443
+ buildApplyMatrix()
444
+ buildApplySMatrix()
445
+
446
+ buildPoseidon()
447
+ module.exportFunction("poseidon")
448
+ }
449
+
450
+ async function buildPoseidon$2() {
451
+ const bn128 = await ffjavascript.getCurveFromName(
452
+ "bn128",
453
+ true,
454
+ buildPoseidonWasm,
455
+ )
456
+
457
+ const F = bn128.Fr
458
+
459
+ const pState = bn128.tm.alloc(32)
460
+ const pIn = bn128.tm.alloc(32 * 16)
461
+ const pOut = bn128.tm.alloc(32 * 17)
462
+
463
+ const poseidon = (arr, state, nOut) => {
464
+ let buff
465
+ let n
466
+ if (Array.isArray(arr)) {
467
+ n = arr.length
468
+ buff = new Uint8Array(n * 32)
469
+ for (let i = 0; i < n; i++) buff.set(F.e(arr[i]), i * 32)
470
+ } else {
471
+ buff = arr
472
+ n = buff.byteLength / 32
473
+ if (n * 32 != buff.byteLength) throw new Error("Invalid iput buff size")
474
+ }
475
+ bn128.tm.setBuff(pIn, buff)
476
+
477
+ if (n < 1 || n > 16) throw new Error("Invalid poseidon size")
478
+
479
+ if (typeof state == "undefined") {
480
+ state = F.zero
481
+ } else {
482
+ state = F.e(state)
483
+ }
484
+ bn128.tm.setBuff(pState, state)
485
+ nOut = nOut || 1
486
+
487
+ bn128.tm.instance.exports.poseidon(pState, pIn, n, pOut, nOut)
488
+ if (nOut == 1) {
489
+ return bn128.tm.getBuff(pOut, 32)
490
+ } else {
491
+ const out = []
492
+ for (let i = 0; i < nOut; i++) {
493
+ out.push(bn128.tm.getBuff(pOut + i * 32, 32))
494
+ }
495
+ return out
496
+ }
497
+ }
498
+
499
+ poseidon.F = F
500
+ return poseidon
501
+ }
502
+
503
+ export default async function getHashes() {
504
+ const bn128 = await ffjavascript.getCurveFromName("bn128", true)
505
+ const poseidon = await buildPoseidon$2()
506
+ return {
507
+ hash0: function (left, right) {
508
+ return poseidon([left, right])
509
+ },
510
+ hash1: function (key, value) {
511
+ return poseidon([key, value, bn128.Fr.one])
512
+ },
513
+ F: bn128.Fr,
514
+ }
515
+ }