@javakha77/circomlibjs-hinkal-fork 0.0.11

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,440 @@
1
+
2
+ import poseidonConstants from "./poseidon_constants_opt.js";
3
+
4
+ import { getCurveFromName, Scalar, F1Field } from "ffjavascript";
5
+
6
+ export async function buildPoseidon() {
7
+ const bn128 = await getCurveFromName("bn128", true, buildPoseidonWasm);
8
+
9
+ const F = bn128.Fr;
10
+
11
+ const pState = bn128.tm.alloc(32);
12
+ const pIn = bn128.tm.alloc(32*16);
13
+ const pOut = bn128.tm.alloc(32*17);
14
+
15
+ const poseidon = (arr, state, nOut) => {
16
+ let buff;
17
+ let n;
18
+ if (Array.isArray(arr)) {
19
+ n = arr.length;
20
+ buff = new Uint8Array(n*32);
21
+ for (let i=0; i<n; i++) buff.set(F.e(arr[i]), i*32);
22
+ } else {
23
+ buff = arr;
24
+ n = buff.byteLength / 32;
25
+ if (n*32 != buff.byteLength) throw new Error("Invalid iput buff size");
26
+ }
27
+ bn128.tm.setBuff(pIn, buff);
28
+
29
+ if ((n<1)||(n>16)) throw new Error("Invalid poseidon size");
30
+
31
+ if (typeof state == "undefined") {
32
+ state = F.zero;
33
+ } else {
34
+ state = F.e(state);
35
+ }
36
+ bn128.tm.setBuff(pState, state);
37
+ nOut = nOut || 1;
38
+
39
+ bn128.tm.instance.exports.poseidon(pState, pIn, n, pOut, nOut);
40
+ if (nOut == 1) {
41
+ return bn128.tm.getBuff(pOut, 32);
42
+ } else {
43
+ const out = [];
44
+ for (let i=0; i<nOut; i++) {
45
+ out.push(bn128.tm.getBuff(pOut+i*32, 32));
46
+ }
47
+ return out
48
+ }
49
+ }
50
+
51
+ poseidon.F = F;
52
+ return poseidon;
53
+ }
54
+
55
+
56
+ export function buildPoseidonWasm(module) {
57
+
58
+ const F = new F1Field(Scalar.e("21888242871839275222246405745257275088548364400416034343698204186575808495617"));
59
+
60
+ const pointers = {
61
+ C: [],
62
+ S: [],
63
+ M: [],
64
+ P: [],
65
+ }
66
+ const N_ROUNDS_P = [56, 57, 56, 60, 60, 63, 64, 63, 60, 66, 60, 65, 70, 60, 64, 68];
67
+ const NSizes = poseidonConstants.C.length;
68
+ const buffIdx = new Uint8Array(NSizes*5*4);
69
+ const buffIdx32 = new Uint32Array(buffIdx.buffer);
70
+ for (let i=0; i<NSizes; i++) {
71
+ buffIdx32[i*5] = N_ROUNDS_P[i];
72
+ const buffC = new Uint8Array(32*poseidonConstants.C[i].length);
73
+ for (let j=0; j<poseidonConstants.C[i].length; j++) {
74
+ F.toRprLEM(buffC, j*32, F.e(poseidonConstants.C[i][j]))
75
+ }
76
+ buffIdx32[i*5 + 1] = module.alloc(buffC);
77
+
78
+ const buffS = new Uint8Array(32*poseidonConstants.S[i].length);
79
+ for (let j=0; j<poseidonConstants.S[i].length; j++) {
80
+ F.toRprLEM(buffS, j*32, F.e(poseidonConstants.S[i][j]))
81
+ }
82
+ buffIdx32[i*5 + 2] = module.alloc(buffS);
83
+
84
+ const N = poseidonConstants.M[i].length;
85
+ const buffM = new Uint8Array(32*N*N);
86
+ for (let j=0; j<N; j++) {
87
+ for (let k=0; k<N; k++) {
88
+ F.toRprLEM(buffM, (j*N+k)*32, F.e(poseidonConstants.M[i][k][j]));
89
+ }
90
+ }
91
+ buffIdx32[i*5 + 3] = module.alloc(buffM);
92
+
93
+ const buffP = new Uint8Array(32*N*N);
94
+ for (let j=0; j<N; j++) {
95
+ for (let k=0; k<N; k++) {
96
+ F.toRprLEM(buffP, (j*N+k)*32, F.e(poseidonConstants.P[i][k][j]));
97
+ }
98
+ }
99
+ buffIdx32[i*5 + 4] = module.alloc(buffP);
100
+ }
101
+
102
+ const pConstants = module.alloc(buffIdx);
103
+ const pState = module.alloc(32*((NSizes+1)*32));
104
+
105
+ function buildAddConstant() {
106
+ const f = module.addFunction("poseidon_addConstant");
107
+ f.addParam("t", "i32");
108
+ f.addParam("pC", "i32");
109
+ f.setReturnType("i32");
110
+ f.addLocal("i", "i32");
111
+ f.addLocal("pState", "i32");
112
+
113
+ const c = f.getCodeBuilder();
114
+
115
+ f.addCode(
116
+ c.setLocal("pState", c.i32_const(pState)),
117
+ c.setLocal("i", c.i32_const(0)),
118
+ c.block(c.loop(
119
+ c.br_if(1, c.i32_eq ( c.getLocal("i"), c.getLocal("t") )),
120
+ c.call("frm_add", c.getLocal("pC"), c.getLocal("pState"), c.getLocal("pState")),
121
+ c.setLocal("pC", c.i32_add(c.getLocal("pC"), c.i32_const(32))),
122
+ c.setLocal("pState", c.i32_add(c.getLocal("pState"), c.i32_const(32))),
123
+ c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))),
124
+ c.br(0)
125
+ )),
126
+ c.ret(c.getLocal("pC"))
127
+ );
128
+ }
129
+
130
+ function buildPower5() {
131
+
132
+ const f = module.addFunction("poseidon_power5");
133
+ f.addParam("p", "i32");
134
+
135
+ const c = f.getCodeBuilder();
136
+
137
+ const AUX = c.i32_const(module.alloc(32));
138
+
139
+ f.addCode(
140
+ c.call("frm_square", c.getLocal("p"), AUX),
141
+ c.call("frm_square", AUX, AUX),
142
+ c.call("frm_mul", c.getLocal("p"), AUX, c.getLocal("p")),
143
+ );
144
+
145
+ }
146
+
147
+ function buildPower5all() {
148
+ const f = module.addFunction("poseidon_power5all");
149
+ f.addParam("t", "i32");
150
+ f.addLocal("i", "i32");
151
+ f.addLocal("pState", "i32");
152
+
153
+ const c = f.getCodeBuilder();
154
+
155
+ f.addCode(
156
+ c.setLocal("pState", c.i32_const(pState)),
157
+ c.setLocal("i", c.i32_const(0)),
158
+ c.block(c.loop(
159
+ c.br_if(1, c.i32_eq ( c.getLocal("i"), c.getLocal("t") )),
160
+ c.call("poseidon_power5", c.getLocal("pState")),
161
+ c.setLocal("pState", c.i32_add(c.getLocal("pState"), c.i32_const(32))),
162
+ c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))),
163
+ c.br(0)
164
+ ))
165
+ );
166
+ }
167
+
168
+
169
+ function buildApplyMatrix() {
170
+ const f = module.addFunction("poseidon_applyMatrix");
171
+ f.addParam("t", "i32");
172
+ f.addParam("pM", "i32");
173
+ f.addLocal("i", "i32");
174
+ f.addLocal("j", "i32");
175
+ f.addLocal("pState", "i32");
176
+ f.addLocal("pStateAux", "i32");
177
+
178
+
179
+ const c = f.getCodeBuilder();
180
+
181
+ const pStateAux = module.alloc(32*((NSizes+1)*32));
182
+
183
+ const pAux = module.alloc(32);
184
+
185
+ f.addCode(
186
+ c.setLocal("pStateAux", c.i32_const(pStateAux)),
187
+ c.setLocal("i", c.i32_const(0)),
188
+ c.block(c.loop(
189
+ c.br_if(1, c.i32_eq ( c.getLocal("i"), c.getLocal("t") )),
190
+ c.call("frm_zero", c.getLocal("pStateAux")),
191
+ c.setLocal("pState", c.i32_const(pState)),
192
+ c.setLocal("j", c.i32_const(0)),
193
+ c.block(c.loop(
194
+ c.br_if(1, c.i32_eq ( c.getLocal("j"), c.getLocal("t") )),
195
+ c.call(
196
+ "frm_mul",
197
+ c.getLocal("pState"),
198
+ c.getLocal("pM"),
199
+ c.i32_const(pAux)
200
+ ),
201
+ c.call(
202
+ "frm_add",
203
+ c.i32_const(pAux),
204
+ c.getLocal("pStateAux"),
205
+ c.getLocal("pStateAux"),
206
+ ),
207
+ c.setLocal("pM", c.i32_add(c.getLocal("pM"), c.i32_const(32))),
208
+ c.setLocal("pState", c.i32_add(c.getLocal("pState"), c.i32_const(32))),
209
+ c.setLocal("j", c.i32_add(c.getLocal("j"), c.i32_const(1))),
210
+ c.br(0)
211
+ )),
212
+ c.setLocal("pStateAux", c.i32_add(c.getLocal("pStateAux"), c.i32_const(32))),
213
+ c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))),
214
+ c.br(0)
215
+ )),
216
+ c.setLocal("pStateAux", c.i32_const(pStateAux)),
217
+ c.setLocal("pState", c.i32_const(pState)),
218
+ c.setLocal("i", c.i32_const(0)),
219
+ c.block(c.loop(
220
+ c.br_if(1, c.i32_eq ( c.getLocal("i"), c.getLocal("t") )),
221
+ c.call("frm_copy", c.getLocal("pStateAux"), c.getLocal("pState")),
222
+ c.setLocal("pState", c.i32_add(c.getLocal("pState"), c.i32_const(32))),
223
+ c.setLocal("pStateAux", c.i32_add(c.getLocal("pStateAux"), c.i32_const(32))),
224
+ c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))),
225
+ c.br(0)
226
+ ))
227
+ );
228
+ }
229
+
230
+ function buildApplySMatrix() {
231
+ const f = module.addFunction("poseidon_applySMatrix");
232
+ f.addParam("t", "i32");
233
+ f.addParam("pS", "i32");
234
+ f.setReturnType("i32");
235
+ f.addLocal("i", "i32");
236
+ f.addLocal("pState", "i32");
237
+
238
+ const c = f.getCodeBuilder();
239
+
240
+ const pS0 = module.alloc(32);
241
+ const pAux = module.alloc(32);
242
+
243
+ f.addCode(
244
+ c.call("frm_zero", c.i32_const(pS0)),
245
+ c.setLocal("pState", c.i32_const(pState)),
246
+ c.setLocal("i", c.i32_const(0)),
247
+ c.block(c.loop(
248
+ c.br_if(1, c.i32_eq ( c.getLocal("i"), c.getLocal("t") )),
249
+ c.call(
250
+ "frm_mul",
251
+ c.getLocal("pState"),
252
+ c.getLocal("pS"),
253
+ c.i32_const(pAux),
254
+ ),
255
+ c.call(
256
+ "frm_add",
257
+ c.i32_const(pS0),
258
+ c.i32_const(pAux),
259
+ c.i32_const(pS0),
260
+ ),
261
+ c.setLocal("pS", c.i32_add(c.getLocal("pS"), c.i32_const(32))),
262
+ c.setLocal("pState", c.i32_add(c.getLocal("pState"), c.i32_const(32))),
263
+ c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))),
264
+ c.br(0)
265
+ )),
266
+
267
+ c.setLocal("pState", c.i32_const(pState+32)),
268
+ c.setLocal("i", c.i32_const(1)),
269
+ c.block(c.loop(
270
+ c.br_if(1, c.i32_eq ( c.getLocal("i"), c.getLocal("t") )),
271
+ c.call(
272
+ "frm_mul",
273
+ c.i32_const(pState),
274
+ c.getLocal("pS"),
275
+ c.i32_const(pAux),
276
+ ),
277
+ c.call(
278
+ "frm_add",
279
+ c.getLocal("pState"),
280
+ c.i32_const(pAux),
281
+ c.getLocal("pState"),
282
+ ),
283
+ c.setLocal("pS", c.i32_add(c.getLocal("pS"), c.i32_const(32))),
284
+ c.setLocal("pState", c.i32_add(c.getLocal("pState"), c.i32_const(32))),
285
+ c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))),
286
+ c.br(0)
287
+ )),
288
+ c.call("frm_copy", c.i32_const(pS0), c.i32_const(pState)),
289
+ c.ret(c.getLocal("pS"))
290
+ );
291
+ }
292
+
293
+ function buildPoseidon() {
294
+ const f = module.addFunction("poseidon");
295
+ f.addParam("pInitState", "i32");
296
+ f.addParam("pIn", "i32");
297
+ f.addParam("n", "i32");
298
+ f.addParam("pOut", "i32");
299
+ f.addParam("nOut", "i32");
300
+ f.addLocal("pC", "i32");
301
+ f.addLocal("pS", "i32");
302
+ f.addLocal("pM", "i32");
303
+ f.addLocal("pP", "i32");
304
+ f.addLocal("t", "i32");
305
+ f.addLocal("i", "i32");
306
+ f.addLocal("nRoundsP", "i32");
307
+ f.addLocal("pAux", "i32");
308
+
309
+ const c = f.getCodeBuilder();
310
+
311
+ f.addCode(
312
+ c.setLocal(
313
+ "t",
314
+ c.i32_add(c.getLocal("n"), c.i32_const(1))
315
+ ),
316
+ c.setLocal(
317
+ "pAux",
318
+ c.i32_add(
319
+ c.i32_const(pConstants),
320
+ c.i32_mul(
321
+ c.i32_sub(c.getLocal("n"), c.i32_const(1)),
322
+ c.i32_const(20)
323
+ )
324
+ )
325
+ ),
326
+ c.setLocal(
327
+ "nRoundsP",
328
+ c.i32_load(c.getLocal("pAux"))
329
+ ),
330
+ c.setLocal(
331
+ "pC",
332
+ c.i32_load(c.i32_add(c.getLocal("pAux"), c.i32_const(4)))
333
+ ),
334
+ c.setLocal(
335
+ "pS",
336
+ c.i32_load(c.i32_add(c.getLocal("pAux"), c.i32_const(8)))
337
+ ),
338
+ c.setLocal(
339
+ "pM",
340
+ c.i32_load(c.i32_add(c.getLocal("pAux"), c.i32_const(12)))
341
+ ),
342
+ c.setLocal(
343
+ "pP",
344
+ c.i32_load(c.i32_add(c.getLocal("pAux"), c.i32_const(16)))
345
+ ),
346
+
347
+ // Initialize state
348
+ c.call("frm_zero", c.i32_const(pState)),
349
+ c.call(
350
+ "frm_copy",
351
+ c.getLocal("pInitState"),
352
+ c.i32_const(pState),
353
+ ),
354
+ c.setLocal("i", c.i32_const(1)),
355
+ c.block(c.loop(
356
+ c.call(
357
+ "frm_copy",
358
+ c.i32_add(
359
+ c.getLocal("pIn"),
360
+ c.i32_mul(c.i32_sub(c.getLocal("i"), c.i32_const(1)), c.i32_const(32))
361
+ ),
362
+ c.i32_add(
363
+ c.i32_const(pState),
364
+ c.i32_mul(c.getLocal("i"), c.i32_const(32))
365
+ )
366
+ ),
367
+ c.br_if(1, c.i32_eq ( c.getLocal("i"), c.getLocal("n") )),
368
+ c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))),
369
+ c.br(0)
370
+ )),
371
+
372
+ // Initialize state
373
+ c.setLocal("pC", c.call("poseidon_addConstant", c.getLocal("t"), c.getLocal("pC"))),
374
+ // First full rounds
375
+ c.setLocal("i", c.i32_const(0)),
376
+ c.block(c.loop(
377
+ c.br_if(1, c.i32_eq ( c.getLocal("i"), c.i32_const(3) )),
378
+ c.call("poseidon_power5all", c.getLocal("t")),
379
+ c.setLocal("pC", c.call("poseidon_addConstant", c.getLocal("t"), c.getLocal("pC"))),
380
+ c.call("poseidon_applyMatrix", c.getLocal("t"), c.getLocal("pM")),
381
+ c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))),
382
+ c.br(0)
383
+ )),
384
+
385
+ c.call("poseidon_power5all", c.getLocal("t")),
386
+ c.setLocal("pC", c.call("poseidon_addConstant", c.getLocal("t"), c.getLocal("pC"))),
387
+ c.call("poseidon_applyMatrix", c.getLocal("t"), c.getLocal("pP")),
388
+
389
+ c.setLocal("i", c.i32_const(0)),
390
+ c.block(c.loop(
391
+ c.br_if(1, c.i32_eq ( c.getLocal("i"), c.getLocal("nRoundsP") )),
392
+ c.call("poseidon_power5", c.i32_const(pState)),
393
+ c.call("frm_add", c.i32_const(pState), c.getLocal("pC"), c.i32_const(pState)),
394
+ c.setLocal("pC", c.i32_add(c.getLocal("pC"), c.i32_const(32))),
395
+ c.setLocal("pS", c.call("poseidon_applySMatrix", c.getLocal("t"), c.getLocal("pS"))),
396
+ c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))),
397
+ c.br(0)
398
+ )),
399
+
400
+ c.setLocal("i", c.i32_const(0)),
401
+ c.block(c.loop(
402
+ c.br_if(1, c.i32_eq ( c.getLocal("i"), c.i32_const(3) )),
403
+ c.call("poseidon_power5all", c.getLocal("t")),
404
+ c.setLocal("pC", c.call("poseidon_addConstant", c.getLocal("t"), c.getLocal("pC"))),
405
+ c.call("poseidon_applyMatrix", c.getLocal("t"), c.getLocal("pM")),
406
+ c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))),
407
+ c.br(0)
408
+ )),
409
+ c.call("poseidon_power5all", c.getLocal("t")),
410
+ c.call("poseidon_applyMatrix", c.getLocal("t"), c.getLocal("pM")),
411
+
412
+ c.setLocal("i", c.i32_const(0)),
413
+ c.block(c.loop(
414
+ c.br_if(1, c.i32_eq ( c.getLocal("i"), c.getLocal("nOut") )),
415
+ c.call("frm_copy",
416
+ c.i32_add(
417
+ c.i32_const(pState),
418
+ c.i32_mul(c.getLocal("i"), c.i32_const(32))
419
+ ),
420
+ c.i32_add(
421
+ c.getLocal("pOut"),
422
+ c.i32_mul(c.getLocal("i"), c.i32_const(32))
423
+ )
424
+ ),
425
+ c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))),
426
+ c.br(0)
427
+ )),
428
+
429
+ );
430
+ }
431
+
432
+ buildAddConstant();
433
+ buildPower5();
434
+ buildPower5all();
435
+ buildApplyMatrix();
436
+ buildApplySMatrix();
437
+
438
+ buildPoseidon();
439
+ module.exportFunction("poseidon");
440
+ }
@@ -0,0 +1,3 @@
1
+ export const CIRCOM_P = 21888242871839275222246405745257275088548364400416034343698204186575808495617n;
2
+ export const CIRCOM_P_HALF = CIRCOM_P / 2n;
3
+ export const CIRCOM_MERKLE_LENGTH = 25;
@@ -0,0 +1,49 @@
1
+ /**
2
+ * @typedef {((key: string, value: any) => any) | null} JsonReplacer
3
+ */
4
+
5
+ /**
6
+ * Deep-clones a value into plain JSON-serializable data: every `bigint` becomes a decimal string.
7
+ * Use with `res.json(...)` so Express never hits "Do not know how to serialize a BigInt".
8
+ */
9
+ export const toJsonSafe = (value) => {
10
+ if (typeof value === 'bigint') {
11
+ return value.toString();
12
+ }
13
+ if (value === null || typeof value !== 'object') {
14
+ return value;
15
+ }
16
+ if (Array.isArray(value)) {
17
+ return value.map((item) => toJsonSafe(item));
18
+ }
19
+ if (value instanceof Date) {
20
+ return value.toISOString();
21
+ }
22
+ return Object.fromEntries(
23
+ Object.entries(value).map(([key, nested]) => [key, toJsonSafe(nested)]),
24
+ );
25
+ };
26
+
27
+ // Safe JSON.stringify wrapper. Converts BigInt -> string so JSON serialization never throws.
28
+ export const safeJsonStringify = (value, replacer = null, space) =>
29
+ JSON.stringify(
30
+ value,
31
+ (key, nestedValue) => {
32
+ const next = typeof replacer === 'function' ? replacer(key, nestedValue) : nestedValue;
33
+ return typeof next === 'bigint' ? next.toString() : next;
34
+ },
35
+ space,
36
+ );
37
+
38
+ export const CustomJSONStringify = (args) => {
39
+ return JSON.stringify(args, (_, value) => (typeof value === 'bigint' ? `bigint:${value.toString()}` : value));
40
+ };
41
+
42
+ export const CustomJSONParse = (stringifiedJSON) => {
43
+ return JSON.parse(stringifiedJSON, (_, value) => {
44
+ if (typeof value === 'string' && value.startsWith('bigint:')) {
45
+ return BigInt(value.slice(7));
46
+ }
47
+ return value;
48
+ });
49
+ };