-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathcommon.js
More file actions
165 lines (142 loc) · 4.8 KB
/
Copy pathcommon.js
File metadata and controls
165 lines (142 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { readFileSync } from "fs";
import { fileURLToPath } from "url";
import { dirname, join } from "path";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
export const TREE_DEPTH = 20;
export const MAX_OUT = 4;
export function debugUint8Array(uint8Array) {
return Array.from(uint8Array, (byte) =>
byte.toString(16).padStart(2, "0"),
).join(", ");
}
export async function initRLN(enableMultiMessageId = false) {
const rlnWasm = await import("../pkg/rln_wasm.js");
const wasmPath = join(__dirname, "../pkg/rln_wasm_bg.wasm");
const wasmBytes = readFileSync(wasmPath);
rlnWasm.initSync({ module: wasmBytes });
const resourceDir = enableMultiMessageId
? "../../rln/resources/tree_depth_20/multi_message_id/max_out_4"
: "../../rln/resources/tree_depth_20";
const zkeyPath = join(__dirname, resourceDir, "rln_final.arkzkey");
const graphPath = join(__dirname, resourceDir, "graph.bin");
console.log("Creating RLN instance");
const zkeyData = readFileSync(zkeyPath);
const graphData = readFileSync(graphPath);
let rlnInstance;
try {
rlnInstance = rlnWasm.WasmRLN.newWithParams(zkeyData, graphData);
} catch (error) {
console.error("RLN instance creation error:", error);
throw error;
}
console.log(" - RLN instance created successfully");
console.log(" - circuit tree depth = " + TREE_DEPTH);
if (enableMultiMessageId) {
console.log(" - circuit max out = " + MAX_OUT);
}
return { rlnWasm, rlnInstance };
}
export function createMember(rlnWasm) {
console.log("\nGenerating identity keys");
const identity = rlnWasm.WasmIdentityKeys.generate();
const identitySecret = identity.getSecret();
const idCommitment = identity.getCommitment();
console.log(" - identity generated successfully");
console.log(" - identity secret = " + identitySecret.debug());
console.log(" - id commitment = " + idCommitment.debug());
console.log("\nCreating message limit");
const userMessageLimit = rlnWasm.WasmFr.fromUint(10);
console.log(" - user message limit = " + userMessageLimit.debug());
console.log("\nComputing rate commitment");
const rateCommitment = rlnWasm.WasmHasher.poseidonHashPair(
idCommitment,
userMessageLimit,
);
console.log(" - rate commitment = " + rateCommitment.debug());
return {
identity,
identitySecret,
idCommitment,
userMessageLimit,
rateCommitment,
};
}
export function computeMerkleProof(rlnWasm, rateCommitment) {
console.log("\nComputing Merkle path for stateless mode");
const defaultLeaf = rlnWasm.WasmFr.zero();
const defaultHashes = [];
defaultHashes[0] = rlnWasm.WasmHasher.poseidonHashPair(defaultLeaf, defaultLeaf);
for (let i = 1; i < TREE_DEPTH - 1; i++) {
defaultHashes[i] = rlnWasm.WasmHasher.poseidonHashPair(
defaultHashes[i - 1],
defaultHashes[i - 1],
);
}
const pathElements = rlnWasm.VecWasmFr.new();
pathElements.push(defaultLeaf);
for (let i = 1; i < TREE_DEPTH; i++) {
pathElements.push(defaultHashes[i - 1]);
}
const identityPathIndex = new Uint8Array(TREE_DEPTH);
console.log("\nComputing Merkle root for stateless mode");
console.log(" - computing root for index 0 with rate commitment");
let computedRoot = rlnWasm.WasmHasher.poseidonHashPair(
rateCommitment,
defaultLeaf,
);
for (let i = 1; i < TREE_DEPTH; i++) {
computedRoot = rlnWasm.WasmHasher.poseidonHashPair(
computedRoot,
defaultHashes[i - 1],
);
}
console.log(" - computed root = " + computedRoot.debug());
const roots = rlnWasm.VecWasmFr.new();
roots.push(computedRoot);
return { pathElements, identityPathIndex, roots };
}
export function hashSignal(rlnWasm, signal) {
return rlnWasm.WasmHasher.hashToFieldLE(signal);
}
export function computeExternalNullifier(
rlnWasm,
epochStr = "test-epoch",
rlnIdStr = "test-rln-identifier",
) {
console.log("\nHashing epoch");
const epoch = rlnWasm.WasmHasher.hashToFieldLE(
new TextEncoder().encode(epochStr),
);
console.log(" - epoch = " + epoch.debug());
console.log("\nHashing RLN identifier");
const rlnIdentifier = rlnWasm.WasmHasher.hashToFieldLE(
new TextEncoder().encode(rlnIdStr),
);
console.log(" - RLN identifier = " + rlnIdentifier.debug());
console.log("\nComputing Poseidon hash for external nullifier");
const externalNullifier = rlnWasm.WasmHasher.poseidonHashPair(
epoch,
rlnIdentifier,
);
console.log(" - external nullifier = " + externalNullifier.debug());
return externalNullifier;
}
export function createWitness(
rlnWasm,
member,
merkleProof,
messageId,
x,
externalNullifier,
) {
return rlnWasm.WasmRLNWitnessInput.newSingle(
member.identitySecret,
member.userMessageLimit,
messageId,
merkleProof.pathElements,
merkleProof.identityPathIndex,
x,
externalNullifier,
);
}