Skip to content

Commit 4236ffe

Browse files
add success/failure extension to proof file (#225)
## Type - [ ] feature - [ ] bugfix - [ ] dev (no functional changes, no API changes) - [ ] fmt (formatting, renaming) - [ ] build - [ ] docs - [ ] testing ## Description ## Breaking changes? - [ ] yes - [ ] no <!-- Reviewable:start --> - - - This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/starkware-libs/bootloader-hints/225) <!-- Reviewable:end -->
1 parent e725600 commit 4236ffe

File tree

2 files changed

+38
-33
lines changed

2 files changed

+38
-33
lines changed

.github/workflows/upload_artifacts_workflow.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,4 @@ jobs:
6363
uses: "google-github-actions/upload-cloud-storage@v2"
6464
with:
6565
path: "target/release/stwo_run_and_prove"
66-
destination: "stwo_run_and_prove_artifacts/${{ env.SHORT_HASH }}/stwo_run_and_prove_release"
66+
destination: "stwo_run_and_prove_artifacts/${{ env.SHORT_HASH }}/release"

crates/stwo_run_and_prove/src/main.rs

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use clap::Parser;
1414
use mockall::automock;
1515
use serde::Serialize;
1616
use std::env;
17+
use std::fs;
1718
use std::io::Write;
1819
use std::path::PathBuf;
1920
use stwo_cairo_adapter::ProverInput;
@@ -35,6 +36,10 @@ use stwo_cairo_utils::file_utils::{IoErrorWithPath, create_file, read_to_string}
3536
use thiserror::Error;
3637
use tracing::{error, info, warn};
3738

39+
static PROOF_PREFIX: &str = "proof_";
40+
static SUCCESS_SUFFIX: &str = "_success";
41+
static FAILURE_SUFFIX: &str = "_failure";
42+
3843
fn parse_usize_ge1(s: &str) -> Result<usize, String> {
3944
let v: usize = s.parse().map_err(|_| "must be a number".to_string())?;
4045
if v >= 1 {
@@ -202,7 +207,7 @@ fn stwo_run_and_prove(
202207
program_output: Option<PathBuf>,
203208
prove_config: ProveConfig,
204209
prover: Box<dyn ProverTrait>,
205-
) -> Result<usize, StwoRunAndProveError> {
210+
) -> Result<(), StwoRunAndProveError> {
206211
let cairo_run_config = get_cairo_run_config(
207212
// we don't use dynamic layout in stwo
208213
&None,
@@ -226,12 +231,12 @@ fn stwo_run_and_prove(
226231
let runner = cairo_run_program(&program, program_input, cairo_run_config)?;
227232
info!("Adapting prover input.");
228233
let prover_input = adapter(&runner);
229-
let successful_proof_attempt = prove_with_retries(prover_input, prove_config, prover)?;
234+
prove_with_retries(prover_input, prove_config, prover)?;
230235
if let Some(output_path) = program_output {
231236
write_output_to_file(runner, output_path)?;
232237
}
233238

234-
Ok(successful_proof_attempt)
239+
Ok(())
235240
}
236241

237242
/// Prepares the prover parameters and generates proof given the prover input and parameters.
@@ -242,7 +247,7 @@ fn prove_with_retries(
242247
prover_input: ProverInput,
243248
prove_config: ProveConfig,
244249
prover: Box<dyn ProverTrait>,
245-
) -> Result<usize, StwoRunAndProveError> {
250+
) -> Result<(), StwoRunAndProveError> {
246251
let prover_params = match prove_config.prover_params_json {
247252
Some(ref path) => sonic_rs::from_str(
248253
&read_to_string(path).map_err(|e| StwoRunAndProveError::from((e, path.clone())))?,
@@ -260,12 +265,15 @@ fn prove_with_retries(
260265
"Attempting to generate proof {}/{}.",
261266
i, prove_config.n_proof_attempts
262267
);
263-
let proof_file_path = prove_config.proofs_dir.join(format!("proof_{i}"));
268+
let proof_file_path = prove_config.proofs_dir.join(format!("{PROOF_PREFIX}{i}"));
269+
let proof_file_name = proof_file_path
270+
.file_name()
271+
.ok_or_else(|| std::io::Error::new(std::io::ErrorKind::InvalidInput, "no file name"))?;
264272

265273
match prover.choose_channel_and_prove(
266274
prover_params,
267275
prover_input.clone(),
268-
proof_file_path,
276+
proof_file_path.clone(),
269277
&proof_format,
270278
prove_config.verify,
271279
) {
@@ -274,10 +282,22 @@ fn prove_with_retries(
274282
"Proof generated and verified successfully on attempt {}/{}",
275283
i, prove_config.n_proof_attempts
276284
);
277-
return Ok(i);
285+
let success_path = proof_file_path.with_file_name(format!(
286+
"{}{}",
287+
proof_file_name.to_string_lossy(),
288+
SUCCESS_SUFFIX
289+
));
290+
fs::rename(proof_file_path, &success_path)?;
291+
return Ok(());
278292
}
279293

280294
Err(StwoRunAndProveError::Verification) => {
295+
let failure_path = proof_file_path.with_file_name(format!(
296+
"{}{}",
297+
proof_file_name.to_string_lossy(),
298+
FAILURE_SUFFIX
299+
));
300+
fs::rename(proof_file_path, &failure_path)?;
281301
if i < prove_config.n_proof_attempts {
282302
warn!(
283303
"Proof verification failed on attempt {}/{}. Retrying.",
@@ -417,7 +437,7 @@ fn write_output_to_file(
417437
output_path: PathBuf,
418438
) -> Result<(), StwoRunAndProveError> {
419439
info!("Saving program output to: {:?}", output_path);
420-
// TODO(Nitsan): move this function to cairo_program_runner_lib or a new utils lib,
440+
// TODO(Nitsan): move this function to cairo_program_runner_lib or a new utils lib,
421441
// and call it from here and from cairo_program_runner.
422442

423443
let mut output_buffer = String::new();
@@ -445,7 +465,6 @@ mod tests {
445465
const PROGRAM_FILE_NAME: &str = "array_sum.json";
446466
const PROVER_PARAMS_FILE_NAME: &str = "prover_params.json";
447467
const EXPECTED_PROOF_FILE_NAME: &str = "array_sum_proof";
448-
const FIRST_PROOF_FILE_NAME: &str = "proof_1";
449468

450469
fn get_path(file_name: &str) -> PathBuf {
451470
let current_path = env::current_dir().expect("failed to get current directory");
@@ -479,7 +498,7 @@ mod tests {
479498
fn run_stwo_run_and_prove(
480499
args: Args,
481500
prover: Box<dyn ProverTrait>,
482-
) -> Result<usize, StwoRunAndProveError> {
501+
) -> Result<(), StwoRunAndProveError> {
483502
let prove_config = ProveConfig {
484503
verify: args.verify,
485504
proofs_dir: args.proofs_dir,
@@ -510,14 +529,7 @@ mod tests {
510529
Ok(())
511530
});
512531

513-
let successful_proof_attempt =
514-
run_stwo_run_and_prove(args, mock_prover).expect("failed to run stwo_run_and_prove");
515-
516-
assert_eq!(
517-
successful_proof_attempt, 1,
518-
"successful proof attempt should be 1, but got {:?}",
519-
successful_proof_attempt
520-
);
532+
run_stwo_run_and_prove(args, mock_prover).expect("failed to run stwo_run_and_prove");
521533

522534
(program_output_tempfile, proofs_tempdir)
523535
}
@@ -564,15 +576,7 @@ mod tests {
564576
results.next().unwrap()
565577
});
566578

567-
let successful_proof_attempt =
568-
run_stwo_run_and_prove(args, mock_prover).expect("failed to run stwo_run_and_prove");
569-
570-
assert_eq!(
571-
successful_proof_attempt, n_proof_attempts,
572-
"successful proof attempt should be {:?}, but got {:?}",
573-
n_proof_attempts, successful_proof_attempt
574-
);
575-
579+
run_stwo_run_and_prove(args, mock_prover).expect("failed to run stwo_run_and_prove");
576580
(program_output_tempfile, proofs_tempdir)
577581
}
578582

@@ -584,7 +588,7 @@ mod tests {
584588
let proof_file = proofs_temp_dir
585589
.path()
586590
.to_path_buf()
587-
.join(FIRST_PROOF_FILE_NAME);
591+
.join(format!("{}1{}", PROOF_PREFIX, SUCCESS_SUFFIX));
588592
let proof_content = std::fs::read_to_string(proof_file).expect("Failed to read proof file");
589593
let expected_proof_file = get_path(EXPECTED_PROOF_FILE_NAME);
590594
let expected_proof_content = std::fs::read_to_string(expected_proof_file)
@@ -614,11 +618,11 @@ mod tests {
614618
let proofs_dir = proofs_temp_dir.path().to_path_buf();
615619

616620
(1..=n_proof_attempts).for_each(|i| {
617-
let proof_file = proofs_dir.join(format!("proof_{}", i));
621+
let proof_file = proofs_dir.join(format!("{PROOF_PREFIX}{i}{FAILURE_SUFFIX}"));
618622
assert!(
619623
proof_file.exists(),
620624
"Proof file {:?} should exist after running with verifier failures",
621-
i,
625+
proof_file,
622626
);
623627
});
624628

@@ -637,11 +641,12 @@ mod tests {
637641
let proofs_dir = proofs_temp_dir.path().to_path_buf();
638642

639643
(1..=n_proof_attempts).for_each(|i| {
640-
let proof_file = proofs_dir.join(format!("proof_{}", i));
644+
let suffix = if i < n_proof_attempts { FAILURE_SUFFIX } else { SUCCESS_SUFFIX };
645+
let proof_file = proofs_dir.join(format!("{PROOF_PREFIX}{i}{suffix}"));
641646
assert!(
642647
proof_file.exists(),
643648
"Proof file {:?} should exist after a run that succeeds on attempt {:?} of proof and verify",
644-
i, n_proof_attempts,
649+
proof_file, n_proof_attempts,
645650
);
646651
});
647652

0 commit comments

Comments
 (0)