Skip to content

Commit 42dc7a7

Browse files
Create a crate for vm_runner (#125)
1 parent 821d6dc commit 42dc7a7

File tree

3 files changed

+89
-4
lines changed

3 files changed

+89
-4
lines changed

Cargo.toml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
11
[workspace]
22
members = [
33
"crates/cairo-program-runner",
4-
"crates/cairo-program-runner-lib"
4+
"crates/cairo-program-runner-lib",
5+
"crates/vm_runner",
56
]
67
resolver = "2"
78

89
[workspace.dependencies]
910
bincode = { version = "2.0.0-rc.3", default-features = false, features = [
1011
"serde",
1112
] }
13+
cairo-lang-runner = { git = "https://github.com/starkware-libs/cairo.git", rev = "ddaec76b4364ca6e0af4d185a99904d13b93f81e" }
1214
cairo-vm = { version = "2.0.1", features = [
1315
"extensive_hints",
1416
"mod_builtin",
1517
"clap",
1618
] }
17-
cairo-lang-runner = { git = "https://github.com/starkware-libs/cairo.git", rev = "ddaec76b4364ca6e0af4d185a99904d13b93f81e" }
1819
clap = { version = "4.3.10", features = ["derive"] }
20+
log = "0.4.21"
21+
num-bigint = "0.4"
1922
num-traits = "0.2.19"
23+
regex = "1.11.1"
2024
serde = { version = "1.0.202", features = ["derive"] }
2125
serde_json = "1.0.117"
2226
starknet-crypto = "0.6.2"
2327
starknet-types-core = "0.1.2"
28+
stwo_cairo_utils = { git = "https://github.com/starkware-libs/stwo-cairo", rev = "ef6b24df"}
29+
stwo-cairo-adapter = { git = "https://github.com/starkware-libs/stwo-cairo", rev = "ef6b24df"}
2430
tempfile = "3.10.1"
2531
thiserror = "1.0.61"
2632
thiserror-no-std = "2.0.2"
27-
regex = "1.11.1"
28-
num-bigint = "0.4"
33+
tracing = "0.1.40"

crates/vm_runner/Cargo.toml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[package]
2+
name = "stwo_vm_runner"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
clap.workspace = true
8+
log.workspace = true
9+
num-traits.workspace = true
10+
serde_json.workspace = true
11+
stwo_cairo_utils.workspace = true
12+
stwo-cairo-adapter.workspace = true
13+
thiserror.workspace = true
14+
tracing.workspace = true

crates/vm_runner/src/main.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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

Comments
 (0)