@@ -3,13 +3,15 @@ use alloy_primitives::{Address, B256};
3
3
use clap:: { Args , ValueEnum } ;
4
4
use raiko_lib:: {
5
5
consts:: VerifierType ,
6
- input:: { BlobProofType , GuestInput , GuestOutput } ,
6
+ input:: {
7
+ AggregationGuestInput , AggregationGuestOutput , BlobProofType , GuestInput , GuestOutput ,
8
+ } ,
7
9
prover:: { IdStore , IdWrite , Proof , ProofKey , Prover , ProverError } ,
8
10
} ;
9
11
use serde:: { Deserialize , Serialize } ;
10
12
use serde_json:: Value ;
11
13
use serde_with:: { serde_as, DisplayFromStr } ;
12
- use std:: { collections:: HashMap , path:: Path , str:: FromStr } ;
14
+ use std:: { collections:: HashMap , fmt :: Display , path:: Path , str:: FromStr } ;
13
15
use utoipa:: ToSchema ;
14
16
15
17
#[ derive( Debug , thiserror:: Error , ToSchema ) ]
@@ -203,6 +205,47 @@ impl ProofType {
203
205
}
204
206
}
205
207
208
+ /// Run the prover driver depending on the proof type.
209
+ pub async fn aggregate_proofs (
210
+ & self ,
211
+ input : AggregationGuestInput ,
212
+ output : & AggregationGuestOutput ,
213
+ config : & Value ,
214
+ store : Option < & mut dyn IdWrite > ,
215
+ ) -> RaikoResult < Proof > {
216
+ let proof = match self {
217
+ ProofType :: Native => NativeProver :: aggregate ( input. clone ( ) , output, config, store)
218
+ . await
219
+ . map_err ( <ProverError as Into < RaikoError > >:: into) ,
220
+ ProofType :: Sp1 => {
221
+ #[ cfg( feature = "sp1" ) ]
222
+ return sp1_driver:: Sp1Prover :: aggregate ( input. clone ( ) , output, config, store)
223
+ . await
224
+ . map_err ( |e| e. into ( ) ) ;
225
+ #[ cfg( not( feature = "sp1" ) ) ]
226
+ Err ( RaikoError :: FeatureNotSupportedError ( * self ) )
227
+ }
228
+ ProofType :: Risc0 => {
229
+ #[ cfg( feature = "risc0" ) ]
230
+ return risc0_driver:: Risc0Prover :: aggregate ( input. clone ( ) , output, config, store)
231
+ . await
232
+ . map_err ( |e| e. into ( ) ) ;
233
+ #[ cfg( not( feature = "risc0" ) ) ]
234
+ Err ( RaikoError :: FeatureNotSupportedError ( * self ) )
235
+ }
236
+ ProofType :: Sgx => {
237
+ #[ cfg( feature = "sgx" ) ]
238
+ return sgx_prover:: SgxProver :: aggregate ( input. clone ( ) , output, config, store)
239
+ . await
240
+ . map_err ( |e| e. into ( ) ) ;
241
+ #[ cfg( not( feature = "sgx" ) ) ]
242
+ Err ( RaikoError :: FeatureNotSupportedError ( * self ) )
243
+ }
244
+ } ?;
245
+
246
+ Ok ( proof)
247
+ }
248
+
206
249
pub async fn cancel_proof (
207
250
& self ,
208
251
proof_key : ProofKey ,
@@ -302,7 +345,7 @@ pub struct ProofRequestOpt {
302
345
pub prover_args : ProverSpecificOpts ,
303
346
}
304
347
305
- #[ derive( Default , Clone , Serialize , Deserialize , Debug , ToSchema , Args ) ]
348
+ #[ derive( Default , Clone , Serialize , Deserialize , Debug , ToSchema , Args , PartialEq , Eq , Hash ) ]
306
349
pub struct ProverSpecificOpts {
307
350
/// Native prover specific options.
308
351
pub native : Option < Value > ,
@@ -398,3 +441,123 @@ impl TryFrom<ProofRequestOpt> for ProofRequest {
398
441
} )
399
442
}
400
443
}
444
+
445
+ #[ derive( Default , Clone , Serialize , Deserialize , Debug , ToSchema ) ]
446
+ #[ serde( default ) ]
447
+ /// A request for proof aggregation of multiple proofs.
448
+ pub struct AggregationRequest {
449
+ /// The block numbers and l1 inclusion block numbers for the blocks to aggregate proofs for.
450
+ pub block_numbers : Vec < ( u64 , Option < u64 > ) > ,
451
+ /// The network to generate the proof for.
452
+ pub network : Option < String > ,
453
+ /// The L1 network to generate the proof for.
454
+ pub l1_network : Option < String > ,
455
+ // Graffiti.
456
+ pub graffiti : Option < String > ,
457
+ /// The protocol instance data.
458
+ pub prover : Option < String > ,
459
+ /// The proof type.
460
+ pub proof_type : Option < String > ,
461
+ /// Blob proof type.
462
+ pub blob_proof_type : Option < String > ,
463
+ #[ serde( flatten) ]
464
+ /// Any additional prover params in JSON format.
465
+ pub prover_args : ProverSpecificOpts ,
466
+ }
467
+
468
+ impl AggregationRequest {
469
+ /// Merge proof request options into aggregation request options.
470
+ pub fn merge ( & mut self , opts : & ProofRequestOpt ) -> RaikoResult < ( ) > {
471
+ let this = serde_json:: to_value ( & self ) ?;
472
+ let mut opts = serde_json:: to_value ( opts) ?;
473
+ merge ( & mut opts, & this) ;
474
+ * self = serde_json:: from_value ( opts) ?;
475
+ Ok ( ( ) )
476
+ }
477
+ }
478
+
479
+ impl From < AggregationRequest > for Vec < ProofRequestOpt > {
480
+ fn from ( value : AggregationRequest ) -> Self {
481
+ value
482
+ . block_numbers
483
+ . iter ( )
484
+ . map (
485
+ |& ( block_number, l1_inclusion_block_number) | ProofRequestOpt {
486
+ block_number : Some ( block_number) ,
487
+ l1_inclusion_block_number,
488
+ network : value. network . clone ( ) ,
489
+ l1_network : value. l1_network . clone ( ) ,
490
+ graffiti : value. graffiti . clone ( ) ,
491
+ prover : value. prover . clone ( ) ,
492
+ proof_type : value. proof_type . clone ( ) ,
493
+ blob_proof_type : value. blob_proof_type . clone ( ) ,
494
+ prover_args : value. prover_args . clone ( ) ,
495
+ } ,
496
+ )
497
+ . collect ( )
498
+ }
499
+ }
500
+
501
+ impl From < ProofRequestOpt > for AggregationRequest {
502
+ fn from ( value : ProofRequestOpt ) -> Self {
503
+ let block_numbers = if let Some ( block_number) = value. block_number {
504
+ vec ! [ ( block_number, value. l1_inclusion_block_number) ]
505
+ } else {
506
+ vec ! [ ]
507
+ } ;
508
+
509
+ Self {
510
+ block_numbers,
511
+ network : value. network ,
512
+ l1_network : value. l1_network ,
513
+ graffiti : value. graffiti ,
514
+ prover : value. prover ,
515
+ proof_type : value. proof_type ,
516
+ blob_proof_type : value. blob_proof_type ,
517
+ prover_args : value. prover_args ,
518
+ }
519
+ }
520
+ }
521
+
522
+ #[ derive( Default , Clone , Serialize , Deserialize , Debug , ToSchema , PartialEq , Eq , Hash ) ]
523
+ #[ serde( default ) ]
524
+ /// A request for proof aggregation of multiple proofs.
525
+ pub struct AggregationOnlyRequest {
526
+ /// The block numbers and l1 inclusion block numbers for the blocks to aggregate proofs for.
527
+ pub proofs : Vec < Proof > ,
528
+ /// The proof type.
529
+ pub proof_type : Option < String > ,
530
+ #[ serde( flatten) ]
531
+ /// Any additional prover params in JSON format.
532
+ pub prover_args : ProverSpecificOpts ,
533
+ }
534
+
535
+ impl Display for AggregationOnlyRequest {
536
+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
537
+ f. write_str ( & format ! (
538
+ "AggregationOnlyRequest {{ {:?}, {:?} }}" ,
539
+ self . proof_type, self . prover_args
540
+ ) )
541
+ }
542
+ }
543
+
544
+ impl From < ( AggregationRequest , Vec < Proof > ) > for AggregationOnlyRequest {
545
+ fn from ( ( request, proofs) : ( AggregationRequest , Vec < Proof > ) ) -> Self {
546
+ Self {
547
+ proofs,
548
+ proof_type : request. proof_type ,
549
+ prover_args : request. prover_args ,
550
+ }
551
+ }
552
+ }
553
+
554
+ impl AggregationOnlyRequest {
555
+ /// Merge proof request options into aggregation request options.
556
+ pub fn merge ( & mut self , opts : & ProofRequestOpt ) -> RaikoResult < ( ) > {
557
+ let this = serde_json:: to_value ( & self ) ?;
558
+ let mut opts = serde_json:: to_value ( opts) ?;
559
+ merge ( & mut opts, & this) ;
560
+ * self = serde_json:: from_value ( opts) ?;
561
+ Ok ( ( ) )
562
+ }
563
+ }
0 commit comments