@@ -178,7 +178,7 @@ fn stwo_run_and_prove(
178178 program_output : Option < PathBuf > ,
179179 prove_config : ProveConfig ,
180180 prover : Box < dyn ProverTrait > ,
181- ) -> Result < ( ) , StwoRunAndProveError > {
181+ ) -> Result < usize , StwoRunAndProveError > {
182182 let cairo_run_config = get_cairo_run_config (
183183 // we don't use dynamic layout in stwo
184184 & None ,
@@ -201,13 +201,14 @@ fn stwo_run_and_prove(
201201 let mut prover_input_info = runner. get_prover_input_info ( ) ?;
202202 info ! ( "Adapting prover input." ) ;
203203 let prover_input = adapter ( & mut prover_input_info) ?;
204- let output_vec = prove_with_retries ( prover_input, prove_config, prover) ?;
204+ let ( successful_proof_attempt, output_vec) =
205+ prove_with_retries ( prover_input, prove_config, prover) ?;
205206
206207 if let Some ( output_path) = program_output {
207208 save_output_to_file ( output_vec, output_path) ?;
208209 }
209210
210- Ok ( ( ) )
211+ Ok ( successful_proof_attempt )
211212}
212213
213214/// Prepares the prover parameters and generates proof given the prover input and parameters.
@@ -218,7 +219,7 @@ fn prove_with_retries(
218219 prover_input : ProverInput ,
219220 prove_config : ProveConfig ,
220221 prover : Box < dyn ProverTrait > ,
221- ) -> Result < OutputVec , StwoRunAndProveError > {
222+ ) -> Result < ( usize , OutputVec ) , StwoRunAndProveError > {
222223 let ProverParameters {
223224 channel_hash,
224225 pcs_config,
@@ -238,11 +239,10 @@ fn prove_with_retries(
238239 std:: fs:: create_dir_all ( & prove_config. proofs_dir ) ?;
239240 let proof_format = prove_config. proof_format ;
240241
241- for i in 0 ..prove_config. n_proof_attempts {
242+ for i in 1 ..prove_config. n_proof_attempts + 1 {
242243 info ! (
243244 "Attempting to generate proof {}/{}." ,
244- i + 1 ,
245- prove_config. n_proof_attempts
245+ i, prove_config. n_proof_attempts
246246 ) ;
247247 let proof_file_path = prove_config. proofs_dir . join ( format ! ( "proof_{}" , i) ) ;
248248
@@ -256,25 +256,22 @@ fn prove_with_retries(
256256 Ok ( output_values) => {
257257 info ! (
258258 "Proof generated and verified successfully on attempt {}/{}" ,
259- i + 1 ,
260- prove_config. n_proof_attempts
259+ i, prove_config. n_proof_attempts
261260 ) ;
262- return Ok ( output_values) ;
261+ return Ok ( ( i , output_values) ) ;
263262 }
264263
265264 Err ( StwoRunAndProveError :: Verification ) => {
266- if i < prove_config. n_proof_attempts - 1 {
265+ if i < prove_config. n_proof_attempts {
267266 warn ! (
268267 "Proof verification failed on attempt {}/{}. Retrying." ,
269- i + 1 ,
270- prove_config. n_proof_attempts
268+ i, prove_config. n_proof_attempts
271269 ) ;
272270 continue ;
273271 }
274272 error ! (
275273 "Proof verification failed on last attempt - {}/{}." ,
276- i + 1 ,
277- prove_config. n_proof_attempts
274+ i, prove_config. n_proof_attempts
278275 ) ;
279276 return Err ( StwoRunAndProveError :: Verification ) ;
280277 }
@@ -425,7 +422,7 @@ mod tests {
425422 const PROGRAM_FILE_NAME : & str = "array_sum.json" ;
426423 const PROVER_PARAMS_FILE_NAME : & str = "prover_params.json" ;
427424 const EXPECTED_PROOF_FILE_NAME : & str = "array_sum_proof" ;
428- const FIRST_PROOF_FILE_NAME : & str = "proof_0 " ;
425+ const FIRST_PROOF_FILE_NAME : & str = "proof_1 " ;
429426
430427 fn get_path ( file_name : & str ) -> PathBuf {
431428 let current_path = env:: current_dir ( ) . expect ( "failed to get current directory" ) ;
@@ -459,7 +456,7 @@ mod tests {
459456 fn run_stwo_run_and_prove (
460457 args : Args ,
461458 prover : Box < dyn ProverTrait > ,
462- ) -> Result < ( ) , StwoRunAndProveError > {
459+ ) -> Result < usize , StwoRunAndProveError > {
463460 let prove_config = ProveConfig {
464461 verify : args. verify ,
465462 proofs_dir : args. proofs_dir ,
@@ -483,14 +480,21 @@ mod tests {
483480 let mut mock_prover = Box :: new ( MockProverTrait :: new ( ) ) ;
484481 mock_prover
485482 . expect_choose_channel_and_prove ( )
486- . times ( n_proof_attempts )
483+ . times ( 1 )
487484 . returning ( move |_, proof_file, _, _, _| {
488485 let expected_proof_file = get_path ( EXPECTED_PROOF_FILE_NAME ) ;
489486 fs:: copy ( & expected_proof_file, & proof_file) . expect ( "Failed to copy proof file." ) ;
490487 Ok ( vec ! [ ARRAY_SUM_EXPECTED_OUTPUT ] )
491488 } ) ;
492489
493- run_stwo_run_and_prove ( args, mock_prover) . expect ( "failed to run stwo_run_and_prove" ) ;
490+ let successful_proof_attempt =
491+ run_stwo_run_and_prove ( args, mock_prover) . expect ( "failed to run stwo_run_and_prove" ) ;
492+
493+ assert_eq ! (
494+ successful_proof_attempt, 1 ,
495+ "successful proof attempt should be 1, but got {:?}" ,
496+ successful_proof_attempt
497+ ) ;
494498
495499 ( program_output_tempfile, proofs_tempdir)
496500 }
@@ -550,15 +554,17 @@ mod tests {
550554
551555 #[ test]
552556 fn test_stwo_run_and_prove_retries ( ) {
553- let ( output_temp_file, proofs_temp_dir) = run_with_verification_error_mock_prover ( 3 ) ;
557+ let n_proof_attempts = 3 ;
558+ let ( output_temp_file, proofs_temp_dir) =
559+ run_with_verification_error_mock_prover ( n_proof_attempts) ;
554560 let proofs_dir = proofs_temp_dir. path ( ) . to_path_buf ( ) ;
555561
556- for i in 0 .. 3 {
562+ for i in 1 ..n_proof_attempts + 1 {
557563 let proof_file = proofs_dir. join ( format ! ( "proof_{}" , i) ) ;
558564 assert ! (
559565 proof_file. exists( ) ,
560566 "Proof file {:?} should exist after running with verifier failures" ,
561- i + 1 ,
567+ i,
562568 ) ;
563569 }
564570 assert ! (
0 commit comments