Skip to content

Commit efce14d

Browse files
committed
feat: add math cairo tests to cairo-vm
2 parents d6a6c58 + 78eae20 commit efce14d

48 files changed

Lines changed: 1229 additions & 1028 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CHANGELOG.md

Lines changed: 527 additions & 507 deletions
Large diffs are not rendered by default.

CONTRIBUTING.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ All types of contributions are encouraged and valued. See the [Table of Contents
2929

3030
> If you want to ask a question, we assume that you have read the available [Documentation](docs/).
3131
32-
Before you ask a question, it is best to search for existing [Issues](https://github.com/lambdaclass/cairo-vm/issues) that might help you. In case you have found a suitable issue and still need clarification, you can write your question in this issue. It is also advisable to search the internet for answers first.
32+
Before you ask a question, it is best to search for existing [Issues](https://github.com/starkware-libs/cairo-vm/issues) that might help you. In case you have found a suitable issue and still need clarification, you can write your question in this issue. It is also advisable to search the internet for answers first.
3333

3434
If you then still feel the need to ask a question and need clarification, we recommend the following:
3535

36-
- Open an [Issue](https://github.com/lambdaclass/cairo-vm/issues/new).
36+
- Open an [Issue](https://github.com/starkware-libs/cairo-vm/issues/new).
3737
- Provide as much context as you can about what you're running into.
3838
- Provide project and platform versions (cairo language, rust compiler, Python, etc), depending on what seems relevant.
3939

@@ -73,7 +73,7 @@ A good bug report shouldn't leave others needing to chase you up for more inform
7373

7474
We use GitHub issues to track bugs and errors. If you run into an issue with the project:
7575

76-
- Open an [Issue](https://github.com/lambdaclass/cairo-vm/issues/new). (Since we can't be sure at this point whether it is a bug or not, we ask you not to talk about a bug yet and not to label the issue.)
76+
- Open an [Issue](https://github.com/starkware-libs/cairo-vm/issues/new). (Since we can't be sure at this point whether it is a bug or not, we ask you not to talk about a bug yet and not to label the issue.)
7777
- Explain the behavior you would expect and the actual behavior.
7878
- Please provide as much context as possible and describe the *reproduction steps* that someone else can follow to recreate the issue on their own. This usually includes your code. For good bug reports you should isolate the problem and create a reduced test case.
7979
- Provide the information you collected in the previous section.
@@ -96,13 +96,13 @@ This section guides you through submitting an enhancement suggestion for CONTRIB
9696

9797
- Make sure that you are using the latest version.
9898
- Read the [documentation](docs/) carefully and find out if the functionality is already covered, maybe by an individual configuration.
99-
- Perform a [search](https://github.com/lambdaclass/cairo-vm/issues) to see if the enhancement has already been suggested. If it has, add a comment to the existing issue instead of opening a new one.
99+
- Perform a [search](https://github.com/starkware-libs/cairo-vm/issues) to see if the enhancement has already been suggested. If it has, add a comment to the existing issue instead of opening a new one.
100100
- Find out whether your idea fits with the scope and aims of the project. It's up to you to make a strong case to convince the project's developers of the merits of this feature. Keep in mind that we want features that will be useful to the majority of our users and not just a small subset. If you're just targeting a minority of users, consider writing an add-on/plugin library.
101101

102102

103103
#### How Do I Submit a Good Enhancement Suggestion?
104104

105-
Enhancement suggestions are tracked as [GitHub issues](https://github.com/lambdaclass/cairo-vm/issues).
105+
Enhancement suggestions are tracked as [GitHub issues](https://github.com/starkware-libs/cairo-vm/issues).
106106

107107
- Use a **clear and descriptive title** for the issue to identify the suggestion.
108108
- Provide a **step-by-step description of the suggested enhancement** in as many details as possible.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ resolver = "2"
1717
version = "3.2.0"
1818
edition = "2021"
1919
license = "Apache-2.0"
20-
repository = "https://github.com/lambdaclass/cairo-vm/"
20+
repository = "https://github.com/starkware-libs/cairo-vm/"
2121
readme = "README.md"
2222
keywords = ["starknet", "cairo", "vm"]
2323

README.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77

88
A faster and safer implementation of the Cairo VM in Rust
99

10-
[Report Bug](https://github.com/lambdaclass/cairo-vm/issues/new?assignees=&labels=bug&projects=&template=bug_report.md&title=bug%3A+) · [Request Feature](https://github.com/lambdaclass/cairo-vm/issues/new?labels=enhancement&title=feat%3A+)
10+
[Report Bug](https://github.com/starkware-libs/cairo-vm/issues/new?assignees=&labels=bug&projects=&template=bug_report.md&title=bug%3A+) · [Request Feature](https://github.com/starkware-libs/cairo-vm/issues/new?labels=enhancement&title=feat%3A+)
1111

12-
[![rust](https://github.com/lambdaclass/cairo-vm/actions/workflows/rust.yml/badge.svg)](https://github.com/lambdaclass/cairo-vm/actions/workflows/rust.yml)
13-
[![codecov](https://img.shields.io/codecov/c/github/lambdaclass/cairo-vm)](https://codecov.io/gh/lambdaclass/cairo-vm)
14-
[![license](https://img.shields.io/github/license/lambdaclass/cairo-vm)](/LICENSE)
12+
[![rust](https://github.com/starkware-libs/cairo-vm/actions/workflows/rust.yml/badge.svg)](https://github.com/starkware-libs/cairo-vm/actions/workflows/rust.yml)
13+
[![codecov](https://img.shields.io/codecov/c/github/starkware-libs/cairo-vm)](https://codecov.io/gh/starkware-libs/cairo-vm)
14+
[![license](https://img.shields.io/github/license/starkware-libs/cairo-vm)](/LICENSE)
1515
[![pr-welcome]](#-contributing)
1616
[![Telegram Chat][tg-badge]][tg-url]
1717

@@ -150,7 +150,7 @@ The flag `--layout` determines which builtins can be used. More info about layou
150150
To sum up, the following code will get you from zero to running a Cairo program:
151151

152152
```bash
153-
git clone https://github.com/lambdaclass/cairo-vm.git
153+
git clone https://github.com/starkware-libs/cairo-vm.git
154154

155155
cd cairo-vm
156156

@@ -213,7 +213,7 @@ Currently, as this VM is under construction, it's missing some of the features o
213213
There are two ways to use non-standard hints in this VM:
214214

215215
- Extend the cairo-vm code and build your own binary using the interface [HintProcessor](docs/hint_processor/README.md).
216-
- Use [cairo-vm-py](https://github.com/lambdaclass/cairo-vm-py) which supports running any hint in a Python interpreter.
216+
217217

218218
### Running a function in a Cairo program with arguments
219219

@@ -229,7 +229,8 @@ When running a Cairo program directly using the Cairo-vm repository you would fi
229229
2. Instantiate the VM, the cairo_runner, the hint processor, and the entrypoint
230230

231231
```rust
232-
let mut cairo_runner = CairoRunner::new(&program, LayoutName::all_cairo, false, false);
232+
let mut cairo_runner =
233+
CairoRunner::new(&program, CairoLayout::new(LayoutName::all_cairo, None)?, false, false);
233234

234235
let mut hint_processor = BuiltinHintProcessor::new_empty();
235236

@@ -286,7 +287,7 @@ Running a [Cairo program](./cairo_programs/benchmarks/big_fibonacci.cairo) that
286287
- [Flamegraph](./docs/benchmarks/flamegraph.svg)
287288
- Github action [results](https://lambdaclass.github.io/cairo-vm/)
288289

289-
Note before running the benchmark suite: the benchmark named [iai_benchmark](https://github.com/lambdaclass/cairo-vm/blob/8dba86dbec935fa04a255e2edf3d5d184950fa22/Cargo.toml#L59) depends on Valgrind. Please make sure it is installed prior to running the `iai_benchmark` benchmark.
290+
Note before running the benchmark suite: the benchmark named [iai_benchmark](https://github.com/starkware-libs/cairo-vm/blob/8dba86dbec935fa04a255e2edf3d5d184950fa22/Cargo.toml#L59) depends on Valgrind. Please make sure it is installed prior to running the `iai_benchmark` benchmark.
290291

291292
Run the complete benchmark suite with cargo:
292293

@@ -335,7 +336,7 @@ You can find more detailed instructions in the [CONTRIBUTING.md](CONTRIBUTING.md
335336
## 🌞 Related Projects
336337

337338
- [starknet_in_rust](https://github.com/lambdaclass/starknet_in_rust): implementation of Starknet in Rust, powered by the cairo-vm.
338-
- [cairo-vm-py](https://github.com/lambdaclass/cairo-vm-py): Bindings for using cairo-vm from Python code.
339+
339340

340341
## 📚 Documentation
341342

RELEASE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
- The versions must be the same.
1515
- You need to update the workspace dependency `cairo-vm`, which
1616
you can find in the root cargo manifest under the section `[workspace.dependencies]`.
17-
- [Here](https://github.com/lambdaclass/cairo-vm/pull/1748/files) is an
17+
- [Here](https://github.com/starkware-libs/cairo-vm/pull/1748/files) is an
1818
example pull request with these changes.
1919
- [ ] Run `cargo update` and `git add Cargo.lock`
2020
- [ ] Update `CHANGELOG.md`:

bench/criterion_benchmark.rs

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use cairo_vm::{
2+
cairo_run::Cairo0RunConfig,
23
types::{layout_name::LayoutName, program::Program},
34
vm::runners::cairo_runner::CairoRunner,
45
};
@@ -26,17 +27,20 @@ fn build_many_runners(c: &mut Criterion) {
2627
let program = Program::from_bytes(program.as_slice(), Some("main")).unwrap();
2728
c.bench_function("build runner", |b| {
2829
b.iter_with_large_drop(|| {
29-
_ = black_box(
30-
CairoRunner::new(
31-
black_box(&program),
32-
black_box(LayoutName::starknet_with_keccak),
33-
black_box(None),
34-
black_box(false),
35-
black_box(false),
36-
black_box(false),
37-
)
38-
.unwrap(),
39-
);
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+
));
4044
})
4145
});
4246
}
@@ -50,13 +54,16 @@ fn load_program_data(c: &mut Criterion) {
5054
|| {
5155
CairoRunner::new(
5256
&program,
53-
LayoutName::starknet_with_keccak,
54-
None,
55-
false,
56-
false,
57-
false,
57+
&Cairo0RunConfig {
58+
layout: LayoutName::starknet_with_keccak,
59+
proof_mode: false,
60+
trace_enabled: false,
61+
disable_trace_padding: false,
62+
..Default::default()
63+
}
64+
.run_config()
65+
.unwrap(),
5866
)
59-
.unwrap()
6067
},
6168
|mut runner| _ = black_box(runner.initialize(false).unwrap()),
6269
BatchSize::SmallInput,

bench/iai_benchmark.rs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use cairo_vm::cairo_run::Cairo0RunConfig;
12
use core::hint::black_box;
23
use iai_callgrind::library_benchmark;
34
use iai_callgrind::library_benchmark_group;
@@ -36,13 +37,16 @@ fn build_runner() {
3637
let program = parse_program_helper();
3738
let runner = CairoRunner::new(
3839
black_box(&program),
39-
LayoutName::starknet_with_keccak,
40-
None,
41-
false,
42-
false,
43-
false,
44-
)
45-
.unwrap();
40+
&Cairo0RunConfig {
41+
layout: LayoutName::starknet_with_keccak,
42+
proof_mode: false,
43+
trace_enabled: false,
44+
disable_trace_padding: false,
45+
..Default::default()
46+
}
47+
.run_config()
48+
.unwrap(),
49+
);
4650
core::mem::drop(black_box(runner));
4751
}
4852

@@ -54,13 +58,16 @@ fn build_runner_helper() -> CairoRunner {
5458
let program = Program::from_bytes(program.as_slice(), Some("main")).unwrap();
5559
CairoRunner::new(
5660
&program,
57-
LayoutName::starknet_with_keccak,
58-
None,
59-
false,
60-
false,
61-
false,
61+
&Cairo0RunConfig {
62+
layout: LayoutName::starknet_with_keccak,
63+
proof_mode: false,
64+
trace_enabled: false,
65+
disable_trace_padding: false,
66+
..Default::default()
67+
}
68+
.run_config()
69+
.unwrap(),
6270
)
63-
.unwrap()
6471
}
6572

6673
#[library_benchmark]

cairo-vm-cli/src/main.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use cairo_vm::vm::runners::cairo_pie::CairoPie;
1515
#[cfg(feature = "with_tracer")]
1616
use cairo_vm::vm::runners::cairo_runner::CairoRunner;
1717
use cairo_vm::vm::runners::cairo_runner::RunResources;
18+
use cairo_vm::vm::vm_core::DEFAULT_MAX_TRACEBACK_ENTRIES;
1819
#[cfg(feature = "with_tracer")]
1920
use cairo_vm_tracer::error::trace_data_errors::TraceDataError;
2021
#[cfg(feature = "with_tracer")]
@@ -145,7 +146,7 @@ fn run(args: impl Iterator<Item = String>) -> Result<(), Error> {
145146
None => None,
146147
};
147148

148-
let cairo_run_config = cairo_run::CairoRunConfig {
149+
let cairo_run_config = cairo_run::Cairo0RunConfig {
149150
entrypoint: &args.entrypoint,
150151
trace_enabled,
151152
relocate_mem: args.memory_file.is_some() || args.air_public_input.is_some(),
@@ -157,6 +158,7 @@ fn run(args: impl Iterator<Item = String>) -> Result<(), Error> {
157158
allow_missing_builtins: args.allow_missing_builtins,
158159
dynamic_layout_params: cairo_layout_params,
159160
disable_trace_padding: false,
161+
max_traceback_entries: DEFAULT_MAX_TRACEBACK_ENTRIES,
160162
};
161163

162164
let mut cairo_runner = match if args.run_from_cairo_pie {
@@ -269,8 +271,8 @@ mod tests {
269271
use super::*;
270272
use assert_matches::assert_matches;
271273
use cairo_vm::{
272-
hint_processor::hint_processor_definition::HintProcessor, types::program::Program,
273-
vm::runners::cairo_runner::CairoRunner,
274+
cairo_run::Cairo0RunConfig, hint_processor::hint_processor_definition::HintProcessor,
275+
types::program::Program, vm::runners::cairo_runner::CairoRunner,
274276
};
275277
use rstest::rstest;
276278

@@ -280,8 +282,18 @@ mod tests {
280282
hint_processor: &mut dyn HintProcessor,
281283
) -> Result<CairoRunner, CairoRunError> {
282284
let program = Program::from_bytes(program_content, Some("main")).unwrap();
283-
let mut cairo_runner =
284-
CairoRunner::new(&program, LayoutName::all_cairo, None, false, true, false).unwrap();
285+
let mut cairo_runner = CairoRunner::new(
286+
&program,
287+
&Cairo0RunConfig {
288+
layout: LayoutName::all_cairo,
289+
proof_mode: false,
290+
trace_enabled: true,
291+
disable_trace_padding: false,
292+
..Default::default()
293+
}
294+
.run_config()
295+
.unwrap(),
296+
);
285297
let end = cairo_runner
286298
.initialize(false)
287299
.map_err(CairoRunError::Runner)?;

cairo1-run/src/cairo_run.rs

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,19 @@ use cairo_lang_sierra_type_size::get_type_size_map;
3434
use cairo_lang_utils::{
3535
bigint::BigIntAsHex, casts::IntoOrPanic, unordered_hash_map::UnorderedHashMap,
3636
};
37+
use cairo_vm::vm::{runners::cairo_runner::CairoRunConfig, vm_core::DEFAULT_MAX_TRACEBACK_ENTRIES};
3738
use std::{collections::HashMap, iter::Peekable};
3839

3940
use cairo_vm::{
4041
hint_processor::cairo_1_hint_processor::hint_processor::Cairo1HintProcessor,
4142
math_utils::signed_felt,
4243
serde::deserialize_program::{ApTracking, FlowTrackingData, HintParams, ReferenceManager},
4344
types::{
44-
builtin_name::BuiltinName, layout::CairoLayoutParams, layout_name::LayoutName,
45-
program::Program, relocatable::MaybeRelocatable,
45+
builtin_name::BuiltinName,
46+
layout::{CairoLayout, CairoLayoutParams},
47+
layout_name::LayoutName,
48+
program::Program,
49+
relocatable::MaybeRelocatable,
4650
},
4751
vm::{
4852
errors::{runner_errors::RunnerError, vm_errors::VirtualMachineError},
@@ -101,6 +105,15 @@ pub struct Cairo1RunConfig<'a> {
101105
pub finalize_builtins: bool,
102106
/// Appends the return and input values to the output segment. This is performed by default when running in proof_mode
103107
pub append_return_values: bool,
108+
/// Disable padding of the trace.
109+
/// By default, the trace is padded to accommodate the expected builtins-n_steps relationships
110+
/// according to the layout.
111+
/// When the padding is disabled:
112+
/// - It doesn't modify/pad n_steps.
113+
/// - It still pads each builtin segment to the next power of 2 (w.r.t the number of used
114+
/// instances of the builtin) compared to their sizes at the end of the execution.
115+
pub disable_trace_padding: bool,
116+
pub max_traceback_entries: u32,
104117
}
105118

106119
impl Default for Cairo1RunConfig<'_> {
@@ -115,6 +128,8 @@ impl Default for Cairo1RunConfig<'_> {
115128
finalize_builtins: false,
116129
append_return_values: false,
117130
dynamic_layout_params: None,
131+
disable_trace_padding: false,
132+
max_traceback_entries: DEFAULT_MAX_TRACEBACK_ENTRIES,
118133
}
119134
}
120135
}
@@ -125,6 +140,21 @@ impl Cairo1RunConfig<'_> {
125140
fn copy_to_output(&self) -> bool {
126141
self.append_return_values || self.proof_mode
127142
}
143+
144+
pub fn run_config(&self) -> Result<CairoRunConfig, RunnerError> {
145+
let runner_mode = if self.proof_mode {
146+
RunnerMode::ProofModeCairo1
147+
} else {
148+
RunnerMode::ExecutionMode
149+
};
150+
CairoRunConfig::new(
151+
CairoLayout::new(self.layout, self.dynamic_layout_params.clone())?,
152+
self.trace_enabled,
153+
self.disable_trace_padding,
154+
runner_mode,
155+
self.max_traceback_entries,
156+
)
157+
}
128158
}
129159

130160
/// Runs a Cairo 1 program
@@ -250,20 +280,7 @@ pub fn cairo_run_program(
250280
)?
251281
};
252282

253-
let runner_mode = if cairo_run_config.proof_mode {
254-
RunnerMode::ProofModeCairo1
255-
} else {
256-
RunnerMode::ExecutionMode
257-
};
258-
259-
let mut runner = CairoRunner::new_v2(
260-
&program,
261-
cairo_run_config.layout,
262-
cairo_run_config.dynamic_layout_params.clone(),
263-
runner_mode,
264-
cairo_run_config.trace_enabled,
265-
false,
266-
)?;
283+
let mut runner = CairoRunner::new(&program, &cairo_run_config.run_config()?);
267284
let end = runner.initialize(cairo_run_config.proof_mode)?;
268285
load_arguments(&mut runner, &cairo_run_config, main_func)?;
269286

cairo1-run/src/main.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use cairo_lang_compiler::{
55
};
66
use cairo_vm::cairo_run::{write_encoded_memory, write_encoded_trace};
77
use cairo_vm::types::layout::CairoLayoutParams;
8+
use cairo_vm::vm::vm_core::DEFAULT_MAX_TRACEBACK_ENTRIES;
89
use cairo_vm::{
910
air_public_input::PublicInputError, types::layout_name::LayoutName,
1011
vm::errors::trace_errors::TraceError, Felt252,
@@ -143,6 +144,8 @@ fn run(args: impl Iterator<Item = String>) -> Result<Option<String>, Error> {
143144
finalize_builtins: args.air_public_input.is_some() || args.cairo_pie_output.is_some(),
144145
append_return_values: args.append_return_values,
145146
dynamic_layout_params: cairo_layout_params,
147+
disable_trace_padding: false,
148+
max_traceback_entries: DEFAULT_MAX_TRACEBACK_ENTRIES,
146149
};
147150

148151
// Try to parse the file as a sierra program

0 commit comments

Comments
 (0)