@waku/rln 0.0.8-3d977ba → 0.0.8-4f8118e
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.
- package/bundle/index.js +1157 -2
- package/dist/.tsbuildinfo +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/rln.d.ts +2 -1
- package/dist/rln.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +2 -1
- package/src/rln.ts +6 -2
- package/src/witness_calculator.d.ts +5 -1
- package/bundle/rln-7c06a1d6.js +0 -1147
package/bundle/index.js
CHANGED
@@ -1,10 +1,1165 @@
|
|
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
|
+
const verificationKey = {
|
580
|
+
"protocol": "groth16",
|
581
|
+
"curve": "bn128",
|
582
|
+
"nPublic": 6,
|
583
|
+
"vk_alpha_1": [
|
584
|
+
"1805378556360488226980822394597799963030511477964155500103132920745199284516",
|
585
|
+
"11990395240534218699464972016456017378439762088320057798320175886595281336136",
|
586
|
+
"1"
|
587
|
+
],
|
588
|
+
"vk_beta_2": [
|
589
|
+
[
|
590
|
+
"11031529986141021025408838211017932346992429731488270384177563837022796743627",
|
591
|
+
"16042159910707312759082561183373181639420894978640710177581040523252926273854"
|
592
|
+
],
|
593
|
+
[
|
594
|
+
"20112698439519222240302944148895052359035104222313380895334495118294612255131",
|
595
|
+
"19441583024670359810872018179190533814486480928824742448673677460151702019379"
|
596
|
+
],
|
597
|
+
[
|
598
|
+
"1",
|
599
|
+
"0"
|
600
|
+
]
|
601
|
+
],
|
602
|
+
"vk_gamma_2": [
|
603
|
+
[
|
604
|
+
"10857046999023057135944570762232829481370756359578518086990519993285655852781",
|
605
|
+
"11559732032986387107991004021392285783925812861821192530917403151452391805634"
|
606
|
+
],
|
607
|
+
[
|
608
|
+
"8495653923123431417604973247489272438418190587263600148770280649306958101930",
|
609
|
+
"4082367875863433681332203403145435568316851327593401208105741076214120093531"
|
610
|
+
],
|
611
|
+
[
|
612
|
+
"1",
|
613
|
+
"0"
|
614
|
+
]
|
615
|
+
],
|
616
|
+
"vk_delta_2": [
|
617
|
+
[
|
618
|
+
"1948496782571164085469528023647105317580208688174386157591917599801657832035",
|
619
|
+
"20445814069256658101339037520922621162739470138213615104905368409238414511981"
|
620
|
+
],
|
621
|
+
[
|
622
|
+
"10024680869920840984813249386422727863826862577760330492647062850849851925340",
|
623
|
+
"10512156247842686783409460795717734694774542185222602679117887145206209285142"
|
624
|
+
],
|
625
|
+
[
|
626
|
+
"1",
|
627
|
+
"0"
|
628
|
+
]
|
629
|
+
],
|
630
|
+
"vk_alphabeta_12": [
|
631
|
+
[
|
632
|
+
[
|
633
|
+
"5151991366823434428398919091000210787450832786814248297320989361921939794156",
|
634
|
+
"15735191313289001022885148627913534790382722933676436876510746491415970766821"
|
635
|
+
],
|
636
|
+
[
|
637
|
+
"3387907257437913904447588318761906430938415556102110876587455322225272831272",
|
638
|
+
"1998779853452712881084781956683721603875246565720647583735935725110674288056"
|
639
|
+
],
|
640
|
+
[
|
641
|
+
"14280074182991498185075387990446437410077692353432005297922275464876153151820",
|
642
|
+
"17092408446352310039633488224969232803092763095456307462247653153107223117633"
|
643
|
+
]
|
644
|
+
],
|
645
|
+
[
|
646
|
+
[
|
647
|
+
"4359046709531668109201634396816565829237358165496082832279660960675584351266",
|
648
|
+
"4511888308846208349307186938266411423935335853916317436093178288331845821336"
|
649
|
+
],
|
650
|
+
[
|
651
|
+
"11429499807090785857812316277335883295048773373068683863667725283965356423273",
|
652
|
+
"16232274853200678548795010078253506586114563833318973594428907292096178657392"
|
653
|
+
],
|
654
|
+
[
|
655
|
+
"18068999605870933925311275504102553573815570223888590384919752303726860800970",
|
656
|
+
"17309569111965782732372130116757295842160193489132771344011460471298173784984"
|
657
|
+
]
|
658
|
+
]
|
659
|
+
],
|
660
|
+
"IC": [
|
661
|
+
[
|
662
|
+
"18693301901828818437917730940595978397160482710354161265484535387752523310572",
|
663
|
+
"17985273354976640088538673802000794244421192643855111089693820179790551470769",
|
664
|
+
"1"
|
665
|
+
],
|
666
|
+
[
|
667
|
+
"21164641723988537620541455173278629777250883365474191521194244273980931825942",
|
668
|
+
"998385854410718613441067082771678946155853656328717326195057262123686425518",
|
669
|
+
"1"
|
670
|
+
],
|
671
|
+
[
|
672
|
+
"21666968581672145768705229094968410656430989593283335488162701230986314747515",
|
673
|
+
"17996457608540683483506630273632100555125353447506062045735279661096094677264",
|
674
|
+
"1"
|
675
|
+
],
|
676
|
+
[
|
677
|
+
"20137761979695192602424300886442379728165712610493092740175904438282083668117",
|
678
|
+
"19184814924890679891263780109959113289320127263583260218200636509492157834679",
|
679
|
+
"1"
|
680
|
+
],
|
681
|
+
[
|
682
|
+
"10943171273393803842589314082509655332154393332394322726077270895078286354146",
|
683
|
+
"10872472035685319847811233167729172672344935625121511932198535224727331126439",
|
684
|
+
"1"
|
685
|
+
],
|
686
|
+
[
|
687
|
+
"13049169779481227658517545034348883391527506091990880778783387628208561946597",
|
688
|
+
"10083689369261379027228809473568899816311684698866922944902456565434209079955",
|
689
|
+
"1"
|
690
|
+
],
|
691
|
+
[
|
692
|
+
"19633516378466409167014413361365552102431118630694133723053441455184566611083",
|
693
|
+
"8059525100726933978719058611146131904598011633549012007359165766216730722269",
|
694
|
+
"1"
|
695
|
+
]
|
696
|
+
]
|
697
|
+
};
|
698
|
+
|
699
|
+
// File generated with https://github.com/iden3/circom
|
700
|
+
// following the instructions from:
|
701
|
+
// https://github.com/vacp2p/zerokit/tree/master/rln#compiling-circuits
|
702
|
+
async function builder(code, options) {
|
703
|
+
options = options || {};
|
704
|
+
let wasmModule;
|
705
|
+
try {
|
706
|
+
wasmModule = await WebAssembly.compile(code);
|
707
|
+
}
|
708
|
+
catch (err) {
|
709
|
+
console.log(err);
|
710
|
+
console.log("\nTry to run circom --c in order to generate c++ code instead\n");
|
711
|
+
throw new Error(err);
|
712
|
+
}
|
713
|
+
let wc;
|
714
|
+
let errStr = "";
|
715
|
+
let msgStr = "";
|
716
|
+
const instance = await WebAssembly.instantiate(wasmModule, {
|
717
|
+
runtime: {
|
718
|
+
exceptionHandler: function (code) {
|
719
|
+
let err;
|
720
|
+
if (code == 1) {
|
721
|
+
err = "Signal not found.\n";
|
722
|
+
}
|
723
|
+
else if (code == 2) {
|
724
|
+
err = "Too many signals set.\n";
|
725
|
+
}
|
726
|
+
else if (code == 3) {
|
727
|
+
err = "Signal already set.\n";
|
728
|
+
}
|
729
|
+
else if (code == 4) {
|
730
|
+
err = "Assert Failed.\n";
|
731
|
+
}
|
732
|
+
else if (code == 5) {
|
733
|
+
err = "Not enough memory.\n";
|
734
|
+
}
|
735
|
+
else if (code == 6) {
|
736
|
+
err = "Input signal array access exceeds the size.\n";
|
737
|
+
}
|
738
|
+
else {
|
739
|
+
err = "Unknown error.\n";
|
740
|
+
}
|
741
|
+
throw new Error(err + errStr);
|
742
|
+
},
|
743
|
+
printErrorMessage: function () {
|
744
|
+
errStr += getMessage() + "\n";
|
745
|
+
// console.error(getMessage());
|
746
|
+
},
|
747
|
+
writeBufferMessage: function () {
|
748
|
+
const msg = getMessage();
|
749
|
+
// Any calls to `log()` will always end with a `\n`, so that's when we print and reset
|
750
|
+
if (msg === "\n") {
|
751
|
+
console.log(msgStr);
|
752
|
+
msgStr = "";
|
753
|
+
}
|
754
|
+
else {
|
755
|
+
// If we've buffered other content, put a space in between the items
|
756
|
+
if (msgStr !== "") {
|
757
|
+
msgStr += " ";
|
758
|
+
}
|
759
|
+
// Then append the message to the message we are creating
|
760
|
+
msgStr += msg;
|
761
|
+
}
|
762
|
+
},
|
763
|
+
showSharedRWMemory: function () {
|
764
|
+
printSharedRWMemory();
|
765
|
+
}
|
766
|
+
}
|
767
|
+
});
|
768
|
+
const sanityCheck = options;
|
769
|
+
// options &&
|
770
|
+
// (
|
771
|
+
// options.sanityCheck ||
|
772
|
+
// options.logGetSignal ||
|
773
|
+
// options.logSetSignal ||
|
774
|
+
// options.logStartComponent ||
|
775
|
+
// options.logFinishComponent
|
776
|
+
// );
|
777
|
+
wc = new WitnessCalculator(instance, sanityCheck);
|
778
|
+
return wc;
|
779
|
+
function getMessage() {
|
780
|
+
var message = "";
|
781
|
+
var c = instance.exports.getMessageChar();
|
782
|
+
while (c != 0) {
|
783
|
+
message += String.fromCharCode(c);
|
784
|
+
c = instance.exports.getMessageChar();
|
785
|
+
}
|
786
|
+
return message;
|
787
|
+
}
|
788
|
+
function printSharedRWMemory() {
|
789
|
+
const shared_rw_memory_size = instance.exports.getFieldNumLen32();
|
790
|
+
const arr = new Uint32Array(shared_rw_memory_size);
|
791
|
+
for (let j = 0; j < shared_rw_memory_size; j++) {
|
792
|
+
arr[shared_rw_memory_size - 1 - j] = instance.exports.readSharedRWMemory(j);
|
793
|
+
}
|
794
|
+
// If we've buffered other content, put a space in between the items
|
795
|
+
if (msgStr !== "") {
|
796
|
+
msgStr += " ";
|
797
|
+
}
|
798
|
+
// Then append the value to the message we are creating
|
799
|
+
msgStr += (fromArray32(arr).toString());
|
800
|
+
}
|
801
|
+
}
|
802
|
+
class WitnessCalculator {
|
803
|
+
constructor(instance, sanityCheck) {
|
804
|
+
this.instance = instance;
|
805
|
+
this.version = this.instance.exports.getVersion();
|
806
|
+
this.n32 = this.instance.exports.getFieldNumLen32();
|
807
|
+
this.instance.exports.getRawPrime();
|
808
|
+
const arr = new Uint32Array(this.n32);
|
809
|
+
for (let i = 0; i < this.n32; i++) {
|
810
|
+
arr[this.n32 - 1 - i] = this.instance.exports.readSharedRWMemory(i);
|
811
|
+
}
|
812
|
+
this.prime = fromArray32(arr);
|
813
|
+
this.witnessSize = this.instance.exports.getWitnessSize();
|
814
|
+
this.sanityCheck = sanityCheck;
|
815
|
+
}
|
816
|
+
circom_version() {
|
817
|
+
return this.instance.exports.getVersion();
|
818
|
+
}
|
819
|
+
async _doCalculateWitness(input, sanityCheck) {
|
820
|
+
//input is assumed to be a map from signals to arrays of bigints
|
821
|
+
this.instance.exports.init((this.sanityCheck || sanityCheck) ? 1 : 0);
|
822
|
+
const keys = Object.keys(input);
|
823
|
+
var input_counter = 0;
|
824
|
+
keys.forEach((k) => {
|
825
|
+
const h = fnvHash(k);
|
826
|
+
const hMSB = parseInt(h.slice(0, 8), 16);
|
827
|
+
const hLSB = parseInt(h.slice(8, 16), 16);
|
828
|
+
const fArr = flatArray(input[k]);
|
829
|
+
let signalSize = this.instance.exports.getInputSignalSize(hMSB, hLSB);
|
830
|
+
if (signalSize < 0) {
|
831
|
+
throw new Error(`Signal ${k} not found\n`);
|
832
|
+
}
|
833
|
+
if (fArr.length < signalSize) {
|
834
|
+
throw new Error(`Not enough values for input signal ${k}\n`);
|
835
|
+
}
|
836
|
+
if (fArr.length > signalSize) {
|
837
|
+
throw new Error(`Too many values for input signal ${k}\n`);
|
838
|
+
}
|
839
|
+
for (let i = 0; i < fArr.length; i++) {
|
840
|
+
const arrFr = toArray32(BigInt(fArr[i]) % this.prime, this.n32);
|
841
|
+
for (let j = 0; j < this.n32; j++) {
|
842
|
+
this.instance.exports.writeSharedRWMemory(j, arrFr[this.n32 - 1 - j]);
|
843
|
+
}
|
844
|
+
try {
|
845
|
+
this.instance.exports.setInputSignal(hMSB, hLSB, i);
|
846
|
+
input_counter++;
|
847
|
+
}
|
848
|
+
catch (err) {
|
849
|
+
// console.log(`After adding signal ${i} of ${k}`)
|
850
|
+
throw new Error(err);
|
851
|
+
}
|
852
|
+
}
|
853
|
+
});
|
854
|
+
if (input_counter < this.instance.exports.getInputSize()) {
|
855
|
+
throw new Error(`Not all inputs have been set. Only ${input_counter} out of ${this.instance.exports.getInputSize()}`);
|
856
|
+
}
|
857
|
+
}
|
858
|
+
async calculateWitness(input, sanityCheck) {
|
859
|
+
const w = [];
|
860
|
+
await this._doCalculateWitness(input, sanityCheck);
|
861
|
+
for (let i = 0; i < this.witnessSize; i++) {
|
862
|
+
this.instance.exports.getWitness(i);
|
863
|
+
const arr = new Uint32Array(this.n32);
|
864
|
+
for (let j = 0; j < this.n32; j++) {
|
865
|
+
arr[this.n32 - 1 - j] = this.instance.exports.readSharedRWMemory(j);
|
866
|
+
}
|
867
|
+
w.push(fromArray32(arr));
|
868
|
+
}
|
869
|
+
return w;
|
870
|
+
}
|
871
|
+
async calculateBinWitness(input, sanityCheck) {
|
872
|
+
const buff32 = new Uint32Array(this.witnessSize * this.n32);
|
873
|
+
const buff = new Uint8Array(buff32.buffer);
|
874
|
+
await this._doCalculateWitness(input, sanityCheck);
|
875
|
+
for (let i = 0; i < this.witnessSize; i++) {
|
876
|
+
this.instance.exports.getWitness(i);
|
877
|
+
const pos = i * this.n32;
|
878
|
+
for (let j = 0; j < this.n32; j++) {
|
879
|
+
buff32[pos + j] = this.instance.exports.readSharedRWMemory(j);
|
880
|
+
}
|
881
|
+
}
|
882
|
+
return buff;
|
883
|
+
}
|
884
|
+
async calculateWTNSBin(input, sanityCheck) {
|
885
|
+
const buff32 = new Uint32Array(this.witnessSize * this.n32 + this.n32 + 11);
|
886
|
+
const buff = new Uint8Array(buff32.buffer);
|
887
|
+
await this._doCalculateWitness(input, sanityCheck);
|
888
|
+
//"wtns"
|
889
|
+
buff[0] = "w".charCodeAt(0);
|
890
|
+
buff[1] = "t".charCodeAt(0);
|
891
|
+
buff[2] = "n".charCodeAt(0);
|
892
|
+
buff[3] = "s".charCodeAt(0);
|
893
|
+
//version 2
|
894
|
+
buff32[1] = 2;
|
895
|
+
//number of sections: 2
|
896
|
+
buff32[2] = 2;
|
897
|
+
//id section 1
|
898
|
+
buff32[3] = 1;
|
899
|
+
const n8 = this.n32 * 4;
|
900
|
+
//id section 1 length in 64bytes
|
901
|
+
const idSection1length = 8 + n8;
|
902
|
+
const idSection1lengthHex = idSection1length.toString(16);
|
903
|
+
buff32[4] = parseInt(idSection1lengthHex.slice(0, 8), 16);
|
904
|
+
buff32[5] = parseInt(idSection1lengthHex.slice(8, 16), 16);
|
905
|
+
//this.n32
|
906
|
+
buff32[6] = n8;
|
907
|
+
//prime number
|
908
|
+
this.instance.exports.getRawPrime();
|
909
|
+
var pos = 7;
|
910
|
+
for (let j = 0; j < this.n32; j++) {
|
911
|
+
buff32[pos + j] = this.instance.exports.readSharedRWMemory(j);
|
912
|
+
}
|
913
|
+
pos += this.n32;
|
914
|
+
// witness size
|
915
|
+
buff32[pos] = this.witnessSize;
|
916
|
+
pos++;
|
917
|
+
//id section 2
|
918
|
+
buff32[pos] = 2;
|
919
|
+
pos++;
|
920
|
+
// section 2 length
|
921
|
+
const idSection2length = n8 * this.witnessSize;
|
922
|
+
const idSection2lengthHex = idSection2length.toString(16);
|
923
|
+
buff32[pos] = parseInt(idSection2lengthHex.slice(0, 8), 16);
|
924
|
+
buff32[pos + 1] = parseInt(idSection2lengthHex.slice(8, 16), 16);
|
925
|
+
pos += 2;
|
926
|
+
for (let i = 0; i < this.witnessSize; i++) {
|
927
|
+
this.instance.exports.getWitness(i);
|
928
|
+
for (let j = 0; j < this.n32; j++) {
|
929
|
+
buff32[pos + j] = this.instance.exports.readSharedRWMemory(j);
|
930
|
+
}
|
931
|
+
pos += this.n32;
|
932
|
+
}
|
933
|
+
return buff;
|
934
|
+
}
|
935
|
+
}
|
936
|
+
function toArray32(rem, size) {
|
937
|
+
const res = []; //new Uint32Array(size); //has no unshift
|
938
|
+
const radix = BigInt(0x100000000);
|
939
|
+
while (rem) {
|
940
|
+
res.unshift(Number(rem % radix));
|
941
|
+
rem = rem / radix;
|
942
|
+
}
|
943
|
+
if (size) {
|
944
|
+
var i = size - res.length;
|
945
|
+
while (i > 0) {
|
946
|
+
res.unshift(0);
|
947
|
+
i--;
|
948
|
+
}
|
949
|
+
}
|
950
|
+
return res;
|
951
|
+
}
|
952
|
+
function fromArray32(arr) {
|
953
|
+
var res = BigInt(0);
|
954
|
+
const radix = BigInt(0x100000000);
|
955
|
+
for (let i = 0; i < arr.length; i++) {
|
956
|
+
res = res * radix + BigInt(arr[i]);
|
957
|
+
}
|
958
|
+
return res;
|
959
|
+
}
|
960
|
+
function flatArray(a) {
|
961
|
+
var res = [];
|
962
|
+
fillArray(res, a);
|
963
|
+
return res;
|
964
|
+
function fillArray(res, a) {
|
965
|
+
if (Array.isArray(a)) {
|
966
|
+
for (let i = 0; i < a.length; i++) {
|
967
|
+
fillArray(res, a[i]);
|
968
|
+
}
|
969
|
+
}
|
970
|
+
else {
|
971
|
+
res.push(a);
|
972
|
+
}
|
973
|
+
}
|
974
|
+
}
|
975
|
+
function fnvHash(str) {
|
976
|
+
const uint64_max = BigInt(2) ** BigInt(64);
|
977
|
+
let hash = BigInt("0xCBF29CE484222325");
|
978
|
+
for (var i = 0; i < str.length; i++) {
|
979
|
+
hash ^= BigInt(str[i].charCodeAt());
|
980
|
+
hash *= BigInt(0x100000001B3);
|
981
|
+
hash %= uint64_max;
|
982
|
+
}
|
983
|
+
let shash = hash.toString(16);
|
984
|
+
let n = 16 - shash.length;
|
985
|
+
shash = '0'.repeat(n).concat(shash);
|
986
|
+
return shash;
|
987
|
+
}
|
988
|
+
|
989
|
+
/**
|
990
|
+
* Concatenate Uint8Arrays
|
991
|
+
* @param input
|
992
|
+
* @returns concatenation of all Uint8Array received as input
|
993
|
+
*/
|
994
|
+
function concatenate(...input) {
|
995
|
+
let totalLength = 0;
|
996
|
+
for (const arr of input) {
|
997
|
+
totalLength += arr.length;
|
998
|
+
}
|
999
|
+
const result = new Uint8Array(totalLength);
|
1000
|
+
let offset = 0;
|
1001
|
+
for (const arr of input) {
|
1002
|
+
result.set(arr, offset);
|
1003
|
+
offset += arr.length;
|
1004
|
+
}
|
1005
|
+
return result;
|
1006
|
+
}
|
1007
|
+
const stringEncoder = new TextEncoder();
|
1008
|
+
const DEPTH = 20;
|
1009
|
+
async function loadWitnessCalculator() {
|
1010
|
+
const url = new URL(new URL('assets/rln-fb4d7b4b.wasm', import.meta.url).href, import.meta.url);
|
1011
|
+
const response = await fetch(url);
|
1012
|
+
return await builder(new Uint8Array(await response.arrayBuffer()), false);
|
1013
|
+
}
|
1014
|
+
async function loadZkey() {
|
1015
|
+
const url = new URL(new URL('assets/rln_final-a641c06e.zkey', import.meta.url).href, import.meta.url);
|
1016
|
+
const response = await fetch(url);
|
1017
|
+
return new Uint8Array(await response.arrayBuffer());
|
1018
|
+
}
|
1019
|
+
/**
|
1020
|
+
* Create an instance of RLN
|
1021
|
+
* @returns RLNInstance
|
1022
|
+
*/
|
1023
|
+
async function create$1() {
|
1024
|
+
await init();
|
1025
|
+
init_panic_hook();
|
1026
|
+
const witnessCalculator = await loadWitnessCalculator();
|
1027
|
+
const zkey = await loadZkey();
|
1028
|
+
const vkey = stringEncoder.encode(JSON.stringify(verificationKey));
|
1029
|
+
const zkRLN = newRLN(DEPTH, zkey, vkey);
|
1030
|
+
return new RLNInstance(zkRLN, witnessCalculator);
|
1031
|
+
}
|
1032
|
+
class MembershipKey {
|
1033
|
+
constructor(IDKey, IDCommitment) {
|
1034
|
+
this.IDKey = IDKey;
|
1035
|
+
this.IDCommitment = IDCommitment;
|
1036
|
+
}
|
1037
|
+
static fromBytes(memKeys) {
|
1038
|
+
const idKey = memKeys.subarray(0, 32);
|
1039
|
+
const idCommitment = memKeys.subarray(32);
|
1040
|
+
return new MembershipKey(idKey, idCommitment);
|
1041
|
+
}
|
1042
|
+
}
|
1043
|
+
// Adapted from https://github.com/feross/buffer
|
1044
|
+
function checkInt(buf, value, offset, ext, max, min) {
|
1045
|
+
if (value > max || value < min)
|
1046
|
+
throw new RangeError('"value" argument is out of bounds');
|
1047
|
+
if (offset + ext > buf.length)
|
1048
|
+
throw new RangeError("Index out of range");
|
1049
|
+
}
|
1050
|
+
const writeUIntLE = function writeUIntLE(buf, value, offset, byteLength, noAssert) {
|
1051
|
+
value = +value;
|
1052
|
+
offset = offset >>> 0;
|
1053
|
+
byteLength = byteLength >>> 0;
|
1054
|
+
if (!noAssert) {
|
1055
|
+
const maxBytes = Math.pow(2, 8 * byteLength) - 1;
|
1056
|
+
checkInt(buf, value, offset, byteLength, maxBytes, 0);
|
1057
|
+
}
|
1058
|
+
let mul = 1;
|
1059
|
+
let i = 0;
|
1060
|
+
buf[offset] = value & 0xff;
|
1061
|
+
while (++i < byteLength && (mul *= 0x100)) {
|
1062
|
+
buf[offset + i] = (value / mul) & 0xff;
|
1063
|
+
}
|
1064
|
+
return buf;
|
1065
|
+
};
|
1066
|
+
const DefaultEpochUnitSeconds = 10; // the rln-relay epoch length in seconds
|
1067
|
+
function toEpoch(timestamp, epochUnitSeconds = DefaultEpochUnitSeconds) {
|
1068
|
+
const unix = Math.floor(timestamp.getTime() / 1000 / epochUnitSeconds);
|
1069
|
+
return writeUIntLE(new Uint8Array(32), unix, 0, 8);
|
1070
|
+
}
|
1071
|
+
const proofOffset = 128;
|
1072
|
+
const rootOffset = proofOffset + 32;
|
1073
|
+
const epochOffset = rootOffset + 32;
|
1074
|
+
const shareXOffset = epochOffset + 32;
|
1075
|
+
const shareYOffset = shareXOffset + 32;
|
1076
|
+
const nullifierOffset = shareYOffset + 32;
|
1077
|
+
const rlnIdentifierOffset = nullifierOffset + 32;
|
1078
|
+
class Proof {
|
1079
|
+
constructor(proofBytes) {
|
1080
|
+
if (proofBytes.length < rlnIdentifierOffset)
|
1081
|
+
throw "invalid proof";
|
1082
|
+
// parse the proof as proof<128> | share_y<32> | nullifier<32> | root<32> | epoch<32> | share_x<32> | rln_identifier<32>
|
1083
|
+
this.proof = proofBytes.subarray(0, proofOffset);
|
1084
|
+
this.merkleRoot = proofBytes.subarray(proofOffset, rootOffset);
|
1085
|
+
this.epoch = proofBytes.subarray(rootOffset, epochOffset);
|
1086
|
+
this.shareX = proofBytes.subarray(epochOffset, shareXOffset);
|
1087
|
+
this.shareY = proofBytes.subarray(shareXOffset, shareYOffset);
|
1088
|
+
this.nullifier = proofBytes.subarray(shareYOffset, nullifierOffset);
|
1089
|
+
this.rlnIdentifier = proofBytes.subarray(nullifierOffset, rlnIdentifierOffset);
|
1090
|
+
}
|
1091
|
+
}
|
1092
|
+
function proofToBytes(p) {
|
1093
|
+
return concatenate(p.proof, p.merkleRoot, p.epoch, p.shareX, p.shareY, p.nullifier, p.rlnIdentifier);
|
1094
|
+
}
|
1095
|
+
class RLNInstance {
|
1096
|
+
constructor(zkRLN, witnessCalculator) {
|
1097
|
+
this.zkRLN = zkRLN;
|
1098
|
+
this.witnessCalculator = witnessCalculator;
|
1099
|
+
}
|
1100
|
+
generateMembershipKey() {
|
1101
|
+
const memKeys = generateMembershipKey(this.zkRLN);
|
1102
|
+
return MembershipKey.fromBytes(memKeys);
|
1103
|
+
}
|
1104
|
+
insertMember(idCommitment) {
|
1105
|
+
insertMember(this.zkRLN, idCommitment);
|
1106
|
+
}
|
1107
|
+
serializeMessage(uint8Msg, memIndex, epoch, idKey) {
|
1108
|
+
// calculate message length
|
1109
|
+
const msgLen = writeUIntLE(new Uint8Array(8), uint8Msg.length, 0, 8);
|
1110
|
+
// Converting index to LE bytes
|
1111
|
+
const memIndexBytes = writeUIntLE(new Uint8Array(8), memIndex, 0, 8);
|
1112
|
+
// [ id_key<32> | id_index<8> | epoch<32> | signal_len<8> | signal<var> ]
|
1113
|
+
return concatenate(idKey, memIndexBytes, epoch, msgLen, uint8Msg);
|
1114
|
+
}
|
1115
|
+
async generateProof(msg, index, epoch, idKey) {
|
1116
|
+
if (epoch == undefined) {
|
1117
|
+
epoch = toEpoch(new Date());
|
1118
|
+
}
|
1119
|
+
else if (epoch instanceof Date) {
|
1120
|
+
epoch = toEpoch(epoch);
|
1121
|
+
}
|
1122
|
+
if (epoch.length != 32)
|
1123
|
+
throw "invalid epoch";
|
1124
|
+
if (idKey.length != 32)
|
1125
|
+
throw "invalid id key";
|
1126
|
+
if (index < 0)
|
1127
|
+
throw "index must be >= 0";
|
1128
|
+
const serialized_msg = this.serializeMessage(msg, index, epoch, idKey);
|
1129
|
+
const rlnWitness = getSerializedRLNWitness(this.zkRLN, serialized_msg);
|
1130
|
+
const inputs = RLNWitnessToJson(this.zkRLN, rlnWitness);
|
1131
|
+
const calculatedWitness = await this.witnessCalculator.calculateWitness(inputs, false); // no sanity check being used in zerokit
|
1132
|
+
const proofBytes = generate_rln_proof_with_witness(this.zkRLN, calculatedWitness, rlnWitness);
|
1133
|
+
return new Proof(proofBytes);
|
1134
|
+
}
|
1135
|
+
verifyProof(proof) {
|
1136
|
+
let pBytes;
|
1137
|
+
if (proof instanceof Uint8Array) {
|
1138
|
+
pBytes = proof;
|
1139
|
+
}
|
1140
|
+
else {
|
1141
|
+
pBytes = proofToBytes(proof);
|
1142
|
+
}
|
1143
|
+
return verifyProof(this.zkRLN, pBytes);
|
1144
|
+
}
|
1145
|
+
}
|
1146
|
+
|
1147
|
+
var rln = /*#__PURE__*/Object.freeze({
|
1148
|
+
__proto__: null,
|
1149
|
+
create: create$1,
|
1150
|
+
MembershipKey: MembershipKey,
|
1151
|
+
toEpoch: toEpoch,
|
1152
|
+
Proof: Proof,
|
1153
|
+
RLNInstance: RLNInstance
|
1154
|
+
});
|
1155
|
+
|
1
1156
|
// reexport the create function, dynamically imported from rln.ts
|
2
1157
|
async function create() {
|
3
1158
|
// A dependency graph that contains any wasm must all be imported
|
4
1159
|
// asynchronously. This file does the single async import, so
|
5
1160
|
// that no one else needs to worry about it again.
|
6
|
-
const rlnModule = await
|
1161
|
+
const rlnModule = await Promise.resolve().then(function () { return rln; });
|
7
1162
|
return await rlnModule.create();
|
8
1163
|
}
|
9
1164
|
|
10
|
-
export { create };
|
1165
|
+
export { MembershipKey, create };
|