-
Notifications
You must be signed in to change notification settings - Fork 599
Expand file tree
/
Copy pathbb_prover.ts
More file actions
1012 lines (876 loc) · 39.5 KB
/
bb_prover.ts
File metadata and controls
1012 lines (876 loc) · 39.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* eslint-disable require-await */
import {
type AvmProofAndVerificationKey,
type PublicInputsAndRecursiveProof,
type PublicKernelNonTailRequest,
type PublicKernelTailRequest,
type ServerCircuitProver,
makePublicInputsAndRecursiveProof,
} from '@aztec/circuit-types';
import { type CircuitProvingStats, type CircuitWitnessGenerationStats } from '@aztec/circuit-types/stats';
import {
AGGREGATION_OBJECT_LENGTH,
type AvmCircuitInputs,
type AvmVerificationKeyData,
type BaseOrMergeRollupPublicInputs,
type BaseParityInputs,
type BaseRollupInputs,
type BlockMergeRollupInputs,
type BlockRootOrBlockMergePublicInputs,
type BlockRootRollupInputs,
EmptyNestedCircuitInputs,
EmptyNestedData,
Fr,
type KernelCircuitPublicInputs,
type MergeRollupInputs,
NESTED_RECURSIVE_PROOF_LENGTH,
type PrivateKernelEmptyInputData,
PrivateKernelEmptyInputs,
Proof,
type PublicKernelCircuitPublicInputs,
RECURSIVE_PROOF_LENGTH,
RecursiveProof,
RootParityInput,
type RootParityInputs,
type RootRollupInputs,
type RootRollupPublicInputs,
TUBE_PROOF_LENGTH,
type TubeInputs,
type VerificationKeyAsFields,
type VerificationKeyData,
makeRecursiveProofFromBinary,
} from '@aztec/circuits.js';
import { runInDirectory } from '@aztec/foundation/fs';
import { createDebugLogger } from '@aztec/foundation/log';
import { Timer } from '@aztec/foundation/timer';
import {
ProtocolCircuitVkIndexes,
ServerCircuitArtifacts,
type ServerProtocolArtifact,
convertBaseParityInputsToWitnessMap,
convertBaseParityOutputsFromWitnessMap,
convertBaseRollupInputsToWitnessMap,
convertBaseRollupOutputsFromWitnessMap,
convertBlockMergeRollupInputsToWitnessMap,
convertBlockMergeRollupOutputsFromWitnessMap,
convertBlockRootRollupInputsToWitnessMap,
convertBlockRootRollupOutputsFromWitnessMap,
convertMergeRollupInputsToWitnessMap,
convertMergeRollupOutputsFromWitnessMap,
convertPrivateKernelEmptyInputsToWitnessMap,
convertPrivateKernelEmptyOutputsFromWitnessMap,
convertPublicTailInputsToWitnessMap,
convertPublicTailOutputFromWitnessMap,
convertRootParityInputsToWitnessMap,
convertRootParityOutputsFromWitnessMap,
convertRootRollupInputsToWitnessMap,
convertRootRollupOutputsFromWitnessMap,
getVKSiblingPath,
} from '@aztec/noir-protocol-circuits-types';
import { NativeACVMSimulator } from '@aztec/simulator';
import { Attributes, type TelemetryClient, trackSpan } from '@aztec/telemetry-client';
import { abiEncode } from '@noir-lang/noirc_abi';
import { type Abi, type WitnessMap } from '@noir-lang/types';
import crypto from 'crypto';
import * as fs from 'fs/promises';
import * as path from 'path';
import {
type BBFailure,
type BBSuccess,
BB_RESULT,
PROOF_FIELDS_FILENAME,
PROOF_FILENAME,
VK_FILENAME,
generateAvmProof,
generateKeyForNoirCircuit,
generateProof,
generateTubeProof,
verifyAvmProof,
verifyProof,
writeProofAsFields,
} from '../bb/execute.js';
import type { ACVMConfig, BBConfig } from '../config.js';
import { type UltraHonkFlavor, getUltraHonkFlavorForCircuit } from '../honk.js';
import { ProverInstrumentation } from '../instrumentation.js';
import { PublicKernelArtifactMapping } from '../mappings/mappings.js';
import { mapProtocolArtifactNameToCircuitName } from '../stats.js';
import { extractAvmVkData, extractVkData } from '../verification_key/verification_key_data.js';
const logger = createDebugLogger('aztec:bb-prover');
export interface BBProverConfig extends BBConfig, ACVMConfig {
// list of circuits supported by this prover. defaults to all circuits if empty
circuitFilter?: ServerProtocolArtifact[];
}
/**
* Prover implementation that uses barretenberg native proving
*/
export class BBNativeRollupProver implements ServerCircuitProver {
private verificationKeys = new Map<
`ultra${'_keccak_' | '_'}honk_${ServerProtocolArtifact}`,
Promise<VerificationKeyData>
>();
private instrumentation: ProverInstrumentation;
constructor(private config: BBProverConfig, telemetry: TelemetryClient) {
this.instrumentation = new ProverInstrumentation(telemetry, 'BBNativeRollupProver');
}
get tracer() {
return this.instrumentation.tracer;
}
static async new(config: BBProverConfig, telemetry: TelemetryClient) {
await fs.access(config.acvmBinaryPath, fs.constants.R_OK);
await fs.mkdir(config.acvmWorkingDirectory, { recursive: true });
await fs.access(config.bbBinaryPath, fs.constants.R_OK);
await fs.mkdir(config.bbWorkingDirectory, { recursive: true });
logger.info(`Using native BB at ${config.bbBinaryPath} and working directory ${config.bbWorkingDirectory}`);
logger.info(`Using native ACVM at ${config.acvmBinaryPath} and working directory ${config.acvmWorkingDirectory}`);
return new BBNativeRollupProver(config, telemetry);
}
/**
* Simulates the base parity circuit from its inputs.
* @param inputs - Inputs to the circuit.
* @returns The public inputs of the parity circuit.
*/
@trackSpan('BBNativeRollupProver.getBaseParityProof', { [Attributes.PROTOCOL_CIRCUIT_NAME]: 'base-parity' })
public async getBaseParityProof(inputs: BaseParityInputs): Promise<RootParityInput<typeof RECURSIVE_PROOF_LENGTH>> {
const { circuitOutput, proof } = await this.createRecursiveProof(
inputs,
'BaseParityArtifact',
RECURSIVE_PROOF_LENGTH,
convertBaseParityInputsToWitnessMap,
convertBaseParityOutputsFromWitnessMap,
);
const verificationKey = await this.getVerificationKeyDataForCircuit('BaseParityArtifact');
await this.verifyProof('BaseParityArtifact', proof.binaryProof);
return new RootParityInput(
proof,
verificationKey.keyAsFields,
getVKSiblingPath(ProtocolCircuitVkIndexes.BaseParityArtifact),
circuitOutput,
);
}
/**
* Simulates the root parity circuit from its inputs.
* @param inputs - Inputs to the circuit.
* @returns The public inputs of the parity circuit.
*/
@trackSpan('BBNativeRollupProver.getRootParityProof', { [Attributes.PROTOCOL_CIRCUIT_NAME]: 'root-parity' })
public async getRootParityProof(
inputs: RootParityInputs,
): Promise<RootParityInput<typeof NESTED_RECURSIVE_PROOF_LENGTH>> {
const { circuitOutput, proof } = await this.createRecursiveProof(
inputs,
'RootParityArtifact',
NESTED_RECURSIVE_PROOF_LENGTH,
convertRootParityInputsToWitnessMap,
convertRootParityOutputsFromWitnessMap,
);
const verificationKey = await this.getVerificationKeyDataForCircuit('RootParityArtifact');
await this.verifyProof('RootParityArtifact', proof.binaryProof);
return new RootParityInput(
proof,
verificationKey.keyAsFields,
getVKSiblingPath(ProtocolCircuitVkIndexes.RootParityArtifact),
circuitOutput,
);
}
/**
* Creates an AVM proof and verifies it.
* @param inputs - The inputs to the AVM circuit.
* @returns The proof.
*/
@trackSpan('BBNativeRollupProver.getAvmProof', inputs => ({
[Attributes.APP_CIRCUIT_NAME]: inputs.functionName,
}))
public async getAvmProof(inputs: AvmCircuitInputs): Promise<AvmProofAndVerificationKey> {
const proofAndVk = await this.createAvmProof(inputs);
await this.verifyAvmProof(proofAndVk.proof, proofAndVk.verificationKey);
return proofAndVk;
}
/**
* Requests that a public kernel circuit be executed and the proof generated
* @param kernelRequest - The object encapsulating the request for a proof
* @returns The requested circuit's public inputs and proof
*/
@trackSpan('BBNativeRollupProver.getPublicKernelProof', kernelReq => ({
[Attributes.PROTOCOL_CIRCUIT_NAME]: mapProtocolArtifactNameToCircuitName(
PublicKernelArtifactMapping[kernelReq.type]!.artifact,
),
}))
public async getPublicKernelProof(
kernelRequest: PublicKernelNonTailRequest,
): Promise<PublicInputsAndRecursiveProof<PublicKernelCircuitPublicInputs>> {
const kernelOps = PublicKernelArtifactMapping[kernelRequest.type];
if (kernelOps === undefined) {
throw new Error(`Unable to prove kernel type ${kernelRequest.type}`);
}
// We may need to convert the recursive proof into fields format
kernelRequest.inputs.previousKernel.proof = await this.ensureValidProof(
kernelRequest.inputs.previousKernel.proof,
kernelOps.artifact,
kernelRequest.inputs.previousKernel.vk,
);
await this.verifyWithKey(
getUltraHonkFlavorForCircuit(kernelOps.artifact),
kernelRequest.inputs.previousKernel.vk,
kernelRequest.inputs.previousKernel.proof.binaryProof,
);
const { circuitOutput, proof } = await this.createRecursiveProof(
kernelRequest.inputs,
kernelOps.artifact,
NESTED_RECURSIVE_PROOF_LENGTH,
kernelOps.convertInputs,
kernelOps.convertOutputs,
);
const verificationKey = await this.getVerificationKeyDataForCircuit(kernelOps.artifact);
await this.verifyProof(kernelOps.artifact, proof.binaryProof);
return makePublicInputsAndRecursiveProof(circuitOutput, proof, verificationKey);
}
/**
* Requests that the public kernel tail circuit be executed and the proof generated
* @param kernelRequest - The object encapsulating the request for a proof
* @returns The requested circuit's public inputs and proof
*/
public async getPublicTailProof(
kernelRequest: PublicKernelTailRequest,
): Promise<PublicInputsAndRecursiveProof<KernelCircuitPublicInputs>> {
const { circuitOutput, proof } = await this.createRecursiveProof(
kernelRequest.inputs,
'PublicKernelTailArtifact',
NESTED_RECURSIVE_PROOF_LENGTH,
convertPublicTailInputsToWitnessMap,
convertPublicTailOutputFromWitnessMap,
);
const verificationKey = await this.getVerificationKeyDataForCircuit('PublicKernelTailArtifact');
await this.verifyProof('PublicKernelTailArtifact', proof.binaryProof);
return makePublicInputsAndRecursiveProof(circuitOutput, proof, verificationKey);
}
/**
* Simulates the base rollup circuit from its inputs.
* @param baseRollupInput - Inputs to the circuit.
* @returns The public inputs as outputs of the simulation.
*/
public async getBaseRollupProof(
baseRollupInput: BaseRollupInputs, // TODO: remove tail proof from here
): Promise<PublicInputsAndRecursiveProof<BaseOrMergeRollupPublicInputs>> {
// We may need to convert the recursive proof into fields format
logger.debug(`kernel Data proof: ${baseRollupInput.kernelData.proof}`);
logger.debug(`Number of public inputs in baseRollupInput: ${baseRollupInput.kernelData.vk.numPublicInputs}`);
baseRollupInput.kernelData.proof = await this.ensureValidProof(
baseRollupInput.kernelData.proof,
'BaseRollupArtifact',
baseRollupInput.kernelData.vk,
);
const { circuitOutput, proof } = await this.createRecursiveProof(
baseRollupInput, // BaseRollupInputs
'BaseRollupArtifact',
NESTED_RECURSIVE_PROOF_LENGTH, // WORKTODO: this should be BASE_ROLLUP_PROOF_LENGTH or something like this
convertBaseRollupInputsToWitnessMap,
convertBaseRollupOutputsFromWitnessMap,
);
const verificationKey = await this.getVerificationKeyDataForCircuit('BaseRollupArtifact');
await this.verifyProof('BaseRollupArtifact', proof.binaryProof);
return makePublicInputsAndRecursiveProof(circuitOutput, proof, verificationKey);
}
/**
* Simulates the merge rollup circuit from its inputs.
* @param input - Inputs to the circuit.
* @returns The public inputs as outputs of the simulation.
*/
public async getMergeRollupProof(
input: MergeRollupInputs,
): Promise<PublicInputsAndRecursiveProof<BaseOrMergeRollupPublicInputs>> {
const { circuitOutput, proof } = await this.createRecursiveProof(
input,
'MergeRollupArtifact',
NESTED_RECURSIVE_PROOF_LENGTH,
convertMergeRollupInputsToWitnessMap,
convertMergeRollupOutputsFromWitnessMap,
);
const verificationKey = await this.getVerificationKeyDataForCircuit('MergeRollupArtifact');
await this.verifyProof('MergeRollupArtifact', proof.binaryProof);
return makePublicInputsAndRecursiveProof(circuitOutput, proof, verificationKey);
}
/**
* Simulates the block root rollup circuit from its inputs.
* @param input - Inputs to the circuit.
* @returns The public inputs as outputs of the simulation.
*/
public async getBlockRootRollupProof(
input: BlockRootRollupInputs,
): Promise<PublicInputsAndRecursiveProof<BlockRootOrBlockMergePublicInputs>> {
// TODO(#7346): When batch rollups are integrated, we probably want the below to be this.createRecursiveProof
// since we will no longer be verifying it directly on L1
const { circuitOutput, proof } = await this.createProof(
input,
'BlockRootRollupArtifact',
convertBlockRootRollupInputsToWitnessMap,
convertBlockRootRollupOutputsFromWitnessMap,
);
const recursiveProof = makeRecursiveProofFromBinary(proof, NESTED_RECURSIVE_PROOF_LENGTH);
const verificationKey = await this.getVerificationKeyDataForCircuit('BlockRootRollupArtifact');
await this.verifyProof('BlockRootRollupArtifact', proof);
return makePublicInputsAndRecursiveProof(circuitOutput, recursiveProof, verificationKey);
}
/**
* Simulates the block merge rollup circuit from its inputs.
* @param input - Inputs to the circuit.
* @returns The public inputs as outputs of the simulation.
*/
public async getBlockMergeRollupProof(
input: BlockMergeRollupInputs,
): Promise<PublicInputsAndRecursiveProof<BlockRootOrBlockMergePublicInputs>> {
const { circuitOutput, proof } = await this.createRecursiveProof(
input,
'BlockMergeRollupArtifact',
NESTED_RECURSIVE_PROOF_LENGTH,
convertBlockMergeRollupInputsToWitnessMap,
convertBlockMergeRollupOutputsFromWitnessMap,
);
const verificationKey = await this.getVerificationKeyDataForCircuit('BlockMergeRollupArtifact');
await this.verifyProof('BlockMergeRollupArtifact', proof.binaryProof);
return makePublicInputsAndRecursiveProof(circuitOutput, proof, verificationKey);
}
/**
* Simulates the root rollup circuit from its inputs.
* @param input - Inputs to the circuit.
* @returns The public inputs as outputs of the simulation.
*/
public async getRootRollupProof(
input: RootRollupInputs,
): Promise<PublicInputsAndRecursiveProof<RootRollupPublicInputs>> {
const { circuitOutput, proof } = await this.createProof(
input,
'RootRollupArtifact',
convertRootRollupInputsToWitnessMap,
convertRootRollupOutputsFromWitnessMap,
);
const recursiveProof = makeRecursiveProofFromBinary(proof, NESTED_RECURSIVE_PROOF_LENGTH);
const verificationKey = await this.getVerificationKeyDataForCircuit('RootRollupArtifact');
await this.verifyProof('RootRollupArtifact', proof);
return makePublicInputsAndRecursiveProof(circuitOutput, recursiveProof, verificationKey);
}
public async getEmptyPrivateKernelProof(
inputs: PrivateKernelEmptyInputData,
): Promise<PublicInputsAndRecursiveProof<KernelCircuitPublicInputs>> {
const emptyNested = await this.getEmptyNestedProof();
const emptyPrivateKernelProof = await this.getEmptyPrivateKernelProofFromEmptyNested(
PrivateKernelEmptyInputs.from({
...inputs,
emptyNested,
}),
);
return emptyPrivateKernelProof;
}
public async getEmptyTubeProof(
inputs: PrivateKernelEmptyInputData,
): Promise<PublicInputsAndRecursiveProof<KernelCircuitPublicInputs>> {
const emptyNested = await this.getEmptyNestedProof();
const emptyPrivateKernelProof = await this.getEmptyTubeProofFromEmptyNested(
PrivateKernelEmptyInputs.from({
...inputs,
emptyNested,
}),
);
return emptyPrivateKernelProof;
}
private async getEmptyNestedProof(): Promise<EmptyNestedData> {
const inputs = new EmptyNestedCircuitInputs();
const { proof } = await this.createRecursiveProof(
inputs,
'EmptyNestedArtifact',
RECURSIVE_PROOF_LENGTH,
(nothing: any) => abiEncode(ServerCircuitArtifacts.EmptyNestedArtifact.abi as Abi, { _inputs: nothing as any }),
() => new EmptyNestedCircuitInputs(),
);
const verificationKey = await this.getVerificationKeyDataForCircuit('EmptyNestedArtifact');
await this.verifyProof('EmptyNestedArtifact', proof.binaryProof);
// logger.debug(`EmptyNestedData proof size: ${proof.proof.length}`);
// logger.debug(`EmptyNestedData proof: ${proof.proof}`);
// logger.debug(`EmptyNestedData vk size: ${verificationKey.keyAsFields.key.length}`);
// logger.debug(`EmptyNestedData vk: ${verificationKey.keyAsFields.key}`);
return new EmptyNestedData(proof, verificationKey.keyAsFields);
}
private async getEmptyTubeProofFromEmptyNested(
inputs: PrivateKernelEmptyInputs,
): Promise<PublicInputsAndRecursiveProof<KernelCircuitPublicInputs>> {
const { circuitOutput, proof } = await this.createRecursiveProof(
inputs,
'PrivateKernelEmptyArtifact',
NESTED_RECURSIVE_PROOF_LENGTH,
convertPrivateKernelEmptyInputsToWitnessMap,
convertPrivateKernelEmptyOutputsFromWitnessMap,
);
// info(`proof: ${proof.proof}`);
const verificationKey = await this.getVerificationKeyDataForCircuit('PrivateKernelEmptyArtifact');
await this.verifyProof('PrivateKernelEmptyArtifact', proof.binaryProof);
return makePublicInputsAndRecursiveProof(circuitOutput, proof, verificationKey);
}
private async getEmptyPrivateKernelProofFromEmptyNested(
inputs: PrivateKernelEmptyInputs,
): Promise<PublicInputsAndRecursiveProof<KernelCircuitPublicInputs>> {
const { circuitOutput, proof } = await this.createRecursiveProof(
inputs,
'PrivateKernelEmptyArtifact',
NESTED_RECURSIVE_PROOF_LENGTH,
convertPrivateKernelEmptyInputsToWitnessMap,
convertPrivateKernelEmptyOutputsFromWitnessMap,
);
//info(`proof: ${proof.proof}`);
const verificationKey = await this.getVerificationKeyDataForCircuit('PrivateKernelEmptyArtifact');
await this.verifyProof('PrivateKernelEmptyArtifact', proof.binaryProof);
return makePublicInputsAndRecursiveProof(circuitOutput, proof, verificationKey);
}
private async generateProofWithBB<
Input extends { toBuffer: () => Buffer },
Output extends { toBuffer: () => Buffer },
>(
input: Input,
circuitType: ServerProtocolArtifact,
convertInput: (input: Input) => WitnessMap,
convertOutput: (outputWitness: WitnessMap) => Output,
workingDirectory: string,
): Promise<{ circuitOutput: Output; vkData: VerificationKeyData; provingResult: BBSuccess }> {
// Have the ACVM write the partial witness here
const outputWitnessFile = path.join(workingDirectory, 'partial-witness.gz');
// Generate the partial witness using the ACVM
// A further temp directory will be created beneath ours and then cleaned up after the partial witness has been copied to our specified location
const simulator = new NativeACVMSimulator(
this.config.acvmWorkingDirectory,
this.config.acvmBinaryPath,
outputWitnessFile,
);
const artifact = ServerCircuitArtifacts[circuitType];
logger.debug(`Generating witness data for ${circuitType}`);
const inputWitness = convertInput(input);
const timer = new Timer();
const outputWitness = await simulator.simulateCircuit(inputWitness, artifact);
const output = convertOutput(outputWitness);
const circuitName = mapProtocolArtifactNameToCircuitName(circuitType);
this.instrumentation.recordDuration('witGenDuration', circuitName, timer);
this.instrumentation.recordSize('witGenInputSize', circuitName, input.toBuffer().length);
this.instrumentation.recordSize('witGenOutputSize', circuitName, output.toBuffer().length);
logger.info(`Generated witness`, {
circuitName,
duration: timer.ms(),
inputSize: input.toBuffer().length,
outputSize: output.toBuffer().length,
eventName: 'circuit-witness-generation',
} satisfies CircuitWitnessGenerationStats);
// Now prove the circuit from the generated witness
logger.debug(`Proving ${circuitType}...`);
const provingResult = await generateProof(
this.config.bbBinaryPath,
workingDirectory,
circuitType,
Buffer.from(artifact.bytecode, 'base64'),
outputWitnessFile,
getUltraHonkFlavorForCircuit(circuitType),
logger.debug,
);
if (provingResult.status === BB_RESULT.FAILURE) {
logger.error(`Failed to generate proof for ${circuitType}: ${provingResult.reason}`);
throw new Error(provingResult.reason);
}
// Ensure our vk cache is up to date
const vkData = await this.updateVerificationKeyAfterProof(provingResult.vkPath!, circuitType);
return {
circuitOutput: output,
vkData,
provingResult,
};
}
private async createProof<Input extends { toBuffer: () => Buffer }, Output extends { toBuffer: () => Buffer }>(
input: Input,
circuitType: ServerProtocolArtifact,
convertInput: (input: Input) => WitnessMap,
convertOutput: (outputWitness: WitnessMap) => Output,
): Promise<{ circuitOutput: Output; proof: Proof }> {
const operation = async (bbWorkingDirectory: string) => {
const {
provingResult,
vkData,
circuitOutput: output,
} = await this.generateProofWithBB(input, circuitType, convertInput, convertOutput, bbWorkingDirectory);
// Read the binary proof
const rawProof = await fs.readFile(`${provingResult.proofPath!}/${PROOF_FILENAME}`);
const proof = new Proof(rawProof, vkData.numPublicInputs);
const circuitName = mapProtocolArtifactNameToCircuitName(circuitType);
this.instrumentation.recordDuration('provingDuration', circuitName, provingResult.durationMs);
this.instrumentation.recordSize('proofSize', circuitName, proof.buffer.length);
this.instrumentation.recordSize('circuitPublicInputCount', circuitName, vkData.numPublicInputs);
this.instrumentation.recordSize('circuitSize', circuitName, vkData.circuitSize);
logger.info(`Generated proof for ${circuitType} in ${Math.ceil(provingResult.durationMs)} ms`, {
circuitName,
// does not include reading the proof from disk
duration: provingResult.durationMs,
proofSize: proof.buffer.length,
eventName: 'circuit-proving',
// circuitOutput is the partial witness that became the input to the proof
inputSize: output.toBuffer().length,
circuitSize: vkData.circuitSize,
numPublicInputs: vkData.numPublicInputs,
} satisfies CircuitProvingStats);
return { circuitOutput: output, proof };
};
return await this.runInDirectory(operation);
}
private async generateAvmProofWithBB(input: AvmCircuitInputs, workingDirectory: string): Promise<BBSuccess> {
logger.info(`Proving avm-circuit for ${input.functionName}...`);
const provingResult = await generateAvmProof(this.config.bbBinaryPath, workingDirectory, input, logger.verbose);
if (provingResult.status === BB_RESULT.FAILURE) {
logger.error(`Failed to generate AVM proof for ${input.functionName}: ${provingResult.reason}`);
throw new Error(provingResult.reason);
}
return provingResult;
}
private async generateTubeProofWithBB(bbWorkingDirectory: string, input: TubeInputs): Promise<BBSuccess> {
logger.debug(`Proving tube...`);
const hasher = crypto.createHash('sha256');
hasher.update(input.toBuffer());
await input.clientIVCData.writeToOutputDirectory(bbWorkingDirectory);
const provingResult = await generateTubeProof(this.config.bbBinaryPath, bbWorkingDirectory, logger.verbose);
if (provingResult.status === BB_RESULT.FAILURE) {
logger.error(`Failed to generate proof for tube proof: ${provingResult.reason}`);
throw new Error(provingResult.reason);
}
return provingResult;
}
private async createAvmProof(input: AvmCircuitInputs): Promise<AvmProofAndVerificationKey> {
const operation = async (bbWorkingDirectory: string): Promise<AvmProofAndVerificationKey> => {
const provingResult = await this.generateAvmProofWithBB(input, bbWorkingDirectory);
const rawProof = await fs.readFile(provingResult.proofPath!);
// TODO(https://github.com/AztecProtocol/aztec-packages/issues/6773): this VK data format is wrong.
// In particular, the number of public inputs, etc will be wrong.
const verificationKey = await extractAvmVkData(provingResult.vkPath!);
const proof = new Proof(rawProof, verificationKey.numPublicInputs);
const circuitType = 'avm-circuit' as const;
const appCircuitName = 'unknown' as const;
this.instrumentation.recordAvmDuration('provingDuration', appCircuitName, provingResult.durationMs);
this.instrumentation.recordAvmSize('proofSize', appCircuitName, proof.buffer.length);
this.instrumentation.recordAvmSize('circuitPublicInputCount', appCircuitName, verificationKey.numPublicInputs);
this.instrumentation.recordAvmSize('circuitSize', appCircuitName, verificationKey.circuitSize);
logger.info(
`Generated proof for ${circuitType}(${input.functionName}) in ${Math.ceil(provingResult.durationMs)} ms`,
{
circuitName: circuitType,
appCircuitName: input.functionName,
// does not include reading the proof from disk
duration: provingResult.durationMs,
proofSize: proof.buffer.length,
eventName: 'circuit-proving',
inputSize: input.toBuffer().length,
circuitSize: verificationKey.circuitSize, // FIX: wrong in VK
numPublicInputs: verificationKey.numPublicInputs, // FIX: wrong in VK
} satisfies CircuitProvingStats,
);
return { proof, verificationKey };
};
return await this.runInDirectory(operation);
}
public async getTubeProof(
input: TubeInputs,
): Promise<{ tubeVK: VerificationKeyData; tubeProof: RecursiveProof<typeof TUBE_PROOF_LENGTH> }> {
// this probably is gonna need to call client ivc
const operation = async (bbWorkingDirectory: string) => {
logger.debug(`createTubeProof: ${bbWorkingDirectory}`);
const provingResult = await this.generateTubeProofWithBB(bbWorkingDirectory, input);
// Read the proof as fields
const tubeVK = await extractVkData(provingResult.vkPath!);
const tubeProof = await this.readTubeProofAsFields(provingResult.proofPath!, tubeVK, TUBE_PROOF_LENGTH);
this.instrumentation.recordDuration('provingDuration', 'tubeCircuit', provingResult.durationMs);
this.instrumentation.recordSize('proofSize', 'tubeCircuit', tubeProof.binaryProof.buffer.length);
this.instrumentation.recordSize('circuitPublicInputCount', 'tubeCircuit', tubeVK.numPublicInputs);
this.instrumentation.recordSize('circuitSize', 'tubeCircuit', tubeVK.circuitSize);
// Sanity check the tube proof (can be removed later)
await this.verifyWithKey('ultra_honk', tubeVK, tubeProof.binaryProof);
logger.info(
`Generated proof for tubeCircuit in ${Math.ceil(provingResult.durationMs)} ms, size: ${
tubeProof.proof.length
} fields`,
);
return { tubeVK, tubeProof };
};
return await this.runInDirectory(operation);
}
/**
* Executes a circuit and returns its outputs and corresponding proof with embedded aggregation object
* @param witnessMap - The input witness
* @param circuitType - The type of circuit to be executed
* @param proofLength - The length of the proof to be generated. This is a dummy parameter to aid in type checking
* @param convertInput - Function for mapping the input object to a witness map.
* @param convertOutput - Function for parsing the output witness to it's corresponding object
* @returns The circuits output object and it's proof
*/
private async createRecursiveProof<
PROOF_LENGTH extends number,
CircuitInputType extends { toBuffer: () => Buffer },
CircuitOutputType extends { toBuffer: () => Buffer },
>(
input: CircuitInputType,
circuitType: ServerProtocolArtifact,
proofLength: PROOF_LENGTH,
convertInput: (input: CircuitInputType) => WitnessMap,
convertOutput: (outputWitness: WitnessMap) => CircuitOutputType,
): Promise<{ circuitOutput: CircuitOutputType; proof: RecursiveProof<PROOF_LENGTH> }> {
// this probably is gonna need to call client ivc
const operation = async (bbWorkingDirectory: string) => {
const {
provingResult,
vkData,
circuitOutput: output,
} = await this.generateProofWithBB(input, circuitType, convertInput, convertOutput, bbWorkingDirectory);
// Read the proof as fields
const proof = await this.readProofAsFields(provingResult.proofPath!, circuitType, proofLength);
const circuitName = mapProtocolArtifactNameToCircuitName(circuitType);
this.instrumentation.recordDuration('provingDuration', circuitName, provingResult.durationMs);
this.instrumentation.recordSize('proofSize', circuitName, proof.binaryProof.buffer.length);
this.instrumentation.recordSize('circuitPublicInputCount', circuitName, vkData.numPublicInputs);
this.instrumentation.recordSize('circuitSize', circuitName, vkData.circuitSize);
logger.info(
`Generated proof for ${circuitType} in ${Math.ceil(provingResult.durationMs)} ms, size: ${
proof.proof.length
} fields`,
{
circuitName,
circuitSize: vkData.circuitSize,
duration: provingResult.durationMs,
inputSize: output.toBuffer().length,
proofSize: proof.binaryProof.buffer.length,
eventName: 'circuit-proving',
numPublicInputs: vkData.numPublicInputs,
} satisfies CircuitProvingStats,
);
return {
circuitOutput: output,
proof,
};
};
return await this.runInDirectory(operation);
}
/**
* Verifies a proof, will generate the verification key if one is not cached internally
* @param circuitType - The type of circuit whose proof is to be verified
* @param proof - The proof to be verified
*/
public async verifyProof(circuitType: ServerProtocolArtifact, proof: Proof) {
const verificationKey = await this.getVerificationKeyDataForCircuit(circuitType);
return await this.verifyWithKey(getUltraHonkFlavorForCircuit(circuitType), verificationKey, proof);
}
public async verifyAvmProof(proof: Proof, verificationKey: AvmVerificationKeyData) {
return await this.verifyWithKeyInternal(proof, verificationKey, (proofPath, vkPath) =>
verifyAvmProof(this.config.bbBinaryPath, proofPath, vkPath, logger.debug),
);
}
public async verifyWithKey(flavor: UltraHonkFlavor, verificationKey: VerificationKeyData, proof: Proof) {
return await this.verifyWithKeyInternal(proof, verificationKey, (proofPath, vkPath) =>
verifyProof(this.config.bbBinaryPath, proofPath, vkPath, flavor, logger.debug),
);
}
private async verifyWithKeyInternal(
proof: Proof,
verificationKey: { keyAsBytes: Buffer },
verificationFunction: (proofPath: string, vkPath: string) => Promise<BBFailure | BBSuccess>,
) {
const operation = async (bbWorkingDirectory: string) => {
const proofFileName = path.join(bbWorkingDirectory, PROOF_FILENAME);
const verificationKeyPath = path.join(bbWorkingDirectory, VK_FILENAME);
await fs.writeFile(proofFileName, proof.buffer);
await fs.writeFile(verificationKeyPath, verificationKey.keyAsBytes);
const result = await verificationFunction(proofFileName, verificationKeyPath!);
if (result.status === BB_RESULT.FAILURE) {
const errorMessage = `Failed to verify proof from key!`;
throw new Error(errorMessage);
}
logger.info(`Successfully verified proof from key in ${result.durationMs} ms`);
};
await this.runInDirectory(operation);
}
/**
* Returns the verification key for a circuit, will generate it if not cached internally
* @param circuitType - The type of circuit for which the verification key is required
* @returns The verification key
*/
public async getVerificationKeyForCircuit(circuitType: ServerProtocolArtifact): Promise<VerificationKeyAsFields> {
const vkData = await this.getVerificationKeyDataForCircuit(circuitType);
return vkData.clone().keyAsFields;
}
/**
* Will check a recursive proof argument for validity of it's 'fields' format of proof and convert if required
* @param proof - The input proof that may need converting
* @returns - The valid proof
*/
public async ensureValidProof(
proof: RecursiveProof<typeof NESTED_RECURSIVE_PROOF_LENGTH>,
circuit: ServerProtocolArtifact,
vk: VerificationKeyData,
) {
// If the 'fields' proof is already valid then simply return
// This will be false for proofs coming from clients
if (proof.fieldsValid) {
return proof;
}
const operation = async (bbWorkingDirectory: string) => {
const numPublicInputs = vk.numPublicInputs - AGGREGATION_OBJECT_LENGTH;
const proofFullFilename = path.join(bbWorkingDirectory, PROOF_FILENAME);
const vkFullFilename = path.join(bbWorkingDirectory, VK_FILENAME);
logger.debug(
`Converting proof to fields format for circuit ${circuit}, directory ${bbWorkingDirectory}, num public inputs: ${vk.numPublicInputs}, proof length ${proof.binaryProof.buffer.length}, vk length ${vk.keyAsBytes.length}`,
);
await fs.writeFile(proofFullFilename, proof.binaryProof.buffer);
await fs.writeFile(vkFullFilename, vk.keyAsBytes);
const logFunction = (message: string) => {
logger.debug(`${circuit} BB out - ${message}`);
};
const result = await writeProofAsFields(
this.config.bbBinaryPath,
bbWorkingDirectory,
PROOF_FILENAME,
vkFullFilename,
logFunction,
);
if (result.status === BB_RESULT.FAILURE) {
const errorMessage = `Failed to convert ${circuit} proof to fields, ${result.reason}`;
throw new Error(errorMessage);
}
const proofString = await fs.readFile(path.join(bbWorkingDirectory, PROOF_FIELDS_FILENAME), {
encoding: 'utf-8',
});
const json = JSON.parse(proofString);
const fields = json
.slice(0, 3)
.map(Fr.fromString)
.concat(json.slice(3 + numPublicInputs).map(Fr.fromString));
return new RecursiveProof<typeof NESTED_RECURSIVE_PROOF_LENGTH>(
fields,
new Proof(proof.binaryProof.buffer, vk.numPublicInputs),
true,
);
};
return await this.runInDirectory(operation);
}
/**
* Returns the verification key data for a circuit, will generate and cache it if not cached internally
* @param circuitType - The type of circuit for which the verification key is required
* @returns The verification key data
*/
private async getVerificationKeyDataForCircuit(circuitType: ServerProtocolArtifact): Promise<VerificationKeyData> {
const flavor = getUltraHonkFlavorForCircuit(circuitType);
let promise = this.verificationKeys.get(`${flavor}_${circuitType}`);
if (!promise) {
promise = generateKeyForNoirCircuit(
this.config.bbBinaryPath,
this.config.bbWorkingDirectory,
circuitType,
ServerCircuitArtifacts[circuitType],
flavor,
logger.debug,
).then(result => {
if (result.status === BB_RESULT.FAILURE) {
throw new Error(`Failed to generate verification key for ${circuitType}, ${result.reason}`);
}
return extractVkData(result.vkPath!);
});
this.verificationKeys.set(`${flavor}_${circuitType}`, promise);
}
const vk = await promise;
return vk.clone();
}
/**
* Ensures our verification key cache includes the key data located at the specified directory
* @param filePath - The directory containing the verification key data files
* @param circuitType - The type of circuit to which the verification key corresponds
*/
private async updateVerificationKeyAfterProof(
filePath: string,
circuitType: ServerProtocolArtifact,
): Promise<VerificationKeyData> {
const flavor = getUltraHonkFlavorForCircuit(circuitType);
let promise = this.verificationKeys.get(`${flavor}_${circuitType}`);
if (!promise) {
promise = extractVkData(filePath);
this.verificationKeys.set(`${flavor}_${circuitType}`, promise);
}
return promise;
}
/**
* Parses and returns the proof data stored at the specified directory
* @param filePath - The directory containing the proof data
* @param circuitType - The type of circuit proven
* @returns The proof
*/
private async readProofAsFields<PROOF_LENGTH extends number>(
filePath: string,
circuitType: ServerProtocolArtifact,
proofLength: PROOF_LENGTH,
): Promise<RecursiveProof<PROOF_LENGTH>> {
const proofFilename = path.join(filePath, PROOF_FILENAME);
const proofFieldsFilename = path.join(filePath, PROOF_FIELDS_FILENAME);
const [binaryProof, proofString] = await Promise.all([
fs.readFile(proofFilename),
fs.readFile(proofFieldsFilename, { encoding: 'utf-8' }),
]);
const json = JSON.parse(proofString);
const vkData = await this.getVerificationKeyDataForCircuit(circuitType);
const numPublicInputs = vkData.numPublicInputs - AGGREGATION_OBJECT_LENGTH;
const fieldsWithoutPublicInputs = json
.slice(0, 3)
.map(Fr.fromString)
.concat(json.slice(3 + numPublicInputs).map(Fr.fromString));
logger.debug(`num pub inputs ${vkData.numPublicInputs} circuit=${circuitType}`);
const proof = new RecursiveProof<PROOF_LENGTH>(
fieldsWithoutPublicInputs,
new Proof(binaryProof, numPublicInputs),
true,
);
if (proof.proof.length !== proofLength) {
throw new Error(`Proof length doesn't match expected length (${proof.proof.length} != ${proofLength})`);
}
return proof;
}
/**
* Parses and returns a tube proof stored in the specified directory. TODO merge wih above
* @param filePath - The directory containing the proof data
* @param circuitType - The type of circuit proven
* @returns The proof
* TODO(#7369) This is entirely redundant now with the above method, deduplicate
*/
private async readTubeProofAsFields<PROOF_LENGTH extends number>(
filePath: string,
vkData: VerificationKeyData,
proofLength: PROOF_LENGTH,
): Promise<RecursiveProof<PROOF_LENGTH>> {
const proofFilename = path.join(filePath, PROOF_FILENAME);
const proofFieldsFilename = path.join(filePath, PROOF_FIELDS_FILENAME);
const [binaryProof, proofString] = await Promise.all([
fs.readFile(proofFilename),
fs.readFile(proofFieldsFilename, { encoding: 'utf-8' }),
]);
const json = JSON.parse(proofString);
const numPublicInputs = vkData.numPublicInputs - AGGREGATION_OBJECT_LENGTH;
if (numPublicInputs === 0) {
throw new Error(`Tube proof should have public inputs (e.g. the number of public inputs from PrivateKernelTail)`);
}
const proofFields = json
.slice(0, 3)
.map(Fr.fromString)
.concat(json.slice(3 + numPublicInputs).map(Fr.fromString));
logger.debug(
`Circuit type: tube circuit, complete proof length: ${json.length}, num public inputs: ${numPublicInputs}, circuit size: ${vkData.circuitSize}, is recursive: ${vkData.isRecursive}, raw length: ${binaryProof.length}`,
);
const proof = new RecursiveProof<PROOF_LENGTH>(proofFields, new Proof(binaryProof, numPublicInputs), true);
if (proof.proof.length !== proofLength) {
throw new Error(`Proof length doesn't match expected length (${proof.proof.length} != ${proofLength})`);
}
return proof;
}