@waku/rln 0.0.5 → 0.0.7

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,1147 @@
1
+ let wasm;
2
+
3
+ const cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
4
+
5
+ cachedTextDecoder.decode();
6
+
7
+ let cachedUint8Memory0 = new Uint8Array();
8
+
9
+ function getUint8Memory0() {
10
+ if (cachedUint8Memory0.byteLength === 0) {
11
+ cachedUint8Memory0 = new Uint8Array(wasm.memory.buffer);
12
+ }
13
+ return cachedUint8Memory0;
14
+ }
15
+
16
+ function getStringFromWasm0(ptr, len) {
17
+ return cachedTextDecoder.decode(getUint8Memory0().subarray(ptr, ptr + len));
18
+ }
19
+
20
+ const heap = new Array(32).fill(undefined);
21
+
22
+ heap.push(undefined, null, true, false);
23
+
24
+ let heap_next = heap.length;
25
+
26
+ function addHeapObject(obj) {
27
+ if (heap_next === heap.length) heap.push(heap.length + 1);
28
+ const idx = heap_next;
29
+ heap_next = heap[idx];
30
+
31
+ heap[idx] = obj;
32
+ return idx;
33
+ }
34
+
35
+ function getObject(idx) { return heap[idx]; }
36
+
37
+ function dropObject(idx) {
38
+ if (idx < 36) return;
39
+ heap[idx] = heap_next;
40
+ heap_next = idx;
41
+ }
42
+
43
+ function takeObject(idx) {
44
+ const ret = getObject(idx);
45
+ dropObject(idx);
46
+ return ret;
47
+ }
48
+
49
+ let WASM_VECTOR_LEN = 0;
50
+
51
+ const cachedTextEncoder = new TextEncoder('utf-8');
52
+
53
+ const encodeString = (typeof cachedTextEncoder.encodeInto === 'function'
54
+ ? function (arg, view) {
55
+ return cachedTextEncoder.encodeInto(arg, view);
56
+ }
57
+ : function (arg, view) {
58
+ const buf = cachedTextEncoder.encode(arg);
59
+ view.set(buf);
60
+ return {
61
+ read: arg.length,
62
+ written: buf.length
63
+ };
64
+ });
65
+
66
+ function passStringToWasm0(arg, malloc, realloc) {
67
+
68
+ if (realloc === undefined) {
69
+ const buf = cachedTextEncoder.encode(arg);
70
+ const ptr = malloc(buf.length);
71
+ getUint8Memory0().subarray(ptr, ptr + buf.length).set(buf);
72
+ WASM_VECTOR_LEN = buf.length;
73
+ return ptr;
74
+ }
75
+
76
+ let len = arg.length;
77
+ let ptr = malloc(len);
78
+
79
+ const mem = getUint8Memory0();
80
+
81
+ let offset = 0;
82
+
83
+ for (; offset < len; offset++) {
84
+ const code = arg.charCodeAt(offset);
85
+ if (code > 0x7F) break;
86
+ mem[ptr + offset] = code;
87
+ }
88
+
89
+ if (offset !== len) {
90
+ if (offset !== 0) {
91
+ arg = arg.slice(offset);
92
+ }
93
+ ptr = realloc(ptr, len, len = offset + arg.length * 3);
94
+ const view = getUint8Memory0().subarray(ptr + offset, ptr + len);
95
+ const ret = encodeString(arg, view);
96
+
97
+ offset += ret.written;
98
+ }
99
+
100
+ WASM_VECTOR_LEN = offset;
101
+ return ptr;
102
+ }
103
+
104
+ function isLikeNone(x) {
105
+ return x === undefined || x === null;
106
+ }
107
+
108
+ let cachedInt32Memory0 = new Int32Array();
109
+
110
+ function getInt32Memory0() {
111
+ if (cachedInt32Memory0.byteLength === 0) {
112
+ cachedInt32Memory0 = new Int32Array(wasm.memory.buffer);
113
+ }
114
+ return cachedInt32Memory0;
115
+ }
116
+
117
+ function debugString(val) {
118
+ // primitive types
119
+ const type = typeof val;
120
+ if (type == 'number' || type == 'boolean' || val == null) {
121
+ return `${val}`;
122
+ }
123
+ if (type == 'string') {
124
+ return `"${val}"`;
125
+ }
126
+ if (type == 'symbol') {
127
+ const description = val.description;
128
+ if (description == null) {
129
+ return 'Symbol';
130
+ } else {
131
+ return `Symbol(${description})`;
132
+ }
133
+ }
134
+ if (type == 'function') {
135
+ const name = val.name;
136
+ if (typeof name == 'string' && name.length > 0) {
137
+ return `Function(${name})`;
138
+ } else {
139
+ return 'Function';
140
+ }
141
+ }
142
+ // objects
143
+ if (Array.isArray(val)) {
144
+ const length = val.length;
145
+ let debug = '[';
146
+ if (length > 0) {
147
+ debug += debugString(val[0]);
148
+ }
149
+ for(let i = 1; i < length; i++) {
150
+ debug += ', ' + debugString(val[i]);
151
+ }
152
+ debug += ']';
153
+ return debug;
154
+ }
155
+ // Test for built-in
156
+ const builtInMatches = /\[object ([^\]]+)\]/.exec(toString.call(val));
157
+ let className;
158
+ if (builtInMatches.length > 1) {
159
+ className = builtInMatches[1];
160
+ } else {
161
+ // Failed to match the standard '[object ClassName]'
162
+ return toString.call(val);
163
+ }
164
+ if (className == 'Object') {
165
+ // we're a user defined class or Object
166
+ // JSON.stringify avoids problems with cycles, and is generally much
167
+ // easier than looping through ownProperties of `val`.
168
+ try {
169
+ return 'Object(' + JSON.stringify(val) + ')';
170
+ } catch (_) {
171
+ return 'Object';
172
+ }
173
+ }
174
+ // errors
175
+ if (val instanceof Error) {
176
+ return `${val.name}: ${val.message}\n${val.stack}`;
177
+ }
178
+ // TODO we could test for more things here, like `Set`s and `Map`s.
179
+ return className;
180
+ }
181
+ /**
182
+ */
183
+ function init_panic_hook() {
184
+ wasm.init_panic_hook();
185
+ }
186
+
187
+ /**
188
+ * @param {number} tree_height
189
+ * @param {Uint8Array} zkey
190
+ * @param {Uint8Array} vk
191
+ * @returns {number}
192
+ */
193
+ function newRLN(tree_height, zkey, vk) {
194
+ const ret = wasm.newRLN(tree_height, addHeapObject(zkey), addHeapObject(vk));
195
+ return ret;
196
+ }
197
+
198
+ /**
199
+ * @param {number} ctx
200
+ * @param {Uint8Array} input
201
+ * @returns {Uint8Array}
202
+ */
203
+ function getSerializedRLNWitness(ctx, input) {
204
+ const ret = wasm.getSerializedRLNWitness(ctx, addHeapObject(input));
205
+ return takeObject(ret);
206
+ }
207
+
208
+ /**
209
+ * @param {number} ctx
210
+ * @param {Uint8Array} input
211
+ */
212
+ function insertMember(ctx, input) {
213
+ try {
214
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
215
+ wasm.insertMember(retptr, ctx, addHeapObject(input));
216
+ var r0 = getInt32Memory0()[retptr / 4 + 0];
217
+ var r1 = getInt32Memory0()[retptr / 4 + 1];
218
+ if (r1) {
219
+ throw takeObject(r0);
220
+ }
221
+ } finally {
222
+ wasm.__wbindgen_add_to_stack_pointer(16);
223
+ }
224
+ }
225
+
226
+ /**
227
+ * @param {number} ctx
228
+ * @param {Uint8Array} serialized_witness
229
+ * @returns {object}
230
+ */
231
+ function RLNWitnessToJson(ctx, serialized_witness) {
232
+ const ret = wasm.RLNWitnessToJson(ctx, addHeapObject(serialized_witness));
233
+ return takeObject(ret);
234
+ }
235
+
236
+ let cachedUint32Memory0 = new Uint32Array();
237
+
238
+ function getUint32Memory0() {
239
+ if (cachedUint32Memory0.byteLength === 0) {
240
+ cachedUint32Memory0 = new Uint32Array(wasm.memory.buffer);
241
+ }
242
+ return cachedUint32Memory0;
243
+ }
244
+
245
+ function passArrayJsValueToWasm0(array, malloc) {
246
+ const ptr = malloc(array.length * 4);
247
+ const mem = getUint32Memory0();
248
+ for (let i = 0; i < array.length; i++) {
249
+ mem[ptr / 4 + i] = addHeapObject(array[i]);
250
+ }
251
+ WASM_VECTOR_LEN = array.length;
252
+ return ptr;
253
+ }
254
+ /**
255
+ * @param {number} ctx
256
+ * @param {(bigint)[]} calculated_witness
257
+ * @param {Uint8Array} serialized_witness
258
+ * @returns {Uint8Array}
259
+ */
260
+ function generate_rln_proof_with_witness(ctx, calculated_witness, serialized_witness) {
261
+ try {
262
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
263
+ const ptr0 = passArrayJsValueToWasm0(calculated_witness, wasm.__wbindgen_malloc);
264
+ const len0 = WASM_VECTOR_LEN;
265
+ wasm.generate_rln_proof_with_witness(retptr, ctx, ptr0, len0, addHeapObject(serialized_witness));
266
+ var r0 = getInt32Memory0()[retptr / 4 + 0];
267
+ var r1 = getInt32Memory0()[retptr / 4 + 1];
268
+ var r2 = getInt32Memory0()[retptr / 4 + 2];
269
+ if (r2) {
270
+ throw takeObject(r1);
271
+ }
272
+ return takeObject(r0);
273
+ } finally {
274
+ wasm.__wbindgen_add_to_stack_pointer(16);
275
+ }
276
+ }
277
+
278
+ /**
279
+ * @param {number} ctx
280
+ * @returns {Uint8Array}
281
+ */
282
+ function generateMembershipKey(ctx) {
283
+ try {
284
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
285
+ wasm.generateMembershipKey(retptr, ctx);
286
+ var r0 = getInt32Memory0()[retptr / 4 + 0];
287
+ var r1 = getInt32Memory0()[retptr / 4 + 1];
288
+ var r2 = getInt32Memory0()[retptr / 4 + 2];
289
+ if (r2) {
290
+ throw takeObject(r1);
291
+ }
292
+ return takeObject(r0);
293
+ } finally {
294
+ wasm.__wbindgen_add_to_stack_pointer(16);
295
+ }
296
+ }
297
+
298
+ /**
299
+ * @param {number} ctx
300
+ * @param {Uint8Array} proof
301
+ * @returns {boolean}
302
+ */
303
+ function verifyProof(ctx, proof) {
304
+ const ret = wasm.verifyProof(ctx, addHeapObject(proof));
305
+ return ret !== 0;
306
+ }
307
+
308
+ function handleError(f, args) {
309
+ try {
310
+ return f.apply(this, args);
311
+ } catch (e) {
312
+ wasm.__wbindgen_exn_store(addHeapObject(e));
313
+ }
314
+ }
315
+
316
+ function getArrayU8FromWasm0(ptr, len) {
317
+ return getUint8Memory0().subarray(ptr / 1, ptr / 1 + len);
318
+ }
319
+
320
+ async function load(module, imports) {
321
+ if (typeof Response === 'function' && module instanceof Response) {
322
+ if (typeof WebAssembly.instantiateStreaming === 'function') {
323
+ try {
324
+ return await WebAssembly.instantiateStreaming(module, imports);
325
+
326
+ } catch (e) {
327
+ if (module.headers.get('Content-Type') != 'application/wasm') {
328
+ console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e);
329
+
330
+ } else {
331
+ throw e;
332
+ }
333
+ }
334
+ }
335
+
336
+ const bytes = await module.arrayBuffer();
337
+ return await WebAssembly.instantiate(bytes, imports);
338
+
339
+ } else {
340
+ const instance = await WebAssembly.instantiate(module, imports);
341
+
342
+ if (instance instanceof WebAssembly.Instance) {
343
+ return { instance, module };
344
+
345
+ } else {
346
+ return instance;
347
+ }
348
+ }
349
+ }
350
+
351
+ function getImports() {
352
+ const imports = {};
353
+ imports.wbg = {};
354
+ imports.wbg.__wbindgen_string_new = function(arg0, arg1) {
355
+ const ret = getStringFromWasm0(arg0, arg1);
356
+ return addHeapObject(ret);
357
+ };
358
+ imports.wbg.__wbindgen_object_drop_ref = function(arg0) {
359
+ takeObject(arg0);
360
+ };
361
+ imports.wbg.__wbindgen_is_string = function(arg0) {
362
+ const ret = typeof(getObject(arg0)) === 'string';
363
+ return ret;
364
+ };
365
+ imports.wbg.__wbindgen_string_get = function(arg0, arg1) {
366
+ const obj = getObject(arg1);
367
+ const ret = typeof(obj) === 'string' ? obj : undefined;
368
+ var ptr0 = isLikeNone(ret) ? 0 : passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
369
+ var len0 = WASM_VECTOR_LEN;
370
+ getInt32Memory0()[arg0 / 4 + 1] = len0;
371
+ getInt32Memory0()[arg0 / 4 + 0] = ptr0;
372
+ };
373
+ imports.wbg.__wbg_BigInt_d0c7d465bfa30d3b = function(arg0) {
374
+ const ret = BigInt(arg0);
375
+ return addHeapObject(ret);
376
+ };
377
+ imports.wbg.__wbindgen_number_new = function(arg0) {
378
+ const ret = arg0;
379
+ return addHeapObject(ret);
380
+ };
381
+ imports.wbg.__wbg_BigInt_1fab4952b6c4a499 = function(arg0) {
382
+ const ret = BigInt(BigInt.asUintN(64, arg0));
383
+ return addHeapObject(ret);
384
+ };
385
+ imports.wbg.__wbindgen_object_clone_ref = function(arg0) {
386
+ const ret = getObject(arg0);
387
+ return addHeapObject(ret);
388
+ };
389
+ imports.wbg.__wbindgen_is_undefined = function(arg0) {
390
+ const ret = getObject(arg0) === undefined;
391
+ return ret;
392
+ };
393
+ imports.wbg.__wbindgen_is_object = function(arg0) {
394
+ const val = getObject(arg0);
395
+ const ret = typeof(val) === 'object' && val !== null;
396
+ return ret;
397
+ };
398
+ imports.wbg.__wbg_set_c943d600fa71e4dd = function(arg0, arg1, arg2) {
399
+ getObject(arg0)[takeObject(arg1)] = takeObject(arg2);
400
+ };
401
+ imports.wbg.__wbg_new_abda76e883ba8a5f = function() {
402
+ const ret = new Error();
403
+ return addHeapObject(ret);
404
+ };
405
+ imports.wbg.__wbg_stack_658279fe44541cf6 = function(arg0, arg1) {
406
+ const ret = getObject(arg1).stack;
407
+ const ptr0 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
408
+ const len0 = WASM_VECTOR_LEN;
409
+ getInt32Memory0()[arg0 / 4 + 1] = len0;
410
+ getInt32Memory0()[arg0 / 4 + 0] = ptr0;
411
+ };
412
+ imports.wbg.__wbg_error_f851667af71bcfc6 = function(arg0, arg1) {
413
+ try {
414
+ console.error(getStringFromWasm0(arg0, arg1));
415
+ } finally {
416
+ wasm.__wbindgen_free(arg0, arg1);
417
+ }
418
+ };
419
+ imports.wbg.__wbg_randomFillSync_065afffde01daa66 = function() { return handleError(function (arg0, arg1, arg2) {
420
+ getObject(arg0).randomFillSync(getArrayU8FromWasm0(arg1, arg2));
421
+ }, arguments) };
422
+ imports.wbg.__wbg_getRandomValues_b99eec4244a475bb = function() { return handleError(function (arg0, arg1) {
423
+ getObject(arg0).getRandomValues(getObject(arg1));
424
+ }, arguments) };
425
+ imports.wbg.__wbg_process_0cc2ada8524d6f83 = function(arg0) {
426
+ const ret = getObject(arg0).process;
427
+ return addHeapObject(ret);
428
+ };
429
+ imports.wbg.__wbg_versions_c11acceab27a6c87 = function(arg0) {
430
+ const ret = getObject(arg0).versions;
431
+ return addHeapObject(ret);
432
+ };
433
+ imports.wbg.__wbg_node_7ff1ce49caf23815 = function(arg0) {
434
+ const ret = getObject(arg0).node;
435
+ return addHeapObject(ret);
436
+ };
437
+ imports.wbg.__wbg_static_accessor_NODE_MODULE_cf6401cc1091279e = function() {
438
+ const ret = module;
439
+ return addHeapObject(ret);
440
+ };
441
+ imports.wbg.__wbg_require_a746e79b322b9336 = function() { return handleError(function (arg0, arg1, arg2) {
442
+ const ret = getObject(arg0).require(getStringFromWasm0(arg1, arg2));
443
+ return addHeapObject(ret);
444
+ }, arguments) };
445
+ imports.wbg.__wbg_crypto_2036bed7c44c25e7 = function(arg0) {
446
+ const ret = getObject(arg0).crypto;
447
+ return addHeapObject(ret);
448
+ };
449
+ imports.wbg.__wbg_msCrypto_a21fc88caf1ecdc8 = function(arg0) {
450
+ const ret = getObject(arg0).msCrypto;
451
+ return addHeapObject(ret);
452
+ };
453
+ imports.wbg.__wbg_new_1d9a920c6bfc44a8 = function() {
454
+ const ret = new Array();
455
+ return addHeapObject(ret);
456
+ };
457
+ imports.wbg.__wbg_newnoargs_b5b063fc6c2f0376 = function(arg0, arg1) {
458
+ const ret = new Function(getStringFromWasm0(arg0, arg1));
459
+ return addHeapObject(ret);
460
+ };
461
+ imports.wbg.__wbg_new_268f7b7dd3430798 = function() {
462
+ const ret = new Map();
463
+ return addHeapObject(ret);
464
+ };
465
+ imports.wbg.__wbg_call_97ae9d8645dc388b = function() { return handleError(function (arg0, arg1) {
466
+ const ret = getObject(arg0).call(getObject(arg1));
467
+ return addHeapObject(ret);
468
+ }, arguments) };
469
+ imports.wbg.__wbg_new_0b9bfdd97583284e = function() {
470
+ const ret = new Object();
471
+ return addHeapObject(ret);
472
+ };
473
+ imports.wbg.__wbg_self_6d479506f72c6a71 = function() { return handleError(function () {
474
+ const ret = self.self;
475
+ return addHeapObject(ret);
476
+ }, arguments) };
477
+ imports.wbg.__wbg_window_f2557cc78490aceb = function() { return handleError(function () {
478
+ const ret = window.window;
479
+ return addHeapObject(ret);
480
+ }, arguments) };
481
+ imports.wbg.__wbg_globalThis_7f206bda628d5286 = function() { return handleError(function () {
482
+ const ret = globalThis.globalThis;
483
+ return addHeapObject(ret);
484
+ }, arguments) };
485
+ imports.wbg.__wbg_global_ba75c50d1cf384f4 = function() { return handleError(function () {
486
+ const ret = global.global;
487
+ return addHeapObject(ret);
488
+ }, arguments) };
489
+ imports.wbg.__wbg_set_a68214f35c417fa9 = function(arg0, arg1, arg2) {
490
+ getObject(arg0)[arg1 >>> 0] = takeObject(arg2);
491
+ };
492
+ imports.wbg.__wbg_toString_d9cd5f001405e8ff = function() { return handleError(function (arg0, arg1) {
493
+ const ret = getObject(arg0).toString(arg1);
494
+ return addHeapObject(ret);
495
+ }, arguments) };
496
+ imports.wbg.__wbg_new_8d2af00bc1e329ee = function(arg0, arg1) {
497
+ const ret = new Error(getStringFromWasm0(arg0, arg1));
498
+ return addHeapObject(ret);
499
+ };
500
+ imports.wbg.__wbg_set_933729cf5b66ac11 = function(arg0, arg1, arg2) {
501
+ const ret = getObject(arg0).set(getObject(arg1), getObject(arg2));
502
+ return addHeapObject(ret);
503
+ };
504
+ imports.wbg.__wbg_fromEntries_7abdcb92016eb4b9 = function() { return handleError(function (arg0) {
505
+ const ret = Object.fromEntries(getObject(arg0));
506
+ return addHeapObject(ret);
507
+ }, arguments) };
508
+ imports.wbg.__wbg_buffer_3f3d764d4747d564 = function(arg0) {
509
+ const ret = getObject(arg0).buffer;
510
+ return addHeapObject(ret);
511
+ };
512
+ imports.wbg.__wbg_newwithbyteoffsetandlength_d9aa266703cb98be = function(arg0, arg1, arg2) {
513
+ const ret = new Uint8Array(getObject(arg0), arg1 >>> 0, arg2 >>> 0);
514
+ return addHeapObject(ret);
515
+ };
516
+ imports.wbg.__wbg_new_8c3f0052272a457a = function(arg0) {
517
+ const ret = new Uint8Array(getObject(arg0));
518
+ return addHeapObject(ret);
519
+ };
520
+ imports.wbg.__wbg_set_83db9690f9353e79 = function(arg0, arg1, arg2) {
521
+ getObject(arg0).set(getObject(arg1), arg2 >>> 0);
522
+ };
523
+ imports.wbg.__wbg_length_9e1ae1900cb0fbd5 = function(arg0) {
524
+ const ret = getObject(arg0).length;
525
+ return ret;
526
+ };
527
+ imports.wbg.__wbg_newwithlength_f5933855e4f48a19 = function(arg0) {
528
+ const ret = new Uint8Array(arg0 >>> 0);
529
+ return addHeapObject(ret);
530
+ };
531
+ imports.wbg.__wbg_subarray_58ad4efbb5bcb886 = function(arg0, arg1, arg2) {
532
+ const ret = getObject(arg0).subarray(arg1 >>> 0, arg2 >>> 0);
533
+ return addHeapObject(ret);
534
+ };
535
+ imports.wbg.__wbindgen_debug_string = function(arg0, arg1) {
536
+ const ret = debugString(getObject(arg1));
537
+ const ptr0 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
538
+ const len0 = WASM_VECTOR_LEN;
539
+ getInt32Memory0()[arg0 / 4 + 1] = len0;
540
+ getInt32Memory0()[arg0 / 4 + 0] = ptr0;
541
+ };
542
+ imports.wbg.__wbindgen_throw = function(arg0, arg1) {
543
+ throw new Error(getStringFromWasm0(arg0, arg1));
544
+ };
545
+ imports.wbg.__wbindgen_memory = function() {
546
+ const ret = wasm.memory;
547
+ return addHeapObject(ret);
548
+ };
549
+
550
+ return imports;
551
+ }
552
+
553
+ function finalizeInit(instance, module) {
554
+ wasm = instance.exports;
555
+ init.__wbindgen_wasm_module = module;
556
+ cachedInt32Memory0 = new Int32Array();
557
+ cachedUint32Memory0 = new Uint32Array();
558
+ cachedUint8Memory0 = new Uint8Array();
559
+
560
+
561
+ return wasm;
562
+ }
563
+
564
+ async function init(input) {
565
+ if (typeof input === 'undefined') {
566
+ input = new URL(new URL('assets/rln_wasm_bg-0185b546.wasm', import.meta.url).href, import.meta.url);
567
+ }
568
+ const imports = getImports();
569
+
570
+ if (typeof input === 'string' || (typeof Request === 'function' && input instanceof Request) || (typeof URL === 'function' && input instanceof URL)) {
571
+ input = fetch(input);
572
+ }
573
+
574
+ const { instance, module } = await load(await input, imports);
575
+
576
+ return finalizeInit(instance, module);
577
+ }
578
+
579
+ var protocol = "groth16";
580
+ var curve = "bn128";
581
+ var nPublic = 6;
582
+ var vk_alpha_1 = [
583
+ "1805378556360488226980822394597799963030511477964155500103132920745199284516",
584
+ "11990395240534218699464972016456017378439762088320057798320175886595281336136",
585
+ "1"
586
+ ];
587
+ var vk_beta_2 = [
588
+ [
589
+ "11031529986141021025408838211017932346992429731488270384177563837022796743627",
590
+ "16042159910707312759082561183373181639420894978640710177581040523252926273854"
591
+ ],
592
+ [
593
+ "20112698439519222240302944148895052359035104222313380895334495118294612255131",
594
+ "19441583024670359810872018179190533814486480928824742448673677460151702019379"
595
+ ],
596
+ [
597
+ "1",
598
+ "0"
599
+ ]
600
+ ];
601
+ var vk_gamma_2 = [
602
+ [
603
+ "10857046999023057135944570762232829481370756359578518086990519993285655852781",
604
+ "11559732032986387107991004021392285783925812861821192530917403151452391805634"
605
+ ],
606
+ [
607
+ "8495653923123431417604973247489272438418190587263600148770280649306958101930",
608
+ "4082367875863433681332203403145435568316851327593401208105741076214120093531"
609
+ ],
610
+ [
611
+ "1",
612
+ "0"
613
+ ]
614
+ ];
615
+ var vk_delta_2 = [
616
+ [
617
+ "1948496782571164085469528023647105317580208688174386157591917599801657832035",
618
+ "20445814069256658101339037520922621162739470138213615104905368409238414511981"
619
+ ],
620
+ [
621
+ "10024680869920840984813249386422727863826862577760330492647062850849851925340",
622
+ "10512156247842686783409460795717734694774542185222602679117887145206209285142"
623
+ ],
624
+ [
625
+ "1",
626
+ "0"
627
+ ]
628
+ ];
629
+ var vk_alphabeta_12 = [
630
+ [
631
+ [
632
+ "5151991366823434428398919091000210787450832786814248297320989361921939794156",
633
+ "15735191313289001022885148627913534790382722933676436876510746491415970766821"
634
+ ],
635
+ [
636
+ "3387907257437913904447588318761906430938415556102110876587455322225272831272",
637
+ "1998779853452712881084781956683721603875246565720647583735935725110674288056"
638
+ ],
639
+ [
640
+ "14280074182991498185075387990446437410077692353432005297922275464876153151820",
641
+ "17092408446352310039633488224969232803092763095456307462247653153107223117633"
642
+ ]
643
+ ],
644
+ [
645
+ [
646
+ "4359046709531668109201634396816565829237358165496082832279660960675584351266",
647
+ "4511888308846208349307186938266411423935335853916317436093178288331845821336"
648
+ ],
649
+ [
650
+ "11429499807090785857812316277335883295048773373068683863667725283965356423273",
651
+ "16232274853200678548795010078253506586114563833318973594428907292096178657392"
652
+ ],
653
+ [
654
+ "18068999605870933925311275504102553573815570223888590384919752303726860800970",
655
+ "17309569111965782732372130116757295842160193489132771344011460471298173784984"
656
+ ]
657
+ ]
658
+ ];
659
+ var IC = [
660
+ [
661
+ "18693301901828818437917730940595978397160482710354161265484535387752523310572",
662
+ "17985273354976640088538673802000794244421192643855111089693820179790551470769",
663
+ "1"
664
+ ],
665
+ [
666
+ "21164641723988537620541455173278629777250883365474191521194244273980931825942",
667
+ "998385854410718613441067082771678946155853656328717326195057262123686425518",
668
+ "1"
669
+ ],
670
+ [
671
+ "21666968581672145768705229094968410656430989593283335488162701230986314747515",
672
+ "17996457608540683483506630273632100555125353447506062045735279661096094677264",
673
+ "1"
674
+ ],
675
+ [
676
+ "20137761979695192602424300886442379728165712610493092740175904438282083668117",
677
+ "19184814924890679891263780109959113289320127263583260218200636509492157834679",
678
+ "1"
679
+ ],
680
+ [
681
+ "10943171273393803842589314082509655332154393332394322726077270895078286354146",
682
+ "10872472035685319847811233167729172672344935625121511932198535224727331126439",
683
+ "1"
684
+ ],
685
+ [
686
+ "13049169779481227658517545034348883391527506091990880778783387628208561946597",
687
+ "10083689369261379027228809473568899816311684698866922944902456565434209079955",
688
+ "1"
689
+ ],
690
+ [
691
+ "19633516378466409167014413361365552102431118630694133723053441455184566611083",
692
+ "8059525100726933978719058611146131904598011633549012007359165766216730722269",
693
+ "1"
694
+ ]
695
+ ];
696
+ var verificationKey = {
697
+ protocol: protocol,
698
+ curve: curve,
699
+ nPublic: nPublic,
700
+ vk_alpha_1: vk_alpha_1,
701
+ vk_beta_2: vk_beta_2,
702
+ vk_gamma_2: vk_gamma_2,
703
+ vk_delta_2: vk_delta_2,
704
+ vk_alphabeta_12: vk_alphabeta_12,
705
+ IC: IC
706
+ };
707
+
708
+ // File generated with https://github.com/iden3/circom
709
+ // following the instructions from:
710
+ // https://github.com/vacp2p/zerokit/tree/master/rln#compiling-circuits
711
+ async function builder(code, options) {
712
+ options = options || {};
713
+ let wasmModule;
714
+ try {
715
+ wasmModule = await WebAssembly.compile(code);
716
+ }
717
+ catch (err) {
718
+ console.log(err);
719
+ console.log("\nTry to run circom --c in order to generate c++ code instead\n");
720
+ throw new Error(err);
721
+ }
722
+ let wc;
723
+ let errStr = "";
724
+ let msgStr = "";
725
+ const instance = await WebAssembly.instantiate(wasmModule, {
726
+ runtime: {
727
+ exceptionHandler: function (code) {
728
+ let err;
729
+ if (code == 1) {
730
+ err = "Signal not found.\n";
731
+ }
732
+ else if (code == 2) {
733
+ err = "Too many signals set.\n";
734
+ }
735
+ else if (code == 3) {
736
+ err = "Signal already set.\n";
737
+ }
738
+ else if (code == 4) {
739
+ err = "Assert Failed.\n";
740
+ }
741
+ else if (code == 5) {
742
+ err = "Not enough memory.\n";
743
+ }
744
+ else if (code == 6) {
745
+ err = "Input signal array access exceeds the size.\n";
746
+ }
747
+ else {
748
+ err = "Unknown error.\n";
749
+ }
750
+ throw new Error(err + errStr);
751
+ },
752
+ printErrorMessage: function () {
753
+ errStr += getMessage() + "\n";
754
+ // console.error(getMessage());
755
+ },
756
+ writeBufferMessage: function () {
757
+ const msg = getMessage();
758
+ // Any calls to `log()` will always end with a `\n`, so that's when we print and reset
759
+ if (msg === "\n") {
760
+ console.log(msgStr);
761
+ msgStr = "";
762
+ }
763
+ else {
764
+ // If we've buffered other content, put a space in between the items
765
+ if (msgStr !== "") {
766
+ msgStr += " ";
767
+ }
768
+ // Then append the message to the message we are creating
769
+ msgStr += msg;
770
+ }
771
+ },
772
+ showSharedRWMemory: function () {
773
+ printSharedRWMemory();
774
+ }
775
+ }
776
+ });
777
+ const sanityCheck = options;
778
+ // options &&
779
+ // (
780
+ // options.sanityCheck ||
781
+ // options.logGetSignal ||
782
+ // options.logSetSignal ||
783
+ // options.logStartComponent ||
784
+ // options.logFinishComponent
785
+ // );
786
+ wc = new WitnessCalculator(instance, sanityCheck);
787
+ return wc;
788
+ function getMessage() {
789
+ var message = "";
790
+ var c = instance.exports.getMessageChar();
791
+ while (c != 0) {
792
+ message += String.fromCharCode(c);
793
+ c = instance.exports.getMessageChar();
794
+ }
795
+ return message;
796
+ }
797
+ function printSharedRWMemory() {
798
+ const shared_rw_memory_size = instance.exports.getFieldNumLen32();
799
+ const arr = new Uint32Array(shared_rw_memory_size);
800
+ for (let j = 0; j < shared_rw_memory_size; j++) {
801
+ arr[shared_rw_memory_size - 1 - j] = instance.exports.readSharedRWMemory(j);
802
+ }
803
+ // If we've buffered other content, put a space in between the items
804
+ if (msgStr !== "") {
805
+ msgStr += " ";
806
+ }
807
+ // Then append the value to the message we are creating
808
+ msgStr += (fromArray32(arr).toString());
809
+ }
810
+ }
811
+ class WitnessCalculator {
812
+ constructor(instance, sanityCheck) {
813
+ this.instance = instance;
814
+ this.version = this.instance.exports.getVersion();
815
+ this.n32 = this.instance.exports.getFieldNumLen32();
816
+ this.instance.exports.getRawPrime();
817
+ const arr = new Uint32Array(this.n32);
818
+ for (let i = 0; i < this.n32; i++) {
819
+ arr[this.n32 - 1 - i] = this.instance.exports.readSharedRWMemory(i);
820
+ }
821
+ this.prime = fromArray32(arr);
822
+ this.witnessSize = this.instance.exports.getWitnessSize();
823
+ this.sanityCheck = sanityCheck;
824
+ }
825
+ circom_version() {
826
+ return this.instance.exports.getVersion();
827
+ }
828
+ async _doCalculateWitness(input, sanityCheck) {
829
+ //input is assumed to be a map from signals to arrays of bigints
830
+ this.instance.exports.init((this.sanityCheck || sanityCheck) ? 1 : 0);
831
+ const keys = Object.keys(input);
832
+ var input_counter = 0;
833
+ keys.forEach((k) => {
834
+ const h = fnvHash(k);
835
+ const hMSB = parseInt(h.slice(0, 8), 16);
836
+ const hLSB = parseInt(h.slice(8, 16), 16);
837
+ const fArr = flatArray(input[k]);
838
+ let signalSize = this.instance.exports.getInputSignalSize(hMSB, hLSB);
839
+ if (signalSize < 0) {
840
+ throw new Error(`Signal ${k} not found\n`);
841
+ }
842
+ if (fArr.length < signalSize) {
843
+ throw new Error(`Not enough values for input signal ${k}\n`);
844
+ }
845
+ if (fArr.length > signalSize) {
846
+ throw new Error(`Too many values for input signal ${k}\n`);
847
+ }
848
+ for (let i = 0; i < fArr.length; i++) {
849
+ const arrFr = toArray32(BigInt(fArr[i]) % this.prime, this.n32);
850
+ for (let j = 0; j < this.n32; j++) {
851
+ this.instance.exports.writeSharedRWMemory(j, arrFr[this.n32 - 1 - j]);
852
+ }
853
+ try {
854
+ this.instance.exports.setInputSignal(hMSB, hLSB, i);
855
+ input_counter++;
856
+ }
857
+ catch (err) {
858
+ // console.log(`After adding signal ${i} of ${k}`)
859
+ throw new Error(err);
860
+ }
861
+ }
862
+ });
863
+ if (input_counter < this.instance.exports.getInputSize()) {
864
+ throw new Error(`Not all inputs have been set. Only ${input_counter} out of ${this.instance.exports.getInputSize()}`);
865
+ }
866
+ }
867
+ async calculateWitness(input, sanityCheck) {
868
+ const w = [];
869
+ await this._doCalculateWitness(input, sanityCheck);
870
+ for (let i = 0; i < this.witnessSize; i++) {
871
+ this.instance.exports.getWitness(i);
872
+ const arr = new Uint32Array(this.n32);
873
+ for (let j = 0; j < this.n32; j++) {
874
+ arr[this.n32 - 1 - j] = this.instance.exports.readSharedRWMemory(j);
875
+ }
876
+ w.push(fromArray32(arr));
877
+ }
878
+ return w;
879
+ }
880
+ async calculateBinWitness(input, sanityCheck) {
881
+ const buff32 = new Uint32Array(this.witnessSize * this.n32);
882
+ const buff = new Uint8Array(buff32.buffer);
883
+ await this._doCalculateWitness(input, sanityCheck);
884
+ for (let i = 0; i < this.witnessSize; i++) {
885
+ this.instance.exports.getWitness(i);
886
+ const pos = i * this.n32;
887
+ for (let j = 0; j < this.n32; j++) {
888
+ buff32[pos + j] = this.instance.exports.readSharedRWMemory(j);
889
+ }
890
+ }
891
+ return buff;
892
+ }
893
+ async calculateWTNSBin(input, sanityCheck) {
894
+ const buff32 = new Uint32Array(this.witnessSize * this.n32 + this.n32 + 11);
895
+ const buff = new Uint8Array(buff32.buffer);
896
+ await this._doCalculateWitness(input, sanityCheck);
897
+ //"wtns"
898
+ buff[0] = "w".charCodeAt(0);
899
+ buff[1] = "t".charCodeAt(0);
900
+ buff[2] = "n".charCodeAt(0);
901
+ buff[3] = "s".charCodeAt(0);
902
+ //version 2
903
+ buff32[1] = 2;
904
+ //number of sections: 2
905
+ buff32[2] = 2;
906
+ //id section 1
907
+ buff32[3] = 1;
908
+ const n8 = this.n32 * 4;
909
+ //id section 1 length in 64bytes
910
+ const idSection1length = 8 + n8;
911
+ const idSection1lengthHex = idSection1length.toString(16);
912
+ buff32[4] = parseInt(idSection1lengthHex.slice(0, 8), 16);
913
+ buff32[5] = parseInt(idSection1lengthHex.slice(8, 16), 16);
914
+ //this.n32
915
+ buff32[6] = n8;
916
+ //prime number
917
+ this.instance.exports.getRawPrime();
918
+ var pos = 7;
919
+ for (let j = 0; j < this.n32; j++) {
920
+ buff32[pos + j] = this.instance.exports.readSharedRWMemory(j);
921
+ }
922
+ pos += this.n32;
923
+ // witness size
924
+ buff32[pos] = this.witnessSize;
925
+ pos++;
926
+ //id section 2
927
+ buff32[pos] = 2;
928
+ pos++;
929
+ // section 2 length
930
+ const idSection2length = n8 * this.witnessSize;
931
+ const idSection2lengthHex = idSection2length.toString(16);
932
+ buff32[pos] = parseInt(idSection2lengthHex.slice(0, 8), 16);
933
+ buff32[pos + 1] = parseInt(idSection2lengthHex.slice(8, 16), 16);
934
+ pos += 2;
935
+ for (let i = 0; i < this.witnessSize; i++) {
936
+ this.instance.exports.getWitness(i);
937
+ for (let j = 0; j < this.n32; j++) {
938
+ buff32[pos + j] = this.instance.exports.readSharedRWMemory(j);
939
+ }
940
+ pos += this.n32;
941
+ }
942
+ return buff;
943
+ }
944
+ }
945
+ function toArray32(rem, size) {
946
+ const res = []; //new Uint32Array(size); //has no unshift
947
+ const radix = BigInt(0x100000000);
948
+ while (rem) {
949
+ res.unshift(Number(rem % radix));
950
+ rem = rem / radix;
951
+ }
952
+ if (size) {
953
+ var i = size - res.length;
954
+ while (i > 0) {
955
+ res.unshift(0);
956
+ i--;
957
+ }
958
+ }
959
+ return res;
960
+ }
961
+ function fromArray32(arr) {
962
+ var res = BigInt(0);
963
+ const radix = BigInt(0x100000000);
964
+ for (let i = 0; i < arr.length; i++) {
965
+ res = res * radix + BigInt(arr[i]);
966
+ }
967
+ return res;
968
+ }
969
+ function flatArray(a) {
970
+ var res = [];
971
+ fillArray(res, a);
972
+ return res;
973
+ function fillArray(res, a) {
974
+ if (Array.isArray(a)) {
975
+ for (let i = 0; i < a.length; i++) {
976
+ fillArray(res, a[i]);
977
+ }
978
+ }
979
+ else {
980
+ res.push(a);
981
+ }
982
+ }
983
+ }
984
+ function fnvHash(str) {
985
+ const uint64_max = BigInt(2) ** BigInt(64);
986
+ let hash = BigInt("0xCBF29CE484222325");
987
+ for (var i = 0; i < str.length; i++) {
988
+ hash ^= BigInt(str[i].charCodeAt());
989
+ hash *= BigInt(0x100000001B3);
990
+ hash %= uint64_max;
991
+ }
992
+ let shash = hash.toString(16);
993
+ let n = 16 - shash.length;
994
+ shash = '0'.repeat(n).concat(shash);
995
+ return shash;
996
+ }
997
+
998
+ /**
999
+ * Concatenate Uint8Arrays
1000
+ * @param input
1001
+ * @returns concatenation of all Uint8Array received as input
1002
+ */
1003
+ function concatenate(...input) {
1004
+ let totalLength = 0;
1005
+ for (const arr of input) {
1006
+ totalLength += arr.length;
1007
+ }
1008
+ const result = new Uint8Array(totalLength);
1009
+ let offset = 0;
1010
+ for (const arr of input) {
1011
+ result.set(arr, offset);
1012
+ offset += arr.length;
1013
+ }
1014
+ return result;
1015
+ }
1016
+ const stringEncoder = new TextEncoder();
1017
+ const DEPTH = 20;
1018
+ async function loadWitnessCalculator() {
1019
+ const url = new URL(new URL('assets/rln-fb4d7b4b.wasm', import.meta.url).href, import.meta.url);
1020
+ const response = await fetch(url);
1021
+ return await builder(new Uint8Array(await response.arrayBuffer()), false);
1022
+ }
1023
+ async function loadZkey() {
1024
+ const url = new URL(new URL('assets/rln_final-a641c06e.zkey', import.meta.url).href, import.meta.url);
1025
+ const response = await fetch(url);
1026
+ return new Uint8Array(await response.arrayBuffer());
1027
+ }
1028
+ /**
1029
+ * Create an instance of RLN
1030
+ * @returns RLNInstance
1031
+ */
1032
+ async function create() {
1033
+ await init();
1034
+ init_panic_hook();
1035
+ const witnessCalculator = await loadWitnessCalculator();
1036
+ const zkey = await loadZkey();
1037
+ const vkey = stringEncoder.encode(JSON.stringify(verificationKey));
1038
+ const zkRLN = newRLN(DEPTH, zkey, vkey);
1039
+ return new RLNInstance(zkRLN, witnessCalculator);
1040
+ }
1041
+ class MembershipKey {
1042
+ constructor(memKeys) {
1043
+ this.IDKey = memKeys.subarray(0, 32);
1044
+ this.IDCommitment = memKeys.subarray(32);
1045
+ }
1046
+ }
1047
+ // Adapted from https://github.com/feross/buffer
1048
+ function checkInt(buf, value, offset, ext, max, min) {
1049
+ if (value > max || value < min)
1050
+ throw new RangeError('"value" argument is out of bounds');
1051
+ if (offset + ext > buf.length)
1052
+ throw new RangeError("Index out of range");
1053
+ }
1054
+ const writeUIntLE = function writeUIntLE(buf, value, offset, byteLength, noAssert) {
1055
+ value = +value;
1056
+ offset = offset >>> 0;
1057
+ byteLength = byteLength >>> 0;
1058
+ if (!noAssert) {
1059
+ const maxBytes = Math.pow(2, 8 * byteLength) - 1;
1060
+ checkInt(buf, value, offset, byteLength, maxBytes, 0);
1061
+ }
1062
+ let mul = 1;
1063
+ let i = 0;
1064
+ buf[offset] = value & 0xff;
1065
+ while (++i < byteLength && (mul *= 0x100)) {
1066
+ buf[offset + i] = (value / mul) & 0xff;
1067
+ }
1068
+ return buf;
1069
+ };
1070
+ const DefaultEpochUnitSeconds = 10; // the rln-relay epoch length in seconds
1071
+ function toEpoch(timestamp, epochUnitSeconds = DefaultEpochUnitSeconds) {
1072
+ const unix = Math.floor(timestamp.getTime() / 1000 / epochUnitSeconds);
1073
+ return writeUIntLE(new Uint8Array(32), unix, 0, 8);
1074
+ }
1075
+ const proofOffset = 128;
1076
+ const rootOffset = proofOffset + 32;
1077
+ const epochOffset = rootOffset + 32;
1078
+ const shareXOffset = epochOffset + 32;
1079
+ const shareYOffset = shareXOffset + 32;
1080
+ const nullifierOffset = shareYOffset + 32;
1081
+ const rlnIdentifierOffset = nullifierOffset + 32;
1082
+ class RateLimitProof {
1083
+ constructor(proofBytes) {
1084
+ if (proofBytes.length < rlnIdentifierOffset)
1085
+ throw "invalid proof";
1086
+ // parse the proof as proof<128> | share_y<32> | nullifier<32> | root<32> | epoch<32> | share_x<32> | rln_identifier<32>
1087
+ this.proof = proofBytes.subarray(0, proofOffset);
1088
+ this.merkleRoot = proofBytes.subarray(proofOffset, rootOffset);
1089
+ this.epoch = proofBytes.subarray(rootOffset, epochOffset);
1090
+ this.shareX = proofBytes.subarray(epochOffset, shareXOffset);
1091
+ this.shareY = proofBytes.subarray(shareXOffset, shareYOffset);
1092
+ this.nullifier = proofBytes.subarray(shareYOffset, nullifierOffset);
1093
+ this.rlnIdentifier = proofBytes.subarray(nullifierOffset, rlnIdentifierOffset);
1094
+ }
1095
+ toBytes() {
1096
+ return concatenate(this.proof, this.merkleRoot, this.epoch, this.shareX, this.shareY, this.nullifier, this.rlnIdentifier);
1097
+ }
1098
+ }
1099
+ class RLNInstance {
1100
+ constructor(zkRLN, witnessCalculator) {
1101
+ this.zkRLN = zkRLN;
1102
+ this.witnessCalculator = witnessCalculator;
1103
+ }
1104
+ generateMembershipKey() {
1105
+ const memKeys = generateMembershipKey(this.zkRLN);
1106
+ return new MembershipKey(memKeys);
1107
+ }
1108
+ insertMember(idCommitment) {
1109
+ insertMember(this.zkRLN, idCommitment);
1110
+ }
1111
+ serializeMessage(uint8Msg, memIndex, epoch, idKey) {
1112
+ // calculate message length
1113
+ const msgLen = writeUIntLE(new Uint8Array(8), uint8Msg.length, 0, 8);
1114
+ // Converting index to LE bytes
1115
+ const memIndexBytes = writeUIntLE(new Uint8Array(8), memIndex, 0, 8);
1116
+ // [ id_key<32> | id_index<8> | epoch<32> | signal_len<8> | signal<var> ]
1117
+ return concatenate(idKey, memIndexBytes, epoch, msgLen, uint8Msg);
1118
+ }
1119
+ async generateProof(msg, index, epoch, idKey) {
1120
+ if (epoch == undefined) {
1121
+ epoch = toEpoch(new Date());
1122
+ }
1123
+ else if (epoch instanceof Date) {
1124
+ epoch = toEpoch(epoch);
1125
+ }
1126
+ if (epoch.length != 32)
1127
+ throw "invalid epoch";
1128
+ if (idKey.length != 32)
1129
+ throw "invalid id key";
1130
+ if (index < 0)
1131
+ throw "index must be >= 0";
1132
+ const serialized_msg = this.serializeMessage(msg, index, epoch, idKey);
1133
+ const rlnWitness = getSerializedRLNWitness(this.zkRLN, serialized_msg);
1134
+ const inputs = RLNWitnessToJson(this.zkRLN, rlnWitness);
1135
+ const calculatedWitness = await this.witnessCalculator.calculateWitness(inputs, false); // no sanity check being used in zerokit
1136
+ const proofBytes = generate_rln_proof_with_witness(this.zkRLN, calculatedWitness, rlnWitness);
1137
+ return new RateLimitProof(proofBytes);
1138
+ }
1139
+ verifyProof(proof) {
1140
+ if (proof instanceof RateLimitProof) {
1141
+ proof = proof.toBytes();
1142
+ }
1143
+ return verifyProof(this.zkRLN, proof);
1144
+ }
1145
+ }
1146
+
1147
+ export { MembershipKey, RLNInstance, RateLimitProof, create, toEpoch };