3
3
#[ cfg( feature = "bonsai-auto-scaling" ) ]
4
4
use crate :: bonsai:: auto_scaling:: shutdown_bonsai;
5
5
use crate :: {
6
- methods:: risc0_aggregation:: RISC0_AGGREGATION_ELF ,
7
- methods:: risc0_guest:: { RISC0_GUEST_ELF , RISC0_GUEST_ID } ,
6
+ methods:: risc0_aggregation:: RISC0_AGGREGATION_ELF , methods :: risc0_batch :: RISC0_BATCH_ELF ,
7
+ methods:: risc0_guest:: RISC0_GUEST_ELF ,
8
8
} ;
9
9
use alloy_primitives:: { hex:: ToHexExt , B256 } ;
10
10
use bonsai:: { cancel_proof, maybe_prove} ;
@@ -18,8 +18,10 @@ use raiko_lib::{
18
18
prover:: { IdStore , IdWrite , Proof , ProofKey , Prover , ProverConfig , ProverError , ProverResult } ,
19
19
} ;
20
20
use risc0_zkvm:: {
21
- compute_image_id, default_prover, serde:: to_vec, sha:: Digestible , ExecutorEnv , ProverOpts ,
22
- Receipt ,
21
+ compute_image_id, default_prover,
22
+ serde:: to_vec,
23
+ sha:: { Digest , Digestible } ,
24
+ ExecutorEnv , ProverOpts , Receipt ,
23
25
} ;
24
26
use serde:: { Deserialize , Serialize } ;
25
27
use serde_with:: serde_as;
@@ -93,7 +95,7 @@ impl Prover for Risc0Prover {
93
95
. await ?;
94
96
95
97
let proof_gen_result = if config. snark && config. bonsai {
96
- bonsai:: bonsai_stark_to_snark ( uuid, receipt, output. hash )
98
+ bonsai:: bonsai_stark_to_snark ( uuid, receipt, output. hash , RISC0_GUEST_ELF )
97
99
. await
98
100
. map ( |r0_response| r0_response. into ( ) )
99
101
. map_err ( |e| ProverError :: GuestError ( e. to_string ( ) ) )
@@ -149,11 +151,16 @@ impl Prover for Risc0Prover {
149
151
. iter ( )
150
152
. map ( |proof| proof. input . unwrap ( ) )
151
153
. collect :: < Vec < _ > > ( ) ;
154
+
155
+ let input_proof_hex_str = input. proofs [ 0 ] . proof . as_ref ( ) . unwrap ( ) ;
156
+ let input_proof_bytes = hex:: decode ( & input_proof_hex_str[ 2 ..] ) . unwrap ( ) ;
157
+ let input_image_id_bytes: [ u8 ; 32 ] = input_proof_bytes[ 32 ..64 ] . try_into ( ) . unwrap ( ) ;
158
+ let input_proof_image_id = Digest :: from ( input_image_id_bytes) ;
152
159
let input = ZkAggregationGuestInput {
153
- image_id : RISC0_GUEST_ID ,
160
+ image_id : input_proof_image_id . as_words ( ) . try_into ( ) . unwrap ( ) ,
154
161
block_inputs,
155
162
} ;
156
- info ! ( "Start aggregate proofs" ) ;
163
+
157
164
// add_assumption makes the receipt to be verified available to the prover.
158
165
let env = {
159
166
let mut env = ExecutorEnv :: builder ( ) ;
@@ -173,10 +180,9 @@ impl Prover for Risc0Prover {
173
180
"Generate aggregation receipt journal: {:?}" ,
174
181
alloy_primitives:: hex:: encode_prefixed( receipt. journal. bytes. clone( ) )
175
182
) ;
176
- let block_proof_image_id = compute_image_id ( RISC0_GUEST_ELF ) . unwrap ( ) ;
177
183
let aggregation_image_id = compute_image_id ( RISC0_AGGREGATION_ELF ) . unwrap ( ) ;
178
184
let proof_data = snarks:: verify_aggregation_groth16_proof (
179
- block_proof_image_id ,
185
+ input_proof_image_id ,
180
186
aggregation_image_id,
181
187
receipt. clone ( ) ,
182
188
)
@@ -223,12 +229,60 @@ impl Prover for Risc0Prover {
223
229
224
230
async fn batch_run (
225
231
& self ,
226
- _input : GuestBatchInput ,
227
- _output : & GuestBatchOutput ,
228
- _config : & ProverConfig ,
229
- _store : Option < & mut dyn IdWrite > ,
232
+ input : GuestBatchInput ,
233
+ output : & GuestBatchOutput ,
234
+ config : & ProverConfig ,
235
+ id_store : Option < & mut dyn IdWrite > ,
230
236
) -> ProverResult < Proof > {
231
- unimplemented ! ( ) ;
237
+ let mut id_store = id_store;
238
+ let config = Risc0Param :: deserialize ( config. get ( "risc0" ) . unwrap ( ) ) . unwrap ( ) ;
239
+ let proof_key = (
240
+ input. taiko . chain_spec . chain_id ,
241
+ input. taiko . batch_id ,
242
+ output. hash ,
243
+ ProofType :: Risc0 as u8 ,
244
+ ) ;
245
+
246
+ let encoded_input = to_vec ( & input) . expect ( "Could not serialize proving input!" ) ;
247
+
248
+ let ( uuid, receipt) = maybe_prove :: < GuestBatchInput , B256 > (
249
+ & config,
250
+ encoded_input,
251
+ RISC0_BATCH_ELF ,
252
+ & output. hash ,
253
+ ( Vec :: < Receipt > :: new ( ) , Vec :: new ( ) ) ,
254
+ proof_key,
255
+ & mut id_store,
256
+ )
257
+ . await ?;
258
+
259
+ let proof_gen_result = if config. snark && config. bonsai {
260
+ bonsai:: bonsai_stark_to_snark ( uuid, receipt, output. hash , RISC0_BATCH_ELF )
261
+ . await
262
+ . map ( |r0_response| r0_response. into ( ) )
263
+ . map_err ( |e| ProverError :: GuestError ( e. to_string ( ) ) )
264
+ } else {
265
+ if !config. snark {
266
+ warn ! ( "proof is not in snark mode, please check." ) ;
267
+ }
268
+ Ok ( Risc0Response {
269
+ proof : receipt. journal . encode_hex_with_prefix ( ) ,
270
+ receipt : serde_json:: to_string ( & receipt) . unwrap ( ) ,
271
+ uuid,
272
+ input : output. hash ,
273
+ }
274
+ . into ( ) )
275
+ } ;
276
+
277
+ #[ cfg( feature = "bonsai-auto-scaling" ) ]
278
+ if config. bonsai {
279
+ // shutdown bonsai
280
+ shutdown_bonsai ( )
281
+ . await
282
+ . map_err ( |e| ProverError :: GuestError ( e. to_string ( ) ) ) ?;
283
+ }
284
+
285
+ proof_gen_result
232
286
}
233
287
}
234
288
0 commit comments