@aztec/bb-prover 0.0.1-commit.03f7ef2 → 0.0.1-commit.04d373f

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 (84) hide show
  1. package/dest/avm_proving_tests/avm_proving_tester.d.ts +13 -8
  2. package/dest/avm_proving_tests/avm_proving_tester.d.ts.map +1 -1
  3. package/dest/avm_proving_tests/avm_proving_tester.js +152 -107
  4. package/dest/bb/bb_js_backend.d.ts +196 -0
  5. package/dest/bb/bb_js_backend.d.ts.map +1 -0
  6. package/dest/bb/bb_js_backend.js +379 -0
  7. package/dest/bb/bb_js_debug.d.ts +52 -0
  8. package/dest/bb/bb_js_debug.d.ts.map +1 -0
  9. package/dest/bb/bb_js_debug.js +176 -0
  10. package/dest/bb/file_names.d.ts +4 -0
  11. package/dest/bb/file_names.d.ts.map +1 -0
  12. package/dest/bb/file_names.js +5 -0
  13. package/dest/config.d.ts +17 -1
  14. package/dest/config.d.ts.map +1 -1
  15. package/dest/index.d.ts +3 -2
  16. package/dest/index.d.ts.map +1 -1
  17. package/dest/index.js +2 -1
  18. package/dest/instrumentation.d.ts +1 -1
  19. package/dest/instrumentation.d.ts.map +1 -1
  20. package/dest/instrumentation.js +21 -43
  21. package/dest/prover/client/bb_private_kernel_prover.d.ts +18 -4
  22. package/dest/prover/client/bb_private_kernel_prover.d.ts.map +1 -1
  23. package/dest/prover/client/bb_private_kernel_prover.js +54 -8
  24. package/dest/prover/client/bundle.d.ts +3 -3
  25. package/dest/prover/client/bundle.d.ts.map +1 -1
  26. package/dest/prover/client/bundle.js +2 -3
  27. package/dest/prover/client/lazy.d.ts +3 -3
  28. package/dest/prover/client/lazy.d.ts.map +1 -1
  29. package/dest/prover/client/lazy.js +2 -3
  30. package/dest/prover/proof_utils.d.ts +11 -1
  31. package/dest/prover/proof_utils.d.ts.map +1 -1
  32. package/dest/prover/proof_utils.js +24 -1
  33. package/dest/prover/server/bb_prover.d.ts +8 -12
  34. package/dest/prover/server/bb_prover.d.ts.map +1 -1
  35. package/dest/prover/server/bb_prover.js +612 -107
  36. package/dest/test/delay_values.d.ts +1 -1
  37. package/dest/test/delay_values.d.ts.map +1 -1
  38. package/dest/test/delay_values.js +27 -25
  39. package/dest/test/index.d.ts +2 -1
  40. package/dest/test/index.d.ts.map +1 -1
  41. package/dest/test/index.js +1 -0
  42. package/dest/test/test_circuit_prover.d.ts +4 -4
  43. package/dest/test/test_circuit_prover.d.ts.map +1 -1
  44. package/dest/test/test_circuit_prover.js +462 -59
  45. package/dest/verification_key/verification_key_data.d.ts +1 -8
  46. package/dest/verification_key/verification_key_data.d.ts.map +1 -1
  47. package/dest/verification_key/verification_key_data.js +1 -20
  48. package/dest/verifier/batch_chonk_verifier.d.ts +45 -0
  49. package/dest/verifier/batch_chonk_verifier.d.ts.map +1 -0
  50. package/dest/verifier/batch_chonk_verifier.js +232 -0
  51. package/dest/verifier/bb_verifier.d.ts +4 -1
  52. package/dest/verifier/bb_verifier.d.ts.map +1 -1
  53. package/dest/verifier/bb_verifier.js +134 -48
  54. package/dest/verifier/index.d.ts +2 -1
  55. package/dest/verifier/index.d.ts.map +1 -1
  56. package/dest/verifier/index.js +1 -0
  57. package/dest/verifier/queued_chonk_verifier.d.ts +2 -3
  58. package/dest/verifier/queued_chonk_verifier.d.ts.map +1 -1
  59. package/dest/verifier/queued_chonk_verifier.js +15 -45
  60. package/package.json +20 -18
  61. package/src/avm_proving_tests/avm_proving_tester.ts +53 -136
  62. package/src/bb/bb_js_backend.ts +435 -0
  63. package/src/bb/bb_js_debug.ts +227 -0
  64. package/src/bb/file_names.ts +6 -0
  65. package/src/config.ts +16 -0
  66. package/src/index.ts +2 -1
  67. package/src/instrumentation.ts +20 -43
  68. package/src/prover/client/bb_private_kernel_prover.ts +135 -9
  69. package/src/prover/client/bundle.ts +3 -4
  70. package/src/prover/client/lazy.ts +3 -4
  71. package/src/prover/proof_utils.ts +41 -1
  72. package/src/prover/server/bb_prover.ts +138 -153
  73. package/src/test/delay_values.ts +28 -24
  74. package/src/test/index.ts +1 -0
  75. package/src/test/test_circuit_prover.ts +10 -13
  76. package/src/verification_key/verification_key_data.ts +1 -26
  77. package/src/verifier/batch_chonk_verifier.ts +276 -0
  78. package/src/verifier/bb_verifier.ts +66 -80
  79. package/src/verifier/index.ts +1 -0
  80. package/src/verifier/queued_chonk_verifier.ts +15 -47
  81. package/dest/bb/execute.d.ts +0 -107
  82. package/dest/bb/execute.d.ts.map +0 -1
  83. package/dest/bb/execute.js +0 -676
  84. package/src/bb/execute.ts +0 -706
package/src/index.ts CHANGED
@@ -2,7 +2,8 @@ export * from './prover/index.js';
2
2
  export * from './test/index.js';
3
3
  export * from './verifier/index.js';
4
4
  export * from './config.js';
5
- export * from './bb/execute.js';
5
+ export * from './bb/file_names.js';
6
+ export * from './bb/bb_js_backend.js';
6
7
  export * from './honk.js';
7
8
  export * from './verification_key/verification_key_data.js';
8
9
 
@@ -7,7 +7,6 @@ import {
7
7
  Metrics,
8
8
  type TelemetryClient,
9
9
  type Tracer,
10
- ValueType,
11
10
  } from '@aztec/telemetry-client';
12
11
 
13
12
  /**
@@ -31,51 +30,21 @@ export class ProverInstrumentation {
31
30
  this.tracer = telemetry.getTracer(name);
32
31
  const meter = telemetry.getMeter(name);
33
32
 
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
- });
33
+ this.simulationDuration = meter.createHistogram(Metrics.CIRCUIT_SIMULATION_DURATION);
39
34
 
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
- });
35
+ this.witGenDuration = meter.createHistogram(Metrics.CIRCUIT_WITNESS_GEN_DURATION);
45
36
 
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
- });
37
+ this.provingDuration = meter.createHistogram(Metrics.CIRCUIT_PROVING_DURATION);
51
38
 
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
- });
39
+ this.witGenInputSize = meter.createGauge(Metrics.CIRCUIT_WITNESS_GEN_INPUT_SIZE);
57
40
 
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
- });
41
+ this.witGenOutputSize = meter.createGauge(Metrics.CIRCUIT_WITNESS_GEN_OUTPUT_SIZE);
63
42
 
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
- });
43
+ this.proofSize = meter.createGauge(Metrics.CIRCUIT_PROVING_PROOF_SIZE);
69
44
 
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
- });
45
+ this.circuitPublicInputCount = meter.createGauge(Metrics.CIRCUIT_PUBLIC_INPUTS_COUNT);
74
46
 
75
- this.circuitSize = meter.createGauge(Metrics.CIRCUIT_SIZE, {
76
- description: 'Records the size of the circuit in gates',
77
- valueType: ValueType.INT,
78
- });
47
+ this.circuitSize = meter.createGauge(Metrics.CIRCUIT_SIZE);
79
48
  }
80
49
 
81
50
  /**
@@ -89,10 +58,18 @@ export class ProverInstrumentation {
89
58
  circuitName: CircuitName,
90
59
  timerOrMS: Timer | number,
91
60
  ) {
92
- const s = typeof timerOrMS === 'number' ? timerOrMS / 1000 : timerOrMS.s();
93
- this[metric].record(s, {
94
- [Attributes.PROTOCOL_CIRCUIT_NAME]: circuitName,
95
- });
61
+ // Simulation duration is stored in ms, while the others are stored in seconds
62
+ if (metric === 'simulationDuration') {
63
+ const ms = typeof timerOrMS === 'number' ? timerOrMS : timerOrMS.ms();
64
+ this[metric].record(Math.trunc(ms), {
65
+ [Attributes.PROTOCOL_CIRCUIT_NAME]: circuitName,
66
+ });
67
+ } else {
68
+ const s = typeof timerOrMS === 'number' ? timerOrMS / 1000 : timerOrMS.s();
69
+ this[metric].record(s, {
70
+ [Attributes.PROTOCOL_CIRCUIT_NAME]: circuitName,
71
+ });
72
+ }
96
73
  }
97
74
 
98
75
  /**
@@ -1,12 +1,20 @@
1
- import { AztecClientBackend, Barretenberg } from '@aztec/bb.js';
2
- import { createLogger } from '@aztec/foundation/log';
1
+ import { AztecClientBackend, type BackendOptions, Barretenberg } from '@aztec/bb.js';
2
+ import { type LogLevel, type Logger, createLogger } from '@aztec/foundation/log';
3
3
  import { Timer } from '@aztec/foundation/timer';
4
4
  import { serializeWitness } from '@aztec/noir-noirc_abi';
5
5
  import {
6
6
  convertHidingKernelPublicInputsToWitnessMapWithAbi,
7
7
  convertHidingKernelToRollupInputsToWitnessMapWithAbi,
8
+ convertPrivateKernelInit2InputsToWitnessMapWithAbi,
9
+ convertPrivateKernelInit2OutputsFromWitnessMapWithAbi,
10
+ convertPrivateKernelInit3InputsToWitnessMapWithAbi,
11
+ convertPrivateKernelInit3OutputsFromWitnessMapWithAbi,
8
12
  convertPrivateKernelInitInputsToWitnessMapWithAbi,
9
13
  convertPrivateKernelInitOutputsFromWitnessMapWithAbi,
14
+ convertPrivateKernelInner2InputsToWitnessMapWithAbi,
15
+ convertPrivateKernelInner2OutputsFromWitnessMapWithAbi,
16
+ convertPrivateKernelInner3InputsToWitnessMapWithAbi,
17
+ convertPrivateKernelInner3OutputsFromWitnessMapWithAbi,
10
18
  convertPrivateKernelInnerInputsToWitnessMapWithAbi,
11
19
  convertPrivateKernelInnerOutputsFromWitnessMapWithAbi,
12
20
  convertPrivateKernelResetInputsToWitnessMapWithAbi,
@@ -32,7 +40,11 @@ import type {
32
40
  HidingKernelToRollupPrivateInputs,
33
41
  PrivateExecutionStep,
34
42
  PrivateKernelCircuitPublicInputs,
43
+ PrivateKernelInit2CircuitPrivateInputs,
44
+ PrivateKernelInit3CircuitPrivateInputs,
35
45
  PrivateKernelInitCircuitPrivateInputs,
46
+ PrivateKernelInner2CircuitPrivateInputs,
47
+ PrivateKernelInner3CircuitPrivateInputs,
36
48
  PrivateKernelInnerCircuitPrivateInputs,
37
49
  PrivateKernelResetCircuitPrivateInputs,
38
50
  PrivateKernelSimulateOutput,
@@ -45,12 +57,17 @@ import type { CircuitSimulationStats, CircuitWitnessGenerationStats } from '@azt
45
57
 
46
58
  import { ungzip } from 'pako';
47
59
 
60
+ export type BBPrivateKernelProverOptions = Omit<BackendOptions, 'logger'> & { logger?: Logger };
48
61
  export abstract class BBPrivateKernelProver implements PrivateKernelProver {
62
+ private log: Logger;
63
+
49
64
  constructor(
50
65
  protected artifactProvider: ArtifactProvider,
51
66
  protected simulator: CircuitSimulator,
52
- protected log = createLogger('bb-prover'),
53
- ) {}
67
+ protected options: BBPrivateKernelProverOptions = {},
68
+ ) {
69
+ this.log = options.logger || createLogger('bb-prover:private-kernel');
70
+ }
54
71
 
55
72
  public async generateInitOutput(
56
73
  inputs: PrivateKernelInitCircuitPrivateInputs,
@@ -74,6 +91,50 @@ export abstract class BBPrivateKernelProver implements PrivateKernelProver {
74
91
  );
75
92
  }
76
93
 
94
+ public async generateInit2Output(
95
+ inputs: PrivateKernelInit2CircuitPrivateInputs,
96
+ ): Promise<PrivateKernelSimulateOutput<PrivateKernelCircuitPublicInputs>> {
97
+ return await this.generateCircuitOutput(
98
+ inputs,
99
+ 'PrivateKernelInit2Artifact',
100
+ convertPrivateKernelInit2InputsToWitnessMapWithAbi,
101
+ convertPrivateKernelInit2OutputsFromWitnessMapWithAbi,
102
+ );
103
+ }
104
+
105
+ public async simulateInit2(
106
+ inputs: PrivateKernelInit2CircuitPrivateInputs,
107
+ ): Promise<PrivateKernelSimulateOutput<PrivateKernelCircuitPublicInputs>> {
108
+ return await this.simulateCircuitOutput(
109
+ inputs,
110
+ 'PrivateKernelInit2Artifact',
111
+ convertPrivateKernelInit2InputsToWitnessMapWithAbi,
112
+ convertPrivateKernelInit2OutputsFromWitnessMapWithAbi,
113
+ );
114
+ }
115
+
116
+ public async generateInit3Output(
117
+ inputs: PrivateKernelInit3CircuitPrivateInputs,
118
+ ): Promise<PrivateKernelSimulateOutput<PrivateKernelCircuitPublicInputs>> {
119
+ return await this.generateCircuitOutput(
120
+ inputs,
121
+ 'PrivateKernelInit3Artifact',
122
+ convertPrivateKernelInit3InputsToWitnessMapWithAbi,
123
+ convertPrivateKernelInit3OutputsFromWitnessMapWithAbi,
124
+ );
125
+ }
126
+
127
+ public async simulateInit3(
128
+ inputs: PrivateKernelInit3CircuitPrivateInputs,
129
+ ): Promise<PrivateKernelSimulateOutput<PrivateKernelCircuitPublicInputs>> {
130
+ return await this.simulateCircuitOutput(
131
+ inputs,
132
+ 'PrivateKernelInit3Artifact',
133
+ convertPrivateKernelInit3InputsToWitnessMapWithAbi,
134
+ convertPrivateKernelInit3OutputsFromWitnessMapWithAbi,
135
+ );
136
+ }
137
+
77
138
  public async generateInnerOutput(
78
139
  inputs: PrivateKernelInnerCircuitPrivateInputs,
79
140
  ): Promise<PrivateKernelSimulateOutput<PrivateKernelCircuitPublicInputs>> {
@@ -96,6 +157,50 @@ export abstract class BBPrivateKernelProver implements PrivateKernelProver {
96
157
  );
97
158
  }
98
159
 
160
+ public async generateInner2Output(
161
+ inputs: PrivateKernelInner2CircuitPrivateInputs,
162
+ ): Promise<PrivateKernelSimulateOutput<PrivateKernelCircuitPublicInputs>> {
163
+ return await this.generateCircuitOutput(
164
+ inputs,
165
+ 'PrivateKernelInner2Artifact',
166
+ convertPrivateKernelInner2InputsToWitnessMapWithAbi,
167
+ convertPrivateKernelInner2OutputsFromWitnessMapWithAbi,
168
+ );
169
+ }
170
+
171
+ public async simulateInner2(
172
+ inputs: PrivateKernelInner2CircuitPrivateInputs,
173
+ ): Promise<PrivateKernelSimulateOutput<PrivateKernelCircuitPublicInputs>> {
174
+ return await this.simulateCircuitOutput(
175
+ inputs,
176
+ 'PrivateKernelInner2Artifact',
177
+ convertPrivateKernelInner2InputsToWitnessMapWithAbi,
178
+ convertPrivateKernelInner2OutputsFromWitnessMapWithAbi,
179
+ );
180
+ }
181
+
182
+ public async generateInner3Output(
183
+ inputs: PrivateKernelInner3CircuitPrivateInputs,
184
+ ): Promise<PrivateKernelSimulateOutput<PrivateKernelCircuitPublicInputs>> {
185
+ return await this.generateCircuitOutput(
186
+ inputs,
187
+ 'PrivateKernelInner3Artifact',
188
+ convertPrivateKernelInner3InputsToWitnessMapWithAbi,
189
+ convertPrivateKernelInner3OutputsFromWitnessMapWithAbi,
190
+ );
191
+ }
192
+
193
+ public async simulateInner3(
194
+ inputs: PrivateKernelInner3CircuitPrivateInputs,
195
+ ): Promise<PrivateKernelSimulateOutput<PrivateKernelCircuitPublicInputs>> {
196
+ return await this.simulateCircuitOutput(
197
+ inputs,
198
+ 'PrivateKernelInner3Artifact',
199
+ convertPrivateKernelInner3InputsToWitnessMapWithAbi,
200
+ convertPrivateKernelInner3OutputsFromWitnessMapWithAbi,
201
+ );
202
+ }
203
+
99
204
  public async generateResetOutput(
100
205
  inputs: PrivateKernelResetCircuitPrivateInputs,
101
206
  ): Promise<PrivateKernelSimulateOutput<PrivateKernelCircuitPublicInputs>> {
@@ -271,26 +376,47 @@ export abstract class BBPrivateKernelProver implements PrivateKernelProver {
271
376
  public async createChonkProof(executionSteps: PrivateExecutionStep[]): Promise<ChonkProofWithPublicInputs> {
272
377
  const timer = new Timer();
273
378
  this.log.info(`Generating ClientIVC proof...`);
379
+ const barretenberg = await Barretenberg.initSingleton({
380
+ ...this.options,
381
+ logger: this.options.logger?.verbose,
382
+ });
274
383
  const backend = new AztecClientBackend(
275
384
  executionSteps.map(step => ungzip(step.bytecode)),
276
- await Barretenberg.initSingleton(),
385
+ barretenberg,
386
+ executionSteps.map(step => step.functionName),
277
387
  );
278
388
 
279
- const [proof] = await backend.prove(
389
+ // Use compressed prove path to get both proof fields and compressed proof bytes
390
+ const result = await backend.prove(
280
391
  executionSteps.map(step => ungzip(serializeWitness(step.witness))),
281
392
  executionSteps.map(step => step.vk),
393
+ { compress: true },
282
394
  );
283
395
  this.log.info(`Generated ClientIVC proof`, {
284
396
  eventName: 'client-ivc-proof-generation',
285
397
  duration: timer.ms(),
286
- proofSize: proof.length,
398
+ proofSize: result.proofFields.length,
399
+ compressedSize: result.compressedProof?.length,
287
400
  });
288
- return ChonkProofWithPublicInputs.fromBufferArray(proof);
401
+
402
+ // Create ChonkProofWithPublicInputs from the flat field elements
403
+ const proofWithPubInputs = ChonkProofWithPublicInputs.fromBufferArray(result.proofFields);
404
+
405
+ // Attach compressed proof bytes to the ChonkProof (without public inputs).
406
+ // The compressed bytes are for the full proof WITH public inputs from bb;
407
+ // when deserializing, the decompressor will strip them to match CHONK_PROOF_LENGTH.
408
+ proofWithPubInputs.compressedProof = result.compressedProof ? Buffer.from(result.compressedProof) : undefined;
409
+
410
+ return proofWithPubInputs;
289
411
  }
290
412
 
291
413
  public async computeGateCountForCircuit(_bytecode: Buffer, _circuitName: string): Promise<number> {
292
414
  // Note we do not pass the vk to the backend. This is unneeded for gate counts.
293
- const backend = new AztecClientBackend([ungzip(_bytecode)], await Barretenberg.initSingleton());
415
+ const barretenberg = await Barretenberg.initSingleton({
416
+ ...this.options,
417
+ logger: this.options.logger?.[(process.env.LOG_LEVEL as LogLevel) || 'verbose'],
418
+ });
419
+ const backend = new AztecClientBackend([ungzip(_bytecode)], barretenberg, [_circuitName]);
294
420
  const gateCount = await backend.gates();
295
421
  return gateCount[0];
296
422
  }
@@ -1,11 +1,10 @@
1
- import { createLogger } from '@aztec/foundation/log';
2
1
  import { BundleArtifactProvider } from '@aztec/noir-protocol-circuits-types/client/bundle';
3
2
  import type { CircuitSimulator } from '@aztec/simulator/client';
4
3
 
5
- import { BBPrivateKernelProver } from './bb_private_kernel_prover.js';
4
+ import { BBPrivateKernelProver, type BBPrivateKernelProverOptions } from './bb_private_kernel_prover.js';
6
5
 
7
6
  export class BBBundlePrivateKernelProver extends BBPrivateKernelProver {
8
- constructor(simulator: CircuitSimulator, log = createLogger('bb-prover:bundle')) {
9
- super(new BundleArtifactProvider(), simulator, log);
7
+ constructor(simulator: CircuitSimulator, options: BBPrivateKernelProverOptions = {}) {
8
+ super(new BundleArtifactProvider(), simulator, options);
10
9
  }
11
10
  }
@@ -1,11 +1,10 @@
1
- import { createLogger } from '@aztec/foundation/log';
2
1
  import { LazyArtifactProvider } from '@aztec/noir-protocol-circuits-types/client/lazy';
3
2
  import type { CircuitSimulator } from '@aztec/simulator/client';
4
3
 
5
- import { BBPrivateKernelProver } from './bb_private_kernel_prover.js';
4
+ import { BBPrivateKernelProver, type BBPrivateKernelProverOptions } from './bb_private_kernel_prover.js';
6
5
 
7
6
  export class BBLazyPrivateKernelProver extends BBPrivateKernelProver {
8
- constructor(simulator: CircuitSimulator, log = createLogger('bb-prover:lazy')) {
9
- super(new LazyArtifactProvider(), simulator, log);
7
+ constructor(simulator: CircuitSimulator, options: BBPrivateKernelProverOptions = {}) {
8
+ super(new LazyArtifactProvider(), simulator, options);
10
9
  }
11
10
  }
@@ -16,7 +16,7 @@ import assert from 'assert';
16
16
  import { promises as fs } from 'fs';
17
17
  import * as path from 'path';
18
18
 
19
- import { PROOF_FILENAME, PUBLIC_INPUTS_FILENAME } from '../bb/execute.js';
19
+ import { PROOF_FILENAME, PUBLIC_INPUTS_FILENAME } from '../bb/file_names.js';
20
20
 
21
21
  /**
22
22
  * Create a ChonkProof proof file.
@@ -113,3 +113,43 @@ export async function readProofsFromOutputDirectory<PROOF_LENGTH extends number>
113
113
  proofLength,
114
114
  );
115
115
  }
116
+
117
+ /**
118
+ * Construct a RecursiveProof from in-memory proof and public input field arrays
119
+ * returned by the bb.js circuitProve API, without reading from files.
120
+ *
121
+ * @param proofFields - Proof fields as 32-byte Uint8Arrays from circuitProve.
122
+ * @param publicInputFields - Public input fields as 32-byte Uint8Arrays from circuitProve.
123
+ * @param vkData - Verification key data for the circuit.
124
+ * @param proofLength - Expected proof field count.
125
+ */
126
+ export function constructRecursiveProofFromBuffers<PROOF_LENGTH extends number>(
127
+ proofFields: Uint8Array[],
128
+ publicInputFields: Uint8Array[],
129
+ vkData: VerificationKeyData,
130
+ proofLength: PROOF_LENGTH,
131
+ ): RecursiveProof<PROOF_LENGTH> {
132
+ assert(
133
+ proofLength == NESTED_RECURSIVE_PROOF_LENGTH ||
134
+ proofLength == NESTED_RECURSIVE_ROLLUP_HONK_PROOF_LENGTH ||
135
+ proofLength == ULTRA_KECCAK_PROOF_LENGTH,
136
+ `Proof length must be one of the expected proof lengths, received ${proofLength}`,
137
+ );
138
+
139
+ // Convert Uint8Array fields to Fr instances
140
+ const proofFieldsFr = proofFields.map(f => Fr.fromBuffer(Buffer.from(f)));
141
+
142
+ assert(
143
+ proofFieldsFr.length == proofLength,
144
+ `Proof fields length mismatch: ${proofFieldsFr.length} != ${proofLength}`,
145
+ );
146
+
147
+ // Reconstruct the binary proof with public inputs prepended (same format as file-based path)
148
+ const binaryPublicInputs = Buffer.concat(publicInputFields.map(f => Buffer.from(f)));
149
+ const binaryProof = Buffer.concat(proofFields.map(f => Buffer.from(f)));
150
+ const binaryProofWithPublicInputs = Buffer.concat([binaryPublicInputs, binaryProof]);
151
+
152
+ const numPublicInputs = getNumCustomPublicInputs(proofLength, vkData);
153
+
154
+ return new RecursiveProof(proofFieldsFr, new Proof(binaryProofWithPublicInputs, numPublicInputs), true, proofLength);
155
+ }