@unlink-xyz/core 0.1.0 → 0.1.2
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/.eslintrc.json +4 -0
- package/account/zkAccount.test.ts +316 -0
- package/account/zkAccount.ts +222 -0
- package/clients/broadcaster.ts +67 -0
- package/clients/http.ts +94 -0
- package/clients/indexer.ts +150 -0
- package/config.ts +39 -0
- package/core.ts +17 -0
- package/dist/account/railgun-imports-prototype.d.ts +12 -0
- package/dist/account/railgun-imports-prototype.d.ts.map +1 -0
- package/dist/account/railgun-imports-prototype.js +30 -0
- package/dist/clients/indexer.d.ts.map +1 -1
- package/dist/clients/indexer.js +1 -1
- package/dist/state/hydrator.d.ts +16 -0
- package/dist/state/hydrator.d.ts.map +1 -0
- package/dist/state/hydrator.js +18 -0
- package/dist/state/job-store.d.ts +12 -0
- package/dist/state/job-store.d.ts.map +1 -0
- package/dist/state/job-store.js +118 -0
- package/dist/state/jobs.d.ts +50 -0
- package/dist/state/jobs.d.ts.map +1 -0
- package/dist/state/jobs.js +1 -0
- package/dist/state.d.ts +83 -0
- package/dist/state.d.ts.map +1 -0
- package/dist/state.js +171 -0
- package/dist/transactions/deposit.d.ts +0 -2
- package/dist/transactions/deposit.d.ts.map +1 -1
- package/dist/transactions/deposit.js +5 -9
- package/dist/transactions/note-sync.d.ts.map +1 -1
- package/dist/transactions/note-sync.js +1 -1
- package/dist/transactions/shield.d.ts +5 -0
- package/dist/transactions/shield.d.ts.map +1 -0
- package/dist/transactions/shield.js +93 -0
- package/dist/transactions/transact.d.ts +0 -5
- package/dist/transactions/transact.d.ts.map +1 -1
- package/dist/transactions/transact.js +2 -2
- package/dist/transactions/utils.d.ts +10 -0
- package/dist/transactions/utils.d.ts.map +1 -0
- package/dist/transactions/utils.js +17 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/utils/time.d.ts +2 -0
- package/dist/utils/time.d.ts.map +1 -0
- package/dist/utils/time.js +3 -0
- package/dist/utils/witness.d.ts +11 -0
- package/dist/utils/witness.d.ts.map +1 -0
- package/dist/utils/witness.js +19 -0
- package/errors.ts +20 -0
- package/index.ts +17 -0
- package/key-derivation/babyjubjub.ts +11 -0
- package/key-derivation/bech32.test.ts +90 -0
- package/key-derivation/bech32.ts +124 -0
- package/key-derivation/bip32.ts +56 -0
- package/key-derivation/bip39.ts +76 -0
- package/key-derivation/bytes.ts +118 -0
- package/key-derivation/hash.ts +13 -0
- package/key-derivation/index.ts +7 -0
- package/key-derivation/wallet-node.ts +155 -0
- package/keys.ts +47 -0
- package/package.json +4 -5
- package/prover/config.ts +104 -0
- package/prover/index.ts +1 -0
- package/prover/prover.integration.test.ts +162 -0
- package/prover/prover.test.ts +309 -0
- package/prover/prover.ts +405 -0
- package/prover/registry.test.ts +90 -0
- package/prover/registry.ts +82 -0
- package/schema.ts +17 -0
- package/setup-artifacts.sh +57 -0
- package/state/index.ts +2 -0
- package/state/merkle/hydrator.ts +69 -0
- package/state/merkle/index.ts +12 -0
- package/state/merkle/merkle-tree.test.ts +50 -0
- package/state/merkle/merkle-tree.ts +163 -0
- package/state/store/ciphertext-store.ts +28 -0
- package/state/store/index.ts +24 -0
- package/state/store/job-store.ts +162 -0
- package/state/store/jobs.ts +64 -0
- package/state/store/leaf-store.ts +39 -0
- package/state/store/note-store.ts +177 -0
- package/state/store/nullifier-store.ts +39 -0
- package/state/store/records.ts +61 -0
- package/state/store/root-store.ts +34 -0
- package/state/store/store.ts +25 -0
- package/state.test.ts +235 -0
- package/storage/index.ts +3 -0
- package/storage/indexeddb.test.ts +99 -0
- package/storage/indexeddb.ts +235 -0
- package/storage/memory.test.ts +59 -0
- package/storage/memory.ts +93 -0
- package/transactions/deposit.test.ts +160 -0
- package/transactions/deposit.ts +227 -0
- package/transactions/index.ts +20 -0
- package/transactions/note-sync.test.ts +155 -0
- package/transactions/note-sync.ts +452 -0
- package/transactions/reconcile.ts +73 -0
- package/transactions/transact.test.ts +451 -0
- package/transactions/transact.ts +811 -0
- package/transactions/types.ts +141 -0
- package/tsconfig.json +14 -0
- package/types/global.d.ts +15 -0
- package/types.ts +24 -0
- package/utils/async.ts +15 -0
- package/utils/bigint.ts +34 -0
- package/utils/crypto.test.ts +69 -0
- package/utils/crypto.ts +58 -0
- package/utils/json-codec.ts +38 -0
- package/utils/polling.ts +6 -0
- package/utils/signature.ts +16 -0
- package/utils/validators.test.ts +64 -0
- package/utils/validators.ts +86 -0
package/prover/prover.ts
ADDED
|
@@ -0,0 +1,405 @@
|
|
|
1
|
+
import { Groth16Proof, PublicSignals } from "snarkjs";
|
|
2
|
+
import * as snarkjs from "snarkjs";
|
|
3
|
+
|
|
4
|
+
import { CoreError } from "../errors.js";
|
|
5
|
+
import {
|
|
6
|
+
ArtifactType,
|
|
7
|
+
buildArtifactUrl,
|
|
8
|
+
DEFAULT_CONFIG,
|
|
9
|
+
getLocalArtifactPath,
|
|
10
|
+
Runtime,
|
|
11
|
+
} from "./config.js";
|
|
12
|
+
import {
|
|
13
|
+
getCircuitConfig,
|
|
14
|
+
SUPPORTED_CIRCUITS,
|
|
15
|
+
type CircuitConfig,
|
|
16
|
+
} from "./registry.js";
|
|
17
|
+
|
|
18
|
+
// Cache for loaded artifacts to avoid reloading 18MB of files on every call
|
|
19
|
+
// Now keyed by circuit name to support multiple circuits
|
|
20
|
+
const artifactCache: Record<
|
|
21
|
+
string,
|
|
22
|
+
{
|
|
23
|
+
wasm: Uint8Array;
|
|
24
|
+
zkey: Uint8Array;
|
|
25
|
+
vkey: VerificationKey;
|
|
26
|
+
}
|
|
27
|
+
> = {};
|
|
28
|
+
|
|
29
|
+
export interface JoinsplitProof {
|
|
30
|
+
readonly proof: Groth16Proof;
|
|
31
|
+
readonly publicSignals: PublicSignals;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface VerificationKey {
|
|
35
|
+
readonly protocol: string;
|
|
36
|
+
readonly curve: string;
|
|
37
|
+
readonly nPublic: number;
|
|
38
|
+
readonly vk_alpha_1: string[];
|
|
39
|
+
readonly vk_beta_2: string[][];
|
|
40
|
+
readonly vk_gamma_2: string[][];
|
|
41
|
+
readonly vk_delta_2: string[][];
|
|
42
|
+
readonly vk_alphabeta_12: string[][][];
|
|
43
|
+
readonly IC: string[][];
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface JoinsplitProofInput {
|
|
47
|
+
readonly merkleRoot: bigint;
|
|
48
|
+
readonly boundParamsHash: bigint;
|
|
49
|
+
readonly nullifiers: bigint[];
|
|
50
|
+
readonly commitmentsOut: bigint[];
|
|
51
|
+
readonly token: bigint;
|
|
52
|
+
readonly publicKey: bigint[];
|
|
53
|
+
readonly signature: bigint[];
|
|
54
|
+
readonly randomIn: bigint[];
|
|
55
|
+
readonly valueIn: bigint[];
|
|
56
|
+
readonly pathElements: bigint[][];
|
|
57
|
+
readonly leavesIndices: bigint[];
|
|
58
|
+
readonly nullifyingKey: bigint;
|
|
59
|
+
readonly npkOut: bigint[];
|
|
60
|
+
readonly valueOut: bigint[];
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Select the appropriate circuit based on input/output dimensions
|
|
65
|
+
* @param inputs Number of inputs (nullifiers)
|
|
66
|
+
* @param outputs Number of outputs (commitments)
|
|
67
|
+
* @returns Circuit configuration
|
|
68
|
+
* @throws CoreError if no matching circuit is found
|
|
69
|
+
*/
|
|
70
|
+
function selectCircuit(inputs: number, outputs: number): CircuitConfig {
|
|
71
|
+
const config = getCircuitConfig(inputs, outputs);
|
|
72
|
+
|
|
73
|
+
if (!config) {
|
|
74
|
+
throw new CoreError(
|
|
75
|
+
`No circuit available for ${inputs} inputs and ${outputs} outputs. ` +
|
|
76
|
+
`Supported circuits: ${SUPPORTED_CIRCUITS.join(", ")}`,
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return config;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Fetch circuit artifacts for a specific circuit
|
|
85
|
+
* Supports both browser (remote) and Node.js (local) environments
|
|
86
|
+
* @param circuitName Full circuit name (e.g., "joinsplit_2x3_16")
|
|
87
|
+
* @returns Artifacts object with wasm, zkey, and vkey
|
|
88
|
+
*/
|
|
89
|
+
async function fetchCircuitArtifacts(circuitName: string) {
|
|
90
|
+
// Check cache first
|
|
91
|
+
if (artifactCache[circuitName]) {
|
|
92
|
+
return artifactCache[circuitName];
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
let wasm: Uint8Array | undefined;
|
|
96
|
+
let zkey: Uint8Array | undefined;
|
|
97
|
+
let vkey: VerificationKey | undefined;
|
|
98
|
+
|
|
99
|
+
// Browser environment: fetch from artifact server
|
|
100
|
+
if (Runtime.isBrowser()) {
|
|
101
|
+
try {
|
|
102
|
+
const baseUrl = DEFAULT_CONFIG.rpcUrl;
|
|
103
|
+
|
|
104
|
+
const wasmUrl = buildArtifactUrl(circuitName, ArtifactType.WASM, baseUrl);
|
|
105
|
+
const zkeyUrl = buildArtifactUrl(circuitName, ArtifactType.ZKEY, baseUrl);
|
|
106
|
+
const vkeyUrl = buildArtifactUrl(circuitName, ArtifactType.VKEY, baseUrl);
|
|
107
|
+
|
|
108
|
+
const [wasmRes, zkeyRes, vkeyRes] = await Promise.all([
|
|
109
|
+
fetch(wasmUrl),
|
|
110
|
+
fetch(zkeyUrl),
|
|
111
|
+
fetch(vkeyUrl),
|
|
112
|
+
]);
|
|
113
|
+
|
|
114
|
+
if (!wasmRes.ok || !zkeyRes.ok || !vkeyRes.ok) {
|
|
115
|
+
throw new CoreError(
|
|
116
|
+
`Failed to fetch artifacts for ${circuitName} from ${baseUrl}. ` +
|
|
117
|
+
`Status: wasm=${wasmRes.status}, zkey=${zkeyRes.status}, vkey=${vkeyRes.status}`,
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
wasm = new Uint8Array(await wasmRes.arrayBuffer());
|
|
122
|
+
zkey = new Uint8Array(await zkeyRes.arrayBuffer());
|
|
123
|
+
vkey = await vkeyRes.json();
|
|
124
|
+
} catch (error) {
|
|
125
|
+
throw new CoreError(
|
|
126
|
+
`Failed to fetch artifacts from server for ${circuitName}: ${(error as Error).message}`,
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
// Node.js environment: try local file paths
|
|
131
|
+
else {
|
|
132
|
+
const wasmCandidates = getLocalArtifactPath(circuitName, ArtifactType.WASM);
|
|
133
|
+
const zkeyCandidates = getLocalArtifactPath(circuitName, ArtifactType.ZKEY);
|
|
134
|
+
const vkeyCandidates = getLocalArtifactPath(circuitName, ArtifactType.VKEY);
|
|
135
|
+
|
|
136
|
+
let wasmLoaded = false;
|
|
137
|
+
for (const candidate of wasmCandidates) {
|
|
138
|
+
try {
|
|
139
|
+
wasm = await readBinaryResource(candidate);
|
|
140
|
+
wasmLoaded = true;
|
|
141
|
+
break;
|
|
142
|
+
} catch {
|
|
143
|
+
// Try next candidate
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (!wasmLoaded) {
|
|
148
|
+
throw new CoreError(
|
|
149
|
+
`Could not find WASM artifact for ${circuitName}. ` +
|
|
150
|
+
`Please run 'pnpm run build:all' in the circuits package.`,
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// Load zkey
|
|
155
|
+
let zkeyLoaded = false;
|
|
156
|
+
for (const candidate of zkeyCandidates) {
|
|
157
|
+
try {
|
|
158
|
+
zkey = await readBinaryResource(candidate);
|
|
159
|
+
zkeyLoaded = true;
|
|
160
|
+
break;
|
|
161
|
+
} catch {
|
|
162
|
+
// Try next candidate
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
if (!zkeyLoaded) {
|
|
167
|
+
throw new CoreError(
|
|
168
|
+
`Could not find zkey artifact for ${circuitName}. ` +
|
|
169
|
+
`Please run 'pnpm run build:all' in the circuits package.`,
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// Load vkey
|
|
174
|
+
let vkeyLoaded = false;
|
|
175
|
+
for (const candidate of vkeyCandidates) {
|
|
176
|
+
try {
|
|
177
|
+
const vkeyText = await readTextResource(candidate);
|
|
178
|
+
vkey = JSON.parse(vkeyText);
|
|
179
|
+
vkeyLoaded = true;
|
|
180
|
+
break;
|
|
181
|
+
} catch {
|
|
182
|
+
// Try next candidate
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
if (!vkeyLoaded) {
|
|
187
|
+
throw new CoreError(
|
|
188
|
+
`Could not find verification key for ${circuitName}. ` +
|
|
189
|
+
`Please run 'pnpm run build:all' in the circuits package.`,
|
|
190
|
+
);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// Verify all artifacts were loaded
|
|
195
|
+
if (!wasm || !zkey || !vkey) {
|
|
196
|
+
throw new CoreError(
|
|
197
|
+
`Failed to load all artifacts for ${circuitName}. ` +
|
|
198
|
+
`Missing: ${!wasm ? "wasm " : ""}${!zkey ? "zkey " : ""}${!vkey ? "vkey" : ""}`,
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// Cache the artifacts for future calls
|
|
203
|
+
// TypeScript now knows these are defined after the check above
|
|
204
|
+
const artifacts = { wasm, zkey, vkey };
|
|
205
|
+
artifactCache[circuitName] = artifacts;
|
|
206
|
+
|
|
207
|
+
return artifacts;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
async function readBinaryResource(relativePath: string): Promise<Uint8Array> {
|
|
211
|
+
const url = new URL(relativePath, import.meta.url);
|
|
212
|
+
|
|
213
|
+
try {
|
|
214
|
+
if (url.protocol === "file:") {
|
|
215
|
+
const fs = (await import("fs")).default;
|
|
216
|
+
const readPromise: Promise<Buffer> = new Promise((resolve, reject) => {
|
|
217
|
+
fs.readFile(url.pathname, (err, data) => {
|
|
218
|
+
if (err) {
|
|
219
|
+
reject(err);
|
|
220
|
+
} else {
|
|
221
|
+
resolve(data);
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
});
|
|
225
|
+
const buf = await readPromise;
|
|
226
|
+
return new Uint8Array(buf);
|
|
227
|
+
}
|
|
228
|
+
const response = await fetch(url);
|
|
229
|
+
if (!response.ok) {
|
|
230
|
+
throw new CoreError(
|
|
231
|
+
`failed to fetch ${relativePath}: ${response.statusText}`,
|
|
232
|
+
);
|
|
233
|
+
}
|
|
234
|
+
return new Uint8Array(await response.arrayBuffer());
|
|
235
|
+
} catch (error) {
|
|
236
|
+
throw new CoreError(
|
|
237
|
+
`failed to read binary resource ${relativePath}: ${
|
|
238
|
+
(error as Error).message
|
|
239
|
+
}`,
|
|
240
|
+
);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
async function readTextResource(relativePath: string): Promise<string> {
|
|
245
|
+
const url = new URL(relativePath, import.meta.url);
|
|
246
|
+
if (url.protocol === "file:") {
|
|
247
|
+
const fs = (await import("fs")).default;
|
|
248
|
+
const readPromise: Promise<Buffer> = new Promise((resolve, reject) => {
|
|
249
|
+
fs.readFile(url.pathname, (err, data) => {
|
|
250
|
+
if (err) {
|
|
251
|
+
reject(err);
|
|
252
|
+
} else {
|
|
253
|
+
resolve(data);
|
|
254
|
+
}
|
|
255
|
+
});
|
|
256
|
+
});
|
|
257
|
+
const buf = await readPromise;
|
|
258
|
+
return buf.toString("utf-8");
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
const response = await fetch(url);
|
|
262
|
+
if (!response.ok) {
|
|
263
|
+
throw new CoreError(
|
|
264
|
+
`failed to fetch ${relativePath}: ${response.statusText}`,
|
|
265
|
+
);
|
|
266
|
+
}
|
|
267
|
+
return response.text();
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
export async function proveTransaction(
|
|
271
|
+
input: JoinsplitProofInput,
|
|
272
|
+
): Promise<JoinsplitProof> {
|
|
273
|
+
try {
|
|
274
|
+
// Auto-detect circuit based on input dimensions
|
|
275
|
+
const nInputs = input.nullifiers.length;
|
|
276
|
+
const nOutputs = input.commitmentsOut.length;
|
|
277
|
+
|
|
278
|
+
const circuit = selectCircuit(nInputs, nOutputs);
|
|
279
|
+
|
|
280
|
+
// Fetch circuit artifacts (wasm and zkey)
|
|
281
|
+
const { wasm, vkey, zkey } = await fetchCircuitArtifacts(circuit.name);
|
|
282
|
+
|
|
283
|
+
const inputSignals = input as unknown as Record<
|
|
284
|
+
string,
|
|
285
|
+
bigint | bigint[] | string
|
|
286
|
+
>;
|
|
287
|
+
|
|
288
|
+
const { proof, publicSignals } = await snarkjs.groth16.fullProve(
|
|
289
|
+
inputSignals,
|
|
290
|
+
wasm,
|
|
291
|
+
zkey,
|
|
292
|
+
);
|
|
293
|
+
|
|
294
|
+
// Additional verification step (optional)
|
|
295
|
+
const isValid = await snarkjs.groth16.verify(vkey, publicSignals, proof);
|
|
296
|
+
if (!isValid) {
|
|
297
|
+
throw new CoreError("generated proof is invalid");
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
return {
|
|
301
|
+
proof,
|
|
302
|
+
publicSignals,
|
|
303
|
+
};
|
|
304
|
+
} catch (error) {
|
|
305
|
+
throw new CoreError("proof generation failed: " + (error as Error).message);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* Get verification key for a specific circuit configuration
|
|
311
|
+
* @param inputs Number of inputs (nullifiers)
|
|
312
|
+
* @param outputs Number of outputs (commitments)
|
|
313
|
+
* @returns Verification key for the circuit
|
|
314
|
+
*/
|
|
315
|
+
export async function getVerificationKey(
|
|
316
|
+
inputs: number,
|
|
317
|
+
outputs: number,
|
|
318
|
+
): Promise<VerificationKey> {
|
|
319
|
+
try {
|
|
320
|
+
const circuit = selectCircuit(inputs, outputs);
|
|
321
|
+
const { vkey } = await fetchCircuitArtifacts(circuit.name);
|
|
322
|
+
return vkey;
|
|
323
|
+
} catch (error) {
|
|
324
|
+
throw new CoreError(
|
|
325
|
+
"failed to load verification key: " + (error as Error).message,
|
|
326
|
+
);
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* Verify a proof with public signals
|
|
332
|
+
* Auto-detects the circuit from the public signals structure
|
|
333
|
+
* @param proof Groth16 proof
|
|
334
|
+
* @param publicSignals Public signals array
|
|
335
|
+
* @returns True if proof is valid
|
|
336
|
+
*/
|
|
337
|
+
export async function verifyProof(
|
|
338
|
+
proof: Groth16Proof,
|
|
339
|
+
publicSignals: PublicSignals,
|
|
340
|
+
): Promise<boolean> {
|
|
341
|
+
try {
|
|
342
|
+
// Public signals structure: [merkleRoot, boundParamsHash, ...nullifiers, ...commitments]
|
|
343
|
+
// We need to determine the circuit from the length
|
|
344
|
+
// This is tricky without additional context, so we'll need to try to infer
|
|
345
|
+
// or require explicit circuit specification
|
|
346
|
+
|
|
347
|
+
// For now, extract nullifier and commitment counts from the public signals
|
|
348
|
+
// The caller should know this information, but we can make an educated guess
|
|
349
|
+
// based on the total length and circuit registry
|
|
350
|
+
|
|
351
|
+
// Length = 2 (root + hash) + nInputs + nOutputs
|
|
352
|
+
const totalLength = publicSignals.length;
|
|
353
|
+
const variableCount = totalLength - 2; // Remove merkleRoot and boundParamsHash
|
|
354
|
+
|
|
355
|
+
// Try to find a matching circuit
|
|
356
|
+
let vkey: VerificationKey | null = null;
|
|
357
|
+
for (const circuitKey of SUPPORTED_CIRCUITS) {
|
|
358
|
+
const parts = circuitKey.split("x");
|
|
359
|
+
const config = getCircuitConfig(parseInt(parts[0]!), parseInt(parts[1]!));
|
|
360
|
+
if (config && config.inputs + config.outputs === variableCount) {
|
|
361
|
+
const artifacts = await fetchCircuitArtifacts(config.name);
|
|
362
|
+
vkey = artifacts.vkey;
|
|
363
|
+
break;
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
if (!vkey) {
|
|
368
|
+
throw new CoreError(
|
|
369
|
+
`Could not determine circuit from ${totalLength} public signals`,
|
|
370
|
+
);
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
return await snarkjs.groth16.verify(vkey, publicSignals, proof);
|
|
374
|
+
} catch (error) {
|
|
375
|
+
throw new CoreError(
|
|
376
|
+
"proof verification failed: " + (error as Error).message,
|
|
377
|
+
);
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/**
|
|
382
|
+
* Verify a proof with explicit circuit specification
|
|
383
|
+
* Preferred method when you know the circuit dimensions
|
|
384
|
+
* @param proof Groth16 proof
|
|
385
|
+
* @param publicSignals Public signals array
|
|
386
|
+
* @param inputs Number of inputs (nullifiers)
|
|
387
|
+
* @param outputs Number of outputs (commitments)
|
|
388
|
+
* @returns True if proof is valid
|
|
389
|
+
*/
|
|
390
|
+
export async function verifyProofWithCircuit(
|
|
391
|
+
proof: Groth16Proof,
|
|
392
|
+
publicSignals: PublicSignals,
|
|
393
|
+
inputs: number,
|
|
394
|
+
outputs: number,
|
|
395
|
+
): Promise<boolean> {
|
|
396
|
+
try {
|
|
397
|
+
const circuit = selectCircuit(inputs, outputs);
|
|
398
|
+
const { vkey } = await fetchCircuitArtifacts(circuit.name);
|
|
399
|
+
return await snarkjs.groth16.verify(vkey, publicSignals, proof);
|
|
400
|
+
} catch (error) {
|
|
401
|
+
throw new CoreError(
|
|
402
|
+
"proof verification failed: " + (error as Error).message,
|
|
403
|
+
);
|
|
404
|
+
}
|
|
405
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
CIRCUIT_REGISTRY,
|
|
5
|
+
getCircuitConfig,
|
|
6
|
+
isCircuitSupported,
|
|
7
|
+
SUPPORTED_CIRCUITS,
|
|
8
|
+
type CircuitKey,
|
|
9
|
+
} from "./registry.js";
|
|
10
|
+
|
|
11
|
+
describe("Circuit Registry", () => {
|
|
12
|
+
it("should have all expected circuits", () => {
|
|
13
|
+
const expectedCircuits: CircuitKey[] = ["1x1", "1x2", "2x3", "3x3"];
|
|
14
|
+
expect(SUPPORTED_CIRCUITS).toEqual(
|
|
15
|
+
expect.arrayContaining(expectedCircuits),
|
|
16
|
+
);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it("should correctly map circuit keys to configurations", () => {
|
|
20
|
+
const config2x3 = CIRCUIT_REGISTRY["2x3"];
|
|
21
|
+
expect(config2x3).toBeDefined();
|
|
22
|
+
expect(config2x3.name).toBe("joinsplit_2x3_16");
|
|
23
|
+
expect(config2x3.inputs).toBe(2);
|
|
24
|
+
expect(config2x3.outputs).toBe(3);
|
|
25
|
+
expect(config2x3.depth).toBe(16);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
describe("getCircuitConfig", () => {
|
|
29
|
+
it("should return config for valid circuit dimensions", () => {
|
|
30
|
+
const config = getCircuitConfig(2, 3);
|
|
31
|
+
expect(config).toBeDefined();
|
|
32
|
+
expect(config?.name).toBe("joinsplit_2x3_16");
|
|
33
|
+
expect(config?.inputs).toBe(2);
|
|
34
|
+
expect(config?.outputs).toBe(3);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("should return undefined for unsupported dimensions", () => {
|
|
38
|
+
const config = getCircuitConfig(4, 5);
|
|
39
|
+
expect(config).toBeUndefined();
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it("should handle all supported circuit dimensions", () => {
|
|
43
|
+
const testCases = [
|
|
44
|
+
{ inputs: 1, outputs: 1, name: "joinsplit_1x1_16" },
|
|
45
|
+
{ inputs: 1, outputs: 2, name: "joinsplit_1x2_16" },
|
|
46
|
+
{ inputs: 2, outputs: 3, name: "joinsplit_2x3_16" },
|
|
47
|
+
{ inputs: 3, outputs: 3, name: "joinsplit_3x3_16" },
|
|
48
|
+
];
|
|
49
|
+
|
|
50
|
+
for (const testCase of testCases) {
|
|
51
|
+
const config = getCircuitConfig(testCase.inputs, testCase.outputs);
|
|
52
|
+
expect(config).toBeDefined();
|
|
53
|
+
expect(config?.name).toBe(testCase.name);
|
|
54
|
+
expect(config?.inputs).toBe(testCase.inputs);
|
|
55
|
+
expect(config?.outputs).toBe(testCase.outputs);
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
describe("isCircuitSupported", () => {
|
|
61
|
+
it("should return true for supported circuits", () => {
|
|
62
|
+
expect(isCircuitSupported(1, 1)).toBe(true);
|
|
63
|
+
expect(isCircuitSupported(1, 2)).toBe(true);
|
|
64
|
+
expect(isCircuitSupported(2, 3)).toBe(true);
|
|
65
|
+
expect(isCircuitSupported(3, 3)).toBe(true);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it("should return false for unsupported circuits", () => {
|
|
69
|
+
expect(isCircuitSupported(4, 4)).toBe(false);
|
|
70
|
+
expect(isCircuitSupported(0, 0)).toBe(false);
|
|
71
|
+
expect(isCircuitSupported(1, 3)).toBe(false);
|
|
72
|
+
expect(isCircuitSupported(2, 2)).toBe(false);
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("should have consistent registry entries", () => {
|
|
77
|
+
for (const [key, config] of Object.entries(CIRCUIT_REGISTRY)) {
|
|
78
|
+
// Key should match inputs x outputs
|
|
79
|
+
const [inputs, outputs] = key.split("x").map(Number);
|
|
80
|
+
expect(config.inputs).toBe(inputs);
|
|
81
|
+
expect(config.outputs).toBe(outputs);
|
|
82
|
+
|
|
83
|
+
// Name should contain the key
|
|
84
|
+
expect(config.name).toContain(`${inputs}x${outputs}`);
|
|
85
|
+
|
|
86
|
+
// Depth should be 16 for all current circuits
|
|
87
|
+
expect(config.depth).toBe(16);
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
});
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
// AUTO-GENERATED FILE - DO NOT EDIT MANUALLY
|
|
2
|
+
// Generated by scripts/generate-registry.ts
|
|
3
|
+
// Source: circuits/circuits.json
|
|
4
|
+
// Generated at: 2025-11-25T13:26:58.576Z
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Circuit registry for automatic prover selection
|
|
8
|
+
* Maps circuit dimensions (inputs x outputs) to circuit configurations
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
export interface CircuitConfig {
|
|
12
|
+
/** Full circuit name (e.g., "joinsplit_2x3_16") */
|
|
13
|
+
readonly name: string;
|
|
14
|
+
/** Number of inputs (nullifiers) */
|
|
15
|
+
readonly inputs: number;
|
|
16
|
+
/** Number of outputs (commitments) */
|
|
17
|
+
readonly outputs: number;
|
|
18
|
+
/** Merkle tree depth */
|
|
19
|
+
readonly depth: number;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export type CircuitKey = "3x3" | "2x3" | "1x2" | "1x1";
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Registry of available circuits
|
|
26
|
+
* Auto-generated from circuits/circuits.json
|
|
27
|
+
*/
|
|
28
|
+
export const CIRCUIT_REGISTRY: Record<CircuitKey, CircuitConfig> = {
|
|
29
|
+
"3x3": {
|
|
30
|
+
name: "joinsplit_3x3_16",
|
|
31
|
+
inputs: 3,
|
|
32
|
+
outputs: 3,
|
|
33
|
+
depth: 16,
|
|
34
|
+
},
|
|
35
|
+
"2x3": {
|
|
36
|
+
name: "joinsplit_2x3_16",
|
|
37
|
+
inputs: 2,
|
|
38
|
+
outputs: 3,
|
|
39
|
+
depth: 16,
|
|
40
|
+
},
|
|
41
|
+
"1x2": {
|
|
42
|
+
name: "joinsplit_1x2_16",
|
|
43
|
+
inputs: 1,
|
|
44
|
+
outputs: 2,
|
|
45
|
+
depth: 16,
|
|
46
|
+
},
|
|
47
|
+
"1x1": {
|
|
48
|
+
name: "joinsplit_1x1_16",
|
|
49
|
+
inputs: 1,
|
|
50
|
+
outputs: 1,
|
|
51
|
+
depth: 16,
|
|
52
|
+
},
|
|
53
|
+
} as const;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Get list of all supported circuit keys
|
|
57
|
+
*/
|
|
58
|
+
export const SUPPORTED_CIRCUITS = Object.keys(CIRCUIT_REGISTRY) as CircuitKey[];
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Get circuit configuration by dimensions
|
|
62
|
+
* @param inputs Number of inputs (nullifiers)
|
|
63
|
+
* @param outputs Number of outputs (commitments)
|
|
64
|
+
* @returns Circuit configuration or undefined if not supported
|
|
65
|
+
*/
|
|
66
|
+
export function getCircuitConfig(
|
|
67
|
+
inputs: number,
|
|
68
|
+
outputs: number,
|
|
69
|
+
): CircuitConfig | undefined {
|
|
70
|
+
const key = `${inputs}x${outputs}` as CircuitKey;
|
|
71
|
+
return CIRCUIT_REGISTRY[key];
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Check if a circuit configuration is supported
|
|
76
|
+
* @param inputs Number of inputs (nullifiers)
|
|
77
|
+
* @param outputs Number of outputs (commitments)
|
|
78
|
+
* @returns True if supported
|
|
79
|
+
*/
|
|
80
|
+
export function isCircuitSupported(inputs: number, outputs: number): boolean {
|
|
81
|
+
return getCircuitConfig(inputs, outputs) !== undefined;
|
|
82
|
+
}
|
package/schema.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { SchemaMismatchError } from "./errors.js";
|
|
2
|
+
import type { Storage } from "./types.js";
|
|
3
|
+
|
|
4
|
+
export const CORE_SCHEMA_VERSION = 1;
|
|
5
|
+
|
|
6
|
+
export async function migrate(storage: Storage) {
|
|
7
|
+
await storage.open();
|
|
8
|
+
const current = await storage.getSchemaVersion();
|
|
9
|
+
if (current === 0) {
|
|
10
|
+
// seed structures if needed
|
|
11
|
+
await storage.setSchemaVersion(CORE_SCHEMA_VERSION);
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
if (current !== CORE_SCHEMA_VERSION) {
|
|
15
|
+
throw new SchemaMismatchError(current, CORE_SCHEMA_VERSION);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
#!/bin/bash
|
|
2
|
+
|
|
3
|
+
# Script to setup SDK artifacts from circuits build
|
|
4
|
+
# Creates symlinks to avoid duplicating large files
|
|
5
|
+
|
|
6
|
+
set -e
|
|
7
|
+
|
|
8
|
+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
9
|
+
SDK_ARTIFACTS="$SCRIPT_DIR/artifacts/circuits"
|
|
10
|
+
CIRCUITS_ARTIFACTS="$SCRIPT_DIR/../../circuits/artifacts"
|
|
11
|
+
|
|
12
|
+
echo "Setting up SDK circuit artifacts..."
|
|
13
|
+
echo "Source: $CIRCUITS_ARTIFACTS"
|
|
14
|
+
echo "Target: $SDK_ARTIFACTS"
|
|
15
|
+
|
|
16
|
+
mkdir -p "$SDK_ARTIFACTS"
|
|
17
|
+
|
|
18
|
+
# Check if circuits artifacts exist
|
|
19
|
+
if [ ! -d "$CIRCUITS_ARTIFACTS" ]; then
|
|
20
|
+
echo "❌ Error: Circuit artifacts not found at $CIRCUITS_ARTIFACTS"
|
|
21
|
+
echo "Please run 'pnpm run circuits:build' in the root directory first."
|
|
22
|
+
exit 1
|
|
23
|
+
fi
|
|
24
|
+
|
|
25
|
+
# Remove old flat structure if it exists
|
|
26
|
+
if [ -f "$SDK_ARTIFACTS/joinsplit.wasm" ]; then
|
|
27
|
+
echo "Removing old artifact structure..."
|
|
28
|
+
rm -f "$SDK_ARTIFACTS/joinsplit.wasm"
|
|
29
|
+
rm -f "$SDK_ARTIFACTS/joinsplit.zkey"
|
|
30
|
+
rm -f "$SDK_ARTIFACTS/joinsplit.vkey.json"
|
|
31
|
+
fi
|
|
32
|
+
|
|
33
|
+
# Create circuit-specific directories and symlink artifacts
|
|
34
|
+
for circuit_dir in "$CIRCUITS_ARTIFACTS"/*; do
|
|
35
|
+
if [ -d "$circuit_dir" ]; then
|
|
36
|
+
circuit_name=$(basename "$circuit_dir")
|
|
37
|
+
target_dir="$SDK_ARTIFACTS/$circuit_name"
|
|
38
|
+
|
|
39
|
+
echo "Setting up $circuit_name..."
|
|
40
|
+
|
|
41
|
+
# Create target directory
|
|
42
|
+
mkdir -p "$target_dir"
|
|
43
|
+
|
|
44
|
+
# Create symlinks
|
|
45
|
+
ln -sf "$circuit_dir/joinsplit.wasm" "$target_dir/joinsplit.wasm"
|
|
46
|
+
ln -sf "$circuit_dir/joinsplit.zkey" "$target_dir/joinsplit.zkey"
|
|
47
|
+
ln -sf "$circuit_dir/joinsplit.vkey.json" "$target_dir/joinsplit.vkey.json"
|
|
48
|
+
|
|
49
|
+
echo " ✓ Linked $circuit_name artifacts"
|
|
50
|
+
fi
|
|
51
|
+
done
|
|
52
|
+
|
|
53
|
+
echo ""
|
|
54
|
+
echo "✓ SDK artifacts setup complete!"
|
|
55
|
+
echo ""
|
|
56
|
+
echo "Available circuits:"
|
|
57
|
+
ls -1 "$SDK_ARTIFACTS"
|
package/state/index.ts
ADDED