11include ! ( concat!( env!( "OUT_DIR" ) , "/methods.rs" ) ) ;
22
33use risc0_zkvm:: { default_prover, ExecutorEnv , ProverOpts , Receipt } ;
4+ use sha3:: { Digest , Keccak256 } ;
45
56use super :: lib:: { AggregatedProof , ProgramOutput , ProofAggregationError } ;
67
7- pub struct Risc0ProofWithPubValuesAndImageId {
8- pub image_id : [ u32 ; 8 ] ,
8+ const RISC0_AGGREGATOR_PROGRAM_ID_BYTES : [ u8 ; 32 ] = {
9+ let mut res = [ 0u8 ; 32 ] ;
10+ let mut i = 0 ;
11+ while i < 8 {
12+ let bytes = RISC0_AGGREGATOR_PROGRAM_ID [ i] . to_be_bytes ( ) ;
13+ res[ i * 4 ] = bytes[ 0 ] ;
14+ res[ i * 4 + 1 ] = bytes[ 1 ] ;
15+ res[ i * 4 + 2 ] = bytes[ 2 ] ;
16+ res[ i * 4 + 3 ] = bytes[ 3 ] ;
17+ i += 1 ;
18+ }
19+ res
20+ } ;
21+
22+ pub struct Risc0ProofReceiptAndImageId {
23+ pub image_id : [ u8 ; 32 ] ,
924 pub receipt : Receipt ,
10- pub public_values : Vec < u8 > ,
1125}
1226
13- impl Risc0ProofWithPubValuesAndImageId {
27+ impl Risc0ProofReceiptAndImageId {
28+ pub fn public_inputs ( & self ) -> & Vec < u8 > {
29+ & self . receipt . journal . bytes
30+ }
31+ }
32+
33+ impl Risc0ProofReceiptAndImageId {
1434 pub fn hash_image_id_and_public_inputs ( & self ) -> [ u8 ; 32 ] {
15- [ 0u8 ; 32 ]
35+ let mut hasher = Keccak256 :: new ( ) ;
36+ hasher. update ( & self . image_id ) ;
37+ hasher. update ( self . public_inputs ( ) ) ;
38+ hasher. finalize ( ) . into ( )
1639 }
1740}
1841
1942pub struct Risc0AggregationInput {
20- pub receipts : Vec < Risc0ProofWithPubValuesAndImageId > ,
43+ pub receipts : Vec < Risc0ProofReceiptAndImageId > ,
2144 pub merkle_root : [ u8 ; 32 ] ,
2245}
2346
@@ -28,39 +51,57 @@ pub(crate) fn aggregate_proofs(
2851
2952 // write assumptions and proof image id + pub inputs
3053 let mut proofs_image_id_and_pub_inputs = vec ! [ ] ;
31- for r in input. receipts {
54+ for proof in input. receipts {
3255 proofs_image_id_and_pub_inputs. push ( risc0_aggregation_program:: Risc0ImageIdAndPubInputs {
33- image_id : r . image_id ,
34- public_inputs : r . public_values ,
56+ image_id : proof . image_id ,
57+ public_inputs : proof . receipt . journal . bytes . clone ( ) ,
3558 } ) ;
36- env_builder. add_assumption ( r . receipt ) ;
59+ env_builder. add_assumption ( proof . receipt ) ;
3760 }
3861
3962 // write input data
4063 let input = risc0_aggregation_program:: Input {
4164 merkle_root : input. merkle_root ,
4265 proofs_image_id_and_pub_inputs,
4366 } ;
44- env_builder. write ( & input) . unwrap ( ) ;
67+ env_builder
68+ . write ( & input)
69+ . map_err ( |_| ProofAggregationError :: Risc0Proving ) ?;
4570
46- let env = env_builder. build ( ) . unwrap ( ) ;
71+ let env = env_builder
72+ . build ( )
73+ . map_err ( |_| ProofAggregationError :: Risc0Proving ) ?;
4774
4875 let prover = default_prover ( ) ;
4976 let receipt = prover
5077 . prove_with_opts ( env, RISC0_AGGREGATOR_PROGRAM_ELF , & ProverOpts :: groth16 ( ) )
51- . unwrap ( )
78+ . map_err ( |_| ProofAggregationError :: Risc0Proving ) ?
5279 . receipt ;
5380
54- Ok ( ProgramOutput :: new ( AggregatedProof :: Risc0 ( receipt) ) )
81+ let output = Risc0ProofReceiptAndImageId {
82+ image_id : RISC0_AGGREGATOR_PROGRAM_ID_BYTES ,
83+ receipt,
84+ } ;
85+
86+ Ok ( ProgramOutput :: new ( AggregatedProof :: Risc0 ( output) ) )
5587}
5688
5789#[ derive( Debug ) ]
5890pub enum AlignedRisc0VerificationError {
59- Verification ,
91+ Verification ( String ) ,
6092 UnsupportedProof ,
6193}
6294
63- pub ( crate ) fn verify ( receipt : & Receipt ) -> Result < ( ) , AlignedRisc0VerificationError > {
64- // TODO validate and verify receipt is of type Compressed, as only they can be aggregated recursively
65- Ok ( ( ) )
95+ pub ( crate ) fn verify (
96+ proof : & Risc0ProofReceiptAndImageId ,
97+ ) -> Result < ( ) , AlignedRisc0VerificationError > {
98+ // only composite proofs are supported for recursion
99+ if proof. receipt . inner . composite ( ) . is_err ( ) {
100+ Err ( AlignedRisc0VerificationError :: UnsupportedProof )
101+ } else {
102+ proof
103+ . receipt
104+ . verify ( proof. image_id )
105+ . map_err ( |e| AlignedRisc0VerificationError :: Verification ( e. to_string ( ) ) )
106+ }
66107}
0 commit comments