Skip to content

Commit 4e722af

Browse files
feat: consolidate CairoRunner ctors
1 parent 839b966 commit 4e722af

9 files changed

Lines changed: 43 additions & 76 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Both branches support Stwo prover opcodes (Blake2s, QM31) since v2.0.0.
1212

1313
#### Upcoming Changes
1414

15+
* feat: consolidate CairoRunner ctors [#2368](https://github.com/starkware-libs/cairo-vm/pull/2368)
16+
1517
* feat: create and use CairoRunConfig as ctor arg for CairoRunner ctors [#2367](https://github.com/starkware-libs/cairo-vm/pull/2367)
1618

1719
* feat: move runner_mode to be a field in StwoCairoRunConfig [#2366](https://github.com/starkware-libs/cairo-vm/pull/2366)

bench/criterion_benchmark.rs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,20 @@ fn build_many_runners(c: &mut Criterion) {
2727
let program = Program::from_bytes(program.as_slice(), Some("main")).unwrap();
2828
c.bench_function("build runner", |b| {
2929
b.iter_with_large_drop(|| {
30-
_ = black_box(
31-
CairoRunner::new(
32-
black_box(&program),
33-
black_box(
34-
&Cairo0RunConfig {
35-
layout: LayoutName::starknet_with_keccak,
36-
proof_mode: false,
37-
trace_enabled: false,
38-
disable_trace_padding: false,
39-
..Default::default()
40-
}
41-
.run_config()
42-
.unwrap(),
43-
),
44-
)
45-
.unwrap(),
46-
);
30+
_ = black_box(CairoRunner::new(
31+
black_box(&program),
32+
black_box(
33+
&Cairo0RunConfig {
34+
layout: LayoutName::starknet_with_keccak,
35+
proof_mode: false,
36+
trace_enabled: false,
37+
disable_trace_padding: false,
38+
..Default::default()
39+
}
40+
.run_config()
41+
.unwrap(),
42+
),
43+
));
4744
})
4845
});
4946
}
@@ -67,7 +64,6 @@ fn load_program_data(c: &mut Criterion) {
6764
.run_config()
6865
.unwrap(),
6966
)
70-
.unwrap()
7167
},
7268
|mut runner| _ = black_box(runner.initialize(false).unwrap()),
7369
BatchSize::SmallInput,

bench/iai_benchmark.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ fn build_runner() {
4646
}
4747
.run_config()
4848
.unwrap(),
49-
)
50-
.unwrap();
49+
);
5150
core::mem::drop(black_box(runner));
5251
}
5352

@@ -69,7 +68,6 @@ fn build_runner_helper() -> CairoRunner {
6968
.run_config()
7069
.unwrap(),
7170
)
72-
.unwrap()
7371
}
7472

7573
#[library_benchmark]

cairo-vm-cli/src/main.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,7 @@ mod tests {
291291
}
292292
.run_config()
293293
.unwrap(),
294-
)
295-
.unwrap();
294+
);
296295
let end = cairo_runner
297296
.initialize(false)
298297
.map_err(CairoRunError::Runner)?;

cairo1-run/src/cairo_run.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ pub fn cairo_run_program(
277277
)?
278278
};
279279

280-
let mut runner = CairoRunner::new_v2(&program, &cairo_run_config.run_config()?);
280+
let mut runner = CairoRunner::new(&program, &cairo_run_config.run_config()?);
281281
let end = runner.initialize(cairo_run_config.proof_mode)?;
282282
load_arguments(&mut runner, &cairo_run_config, main_func)?;
283283

vm/src/cairo_run.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ pub fn cairo_run_stwo(
143143
let _span = span!(Level::INFO, "cairo run stwo").entered();
144144

145145
let proof_mode = cairo_run_config.runner_mode != RunnerMode::ExecutionMode;
146-
let mut cairo_runner = CairoRunner::new_stwo(program, &cairo_run_config.run_config()?)?;
146+
let mut cairo_runner = CairoRunner::new(program, &cairo_run_config.run_config()?);
147147
cairo_runner.exec_scopes = exec_scopes;
148148

149149
let end = cairo_runner.initialize_stwo(allowed_builtins)?;
@@ -197,7 +197,7 @@ pub fn cairo_run_program_with_initial_scope(
197197
.allow_missing_builtins
198198
.unwrap_or(cairo_run_config.proof_mode);
199199

200-
let mut cairo_runner = CairoRunner::new(program, &cairo_run_config.run_config()?)?;
200+
let mut cairo_runner = CairoRunner::new(program, &cairo_run_config.run_config()?);
201201

202202
cairo_runner.exec_scopes = exec_scopes;
203203

@@ -297,7 +297,7 @@ pub fn cairo_run_pie(
297297
let allow_missing_builtins = cairo_run_config.allow_missing_builtins.unwrap_or_default();
298298

299299
let program = Program::from_stripped_program(&pie.metadata.program);
300-
let mut cairo_runner = CairoRunner::new(&program, &cairo_run_config.run_config()?)?;
300+
let mut cairo_runner = CairoRunner::new(&program, &cairo_run_config.run_config()?);
301301

302302
let end = cairo_runner.initialize(allow_missing_builtins)?;
303303
cairo_runner.vm.finalize_segments_by_cairo_pie(pie);
@@ -351,7 +351,7 @@ pub fn cairo_run_pie(
351351
}
352352

353353
/// Runs a Cairo PIE using the Stwo runtime API.
354-
/// Same as `cairo_run_pie` but uses `new_stwo` + `initialize_stwo` instead of layouts.
354+
/// Same as `cairo_run_pie` but uses [StwoCairoRunConfig] + `initialize_stwo` instead of layouts.
355355
/// Note: Cairo PIEs cannot be run in proof mode.
356356
/// WARNING: As the RunResources are part of the HintProcessor trait, the caller should make sure that
357357
/// the number of steps in the `RunResources` matches that of the `ExecutionResources` in the `CairoPie`.
@@ -372,14 +372,14 @@ pub fn cairo_run_pie_stwo(
372372
pie.run_validity_checks()?;
373373

374374
let program = Program::from_stripped_program(&pie.metadata.program);
375-
let mut cairo_runner = CairoRunner::new_stwo(
375+
let mut cairo_runner = CairoRunner::new(
376376
&program,
377377
&StwoCairoRunConfig {
378378
runner_mode: RunnerMode::ExecutionMode,
379379
..cairo_run_config.clone()
380380
}
381381
.run_config()?,
382-
)?;
382+
);
383383

384384
let end = cairo_runner.initialize_stwo(allowed_builtins)?;
385385
cairo_runner.vm.finalize_segments_by_cairo_pie(pie);
@@ -450,7 +450,7 @@ pub fn cairo_run_fuzzed_program(
450450
.allow_missing_builtins
451451
.unwrap_or(cairo_run_config.proof_mode);
452452

453-
let mut cairo_runner = CairoRunner::new(&program, &cairo_run_config.run_config()?)?;
453+
let mut cairo_runner = CairoRunner::new(&program, &cairo_run_config.run_config()?);
454454

455455
let _end = cairo_runner.initialize(allow_missing_builtins)?;
456456

vm/src/tests/mod.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,7 @@ fn run_cairo_1_entrypoint(
115115
}
116116
.run_config()
117117
.unwrap(),
118-
)
119-
.unwrap();
118+
);
120119

121120
let program_builtins = get_casm_contract_builtins(&contract_class, entrypoint_offset);
122121
runner
@@ -226,8 +225,7 @@ fn run_cairo_1_entrypoint_with_run_resources(
226225
}
227226
.run_config()
228227
.unwrap(),
229-
)
230-
.unwrap();
228+
);
231229

232230
let program_builtins = get_casm_contract_builtins(&contract_class, entrypoint_offset);
233231
runner

vm/src/utils.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,6 @@ pub mod test_utils {
260260
.run_config()
261261
.unwrap(),
262262
)
263-
.unwrap()
264263
};
265264
($program:expr, $layout:expr) => {
266265
crate::vm::runners::cairo_runner::CairoRunner::new(
@@ -275,7 +274,6 @@ pub mod test_utils {
275274
.run_config()
276275
.unwrap(),
277276
)
278-
.unwrap()
279277
};
280278
($program:expr, $layout:expr, $proof_mode:expr) => {
281279
crate::vm::runners::cairo_runner::CairoRunner::new(
@@ -290,7 +288,6 @@ pub mod test_utils {
290288
.run_config()
291289
.unwrap(),
292290
)
293-
.unwrap()
294291
};
295292
($program:expr, $layout:expr, $proof_mode:expr, $trace_enabled:expr) => {
296293
crate::vm::runners::cairo_runner::CairoRunner::new(
@@ -305,7 +302,6 @@ pub mod test_utils {
305302
.run_config()
306303
.unwrap(),
307304
)
308-
.unwrap()
309305
};
310306
}
311307
pub(crate) use cairo_runner;

vm/src/vm/runners/cairo_runner.rs

Lines changed: 15 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ pub enum RunnerMode {
195195
impl CairoRunner {
196196
/// The `dynamic_layout_params` argument should only be used with dynamic layout.
197197
/// It is ignored otherwise.
198-
pub fn new_v2(program: &Program, config: &CairoRunConfig) -> CairoRunner {
198+
pub fn new(program: &Program, config: &CairoRunConfig) -> CairoRunner {
199199
CairoRunner {
200200
program: program.clone(),
201201
vm: VirtualMachine::new(config.trace_enabled, config.disable_trace_padding),
@@ -221,19 +221,8 @@ impl CairoRunner {
221221
}
222222
}
223223

224-
/// Creates a `CairoRunner` for Stwo.
225-
/// Same as `new` but without layout parameters. Builtins are created from
226-
/// an explicit list via `initialize_stwo` instead of being derived from a layout.
227-
/// Must be paired with `initialize_stwo`. Do not use with `initialize`.
228-
pub fn new_stwo(
229-
program: &Program,
230-
config: &CairoRunConfig,
231-
) -> Result<CairoRunner, RunnerError> {
232-
Ok(CairoRunner::new_v2(program, config))
233-
}
234-
235224
/// Initializes the runner in Stwo mode: creates builtins, segments, entrypoint, and VM.
236-
/// Must be used with runners created via `new_stwo`. Do not use with `new`.
225+
/// Must be used with runners created via a StwoCairoRunConfig.
237226
pub fn initialize_stwo(
238227
&mut self,
239228
allowed_builtins: &[BuiltinName],
@@ -250,10 +239,6 @@ impl CairoRunner {
250239
Ok(end)
251240
}
252241

253-
pub fn new(program: &Program, config: &CairoRunConfig) -> Result<CairoRunner, RunnerError> {
254-
Ok(Self::new_v2(program, config))
255-
}
256-
257242
pub fn initialize(&mut self, allow_missing_builtins: bool) -> Result<Relocatable, RunnerError> {
258243
info!("Initializing builtins.");
259244
self.initialize_builtins(allow_missing_builtins)?;
@@ -5745,8 +5730,7 @@ mod tests {
57455730
}
57465731
.run_config()
57475732
.unwrap(),
5748-
)
5749-
.expect("failed to create runner");
5733+
);
57505734

57515735
// We allow missing builtins, as we will simulate them later.
57525736
let end = cairo_runner
@@ -5774,7 +5758,7 @@ mod tests {
57745758
Some("main"),
57755759
)
57765760
.unwrap();
5777-
let runner = CairoRunner::new_stwo(
5761+
let runner = CairoRunner::new(
57785762
&program,
57795763
&StwoCairoRunConfig {
57805764
runner_mode: RunnerMode::ProofModeCanonical,
@@ -5784,8 +5768,7 @@ mod tests {
57845768
}
57855769
.run_config()
57865770
.unwrap(),
5787-
)
5788-
.unwrap();
5771+
);
57895772
assert_eq!(runner.runner_mode, RunnerMode::ProofModeCanonical);
57905773
assert!(runner.execution_public_memory.is_some());
57915774
}
@@ -5797,7 +5780,7 @@ mod tests {
57975780
Some("main"),
57985781
)
57995782
.unwrap();
5800-
let runner = CairoRunner::new_stwo(
5783+
let runner = CairoRunner::new(
58015784
&program,
58025785
&StwoCairoRunConfig {
58035786
runner_mode: RunnerMode::ExecutionMode,
@@ -5807,8 +5790,7 @@ mod tests {
58075790
}
58085791
.run_config()
58095792
.unwrap(),
5810-
)
5811-
.unwrap();
5793+
);
58125794
assert_eq!(runner.runner_mode, RunnerMode::ExecutionMode);
58135795
assert!(runner.execution_public_memory.is_none());
58145796
}
@@ -5835,7 +5817,7 @@ mod tests {
58355817
Some("main"),
58365818
)
58375819
.unwrap();
5838-
let mut runner = CairoRunner::new_stwo(
5820+
let mut runner = CairoRunner::new(
58395821
&program,
58405822
&StwoCairoRunConfig {
58415823
runner_mode: RunnerMode::ProofModeCanonical,
@@ -5845,8 +5827,7 @@ mod tests {
58455827
}
58465828
.run_config()
58475829
.unwrap(),
5848-
)
5849-
.unwrap();
5830+
);
58505831
let allowed = vec![
58515832
BuiltinName::output,
58525833
BuiltinName::pedersen,
@@ -5869,7 +5850,7 @@ mod tests {
58695850
#[test]
58705851
fn initialize_builtins_stwo_rejects_ecdsa() {
58715852
let program = Program::default();
5872-
let mut runner = CairoRunner::new_stwo(
5853+
let mut runner = CairoRunner::new(
58735854
&program,
58745855
&StwoCairoRunConfig {
58755856
runner_mode: RunnerMode::ProofModeCanonical,
@@ -5879,8 +5860,7 @@ mod tests {
58795860
}
58805861
.run_config()
58815862
.unwrap(),
5882-
)
5883-
.unwrap();
5863+
);
58845864
match runner.initialize_builtins_stwo(&[BuiltinName::ecdsa]) {
58855865
Err(RunnerError::UnsupportedStwoBuiltin(BuiltinName::ecdsa)) => {}
58865866
_ => panic!("Expected UnsupportedStwoBuiltin(ecdsa) error"),
@@ -5890,7 +5870,7 @@ mod tests {
58905870
#[test]
58915871
fn initialize_builtins_stwo_rejects_keccak() {
58925872
let program = Program::default();
5893-
let mut runner = CairoRunner::new_stwo(
5873+
let mut runner = CairoRunner::new(
58945874
&program,
58955875
&StwoCairoRunConfig {
58965876
runner_mode: RunnerMode::ProofModeCanonical,
@@ -5900,8 +5880,7 @@ mod tests {
59005880
}
59015881
.run_config()
59025882
.unwrap(),
5903-
)
5904-
.unwrap();
5883+
);
59055884
match runner.initialize_builtins_stwo(&[BuiltinName::keccak]) {
59065885
Err(RunnerError::UnsupportedStwoBuiltin(BuiltinName::keccak)) => {}
59075886
_ => panic!("Expected UnsupportedStwoBuiltin(keccak) error"),
@@ -5915,7 +5894,7 @@ mod tests {
59155894
Some("main"),
59165895
)
59175896
.unwrap();
5918-
let mut runner = CairoRunner::new_stwo(
5897+
let mut runner = CairoRunner::new(
59195898
&program,
59205899
&StwoCairoRunConfig {
59215900
runner_mode: RunnerMode::ProofModeCanonical,
@@ -5925,8 +5904,7 @@ mod tests {
59255904
}
59265905
.run_config()
59275906
.unwrap(),
5928-
)
5929-
.unwrap();
5907+
);
59305908
// bitwise_builtin_test requires bitwise, but we only allow output
59315909
match runner.initialize_builtins_stwo(&[BuiltinName::output]) {
59325910
Err(RunnerError::UnsupportedStwoBuiltin(BuiltinName::bitwise)) => {}

0 commit comments

Comments
 (0)