-
Notifications
You must be signed in to change notification settings - Fork 333
/
Copy pathindex.ts
441 lines (378 loc) · 16 KB
/
index.ts
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
import {
BaseOrMergeRollupPublicInputs,
BaseRollupInputs,
KernelCircuitPublicInputs,
KernelCircuitPublicInputsFinal,
MergeRollupInputs,
PrivateKernelInputsInit,
PrivateKernelInputsInner,
PrivateKernelInputsOrdering,
PublicKernelInputs,
RootRollupInputs,
RootRollupPublicInputs,
} from '@aztec/circuits.js';
import { NoirCompiledCircuit } from '@aztec/noir-compiler';
import { WasmBlackBoxFunctionSolver, createBlackBoxSolver, executeCircuitWithBlackBoxSolver } from '@noir-lang/acvm_js';
import { Abi, abiDecode, abiEncode } from '@noir-lang/noirc_abi';
import PrivateKernelInitJson from './target/private_kernel_init.json' assert { type: 'json' };
import PrivateKernelInitSimulatedJson from './target/private_kernel_init_simulated.json' assert { type: 'json' };
import PrivateKernelInnerJson from './target/private_kernel_inner.json' assert { type: 'json' };
import PrivateKernelInnerSimulatedJson from './target/private_kernel_inner_simulated.json' assert { type: 'json' };
import PrivateKernelOrderingJson from './target/private_kernel_ordering.json' assert { type: 'json' };
import PrivateKernelOrderingSimulatedJson from './target/private_kernel_ordering_simulated.json' assert { type: 'json' };
import PublicKernelPrivatePreviousJson from './target/public_kernel_private_previous.json' assert { type: 'json' };
import PublicKernelPrivatePreviousSimulatedJson from './target/public_kernel_private_previous_simulated.json' assert { type: 'json' };
import PublicKernelPublicPreviousJson from './target/public_kernel_public_previous.json' assert { type: 'json' };
import PublicKernelPublicPreviousSimulatedJson from './target/public_kernel_public_previous_simulated.json' assert { type: 'json' };
import BaseRollupSimulatedJson from './target/rollup_base_simulated.json' assert { type: 'json' };
import MergeRollupJson from './target/rollup_merge.json' assert { type: 'json' };
import RootRollupJson from './target/rollup_root.json' assert { type: 'json' };
import {
mapBaseOrMergeRollupPublicInputsFromNoir,
mapBaseRollupInputsToNoir,
mapKernelCircuitPublicInputsFinalFromNoir,
mapKernelCircuitPublicInputsFromNoir,
mapMergeRollupInputsToNoir,
mapPrivateKernelInputsInitToNoir,
mapPrivateKernelInputsInnerToNoir,
mapPrivateKernelInputsOrderingToNoir,
mapPublicKernelInputs,
mapRootRollupInputsToNoir,
mapRootRollupPublicInputsFromNoir,
} from './type_conversion.js';
import { InputType as InitInputType, ReturnType } from './types/private_kernel_init_types.js';
import { InputType as InnerInputType } from './types/private_kernel_inner_types.js';
import {
ReturnType as FinalReturnType,
InputType as OrderingInputType,
} from './types/private_kernel_ordering_types.js';
import {
InputType as PublicPrivatePreviousInputType,
ReturnType as PublicPrivatePreviousReturnType,
} from './types/public_kernel_private_previous_types.js';
import {
InputType as PublicPublicPreviousInputType,
ReturnType as PublicPublicPreviousReturnType,
} from './types/public_kernel_public_previous_types.js';
import { InputType as BaseRollupInputType, ReturnType as BaseRollupReturnType } from './types/rollup_base_types.js';
import { InputType as MergeRollupInputType, ReturnType as MergeRollupReturnType } from './types/rollup_merge_types.js';
import { InputType as RootRollupInputType, ReturnType as RootRollupReturnType } from './types/rollup_root_types.js';
// TODO(Tom): This should be exported from noirc_abi
/**
* The decoded inputs from the circuit.
*/
export type DecodedInputs = {
/**
* The inputs to the circuit
*/
inputs: Record<string, any>;
/**
* The return value of the circuit
*/
return_value: any;
};
export const PrivateKernelInitArtifact = PrivateKernelInitJson as NoirCompiledCircuit;
export const PrivateKernelInnerArtifact = PrivateKernelInnerJson as NoirCompiledCircuit;
export const PrivateKernelOrderingArtifact = PrivateKernelOrderingJson as NoirCompiledCircuit;
export const PublicKernelPrivatePreviousArtifact = PublicKernelPrivatePreviousJson as NoirCompiledCircuit;
export const PublicKernelPublicPreviousArtifact = PublicKernelPublicPreviousJson as NoirCompiledCircuit;
/**
* Executes the init private kernel.
* @param privateKernelInputsInit - The private kernel inputs.
* @returns The public inputs.
*/
export async function executeInit(
privateKernelInputsInit: PrivateKernelInputsInit,
): Promise<KernelCircuitPublicInputs> {
const params: InitInputType = {
input: mapPrivateKernelInputsInitToNoir(privateKernelInputsInit),
};
const returnType = await executePrivateKernelInitWithACVM(params);
return mapKernelCircuitPublicInputsFromNoir(returnType);
}
let solver: Promise<WasmBlackBoxFunctionSolver>;
const getSolver = (): Promise<WasmBlackBoxFunctionSolver> => {
if (!solver) {
solver = createBlackBoxSolver();
}
return solver;
};
/**
* Executes the inner private kernel.
* @param privateKernelInputsInner - The private kernel inputs.
* @returns The public inputs.
*/
export async function executeInner(
privateKernelInputsInner: PrivateKernelInputsInner,
): Promise<KernelCircuitPublicInputs> {
const params: InnerInputType = {
input: mapPrivateKernelInputsInnerToNoir(privateKernelInputsInner),
};
const returnType = await executePrivateKernelInnerWithACVM(params);
return mapKernelCircuitPublicInputsFromNoir(returnType);
}
/**
* Executes the inner private kernel.
* @param privateKernelInputsInit - The private kernel inputs.
* @returns The public inputs.
*/
export async function executeOrdering(
privateKernelInputsOrdering: PrivateKernelInputsOrdering,
): Promise<KernelCircuitPublicInputsFinal> {
const params: OrderingInputType = {
input: mapPrivateKernelInputsOrderingToNoir(privateKernelInputsOrdering),
};
const returnType = await executePrivateKernelOrderingWithACVM(params);
return mapKernelCircuitPublicInputsFinalFromNoir(returnType);
}
/**
* Executes the public kernel.
* @param privateKernelInputsInit - The public kernel private inputs.
* @returns The public inputs.
*/
export async function executePublicKernelPrivatePrevious(
publicKernelPrivateInputs: PublicKernelInputs,
): Promise<KernelCircuitPublicInputs> {
const params: PublicPrivatePreviousInputType = {
input: mapPublicKernelInputs(publicKernelPrivateInputs),
};
const returnType = await executePublicKernelPrivatePreviousWithACVM(params);
return mapKernelCircuitPublicInputsFromNoir(returnType);
}
/**
* Executes the inner public kernel.
* @param privateKernelInputsInit - The public kernel private inputs.
* @returns The public inputs.
*/
export async function executePublicKernelPublicPrevious(
publicKernelPrivateInputs: PublicKernelInputs,
): Promise<KernelCircuitPublicInputs> {
const params: PublicPrivatePreviousInputType = {
input: mapPublicKernelInputs(publicKernelPrivateInputs),
};
const returnType = await executePublicKernelPublicPreviousWithACVM(params);
return mapKernelCircuitPublicInputsFromNoir(returnType);
}
/**
* Executes the root rollup.
* @param rootRollupInputs - The root rollup inputs.
* @returns The public inputs.
*/
export async function executeRootRollup(rootRollupInputs: RootRollupInputs): Promise<RootRollupPublicInputs> {
const params: RootRollupInputType = {
inputs: mapRootRollupInputsToNoir(rootRollupInputs),
};
const returnType = await executeRootRollupWithACVM(params);
return mapRootRollupPublicInputsFromNoir(returnType);
}
/**
* Executes the merge rollup.
* @param mergeRollupInputs - The merge rollup inputs.
* @returns The public inputs.
*/
export async function executeMergeRollup(mergeRollupInputs: MergeRollupInputs): Promise<BaseOrMergeRollupPublicInputs> {
const params: MergeRollupInputType = {
inputs: mapMergeRollupInputsToNoir(mergeRollupInputs),
};
const returnType = await executeMergeRollupWithACVM(params);
return mapBaseOrMergeRollupPublicInputsFromNoir(returnType);
}
/**
* Executes the base rollup.
* @param mergeRollupInputs - The merge rollup inputs.
* @returns The public inputs.
*/
export async function executeBaseRollup(baseRollupInputs: BaseRollupInputs): Promise<BaseOrMergeRollupPublicInputs> {
const params: BaseRollupInputType = {
inputs: mapBaseRollupInputsToNoir(baseRollupInputs),
};
const returnType = await executeBaseRollupWithACVM(params);
return mapBaseOrMergeRollupPublicInputsFromNoir(returnType);
}
/**
* Executes the init private kernel with the given inputs using the acvm.
*
*/
async function executePrivateKernelInitWithACVM(input: InitInputType): Promise<ReturnType> {
const initialWitnessMap = abiEncode(PrivateKernelInitSimulatedJson.abi as Abi, input as any);
// Execute the circuit on those initial witness values
//
// Decode the bytecode from base64 since the acvm does not know about base64 encoding
const decodedBytecode = Buffer.from(PrivateKernelInitSimulatedJson.bytecode, 'base64');
//
// Execute the circuit
const _witnessMap = await executeCircuitWithBlackBoxSolver(
await getSolver(),
decodedBytecode,
initialWitnessMap,
() => {
throw Error('unexpected oracle during execution');
},
);
// Decode the witness map into two fields, the return values and the inputs
const decodedInputs: DecodedInputs = abiDecode(PrivateKernelInitSimulatedJson.abi as Abi, _witnessMap);
// Cast the inputs as the return type
return decodedInputs.return_value as ReturnType;
}
/**
* Executes the inner private kernel with the given inputs using the acvm.
*/
async function executePrivateKernelInnerWithACVM(input: InnerInputType): Promise<ReturnType> {
const initialWitnessMap = abiEncode(PrivateKernelInnerSimulatedJson.abi as Abi, input as any);
// Execute the circuit on those initial witness values
//
// Decode the bytecode from base64 since the acvm does not know about base64 encoding
const decodedBytecode = Buffer.from(PrivateKernelInnerSimulatedJson.bytecode, 'base64');
//
// Execute the circuit
const _witnessMap = await executeCircuitWithBlackBoxSolver(
await getSolver(),
decodedBytecode,
initialWitnessMap,
() => {
throw Error('unexpected oracle during execution');
},
);
// Decode the witness map into two fields, the return values and the inputs
const decodedInputs: DecodedInputs = abiDecode(PrivateKernelInnerSimulatedJson.abi as Abi, _witnessMap);
// Cast the inputs as the return type
return decodedInputs.return_value as ReturnType;
}
/**
* Executes the ordering private kernel with the given inputs using the acvm.
*/
async function executePrivateKernelOrderingWithACVM(input: OrderingInputType): Promise<FinalReturnType> {
const initialWitnessMap = abiEncode(PrivateKernelOrderingSimulatedJson.abi as Abi, input as any);
// Execute the circuit on those initial witness values
//
// Decode the bytecode from base64 since the acvm does not know about base64 encoding
const decodedBytecode = Buffer.from(PrivateKernelOrderingSimulatedJson.bytecode, 'base64');
//
// Execute the circuit
const _witnessMap = await executeCircuitWithBlackBoxSolver(
await getSolver(),
decodedBytecode,
initialWitnessMap,
() => {
throw Error('unexpected oracle during execution');
},
);
// Decode the witness map into two fields, the return values and the inputs
const decodedInputs: DecodedInputs = abiDecode(PrivateKernelOrderingSimulatedJson.abi as Abi, _witnessMap);
// Cast the inputs as the return type
return decodedInputs.return_value as FinalReturnType;
}
/**
* Executes the public kernel with private prevoius kernel with the given inputs
*/
async function executePublicKernelPrivatePreviousWithACVM(
input: PublicPrivatePreviousInputType,
): Promise<PublicPrivatePreviousReturnType> {
const initialWitnessMap = abiEncode(PublicKernelPrivatePreviousSimulatedJson.abi as Abi, input as any);
const decodedBytecode = Buffer.from(PublicKernelPrivatePreviousSimulatedJson.bytecode, 'base64');
// Execute the circuit
const _witnessMap = await executeCircuitWithBlackBoxSolver(
await getSolver(),
decodedBytecode,
initialWitnessMap,
() => {
throw Error('unexpected oracle during execution');
},
);
// Decode the witness map into two fields, the return values and the inputs
const decodedInputs: DecodedInputs = abiDecode(PublicKernelPrivatePreviousSimulatedJson.abi as Abi, _witnessMap);
// Cast the inputs as the return type
return decodedInputs.return_value as PublicPrivatePreviousReturnType;
}
/**
* Executes the ordering private kernel with the given inputs using the acvm.
*/
async function executePublicKernelPublicPreviousWithACVM(
input: PublicPublicPreviousInputType,
): Promise<PublicPublicPreviousReturnType> {
const initialWitnessMap = abiEncode(PublicKernelPublicPreviousSimulatedJson.abi as Abi, input as any);
const decodedBytecode = Buffer.from(PublicKernelPublicPreviousSimulatedJson.bytecode, 'base64');
// Execute the circuit
const _witnessMap = await executeCircuitWithBlackBoxSolver(
await getSolver(),
decodedBytecode,
initialWitnessMap,
() => {
throw Error('unexpected oracle during execution');
},
);
// Decode the witness map into two fields, the return values and the inputs
const decodedInputs: DecodedInputs = abiDecode(PublicKernelPublicPreviousSimulatedJson.abi as Abi, _witnessMap);
// Cast the inputs as the return type
return decodedInputs.return_value as PublicPublicPreviousReturnType;
}
/**
* Executes the root rollup with the given inputs using the acvm.
*/
async function executeRootRollupWithACVM(input: RootRollupInputType): Promise<RootRollupReturnType> {
const initialWitnessMap = abiEncode(RootRollupJson.abi as Abi, input as any);
// Execute the circuit on those initial witness values
//
// Decode the bytecode from base64 since the acvm does not know about base64 encoding
const decodedBytecode = Buffer.from(RootRollupJson.bytecode, 'base64');
//
// Execute the circuit
const _witnessMap = await executeCircuitWithBlackBoxSolver(
await getSolver(),
decodedBytecode,
initialWitnessMap,
() => {
throw Error('unexpected oracle during execution');
},
);
const decodedInputs: DecodedInputs = abiDecode(RootRollupJson.abi as Abi, _witnessMap);
// Cast the inputs as the return type
return decodedInputs.return_value as RootRollupReturnType;
}
/**
* Executes the merge rollup with the given inputs using the acvm.
*/
async function executeMergeRollupWithACVM(input: MergeRollupInputType): Promise<MergeRollupReturnType> {
const initialWitnessMap = abiEncode(MergeRollupJson.abi as Abi, input as any);
// Execute the circuit on those initial witness values
//
// Decode the bytecode from base64 since the acvm does not know about base64 encoding
const decodedBytecode = Buffer.from(MergeRollupJson.bytecode, 'base64');
//
// Execute the circuit
const _witnessMap = await executeCircuitWithBlackBoxSolver(
await getSolver(),
decodedBytecode,
initialWitnessMap,
() => {
throw Error('unexpected oracle during execution');
},
);
const decodedInputs: DecodedInputs = abiDecode(MergeRollupJson.abi as Abi, _witnessMap);
// Cast the inputs as the return type
return decodedInputs.return_value as MergeRollupReturnType;
}
/**
* Executes the base rollup with the given inputs using the acvm.
*/
async function executeBaseRollupWithACVM(input: BaseRollupInputType): Promise<BaseRollupReturnType> {
const initialWitnessMap = abiEncode(BaseRollupSimulatedJson.abi as Abi, input as any);
// Execute the circuit on those initial witness values
//
// Decode the bytecode from base64 since the acvm does not know about base64 encoding
const decodedBytecode = Buffer.from(BaseRollupSimulatedJson.bytecode, 'base64');
//
// Execute the circuit
const _witnessMap = await executeCircuitWithBlackBoxSolver(
await getSolver(),
decodedBytecode,
initialWitnessMap,
() => {
throw Error('unexpected oracle during execution');
},
);
// Decode the witness map into two fields, the return values and the inputs
const decodedInputs: DecodedInputs = abiDecode(BaseRollupSimulatedJson.abi as Abi, _witnessMap);
// Cast the inputs as the return type
return decodedInputs.return_value as BaseRollupReturnType;
}