Skip to content

Commit b098b43

Browse files
authored
refactor(PR 7): cleanup old error type, redesign str-keyed map with CalcWitness/CalcWitnessPartial traits, cleanup rln-wasm(#416)
1 parent 48d64b8 commit b098b43

17 files changed

Lines changed: 717 additions & 692 deletions

File tree

rln-wasm/examples/index.js

Lines changed: 94 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ async function main() {
4040
try {
4141
rlnInstance = rlnWasm.WasmRLN.newWithParams(zkeyData, graphData);
4242
} catch (error) {
43-
console.error("Initial RLN instance creation error:", error);
43+
console.error("RLN instance creation error:", error);
4444
return;
4545
}
4646
console.log(" - RLN instance created successfully");
@@ -53,7 +53,7 @@ async function main() {
5353
}
5454

5555
console.log("\nGenerating identity keys");
56-
let identity = rlnWasm.Identity.generate();
56+
const identity = rlnWasm.Identity.generate();
5757
const identitySecret = identity.getSecretHash();
5858
const idCommitment = identity.getCommitment();
5959
console.log(" - identity generated successfully");
@@ -65,7 +65,7 @@ async function main() {
6565
console.log(" - user message limit = " + userMessageLimit.debug());
6666

6767
console.log("\nComputing rate commitment");
68-
let rateCommitment = rlnWasm.Hasher.poseidonHashPair(
68+
const rateCommitment = rlnWasm.Hasher.poseidonHashPair(
6969
idCommitment,
7070
userMessageLimit,
7171
);
@@ -125,7 +125,7 @@ async function main() {
125125
);
126126
}
127127

128-
const pathElements = new rlnWasm.VecWasmFr();
128+
const pathElements = rlnWasm.VecWasmFr.new();
129129
pathElements.push(defaultLeaf);
130130
for (let i = 1; i < treeDepth; i++) {
131131
pathElements.push(defaultHashes[i - 1]);
@@ -182,41 +182,28 @@ async function main() {
182182
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
183183
0, 0, 0, 0, 0, 0, 0,
184184
]);
185-
let x1;
186-
try {
187-
x1 = rlnWasm.Hasher.hashToFieldLE(signal1);
188-
} catch (error) {
189-
console.error("Hash signal error:", error);
190-
return;
191-
}
185+
const x1 = rlnWasm.Hasher.hashToFieldLE(signal1);
192186
console.log(" - x1 = " + x1.debug());
193187

194188
console.log("\nHashing epoch");
195189
const epochStr = "test-epoch";
196-
let epoch;
197-
try {
198-
epoch = rlnWasm.Hasher.hashToFieldLE(new TextEncoder().encode(epochStr));
199-
} catch (error) {
200-
console.error("Hash epoch error:", error);
201-
return;
202-
}
190+
const epoch = rlnWasm.Hasher.hashToFieldLE(
191+
new TextEncoder().encode(epochStr),
192+
);
203193
console.log(" - epoch = " + epoch.debug());
204194

205195
console.log("\nHashing RLN identifier");
206196
const rlnIdStr = "test-rln-identifier";
207-
let rlnIdentifier;
208-
try {
209-
rlnIdentifier = rlnWasm.Hasher.hashToFieldLE(
210-
new TextEncoder().encode(rlnIdStr),
211-
);
212-
} catch (error) {
213-
console.error("Hash RLN identifier error:", error);
214-
return;
215-
}
197+
const rlnIdentifier = rlnWasm.Hasher.hashToFieldLE(
198+
new TextEncoder().encode(rlnIdStr),
199+
);
216200
console.log(" - RLN identifier = " + rlnIdentifier.debug());
217201

218202
console.log("\nComputing Poseidon hash for external nullifier");
219-
let externalNullifier = rlnWasm.Hasher.poseidonHashPair(epoch, rlnIdentifier);
203+
const externalNullifier = rlnWasm.Hasher.poseidonHashPair(
204+
epoch,
205+
rlnIdentifier,
206+
);
220207
console.log(" - external nullifier = " + externalNullifier.debug());
221208

222209
console.log("\nCreating first message id");
@@ -230,7 +217,7 @@ async function main() {
230217
);
231218
console.log(" - using 2 out of " + maxOut + " slots");
232219

233-
messageIds1 = new rlnWasm.VecWasmFr();
220+
messageIds1 = rlnWasm.VecWasmFr.new();
234221
messageIds1.push(rlnWasm.WasmFr.fromUint(0));
235222
messageIds1.push(rlnWasm.WasmFr.fromUint(1));
236223
messageIds1.push(rlnWasm.WasmFr.zero());
@@ -245,27 +232,32 @@ async function main() {
245232

246233
console.log("\nCreating first RLN witness");
247234
let witness1;
248-
if (MULTI_MESSAGE_ID) {
249-
witness1 = rlnWasm.WasmRLNWitnessInput.newMulti(
250-
identitySecret,
251-
userMessageLimit,
252-
messageIds1,
253-
pathElements,
254-
identityPathIndex,
255-
x1,
256-
externalNullifier,
257-
selectorUsed1,
258-
);
259-
} else {
260-
witness1 = rlnWasm.WasmRLNWitnessInput.newSingle(
261-
identitySecret,
262-
userMessageLimit,
263-
messageId1,
264-
pathElements,
265-
identityPathIndex,
266-
x1,
267-
externalNullifier,
268-
);
235+
try {
236+
if (MULTI_MESSAGE_ID) {
237+
witness1 = rlnWasm.WasmRLNWitnessInput.newMulti(
238+
identitySecret,
239+
userMessageLimit,
240+
messageIds1,
241+
pathElements,
242+
identityPathIndex,
243+
x1,
244+
externalNullifier,
245+
selectorUsed1,
246+
);
247+
} else {
248+
witness1 = rlnWasm.WasmRLNWitnessInput.newSingle(
249+
identitySecret,
250+
userMessageLimit,
251+
messageId1,
252+
pathElements,
253+
identityPathIndex,
254+
x1,
255+
externalNullifier,
256+
);
257+
}
258+
} catch (error) {
259+
console.error("First witness creation error:", error);
260+
return;
269261
}
270262
console.log(" - first RLN witness created successfully");
271263

@@ -307,28 +299,17 @@ async function main() {
307299
console.log(" - proof values extracted successfully");
308300

309301
if (MULTI_MESSAGE_ID) {
310-
try {
311-
const ys = proofValues1.ys();
312-
console.log(" - ys = " + ys.debug());
313-
} catch (error) {
314-
console.error("Error getting ys:", error);
315-
}
316-
317-
try {
318-
const nullifiers = proofValues1.nullifiers();
319-
console.log(" - nullifiers = " + nullifiers.debug());
320-
} catch (error) {
321-
console.error("Error getting nullifiers:", error);
322-
}
302+
console.log(" - ys = " + proofValues1.ys().debug());
303+
console.log(" - nullifiers = " + proofValues1.nullifiers().debug());
323304
} else {
324-
console.log(" - y = " + proofValues1.y.debug());
325-
console.log(" - nullifier = " + proofValues1.nullifier.debug());
305+
console.log(" - y = " + proofValues1.y().debug());
306+
console.log(" - nullifier = " + proofValues1.nullifier().debug());
326307
}
327308

328-
console.log(" - root = " + proofValues1.root.debug());
329-
console.log(" - x = " + proofValues1.x.debug());
309+
console.log(" - root = " + proofValues1.root().debug());
310+
console.log(" - x = " + proofValues1.x().debug());
330311
console.log(
331-
" - external nullifier = " + proofValues1.externalNullifier.debug(),
312+
" - external nullifier = " + proofValues1.externalNullifier().debug(),
332313
);
333314

334315
console.log("\nWasmRLNProof serialization: WasmRLNProof <-> bytes");
@@ -353,7 +334,13 @@ async function main() {
353334
console.log(
354335
"\nWasmRLNProofValues serialization: WasmRLNProofValues <-> bytes",
355336
);
356-
const serProofValues1 = proofValues1.toBytesLE();
337+
let serProofValues1;
338+
try {
339+
serProofValues1 = proofValues1.toBytesLE();
340+
} catch (error) {
341+
console.error("Proof values serialization error:", error);
342+
return;
343+
}
357344
console.log(
358345
" - serialized proof values = [" + debugUint8Array(serProofValues1) + " ]",
359346
);
@@ -368,11 +355,11 @@ async function main() {
368355
console.log(" - proof values deserialized successfully");
369356
console.log(
370357
" - deserialized external nullifier = " +
371-
deserProofValues1.externalNullifier.debug(),
358+
deserProofValues1.externalNullifier().debug(),
372359
);
373360

374361
console.log("\nVerifying first proof");
375-
const roots = new rlnWasm.VecWasmFr();
362+
const roots = rlnWasm.VecWasmFr.new();
376363
roots.push(computedRoot);
377364
let isValid1;
378365
try {
@@ -397,13 +384,7 @@ async function main() {
397384
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
398385
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
399386
]);
400-
let x2;
401-
try {
402-
x2 = rlnWasm.Hasher.hashToFieldLE(signal2);
403-
} catch (error) {
404-
console.error("Hash second signal error:", error);
405-
return;
406-
}
387+
const x2 = rlnWasm.Hasher.hashToFieldLE(signal2);
407388
console.log(" - x2 = " + x2.debug());
408389

409390
if (MULTI_MESSAGE_ID) {
@@ -422,7 +403,7 @@ async function main() {
422403
console.log(" - using 2 out of " + maxOut + " slots");
423404
console.log(" - duplicated slot id 1");
424405

425-
messageIds2 = new rlnWasm.VecWasmFr();
406+
messageIds2 = rlnWasm.VecWasmFr.new();
426407
messageIds2.push(rlnWasm.WasmFr.fromUint(1));
427408
messageIds2.push(rlnWasm.WasmFr.zero());
428409
messageIds2.push(rlnWasm.WasmFr.fromUint(3));
@@ -437,27 +418,32 @@ async function main() {
437418

438419
console.log("\nCreating second RLN witness");
439420
let witness2;
440-
if (MULTI_MESSAGE_ID) {
441-
witness2 = rlnWasm.WasmRLNWitnessInput.newMulti(
442-
identitySecret,
443-
userMessageLimit,
444-
messageIds2,
445-
pathElements,
446-
identityPathIndex,
447-
x2,
448-
externalNullifier,
449-
selectorUsed2,
450-
);
451-
} else {
452-
witness2 = rlnWasm.WasmRLNWitnessInput.newSingle(
453-
identitySecret,
454-
userMessageLimit,
455-
messageId2,
456-
pathElements,
457-
identityPathIndex,
458-
x2,
459-
externalNullifier,
460-
);
421+
try {
422+
if (MULTI_MESSAGE_ID) {
423+
witness2 = rlnWasm.WasmRLNWitnessInput.newMulti(
424+
identitySecret,
425+
userMessageLimit,
426+
messageIds2,
427+
pathElements,
428+
identityPathIndex,
429+
x2,
430+
externalNullifier,
431+
selectorUsed2,
432+
);
433+
} else {
434+
witness2 = rlnWasm.WasmRLNWitnessInput.newSingle(
435+
identitySecret,
436+
userMessageLimit,
437+
messageId2,
438+
pathElements,
439+
identityPathIndex,
440+
x2,
441+
externalNullifier,
442+
);
443+
}
444+
} catch (error) {
445+
console.error("Second witness creation error:", error);
446+
return;
461447
}
462448
console.log(" - second RLN witness created successfully");
463449

@@ -504,10 +490,15 @@ async function main() {
504490
console.log("Second proof verification failed");
505491
}
506492

507-
console.log("\nGenerating partial proof from partial witness");
493+
console.log("\nCreating partial witness from first witness fields");
508494
let partialWitness;
509495
try {
510-
partialWitness = rlnWasm.WasmRLNPartialWitnessInput.fromWitness(witness1);
496+
partialWitness = rlnWasm.WasmRLNPartialWitnessInput.new(
497+
witness1.getIdentitySecret(),
498+
witness1.getUserMessageLimit(),
499+
witness1.getPathElements(),
500+
witness1.getIdentityPathIndex(),
501+
);
511502
} catch (error) {
512503
console.error("Partial witness creation error:", error);
513504
return;
@@ -589,11 +580,7 @@ async function main() {
589580
console.log("\nVerifying full proof");
590581
let isFullProofValid;
591582
try {
592-
isFullProofValid = rlnInstance.verifyWithRoots(
593-
fullProof,
594-
roots,
595-
x1,
596-
);
583+
isFullProofValid = rlnInstance.verifyWithRoots(fullProof, roots, x1);
597584
} catch (error) {
598585
console.error("Full proof verification error:", error);
599586
return;

0 commit comments

Comments
 (0)