Skip to content

Commit 87dcd17

Browse files
authored
Add lib.rs to cairo1-run (#1714)
* Add lib.rs * Add changelog entry * Add FuncArg docs * Add Cairo1RunConfig docs * fix * fmt * fmt
1 parent 5e19e9d commit 87dcd17

File tree

5 files changed

+109
-76
lines changed

5 files changed

+109
-76
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
* Added segment merging of the dictionary segments.
77
* Added validation of the generated segment arena in cairo1 run.
88

9+
* refactor: Add `lib.rs` to cairo1-run[#1714](https://github.com/lambdaclass/cairo-vm/pull/1714)
10+
911
* feat: Implement `extend_additional_data` for `BuiltinRunner`[#1726](https://github.com/lambdaclass/cairo-vm/pull/1726)
1012

1113
* BREAKING: Set dynamic params as null by default on air public input [#1716](https://github.com/lambdaclass/cairo-vm/pull/1716)

cairo1-run/src/cairo_run.rs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::error::Error;
12
use cairo_lang_casm::{
23
builder::{CasmBuilder, Var},
34
casm, casm_build_extend,
@@ -51,21 +52,45 @@ use itertools::{chain, Itertools};
5152
use num_traits::{cast::ToPrimitive, Zero};
5253
use std::{collections::HashMap, iter::Peekable};
5354

54-
use crate::{Error, FuncArg};
55+
/// Representation of a cairo argument
56+
/// Can consist of a single Felt or an array of Felts
57+
#[derive(Debug, Clone)]
58+
pub enum FuncArg {
59+
Array(Vec<Felt252>),
60+
Single(Felt252),
61+
}
62+
63+
impl From<Felt252> for FuncArg {
64+
fn from(value: Felt252) -> Self {
65+
Self::Single(value)
66+
}
67+
}
68+
69+
impl From<Vec<Felt252>> for FuncArg {
70+
fn from(value: Vec<Felt252>) -> Self {
71+
Self::Array(value)
72+
}
73+
}
5574

75+
/// Configuration parameters for a cairo run
5676
#[derive(Debug)]
5777
pub struct Cairo1RunConfig<'a> {
78+
/// Input arguments for the `main` function in the cairo progran
5879
pub args: &'a [FuncArg],
59-
// Serializes program output into a user-friendly format
80+
/// Serialize program output into a user-friendly format
6081
pub serialize_output: bool,
82+
/// Compute cairo trace during execution
6183
pub trace_enabled: bool,
84+
/// Relocate cairo memory at the end of the run
6285
pub relocate_mem: bool,
86+
/// Cairo layout chosen for the run
6387
pub layout: LayoutName,
88+
/// Run in proof_mode
6489
pub proof_mode: bool,
65-
// Should be true if either air_public_input or cairo_pie_output are needed
66-
// Sets builtins stop_ptr by calling `final_stack` on each builtin
90+
/// Should be true if either air_public_input or cairo_pie_output are needed
91+
/// Sets builtins stop_ptr by calling `final_stack` on each builtin
6792
pub finalize_builtins: bool,
68-
// Appends return values to the output segment. This is performed by default when running in proof_mode
93+
/// Appends return values to the output segment. This is performed by default when running in proof_mode
6994
pub append_return_values: bool,
7095
}
7196

cairo1-run/src/error.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
use cairo_lang_sierra::{ids::ConcreteTypeId, program_registry::ProgramRegistryError};
2+
use cairo_lang_sierra_to_casm::{compiler::CompilationError, metadata::MetadataError};
3+
use cairo_vm::{
4+
air_public_input::PublicInputError,
5+
cairo_run::EncodeTraceError,
6+
types::errors::program_errors::ProgramError,
7+
vm::errors::{
8+
memory_errors::MemoryError, runner_errors::RunnerError, trace_errors::TraceError,
9+
vm_errors::VirtualMachineError,
10+
},
11+
Felt252,
12+
};
13+
use thiserror::Error;
14+
15+
#[derive(Debug, Error)]
16+
pub enum Error {
17+
#[error("Invalid arguments")]
18+
Cli(#[from] clap::Error),
19+
#[error("Failed to interact with the file system")]
20+
IO(#[from] std::io::Error),
21+
#[error(transparent)]
22+
EncodeTrace(#[from] EncodeTraceError),
23+
#[error(transparent)]
24+
VirtualMachine(#[from] VirtualMachineError),
25+
#[error(transparent)]
26+
Trace(#[from] TraceError),
27+
#[error(transparent)]
28+
PublicInput(#[from] PublicInputError),
29+
#[error(transparent)]
30+
Runner(#[from] RunnerError),
31+
#[error(transparent)]
32+
ProgramRegistry(#[from] Box<ProgramRegistryError>),
33+
#[error(transparent)]
34+
Compilation(#[from] Box<CompilationError>),
35+
#[error("Failed to compile to sierra:\n {0}")]
36+
SierraCompilation(String),
37+
#[error(transparent)]
38+
Metadata(#[from] MetadataError),
39+
#[error(transparent)]
40+
Program(#[from] ProgramError),
41+
#[error(transparent)]
42+
Memory(#[from] MemoryError),
43+
#[error("Program panicked with {0:?}")]
44+
RunPanic(Vec<Felt252>),
45+
#[error("Function signature has no return types")]
46+
NoRetTypesInSignature,
47+
#[error("No size for concrete type id: {0}")]
48+
NoTypeSizeForId(ConcreteTypeId),
49+
#[error("Concrete type id has no debug name: {0}")]
50+
TypeIdNoDebugName(ConcreteTypeId),
51+
#[error("No info in sierra program registry for concrete type id: {0}")]
52+
NoInfoForType(ConcreteTypeId),
53+
#[error("Failed to extract return values from VM")]
54+
FailedToExtractReturnValues,
55+
#[error("Function expects arguments of size {expected} and received {actual} instead.")]
56+
ArgumentsSizeMismatch { expected: i16, actual: i16 },
57+
#[error("Function param {param_index} only partially contains argument {arg_index}.")]
58+
ArgumentUnaligned {
59+
param_index: usize,
60+
arg_index: usize,
61+
},
62+
}

cairo1-run/src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
pub mod cairo_run;
2+
pub mod error;
3+
// Re-export main struct and functions from crate for convenience
4+
pub use crate::cairo_run::{cairo_run_program, Cairo1RunConfig, FuncArg};
5+
// Re-export cairo_vm structs returned by this crate for ease of use
6+
pub use cairo_vm::{
7+
types::relocatable::{MaybeRelocatable, Relocatable},
8+
vm::{runners::cairo_runner::CairoRunner, vm_core::VirtualMachine},
9+
Felt252,
10+
};

cairo1-run/src/main.rs

Lines changed: 5 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,17 @@
11
use bincode::enc::write::Writer;
2+
use cairo1_run::error::Error;
3+
use cairo1_run::{cairo_run_program, Cairo1RunConfig, FuncArg};
24
use cairo_lang_compiler::{compile_cairo_project_at_path, CompilerConfig};
3-
use cairo_lang_sierra::{ids::ConcreteTypeId, program_registry::ProgramRegistryError};
4-
use cairo_lang_sierra_to_casm::{compiler::CompilationError, metadata::MetadataError};
5-
use cairo_run::Cairo1RunConfig;
65
use cairo_vm::{
7-
air_public_input::PublicInputError,
8-
cairo_run::EncodeTraceError,
9-
types::{errors::program_errors::ProgramError, layout_name::LayoutName},
10-
vm::errors::{
11-
memory_errors::MemoryError, runner_errors::RunnerError, trace_errors::TraceError,
12-
vm_errors::VirtualMachineError,
13-
},
14-
Felt252,
6+
air_public_input::PublicInputError, types::layout_name::LayoutName,
7+
vm::errors::trace_errors::TraceError, Felt252,
158
};
169
use clap::{Parser, ValueHint};
1710
use itertools::Itertools;
1811
use std::{
1912
io::{self, Write},
2013
path::PathBuf,
2114
};
22-
use thiserror::Error;
23-
24-
pub mod cairo_run;
2515

2616
#[derive(Parser, Debug)]
2717
#[clap(author, version, about, long_about = None)]
@@ -65,12 +55,6 @@ struct Args {
6555
append_return_values: bool,
6656
}
6757

68-
#[derive(Debug, Clone)]
69-
pub enum FuncArg {
70-
Array(Vec<Felt252>),
71-
Single(Felt252),
72-
}
73-
7458
#[derive(Debug, Clone, Default)]
7559
struct FuncArgs(Vec<FuncArg>);
7660

@@ -109,55 +93,6 @@ fn process_args(value: &str) -> Result<FuncArgs, String> {
10993
Ok(FuncArgs(args))
11094
}
11195

112-
#[derive(Debug, Error)]
113-
pub enum Error {
114-
#[error("Invalid arguments")]
115-
Cli(#[from] clap::Error),
116-
#[error("Failed to interact with the file system")]
117-
IO(#[from] std::io::Error),
118-
#[error(transparent)]
119-
EncodeTrace(#[from] EncodeTraceError),
120-
#[error(transparent)]
121-
VirtualMachine(#[from] VirtualMachineError),
122-
#[error(transparent)]
123-
Trace(#[from] TraceError),
124-
#[error(transparent)]
125-
PublicInput(#[from] PublicInputError),
126-
#[error(transparent)]
127-
Runner(#[from] RunnerError),
128-
#[error(transparent)]
129-
ProgramRegistry(#[from] Box<ProgramRegistryError>),
130-
#[error(transparent)]
131-
Compilation(#[from] Box<CompilationError>),
132-
#[error("Failed to compile to sierra:\n {0}")]
133-
SierraCompilation(String),
134-
#[error(transparent)]
135-
Metadata(#[from] MetadataError),
136-
#[error(transparent)]
137-
Program(#[from] ProgramError),
138-
#[error(transparent)]
139-
Memory(#[from] MemoryError),
140-
#[error("Program panicked with {0:?}")]
141-
RunPanic(Vec<Felt252>),
142-
#[error("Function signature has no return types")]
143-
NoRetTypesInSignature,
144-
#[error("No size for concrete type id: {0}")]
145-
NoTypeSizeForId(ConcreteTypeId),
146-
#[error("Concrete type id has no debug name: {0}")]
147-
TypeIdNoDebugName(ConcreteTypeId),
148-
#[error("No info in sierra program registry for concrete type id: {0}")]
149-
NoInfoForType(ConcreteTypeId),
150-
#[error("Failed to extract return values from VM")]
151-
FailedToExtractReturnValues,
152-
#[error("Function expects arguments of size {expected} and received {actual} instead.")]
153-
ArgumentsSizeMismatch { expected: i16, actual: i16 },
154-
#[error("Function param {param_index} only partially contains argument {arg_index}.")]
155-
ArgumentUnaligned {
156-
param_index: usize,
157-
arg_index: usize,
158-
},
159-
}
160-
16196
pub struct FileWriter {
16297
buf_writer: io::BufWriter<std::fs::File>,
16398
bytes_written: usize,
@@ -220,8 +155,7 @@ fn run(args: impl Iterator<Item = String>) -> Result<Option<String>, Error> {
220155
}
221156
};
222157

223-
let (runner, vm, _, serialized_output) =
224-
cairo_run::cairo_run_program(&sierra_program, cairo_run_config)?;
158+
let (runner, vm, _, serialized_output) = cairo_run_program(&sierra_program, cairo_run_config)?;
225159

226160
if let Some(file_path) = args.air_public_input {
227161
let json = runner.get_air_public_input(&vm)?.serialize_json()?;

0 commit comments

Comments
 (0)