@audren-xyz/sdk 0.0.0-stage → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/prover.js ADDED
@@ -0,0 +1,515 @@
1
+ /**
2
+ * `@audren-xyz/sdk/prover` — Exact BN254 Groth16 WASM prover backed by `crates/audren-circuits`
3
+ * (`JoinSplit4AryCircuit`, `DEPTH = 20`, `17,861` R1CS, `2^15` FFT domain, `192 B` proof).
4
+ *
5
+ * Works identically in Node.js and Browser Web Workers with zero `wasm-bindgen` glue
6
+ * using the fixed `4,611 B` binary witness -> `384 B` proof+public-outputs ABI.
7
+ */
8
+ var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
9
+ if (typeof path === "string" && /^\.\.?\//.test(path)) {
10
+ return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
11
+ return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
12
+ });
13
+ }
14
+ return path;
15
+ };
16
+ import { sha256 } from '@noble/hashes/sha2.js';
17
+ import { BN254_FR_MODULUS, CANONICAL_PK_BYTES, CANONICAL_PK_SHA256, CANONICAL_WASM_BYTES, CANONICAL_WASM_SHA256, PROVE_OUTPUT_BYTES, TOTAL_DEPTH_4ARY, WITNESS_TOTAL_BYTES, } from './constants.js';
18
+ import { frToBeBytes } from './poseidon.js';
19
+ import { emptyMerklePath4Ary } from './store.js';
20
+ export function dummyInputWitness(spendSk, assetId, blinding) {
21
+ return {
22
+ spendSk,
23
+ tweak: 0n,
24
+ assetId,
25
+ amount: 0n,
26
+ blinding,
27
+ isDummy: true,
28
+ merklePath: emptyMerklePath4Ary(),
29
+ };
30
+ }
31
+ export function dummyOutputWitness(assetId, blinding) {
32
+ return {
33
+ stealthOwner: 0n,
34
+ assetId,
35
+ amount: 0n,
36
+ blinding,
37
+ };
38
+ }
39
+ const U64_MAX = 0xffffffffffffffffn;
40
+ /**
41
+ * Encode `JoinSplitWitness` into the exact `4,611 B` binary witness ABI consumed by
42
+ * `audren_circuits.wasm` (`encode_joinsplit_witness_bytes` in `crates/audren-circuits`).
43
+ */
44
+ export function encodeJoinsplitWitness(w) {
45
+ if (!w || typeof w !== 'object') {
46
+ throw new RangeError('JoinSplitWitness must be an object');
47
+ }
48
+ if (!Array.isArray(w.inputs) || w.inputs.length !== 2) {
49
+ throw new RangeError(`Expected 2 inputs, got ${Array.isArray(w.inputs) ? w.inputs.length : String(w.inputs)}`);
50
+ }
51
+ if (!Array.isArray(w.outputs) || w.outputs.length !== 3) {
52
+ throw new RangeError(`Expected 3 outputs, got ${Array.isArray(w.outputs) ? w.outputs.length : String(w.outputs)}`);
53
+ }
54
+ const buf = new Uint8Array(WITNESS_TOTAL_BYTES);
55
+ const dv = new DataView(buf.buffer);
56
+ let off = 0;
57
+ try {
58
+ const pushBytes = (b) => {
59
+ if (!(b instanceof Uint8Array)) {
60
+ throw new RangeError(`Expected Uint8Array, got ${String(b)}`);
61
+ }
62
+ buf.set(b, off);
63
+ off += b.byteLength;
64
+ };
65
+ const pushFr = (v, label) => {
66
+ if (typeof v !== 'bigint' || v < 0n || v >= BN254_FR_MODULUS) {
67
+ throw new RangeError(`${label} must be a canonical BN254 Fr bigint in 0..r-1, got ${String(v)}`);
68
+ }
69
+ pushBytes(frToBeBytes(v));
70
+ };
71
+ const pushU64 = (v, label) => {
72
+ if (typeof v !== 'bigint' || v < 0n || v > U64_MAX) {
73
+ throw new RangeError(`${label} must be a u64 bigint in 0..=2^64-1, got ${String(v)}`);
74
+ }
75
+ dv.setBigUint64(off, v, true);
76
+ off += 8;
77
+ };
78
+ const pushU8 = (v, label) => {
79
+ if (!Number.isInteger(v) || v < 0 || v > 0xff) {
80
+ throw new RangeError(`${label} must be a u8 integer in 0..=255, got ${v}`);
81
+ }
82
+ buf[off++] = v;
83
+ };
84
+ pushFr(w.superRoot, 'superRoot');
85
+ pushFr(w.txAssetId, 'txAssetId');
86
+ pushFr(w.publicAssetId, 'publicAssetId');
87
+ pushU64(w.publicAmountOut, 'publicAmountOut');
88
+ pushU64(w.relayerFeeAmount, 'relayerFeeAmount');
89
+ pushFr(w.ctxHash, 'ctxHash');
90
+ pushU8(w.omitNf1 ? 1 : 0, 'omitNf1');
91
+ for (let i = 0; i < w.inputs.length; i++) {
92
+ const inp = w.inputs[i];
93
+ if (!inp || typeof inp !== 'object') {
94
+ throw new RangeError(`inputs[${i}] must be an object`);
95
+ }
96
+ pushFr(inp.spendSk, `inputs[${i}].spendSk`);
97
+ pushFr(inp.tweak, `inputs[${i}].tweak`);
98
+ pushFr(inp.assetId, `inputs[${i}].assetId`);
99
+ pushU64(inp.amount, `inputs[${i}].amount`);
100
+ pushFr(inp.blinding, `inputs[${i}].blinding`);
101
+ pushU8(inp.isDummy ? 1 : 0, `inputs[${i}].isDummy`);
102
+ if (!inp.merklePath ||
103
+ typeof inp.merklePath !== 'object' ||
104
+ !Array.isArray(inp.merklePath.positions) ||
105
+ !Array.isArray(inp.merklePath.siblings)) {
106
+ throw new RangeError(`inputs[${i}].merklePath must provide positions and siblings arrays`);
107
+ }
108
+ if (inp.merklePath.positions.length !== TOTAL_DEPTH_4ARY) {
109
+ throw new RangeError(`Expected ${TOTAL_DEPTH_4ARY} Merkle positions`);
110
+ }
111
+ if (inp.merklePath.siblings.length !== TOTAL_DEPTH_4ARY) {
112
+ throw new RangeError(`Expected ${TOTAL_DEPTH_4ARY} Merkle sibling levels`);
113
+ }
114
+ for (let h = 0; h < TOTAL_DEPTH_4ARY; h++) {
115
+ const pos = inp.merklePath.positions[h];
116
+ if (!Number.isInteger(pos) || pos < 0 || pos > 3) {
117
+ throw new RangeError(`Invalid 4-ary Merkle position at depth ${h}: ${pos}`);
118
+ }
119
+ pushU8(pos, `inputs[${i}].merklePath.positions[${h}]`);
120
+ }
121
+ for (let h = 0; h < TOTAL_DEPTH_4ARY; h++) {
122
+ const sib = inp.merklePath.siblings[h];
123
+ if (!Array.isArray(sib) || sib.length !== 3) {
124
+ throw new RangeError(`Expected 3 siblings at depth ${h}, got ${Array.isArray(sib) ? sib.length : String(sib)}`);
125
+ }
126
+ for (let k = 0; k < 3; k++) {
127
+ pushFr(sib[k], `inputs[${i}].merklePath.siblings[${h}][${k}]`);
128
+ }
129
+ }
130
+ }
131
+ for (let i = 0; i < w.outputs.length; i++) {
132
+ const out = w.outputs[i];
133
+ if (!out || typeof out !== 'object') {
134
+ throw new RangeError(`outputs[${i}] must be an object`);
135
+ }
136
+ pushFr(out.stealthOwner, `outputs[${i}].stealthOwner`);
137
+ pushFr(out.assetId, `outputs[${i}].assetId`);
138
+ pushU64(out.amount, `outputs[${i}].amount`);
139
+ pushFr(out.blinding, `outputs[${i}].blinding`);
140
+ }
141
+ if (off !== WITNESS_TOTAL_BYTES) {
142
+ throw new Error(`Witness encoding length mismatch: ${off} !== ${WITNESS_TOTAL_BYTES}`);
143
+ }
144
+ return buf;
145
+ }
146
+ catch (err) {
147
+ buf.fill(0);
148
+ throw err;
149
+ }
150
+ }
151
+ function bytesToHex(bytes) {
152
+ let out = '';
153
+ for (let i = 0; i < bytes.byteLength; i++) {
154
+ out += bytes[i].toString(16).padStart(2, '0');
155
+ }
156
+ return out;
157
+ }
158
+ function sha256Hex(bytes) {
159
+ if (!(bytes instanceof Uint8Array)) {
160
+ throw new RangeError(`Expected Uint8Array for SHA-256, got ${String(bytes)}`);
161
+ }
162
+ return bytesToHex(sha256(bytes));
163
+ }
164
+ function toStrictUint8ArrayOrArrayBuffer(val, label) {
165
+ if (val instanceof Uint8Array)
166
+ return val;
167
+ if (val instanceof ArrayBuffer)
168
+ return new Uint8Array(val);
169
+ throw new RangeError(`${label} must be a Uint8Array or ArrayBuffer, got ${String(val)}`);
170
+ }
171
+ async function resolveDefaultAssetBytes(relPath, hint) {
172
+ const proc = globalThis.process;
173
+ const isNode = typeof proc?.versions?.node === 'string';
174
+ if (isNode) {
175
+ const fsSpec = 'node:' + 'fs/promises';
176
+ const urlSpec = 'node:' + 'url';
177
+ const fsMod = (await import(__rewriteRelativeImportExtension(/* webpackIgnore: true */ fsSpec)));
178
+ const urlMod = (await import(__rewriteRelativeImportExtension(/* webpackIgnore: true */ urlSpec)));
179
+ const assetFile = urlMod.fileURLToPath(new URL(relPath, import.meta.url));
180
+ const buf = await fsMod.readFile(assetFile);
181
+ return new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
182
+ }
183
+ throw new Error(hint);
184
+ }
185
+ function defaultSiblingPkUrl(wasmUrl) {
186
+ const [base] = wasmUrl.split('?');
187
+ const slash = base.lastIndexOf('/');
188
+ const dir = slash >= 0 ? base.slice(0, slash + 1) : '';
189
+ return `${dir}audren_joinsplit.pk?v=${CANONICAL_PK_SHA256.slice(0, 8)}`;
190
+ }
191
+ /**
192
+ * Stateful handle to the loaded `audren_circuits.wasm` BN254 Groth16 prover instance.
193
+ * Reuse one instance per Worker/process to keep the deserialized ceremony proving key warm in WASM RAM.
194
+ */
195
+ export class AudrenWasmProver {
196
+ exports;
197
+ pkReady = false;
198
+ disposed = false;
199
+ poisoned = false;
200
+ constructor(exports) {
201
+ this.exports = exports;
202
+ }
203
+ assertUsable() {
204
+ if (this.disposed || this.exports === null) {
205
+ throw new Error('AudrenWasmProver has been disposed; call AudrenWasmProver.init() to create a new instance');
206
+ }
207
+ if (this.poisoned) {
208
+ throw new Error('AudrenWasmProver WASM instance trapped (e.g. OOM) and is poisoned; create a fresh instance');
209
+ }
210
+ return this.exports;
211
+ }
212
+ /**
213
+ * Release this prover handle, zeroize WASM linear memory, and drop the `WebAssembly.Instance`
214
+ * reference so residual witness secrets and proving-key buffers can be garbage-collected.
215
+ * Subsequent calls throw immediately instead of reusing freed or stale WASM state.
216
+ */
217
+ dispose() {
218
+ const exp = this.exports;
219
+ this.pkReady = false;
220
+ this.disposed = true;
221
+ this.exports = null;
222
+ if (exp) {
223
+ try {
224
+ new Uint8Array(exp.memory.buffer).fill(0);
225
+ }
226
+ catch {
227
+ // Ignore detached or non-writable WASM linear memory during teardown
228
+ }
229
+ }
230
+ }
231
+ static async init(options = {}) {
232
+ if (!options || typeof options !== 'object') {
233
+ throw new RangeError('AudrenWasmProver.init options must be an object');
234
+ }
235
+ // Validate caller-supplied byte buffers synchronously BEFORE initiating any async fetch/file
236
+ // I/O so invalid `pkCompressed` never abandons an in-flight `wasmPromise` (unhandled rejection).
237
+ const prevalidatedWasm = options.wasmBytes !== undefined
238
+ ? toStrictUint8ArrayOrArrayBuffer(options.wasmBytes, 'wasmBytes')
239
+ : undefined;
240
+ const prevalidatedPk = !options.deferPk && options.pkCompressed !== undefined
241
+ ? toStrictUint8ArrayOrArrayBuffer(options.pkCompressed, 'pkCompressed')
242
+ : undefined;
243
+ const wasmPromise = prevalidatedWasm !== undefined
244
+ ? Promise.resolve(prevalidatedWasm)
245
+ : options.wasmUrl
246
+ ? fetch(options.wasmUrl).then(async (res) => {
247
+ if (!res.ok)
248
+ throw new Error(`Failed to fetch Audren WASM prover: HTTP ${res.status}`);
249
+ return new Uint8Array(await res.arrayBuffer());
250
+ })
251
+ : resolveDefaultAssetBytes('../wasm/audren_circuits.wasm', 'In browser environments pass { wasmUrl: "/wasm/audren_circuits.wasm", pkUrl: "/wasm/audren_joinsplit.pk" } to AudrenWasmProver.init()');
252
+ let pkPromise;
253
+ if (options.deferPk) {
254
+ pkPromise = Promise.resolve(null);
255
+ }
256
+ else if (prevalidatedPk !== undefined) {
257
+ pkPromise = Promise.resolve(prevalidatedPk);
258
+ }
259
+ else if (options.pkUrl) {
260
+ pkPromise = fetch(options.pkUrl).then(async (res) => {
261
+ if (!res.ok)
262
+ throw new Error(`Failed to fetch Audren ceremony PK: HTTP ${res.status}`);
263
+ return new Uint8Array(await res.arrayBuffer());
264
+ });
265
+ }
266
+ else if (options.wasmUrl) {
267
+ const siblingPkUrl = defaultSiblingPkUrl(options.wasmUrl);
268
+ pkPromise = fetch(siblingPkUrl).then(async (res) => {
269
+ if (!res.ok)
270
+ throw new Error(`Failed to fetch Audren ceremony PK (${siblingPkUrl}): HTTP ${res.status}`);
271
+ return new Uint8Array(await res.arrayBuffer());
272
+ });
273
+ }
274
+ else {
275
+ pkPromise = resolveDefaultAssetBytes('../wasm/audren_joinsplit.pk', 'In browser environments pass { pkUrl: "/wasm/audren_joinsplit.pk" } or { pkCompressed } to AudrenWasmProver.init()');
276
+ }
277
+ const [bytes, pkBytes] = await Promise.all([wasmPromise, pkPromise]);
278
+ if (options.verifyWasmHash !== false) {
279
+ if (bytes.byteLength !== CANONICAL_WASM_BYTES) {
280
+ throw new Error(`Audren WASM prover size/SHA-256 mismatch: got ${bytes.byteLength} B, expected ${CANONICAL_WASM_BYTES} B`);
281
+ }
282
+ const wasmDigest = sha256Hex(bytes);
283
+ if (wasmDigest !== CANONICAL_WASM_SHA256) {
284
+ throw new Error(`Audren WASM prover SHA-256 mismatch: got ${wasmDigest}, expected ${CANONICAL_WASM_SHA256}`);
285
+ }
286
+ }
287
+ const wasmBuf = new Uint8Array(bytes.byteLength);
288
+ wasmBuf.set(bytes);
289
+ const { instance } = await WebAssembly.instantiate(wasmBuf, {});
290
+ const prover = new AudrenWasmProver(instance.exports);
291
+ if (pkBytes) {
292
+ try {
293
+ prover.loadProvingKeyCompressed(pkBytes, {
294
+ allowCustomPk: options.verifyPkHash === false,
295
+ });
296
+ }
297
+ catch (err) {
298
+ prover.dispose();
299
+ throw err;
300
+ }
301
+ }
302
+ return prover;
303
+ }
304
+ /**
305
+ * Load a compressed `ProvingKey<Bn254>` (`audren_joinsplit.pk`, curve-validated inside WASM).
306
+ * Enforces `CANONICAL_PK_SHA256` by default (even when `init({ deferPk: true })` was used);
307
+ * pass `{ allowCustomPk: true }` to opt out explicitly for custom circuits/testing.
308
+ */
309
+ loadProvingKeyCompressed(pkCompressed, options) {
310
+ const exp = this.assertUsable();
311
+ if (!(pkCompressed instanceof Uint8Array) || pkCompressed.byteLength === 0) {
312
+ throw new RangeError(`pkCompressed must be a non-empty Uint8Array, got ${pkCompressed instanceof Uint8Array ? pkCompressed.byteLength : String(pkCompressed)}`);
313
+ }
314
+ if (!options?.allowCustomPk) {
315
+ if (pkCompressed.byteLength !== CANONICAL_PK_BYTES) {
316
+ throw new Error(`Audren proving key size/SHA-256 mismatch: got ${pkCompressed.byteLength} B, expected ${CANONICAL_PK_BYTES} B`);
317
+ }
318
+ const digest = bytesToHex(sha256(pkCompressed));
319
+ if (digest !== CANONICAL_PK_SHA256) {
320
+ throw new Error(`Audren proving key SHA-256 mismatch: got ${digest}, expected ${CANONICAL_PK_SHA256}`);
321
+ }
322
+ }
323
+ let ptr = 0;
324
+ try {
325
+ ptr = exp.wasm_alloc(pkCompressed.byteLength);
326
+ if (!ptr) {
327
+ this.poisoned = true;
328
+ throw new Error('wasm_alloc returned null pointer for proving key');
329
+ }
330
+ new Uint8Array(exp.memory.buffer, ptr, pkCompressed.byteLength).set(pkCompressed);
331
+ const rc = exp.wasm_load_pk_compressed(ptr, pkCompressed.byteLength);
332
+ if (rc !== 0) {
333
+ throw new Error(`wasm_load_pk_compressed failed with code ${rc}`);
334
+ }
335
+ this.pkReady = true;
336
+ }
337
+ catch (err) {
338
+ if (err instanceof WebAssembly.RuntimeError) {
339
+ this.poisoned = true;
340
+ this.pkReady = false;
341
+ }
342
+ throw err;
343
+ }
344
+ finally {
345
+ if (ptr) {
346
+ try {
347
+ new Uint8Array(exp.memory.buffer, ptr, pkCompressed.byteLength).fill(0);
348
+ exp.wasm_dealloc(ptr, pkCompressed.byteLength);
349
+ }
350
+ catch {
351
+ this.poisoned = true;
352
+ this.pkReady = false;
353
+ }
354
+ }
355
+ }
356
+ }
357
+ /** Compute canonical Circom / `sol_poseidon` BN254 hash directly inside WASM (1..=12 inputs). */
358
+ poseidonHashWasm(inputsBe32) {
359
+ const exp = this.assertUsable();
360
+ if (!Array.isArray(inputsBe32)) {
361
+ throw new RangeError('inputsBe32 must be an array of 32-byte Uint8Arrays');
362
+ }
363
+ const arity = inputsBe32.length;
364
+ if (arity < 1 || arity > 12) {
365
+ throw new RangeError(`Expected 1..=12 inputs, got ${arity}`);
366
+ }
367
+ for (let i = 0; i < arity; i++) {
368
+ const inp = inputsBe32[i];
369
+ if (!(inp instanceof Uint8Array) || inp.byteLength !== 32) {
370
+ throw new RangeError(`inputsBe32[${i}] must be a 32-byte Uint8Array, got ${inp instanceof Uint8Array ? inp.byteLength : String(inp)}`);
371
+ }
372
+ }
373
+ const inLen = arity * 32;
374
+ let inPtr = 0;
375
+ let outPtr = 0;
376
+ try {
377
+ inPtr = exp.wasm_alloc(inLen);
378
+ outPtr = exp.wasm_alloc(32);
379
+ if (!inPtr || !outPtr) {
380
+ this.poisoned = true;
381
+ throw new Error('wasm_alloc returned null pointer for poseidonHashWasm');
382
+ }
383
+ const memIn = new Uint8Array(exp.memory.buffer, inPtr, inLen);
384
+ for (let i = 0; i < arity; i++)
385
+ memIn.set(inputsBe32[i], i * 32);
386
+ const rc = exp.wasm_poseidon_hash(arity, inPtr, outPtr);
387
+ if (rc !== 0)
388
+ throw new Error(`wasm_poseidon_hash failed with code ${rc}`);
389
+ return new Uint8Array(exp.memory.buffer, outPtr, 32).slice();
390
+ }
391
+ catch (err) {
392
+ if (err instanceof WebAssembly.RuntimeError) {
393
+ this.poisoned = true;
394
+ this.pkReady = false;
395
+ }
396
+ throw err;
397
+ }
398
+ finally {
399
+ if (outPtr) {
400
+ try {
401
+ new Uint8Array(exp.memory.buffer, outPtr, 32).fill(0);
402
+ exp.wasm_dealloc(outPtr, 32);
403
+ }
404
+ catch {
405
+ this.poisoned = true;
406
+ this.pkReady = false;
407
+ }
408
+ }
409
+ if (inPtr) {
410
+ try {
411
+ new Uint8Array(exp.memory.buffer, inPtr, inLen).fill(0);
412
+ exp.wasm_dealloc(inPtr, inLen);
413
+ }
414
+ catch {
415
+ this.poisoned = true;
416
+ this.pkReady = false;
417
+ }
418
+ }
419
+ }
420
+ }
421
+ /**
422
+ * Generate a canonical `192 B` Solana-compressed BN254 Groth16 proof + public outputs.
423
+ * Uses cryptographic randomness from `crypto.getRandomValues` unless an explicit `seed32` is passed.
424
+ */
425
+ proveJoinsplit(witness, seed32) {
426
+ const exp = this.assertUsable();
427
+ if (!this.pkReady) {
428
+ throw new Error('Proving key not initialized: pass pkUrl/pkCompressed or call loadProvingKeyCompressed()');
429
+ }
430
+ if (seed32 !== undefined && (!(seed32 instanceof Uint8Array) || seed32.byteLength !== 32)) {
431
+ throw new RangeError(`seed32 must be a 32-byte Uint8Array, got ${seed32 instanceof Uint8Array ? seed32.byteLength : String(seed32)}`);
432
+ }
433
+ const witnessBytes = encodeJoinsplitWitness(witness);
434
+ const generatedSeed = seed32 === undefined;
435
+ let seed = seed32 ?? null;
436
+ let wPtr = 0;
437
+ let sPtr = 0;
438
+ let oPtr = 0;
439
+ try {
440
+ if (seed === null) {
441
+ const s = new Uint8Array(32);
442
+ globalThis.crypto.getRandomValues(s);
443
+ seed = s;
444
+ }
445
+ wPtr = exp.wasm_alloc(WITNESS_TOTAL_BYTES);
446
+ sPtr = exp.wasm_alloc(32);
447
+ oPtr = exp.wasm_alloc(PROVE_OUTPUT_BYTES);
448
+ if (!wPtr || !sPtr || !oPtr) {
449
+ this.poisoned = true;
450
+ throw new Error('wasm_alloc returned null pointer for proveJoinsplit');
451
+ }
452
+ new Uint8Array(exp.memory.buffer, wPtr, WITNESS_TOTAL_BYTES).set(witnessBytes);
453
+ new Uint8Array(exp.memory.buffer, sPtr, 32).set(seed);
454
+ const rc = exp.wasm_prove_joinsplit(wPtr, WITNESS_TOTAL_BYTES, sPtr, oPtr);
455
+ if (rc === -6) {
456
+ throw new Error('Groth16 witness failed off-chain R1CS verification (rc=-6)');
457
+ }
458
+ if (rc !== 0) {
459
+ throw new Error(`wasm_prove_joinsplit failed with code ${rc}`);
460
+ }
461
+ const raw = new Uint8Array(exp.memory.buffer, oPtr, PROVE_OUTPUT_BYTES).slice();
462
+ return {
463
+ proof192: raw.slice(0, 192),
464
+ nf0: raw.slice(192, 224),
465
+ nf1: raw.slice(224, 256),
466
+ cm0: raw.slice(256, 288),
467
+ cm1: raw.slice(288, 320),
468
+ cm2: raw.slice(320, 352),
469
+ publicInputHash: raw.slice(352, 384),
470
+ };
471
+ }
472
+ catch (err) {
473
+ if (err instanceof WebAssembly.RuntimeError) {
474
+ this.poisoned = true;
475
+ this.pkReady = false;
476
+ }
477
+ throw err;
478
+ }
479
+ finally {
480
+ witnessBytes.fill(0);
481
+ if (generatedSeed && seed)
482
+ seed.fill(0);
483
+ if (oPtr) {
484
+ try {
485
+ new Uint8Array(exp.memory.buffer, oPtr, PROVE_OUTPUT_BYTES).fill(0);
486
+ exp.wasm_dealloc(oPtr, PROVE_OUTPUT_BYTES);
487
+ }
488
+ catch {
489
+ this.poisoned = true;
490
+ this.pkReady = false;
491
+ }
492
+ }
493
+ if (sPtr) {
494
+ try {
495
+ new Uint8Array(exp.memory.buffer, sPtr, 32).fill(0);
496
+ exp.wasm_dealloc(sPtr, 32);
497
+ }
498
+ catch {
499
+ this.poisoned = true;
500
+ this.pkReady = false;
501
+ }
502
+ }
503
+ if (wPtr) {
504
+ try {
505
+ new Uint8Array(exp.memory.buffer, wPtr, WITNESS_TOTAL_BYTES).fill(0);
506
+ exp.wasm_dealloc(wPtr, WITNESS_TOTAL_BYTES);
507
+ }
508
+ catch {
509
+ this.poisoned = true;
510
+ this.pkReady = false;
511
+ }
512
+ }
513
+ }
514
+ }
515
+ }