Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dynamic layout basic support #1824

Merged
merged 76 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from 71 commits
Commits
Show all changes
76 commits
Select commit Hold shift + click to select a range
7d14e74
Fix Zero segment location.
Alon-Ti Jun 24, 2024
e71b052
Fixed has_zero_segment naming
yuvalsw Aug 13, 2024
0ec972a
Fix prover input.
Alon-Ti Jun 30, 2024
8c5c924
Fixed version reading when no version is supplied
yuvalsw Jul 7, 2024
057bc6f
Added change to changelog.
yuvalsw Aug 13, 2024
64c844b
fix test_from_serializable()
OmriEshhar1 Aug 21, 2024
51d25a7
fix panic_impl error
OmriEshhar1 Aug 25, 2024
88d1da1
fix cairo version
OmriEshhar1 Aug 26, 2024
c5d3bd5
Add dummy changelog
JulianGCalderon Aug 26, 2024
ea377a8
Pin wasm-bindgen
JulianGCalderon Aug 26, 2024
e8e7bb3
Register change in CHANGELOG
JulianGCalderon Aug 26, 2024
87a736a
Update Cargo.lock
JulianGCalderon Aug 26, 2024
2fe7f90
Remove changes from CHANGELOG
JulianGCalderon Aug 26, 2024
6c7237e
Add argument parsing for layout params file
JulianGCalderon Aug 23, 2024
e4b162b
Add dynamic support (no implement)
JulianGCalderon Aug 23, 2024
43360b9
Add cairo_layout_params_file.example.json
JulianGCalderon Aug 23, 2024
e3e31b8
Implement dynamic layout creation
JulianGCalderon Aug 23, 2024
ad97bd0
Update CHANGELOG
JulianGCalderon Aug 23, 2024
11edca3
Add cli dynamic support for cairo 1
JulianGCalderon Aug 23, 2024
adbba88
Make wasm compatible
JulianGCalderon Aug 23, 2024
8ac5b5b
Merge remote-tracking branch 'lambdaclass-cairo-vm/main' into yuval/f…
OmriEshhar1 Aug 28, 2024
a41f231
Use public_memory_fraction = 4 vy default
JulianGCalderon Aug 28, 2024
f7334f5
Deserialize bool from int
JulianGCalderon Aug 28, 2024
63eba32
Add comparison with python-vm (failing)
JulianGCalderon Aug 28, 2024
3d18e47
Merge remote-tracking branch 'lambdaclass-cairo-vm/main' into yuval/f…
OmriEshhar1 Aug 29, 2024
1f2098a
Rebuild .rs files in makefile
JulianGCalderon Aug 29, 2024
707e0d2
Use 8 as dynamic public_memory_fraction
JulianGCalderon Aug 29, 2024
723eaf2
Use None ratio for dynamic unused builtins
JulianGCalderon Aug 29, 2024
f20b97b
Add rangecheck96 to private inputs
JulianGCalderon Aug 29, 2024
e6d3f9b
Make dyn py files depend on params_file
JulianGCalderon Aug 29, 2024
3f5eef5
Use cpu_component_step=1 by default
JulianGCalderon Aug 29, 2024
026e459
Fix typo in private inputs
JulianGCalderon Aug 29, 2024
15b7742
Add range check value to air private input test
JulianGCalderon Aug 30, 2024
698b35c
Fix zero segment location
JulianGCalderon Aug 30, 2024
c58dc4e
Use zero builtin instead of None
JulianGCalderon Aug 30, 2024
ce985ba
Add debug scripts
JulianGCalderon Aug 30, 2024
6ea8ddf
Merge branch 'main' into dynamic-layout
JulianGCalderon Sep 2, 2024
08874bd
Remove dup makefile recipes
JulianGCalderon Sep 2, 2024
ba4d3c0
remove outdated test
JulianGCalderon Sep 2, 2024
0e0c65b
Enable ensure-no_std on test
JulianGCalderon Sep 2, 2024
0b9fed5
Fix tests
JulianGCalderon Sep 2, 2024
737850f
Add correct test
JulianGCalderon Sep 2, 2024
34e5eaf
Rename tset
JulianGCalderon Sep 3, 2024
2a4213f
Add comment
JulianGCalderon Sep 3, 2024
28eb15c
Add debugging document
JulianGCalderon Sep 3, 2024
20ce2aa
Update cairo layout params file
JulianGCalderon Sep 3, 2024
0036504
Merge branch 'fix-for-sw-fix-version' into dynamic-layout
JulianGCalderon Sep 3, 2024
30f7766
Merge branch 'main' into dynamic-layout
JulianGCalderon Sep 3, 2024
af9fadd
Remove duplicated range check
JulianGCalderon Sep 3, 2024
fbf074d
Remove dup
JulianGCalderon Sep 3, 2024
f8a7f78
Merge branch 'main' into dynamic-layout
JulianGCalderon Sep 4, 2024
f94ae2b
Merge branch 'main' into dynamic-layout
JulianGCalderon Sep 17, 2024
050c71a
Remove debugging and scrippts (moveed to another branch)
JulianGCalderon Sep 17, 2024
3f9e684
Add comment
JulianGCalderon Sep 17, 2024
fca3cbf
Add tests
JulianGCalderon Sep 18, 2024
eb63b5c
Add dynamic test to cairo-vm-cli
JulianGCalderon Sep 18, 2024
a6f1511
Add parse test
JulianGCalderon Sep 18, 2024
f2c0abb
Remove compare all dynamic
JulianGCalderon Sep 18, 2024
5b6868e
Add script for comparing with dynamic layouts
JulianGCalderon Sep 18, 2024
cdab848
Add tests to workflow
JulianGCalderon Sep 18, 2024
2015055
Delete logic changes
JulianGCalderon Sep 18, 2024
c46ed76
Delete more logic changes
JulianGCalderon Sep 18, 2024
7a44d4e
Update rust.yml
JulianGCalderon Sep 19, 2024
859b7fb
Rename compare_outputs_dynamic_layout.sh to compare_outputs_dynamic_l…
JulianGCalderon Sep 19, 2024
47e441b
Update test script
JulianGCalderon Sep 19, 2024
9bbfc36
Merge branch 'main' into dynamic-layout
JulianGCalderon Sep 23, 2024
60c5679
Add more tests
JulianGCalderon Sep 25, 2024
1a2067a
Rename parameter for clarity
JulianGCalderon Sep 25, 2024
a20a2ab
Enable mod builtin only on feature with dynamic layout
JulianGCalderon Sep 25, 2024
7bb8dd3
Remove debug assert
JulianGCalderon Sep 25, 2024
ae0f943
Refactor errors into variants
JulianGCalderon Sep 25, 2024
4bcdf82
Fix failing test
JulianGCalderon Sep 27, 2024
7dcbbfa
Move cairo_layout_params_file to test folder
JulianGCalderon Sep 27, 2024
c59117a
Document cairo_layout_param_file in the README.md
JulianGCalderon Sep 27, 2024
2aab550
Fix clippy warning
JulianGCalderon Sep 27, 2024
37d83fd
Use mod_builtin feature in tests
JulianGCalderon Sep 27, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,45 @@ jobs:
- name: Run script
run: ./vm/src/tests/compare_factorial_outputs_all_layouts.sh

compare-outputs-dynamic-layouts:
name: Compare outputs with dynamic layouts
needs: [ build-programs, build-release ]
runs-on: ubuntu-22.04
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Python3 Build
uses: actions/setup-python@v4
with:
python-version: '3.9'
cache: 'pip'

- name: Install cairo-lang and deps
run: pip install -r requirements.txt

- name: Fetch release binary
uses: actions/cache/restore@v3
with:
key: cli-bin-rel-${{ github.sha }}
path: target/release/cairo-vm-cli
fail-on-cache-miss: true

- uses: actions/download-artifact@master
with:
name: proof_programs
path: cairo_programs/proof_programs/

- name: Fetch programs
uses: actions/cache/restore@v3
with:
path: ${{ env.CAIRO_PROGRAMS_PATH }}
key: cairo_proof_programs-cache-${{ hashFiles('cairo_programs/**/*.cairo', 'examples/wasm-demo/src/array_sum.cairo') }}
fail-on-cache-miss: true

- name: Run script
run: ./vm/src/tests/compare_outputs_dynamic_layouts.sh

compare-run-from-cairo-pie-all-outputs:
name: Compare all outputs from running Cairo PIEs
needs: [ build-programs, build-release, run-cairo-release ]
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ cairo-vm-cli/air_input.pub
ensure-no_std/Cargo.lock
cairo_programs/proof_programs/*.cairo

!cairo_layout_params_file.json
!vm/src/tests/cairo_pie_test_output.json
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

#### Upcoming Changes

* feat(BREAKING): [#1824](https://github.com/lambdaclass/cairo-vm/pull/1824):
* Add support for dynamic layout
* CLI change(BREAKING): The flag `cairo_layout_params_file` must be specified when using dynamic layout.
* Signature change(BREAKING): Both `CairoRunner::new` and `CairoRunner::new_v2` now receive an `Option<CairoLayoutParams>`, used only with dynamic layout.

* chore: bump pip `cairo-lang` 0.13.2 [#1827](https://github.com/lambdaclass/cairo-vm/pull/1827)

* chore: bump `cairo-lang-` dependencies to 2.8.0 [#1833](https://github.com/lambdaclass/cairo-vm/pull/1833/files)
Expand Down
12 changes: 11 additions & 1 deletion bench/criterion_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ fn build_many_runners(c: &mut Criterion) {
CairoRunner::new(
black_box(&program),
black_box(LayoutName::starknet_with_keccak),
black_box(None),
black_box(false),
black_box(false),
)
Expand All @@ -45,7 +46,16 @@ fn load_program_data(c: &mut Criterion) {
let program = Program::from_bytes(program.as_slice(), Some("main")).unwrap();
c.bench_function("initialize", |b| {
b.iter_batched(
|| CairoRunner::new(&program, LayoutName::starknet_with_keccak, false, false).unwrap(),
|| {
CairoRunner::new(
&program,
LayoutName::starknet_with_keccak,
None,
false,
false,
)
.unwrap()
},
|mut runner| _ = black_box(runner.initialize(false).unwrap()),
BatchSize::SmallInput,
)
Expand Down
10 changes: 9 additions & 1 deletion bench/iai_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ fn build_runner() {
let runner = CairoRunner::new(
black_box(&program),
LayoutName::starknet_with_keccak,
None,
false,
false,
)
Expand All @@ -47,7 +48,14 @@ fn build_runner_helper() -> CairoRunner {
//Picked the biggest one at the time of writing
let program = include_bytes!("../cairo_programs/benchmarks/keccak_integration_benchmark.json");
let program = Program::from_bytes(program.as_slice(), Some("main")).unwrap();
CairoRunner::new(&program, LayoutName::starknet_with_keccak, false, false).unwrap()
CairoRunner::new(
&program,
LayoutName::starknet_with_keccak,
None,
false,
false,
)
.unwrap()
}

#[inline(never)]
Expand Down
25 changes: 25 additions & 0 deletions cairo-vm-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use cairo_vm::cairo_run::{self, EncodeTraceError};
use cairo_vm::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::BuiltinHintProcessor;
#[cfg(feature = "with_tracer")]
use cairo_vm::serde::deserialize_program::DebugInfo;
use cairo_vm::types::layout::CairoLayoutParams;
use cairo_vm::types::layout_name::LayoutName;
use cairo_vm::vm::errors::cairo_run_errors::CairoRunError;
use cairo_vm::vm::errors::trace_errors::TraceError;
Expand Down Expand Up @@ -43,8 +44,13 @@ struct Args {
entrypoint: String,
#[structopt(long = "memory_file")]
memory_file: Option<PathBuf>,
/// When using dynamic layout, it's parameters must be specified through a layout params file.
#[clap(long = "layout", default_value = "plain", value_enum)]
layout: LayoutName,
/// Required when using with dynamic layout.
/// Ignored otherwise.
#[clap(long = "cairo_layout_params_file", required_if_eq("layout", "dynamic"))]
cairo_layout_params_file: Option<PathBuf>,
#[structopt(long = "proof_mode")]
proof_mode: bool,
#[structopt(long = "secure_run")]
Expand Down Expand Up @@ -162,6 +168,11 @@ fn run(args: impl Iterator<Item = String>) -> Result<(), Error> {

let trace_enabled = args.trace_file.is_some() || args.air_public_input.is_some();

let cairo_layout_params = match args.cairo_layout_params_file {
Some(file) => Some(CairoLayoutParams::from_file(&file)?),
None => None,
};

let cairo_run_config = cairo_run::CairoRunConfig {
entrypoint: &args.entrypoint,
trace_enabled,
Expand All @@ -170,6 +181,7 @@ fn run(args: impl Iterator<Item = String>) -> Result<(), Error> {
proof_mode: args.proof_mode,
secure_run: args.secure_run,
allow_missing_builtins: args.allow_missing_builtins,
dynamic_layout_params: cairo_layout_params,
..Default::default()
};

Expand Down Expand Up @@ -405,6 +417,19 @@ mod tests {
assert_matches!(run(args), Err(Error::Runner(_)));
}

#[test]
fn test_run_dynamic_params() {
let mut args = vec!["cairo-vm-cli".to_string()];
args.extend_from_slice(&["--layout".to_string(), "dynamic".to_string()]);
args.extend_from_slice(&[
"--cairo_layout_params_file".to_string(),
"../cairo_layout_params_file.json".to_string(),
]);
args.push("../cairo_programs/proof_programs/fibonacci.json".to_string());

assert_matches!(run(args.into_iter()), Ok(_));
}

//Since the functionality here is trivial, I just call the function
//to fool Codecov.
#[test]
Expand Down
7 changes: 5 additions & 2 deletions cairo1-run/src/cairo_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ use cairo_vm::{
math_utils::signed_felt,
serde::deserialize_program::{ApTracking, FlowTrackingData, HintParams, ReferenceManager},
types::{
builtin_name::BuiltinName, layout_name::LayoutName, program::Program,
relocatable::MaybeRelocatable,
builtin_name::BuiltinName, layout::CairoLayoutParams, layout_name::LayoutName,
program::Program, relocatable::MaybeRelocatable,
},
vm::{
errors::{runner_errors::RunnerError, vm_errors::VirtualMachineError},
Expand Down Expand Up @@ -86,6 +86,7 @@ pub struct Cairo1RunConfig<'a> {
pub relocate_mem: bool,
/// Cairo layout chosen for the run
pub layout: LayoutName,
pub dynamic_layout_params: Option<CairoLayoutParams>,
/// Run in proof_mode
pub proof_mode: bool,
/// Should be true if either air_public_input or cairo_pie_output are needed
Expand All @@ -106,6 +107,7 @@ impl Default for Cairo1RunConfig<'_> {
proof_mode: false,
finalize_builtins: false,
append_return_values: false,
dynamic_layout_params: None,
}
}
}
Expand Down Expand Up @@ -248,6 +250,7 @@ pub fn cairo_run_program(
let mut runner = CairoRunner::new_v2(
&program,
cairo_run_config.layout,
cairo_run_config.dynamic_layout_params.clone(),
runner_mode,
cairo_run_config.trace_enabled,
)?;
Expand Down
25 changes: 25 additions & 0 deletions cairo1-run/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use cairo1_run::{cairo_run_program, Cairo1RunConfig, FuncArg};
use cairo_lang_compiler::{
compile_prepared_db, db::RootDatabase, project::setup_project, CompilerConfig,
};
use cairo_vm::types::layout::CairoLayoutParams;
use cairo_vm::{
air_public_input::PublicInputError, types::layout_name::LayoutName,
vm::errors::trace_errors::TraceError, Felt252,
Expand All @@ -24,8 +25,13 @@ struct Args {
trace_file: Option<PathBuf>,
#[structopt(long = "memory_file")]
memory_file: Option<PathBuf>,
/// When using dynamic layout, it's parameters must be specified through a layout params file.
#[clap(long = "layout", default_value = "plain", value_enum)]
layout: LayoutName,
/// Required when using with dynamic layout.
/// Ignored otherwise.
#[clap(long = "cairo_layout_params_file", required_if_eq("layout", "dynamic"))]
cairo_layout_params_file: Option<PathBuf>,
#[clap(long = "proof_mode", value_parser)]
proof_mode: bool,
#[clap(long = "air_public_input", requires = "proof_mode")]
Expand Down Expand Up @@ -153,6 +159,11 @@ fn run(args: impl Iterator<Item = String>) -> Result<Option<String>, Error> {
args.args = process_args(&std::fs::read_to_string(filename)?).unwrap();
}

let cairo_layout_params = match args.cairo_layout_params_file {
Some(file) => Some(CairoLayoutParams::from_file(&file)?),
None => None,
};

let cairo_run_config = Cairo1RunConfig {
proof_mode: args.proof_mode,
serialize_output: args.print_output,
Expand All @@ -162,6 +173,7 @@ fn run(args: impl Iterator<Item = String>) -> Result<Option<String>, Error> {
args: &args.args.0,
finalize_builtins: args.air_public_input.is_some() || args.cairo_pie_output.is_some(),
append_return_values: args.append_return_values,
dynamic_layout_params: cairo_layout_params,
};

// Try to parse the file as a sierra program
Expand Down Expand Up @@ -478,6 +490,19 @@ mod tests {
assert_matches!(run(args), Ok(Some(res)) if res == expected_output, "Program {} failed with flags {}", program, extra_flags.concat());
}

#[test]
fn test_run_dynamic_params() {
let mut args = vec!["cairo1-run".to_string()];
args.extend_from_slice(&["--layout".to_string(), "dynamic".to_string()]);
args.extend_from_slice(&[
"--cairo_layout_params_file".to_string(),
"../cairo_layout_params_file.json".to_string(),
]);
args.push("../cairo_programs/cairo-1-programs/fibonacci.cairo".to_string());

assert_matches!(run(args.into_iter()), Ok(_));
}

// these tests are separated so as to run them without --append_return_values and --proof_mode options
// since they require to use the squashed version of felt252
#[rstest]
Expand Down
29 changes: 29 additions & 0 deletions cairo_layout_params_file.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
fmoletta marked this conversation as resolved.
Show resolved Hide resolved
"rc_units": 4,
"log_diluted_units_per_step": 4,
"cpu_component_step": 8,
"memory_units_per_step": 8,
"uses_pedersen_builtin": true,
"pedersen_ratio": 256,
"uses_range_check_builtin": true,
"range_check_ratio": 8,
"uses_ecdsa_builtin": true,
"ecdsa_ratio": 2048,
"uses_bitwise_builtin": true,
"bitwise_ratio": 16,
"uses_ec_op_builtin": true,
"ec_op_ratio": 1024,
"uses_keccak_builtin": true,
"keccak_ratio": 2048,
"uses_poseidon_builtin": true,
"poseidon_ratio": 256,
"uses_range_check96_builtin": true,
"range_check96_ratio": 8,
"range_check96_ratio_den": 1,
"uses_add_mod_builtin": true,
"add_mod_ratio": 128,
"add_mod_ratio_den": 1,
"uses_mul_mod_builtin": true,
"mul_mod_ratio": 256,
"mul_mod_ratio_den": 1
}
2 changes: 1 addition & 1 deletion fuzzer/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 11 additions & 1 deletion vm/src/cairo_run.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use crate::{
hint_processor::hint_processor_definition::HintProcessor,
types::{builtin_name::BuiltinName, layout_name::LayoutName, program::Program},
types::{
builtin_name::BuiltinName, layout::CairoLayoutParams, layout_name::LayoutName,
program::Program,
},
vm::{
errors::{
cairo_run_errors::CairoRunError, runner_errors::RunnerError, vm_exception::VmException,
Expand All @@ -26,6 +29,9 @@ pub struct CairoRunConfig<'a> {
pub trace_enabled: bool,
pub relocate_mem: bool,
pub layout: LayoutName,
/// The `dynamic_layout_params` argument should only be used with dynamic layout.
/// It is ignored otherwise.
pub dynamic_layout_params: Option<CairoLayoutParams>,
pub proof_mode: bool,
pub secure_run: Option<bool>,
pub disable_trace_padding: bool,
Expand All @@ -43,6 +49,7 @@ impl<'a> Default for CairoRunConfig<'a> {
secure_run: None,
disable_trace_padding: false,
allow_missing_builtins: None,
dynamic_layout_params: None,
}
}
}
Expand All @@ -65,6 +72,7 @@ pub fn cairo_run_program_with_initial_scope(
let mut cairo_runner = CairoRunner::new(
program,
cairo_run_config.layout,
cairo_run_config.dynamic_layout_params.clone(),
cairo_run_config.proof_mode,
cairo_run_config.trace_enabled,
)?;
Expand Down Expand Up @@ -151,6 +159,7 @@ pub fn cairo_run_pie(
let mut cairo_runner = CairoRunner::new(
&program,
cairo_run_config.layout,
cairo_run_config.dynamic_layout_params.clone(),
false,
cairo_run_config.trace_enabled,
)?;
Expand Down Expand Up @@ -223,6 +232,7 @@ pub fn cairo_run_fuzzed_program(
let mut cairo_runner = CairoRunner::new(
&program,
cairo_run_config.layout,
cairo_run_config.dynamic_layout_params.clone(),
cairo_run_config.proof_mode,
cairo_run_config.trace_enabled,
)?;
Expand Down
1 change: 1 addition & 0 deletions vm/src/tests/cairo_run_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1182,6 +1182,7 @@ fn run_program_with_custom_mod_builtin_params(
let mut cairo_runner = CairoRunner::new(
&program,
cairo_run_config.layout,
cairo_run_config.dynamic_layout_params,
cairo_run_config.proof_mode,
cairo_run_config.trace_enabled,
)
Expand Down
Loading
Loading