@aztec/bb-prover 0.0.0-test.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.
Files changed (97) hide show
  1. package/dest/avm_proving_tests/avm_proving_tester.d.ts +25 -0
  2. package/dest/avm_proving_tests/avm_proving_tester.d.ts.map +1 -0
  3. package/dest/avm_proving_tests/avm_proving_tester.js +105 -0
  4. package/dest/bb/cli.d.ts +12 -0
  5. package/dest/bb/cli.d.ts.map +1 -0
  6. package/dest/bb/cli.js +19 -0
  7. package/dest/bb/execute.d.ts +140 -0
  8. package/dest/bb/execute.d.ts.map +1 -0
  9. package/dest/bb/execute.js +780 -0
  10. package/dest/bb/index.d.ts +3 -0
  11. package/dest/bb/index.d.ts.map +1 -0
  12. package/dest/bb/index.js +16 -0
  13. package/dest/config.d.ts +13 -0
  14. package/dest/config.d.ts.map +1 -0
  15. package/dest/config.js +1 -0
  16. package/dest/honk.d.ts +13 -0
  17. package/dest/honk.d.ts.map +1 -0
  18. package/dest/honk.js +18 -0
  19. package/dest/index.d.ts +8 -0
  20. package/dest/index.d.ts.map +1 -0
  21. package/dest/index.js +6 -0
  22. package/dest/instrumentation.d.ts +47 -0
  23. package/dest/instrumentation.d.ts.map +1 -0
  24. package/dest/instrumentation.js +100 -0
  25. package/dest/prover/bb_native_private_kernel_prover.d.ts +25 -0
  26. package/dest/prover/bb_native_private_kernel_prover.d.ts.map +1 -0
  27. package/dest/prover/bb_native_private_kernel_prover.js +69 -0
  28. package/dest/prover/bb_private_kernel_prover.d.ts +32 -0
  29. package/dest/prover/bb_private_kernel_prover.d.ts.map +1 -0
  30. package/dest/prover/bb_private_kernel_prover.js +109 -0
  31. package/dest/prover/bb_prover.d.ts +120 -0
  32. package/dest/prover/bb_prover.d.ts.map +1 -0
  33. package/dest/prover/bb_prover.js +423 -0
  34. package/dest/prover/client_ivc_proof_utils.d.ts +25 -0
  35. package/dest/prover/client_ivc_proof_utils.d.ts.map +1 -0
  36. package/dest/prover/client_ivc_proof_utils.js +43 -0
  37. package/dest/prover/index.d.ts +4 -0
  38. package/dest/prover/index.d.ts.map +1 -0
  39. package/dest/prover/index.js +3 -0
  40. package/dest/stats.d.ts +5 -0
  41. package/dest/stats.d.ts.map +1 -0
  42. package/dest/stats.js +62 -0
  43. package/dest/test/delay_values.d.ts +4 -0
  44. package/dest/test/delay_values.d.ts.map +1 -0
  45. package/dest/test/delay_values.js +29 -0
  46. package/dest/test/index.d.ts +3 -0
  47. package/dest/test/index.d.ts.map +1 -0
  48. package/dest/test/index.js +2 -0
  49. package/dest/test/test_circuit_prover.d.ts +81 -0
  50. package/dest/test/test_circuit_prover.d.ts.map +1 -0
  51. package/dest/test/test_circuit_prover.js +175 -0
  52. package/dest/test/test_verifier.d.ts +6 -0
  53. package/dest/test/test_verifier.d.ts.map +1 -0
  54. package/dest/test/test_verifier.js +5 -0
  55. package/dest/verification_key/verification_key_data.d.ts +9 -0
  56. package/dest/verification_key/verification_key_data.d.ts.map +1 -0
  57. package/dest/verification_key/verification_key_data.js +44 -0
  58. package/dest/verifier/bb_verifier.d.ts +17 -0
  59. package/dest/verifier/bb_verifier.d.ts.map +1 -0
  60. package/dest/verifier/bb_verifier.js +86 -0
  61. package/dest/verifier/index.d.ts +2 -0
  62. package/dest/verifier/index.d.ts.map +1 -0
  63. package/dest/verifier/index.js +1 -0
  64. package/dest/wasm/bb_wasm_private_kernel_prover.d.ts +17 -0
  65. package/dest/wasm/bb_wasm_private_kernel_prover.d.ts.map +1 -0
  66. package/dest/wasm/bb_wasm_private_kernel_prover.js +46 -0
  67. package/dest/wasm/bundle.d.ts +6 -0
  68. package/dest/wasm/bundle.d.ts.map +1 -0
  69. package/dest/wasm/bundle.js +8 -0
  70. package/dest/wasm/lazy.d.ts +6 -0
  71. package/dest/wasm/lazy.d.ts.map +1 -0
  72. package/dest/wasm/lazy.js +8 -0
  73. package/package.json +111 -0
  74. package/src/avm_proving_tests/avm_proving_tester.ts +170 -0
  75. package/src/bb/cli.ts +32 -0
  76. package/src/bb/execute.ts +853 -0
  77. package/src/bb/index.ts +23 -0
  78. package/src/config.ts +13 -0
  79. package/src/honk.ts +30 -0
  80. package/src/index.ts +8 -0
  81. package/src/instrumentation.ts +144 -0
  82. package/src/prover/bb_native_private_kernel_prover.ts +119 -0
  83. package/src/prover/bb_private_kernel_prover.ts +249 -0
  84. package/src/prover/bb_prover.ts +781 -0
  85. package/src/prover/client_ivc_proof_utils.ts +42 -0
  86. package/src/prover/index.ts +3 -0
  87. package/src/stats.ts +64 -0
  88. package/src/test/delay_values.ts +31 -0
  89. package/src/test/index.ts +2 -0
  90. package/src/test/test_circuit_prover.ts +368 -0
  91. package/src/test/test_verifier.ts +8 -0
  92. package/src/verification_key/verification_key_data.ts +45 -0
  93. package/src/verifier/bb_verifier.ts +114 -0
  94. package/src/verifier/index.ts +1 -0
  95. package/src/wasm/bb_wasm_private_kernel_prover.ts +55 -0
  96. package/src/wasm/bundle.ts +11 -0
  97. package/src/wasm/lazy.ts +11 -0
@@ -0,0 +1,23 @@
1
+ #!/usr/bin/env -S node --no-warnings
2
+ import { createConsoleLogger } from '@aztec/foundation/log';
3
+
4
+ import 'source-map-support/register.js';
5
+
6
+ import { getProgram } from './cli.js';
7
+
8
+ const log = createConsoleLogger();
9
+
10
+ /** CLI main entrypoint */
11
+ async function main() {
12
+ process.once('SIGINT', () => process.exit(0));
13
+ process.once('SIGTERM', () => process.exit(0));
14
+
15
+ const program = getProgram(log);
16
+ await program.parseAsync(process.argv);
17
+ }
18
+
19
+ main().catch(err => {
20
+ log(`Error in command execution`);
21
+ log(err);
22
+ process.exit(1);
23
+ });
package/src/config.ts ADDED
@@ -0,0 +1,13 @@
1
+ export interface BBConfig {
2
+ bbBinaryPath: string;
3
+ bbWorkingDirectory: string;
4
+ /** Whether to skip tmp dir cleanup for debugging purposes */
5
+ bbSkipCleanup: boolean;
6
+ }
7
+
8
+ export interface ACVMConfig {
9
+ /** The path to the ACVM binary */
10
+ acvmBinaryPath: string;
11
+ /** The working directory to use for simulation/proving */
12
+ acvmWorkingDirectory: string;
13
+ }
package/src/honk.ts ADDED
@@ -0,0 +1,30 @@
1
+ import type { ServerProtocolArtifact } from '@aztec/noir-protocol-circuits-types/server';
2
+
3
+ export type UltraHonkFlavor = 'ultra_honk' | 'ultra_keccak_honk' | 'ultra_rollup_honk';
4
+
5
+ const UltraKeccakHonkCircuits = ['RootRollupArtifact'] as const satisfies ServerProtocolArtifact[];
6
+ const UltraHonkCircuits = ['BaseParityArtifact', 'RootParityArtifact'] as const satisfies ServerProtocolArtifact[];
7
+
8
+ export type UltraKeccakHonkServerProtocolArtifact = (typeof UltraKeccakHonkCircuits)[number];
9
+ export type UltraHonkServerProtocolArtifact = (typeof UltraHonkCircuits)[number];
10
+ export type UltraRollupHonkServerProtocolArtifact = Exclude<
11
+ Exclude<ServerProtocolArtifact, UltraKeccakHonkServerProtocolArtifact>,
12
+ UltraHonkServerProtocolArtifact
13
+ >;
14
+
15
+ export function getUltraHonkFlavorForCircuit(artifact: UltraKeccakHonkServerProtocolArtifact): 'ultra_keccak_honk';
16
+ export function getUltraHonkFlavorForCircuit(artifact: UltraHonkServerProtocolArtifact): 'ultra_honk';
17
+ export function getUltraHonkFlavorForCircuit(artifact: UltraRollupHonkServerProtocolArtifact): 'ultra_rollup_honk';
18
+ export function getUltraHonkFlavorForCircuit(artifact: ServerProtocolArtifact): UltraHonkFlavor;
19
+ export function getUltraHonkFlavorForCircuit(artifact: ServerProtocolArtifact): UltraHonkFlavor {
20
+ if (isUltraKeccakHonkCircuit(artifact)) {
21
+ return 'ultra_keccak_honk';
22
+ } else if (UltraHonkCircuits.includes(artifact as UltraHonkServerProtocolArtifact)) {
23
+ return 'ultra_honk';
24
+ }
25
+ return 'ultra_rollup_honk';
26
+ }
27
+
28
+ function isUltraKeccakHonkCircuit(artifact: ServerProtocolArtifact): artifact is UltraKeccakHonkServerProtocolArtifact {
29
+ return UltraKeccakHonkCircuits.includes(artifact as UltraKeccakHonkServerProtocolArtifact);
30
+ }
package/src/index.ts ADDED
@@ -0,0 +1,8 @@
1
+ export * from './prover/index.js';
2
+ export * from './test/index.js';
3
+ export * from './verifier/index.js';
4
+ export * from './config.js';
5
+ export * from './bb/execute.js';
6
+ export * from './honk.js';
7
+
8
+ export { type ClientProtocolCircuitVerifier } from '@aztec/stdlib/interfaces/server';
@@ -0,0 +1,144 @@
1
+ import type { Timer } from '@aztec/foundation/timer';
2
+ import type { CircuitName } from '@aztec/stdlib/stats';
3
+ import {
4
+ Attributes,
5
+ type Gauge,
6
+ type Histogram,
7
+ Metrics,
8
+ type TelemetryClient,
9
+ type Tracer,
10
+ ValueType,
11
+ } from '@aztec/telemetry-client';
12
+
13
+ /**
14
+ * Instrumentation class for Prover implementations.
15
+ */
16
+ export class ProverInstrumentation {
17
+ private simulationDuration: Histogram;
18
+ private witGenDuration: Histogram;
19
+ private provingDuration: Histogram;
20
+
21
+ private witGenInputSize: Gauge;
22
+ private witGenOutputSize: Gauge;
23
+
24
+ private proofSize: Gauge;
25
+ private circuitSize: Gauge;
26
+ private circuitPublicInputCount: Gauge;
27
+
28
+ public readonly tracer: Tracer;
29
+
30
+ constructor(telemetry: TelemetryClient, name: string) {
31
+ this.tracer = telemetry.getTracer(name);
32
+ const meter = telemetry.getMeter(name);
33
+
34
+ this.simulationDuration = meter.createHistogram(Metrics.CIRCUIT_SIMULATION_DURATION, {
35
+ description: 'Records how long it takes to simulate a circuit',
36
+ unit: 'ms',
37
+ valueType: ValueType.INT,
38
+ });
39
+
40
+ this.witGenDuration = meter.createHistogram(Metrics.CIRCUIT_WITNESS_GEN_DURATION, {
41
+ description: 'Records how long it takes to generate the partial witness for a circuit',
42
+ unit: 's',
43
+ valueType: ValueType.DOUBLE,
44
+ });
45
+
46
+ this.provingDuration = meter.createHistogram(Metrics.CIRCUIT_PROVING_DURATION, {
47
+ unit: 's',
48
+ description: 'Records how long it takes to prove a circuit',
49
+ valueType: ValueType.DOUBLE,
50
+ });
51
+
52
+ this.witGenInputSize = meter.createGauge(Metrics.CIRCUIT_WITNESS_GEN_INPUT_SIZE, {
53
+ unit: 'By',
54
+ description: 'Records the size of the input to the witness generation',
55
+ valueType: ValueType.INT,
56
+ });
57
+
58
+ this.witGenOutputSize = meter.createGauge(Metrics.CIRCUIT_WITNESS_GEN_OUTPUT_SIZE, {
59
+ unit: 'By',
60
+ description: 'Records the size of the output of the witness generation',
61
+ valueType: ValueType.INT,
62
+ });
63
+
64
+ this.proofSize = meter.createGauge(Metrics.CIRCUIT_PROVING_PROOF_SIZE, {
65
+ unit: 'By',
66
+ description: 'Records the size of the proof generated for a circuit',
67
+ valueType: ValueType.INT,
68
+ });
69
+
70
+ this.circuitPublicInputCount = meter.createGauge(Metrics.CIRCUIT_PUBLIC_INPUTS_COUNT, {
71
+ description: 'Records the number of public inputs in a circuit',
72
+ valueType: ValueType.INT,
73
+ });
74
+
75
+ this.circuitSize = meter.createGauge(Metrics.CIRCUIT_SIZE, {
76
+ description: 'Records the size of the circuit in gates',
77
+ valueType: ValueType.INT,
78
+ });
79
+ }
80
+
81
+ /**
82
+ * Records the duration of a circuit operation.
83
+ * @param metric - The metric to record
84
+ * @param circuitName - The name of the circuit
85
+ * @param timerOrMS - The duration
86
+ */
87
+ recordDuration(
88
+ metric: 'simulationDuration' | 'witGenDuration' | 'provingDuration',
89
+ circuitName: CircuitName | 'tubeCircuit',
90
+ timerOrMS: Timer | number,
91
+ ) {
92
+ const s = typeof timerOrMS === 'number' ? timerOrMS / 1000 : timerOrMS.s();
93
+ this[metric].record(s, {
94
+ [Attributes.PROTOCOL_CIRCUIT_NAME]: circuitName,
95
+ [Attributes.PROTOCOL_CIRCUIT_TYPE]: 'server',
96
+ });
97
+ }
98
+
99
+ /**
100
+ * Records the duration of an AVM circuit operation.
101
+ * @param metric - The metric to record
102
+ * @param appCircuitName - The name of the function circuit (should be a `contract:function` string)
103
+ * @param timerOrMS - The duration
104
+ */
105
+ recordAvmDuration(metric: 'witGenDuration' | 'provingDuration', appCircuitName: string, timerOrMS: Timer | number) {
106
+ const s = typeof timerOrMS === 'number' ? timerOrMS / 1000 : timerOrMS.s();
107
+ this[metric].record(s, {
108
+ [Attributes.APP_CIRCUIT_NAME]: appCircuitName,
109
+ });
110
+ }
111
+
112
+ /**
113
+ * Records the size of a circuit operation.
114
+ * @param metric - Records the size of a circuit operation.
115
+ * @param circuitName - The name of the circuit
116
+ * @param size - The size
117
+ */
118
+ recordSize(
119
+ metric: 'witGenInputSize' | 'witGenOutputSize' | 'proofSize' | 'circuitSize' | 'circuitPublicInputCount',
120
+ circuitName: CircuitName | 'tubeCircuit',
121
+ size: number,
122
+ ) {
123
+ this[metric].record(Math.ceil(size), {
124
+ [Attributes.PROTOCOL_CIRCUIT_NAME]: circuitName,
125
+ [Attributes.PROTOCOL_CIRCUIT_TYPE]: 'server',
126
+ });
127
+ }
128
+
129
+ /**
130
+ * Records the size of an AVM circuit operation.
131
+ * @param metric - The metric to record
132
+ * @param appCircuitName - The name of the function circuit (should be a `contract:function` string)
133
+ * @param size - The size
134
+ */
135
+ recordAvmSize(
136
+ metric: 'witGenInputSize' | 'witGenOutputSize' | 'proofSize' | 'circuitSize' | 'circuitPublicInputCount',
137
+ appCircuitName: string,
138
+ size: number,
139
+ ) {
140
+ this[metric].record(Math.ceil(size), {
141
+ [Attributes.APP_CIRCUIT_NAME]: appCircuitName,
142
+ });
143
+ }
144
+ }
@@ -0,0 +1,119 @@
1
+ import { runInDirectory } from '@aztec/foundation/fs';
2
+ import { type Logger, createLogger } from '@aztec/foundation/log';
3
+ import { BundleArtifactProvider } from '@aztec/noir-protocol-circuits-types/client/bundle';
4
+ import type { SimulationProvider } from '@aztec/simulator/server';
5
+ import type { ClientIvcProof } from '@aztec/stdlib/proofs';
6
+
7
+ import { encode } from '@msgpack/msgpack';
8
+ import { serializeWitness } from '@noir-lang/noirc_abi';
9
+ import type { WitnessMap } from '@noir-lang/types';
10
+ import { promises as fs } from 'fs';
11
+ import path from 'path';
12
+
13
+ import { BB_RESULT, computeGateCountForCircuit, executeBbClientIvcProof } from '../bb/execute.js';
14
+ import type { BBConfig } from '../config.js';
15
+ import { BBPrivateKernelProver } from './bb_private_kernel_prover.js';
16
+ import { readFromOutputDirectory } from './client_ivc_proof_utils.js';
17
+
18
+ /**
19
+ * This proof creator implementation uses the native bb binary.
20
+ */
21
+ export class BBNativePrivateKernelProver extends BBPrivateKernelProver {
22
+ private constructor(
23
+ private bbBinaryPath: string,
24
+ private bbWorkingDirectory: string,
25
+ private skipCleanup: boolean,
26
+ protected override simulationProvider: SimulationProvider,
27
+ protected override log = createLogger('bb-prover:native'),
28
+ ) {
29
+ super(new BundleArtifactProvider(), simulationProvider, log);
30
+ }
31
+
32
+ public static async new(config: BBConfig, simulationProvider: SimulationProvider, log?: Logger) {
33
+ await fs.mkdir(config.bbWorkingDirectory, { recursive: true });
34
+ return new BBNativePrivateKernelProver(
35
+ config.bbBinaryPath,
36
+ config.bbWorkingDirectory,
37
+ !!config.bbSkipCleanup,
38
+ simulationProvider,
39
+ log,
40
+ );
41
+ }
42
+
43
+ private async _createClientIvcProof(
44
+ directory: string,
45
+ acirs: Buffer[],
46
+ witnessStack: WitnessMap[],
47
+ ): Promise<ClientIvcProof> {
48
+ // TODO(#7371): Longer term we won't use this hacked together msgpack format
49
+ // and instead properly create the bincode serialization from rust
50
+ await fs.writeFile(path.join(directory, 'acir.msgpack'), encode(acirs));
51
+ await fs.writeFile(
52
+ path.join(directory, 'witnesses.msgpack'),
53
+ encode(witnessStack.map(map => serializeWitness(map))),
54
+ );
55
+ const provingResult = await executeBbClientIvcProof(
56
+ this.bbBinaryPath,
57
+ directory,
58
+ path.join(directory, 'acir.msgpack'),
59
+ path.join(directory, 'witnesses.msgpack'),
60
+ this.log.info,
61
+ );
62
+
63
+ if (provingResult.status === BB_RESULT.FAILURE) {
64
+ this.log.error(`Failed to generate client ivc proof`);
65
+ throw new Error(provingResult.reason);
66
+ }
67
+
68
+ const proof = await readFromOutputDirectory(directory);
69
+
70
+ this.log.info(`Generated IVC proof`, {
71
+ duration: provingResult.durationMs,
72
+ eventName: 'circuit-proving',
73
+ });
74
+
75
+ return proof;
76
+ }
77
+
78
+ public override async createClientIvcProof(acirs: Buffer[], witnessStack: WitnessMap[]): Promise<ClientIvcProof> {
79
+ this.log.info(`Generating Client IVC proof`);
80
+ const operation = async (directory: string) => {
81
+ return await this._createClientIvcProof(directory, acirs, witnessStack);
82
+ };
83
+ return await this.runInDirectory(operation);
84
+ }
85
+
86
+ public override async computeGateCountForCircuit(bytecode: Buffer, circuitName: string): Promise<number> {
87
+ const logFunction = (message: string) => {
88
+ this.log.debug(`$bb gates ${circuitName} - ${message}`);
89
+ };
90
+
91
+ const result = await computeGateCountForCircuit(
92
+ this.bbBinaryPath,
93
+ this.bbWorkingDirectory,
94
+ circuitName,
95
+ bytecode,
96
+ 'mega_honk',
97
+ logFunction,
98
+ );
99
+ if (result.status === BB_RESULT.FAILURE) {
100
+ throw new Error(result.reason);
101
+ }
102
+
103
+ return result.circuitSize as number;
104
+ }
105
+
106
+ private runInDirectory<T>(fn: (dir: string) => Promise<T>) {
107
+ const log = this.log;
108
+ return runInDirectory(
109
+ this.bbWorkingDirectory,
110
+ (dir: string) =>
111
+ fn(dir).catch(err => {
112
+ log.error(`Error running operation at ${dir}: ${err}`);
113
+ throw err;
114
+ }),
115
+ this.skipCleanup,
116
+ this.log,
117
+ );
118
+ }
119
+ }
@@ -0,0 +1,249 @@
1
+ import { createLogger } from '@aztec/foundation/log';
2
+ import { Timer } from '@aztec/foundation/timer';
3
+ import {
4
+ convertPrivateKernelInitInputsToWitnessMapWithAbi,
5
+ convertPrivateKernelInitOutputsFromWitnessMapWithAbi,
6
+ convertPrivateKernelInnerInputsToWitnessMapWithAbi,
7
+ convertPrivateKernelInnerOutputsFromWitnessMapWithAbi,
8
+ convertPrivateKernelResetInputsToWitnessMapWithAbi,
9
+ convertPrivateKernelResetOutputsFromWitnessMapWithAbi,
10
+ convertPrivateKernelTailForPublicOutputsFromWitnessMapWithAbi,
11
+ convertPrivateKernelTailInputsToWitnessMapWithAbi,
12
+ convertPrivateKernelTailOutputsFromWitnessMapWithAbi,
13
+ convertPrivateKernelTailToPublicInputsToWitnessMapWithAbi,
14
+ getPrivateKernelResetArtifactName,
15
+ updateResetCircuitSampleInputs,
16
+ } from '@aztec/noir-protocol-circuits-types/client';
17
+ import type { ArtifactProvider, ClientProtocolArtifact } from '@aztec/noir-protocol-circuits-types/types';
18
+ import type { SimulationProvider } from '@aztec/simulator/client';
19
+ import type { PrivateKernelProver } from '@aztec/stdlib/interfaces/client';
20
+ import type {
21
+ PrivateKernelCircuitPublicInputs,
22
+ PrivateKernelInitCircuitPrivateInputs,
23
+ PrivateKernelInnerCircuitPrivateInputs,
24
+ PrivateKernelResetCircuitPrivateInputs,
25
+ PrivateKernelSimulateOutput,
26
+ PrivateKernelTailCircuitPrivateInputs,
27
+ PrivateKernelTailCircuitPublicInputs,
28
+ } from '@aztec/stdlib/kernel';
29
+ import type { NoirCompiledCircuit } from '@aztec/stdlib/noir';
30
+ import type { ClientIvcProof } from '@aztec/stdlib/proofs';
31
+ import type { CircuitSimulationStats, CircuitWitnessGenerationStats } from '@aztec/stdlib/stats';
32
+
33
+ import type { Abi, WitnessMap } from '@noir-lang/types';
34
+
35
+ import { mapProtocolArtifactNameToCircuitName } from '../stats.js';
36
+
37
+ export abstract class BBPrivateKernelProver implements PrivateKernelProver {
38
+ constructor(
39
+ protected artifactProvider: ArtifactProvider,
40
+ protected simulationProvider: SimulationProvider,
41
+ protected log = createLogger('bb-prover'),
42
+ ) {}
43
+
44
+ public async generateInitOutput(
45
+ inputs: PrivateKernelInitCircuitPrivateInputs,
46
+ ): Promise<PrivateKernelSimulateOutput<PrivateKernelCircuitPublicInputs>> {
47
+ return await this.generateCircuitOutput(
48
+ inputs,
49
+ 'PrivateKernelInitArtifact',
50
+ convertPrivateKernelInitInputsToWitnessMapWithAbi,
51
+ convertPrivateKernelInitOutputsFromWitnessMapWithAbi,
52
+ );
53
+ }
54
+
55
+ public async simulateInit(
56
+ inputs: PrivateKernelInitCircuitPrivateInputs,
57
+ ): Promise<PrivateKernelSimulateOutput<PrivateKernelCircuitPublicInputs>> {
58
+ return await this.simulateCircuitOutput(
59
+ inputs,
60
+ 'PrivateKernelInitArtifact',
61
+ convertPrivateKernelInitInputsToWitnessMapWithAbi,
62
+ convertPrivateKernelInitOutputsFromWitnessMapWithAbi,
63
+ );
64
+ }
65
+
66
+ public async generateInnerOutput(
67
+ inputs: PrivateKernelInnerCircuitPrivateInputs,
68
+ ): Promise<PrivateKernelSimulateOutput<PrivateKernelCircuitPublicInputs>> {
69
+ return await this.generateCircuitOutput(
70
+ inputs,
71
+ 'PrivateKernelInnerArtifact',
72
+ convertPrivateKernelInnerInputsToWitnessMapWithAbi,
73
+ convertPrivateKernelInnerOutputsFromWitnessMapWithAbi,
74
+ );
75
+ }
76
+
77
+ public async simulateInner(
78
+ inputs: PrivateKernelInnerCircuitPrivateInputs,
79
+ ): Promise<PrivateKernelSimulateOutput<PrivateKernelCircuitPublicInputs>> {
80
+ return await this.simulateCircuitOutput(
81
+ inputs,
82
+ 'PrivateKernelInnerArtifact',
83
+ convertPrivateKernelInnerInputsToWitnessMapWithAbi,
84
+ convertPrivateKernelInnerOutputsFromWitnessMapWithAbi,
85
+ );
86
+ }
87
+
88
+ public async generateResetOutput(
89
+ inputs: PrivateKernelResetCircuitPrivateInputs,
90
+ ): Promise<PrivateKernelSimulateOutput<PrivateKernelCircuitPublicInputs>> {
91
+ const variantInputs = inputs.trimToSizes();
92
+ const artifactName = getPrivateKernelResetArtifactName(inputs.dimensions);
93
+ return await this.generateCircuitOutput(
94
+ variantInputs,
95
+ artifactName,
96
+ convertPrivateKernelResetInputsToWitnessMapWithAbi,
97
+ convertPrivateKernelResetOutputsFromWitnessMapWithAbi,
98
+ );
99
+ }
100
+
101
+ public async simulateReset(
102
+ inputs: PrivateKernelResetCircuitPrivateInputs,
103
+ ): Promise<PrivateKernelSimulateOutput<PrivateKernelCircuitPublicInputs>> {
104
+ updateResetCircuitSampleInputs(inputs);
105
+ const variantInputs = inputs.trimToSizes();
106
+ const artifactName = getPrivateKernelResetArtifactName(inputs.dimensions);
107
+ return await this.simulateCircuitOutput(
108
+ variantInputs,
109
+ artifactName,
110
+ convertPrivateKernelResetInputsToWitnessMapWithAbi,
111
+ convertPrivateKernelResetOutputsFromWitnessMapWithAbi,
112
+ );
113
+ }
114
+
115
+ public async generateTailOutput(
116
+ inputs: PrivateKernelTailCircuitPrivateInputs,
117
+ ): Promise<PrivateKernelSimulateOutput<PrivateKernelTailCircuitPublicInputs>> {
118
+ if (!inputs.isForPublic()) {
119
+ return await this.generateCircuitOutput(
120
+ inputs,
121
+ 'PrivateKernelTailArtifact',
122
+ convertPrivateKernelTailInputsToWitnessMapWithAbi,
123
+ convertPrivateKernelTailOutputsFromWitnessMapWithAbi,
124
+ );
125
+ }
126
+ return await this.generateCircuitOutput(
127
+ inputs,
128
+ 'PrivateKernelTailToPublicArtifact',
129
+ convertPrivateKernelTailToPublicInputsToWitnessMapWithAbi,
130
+ convertPrivateKernelTailForPublicOutputsFromWitnessMapWithAbi,
131
+ );
132
+ }
133
+
134
+ public async simulateTail(
135
+ inputs: PrivateKernelTailCircuitPrivateInputs,
136
+ ): Promise<PrivateKernelSimulateOutput<PrivateKernelTailCircuitPublicInputs>> {
137
+ if (!inputs.isForPublic()) {
138
+ return await this.simulateCircuitOutput(
139
+ inputs,
140
+ 'PrivateKernelTailArtifact',
141
+ convertPrivateKernelTailInputsToWitnessMapWithAbi,
142
+ convertPrivateKernelTailOutputsFromWitnessMapWithAbi,
143
+ );
144
+ }
145
+ return await this.simulateCircuitOutput(
146
+ inputs,
147
+ 'PrivateKernelTailToPublicArtifact',
148
+ convertPrivateKernelTailToPublicInputsToWitnessMapWithAbi,
149
+ convertPrivateKernelTailForPublicOutputsFromWitnessMapWithAbi,
150
+ );
151
+ }
152
+
153
+ public async simulateCircuitOutput<
154
+ I extends { toBuffer: () => Buffer },
155
+ O extends PrivateKernelCircuitPublicInputs | PrivateKernelTailCircuitPublicInputs,
156
+ >(
157
+ inputs: I,
158
+ circuitType: ClientProtocolArtifact,
159
+ convertInputs: (inputs: I, abi: Abi) => WitnessMap,
160
+ convertOutputs: (outputs: WitnessMap, abi: Abi) => O,
161
+ ): Promise<PrivateKernelSimulateOutput<O>> {
162
+ const compiledCircuit: NoirCompiledCircuit = await this.artifactProvider.getSimulatedClientCircuitArtifactByName(
163
+ circuitType,
164
+ );
165
+
166
+ const witnessMap = convertInputs(inputs, compiledCircuit.abi);
167
+
168
+ const timer = new Timer();
169
+ const outputWitness = await this.simulationProvider
170
+ .executeProtocolCircuit(witnessMap, compiledCircuit)
171
+ .catch((err: Error) => {
172
+ this.log.debug(`Failed to simulate ${circuitType}`, {
173
+ circuitName: mapProtocolArtifactNameToCircuitName(circuitType),
174
+ error: err,
175
+ });
176
+ throw err;
177
+ });
178
+ const output = convertOutputs(outputWitness, compiledCircuit.abi);
179
+
180
+ this.log.debug(`Simulated ${circuitType}`, {
181
+ eventName: 'circuit-simulation',
182
+ circuitName: mapProtocolArtifactNameToCircuitName(circuitType),
183
+ duration: timer.ms(),
184
+ inputSize: inputs.toBuffer().length,
185
+ outputSize: output.toBuffer().length,
186
+ } satisfies CircuitSimulationStats);
187
+
188
+ return this.makeEmptyKernelSimulateOutput<O>(output, circuitType);
189
+ }
190
+
191
+ public async generateCircuitOutput<
192
+ I extends { toBuffer: () => Buffer },
193
+ O extends PrivateKernelCircuitPublicInputs | PrivateKernelTailCircuitPublicInputs,
194
+ >(
195
+ inputs: I,
196
+ circuitType: ClientProtocolArtifact,
197
+ convertInputs: (inputs: I, abi: Abi) => WitnessMap,
198
+ convertOutputs: (outputs: WitnessMap, abi: Abi) => O,
199
+ ): Promise<PrivateKernelSimulateOutput<O>> {
200
+ this.log.debug(`Generating witness for ${circuitType}`);
201
+ const compiledCircuit: NoirCompiledCircuit = await this.artifactProvider.getClientCircuitArtifactByName(
202
+ circuitType,
203
+ );
204
+
205
+ const witnessMap = convertInputs(inputs, compiledCircuit.abi);
206
+ const timer = new Timer();
207
+ const outputWitness = await this.simulationProvider.executeProtocolCircuit(witnessMap, compiledCircuit);
208
+ const output = convertOutputs(outputWitness, compiledCircuit.abi);
209
+
210
+ this.log.debug(`Generated witness for ${circuitType}`, {
211
+ eventName: 'circuit-witness-generation',
212
+ circuitName: mapProtocolArtifactNameToCircuitName(circuitType),
213
+ duration: timer.ms(),
214
+ inputSize: inputs.toBuffer().length,
215
+ outputSize: output.toBuffer().length,
216
+ } satisfies CircuitWitnessGenerationStats);
217
+
218
+ const verificationKey = (await this.artifactProvider.getCircuitVkByName(circuitType)).keyAsFields;
219
+ const bytecode = Buffer.from(compiledCircuit.bytecode, 'base64');
220
+
221
+ const kernelOutput: PrivateKernelSimulateOutput<O> = {
222
+ publicInputs: output,
223
+ verificationKey,
224
+ outputWitness,
225
+ bytecode,
226
+ };
227
+ return kernelOutput;
228
+ }
229
+
230
+ public async makeEmptyKernelSimulateOutput<
231
+ PublicInputsType extends PrivateKernelTailCircuitPublicInputs | PrivateKernelCircuitPublicInputs,
232
+ >(publicInputs: PublicInputsType, circuitType: ClientProtocolArtifact) {
233
+ const kernelProofOutput: PrivateKernelSimulateOutput<PublicInputsType> = {
234
+ publicInputs,
235
+ verificationKey: (await this.artifactProvider.getCircuitVkByName(circuitType)).keyAsFields,
236
+ outputWitness: new Map(),
237
+ bytecode: Buffer.from([]),
238
+ };
239
+ return kernelProofOutput;
240
+ }
241
+
242
+ public createClientIvcProof(_acirs: Buffer[], _witnessStack: WitnessMap[]): Promise<ClientIvcProof> {
243
+ throw new Error('Not implemented');
244
+ }
245
+
246
+ public computeGateCountForCircuit(_bytecode: Buffer, _circuitName: string): Promise<number> {
247
+ throw new Error('Not implemented');
248
+ }
249
+ }