Skip to content

Commit 932986c

Browse files
fmolettaOppen
andauthored
Use an enum to represent layout name (#1715)
* Use enum for LayoutName * Move LayoutName to its own module + fix tests * Fix clap enum parsing * Fix tests * Fix test & doc * Manually implement ValueEnum * fmt * Simplify code * Move arbitrary impl to module * Fix benchmark code * Add changelog entry * Remove unnecessary impl * Use value instead of reference Co-authored-by: Mario Rugiero <[email protected]> * Fix test added by merge --------- Co-authored-by: Mario Rugiero <[email protected]>
1 parent 4b17118 commit 932986c

File tree

30 files changed

+240
-216
lines changed

30 files changed

+240
-216
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
#### Upcoming Changes
44

5+
* refactor(BREAKING): Use an enum to represent layout name[#1715](https://github.com/lambdaclass/cairo-vm/pull/1715)
6+
* Add enum `LayoutName` to represent cairo layout names.
7+
* `CairoRunConfig`, `Cairo1RunConfig` & `CairoRunner` field `layout` type changed from `String` to `LayoutName`.
8+
* `CairoLayout` field `name` type changed from `String` to `LayoutName`.
9+
510
* fix(BREAKING): Remove unsafe impl of `Add<usize> for &'a Relocatable`[#1718](https://github.com/lambdaclass/cairo-vm/pull/1718)
611

712
* fix(BREAKING): Handle triple dereference references[#1708](https://github.com/lambdaclass/cairo-vm/pull/1708)

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ When running a Cairo program directly using the Cairo-vm repository you would fi
209209
```rust
210210
let mut vm = VirtualMachine::new(false);
211211

212-
let mut cairo_runner = CairoRunner::new(&program, "all_cairo", false);
212+
let mut cairo_runner = CairoRunner::new(&program, LayoutName::all_cairo, false);
213213

214214
let mut hint_processor = BuiltinHintProcessor::new_empty();
215215

bench/criterion_benchmark.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use cairo_vm::{
2-
types::program::Program,
2+
types::{layout_name::LayoutName, program::Program},
33
vm::{runners::cairo_runner::CairoRunner, vm_core::VirtualMachine},
44
};
55
use criterion::{black_box, criterion_group, criterion_main, BatchSize, Criterion};
@@ -29,7 +29,7 @@ fn build_many_runners(c: &mut Criterion) {
2929
_ = black_box(
3030
CairoRunner::new(
3131
black_box(&program),
32-
black_box("starknet_with_keccak"),
32+
black_box(LayoutName::starknet_with_keccak),
3333
black_box(false),
3434
)
3535
.unwrap(),
@@ -46,7 +46,7 @@ fn load_program_data(c: &mut Criterion) {
4646
b.iter_batched(
4747
|| {
4848
(
49-
CairoRunner::new(&program, "starknet_with_keccak", false).unwrap(),
49+
CairoRunner::new(&program, LayoutName::starknet_with_keccak, false).unwrap(),
5050
VirtualMachine::new(false),
5151
)
5252
},

bench/iai_benchmark.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use core::hint::black_box;
22
use iai_callgrind::main;
33

44
use cairo_vm::{
5-
types::program::Program,
5+
types::{layout_name::LayoutName, program::Program},
66
vm::{runners::cairo_runner::CairoRunner, vm_core::VirtualMachine},
77
};
88

@@ -31,7 +31,8 @@ fn parse_program_helper() -> Program {
3131
#[inline(never)]
3232
fn build_runner() {
3333
let program = parse_program_helper();
34-
let runner = CairoRunner::new(black_box(&program), "starknet_with_keccak", false).unwrap();
34+
let runner =
35+
CairoRunner::new(black_box(&program), LayoutName::starknet_with_keccak, false).unwrap();
3536
core::mem::drop(black_box(runner));
3637
}
3738

@@ -41,7 +42,7 @@ fn build_runner_helper() -> (CairoRunner, VirtualMachine) {
4142
//Picked the biggest one at the time of writing
4243
let program = include_bytes!("../cairo_programs/benchmarks/keccak_integration_benchmark.json");
4344
let program = Program::from_bytes(program.as_slice(), Some("main")).unwrap();
44-
let runner = CairoRunner::new(&program, "starknet_with_keccak", false).unwrap();
45+
let runner = CairoRunner::new(&program, LayoutName::starknet_with_keccak, false).unwrap();
4546
let vm = VirtualMachine::new(false);
4647
(runner, vm)
4748
}

cairo-vm-cli/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ readme.workspace = true
88
keywords.workspace = true
99

1010
[dependencies]
11-
cairo-vm = { workspace = true, features = ["std"] }
11+
cairo-vm = { workspace = true, features = ["std", "clap"] }
1212
cairo-vm-tracer = { workspace = true, optional = true }
1313
clap = { version = "4.3.10", features = ["derive"] }
1414
mimalloc = { version = "0.1.37", default-features = false, optional = true }

cairo-vm-cli/src/main.rs

Lines changed: 4 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use cairo_vm::cairo_run::{self, EncodeTraceError};
66
use cairo_vm::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::BuiltinHintProcessor;
77
#[cfg(feature = "with_tracer")]
88
use cairo_vm::serde::deserialize_program::DebugInfo;
9+
use cairo_vm::types::layout_name::LayoutName;
910
use cairo_vm::vm::errors::cairo_run_errors::CairoRunError;
1011
use cairo_vm::vm::errors::trace_errors::TraceError;
1112
use cairo_vm::vm::errors::vm_errors::VirtualMachineError;
@@ -42,8 +43,8 @@ struct Args {
4243
entrypoint: String,
4344
#[structopt(long = "memory_file")]
4445
memory_file: Option<PathBuf>,
45-
#[clap(long = "layout", default_value = "plain", value_parser=validate_layout)]
46-
layout: String,
46+
#[clap(long = "layout", default_value = "plain", value_enum)]
47+
layout: LayoutName,
4748
#[structopt(long = "proof_mode")]
4849
proof_mode: bool,
4950
#[structopt(long = "secure_run")]
@@ -69,22 +70,6 @@ struct Args {
6970
tracer: bool,
7071
}
7172

72-
fn validate_layout(value: &str) -> Result<String, String> {
73-
match value {
74-
"plain"
75-
| "small"
76-
| "dex"
77-
| "recursive"
78-
| "starknet"
79-
| "starknet_with_keccak"
80-
| "recursive_large_output"
81-
| "all_cairo"
82-
| "all_solidity"
83-
| "dynamic" => Ok(value.to_string()),
84-
_ => Err(format!("{value} is not a valid layout")),
85-
}
86-
}
87-
8873
#[derive(Debug, Error)]
8974
enum Error {
9075
#[error("Invalid arguments")]
@@ -173,7 +158,7 @@ fn run(args: impl Iterator<Item = String>) -> Result<(), Error> {
173158
entrypoint: &args.entrypoint,
174159
trace_enabled,
175160
relocate_mem: args.memory_file.is_some() || args.air_public_input.is_some(),
176-
layout: &args.layout,
161+
layout: args.layout,
177162
proof_mode: args.proof_mode,
178163
secure_run: args.secure_run,
179164
allow_missing_builtins: args.allow_missing_builtins,
@@ -410,28 +395,4 @@ mod tests {
410395
fn test_main() {
411396
main().unwrap();
412397
}
413-
414-
#[test]
415-
fn test_valid_layouts() {
416-
let valid_layouts = vec![
417-
"plain",
418-
"small",
419-
"dex",
420-
"starknet",
421-
"starknet_with_keccak",
422-
"recursive_large_output",
423-
"all_cairo",
424-
"all_solidity",
425-
];
426-
427-
for layout in valid_layouts {
428-
assert_eq!(validate_layout(layout), Ok(layout.to_string()));
429-
}
430-
}
431-
432-
#[test]
433-
fn test_invalid_layout() {
434-
let invalid_layout = "invalid layout name";
435-
assert!(validate_layout(invalid_layout).is_err());
436-
}
437398
}

cairo1-run/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ keywords.workspace = true
99
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1010

1111
[dependencies]
12-
cairo-vm = {workspace = true, features = ["std", "cairo-1-hints"]}
12+
cairo-vm = {workspace = true, features = ["std", "cairo-1-hints", "clap"]}
1313

1414
cairo-lang-sierra-type-size = { version = "2.5.4", default-features = false }
1515
cairo-lang-sierra-ap-change = { version = "2.5.4", default-features = false }

cairo1-run/src/cairo_run.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use cairo_vm::{
3232
serde::deserialize_program::{
3333
ApTracking, BuiltinName, FlowTrackingData, HintParams, ReferenceManager,
3434
},
35-
types::{program::Program, relocatable::MaybeRelocatable},
35+
types::{layout_name::LayoutName, program::Program, relocatable::MaybeRelocatable},
3636
vm::{
3737
errors::{runner_errors::RunnerError, vm_errors::VirtualMachineError},
3838
runners::{
@@ -59,7 +59,7 @@ pub struct Cairo1RunConfig<'a> {
5959
pub serialize_output: bool,
6060
pub trace_enabled: bool,
6161
pub relocate_mem: bool,
62-
pub layout: &'a str,
62+
pub layout: LayoutName,
6363
pub proof_mode: bool,
6464
// Should be true if either air_public_input or cairo_pie_output are needed
6565
// Sets builtins stop_ptr by calling `final_stack` on each builtin
@@ -75,7 +75,7 @@ impl Default for Cairo1RunConfig<'_> {
7575
serialize_output: false,
7676
trace_enabled: false,
7777
relocate_mem: false,
78-
layout: "plain",
78+
layout: LayoutName::plain,
7979
proof_mode: false,
8080
finalize_builtins: false,
8181
append_return_values: false,
@@ -1170,7 +1170,7 @@ mod tests {
11701170
// Set proof_mode
11711171
let cairo_run_config = Cairo1RunConfig {
11721172
proof_mode,
1173-
layout: "all_cairo",
1173+
layout: LayoutName::all_cairo,
11741174
append_return_values: !proof_mode, // This is so we can test appending return values when not running in proof_mode
11751175
finalize_builtins: true,
11761176
..Default::default()

cairo1-run/src/main.rs

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use cairo_run::Cairo1RunConfig;
66
use cairo_vm::{
77
air_public_input::PublicInputError,
88
cairo_run::EncodeTraceError,
9-
types::errors::program_errors::ProgramError,
9+
types::{errors::program_errors::ProgramError, layout_name::LayoutName},
1010
vm::errors::{
1111
memory_errors::MemoryError, runner_errors::RunnerError, trace_errors::TraceError,
1212
vm_errors::VirtualMachineError,
@@ -32,8 +32,8 @@ struct Args {
3232
trace_file: Option<PathBuf>,
3333
#[structopt(long = "memory_file")]
3434
memory_file: Option<PathBuf>,
35-
#[clap(long = "layout", default_value = "plain", value_parser=validate_layout)]
36-
layout: String,
35+
#[clap(long = "layout", default_value = "plain", value_enum)]
36+
layout: LayoutName,
3737
#[clap(long = "proof_mode", value_parser)]
3838
proof_mode: bool,
3939
#[clap(long = "air_public_input", requires = "proof_mode")]
@@ -109,22 +109,6 @@ fn process_args(value: &str) -> Result<FuncArgs, String> {
109109
Ok(FuncArgs(args))
110110
}
111111

112-
fn validate_layout(value: &str) -> Result<String, String> {
113-
match value {
114-
"plain"
115-
| "small"
116-
| "dex"
117-
| "recursive"
118-
| "starknet"
119-
| "starknet_with_keccak"
120-
| "recursive_large_output"
121-
| "all_cairo"
122-
| "all_solidity"
123-
| "dynamic" => Ok(value.to_string()),
124-
_ => Err(format!("{value} is not a valid layout")),
125-
}
126-
}
127-
128112
#[derive(Debug, Error)]
129113
pub enum Error {
130114
#[error("Invalid arguments")]
@@ -214,7 +198,7 @@ fn run(args: impl Iterator<Item = String>) -> Result<Option<String>, Error> {
214198
proof_mode: args.proof_mode,
215199
serialize_output: args.print_output,
216200
relocate_mem: args.memory_file.is_some() || args.air_public_input.is_some(),
217-
layout: &args.layout,
201+
layout: args.layout,
218202
trace_enabled: args.trace_file.is_some() || args.air_public_input.is_some(),
219203
args: &args.args.0,
220204
finalize_builtins: args.air_private_input.is_some() || args.cairo_pie_output.is_some(),

0 commit comments

Comments
 (0)