|
| 1 | +use std::path::PathBuf; |
| 2 | +use std::process::ExitCode; |
| 3 | + |
| 4 | +use clap::Parser; |
| 5 | +use stwo_cairo_adapter::plain::adapt_finished_runner; |
| 6 | +use stwo_cairo_adapter::vm_import::VmImportError; |
| 7 | +use stwo_cairo_adapter::{ExecutionResources, ProverInput}; |
| 8 | +use stwo_cairo_utils::binary_utils::run_binary; |
| 9 | +use stwo_cairo_utils::vm_utils::{run_vm, VmArgs, VmError}; |
| 10 | +use thiserror::Error; |
| 11 | +use tracing::{span, Level}; |
| 12 | + |
| 13 | +/// Command line arguments for stwo_vm_runner. |
| 14 | +/// Example command line (use absolute paths): |
| 15 | +/// ``` |
| 16 | +/// cargo run -r --bin stwo_vm_runner -- --run_from_cairo_pie |
| 17 | +/// --output_path path/to/output --secure_run=true path/to/cairo/pie |
| 18 | +/// ``` |
| 19 | +#[derive(Parser, Debug)] |
| 20 | +#[clap(author, version, about, long_about = None)] |
| 21 | +struct Args { |
| 22 | + #[command(flatten)] |
| 23 | + vm_args: VmArgs, |
| 24 | + /// The file path for the output (the adapted execution resources of the VM run). |
| 25 | + #[structopt(long = "output_path")] |
| 26 | + output_path: PathBuf, |
| 27 | +} |
| 28 | + |
| 29 | +#[derive(Debug, Error)] |
| 30 | +enum Error { |
| 31 | + #[error("Invalid arguments")] |
| 32 | + Cli(#[from] clap::Error), |
| 33 | + #[error("Failed to interact with the file system")] |
| 34 | + IO(#[from] std::io::Error), |
| 35 | + #[error("VM run failed: {0}")] |
| 36 | + Vm(#[from] VmError), |
| 37 | + #[error("Serialization failed: {0}")] |
| 38 | + Serde(#[from] serde_json::Error), |
| 39 | + #[error("VM import failed: {0}")] |
| 40 | + VmImport(#[from] VmImportError), |
| 41 | +} |
| 42 | + |
| 43 | +fn main() -> ExitCode { |
| 44 | + run_binary(run, "stwo_vm_runner") |
| 45 | +} |
| 46 | + |
| 47 | +fn run(args: impl Iterator<Item = String>) -> Result<ProverInput, Error> { |
| 48 | + let _span = span!(Level::INFO, "run").entered(); |
| 49 | + let args = Args::try_parse_from(args)?; |
| 50 | + |
| 51 | + // Usually vm_runner runs the VM in non-proof-mode, in which case we don't need |
| 52 | + // `disable_trace_padding`. If it runs the VM in proof-mode, it should also disable trace |
| 53 | + // padding as this is the padding relevant for Stwo. |
| 54 | + let disable_trace_padding = args.vm_args.proof_mode; |
| 55 | + let cairo_runner = run_vm(&args.vm_args, disable_trace_padding)?; |
| 56 | + let cairo_input = adapt_finished_runner(cairo_runner)?; |
| 57 | + |
| 58 | + let execution_resources = ExecutionResources::from_prover_input(&cairo_input); |
| 59 | + log::info!("Execution resources: {:#?}", execution_resources); |
| 60 | + std::fs::write( |
| 61 | + args.output_path, |
| 62 | + serde_json::to_string(&execution_resources)?, |
| 63 | + )?; |
| 64 | + |
| 65 | + Ok(cairo_input) |
| 66 | +} |
0 commit comments