From 7d14e749cef97703b927b85ae41255601ee0ae3b Mon Sep 17 00:00:00 2001 From: Alon Titelman Date: Mon, 24 Jun 2024 16:40:22 +0300 Subject: [PATCH 01/68] Fix Zero segment location. --- vm/src/cairo_run.rs | 7 ++++++- vm/src/vm/runners/builtin_runner/modulo.rs | 5 ++++- vm/src/vm/runners/cairo_runner.rs | 5 +++++ vm/src/vm/vm_memory/memory_segments.rs | 6 +++++- 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/vm/src/cairo_run.rs b/vm/src/cairo_run.rs index b7d5d005e3..37e233b947 100644 --- a/vm/src/cairo_run.rs +++ b/vm/src/cairo_run.rs @@ -173,7 +173,12 @@ pub fn cairo_run_pie( } } // Load previous execution memory - let n_extra_segments = pie.metadata.extra_segments.len(); + let has_zero_segment = if cairo_runner.vm.segments.has_zero_segment() { + 0 + } else { + 1 + }; + let n_extra_segments = pie.metadata.extra_segments.len() - has_zero_segment; cairo_runner .vm .segments diff --git a/vm/src/vm/runners/builtin_runner/modulo.rs b/vm/src/vm/runners/builtin_runner/modulo.rs index c5d2dc56b5..e7e7d848cb 100644 --- a/vm/src/vm/runners/builtin_runner/modulo.rs +++ b/vm/src/vm/runners/builtin_runner/modulo.rs @@ -118,7 +118,10 @@ impl ModBuiltinRunner { pub fn initialize_segments(&mut self, segments: &mut MemorySegmentManager) { self.base = segments.add().segment_index as usize; // segments.add() always returns a positive index - self.zero_segment_index = segments.add_zero_segment(self.zero_segment_size) + } + + pub fn initialize_zero_segment(&mut self, segments: &mut MemorySegmentManager) { + self.zero_segment_index = segments.add_zero_segment(self.zero_segment_size); } pub fn initial_stack(&self) -> Vec { diff --git a/vm/src/vm/runners/cairo_runner.rs b/vm/src/vm/runners/cairo_runner.rs index 4c42ad463d..ecf47892b0 100644 --- a/vm/src/vm/runners/cairo_runner.rs +++ b/vm/src/vm/runners/cairo_runner.rs @@ -234,6 +234,11 @@ impl CairoRunner { self.initialize_builtins(allow_missing_builtins)?; self.initialize_segments(None); let end = self.initialize_main_entrypoint()?; + for builtin_runner in self.vm.builtin_runners.iter_mut() { + if let BuiltinRunner::Mod(runner) = builtin_runner { + runner.initialize_zero_segment(&mut self.vm.segments); + } + } self.initialize_vm()?; Ok(end) } diff --git a/vm/src/vm/vm_memory/memory_segments.rs b/vm/src/vm/vm_memory/memory_segments.rs index 04a0a58f18..6b716eb0c6 100644 --- a/vm/src/vm/vm_memory/memory_segments.rs +++ b/vm/src/vm/vm_memory/memory_segments.rs @@ -278,11 +278,15 @@ impl MemorySegmentManager { .insert(segment_index, public_memory.cloned().unwrap_or_default()); } + pub fn has_zero_segment(&self) -> bool { + self.zero_segment_index.is_zero() + } + // Creates the zero segment if it wasn't previously created // Fills the segment with the value 0 until size is reached // Returns the index of the zero segment pub(crate) fn add_zero_segment(&mut self, size: usize) -> usize { - if self.zero_segment_index.is_zero() { + if self.has_zero_segment() { self.zero_segment_index = self.add().segment_index as usize; } From e71b05222f78bbe0c4b432de27da9b8fc95f84a5 Mon Sep 17 00:00:00 2001 From: Yuval Goldberg Date: Tue, 13 Aug 2024 08:21:36 +0300 Subject: [PATCH 02/68] Fixed has_zero_segment naming --- vm/src/cairo_run.rs | 6 +----- vm/src/vm/vm_memory/memory_segments.rs | 8 ++++---- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/vm/src/cairo_run.rs b/vm/src/cairo_run.rs index 37e233b947..de2540fb2e 100644 --- a/vm/src/cairo_run.rs +++ b/vm/src/cairo_run.rs @@ -173,11 +173,7 @@ pub fn cairo_run_pie( } } // Load previous execution memory - let has_zero_segment = if cairo_runner.vm.segments.has_zero_segment() { - 0 - } else { - 1 - }; + let has_zero_segment = cairo_runner.vm.segments.has_zero_segment() as usize; let n_extra_segments = pie.metadata.extra_segments.len() - has_zero_segment; cairo_runner .vm diff --git a/vm/src/vm/vm_memory/memory_segments.rs b/vm/src/vm/vm_memory/memory_segments.rs index 6b716eb0c6..b6d7379786 100644 --- a/vm/src/vm/vm_memory/memory_segments.rs +++ b/vm/src/vm/vm_memory/memory_segments.rs @@ -217,7 +217,7 @@ impl MemorySegmentManager { } let accessed_amount = // Instead of marking the values in the zero segment until zero_segment_size as accessed we use zero_segment_size as accessed_amount - if !self.zero_segment_index.is_zero() && i == self.zero_segment_index { + if self.has_zero_segment() && i == self.zero_segment_index { self.zero_segment_size } else { match self.memory.get_amount_of_accessed_addresses_for_segment(i) { @@ -279,14 +279,14 @@ impl MemorySegmentManager { } pub fn has_zero_segment(&self) -> bool { - self.zero_segment_index.is_zero() + !self.zero_segment_index.is_zero() } // Creates the zero segment if it wasn't previously created // Fills the segment with the value 0 until size is reached // Returns the index of the zero segment pub(crate) fn add_zero_segment(&mut self, size: usize) -> usize { - if self.has_zero_segment() { + if !self.has_zero_segment() { self.zero_segment_index = self.add().segment_index as usize; } @@ -302,7 +302,7 @@ impl MemorySegmentManager { // Finalizes the zero segment and clears it's tracking data from the manager pub(crate) fn finalize_zero_segment(&mut self) { - if !self.zero_segment_index.is_zero() { + if self.has_zero_segment() { self.finalize(Some(self.zero_segment_size), self.zero_segment_index, None); self.zero_segment_index = 0; self.zero_segment_size = 0; From 0ec972a83745a144158775a25252f95fc145816f Mon Sep 17 00:00:00 2001 From: Alon Titelman Date: Mon, 1 Jul 2024 01:06:19 +0300 Subject: [PATCH 03/68] Fix prover input. --- vm/src/air_private_input.rs | 3 +++ vm/src/vm/runners/builtin_runner/signature.rs | 1 - 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/vm/src/air_private_input.rs b/vm/src/air_private_input.rs index fa05cc86d5..b30b05e98a 100644 --- a/vm/src/air_private_input.rs +++ b/vm/src/air_private_input.rs @@ -19,6 +19,8 @@ pub struct AirPrivateInputSerializable { #[serde(skip_serializing_if = "Option::is_none")] range_check: Option>, #[serde(skip_serializing_if = "Option::is_none")] + range_check96: Option>, + #[serde(skip_serializing_if = "Option::is_none")] ecdsa: Option>, #[serde(skip_serializing_if = "Option::is_none")] bitwise: Option>, @@ -157,6 +159,7 @@ impl AirPrivateInput { memory_path, pedersen: self.0.get(&BuiltinName::pedersen).cloned(), range_check: self.0.get(&BuiltinName::range_check).cloned(), + range_check96: self.0.get(&BuiltinName::range_check96).cloned(), ecdsa: self.0.get(&BuiltinName::ecdsa).cloned(), bitwise: self.0.get(&BuiltinName::bitwise).cloned(), ec_op: self.0.get(&BuiltinName::ec_op).cloned(), diff --git a/vm/src/vm/runners/builtin_runner/signature.rs b/vm/src/vm/runners/builtin_runner/signature.rs index f81f77181b..6e94206ef3 100644 --- a/vm/src/vm/runners/builtin_runner/signature.rs +++ b/vm/src/vm/runners/builtin_runner/signature.rs @@ -220,7 +220,6 @@ impl SignatureBuiltinRunner { private_inputs.push(PrivateInput::Signature(PrivateInputSignature { index: addr .offset - .saturating_sub(self.base) .checked_div(CELLS_PER_SIGNATURE as usize) .unwrap_or_default(), pubkey: *pubkey, From 8c5c9244940116773f4ba42c4f53135d4af51c23 Mon Sep 17 00:00:00 2001 From: Yuval Goldberg Date: Sun, 7 Jul 2024 17:43:38 +0300 Subject: [PATCH 04/68] Fixed version reading when no version is supplied --- vm/src/vm/runners/cairo_pie.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/vm/src/vm/runners/cairo_pie.rs b/vm/src/vm/runners/cairo_pie.rs index 371b6de82f..83f215b02f 100644 --- a/vm/src/vm/runners/cairo_pie.rs +++ b/vm/src/vm/runners/cairo_pie.rs @@ -322,8 +322,13 @@ impl CairoPie { ) -> Result { use std::io::Read; - let reader = std::io::BufReader::new(zip_reader.by_name("version.json")?); - let version: CairoPieVersion = serde_json::from_reader(reader)?; + let version = match zip_reader.by_name("version.json") { + Ok(version_buffer) => { + let reader = std::io::BufReader::new(version_buffer); + serde_json::from_reader(reader)? + } + Err(_) => CairoPieVersion { cairo_pie: () }, + }; let reader = std::io::BufReader::new(zip_reader.by_name("metadata.json")?); let metadata: CairoPieMetadata = serde_json::from_reader(reader)?; From 057bc6f5663d31e226d49b7cb7029996d32729be Mon Sep 17 00:00:00 2001 From: Yuval Goldberg Date: Tue, 13 Aug 2024 08:33:57 +0300 Subject: [PATCH 05/68] Added change to changelog. --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 45b9f6ab2b..be59397c42 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ #### Upcoming Changes +* fix: Added the following VM fixes: [#1820](https://github.com/lambdaclass/cairo-vm/pull/1820) + * Fix zero segment location. + * Fix has_zero_segment naming. + * Fix prover input. + * Fix version reading when no version is supplied. + #### [1.0.1] - 2024-08-12 * fix(BREAKING): [#1818](https://github.com/lambdaclass/cairo-vm/pull/1818): From 64c844b561a3805cec1dcd570d61d3f63a359e78 Mon Sep 17 00:00:00 2001 From: Omri Eshhar Date: Wed, 21 Aug 2024 09:13:53 +0300 Subject: [PATCH 06/68] fix test_from_serializable() --- vm/src/air_private_input.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/vm/src/air_private_input.rs b/vm/src/air_private_input.rs index b30b05e98a..6275ebd6a1 100644 --- a/vm/src/air_private_input.rs +++ b/vm/src/air_private_input.rs @@ -233,6 +233,10 @@ mod tests { index: 10000, value: Felt252::from(8000), })]), + range_check96: Some(vec![PrivateInput::Value(PrivateInputValue { + index: 10000, + value: Felt252::from(8000), + })]), ecdsa: Some(vec![PrivateInput::Signature(PrivateInputSignature { index: 0, pubkey: Felt252::from(123), From 51d25a7865db3bc680485701eed1865c92fe8231 Mon Sep 17 00:00:00 2001 From: Omri Eshhar Date: Sun, 25 Aug 2024 11:48:52 +0300 Subject: [PATCH 07/68] fix panic_impl error --- ensure-no_std/src/main.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/ensure-no_std/src/main.rs b/ensure-no_std/src/main.rs index b25cce251d..bc3e032cd5 100644 --- a/ensure-no_std/src/main.rs +++ b/ensure-no_std/src/main.rs @@ -4,6 +4,7 @@ use core::panic::PanicInfo; /// This function is called on panic. +#[cfg(not(test))] #[panic_handler] fn panic(_info: &PanicInfo) -> ! { loop {} From 88d1da19e1480f874b2db74eaa25a0d11fc667a6 Mon Sep 17 00:00:00 2001 From: Omri Eshhar Date: Mon, 26 Aug 2024 15:59:12 +0300 Subject: [PATCH 08/68] fix cairo version --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index a58845926c..2519096f45 100644 --- a/Makefile +++ b/Makefile @@ -180,7 +180,7 @@ $(CAIRO_2_CONTRACTS_TEST_DIR)/%.casm: $(CAIRO_2_CONTRACTS_TEST_DIR)/%.sierra # ====================== CAIRO_2_REPO_DIR = cairo2 -CAIRO_2_VERSION = 2.5.4 +CAIRO_2_VERSION = 2.7.0 build-cairo-2-compiler-macos: @if [ ! -d "$(CAIRO_2_REPO_DIR)" ]; then \ From c5d3bd5a9fe2f9b6e8763ca71cc46368361ae377 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Mon, 26 Aug 2024 11:51:24 -0300 Subject: [PATCH 09/68] Add dummy changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 45b9f6ab2b..898476cae5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ #### Upcoming Changes +* dummy change + #### [1.0.1] - 2024-08-12 * fix(BREAKING): [#1818](https://github.com/lambdaclass/cairo-vm/pull/1818): From ea377a82ab398c4815b3a91b18053221fb13c5b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Mon, 26 Aug 2024 12:34:23 -0300 Subject: [PATCH 10/68] Pin wasm-bindgen --- vm/Cargo.toml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/vm/Cargo.toml b/vm/Cargo.toml index b5ee76dc53..6311f1b562 100644 --- a/vm/Cargo.toml +++ b/vm/Cargo.toml @@ -78,6 +78,10 @@ arbitrary = { workspace = true, features = ["derive"], optional = true } # Used to derive clap traits for CLIs clap = { version = "4.3.10", features = ["derive"], optional = true} +# Pin wasm-bindgen version to fix ensure-no_std CI workflow +# It's not used directly +wasm-bindgen = { version = "= 0.2.92" } + [dev-dependencies] assert_matches = "1.5.0" rstest = { version = "0.17.0", default-features = false } From e8e7bb348a284a142432c43464927ba88fd36e6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Mon, 26 Aug 2024 12:39:13 -0300 Subject: [PATCH 11/68] Register change in CHANGELOG --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 898476cae5..5eba45cdb0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,8 @@ #### Upcoming Changes -* dummy change +* Fix CI: [#1825](https://github.com/lambdaclass/cairo-vm/pull/1825): + * Pins wasm-bindgen version to fix CI #### [1.0.1] - 2024-08-12 From 87a736af5b116dba5204db790b0c9ab5c7bbef88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Mon, 26 Aug 2024 12:57:04 -0300 Subject: [PATCH 12/68] Update Cargo.lock --- Cargo.lock | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.lock b/Cargo.lock index a4d940f903..09adbfee16 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -968,6 +968,7 @@ dependencies = [ "starknet-crypto", "starknet-types-core", "thiserror-no-std", + "wasm-bindgen", "wasm-bindgen-test", "zip", ] From 2fe7f90ac74933d894888cd848355359034448c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Mon, 26 Aug 2024 14:48:24 -0300 Subject: [PATCH 13/68] Remove changes from CHANGELOG --- CHANGELOG.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5eba45cdb0..45b9f6ab2b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,9 +2,6 @@ #### Upcoming Changes -* Fix CI: [#1825](https://github.com/lambdaclass/cairo-vm/pull/1825): - * Pins wasm-bindgen version to fix CI - #### [1.0.1] - 2024-08-12 * fix(BREAKING): [#1818](https://github.com/lambdaclass/cairo-vm/pull/1818): From 6c7237ef31b69224f7c649ff5734c7bb7eb449af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Fri, 23 Aug 2024 13:05:16 -0300 Subject: [PATCH 14/68] Add argument parsing for layout params file --- cairo-vm-cli/src/main.rs | 5 +++++ vm/src/types/layout_name.rs | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/cairo-vm-cli/src/main.rs b/cairo-vm-cli/src/main.rs index 08095128ca..51caac41e0 100644 --- a/cairo-vm-cli/src/main.rs +++ b/cairo-vm-cli/src/main.rs @@ -43,8 +43,13 @@ struct Args { entrypoint: String, #[structopt(long = "memory_file")] memory_file: Option, + /// 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, #[structopt(long = "proof_mode")] proof_mode: bool, #[structopt(long = "secure_run")] diff --git a/vm/src/types/layout_name.rs b/vm/src/types/layout_name.rs index 8cfd792dc0..4082743f42 100644 --- a/vm/src/types/layout_name.rs +++ b/vm/src/types/layout_name.rs @@ -36,7 +36,7 @@ impl LayoutName { LayoutName::recursive_with_poseidon => "recursive_with_poseidon", LayoutName::all_solidity => "all_solidity", LayoutName::all_cairo => "all_cairo", - LayoutName::dynamic => "all_cairo", + LayoutName::dynamic => "dynamic", } } } From e4b162b9f3618220386c3657bd1f36b0cc58d129 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Fri, 23 Aug 2024 15:51:00 -0300 Subject: [PATCH 15/68] Add dynamic support (no implement) --- bench/criterion_benchmark.rs | 12 ++++- bench/iai_benchmark.rs | 10 +++- cairo-vm-cli/src/main.rs | 1 + cairo1-run/src/cairo_run.rs | 1 + vm/src/cairo_run.rs | 7 +++ vm/src/tests/cairo_run_test.rs | 1 + vm/src/tests/mod.rs | 2 + vm/src/types/layout.rs | 55 +++++++++++++++++----- vm/src/utils.rs | 9 +++- vm/src/vm/errors/runner_errors.rs | 2 + vm/src/vm/runners/builtin_runner/modulo.rs | 3 +- vm/src/vm/runners/cairo_runner.rs | 30 +++++++++++- 12 files changed, 114 insertions(+), 19 deletions(-) diff --git a/bench/criterion_benchmark.rs b/bench/criterion_benchmark.rs index 7078ad21a8..d473fe61f6 100644 --- a/bench/criterion_benchmark.rs +++ b/bench/criterion_benchmark.rs @@ -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), ) @@ -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, ) diff --git a/bench/iai_benchmark.rs b/bench/iai_benchmark.rs index b5bff69dce..441f79b5bc 100644 --- a/bench/iai_benchmark.rs +++ b/bench/iai_benchmark.rs @@ -34,6 +34,7 @@ fn build_runner() { let runner = CairoRunner::new( black_box(&program), LayoutName::starknet_with_keccak, + None, false, false, ) @@ -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)] diff --git a/cairo-vm-cli/src/main.rs b/cairo-vm-cli/src/main.rs index 51caac41e0..e43e06c748 100644 --- a/cairo-vm-cli/src/main.rs +++ b/cairo-vm-cli/src/main.rs @@ -175,6 +175,7 @@ fn run(args: impl Iterator) -> Result<(), Error> { proof_mode: args.proof_mode, secure_run: args.secure_run, allow_missing_builtins: args.allow_missing_builtins, + cairo_layout_params_file: args.cairo_layout_params_file, ..Default::default() }; diff --git a/cairo1-run/src/cairo_run.rs b/cairo1-run/src/cairo_run.rs index 700225b880..0bd7445758 100644 --- a/cairo1-run/src/cairo_run.rs +++ b/cairo1-run/src/cairo_run.rs @@ -248,6 +248,7 @@ pub fn cairo_run_program( let mut runner = CairoRunner::new_v2( &program, cairo_run_config.layout, + None, runner_mode, cairo_run_config.trace_enabled, )?; diff --git a/vm/src/cairo_run.rs b/vm/src/cairo_run.rs index b7d5d005e3..806e0b08bd 100644 --- a/vm/src/cairo_run.rs +++ b/vm/src/cairo_run.rs @@ -1,3 +1,5 @@ +use std::path::PathBuf; + use crate::{ hint_processor::hint_processor_definition::HintProcessor, types::{builtin_name::BuiltinName, layout_name::LayoutName, program::Program}, @@ -26,6 +28,7 @@ pub struct CairoRunConfig<'a> { pub trace_enabled: bool, pub relocate_mem: bool, pub layout: LayoutName, + pub cairo_layout_params_file: Option, pub proof_mode: bool, pub secure_run: Option, pub disable_trace_padding: bool, @@ -43,6 +46,7 @@ impl<'a> Default for CairoRunConfig<'a> { secure_run: None, disable_trace_padding: false, allow_missing_builtins: None, + cairo_layout_params_file: None, } } } @@ -65,6 +69,7 @@ pub fn cairo_run_program_with_initial_scope( let mut cairo_runner = CairoRunner::new( program, cairo_run_config.layout, + cairo_run_config.cairo_layout_params_file.clone(), cairo_run_config.proof_mode, cairo_run_config.trace_enabled, )?; @@ -151,6 +156,7 @@ pub fn cairo_run_pie( let mut cairo_runner = CairoRunner::new( &program, cairo_run_config.layout, + cairo_run_config.cairo_layout_params_file.clone(), false, cairo_run_config.trace_enabled, )?; @@ -222,6 +228,7 @@ pub fn cairo_run_fuzzed_program( let mut cairo_runner = CairoRunner::new( &program, cairo_run_config.layout, + cairo_run_config.cairo_layout_params_file.clone(), cairo_run_config.proof_mode, cairo_run_config.trace_enabled, )?; diff --git a/vm/src/tests/cairo_run_test.rs b/vm/src/tests/cairo_run_test.rs index ce42ca31dd..741955a16a 100644 --- a/vm/src/tests/cairo_run_test.rs +++ b/vm/src/tests/cairo_run_test.rs @@ -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.cairo_layout_params_file, cairo_run_config.proof_mode, cairo_run_config.trace_enabled, ) diff --git a/vm/src/tests/mod.rs b/vm/src/tests/mod.rs index 11026af5ad..675bc59aea 100644 --- a/vm/src/tests/mod.rs +++ b/vm/src/tests/mod.rs @@ -113,6 +113,7 @@ fn run_cairo_1_entrypoint( let mut runner = CairoRunner::new( &(contract_class.clone().try_into().unwrap()), LayoutName::all_cairo, + None, false, false, ) @@ -217,6 +218,7 @@ fn run_cairo_1_entrypoint_with_run_resources( let mut runner = CairoRunner::new( &(contract_class.clone().try_into().unwrap()), LayoutName::all_cairo, + None, false, false, ) diff --git a/vm/src/types/layout.rs b/vm/src/types/layout.rs index ee0fae3196..9caecdf4a0 100644 --- a/vm/src/types/layout.rs +++ b/vm/src/types/layout.rs @@ -1,3 +1,5 @@ +use std::{fs::File, io, path::Path}; + use crate::types::layout_name::LayoutName; use super::instance_definitions::{ @@ -6,7 +8,7 @@ use super::instance_definitions::{ pub(crate) const MEMORY_UNITS_PER_STEP: u32 = 8; -use serde::Serialize; +use serde::{Deserialize, Serialize}; #[derive(Serialize, Debug)] pub struct CairoLayout { @@ -117,7 +119,7 @@ impl CairoLayout { } } - pub(crate) fn dynamic_instance() -> CairoLayout { + pub(crate) fn dynamic_instance(_params: CairoLayoutParams) -> CairoLayout { CairoLayout { name: LayoutName::dynamic, rc_units: 16, @@ -126,6 +128,44 @@ impl CairoLayout { diluted_pool_instance_def: Some(DilutedPoolInstanceDef::default()), } } + + pub(crate) fn dynamic_instance_from_file(params_file: &Path) -> io::Result { + let params_file = File::open(params_file)?; + let params: CairoLayoutParams = serde_json::from_reader(params_file)?; + + Ok(Self::dynamic_instance(params)) + } +} + +#[derive(Deserialize, Debug)] +pub struct CairoLayoutParams { + pub rc_units: u32, + pub log_diluted_units_per_step: u32, + pub cpu_component_step: u32, + pub memory_units_per_step: u32, + pub uses_pedersen_builtin: bool, + pub pedersen_ratio: u32, + pub uses_range_check_builtin: bool, + pub range_check_ratio: u32, + pub uses_ecdsa_builtin: bool, + pub ecdsa_ratio: u32, + pub uses_bitwise_builtin: bool, + pub bitwise_ratio: u32, + pub uses_ec_op_builtin: bool, + pub ec_op_ratio: u32, + pub uses_keccak_builtin: bool, + pub keccak_ratio: u32, + pub uses_poseidon_builtin: bool, + pub poseidon_ratio: u32, + pub uses_range_check96_builtin: bool, + pub range_check96_ratio: u32, + pub range_check96_ratio_den: u32, + pub uses_add_mod_builtin: bool, + pub add_mod_ratio: u32, + pub add_mod_ratio_den: u32, + pub uses_mul_mod_builtin: bool, + pub mul_mod_ratio: u32, + pub mul_mod_ratio_den: u32, } #[cfg(test)] @@ -257,15 +297,6 @@ mod tests { #[test] fn get_dynamic_instance() { - let layout = CairoLayout::dynamic_instance(); - let builtins = BuiltinsInstanceDef::dynamic(); - assert_eq!(layout.name, LayoutName::dynamic); - assert_eq!(layout.rc_units, 16); - assert_eq!(layout.builtins, builtins); - assert_eq!(layout.public_memory_fraction, 8); - assert_eq!( - layout.diluted_pool_instance_def, - Some(DilutedPoolInstanceDef::default()) - ); + let _params: CairoLayoutParams = todo!(); } } diff --git a/vm/src/utils.rs b/vm/src/utils.rs index 070efcaf6d..760c5201ad 100644 --- a/vm/src/utils.rs +++ b/vm/src/utils.rs @@ -252,19 +252,23 @@ pub mod test_utils { crate::vm::runners::cairo_runner::CairoRunner::new( &$program, crate::types::layout_name::LayoutName::all_cairo, + None, false, false, ) .unwrap() }; ($program:expr, $layout:expr) => { - crate::vm::runners::cairo_runner::CairoRunner::new(&$program, $layout, false, false) - .unwrap() + crate::vm::runners::cairo_runner::CairoRunner::new( + &$program, $layout, None, false, false, + ) + .unwrap() }; ($program:expr, $layout:expr, $proof_mode:expr) => { crate::vm::runners::cairo_runner::CairoRunner::new( &$program, $layout, + None, $proof_mode, false, ) @@ -274,6 +278,7 @@ pub mod test_utils { crate::vm::runners::cairo_runner::CairoRunner::new( &$program, $layout, + None, $proof_mode, $trace_enabled, ) diff --git a/vm/src/vm/errors/runner_errors.rs b/vm/src/vm/errors/runner_errors.rs index 561f89997e..b0edf339f3 100644 --- a/vm/src/vm/errors/runner_errors.rs +++ b/vm/src/vm/errors/runner_errors.rs @@ -132,6 +132,8 @@ pub enum RunnerError { CairoPieProofMode, #[error("{0}: Invalid additional data")] InvalidAdditionalData(BuiltinName), + #[error("Bad dynamic layout params: {0}")] + BadDynamicLayoutParams(String), } #[cfg(test)] diff --git a/vm/src/vm/runners/builtin_runner/modulo.rs b/vm/src/vm/runners/builtin_runner/modulo.rs index c5d2dc56b5..554e8912da 100644 --- a/vm/src/vm/runners/builtin_runner/modulo.rs +++ b/vm/src/vm/runners/builtin_runner/modulo.rs @@ -697,7 +697,8 @@ mod tests { let mut hint_processor = BuiltinHintProcessor::new_empty(); let program = Program::from_bytes(program_data, Some("main")).unwrap(); - let mut runner = CairoRunner::new(&program, LayoutName::all_cairo, true, false).unwrap(); + let mut runner = + CairoRunner::new(&program, LayoutName::all_cairo, None, true, false).unwrap(); let end = runner.initialize(false).unwrap(); // Modify add_mod & mul_mod params diff --git a/vm/src/vm/runners/cairo_runner.rs b/vm/src/vm/runners/cairo_runner.rs index 4c42ad463d..77683e5523 100644 --- a/vm/src/vm/runners/cairo_runner.rs +++ b/vm/src/vm/runners/cairo_runner.rs @@ -1,3 +1,5 @@ +use std::path::PathBuf; + use crate::{ air_private_input::AirPrivateInput, air_public_input::{PublicInput, PublicInputError}, @@ -168,9 +170,12 @@ pub enum RunnerMode { } impl CairoRunner { + /// The `cairo_layout_params_file` argument should only be used with dynamic layout. + /// It is ignored otherwise. pub fn new_v2( program: &Program, layout: LayoutName, + cairo_layout_params_file: Option, mode: RunnerMode, trace_enabled: bool, ) -> Result { @@ -185,7 +190,20 @@ impl CairoRunner { LayoutName::recursive_with_poseidon => CairoLayout::recursive_with_poseidon(), LayoutName::all_cairo => CairoLayout::all_cairo_instance(), LayoutName::all_solidity => CairoLayout::all_solidity_instance(), - LayoutName::dynamic => CairoLayout::dynamic_instance(), + LayoutName::dynamic => { + debug_assert!( + cairo_layout_params_file.is_some(), + "cairo layout params is missing with dynamic layout" + ); + + let params_file = + cairo_layout_params_file.ok_or(RunnerError::BadDynamicLayoutParams( + "cairo layout param file is missing".to_string(), + ))?; + + CairoLayout::dynamic_instance_from_file(¶ms_file) + .map_err(|err| RunnerError::BadDynamicLayoutParams(err.to_string()))? + } }; Ok(CairoRunner { program: program.clone(), @@ -215,6 +233,7 @@ impl CairoRunner { pub fn new( program: &Program, layout: LayoutName, + cairo_layout_params_file: Option, proof_mode: bool, trace_enabled: bool, ) -> Result { @@ -222,11 +241,18 @@ impl CairoRunner { Self::new_v2( program, layout, + cairo_layout_params_file, RunnerMode::ProofModeCanonical, trace_enabled, ) } else { - Self::new_v2(program, layout, RunnerMode::ExecutionMode, trace_enabled) + Self::new_v2( + program, + layout, + cairo_layout_params_file, + RunnerMode::ExecutionMode, + trace_enabled, + ) } } From 43360b91233bdd8fd83beb39c93944eae0bd6937 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Fri, 23 Aug 2024 16:16:10 -0300 Subject: [PATCH 16/68] Add cairo_layout_params_file.example.json --- .gitignore | 1 + cairo_layout_params_file.example.json | 29 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 cairo_layout_params_file.example.json diff --git a/.gitignore b/.gitignore index 69a905dfdf..83e14d8942 100644 --- a/.gitignore +++ b/.gitignore @@ -26,4 +26,5 @@ cairo-vm-cli/air_input.pub ensure-no_std/Cargo.lock cairo_programs/proof_programs/*.cairo +!cairo_layout_params_file.example.json !vm/src/tests/cairo_pie_test_output.json diff --git a/cairo_layout_params_file.example.json b/cairo_layout_params_file.example.json new file mode 100644 index 0000000000..2e1a9ba54b --- /dev/null +++ b/cairo_layout_params_file.example.json @@ -0,0 +1,29 @@ +{ + "rc_units": 16, + "log_diluted_units_per_step": 1, + "cpu_component_step": 1, + "memory_units_per_step": 8, + "uses_pedersen_builtin": true, + "pedersen_ratio": 16, + "uses_range_check_builtin": true, + "range_check_ratio": 16, + "uses_ecdsa_builtin": true, + "ecdsa_ratio": 16, + "uses_bitwise_builtin": true, + "bitwise_ratio": 16, + "uses_ec_op_builtin": true, + "ec_op_ratio": 16, + "uses_keccak_builtin": true, + "keccak_ratio": 16, + "uses_poseidon_builtin": true, + "poseidon_ratio": 16, + "uses_range_check96_builtin": true, + "range_check96_ratio": 16, + "range_check96_ratio_den": 1, + "uses_add_mod_builtin": true, + "add_mod_ratio": 16, + "add_mod_ratio_den": 1, + "uses_mul_mod_builtin": true, + "mul_mod_ratio": 16, + "mul_mod_ratio_den": 1 +} From e3e31b8e3eca2010ff702a56f4600e5cfba41fb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Fri, 23 Aug 2024 17:45:05 -0300 Subject: [PATCH 17/68] Implement dynamic layout creation --- .../builtins_instance_def.rs | 118 +++++++++++++----- vm/src/types/layout.rs | 108 ++++++++++++++-- 2 files changed, 186 insertions(+), 40 deletions(-) diff --git a/vm/src/types/instance_definitions/builtins_instance_def.rs b/vm/src/types/instance_definitions/builtins_instance_def.rs index ab13cf14fb..a028c42486 100644 --- a/vm/src/types/instance_definitions/builtins_instance_def.rs +++ b/vm/src/types/instance_definitions/builtins_instance_def.rs @@ -1,3 +1,5 @@ +use crate::types::layout::CairoLayoutParams; + use super::mod_instance_def::ModInstanceDef; use super::{ bitwise_instance_def::BitwiseInstanceDef, ec_op_instance_def::EcOpInstanceDef, @@ -192,25 +194,94 @@ impl BuiltinsInstanceDef { } } - pub(crate) fn dynamic() -> BuiltinsInstanceDef { + pub(crate) fn dynamic(params: CairoLayoutParams) -> BuiltinsInstanceDef { + let pedersen = if params.uses_pedersen_builtin { + Some(PedersenInstanceDef { + ratio: Some(params.pedersen_ratio), + }) + } else { + None + }; + let range_check = if params.uses_range_check_builtin { + Some(RangeCheckInstanceDef { + ratio: Some(params.range_check_ratio), + }) + } else { + None + }; + let ecdsa = if params.uses_ecdsa_builtin { + Some(EcdsaInstanceDef { + ratio: Some(params.ecdsa_ratio), + }) + } else { + None + }; + let bitwise = if params.uses_bitwise_builtin { + Some(BitwiseInstanceDef { + ratio: Some(params.bitwise_ratio), + }) + } else { + None + }; + let ec_op = if params.uses_ec_op_builtin { + Some(EcOpInstanceDef { + ratio: Some(params.ec_op_ratio), + }) + } else { + None + }; + let keccak = if params.uses_keccak_builtin { + Some(KeccakInstanceDef { + ratio: Some(params.keccak_ratio), + }) + } else { + None + }; + let poseidon = if params.uses_poseidon_builtin { + Some(PoseidonInstanceDef { + ratio: Some(params.poseidon_ratio), + }) + } else { + None + }; + let range_check96 = if params.uses_range_check96_builtin { + Some(RangeCheckInstanceDef { + ratio: Some(params.range_check96_ratio), + }) + } else { + None + }; + let add_mod = if params.uses_add_mod_builtin { + Some(ModInstanceDef { + ratio: Some(params.add_mod_ratio), + word_bit_len: 1, + batch_size: 96, + }) + } else { + None + }; + let mul_mod = if params.uses_mul_mod_builtin { + Some(ModInstanceDef { + ratio: Some(params.mul_mod_ratio), + word_bit_len: 1, + batch_size: 96, + }) + } else { + None + }; + BuiltinsInstanceDef { output: true, - pedersen: Some(PedersenInstanceDef::new(None)), - range_check: Some(RangeCheckInstanceDef::new(None)), - ecdsa: Some(EcdsaInstanceDef::new(None)), - bitwise: Some(BitwiseInstanceDef::new(None)), - ec_op: Some(EcOpInstanceDef::new(None)), - keccak: None, - poseidon: None, - range_check96: None, - #[cfg(feature = "mod_builtin")] - add_mod: Some(ModInstanceDef::new(None, 1, 96)), - #[cfg(feature = "mod_builtin")] - mul_mod: Some(ModInstanceDef::new(None, 1, 96)), - #[cfg(not(feature = "mod_builtin"))] - add_mod: None, - #[cfg(not(feature = "mod_builtin"))] - mul_mod: None, + pedersen, + range_check, + ecdsa, + bitwise, + ec_op, + keccak, + poseidon, + range_check96, + add_mod, + mul_mod, } } } @@ -342,17 +413,4 @@ mod tests { assert!(builtins.keccak.is_none()); assert!(builtins.poseidon.is_none()); } - - #[test] - fn get_builtins_dynamic() { - let builtins = BuiltinsInstanceDef::dynamic(); - assert!(builtins.output); - assert!(builtins.pedersen.is_some()); - assert!(builtins.range_check.is_some()); - assert!(builtins.ecdsa.is_some()); - assert!(builtins.bitwise.is_some()); - assert!(builtins.ec_op.is_some()); - assert!(builtins.keccak.is_none()); - assert!(builtins.poseidon.is_none()); - } } diff --git a/vm/src/types/layout.rs b/vm/src/types/layout.rs index 9caecdf4a0..0d6209e2e7 100644 --- a/vm/src/types/layout.rs +++ b/vm/src/types/layout.rs @@ -119,13 +119,16 @@ impl CairoLayout { } } - pub(crate) fn dynamic_instance(_params: CairoLayoutParams) -> CairoLayout { + pub(crate) fn dynamic_instance(params: CairoLayoutParams) -> CairoLayout { CairoLayout { name: LayoutName::dynamic, - rc_units: 16, - builtins: BuiltinsInstanceDef::dynamic(), + rc_units: params.rc_units, public_memory_fraction: 8, - diluted_pool_instance_def: Some(DilutedPoolInstanceDef::default()), + diluted_pool_instance_def: Some(DilutedPoolInstanceDef { + units_per_step: 2_u32.pow(params.log_diluted_units_per_step), + ..DilutedPoolInstanceDef::default() + }), + builtins: BuiltinsInstanceDef::dynamic(params), } } @@ -137,12 +140,10 @@ impl CairoLayout { } } -#[derive(Deserialize, Debug)] +#[derive(Deserialize, Debug, Default)] pub struct CairoLayoutParams { pub rc_units: u32, pub log_diluted_units_per_step: u32, - pub cpu_component_step: u32, - pub memory_units_per_step: u32, pub uses_pedersen_builtin: bool, pub pedersen_ratio: u32, pub uses_range_check_builtin: bool, @@ -159,17 +160,27 @@ pub struct CairoLayoutParams { pub poseidon_ratio: u32, pub uses_range_check96_builtin: bool, pub range_check96_ratio: u32, - pub range_check96_ratio_den: u32, pub uses_add_mod_builtin: bool, pub add_mod_ratio: u32, - pub add_mod_ratio_den: u32, pub uses_mul_mod_builtin: bool, pub mul_mod_ratio: u32, + // the following are not used right now + pub cpu_component_step: u32, + pub memory_units_per_step: u32, + pub range_check96_ratio_den: u32, pub mul_mod_ratio_den: u32, + pub add_mod_ratio_den: u32, } #[cfg(test)] mod tests { + use crate::types::instance_definitions::{ + bitwise_instance_def::BitwiseInstanceDef, ec_op_instance_def::EcOpInstanceDef, + ecdsa_instance_def::EcdsaInstanceDef, keccak_instance_def::KeccakInstanceDef, + mod_instance_def::ModInstanceDef, pedersen_instance_def::PedersenInstanceDef, + range_check_instance_def::RangeCheckInstanceDef, + }; + use super::*; #[cfg(target_arch = "wasm32")] @@ -297,6 +308,83 @@ mod tests { #[test] fn get_dynamic_instance() { - let _params: CairoLayoutParams = todo!(); + // dummy cairo layout params + let params = CairoLayoutParams { + rc_units: 32, + log_diluted_units_per_step: 5, + uses_pedersen_builtin: true, + pedersen_ratio: 32, + uses_range_check_builtin: true, + range_check_ratio: 32, + uses_ecdsa_builtin: true, + ecdsa_ratio: 32, + uses_bitwise_builtin: true, + bitwise_ratio: 32, + uses_ec_op_builtin: true, + ec_op_ratio: 32, + uses_keccak_builtin: true, + keccak_ratio: 32, + uses_poseidon_builtin: false, + uses_range_check96_builtin: false, + uses_add_mod_builtin: false, + uses_mul_mod_builtin: true, + mul_mod_ratio: 32, + ..Default::default() // + // cpu_component_step: todo!(), + // memory_units_per_step: todo!(), + // range_check96_ratio_den: todo!(), + // add_mod_ratio_den: todo!(), + // mul_mod_ratio_den: todo!(), + }; + + let layout = CairoLayout::dynamic_instance(params); + + assert_eq!(layout.name, LayoutName::dynamic); + assert_eq!(layout.rc_units, 32); + assert_eq!(layout.public_memory_fraction, 8); // hardcoded + assert_eq!( + layout.diluted_pool_instance_def, + Some(DilutedPoolInstanceDef { + units_per_step: 32, + ..DilutedPoolInstanceDef::default() // hardcoded + }) + ); + + assert!(layout.builtins.output); + assert_eq!( + layout.builtins.pedersen, + Some(PedersenInstanceDef { ratio: Some(32) }) + ); + assert_eq!( + layout.builtins.range_check, + Some(RangeCheckInstanceDef { ratio: Some(32) }) + ); + assert_eq!( + layout.builtins.ecdsa, + Some(EcdsaInstanceDef { ratio: Some(32) }) + ); + assert_eq!( + layout.builtins.bitwise, + Some(BitwiseInstanceDef { ratio: Some(32) }) + ); + assert_eq!( + layout.builtins.ec_op, + Some(EcOpInstanceDef { ratio: Some(32) }) + ); + assert_eq!( + layout.builtins.keccak, + Some(KeccakInstanceDef { ratio: Some(32) }) + ); + assert_eq!(layout.builtins.poseidon, None,); + assert_eq!(layout.builtins.range_check96, None,); + assert_eq!(layout.builtins.add_mod, None); + assert_eq!( + layout.builtins.mul_mod, + Some(ModInstanceDef { + ratio: Some(32), + word_bit_len: 1, // hardcoded + batch_size: 96 // hardcoded + }) + ); } } From ad97bd07aaf30c2fd35267819ff42c8287ed44d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Fri, 23 Aug 2024 18:02:13 -0300 Subject: [PATCH 18/68] Update CHANGELOG --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 45b9f6ab2b..3de9d8dcb2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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`, used only with dynamic layout. + #### [1.0.1] - 2024-08-12 * fix(BREAKING): [#1818](https://github.com/lambdaclass/cairo-vm/pull/1818): From 11edca34ea136ead98b39c72bc14d5c8c8cb0b8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Fri, 23 Aug 2024 18:14:01 -0300 Subject: [PATCH 19/68] Add cli dynamic support for cairo 1 --- cairo1-run/src/cairo_run.rs | 6 ++++-- cairo1-run/src/main.rs | 6 ++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/cairo1-run/src/cairo_run.rs b/cairo1-run/src/cairo_run.rs index 0bd7445758..60bd02614f 100644 --- a/cairo1-run/src/cairo_run.rs +++ b/cairo1-run/src/cairo_run.rs @@ -51,7 +51,7 @@ use cairo_vm::{ use itertools::{chain, Itertools}; use num_bigint::{BigInt, Sign}; use num_traits::{cast::ToPrimitive, Zero}; -use std::{collections::HashMap, iter::Peekable}; +use std::{collections::HashMap, iter::Peekable, path::PathBuf}; /// Representation of a cairo argument /// Can consist of a single Felt or an array of Felts @@ -86,6 +86,7 @@ pub struct Cairo1RunConfig<'a> { pub relocate_mem: bool, /// Cairo layout chosen for the run pub layout: LayoutName, + pub cairo_layout_params_file: Option, /// Run in proof_mode pub proof_mode: bool, /// Should be true if either air_public_input or cairo_pie_output are needed @@ -106,6 +107,7 @@ impl Default for Cairo1RunConfig<'_> { proof_mode: false, finalize_builtins: false, append_return_values: false, + cairo_layout_params_file: None, } } } @@ -248,7 +250,7 @@ pub fn cairo_run_program( let mut runner = CairoRunner::new_v2( &program, cairo_run_config.layout, - None, + cairo_run_config.cairo_layout_params_file.clone(), runner_mode, cairo_run_config.trace_enabled, )?; diff --git a/cairo1-run/src/main.rs b/cairo1-run/src/main.rs index 8a4a5058bb..41587e8b92 100644 --- a/cairo1-run/src/main.rs +++ b/cairo1-run/src/main.rs @@ -24,8 +24,13 @@ struct Args { trace_file: Option, #[structopt(long = "memory_file")] memory_file: Option, + /// 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, #[clap(long = "proof_mode", value_parser)] proof_mode: bool, #[clap(long = "air_public_input", requires = "proof_mode")] @@ -162,6 +167,7 @@ fn run(args: impl Iterator) -> Result, 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, + cairo_layout_params_file: args.cairo_layout_params_file, }; // Try to parse the file as a sierra program From adbba88078169100da120d8d21ff61559169512f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Fri, 23 Aug 2024 18:55:27 -0300 Subject: [PATCH 20/68] Make wasm compatible --- cairo-vm-cli/src/main.rs | 8 +++++++- cairo1-run/src/cairo_run.rs | 12 ++++++------ cairo1-run/src/main.rs | 8 +++++++- fuzzer/Cargo.lock | 2 +- vm/src/cairo_run.rs | 17 ++++++++-------- vm/src/tests/cairo_run_test.rs | 2 +- vm/src/types/layout.rs | 24 +++++++++++++---------- vm/src/vm/runners/cairo_runner.rs | 32 +++++++++++++++---------------- 8 files changed, 61 insertions(+), 44 deletions(-) diff --git a/cairo-vm-cli/src/main.rs b/cairo-vm-cli/src/main.rs index e43e06c748..3a164f2c4b 100644 --- a/cairo-vm-cli/src/main.rs +++ b/cairo-vm-cli/src/main.rs @@ -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; @@ -167,6 +168,11 @@ fn run(args: impl Iterator) -> 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, @@ -175,7 +181,7 @@ fn run(args: impl Iterator) -> Result<(), Error> { proof_mode: args.proof_mode, secure_run: args.secure_run, allow_missing_builtins: args.allow_missing_builtins, - cairo_layout_params_file: args.cairo_layout_params_file, + cairo_layout_params, ..Default::default() }; diff --git a/cairo1-run/src/cairo_run.rs b/cairo1-run/src/cairo_run.rs index 60bd02614f..7aca8f5710 100644 --- a/cairo1-run/src/cairo_run.rs +++ b/cairo1-run/src/cairo_run.rs @@ -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}, @@ -51,7 +51,7 @@ use cairo_vm::{ use itertools::{chain, Itertools}; use num_bigint::{BigInt, Sign}; use num_traits::{cast::ToPrimitive, Zero}; -use std::{collections::HashMap, iter::Peekable, path::PathBuf}; +use std::{collections::HashMap, iter::Peekable}; /// Representation of a cairo argument /// Can consist of a single Felt or an array of Felts @@ -86,7 +86,7 @@ pub struct Cairo1RunConfig<'a> { pub relocate_mem: bool, /// Cairo layout chosen for the run pub layout: LayoutName, - pub cairo_layout_params_file: Option, + pub cairo_layout_params: Option, /// Run in proof_mode pub proof_mode: bool, /// Should be true if either air_public_input or cairo_pie_output are needed @@ -107,7 +107,7 @@ impl Default for Cairo1RunConfig<'_> { proof_mode: false, finalize_builtins: false, append_return_values: false, - cairo_layout_params_file: None, + cairo_layout_params: None, } } } @@ -250,7 +250,7 @@ pub fn cairo_run_program( let mut runner = CairoRunner::new_v2( &program, cairo_run_config.layout, - cairo_run_config.cairo_layout_params_file.clone(), + cairo_run_config.cairo_layout_params.clone(), runner_mode, cairo_run_config.trace_enabled, )?; diff --git a/cairo1-run/src/main.rs b/cairo1-run/src/main.rs index 41587e8b92..8969b747d8 100644 --- a/cairo1-run/src/main.rs +++ b/cairo1-run/src/main.rs @@ -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, @@ -158,6 +159,11 @@ fn run(args: impl Iterator) -> Result, 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, @@ -167,7 +173,7 @@ fn run(args: impl Iterator) -> Result, 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, - cairo_layout_params_file: args.cairo_layout_params_file, + cairo_layout_params, }; // Try to parse the file as a sierra program diff --git a/fuzzer/Cargo.lock b/fuzzer/Cargo.lock index e1ccedbfb8..e1ef312c3a 100644 --- a/fuzzer/Cargo.lock +++ b/fuzzer/Cargo.lock @@ -216,7 +216,7 @@ dependencies = [ [[package]] name = "cairo-vm" -version = "1.0.0-rc5" +version = "1.0.1" dependencies = [ "anyhow", "arbitrary", diff --git a/vm/src/cairo_run.rs b/vm/src/cairo_run.rs index 806e0b08bd..a96e6860a1 100644 --- a/vm/src/cairo_run.rs +++ b/vm/src/cairo_run.rs @@ -1,8 +1,9 @@ -use std::path::PathBuf; - 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, @@ -28,7 +29,7 @@ pub struct CairoRunConfig<'a> { pub trace_enabled: bool, pub relocate_mem: bool, pub layout: LayoutName, - pub cairo_layout_params_file: Option, + pub cairo_layout_params: Option, pub proof_mode: bool, pub secure_run: Option, pub disable_trace_padding: bool, @@ -46,7 +47,7 @@ impl<'a> Default for CairoRunConfig<'a> { secure_run: None, disable_trace_padding: false, allow_missing_builtins: None, - cairo_layout_params_file: None, + cairo_layout_params: None, } } } @@ -69,7 +70,7 @@ pub fn cairo_run_program_with_initial_scope( let mut cairo_runner = CairoRunner::new( program, cairo_run_config.layout, - cairo_run_config.cairo_layout_params_file.clone(), + cairo_run_config.cairo_layout_params.clone(), cairo_run_config.proof_mode, cairo_run_config.trace_enabled, )?; @@ -156,7 +157,7 @@ pub fn cairo_run_pie( let mut cairo_runner = CairoRunner::new( &program, cairo_run_config.layout, - cairo_run_config.cairo_layout_params_file.clone(), + cairo_run_config.cairo_layout_params.clone(), false, cairo_run_config.trace_enabled, )?; @@ -228,7 +229,7 @@ pub fn cairo_run_fuzzed_program( let mut cairo_runner = CairoRunner::new( &program, cairo_run_config.layout, - cairo_run_config.cairo_layout_params_file.clone(), + cairo_run_config.cairo_layout_params.clone(), cairo_run_config.proof_mode, cairo_run_config.trace_enabled, )?; diff --git a/vm/src/tests/cairo_run_test.rs b/vm/src/tests/cairo_run_test.rs index 741955a16a..7f17ea62cf 100644 --- a/vm/src/tests/cairo_run_test.rs +++ b/vm/src/tests/cairo_run_test.rs @@ -1182,7 +1182,7 @@ fn run_program_with_custom_mod_builtin_params( let mut cairo_runner = CairoRunner::new( &program, cairo_run_config.layout, - cairo_run_config.cairo_layout_params_file, + cairo_run_config.cairo_layout_params, cairo_run_config.proof_mode, cairo_run_config.trace_enabled, ) diff --git a/vm/src/types/layout.rs b/vm/src/types/layout.rs index 0d6209e2e7..debbb2f244 100644 --- a/vm/src/types/layout.rs +++ b/vm/src/types/layout.rs @@ -1,5 +1,3 @@ -use std::{fs::File, io, path::Path}; - use crate::types::layout_name::LayoutName; use super::instance_definitions::{ @@ -131,16 +129,13 @@ impl CairoLayout { builtins: BuiltinsInstanceDef::dynamic(params), } } - - pub(crate) fn dynamic_instance_from_file(params_file: &Path) -> io::Result { - let params_file = File::open(params_file)?; - let params: CairoLayoutParams = serde_json::from_reader(params_file)?; - - Ok(Self::dynamic_instance(params)) - } } -#[derive(Deserialize, Debug, Default)] +#[cfg(feature = "test_utils")] +use arbitrary::{self, Arbitrary}; + +#[cfg_attr(feature = "test_utils", derive(Arbitrary))] +#[derive(Deserialize, Debug, Default, Clone)] pub struct CairoLayoutParams { pub rc_units: u32, pub log_diluted_units_per_step: u32, @@ -172,6 +167,15 @@ pub struct CairoLayoutParams { pub add_mod_ratio_den: u32, } +impl CairoLayoutParams { + #[cfg(feature = "std")] + pub fn from_file(params_path: &std::path::Path) -> std::io::Result { + let params_file = std::fs::File::open(params_path)?; + let params = serde_json::from_reader(params_file)?; + Ok(params) + } +} + #[cfg(test)] mod tests { use crate::types::instance_definitions::{ diff --git a/vm/src/vm/runners/cairo_runner.rs b/vm/src/vm/runners/cairo_runner.rs index 77683e5523..f87b112f63 100644 --- a/vm/src/vm/runners/cairo_runner.rs +++ b/vm/src/vm/runners/cairo_runner.rs @@ -1,5 +1,3 @@ -use std::path::PathBuf; - use crate::{ air_private_input::AirPrivateInput, air_public_input::{PublicInput, PublicInputError}, @@ -9,7 +7,11 @@ use crate::{ ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign}, prelude::*, }, - types::{builtin_name::BuiltinName, layout::MEMORY_UNITS_PER_STEP, layout_name::LayoutName}, + types::{ + builtin_name::BuiltinName, + layout::{CairoLayoutParams, MEMORY_UNITS_PER_STEP}, + layout_name::LayoutName, + }, vm::{ runners::builtin_runner::SegmentArenaBuiltinRunner, trace::trace_entry::{relocate_trace_register, RelocatedTraceEntry}, @@ -170,12 +172,12 @@ pub enum RunnerMode { } impl CairoRunner { - /// The `cairo_layout_params_file` argument should only be used with dynamic layout. + /// The `cairo_layout_params` argument should only be used with dynamic layout. /// It is ignored otherwise. pub fn new_v2( program: &Program, layout: LayoutName, - cairo_layout_params_file: Option, + cairo_layout_params: Option, mode: RunnerMode, trace_enabled: bool, ) -> Result { @@ -192,17 +194,15 @@ impl CairoRunner { LayoutName::all_solidity => CairoLayout::all_solidity_instance(), LayoutName::dynamic => { debug_assert!( - cairo_layout_params_file.is_some(), - "cairo layout params is missing with dynamic layout" + cairo_layout_params.is_some(), + "cairo layout params is missing while using dynamic layout" ); - let params_file = - cairo_layout_params_file.ok_or(RunnerError::BadDynamicLayoutParams( - "cairo layout param file is missing".to_string(), - ))?; + let params = cairo_layout_params.ok_or(RunnerError::BadDynamicLayoutParams( + "cairo layout param is missing".to_string(), + ))?; - CairoLayout::dynamic_instance_from_file(¶ms_file) - .map_err(|err| RunnerError::BadDynamicLayoutParams(err.to_string()))? + CairoLayout::dynamic_instance(params) } }; Ok(CairoRunner { @@ -233,7 +233,7 @@ impl CairoRunner { pub fn new( program: &Program, layout: LayoutName, - cairo_layout_params_file: Option, + cairo_layout_params: Option, proof_mode: bool, trace_enabled: bool, ) -> Result { @@ -241,7 +241,7 @@ impl CairoRunner { Self::new_v2( program, layout, - cairo_layout_params_file, + cairo_layout_params, RunnerMode::ProofModeCanonical, trace_enabled, ) @@ -249,7 +249,7 @@ impl CairoRunner { Self::new_v2( program, layout, - cairo_layout_params_file, + cairo_layout_params, RunnerMode::ExecutionMode, trace_enabled, ) From a41f231a05bb6c1400fe0a3112ef868e1b8e3095 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Wed, 28 Aug 2024 11:58:51 -0300 Subject: [PATCH 21/68] Use public_memory_fraction = 4 vy default --- vm/src/types/layout.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vm/src/types/layout.rs b/vm/src/types/layout.rs index debbb2f244..07d49af133 100644 --- a/vm/src/types/layout.rs +++ b/vm/src/types/layout.rs @@ -121,7 +121,7 @@ impl CairoLayout { CairoLayout { name: LayoutName::dynamic, rc_units: params.rc_units, - public_memory_fraction: 8, + public_memory_fraction: 4, diluted_pool_instance_def: Some(DilutedPoolInstanceDef { units_per_step: 2_u32.pow(params.log_diluted_units_per_step), ..DilutedPoolInstanceDef::default() @@ -345,7 +345,7 @@ mod tests { assert_eq!(layout.name, LayoutName::dynamic); assert_eq!(layout.rc_units, 32); - assert_eq!(layout.public_memory_fraction, 8); // hardcoded + assert_eq!(layout.public_memory_fraction, 4); // hardcoded assert_eq!( layout.diluted_pool_instance_def, Some(DilutedPoolInstanceDef { From f7334f548beac74c230d941c4ffe4eda964afb66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Wed, 28 Aug 2024 15:15:42 -0300 Subject: [PATCH 22/68] Deserialize bool from int --- vm/src/types/layout.rs | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/vm/src/types/layout.rs b/vm/src/types/layout.rs index 07d49af133..12e4b9b895 100644 --- a/vm/src/types/layout.rs +++ b/vm/src/types/layout.rs @@ -6,7 +6,7 @@ use super::instance_definitions::{ pub(crate) const MEMORY_UNITS_PER_STEP: u32 = 8; -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Deserializer, Serialize}; #[derive(Serialize, Debug)] pub struct CairoLayout { @@ -139,24 +139,34 @@ use arbitrary::{self, Arbitrary}; pub struct CairoLayoutParams { pub rc_units: u32, pub log_diluted_units_per_step: u32, + #[serde(deserialize_with = "bool_from_int_or_bool")] pub uses_pedersen_builtin: bool, pub pedersen_ratio: u32, + #[serde(deserialize_with = "bool_from_int_or_bool")] pub uses_range_check_builtin: bool, pub range_check_ratio: u32, + #[serde(deserialize_with = "bool_from_int_or_bool")] pub uses_ecdsa_builtin: bool, pub ecdsa_ratio: u32, + #[serde(deserialize_with = "bool_from_int_or_bool")] pub uses_bitwise_builtin: bool, pub bitwise_ratio: u32, + #[serde(deserialize_with = "bool_from_int_or_bool")] pub uses_ec_op_builtin: bool, pub ec_op_ratio: u32, + #[serde(deserialize_with = "bool_from_int_or_bool")] pub uses_keccak_builtin: bool, pub keccak_ratio: u32, + #[serde(deserialize_with = "bool_from_int_or_bool")] pub uses_poseidon_builtin: bool, pub poseidon_ratio: u32, + #[serde(deserialize_with = "bool_from_int_or_bool")] pub uses_range_check96_builtin: bool, pub range_check96_ratio: u32, + #[serde(deserialize_with = "bool_from_int_or_bool")] pub uses_add_mod_builtin: bool, pub add_mod_ratio: u32, + #[serde(deserialize_with = "bool_from_int_or_bool")] pub uses_mul_mod_builtin: bool, pub mul_mod_ratio: u32, // the following are not used right now @@ -176,6 +186,24 @@ impl CairoLayoutParams { } } +fn bool_from_int_or_bool<'de, D>(deserializer: D) -> Result +where + D: Deserializer<'de>, +{ + #[derive(Deserialize)] + #[serde(untagged)] + enum IntOrBool { + Int(i64), + Boolean(bool), + } + + match IntOrBool::deserialize(deserializer)? { + IntOrBool::Int(0) => Ok(false), + IntOrBool::Int(_) => Ok(true), + IntOrBool::Boolean(v) => Ok(v), + } +} + #[cfg(test)] mod tests { use crate::types::instance_definitions::{ From 63eba32d51e9f1e5ab966212e002eebfcaf3cd3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Wed, 28 Aug 2024 17:16:14 -0300 Subject: [PATCH 23/68] Add comparison with python-vm (failing) --- .gitignore | 2 +- Makefile | 31 ++++++++ cairo_layout_params_file.example.json | 29 ------- cairo_layout_params_file.json | 29 +++++++ vm/src/tests/compare_vm_state_dyn.sh | 104 ++++++++++++++++++++++++++ 5 files changed, 165 insertions(+), 30 deletions(-) delete mode 100644 cairo_layout_params_file.example.json create mode 100644 cairo_layout_params_file.json create mode 100755 vm/src/tests/compare_vm_state_dyn.sh diff --git a/.gitignore b/.gitignore index 83e14d8942..69fa82e403 100644 --- a/.gitignore +++ b/.gitignore @@ -26,5 +26,5 @@ cairo-vm-cli/air_input.pub ensure-no_std/Cargo.lock cairo_programs/proof_programs/*.cairo -!cairo_layout_params_file.example.json +!cairo_layout_params_file.json !vm/src/tests/cairo_pie_test_output.json diff --git a/Makefile b/Makefile index a58845926c..9cd5858dc1 100644 --- a/Makefile +++ b/Makefile @@ -123,6 +123,34 @@ $(BAD_TEST_DIR)/%.json: $(BAD_TEST_DIR)/%.cairo $(PRINT_TEST_DIR)/%.json: $(PRINT_TEST_DIR)/%.cairo cairo-compile $< --output $@ +# ======================= +# Run with dynamic layout +# ======================= + +CAIRO_DYN_MEM_PROOF:=$(patsubst $(TEST_PROOF_DIR)/%.json, $(TEST_PROOF_DIR)/%.dyn.memory, $(COMPILED_PROOF_TESTS)) +CAIRO_DYN_TRACE_PROOF:=$(patsubst $(TEST_PROOF_DIR)/%.json, $(TEST_PROOF_DIR)/%.dyn.trace, $(COMPILED_PROOF_TESTS)) +CAIRO_DYN_AIR_PUBLIC_INPUT:=$(patsubst $(TEST_PROOF_DIR)/%.json, $(TEST_PROOF_DIR)/%.dyn.air_public_input, $(COMPILED_PROOF_TESTS)) +CAIRO_DYN_AIR_PRIVATE_INPUT:=$(patsubst $(TEST_PROOF_DIR)/%.json, $(TEST_PROOF_DIR)/%.dyn.air_private_input, $(COMPILED_PROOF_TESTS)) + +CAIRO_DYN_RS_MEM_PROOF:=$(patsubst $(TEST_PROOF_DIR)/%.json, $(TEST_PROOF_DIR)/%.rs.dyn.memory, $(COMPILED_PROOF_TESTS)) +CAIRO_DYN_RS_TRACE_PROOF:=$(patsubst $(TEST_PROOF_DIR)/%.json, $(TEST_PROOF_DIR)/%.rs.dyn.trace, $(COMPILED_PROOF_TESTS)) +CAIRO_DYN_RS_AIR_PUBLIC_INPUT:=$(patsubst $(TEST_PROOF_DIR)/%.json, $(TEST_PROOF_DIR)/%.rs.dyn.air_public_input, $(COMPILED_PROOF_TESTS)) +CAIRO_DYN_RS_AIR_PRIVATE_INPUT:=$(patsubst $(TEST_PROOF_DIR)/%.json, $(TEST_PROOF_DIR)/%.rs.dyn.air_private_input, $(COMPILED_PROOF_TESTS)) + +PROOF_BENCH_DIR=cairo_programs/benchmarks +PROOF_BENCH_FILES:=$(wildcard $(PROOF_BENCH_DIR)/*.cairo) +PROOF_COMPILED_BENCHES:=$(patsubst $(PROOF_BENCH_DIR)/%.cairo, $(PROOF_BENCH_DIR)/%.json, $(PROOF_BENCH_FILES)) + +MOD_BUILTIN_TEST_PROOF_DIR=cairo_programs/mod_builtin_feature/proof +MOD_BUILTIN_TEST_PROOF_FILES:=$(wildcard $(MOD_BUILTIN_TEST_PROOF_DIR)/*.cairo) +COMPILED_MOD_BUILTIN_PROOF_TESTS:=$(patsubst $(MOD_BUILTIN_TEST_PROOF_DIR)/%.cairo, $(MOD_BUILTIN_TEST_PROOF_DIR)/%.json, $(MOD_BUILTIN_TEST_PROOF_FILES)) + +$(TEST_PROOF_DIR)/%.rs.dyn.trace $(TEST_PROOF_DIR)/%.rs.dyn.memory $(TEST_PROOF_DIR)/%.rs.dyn.air_public_input $(TEST_PROOF_DIR)/%.rs.dyn.air_private_input: $(TEST_PROOF_DIR)/%.json + cargo run -p cairo-vm-cli --release -- --layout dynamic --proof_mode $< --trace_file $(@D)/$(*F).rs.dyn.trace --memory_file $(@D)/$(*F).rs.dyn.memory --air_public_input $(@D)/$(*F).rs.dyn.air_public_input --air_private_input $(@D)/$(*F).rs.dyn.air_private_input --cairo_layout_params_file cairo_layout_params_file.json + +$(TEST_PROOF_DIR)/%.dyn.trace $(TEST_PROOF_DIR)/%.dyn.memory $(TEST_PROOF_DIR)/%.dyn.air_public_input $(TEST_PROOF_DIR)/%.dyn.air_private_input: $(TEST_PROOF_DIR)/%.json + cairo-run --layout dynamic --proof_mode --program $< --trace_file $(@D)/$(*F).dyn.trace --air_public_input $(@D)/$(*F).dyn.air_public_input --memory_file $(@D)/$(*F).dyn.memory --air_private_input $(@D)/$(*F).dyn.air_private_input --cairo_layout_params_file cairo_layout_params_file.json + # ====================== # Test Cairo 1 Contracts # ====================== @@ -332,6 +360,9 @@ compare_pie: $(CAIRO_RS_PIE) $(CAIRO_PIE) compare_all_pie_outputs: $(CAIRO_RS_PIE) cd vm/src/tests; ./compare_all_pie_outputs.sh +compare_all_proof_dyn: $(COMPILED_DYN_PROOF_TESTS) $(CAIRO_DYN_RS_TRACE_PROOF) $(CAIRO_DYN_TRACE_PROOF) $(CAIRO_DYN_RS_MEM_PROOF) $(CAIRO_DYN_MEM_PROOF) $(CAIRO_DYN_RS_AIR_PUBLIC_INPUT) $(CAIRO_DYN_AIR_PUBLIC_INPUT) $(CAIRO_DYN_RS_AIR_PRIVATE_INPUT) $(CAIRO_DYN_AIR_PRIVATE_INPUT) + cd vm/src/tests; ./compare_vm_state_dyn.sh trace memory proof_mode air_public_input air_private_input + # Run with nightly enable the `doc_cfg` feature wich let us provide clear explaination about which parts of the code are behind a feature flag docs: RUSTDOCFLAGS="--cfg docsrs" cargo +nightly doc --verbose --release --locked --no-deps --all-features --open diff --git a/cairo_layout_params_file.example.json b/cairo_layout_params_file.example.json deleted file mode 100644 index 2e1a9ba54b..0000000000 --- a/cairo_layout_params_file.example.json +++ /dev/null @@ -1,29 +0,0 @@ -{ - "rc_units": 16, - "log_diluted_units_per_step": 1, - "cpu_component_step": 1, - "memory_units_per_step": 8, - "uses_pedersen_builtin": true, - "pedersen_ratio": 16, - "uses_range_check_builtin": true, - "range_check_ratio": 16, - "uses_ecdsa_builtin": true, - "ecdsa_ratio": 16, - "uses_bitwise_builtin": true, - "bitwise_ratio": 16, - "uses_ec_op_builtin": true, - "ec_op_ratio": 16, - "uses_keccak_builtin": true, - "keccak_ratio": 16, - "uses_poseidon_builtin": true, - "poseidon_ratio": 16, - "uses_range_check96_builtin": true, - "range_check96_ratio": 16, - "range_check96_ratio_den": 1, - "uses_add_mod_builtin": true, - "add_mod_ratio": 16, - "add_mod_ratio_den": 1, - "uses_mul_mod_builtin": true, - "mul_mod_ratio": 16, - "mul_mod_ratio_den": 1 -} diff --git a/cairo_layout_params_file.json b/cairo_layout_params_file.json new file mode 100644 index 0000000000..6e6efcfa7c --- /dev/null +++ b/cairo_layout_params_file.json @@ -0,0 +1,29 @@ +{ + "rc_units": 4, + "log_diluted_units_per_step": 4, + "cpu_component_step": 4, + "memory_units_per_step": 8, + "uses_pedersen_builtin": true, + "pedersen_ratio": 32, + "uses_range_check_builtin": true, + "range_check_ratio": 16, + "uses_ecdsa_builtin": true, + "ecdsa_ratio": 2048, + "uses_bitwise_builtin": true, + "bitwise_ratio": 64, + "uses_ec_op_builtin": true, + "ec_op_ratio": 1024, + "uses_keccak_builtin": true, + "keccak_ratio": 2048, + "uses_poseidon_builtin": true, + "poseidon_ratio": 32, + "uses_range_check96_builtin": false, + "range_check96_ratio": 0, + "range_check96_ratio_den": 0, + "uses_add_mod_builtin": false, + "add_mod_ratio": 0, + "add_mod_ratio_den": 0, + "uses_mul_mod_builtin": false, + "mul_mod_ratio": 0, + "mul_mod_ratio_den": 0 +} diff --git a/vm/src/tests/compare_vm_state_dyn.sh b/vm/src/tests/compare_vm_state_dyn.sh new file mode 100755 index 0000000000..738b575ffd --- /dev/null +++ b/vm/src/tests/compare_vm_state_dyn.sh @@ -0,0 +1,104 @@ +#!/usr/bin/env sh + +# move to the directory where the script is located +cd $(dirname "$0") + +tests_path="../../../cairo_programs" +proof_tests_path="${tests_path}/proof_programs" +exit_code=0 +trace=false +memory=false +air_public_input=false +air_private_input=false +passed_tests=0 +failed_tests=0 + +for i in $@; do + case $i in + "trace") trace=true + echo "Requested trace comparison" + ;; + "memory") memory=true + echo "Requested memory comparison" + ;; + "proof_mode") tests_path=$proof_tests_path + echo "Requested proof mode usage" + ;; + "air_public_input") air_public_input=true + echo "Requested air_public_input comparison" + ;; + "air_private_input") air_private_input=true + echo "Requested air_private_input comparison" + ;; + *) + ;; + esac +done + +if $air_public_input; then + if [ $tests_path != $proof_tests_path ]; then + echo "Can't compare air_public_input without proof_mode" + exit 1 + fi +fi + +files=$(ls $tests_path) +EXIT_CODE=$? +if [ ${EXIT_CODE} != 0 ]; then + exit ${EXIT_CODE} +fi + +for file in $(ls $tests_path | grep .cairo$ | sed -E 's/\.cairo$//'); do + path_file="$tests_path/$file" + + if $trace; then + if ! diff -q $path_file.dyn.trace $path_file.rs.dyn.trace; then + echo "Traces for $file differ" + exit_code=1 + failed_tests=$((failed_tests + 1)) + else + passed_tests=$((passed_tests + 1)) + fi + fi + + if $memory; then + if ! ./memory_comparator.py $path_file.dyn.memory $path_file.rs.dyn.memory; then + echo "Memory differs for $file" + exit_code=1 + failed_tests=$((failed_tests + 1)) + else + passed_tests=$((passed_tests + 1)) + fi + fi + + if $air_public_input; then + if ! ./air_public_input_comparator.py $path_file.dyn.air_public_input $path_file.rs.dyn.air_public_input; then + echo "Air Public Input differs for $file" + exit_code=1 + failed_tests=$((failed_tests + 1)) + else + passed_tests=$((passed_tests + 1)) + fi + fi + + if $air_private_input; then + if ! ./air_private_input_comparator.py $path_file.dyn.air_private_input $path_file.rs.dyn.air_private_input; then + echo "Air Private Input differs for $file" + exit_code=1 + failed_tests=$((failed_tests + 1)) + else + passed_tests=$((passed_tests + 1)) + fi + fi +done + +if test $failed_tests != 0; then + echo "Comparisons: $failed_tests failed, $passed_tests passed, $((failed_tests + passed_tests)) total" +elif test $passed_tests = 0; then + echo "No tests ran!" + exit_code=2 +else + echo "All $passed_tests tests passed; no discrepancies found" +fi + +exit "${exit_code}" From 1f2098aa8f3323c0cbe33b9d04e853206dfa4755 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Thu, 29 Aug 2024 11:03:49 -0300 Subject: [PATCH 24/68] Rebuild .rs files in makefile --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 9cd5858dc1..e54efc51b6 100644 --- a/Makefile +++ b/Makefile @@ -145,7 +145,7 @@ MOD_BUILTIN_TEST_PROOF_DIR=cairo_programs/mod_builtin_feature/proof MOD_BUILTIN_TEST_PROOF_FILES:=$(wildcard $(MOD_BUILTIN_TEST_PROOF_DIR)/*.cairo) COMPILED_MOD_BUILTIN_PROOF_TESTS:=$(patsubst $(MOD_BUILTIN_TEST_PROOF_DIR)/%.cairo, $(MOD_BUILTIN_TEST_PROOF_DIR)/%.json, $(MOD_BUILTIN_TEST_PROOF_FILES)) -$(TEST_PROOF_DIR)/%.rs.dyn.trace $(TEST_PROOF_DIR)/%.rs.dyn.memory $(TEST_PROOF_DIR)/%.rs.dyn.air_public_input $(TEST_PROOF_DIR)/%.rs.dyn.air_private_input: $(TEST_PROOF_DIR)/%.json +$(TEST_PROOF_DIR)/%.rs.dyn.trace $(TEST_PROOF_DIR)/%.rs.dyn.memory $(TEST_PROOF_DIR)/%.rs.dyn.air_public_input $(TEST_PROOF_DIR)/%.rs.dyn.air_private_input: $(TEST_PROOF_DIR)/%.json $(RELBIN) cargo run -p cairo-vm-cli --release -- --layout dynamic --proof_mode $< --trace_file $(@D)/$(*F).rs.dyn.trace --memory_file $(@D)/$(*F).rs.dyn.memory --air_public_input $(@D)/$(*F).rs.dyn.air_public_input --air_private_input $(@D)/$(*F).rs.dyn.air_private_input --cairo_layout_params_file cairo_layout_params_file.json $(TEST_PROOF_DIR)/%.dyn.trace $(TEST_PROOF_DIR)/%.dyn.memory $(TEST_PROOF_DIR)/%.dyn.air_public_input $(TEST_PROOF_DIR)/%.dyn.air_private_input: $(TEST_PROOF_DIR)/%.json From 707e0d2dda9b961c1be7a64eb37924a76601bfec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Thu, 29 Aug 2024 11:04:20 -0300 Subject: [PATCH 25/68] Use 8 as dynamic public_memory_fraction The same value is used in python-vm --- vm/src/types/layout.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vm/src/types/layout.rs b/vm/src/types/layout.rs index 12e4b9b895..2bd8030c1a 100644 --- a/vm/src/types/layout.rs +++ b/vm/src/types/layout.rs @@ -121,7 +121,7 @@ impl CairoLayout { CairoLayout { name: LayoutName::dynamic, rc_units: params.rc_units, - public_memory_fraction: 4, + public_memory_fraction: 8, diluted_pool_instance_def: Some(DilutedPoolInstanceDef { units_per_step: 2_u32.pow(params.log_diluted_units_per_step), ..DilutedPoolInstanceDef::default() From 723eaf26d232452f1d992ba6e876b13c16017cf2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Thu, 29 Aug 2024 14:32:42 -0300 Subject: [PATCH 26/68] Use None ratio for dynamic unused builtins --- .../builtins_instance_def.rs | 116 ++++++---------- vm/src/types/layout.rs | 128 +++++++++++++++--- 2 files changed, 150 insertions(+), 94 deletions(-) diff --git a/vm/src/types/instance_definitions/builtins_instance_def.rs b/vm/src/types/instance_definitions/builtins_instance_def.rs index a028c42486..ca281a662a 100644 --- a/vm/src/types/instance_definitions/builtins_instance_def.rs +++ b/vm/src/types/instance_definitions/builtins_instance_def.rs @@ -195,80 +195,40 @@ impl BuiltinsInstanceDef { } pub(crate) fn dynamic(params: CairoLayoutParams) -> BuiltinsInstanceDef { - let pedersen = if params.uses_pedersen_builtin { - Some(PedersenInstanceDef { - ratio: Some(params.pedersen_ratio), - }) - } else { - None - }; - let range_check = if params.uses_range_check_builtin { - Some(RangeCheckInstanceDef { - ratio: Some(params.range_check_ratio), - }) - } else { - None - }; - let ecdsa = if params.uses_ecdsa_builtin { - Some(EcdsaInstanceDef { - ratio: Some(params.ecdsa_ratio), - }) - } else { - None - }; - let bitwise = if params.uses_bitwise_builtin { - Some(BitwiseInstanceDef { - ratio: Some(params.bitwise_ratio), - }) - } else { - None - }; - let ec_op = if params.uses_ec_op_builtin { - Some(EcOpInstanceDef { - ratio: Some(params.ec_op_ratio), - }) - } else { - None - }; - let keccak = if params.uses_keccak_builtin { - Some(KeccakInstanceDef { - ratio: Some(params.keccak_ratio), - }) - } else { - None - }; - let poseidon = if params.uses_poseidon_builtin { - Some(PoseidonInstanceDef { - ratio: Some(params.poseidon_ratio), - }) - } else { - None - }; - let range_check96 = if params.uses_range_check96_builtin { - Some(RangeCheckInstanceDef { - ratio: Some(params.range_check96_ratio), - }) - } else { - None - }; - let add_mod = if params.uses_add_mod_builtin { - Some(ModInstanceDef { - ratio: Some(params.add_mod_ratio), - word_bit_len: 1, - batch_size: 96, - }) - } else { - None - }; - let mul_mod = if params.uses_mul_mod_builtin { - Some(ModInstanceDef { - ratio: Some(params.mul_mod_ratio), - word_bit_len: 1, - batch_size: 96, - }) - } else { - None - }; + let pedersen = Some(PedersenInstanceDef { + ratio: non_zero(params.pedersen_ratio), + }); + let range_check = Some(RangeCheckInstanceDef { + ratio: non_zero(params.range_check_ratio), + }); + let ecdsa = Some(EcdsaInstanceDef { + ratio: non_zero(params.ecdsa_ratio), + }); + let bitwise = Some(BitwiseInstanceDef { + ratio: non_zero(params.bitwise_ratio), + }); + let ec_op = Some(EcOpInstanceDef { + ratio: non_zero(params.ec_op_ratio), + }); + let keccak = Some(KeccakInstanceDef { + ratio: non_zero(params.keccak_ratio), + }); + let poseidon = Some(PoseidonInstanceDef { + ratio: non_zero(params.poseidon_ratio), + }); + let range_check96 = Some(RangeCheckInstanceDef { + ratio: non_zero(params.range_check96_ratio), + }); + let add_mod = Some(ModInstanceDef { + ratio: non_zero(params.add_mod_ratio), + word_bit_len: 1, + batch_size: 96, + }); + let mul_mod = Some(ModInstanceDef { + ratio: non_zero(params.mul_mod_ratio), + word_bit_len: 1, + batch_size: 96, + }); BuiltinsInstanceDef { output: true, @@ -286,6 +246,14 @@ impl BuiltinsInstanceDef { } } +fn non_zero(n: u32) -> Option { + if n == 0 { + None + } else { + Some(n) + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/vm/src/types/layout.rs b/vm/src/types/layout.rs index 2bd8030c1a..6773c192d7 100644 --- a/vm/src/types/layout.rs +++ b/vm/src/types/layout.rs @@ -135,8 +135,44 @@ impl CairoLayout { use arbitrary::{self, Arbitrary}; #[cfg_attr(feature = "test_utils", derive(Arbitrary))] -#[derive(Deserialize, Debug, Default, Clone)] +#[derive(Deserialize, Debug, Clone, Default)] +#[serde(try_from = "RawCairoLayoutParams")] pub struct CairoLayoutParams { + pub rc_units: u32, + pub log_diluted_units_per_step: u32, + pub pedersen_ratio: u32, + pub range_check_ratio: u32, + pub ecdsa_ratio: u32, + pub bitwise_ratio: u32, + pub ec_op_ratio: u32, + pub keccak_ratio: u32, + pub poseidon_ratio: u32, + pub range_check96_ratio: u32, + pub add_mod_ratio: u32, + pub mul_mod_ratio: u32, + // the following are not used right now + pub cpu_component_step: u32, + pub memory_units_per_step: u32, + pub range_check96_ratio_den: u32, + pub mul_mod_ratio_den: u32, + pub add_mod_ratio_den: u32, +} + +impl CairoLayoutParams { + #[cfg(feature = "std")] + pub fn from_file(params_path: &std::path::Path) -> std::io::Result { + let params_file = std::fs::File::open(params_path)?; + let params = serde_json::from_reader(params_file)?; + Ok(params) + } +} + +// The CairoLayoutParams contains aditional constraints that can't be validated by serde alone. +// To work around this. we use an aditional structure `RawCairoLayoutParams` that gets deserialized by serde +// and then its tranformed into `CairoLayoutParams`. + +#[derive(Deserialize, Debug, Default, Clone)] +pub struct RawCairoLayoutParams { pub rc_units: u32, pub log_diluted_units_per_step: u32, #[serde(deserialize_with = "bool_from_int_or_bool")] @@ -177,12 +213,60 @@ pub struct CairoLayoutParams { pub add_mod_ratio_den: u32, } -impl CairoLayoutParams { - #[cfg(feature = "std")] - pub fn from_file(params_path: &std::path::Path) -> std::io::Result { - let params_file = std::fs::File::open(params_path)?; - let params = serde_json::from_reader(params_file)?; - Ok(params) +impl TryFrom for CairoLayoutParams { + type Error = &'static str; + + fn try_from(value: RawCairoLayoutParams) -> Result { + if !value.uses_pedersen_builtin && value.pedersen_ratio != 0 { + return Err("pedersen ratio should be 0 when disabled"); + } + if !value.uses_range_check_builtin && value.range_check_ratio != 0 { + return Err("range_check ratio should be 0 when disabled"); + } + if !value.uses_ecdsa_builtin && value.ecdsa_ratio != 0 { + return Err("ecdsa ratio should be 0 when disabled"); + } + if !value.uses_bitwise_builtin && value.bitwise_ratio != 0 { + return Err("bitwise ratio should be 0 when disabled"); + } + if !value.uses_ec_op_builtin && value.ec_op_ratio != 0 { + return Err("ec_op ratio should be 0 when disabled"); + } + if !value.uses_keccak_builtin && value.keccak_ratio != 0 { + return Err("keccak ratio should be 0 when disabled"); + } + if !value.uses_poseidon_builtin && value.poseidon_ratio != 0 { + return Err("poseidon ratio should be 0 when disabled"); + } + if !value.uses_range_check96_builtin && value.range_check96_ratio != 0 { + return Err("range_check96 ratio should be 0 when disabled"); + } + if !value.uses_add_mod_builtin && value.add_mod_ratio != 0 { + return Err("add_mod ratio should be 0 when disabled"); + } + if !value.uses_mul_mod_builtin && value.mul_mod_ratio != 0 { + return Err("mul_mod ratio should be 0 when disabled"); + } + + Ok(CairoLayoutParams { + rc_units: value.rc_units, + log_diluted_units_per_step: value.log_diluted_units_per_step, + cpu_component_step: value.cpu_component_step, + memory_units_per_step: value.memory_units_per_step, + range_check96_ratio_den: value.range_check96_ratio_den, + mul_mod_ratio_den: value.mul_mod_ratio_den, + add_mod_ratio_den: value.add_mod_ratio_den, + pedersen_ratio: value.pedersen_ratio, + range_check_ratio: value.range_check_ratio, + ecdsa_ratio: value.ecdsa_ratio, + bitwise_ratio: value.bitwise_ratio, + ec_op_ratio: value.ec_op_ratio, + keccak_ratio: value.keccak_ratio, + poseidon_ratio: value.poseidon_ratio, + range_check96_ratio: value.range_check96_ratio, + add_mod_ratio: value.add_mod_ratio, + mul_mod_ratio: value.mul_mod_ratio, + }) } } @@ -210,6 +294,7 @@ mod tests { bitwise_instance_def::BitwiseInstanceDef, ec_op_instance_def::EcOpInstanceDef, ecdsa_instance_def::EcdsaInstanceDef, keccak_instance_def::KeccakInstanceDef, mod_instance_def::ModInstanceDef, pedersen_instance_def::PedersenInstanceDef, + poseidon_instance_def::PoseidonInstanceDef, range_check_instance_def::RangeCheckInstanceDef, }; @@ -344,22 +429,12 @@ mod tests { let params = CairoLayoutParams { rc_units: 32, log_diluted_units_per_step: 5, - uses_pedersen_builtin: true, pedersen_ratio: 32, - uses_range_check_builtin: true, range_check_ratio: 32, - uses_ecdsa_builtin: true, ecdsa_ratio: 32, - uses_bitwise_builtin: true, bitwise_ratio: 32, - uses_ec_op_builtin: true, ec_op_ratio: 32, - uses_keccak_builtin: true, keccak_ratio: 32, - uses_poseidon_builtin: false, - uses_range_check96_builtin: false, - uses_add_mod_builtin: false, - uses_mul_mod_builtin: true, mul_mod_ratio: 32, ..Default::default() // // cpu_component_step: todo!(), @@ -407,9 +482,14 @@ mod tests { layout.builtins.keccak, Some(KeccakInstanceDef { ratio: Some(32) }) ); - assert_eq!(layout.builtins.poseidon, None,); - assert_eq!(layout.builtins.range_check96, None,); - assert_eq!(layout.builtins.add_mod, None); + assert_eq!( + layout.builtins.poseidon, + Some(PoseidonInstanceDef { ratio: Some(0) }), + ); + assert_eq!( + layout.builtins.range_check96, + Some(RangeCheckInstanceDef { ratio: Some(0) }) + ); assert_eq!( layout.builtins.mul_mod, Some(ModInstanceDef { @@ -418,5 +498,13 @@ mod tests { batch_size: 96 // hardcoded }) ); + assert_eq!( + layout.builtins.add_mod, + Some(ModInstanceDef { + ratio: Some(0), + word_bit_len: 1, // hardcoded + batch_size: 96 // hardcoded + }) + ); } } From f20b97bbff9fc76d45067250d76f0da63ac0472e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Thu, 29 Aug 2024 15:09:55 -0300 Subject: [PATCH 27/68] Add rangecheck96 to private inputs --- vm/src/air_private_input.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/vm/src/air_private_input.rs b/vm/src/air_private_input.rs index fa05cc86d5..10a134912c 100644 --- a/vm/src/air_private_input.rs +++ b/vm/src/air_private_input.rs @@ -29,6 +29,8 @@ pub struct AirPrivateInputSerializable { #[serde(skip_serializing_if = "Option::is_none")] poseidon: Option>, #[serde(skip_serializing_if = "Option::is_none")] + range_check_96: Option>, + #[serde(skip_serializing_if = "Option::is_none")] add_mod: Option, #[serde(skip_serializing_if = "Option::is_none")] mul_mod: Option, @@ -162,6 +164,7 @@ impl AirPrivateInput { ec_op: self.0.get(&BuiltinName::ec_op).cloned(), keccak: self.0.get(&BuiltinName::keccak).cloned(), poseidon: self.0.get(&BuiltinName::poseidon).cloned(), + range_check_96: self.0.get(&BuiltinName::range_check96).cloned(), add_mod: self .0 .get(&BuiltinName::add_mod) @@ -273,6 +276,7 @@ mod tests { )]), add_mod: None, mul_mod: None, + range_check_96: None, }; let private_input = AirPrivateInput::from(serializable_private_input.clone()); From e6d3f9b48b5a9f0d90dd1f55e51b71dc322cb326 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Thu, 29 Aug 2024 16:27:35 -0300 Subject: [PATCH 28/68] Make dyn py files depend on params_file --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index e54efc51b6..26d5c7e30b 100644 --- a/Makefile +++ b/Makefile @@ -148,7 +148,7 @@ COMPILED_MOD_BUILTIN_PROOF_TESTS:=$(patsubst $(MOD_BUILTIN_TEST_PROOF_DIR)/%.cai $(TEST_PROOF_DIR)/%.rs.dyn.trace $(TEST_PROOF_DIR)/%.rs.dyn.memory $(TEST_PROOF_DIR)/%.rs.dyn.air_public_input $(TEST_PROOF_DIR)/%.rs.dyn.air_private_input: $(TEST_PROOF_DIR)/%.json $(RELBIN) cargo run -p cairo-vm-cli --release -- --layout dynamic --proof_mode $< --trace_file $(@D)/$(*F).rs.dyn.trace --memory_file $(@D)/$(*F).rs.dyn.memory --air_public_input $(@D)/$(*F).rs.dyn.air_public_input --air_private_input $(@D)/$(*F).rs.dyn.air_private_input --cairo_layout_params_file cairo_layout_params_file.json -$(TEST_PROOF_DIR)/%.dyn.trace $(TEST_PROOF_DIR)/%.dyn.memory $(TEST_PROOF_DIR)/%.dyn.air_public_input $(TEST_PROOF_DIR)/%.dyn.air_private_input: $(TEST_PROOF_DIR)/%.json +$(TEST_PROOF_DIR)/%.dyn.trace $(TEST_PROOF_DIR)/%.dyn.memory $(TEST_PROOF_DIR)/%.dyn.air_public_input $(TEST_PROOF_DIR)/%.dyn.air_private_input: $(TEST_PROOF_DIR)/%.json cairo_layout_params_file.json cairo-run --layout dynamic --proof_mode --program $< --trace_file $(@D)/$(*F).dyn.trace --air_public_input $(@D)/$(*F).dyn.air_public_input --memory_file $(@D)/$(*F).dyn.memory --air_private_input $(@D)/$(*F).dyn.air_private_input --cairo_layout_params_file cairo_layout_params_file.json # ====================== From 3f5eef523d34ceb46b9c7cb170aa1c51bc478606 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Thu, 29 Aug 2024 16:27:48 -0300 Subject: [PATCH 29/68] Use cpu_component_step=1 by default --- cairo_layout_params_file.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cairo_layout_params_file.json b/cairo_layout_params_file.json index 6e6efcfa7c..36a8013ffc 100644 --- a/cairo_layout_params_file.json +++ b/cairo_layout_params_file.json @@ -1,7 +1,7 @@ { "rc_units": 4, "log_diluted_units_per_step": 4, - "cpu_component_step": 4, + "cpu_component_step": 1, "memory_units_per_step": 8, "uses_pedersen_builtin": true, "pedersen_ratio": 32, From 026e459a6556ea870b1d4842d5cdf66a84229069 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Thu, 29 Aug 2024 17:26:56 -0300 Subject: [PATCH 30/68] Fix typo in private inputs --- vm/src/air_private_input.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/vm/src/air_private_input.rs b/vm/src/air_private_input.rs index 10a134912c..09f0b567a9 100644 --- a/vm/src/air_private_input.rs +++ b/vm/src/air_private_input.rs @@ -29,7 +29,7 @@ pub struct AirPrivateInputSerializable { #[serde(skip_serializing_if = "Option::is_none")] poseidon: Option>, #[serde(skip_serializing_if = "Option::is_none")] - range_check_96: Option>, + range_check96: Option>, #[serde(skip_serializing_if = "Option::is_none")] add_mod: Option, #[serde(skip_serializing_if = "Option::is_none")] @@ -164,7 +164,7 @@ impl AirPrivateInput { ec_op: self.0.get(&BuiltinName::ec_op).cloned(), keccak: self.0.get(&BuiltinName::keccak).cloned(), poseidon: self.0.get(&BuiltinName::poseidon).cloned(), - range_check_96: self.0.get(&BuiltinName::range_check96).cloned(), + range_check96: self.0.get(&BuiltinName::range_check96).cloned(), add_mod: self .0 .get(&BuiltinName::add_mod) @@ -276,7 +276,7 @@ mod tests { )]), add_mod: None, mul_mod: None, - range_check_96: None, + range_check96: None, }; let private_input = AirPrivateInput::from(serializable_private_input.clone()); From 15b77421603d1651a90a5bbb0549c083500d82d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Fri, 30 Aug 2024 12:00:58 -0300 Subject: [PATCH 31/68] Add range check value to air private input test --- vm/src/air_private_input.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/vm/src/air_private_input.rs b/vm/src/air_private_input.rs index 09f0b567a9..9b6cfbf8f2 100644 --- a/vm/src/air_private_input.rs +++ b/vm/src/air_private_input.rs @@ -276,7 +276,10 @@ mod tests { )]), add_mod: None, mul_mod: None, - range_check96: None, + range_check96: Some(vec![PrivateInput::Value(PrivateInputValue { + index: 10000, + value: Felt252::from(8000), + })]), }; let private_input = AirPrivateInput::from(serializable_private_input.clone()); From 698b35ce1d8aae91c1fab7803f6b3c4daa09c3f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Fri, 30 Aug 2024 12:11:55 -0300 Subject: [PATCH 32/68] Fix zero segment location --- vm/src/vm/runners/builtin_runner/modulo.rs | 5 ++++- vm/src/vm/runners/cairo_runner.rs | 5 +++++ vm/src/vm/vm_memory/memory_segments.rs | 10 +++++++--- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/vm/src/vm/runners/builtin_runner/modulo.rs b/vm/src/vm/runners/builtin_runner/modulo.rs index 554e8912da..bcaecc9088 100644 --- a/vm/src/vm/runners/builtin_runner/modulo.rs +++ b/vm/src/vm/runners/builtin_runner/modulo.rs @@ -118,7 +118,10 @@ impl ModBuiltinRunner { pub fn initialize_segments(&mut self, segments: &mut MemorySegmentManager) { self.base = segments.add().segment_index as usize; // segments.add() always returns a positive index - self.zero_segment_index = segments.add_zero_segment(self.zero_segment_size) + } + + pub fn initialize_zero_segment(&mut self, segments: &mut MemorySegmentManager) { + self.zero_segment_index = segments.add_zero_segment(self.zero_segment_size); } pub fn initial_stack(&self) -> Vec { diff --git a/vm/src/vm/runners/cairo_runner.rs b/vm/src/vm/runners/cairo_runner.rs index f87b112f63..85f1314e9b 100644 --- a/vm/src/vm/runners/cairo_runner.rs +++ b/vm/src/vm/runners/cairo_runner.rs @@ -260,6 +260,11 @@ impl CairoRunner { self.initialize_builtins(allow_missing_builtins)?; self.initialize_segments(None); let end = self.initialize_main_entrypoint()?; + for builtin_runner in self.vm.builtin_runners.iter_mut() { + if let BuiltinRunner::Mod(runner) = builtin_runner { + runner.initialize_zero_segment(&mut self.vm.segments); + } + } self.initialize_vm()?; Ok(end) } diff --git a/vm/src/vm/vm_memory/memory_segments.rs b/vm/src/vm/vm_memory/memory_segments.rs index 04a0a58f18..a98b43bf52 100644 --- a/vm/src/vm/vm_memory/memory_segments.rs +++ b/vm/src/vm/vm_memory/memory_segments.rs @@ -217,7 +217,7 @@ impl MemorySegmentManager { } let accessed_amount = // Instead of marking the values in the zero segment until zero_segment_size as accessed we use zero_segment_size as accessed_amount - if !self.zero_segment_index.is_zero() && i == self.zero_segment_index { + if self.has_zero_segment() && i == self.zero_segment_index { self.zero_segment_size } else { match self.memory.get_amount_of_accessed_addresses_for_segment(i) { @@ -238,6 +238,10 @@ impl MemorySegmentManager { Ok(memory_holes) } + pub fn has_zero_segment(&self) -> bool { + !self.zero_segment_index.is_zero() + } + /// Returns a list of addresses of memory cells that constitute the public memory. /// segment_offsets is the result of self.relocate_segments() pub fn get_public_memory_addresses( @@ -282,7 +286,7 @@ impl MemorySegmentManager { // Fills the segment with the value 0 until size is reached // Returns the index of the zero segment pub(crate) fn add_zero_segment(&mut self, size: usize) -> usize { - if self.zero_segment_index.is_zero() { + if !self.has_zero_segment() { self.zero_segment_index = self.add().segment_index as usize; } @@ -298,7 +302,7 @@ impl MemorySegmentManager { // Finalizes the zero segment and clears it's tracking data from the manager pub(crate) fn finalize_zero_segment(&mut self) { - if !self.zero_segment_index.is_zero() { + if self.has_zero_segment() { self.finalize(Some(self.zero_segment_size), self.zero_segment_index, None); self.zero_segment_index = 0; self.zero_segment_size = 0; From c58dc4ebe32365fd7895bec2b76ee5cf3b66b178 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Fri, 30 Aug 2024 17:15:02 -0300 Subject: [PATCH 33/68] Use zero builtin instead of None --- .../builtins_instance_def.rs | 36 ++++++++----------- vm/src/vm/runners/builtin_runner/mod.rs | 24 +++++++++---- vm/src/vm/runners/cairo_runner.rs | 7 ++-- 3 files changed, 34 insertions(+), 33 deletions(-) diff --git a/vm/src/types/instance_definitions/builtins_instance_def.rs b/vm/src/types/instance_definitions/builtins_instance_def.rs index ca281a662a..d0790d223b 100644 --- a/vm/src/types/instance_definitions/builtins_instance_def.rs +++ b/vm/src/types/instance_definitions/builtins_instance_def.rs @@ -196,38 +196,38 @@ impl BuiltinsInstanceDef { pub(crate) fn dynamic(params: CairoLayoutParams) -> BuiltinsInstanceDef { let pedersen = Some(PedersenInstanceDef { - ratio: non_zero(params.pedersen_ratio), + ratio: Some(params.pedersen_ratio), }); let range_check = Some(RangeCheckInstanceDef { - ratio: non_zero(params.range_check_ratio), + ratio: Some(params.range_check_ratio), }); let ecdsa = Some(EcdsaInstanceDef { - ratio: non_zero(params.ecdsa_ratio), + ratio: Some(params.ecdsa_ratio), }); let bitwise = Some(BitwiseInstanceDef { - ratio: non_zero(params.bitwise_ratio), + ratio: Some(params.bitwise_ratio), }); let ec_op = Some(EcOpInstanceDef { - ratio: non_zero(params.ec_op_ratio), + ratio: Some(params.ec_op_ratio), }); let keccak = Some(KeccakInstanceDef { - ratio: non_zero(params.keccak_ratio), + ratio: Some(params.keccak_ratio), }); let poseidon = Some(PoseidonInstanceDef { - ratio: non_zero(params.poseidon_ratio), + ratio: Some(params.poseidon_ratio), }); let range_check96 = Some(RangeCheckInstanceDef { - ratio: non_zero(params.range_check96_ratio), + ratio: Some(params.range_check96_ratio), }); let add_mod = Some(ModInstanceDef { - ratio: non_zero(params.add_mod_ratio), - word_bit_len: 1, - batch_size: 96, + ratio: Some(params.add_mod_ratio), + word_bit_len: 96, + batch_size: 1, }); let mul_mod = Some(ModInstanceDef { - ratio: non_zero(params.mul_mod_ratio), - word_bit_len: 1, - batch_size: 96, + ratio: Some(params.mul_mod_ratio), + word_bit_len: 96, + batch_size: 1, }); BuiltinsInstanceDef { @@ -246,14 +246,6 @@ impl BuiltinsInstanceDef { } } -fn non_zero(n: u32) -> Option { - if n == 0 { - None - } else { - Some(n) - } -} - #[cfg(test)] mod tests { use super::*; diff --git a/vm/src/vm/runners/builtin_runner/mod.rs b/vm/src/vm/runners/builtin_runner/mod.rs index 26d4a9026c..17e290972f 100644 --- a/vm/src/vm/runners/builtin_runner/mod.rs +++ b/vm/src/vm/runners/builtin_runner/mod.rs @@ -170,6 +170,14 @@ impl BuiltinRunner { pub fn get_allocated_memory_units( &self, vm: &VirtualMachine, + ) -> Result { + Ok(self.get_allocated_instances(vm)? * self.cells_per_instance() as usize) + } + + ///Returns the builtin's allocated instances + pub fn get_allocated_instances( + &self, + vm: &VirtualMachine, ) -> Result { match *self { BuiltinRunner::Output(_) | BuiltinRunner::SegmentArena(_) => Ok(0), @@ -179,12 +187,16 @@ impl BuiltinRunner { // Dynamic layout has the exact number of instances it needs (up to a power of 2). let instances: usize = self.get_used_cells(&vm.segments)? / self.cells_per_instance() as usize; - let components = (instances / self.instances_per_component() as usize) - .next_power_of_two(); - Ok(self.cells_per_instance() as usize - * self.instances_per_component() as usize - * components) + let needed_components = instances / self.instances_per_component() as usize; + + let components = if needed_components > 0 { + needed_components.next_power_of_two() + } else { + 0 + }; + Ok(self.instances_per_component() as usize * components) } + Some(0) => Ok(0), Some(ratio) => { let min_step = (ratio * self.instances_per_component()) as usize; if vm.current_step < min_step { @@ -195,7 +207,7 @@ impl BuiltinRunner { }; let value = safe_div_usize(vm.current_step, ratio as usize) .map_err(|_| MemoryError::ErrorCalculatingMemoryUnits)?; - Ok(self.cells_per_instance() as usize * value) + Ok(value) } } } diff --git a/vm/src/vm/runners/cairo_runner.rs b/vm/src/vm/runners/cairo_runner.rs index 85f1314e9b..1da8d2731d 100644 --- a/vm/src/vm/runners/cairo_runner.rs +++ b/vm/src/vm/runners/cairo_runner.rs @@ -21,7 +21,6 @@ use crate::{ use crate::{ hint_processor::hint_processor_definition::{HintProcessor, HintReference}, - math_utils::safe_div_usize, types::{ errors::{math_errors::MathError, program_errors::ProgramError}, exec_scope::ExecutionScopes, @@ -848,10 +847,8 @@ impl CairoRunner { diluted_pool_instance.n_bits, ); - let multiplier = safe_div_usize( - self.vm.current_step, - builtin_runner.ratio().unwrap_or(1) as usize, - )?; + let multiplier = builtin_runner.get_allocated_instances(&self.vm)?; + used_units_by_builtins += used_units * multiplier; } From ce985ba6d4783ff6756ea18d6f557141860e2f93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Fri, 30 Aug 2024 18:43:58 -0300 Subject: [PATCH 34/68] Add debug scripts --- vm/src/tests/air_private_inputs_differ.bash | 6 ++++++ vm/src/tests/air_public_inputs_differ.bash | 6 ++++++ vm/src/tests/memory_differ.bash | 6 ++++++ vm/src/tests/memory_viewer.bash | 5 +++++ 4 files changed, 23 insertions(+) create mode 100755 vm/src/tests/air_private_inputs_differ.bash create mode 100755 vm/src/tests/air_public_inputs_differ.bash create mode 100755 vm/src/tests/memory_differ.bash create mode 100755 vm/src/tests/memory_viewer.bash diff --git a/vm/src/tests/air_private_inputs_differ.bash b/vm/src/tests/air_private_inputs_differ.bash new file mode 100755 index 0000000000..8ce3a0215c --- /dev/null +++ b/vm/src/tests/air_private_inputs_differ.bash @@ -0,0 +1,6 @@ +#!/usr/bin/env bash + +delta \ + <(jq -S 'del(.memory_path,.trace_path)' "$1") \ + <(jq -S 'del(.memory_path,.trace_path)' "$2") \ + -s diff --git a/vm/src/tests/air_public_inputs_differ.bash b/vm/src/tests/air_public_inputs_differ.bash new file mode 100755 index 0000000000..84637c350f --- /dev/null +++ b/vm/src/tests/air_public_inputs_differ.bash @@ -0,0 +1,6 @@ +#!/usr/bin/env bash + +delta \ + <(jq -S . "$1") \ + <(jq -S . "$2") \ + -s diff --git a/vm/src/tests/memory_differ.bash b/vm/src/tests/memory_differ.bash new file mode 100755 index 0000000000..b5c0f2649c --- /dev/null +++ b/vm/src/tests/memory_differ.bash @@ -0,0 +1,6 @@ +#!/usr/bin/env bash + +delta \ + <(hexdump -e '1/8 "%u " 4/8 "%x " "\n"' "$1" | sort -n) \ + <(hexdump -e '1/8 "%u " 4/8 "%x " "\n"' "$2" | sort -n) \ + -s diff --git a/vm/src/tests/memory_viewer.bash b/vm/src/tests/memory_viewer.bash new file mode 100755 index 0000000000..a6e74032f2 --- /dev/null +++ b/vm/src/tests/memory_viewer.bash @@ -0,0 +1,5 @@ +#!/usr/bin/env bash +# +# outputs memory file in "ADDR VALUE" format, on cell per line + +hexdump -e '1/8 "%10u " 4/8 "%x " "\n"' "$1" | sort -n From 08874bd5e8bd4513a2f27d0766640170e45274dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Mon, 2 Sep 2024 11:59:57 -0300 Subject: [PATCH 35/68] Remove dup makefile recipes --- Makefile | 8 -------- 1 file changed, 8 deletions(-) diff --git a/Makefile b/Makefile index d9ad9d854e..b742675a24 100644 --- a/Makefile +++ b/Makefile @@ -137,14 +137,6 @@ CAIRO_DYN_RS_TRACE_PROOF:=$(patsubst $(TEST_PROOF_DIR)/%.json, $(TEST_PROOF_DIR) CAIRO_DYN_RS_AIR_PUBLIC_INPUT:=$(patsubst $(TEST_PROOF_DIR)/%.json, $(TEST_PROOF_DIR)/%.rs.dyn.air_public_input, $(COMPILED_PROOF_TESTS)) CAIRO_DYN_RS_AIR_PRIVATE_INPUT:=$(patsubst $(TEST_PROOF_DIR)/%.json, $(TEST_PROOF_DIR)/%.rs.dyn.air_private_input, $(COMPILED_PROOF_TESTS)) -PROOF_BENCH_DIR=cairo_programs/benchmarks -PROOF_BENCH_FILES:=$(wildcard $(PROOF_BENCH_DIR)/*.cairo) -PROOF_COMPILED_BENCHES:=$(patsubst $(PROOF_BENCH_DIR)/%.cairo, $(PROOF_BENCH_DIR)/%.json, $(PROOF_BENCH_FILES)) - -MOD_BUILTIN_TEST_PROOF_DIR=cairo_programs/mod_builtin_feature/proof -MOD_BUILTIN_TEST_PROOF_FILES:=$(wildcard $(MOD_BUILTIN_TEST_PROOF_DIR)/*.cairo) -COMPILED_MOD_BUILTIN_PROOF_TESTS:=$(patsubst $(MOD_BUILTIN_TEST_PROOF_DIR)/%.cairo, $(MOD_BUILTIN_TEST_PROOF_DIR)/%.json, $(MOD_BUILTIN_TEST_PROOF_FILES)) - $(TEST_PROOF_DIR)/%.rs.dyn.trace $(TEST_PROOF_DIR)/%.rs.dyn.memory $(TEST_PROOF_DIR)/%.rs.dyn.air_public_input $(TEST_PROOF_DIR)/%.rs.dyn.air_private_input: $(TEST_PROOF_DIR)/%.json $(RELBIN) cargo run -p cairo-vm-cli --release -- --layout dynamic --proof_mode $< --trace_file $(@D)/$(*F).rs.dyn.trace --memory_file $(@D)/$(*F).rs.dyn.memory --air_public_input $(@D)/$(*F).rs.dyn.air_public_input --air_private_input $(@D)/$(*F).rs.dyn.air_private_input --cairo_layout_params_file cairo_layout_params_file.json From ba4d3c03b86c36047527d1e38ab25d6f04256cf8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Mon, 2 Sep 2024 15:46:36 -0300 Subject: [PATCH 36/68] remove outdated test --- vm/src/vm/runners/builtin_runner/modulo.rs | 208 --------------------- 1 file changed, 208 deletions(-) diff --git a/vm/src/vm/runners/builtin_runner/modulo.rs b/vm/src/vm/runners/builtin_runner/modulo.rs index e7e7d848cb..b4ec583688 100644 --- a/vm/src/vm/runners/builtin_runner/modulo.rs +++ b/vm/src/vm/runners/builtin_runner/modulo.rs @@ -677,211 +677,3 @@ fn apply_op(lhs: &BigUint, rhs: &BigUint, op: &Operation) -> Result div_mod_unsigned(lhs, rhs, p)?, }) } - -#[cfg(test)] -mod tests { - - #[test] - #[cfg(feature = "mod_builtin")] - fn test_air_private_input_small_batch_size() { - use super::*; - use crate::{ - air_private_input::{ModInput, ModInputInstance, ModInputMemoryVars, PrivateInput}, - hint_processor::builtin_hint_processor::builtin_hint_processor_definition::BuiltinHintProcessor, - types::layout_name::LayoutName, - utils::test_utils::Program, - vm::runners::{builtin_runner::BuiltinRunner, cairo_runner::CairoRunner}, - Felt252, - }; - - let program_data = include_bytes!( - "../../../../../cairo_programs/mod_builtin_feature/proof/mod_builtin.json" - ); - - let mut hint_processor = BuiltinHintProcessor::new_empty(); - let program = Program::from_bytes(program_data, Some("main")).unwrap(); - let mut runner = CairoRunner::new(&program, LayoutName::all_cairo, true, false).unwrap(); - - let end = runner.initialize(false).unwrap(); - // Modify add_mod & mul_mod params - for runner in runner.vm.get_builtin_runners_as_mut() { - if let BuiltinRunner::Mod(runner) = runner { - runner.override_layout_params(1, 3) - } - } - - runner.run_until_pc(end, &mut hint_processor).unwrap(); - runner.run_for_steps(1, &mut hint_processor).unwrap(); - runner.end_run(false, false, &mut hint_processor).unwrap(); - runner.read_return_values(false).unwrap(); - runner.finalize_segments().unwrap(); - - let air_private_input = runner.get_air_private_input(); - assert_eq!( - air_private_input.0.get(&BuiltinName::add_mod).unwrap()[0], - PrivateInput::Mod(ModInput { - instances: vec![ - ModInputInstance { - index: 0, - p0: Felt252::ONE, - p1: Felt252::ONE, - p2: Felt252::ZERO, - p3: Felt252::ZERO, - values_ptr: 23023, - offsets_ptr: 23055, - n: 2, - batch: BTreeMap::from([( - 0, - ModInputMemoryVars { - a_offset: 0, - a0: Felt252::ONE, - a1: Felt252::ZERO, - a2: Felt252::ZERO, - a3: Felt252::ZERO, - b_offset: 12, - b0: Felt252::ZERO, - b1: Felt252::ZERO, - b2: Felt252::ZERO, - b3: Felt252::ZERO, - c_offset: 4, - c0: Felt252::TWO, - c1: Felt252::ONE, - c2: Felt252::ZERO, - c3: Felt252::ZERO - } - ),]) - }, - ModInputInstance { - index: 1, - p0: Felt252::ONE, - p1: Felt252::ONE, - p2: Felt252::ZERO, - p3: Felt252::ZERO, - values_ptr: 23023, - offsets_ptr: 23058, - n: 1, - batch: BTreeMap::from([( - 0, - ModInputMemoryVars { - a_offset: 16, - a0: Felt252::ZERO, - a1: Felt252::ZERO, - a2: Felt252::ZERO, - a3: Felt252::ZERO, - b_offset: 20, - b0: Felt252::TWO, - b1: Felt252::ZERO, - b2: Felt252::ZERO, - b3: Felt252::ZERO, - c_offset: 24, - c0: Felt252::TWO, - c1: Felt252::ZERO, - c2: Felt252::ZERO, - c3: Felt252::ZERO - } - ),]) - } - ], - zero_value_address: 22123 - }) - ); - assert_eq!( - air_private_input.0.get(&BuiltinName::mul_mod).unwrap()[0], - PrivateInput::Mod(ModInput { - instances: vec![ - ModInputInstance { - index: 0, - p0: Felt252::ONE, - p1: Felt252::ONE, - p2: Felt252::ZERO, - p3: Felt252::ZERO, - values_ptr: 23023, - offsets_ptr: 23061, - n: 3, - batch: BTreeMap::from([( - 0, - ModInputMemoryVars { - a_offset: 12, - a0: Felt252::ZERO, - a1: Felt252::ZERO, - a2: Felt252::ZERO, - a3: Felt252::ZERO, - b_offset: 8, - b0: Felt252::TWO, - b1: Felt252::ZERO, - b2: Felt252::ZERO, - b3: Felt252::ZERO, - c_offset: 16, - c0: Felt252::ZERO, - c1: Felt252::ZERO, - c2: Felt252::ZERO, - c3: Felt252::ZERO - } - ),]) - }, - ModInputInstance { - index: 1, - p0: Felt252::ONE, - p1: Felt252::ONE, - p2: Felt252::ZERO, - p3: Felt252::ZERO, - values_ptr: 23023, - offsets_ptr: 23064, - n: 2, - batch: BTreeMap::from([( - 0, - ModInputMemoryVars { - a_offset: 0, - a0: Felt252::ONE, - a1: Felt252::ZERO, - a2: Felt252::ZERO, - a3: Felt252::ZERO, - b_offset: 8, - b0: Felt252::TWO, - b1: Felt252::ZERO, - b2: Felt252::ZERO, - b3: Felt252::ZERO, - c_offset: 20, - c0: Felt252::TWO, - c1: Felt252::ZERO, - c2: Felt252::ZERO, - c3: Felt252::ZERO - } - ),]) - }, - ModInputInstance { - index: 2, - p0: Felt252::ONE, - p1: Felt252::ONE, - p2: Felt252::ZERO, - p3: Felt252::ZERO, - values_ptr: 23023, - offsets_ptr: 23067, - n: 1, - batch: BTreeMap::from([( - 0, - ModInputMemoryVars { - a_offset: 8, - a0: Felt252::TWO, - a1: Felt252::ZERO, - a2: Felt252::ZERO, - a3: Felt252::ZERO, - b_offset: 28, - b0: Felt252::ONE, - b1: Felt252::ZERO, - b2: Felt252::ZERO, - b3: Felt252::ZERO, - c_offset: 24, - c0: Felt252::TWO, - c1: Felt252::ZERO, - c2: Felt252::ZERO, - c3: Felt252::ZERO - } - ),]) - } - ], - zero_value_address: 22123 - }) - ) - } -} From 0e0c65b77b7e5ab357864ff54ed940dfdb62edfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Mon, 2 Sep 2024 15:48:44 -0300 Subject: [PATCH 37/68] Enable ensure-no_std on test --- ensure-no_std/src/main.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/ensure-no_std/src/main.rs b/ensure-no_std/src/main.rs index bc3e032cd5..b25cce251d 100644 --- a/ensure-no_std/src/main.rs +++ b/ensure-no_std/src/main.rs @@ -4,7 +4,6 @@ use core::panic::PanicInfo; /// This function is called on panic. -#[cfg(not(test))] #[panic_handler] fn panic(_info: &PanicInfo) -> ! { loop {} From 0b9fed552d1b7d986acdf90ff2312adf35888beb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Mon, 2 Sep 2024 15:53:11 -0300 Subject: [PATCH 38/68] Fix tests --- vm/src/types/layout.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/vm/src/types/layout.rs b/vm/src/types/layout.rs index 6773c192d7..061eda2e36 100644 --- a/vm/src/types/layout.rs +++ b/vm/src/types/layout.rs @@ -448,7 +448,7 @@ mod tests { assert_eq!(layout.name, LayoutName::dynamic); assert_eq!(layout.rc_units, 32); - assert_eq!(layout.public_memory_fraction, 4); // hardcoded + assert_eq!(layout.public_memory_fraction, 8); // hardcoded assert_eq!( layout.diluted_pool_instance_def, Some(DilutedPoolInstanceDef { @@ -494,16 +494,16 @@ mod tests { layout.builtins.mul_mod, Some(ModInstanceDef { ratio: Some(32), - word_bit_len: 1, // hardcoded - batch_size: 96 // hardcoded + word_bit_len: 96, // hardcoded + batch_size: 1 // hardcoded }) ); assert_eq!( layout.builtins.add_mod, Some(ModInstanceDef { ratio: Some(0), - word_bit_len: 1, // hardcoded - batch_size: 96 // hardcoded + word_bit_len: 96, // hardcoded + batch_size: 1 // hardcoded }) ); } From 737850f05643f0133e1d79954fb38c5193da1726 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Mon, 2 Sep 2024 16:24:01 -0300 Subject: [PATCH 39/68] Add correct test --- vm/src/vm/runners/builtin_runner/modulo.rs | 202 +++++++++++++++++++++ 1 file changed, 202 insertions(+) diff --git a/vm/src/vm/runners/builtin_runner/modulo.rs b/vm/src/vm/runners/builtin_runner/modulo.rs index b4ec583688..af38fffbf4 100644 --- a/vm/src/vm/runners/builtin_runner/modulo.rs +++ b/vm/src/vm/runners/builtin_runner/modulo.rs @@ -677,3 +677,205 @@ fn apply_op(lhs: &BigUint, rhs: &BigUint, op: &Operation) -> Result div_mod_unsigned(lhs, rhs, p)?, }) } + +#[cfg(test)] +mod tests { + #[test] + #[cfg(feature = "mod_builtin")] + fn test_air_private_input_small_batch_size() { + use super::*; + use crate::{ + air_private_input::{ModInput, ModInputInstance, ModInputMemoryVars, PrivateInput}, + hint_processor::builtin_hint_processor::builtin_hint_processor_definition::BuiltinHintProcessor, + types::layout_name::LayoutName, + utils::test_utils::Program, + vm::runners::cairo_runner::CairoRunner, + Felt252, + }; + + let program_data = include_bytes!( + "../../../../../cairo_programs/mod_builtin_feature/proof/mod_builtin.json" + ); + + let mut hint_processor = BuiltinHintProcessor::new_empty(); + let program = Program::from_bytes(program_data, Some("main")).unwrap(); + let mut runner = CairoRunner::new(&program, LayoutName::all_cairo, true, false).unwrap(); + + let end = runner.initialize(false).unwrap(); + // Modify add_mod & mul_mod params + + runner.run_until_pc(end, &mut hint_processor).unwrap(); + runner.run_for_steps(1, &mut hint_processor).unwrap(); + runner.end_run(false, false, &mut hint_processor).unwrap(); + runner.read_return_values(false).unwrap(); + runner.finalize_segments().unwrap(); + + let air_private_input = runner.get_air_private_input(); + assert_eq!( + air_private_input.0.get(&BuiltinName::add_mod).unwrap()[0], + PrivateInput::Mod(ModInput { + instances: vec![ + ModInputInstance { + index: 0, + p0: Felt252::ONE, + p1: Felt252::ONE, + p2: Felt252::ZERO, + p3: Felt252::ZERO, + values_ptr: 23023, + offsets_ptr: 23055, + n: 2, + batch: BTreeMap::from([( + 0, + ModInputMemoryVars { + a_offset: 0, + a0: Felt252::ONE, + a1: Felt252::ZERO, + a2: Felt252::ZERO, + a3: Felt252::ZERO, + b_offset: 12, + b0: Felt252::ZERO, + b1: Felt252::ZERO, + b2: Felt252::ZERO, + b3: Felt252::ZERO, + c_offset: 4, + c0: Felt252::TWO, + c1: Felt252::ONE, + c2: Felt252::ZERO, + c3: Felt252::ZERO + } + ),]) + }, + ModInputInstance { + index: 1, + p0: Felt252::ONE, + p1: Felt252::ONE, + p2: Felt252::ZERO, + p3: Felt252::ZERO, + values_ptr: 23023, + offsets_ptr: 23058, + n: 1, + batch: BTreeMap::from([( + 0, + ModInputMemoryVars { + a_offset: 16, + a0: Felt252::ZERO, + a1: Felt252::ZERO, + a2: Felt252::ZERO, + a3: Felt252::ZERO, + b_offset: 20, + b0: Felt252::TWO, + b1: Felt252::ZERO, + b2: Felt252::ZERO, + b3: Felt252::ZERO, + c_offset: 24, + c0: Felt252::TWO, + c1: Felt252::ZERO, + c2: Felt252::ZERO, + c3: Felt252::ZERO + } + ),]) + } + ], + zero_value_address: 23019 + }) + ); + assert_eq!( + air_private_input.0.get(&BuiltinName::mul_mod).unwrap()[0], + PrivateInput::Mod(ModInput { + instances: vec![ + ModInputInstance { + index: 0, + p0: Felt252::ONE, + p1: Felt252::ONE, + p2: Felt252::ZERO, + p3: Felt252::ZERO, + values_ptr: 23023, + offsets_ptr: 23061, + n: 3, + batch: BTreeMap::from([( + 0, + ModInputMemoryVars { + a_offset: 12, + a0: Felt252::ZERO, + a1: Felt252::ZERO, + a2: Felt252::ZERO, + a3: Felt252::ZERO, + b_offset: 8, + b0: Felt252::TWO, + b1: Felt252::ZERO, + b2: Felt252::ZERO, + b3: Felt252::ZERO, + c_offset: 16, + c0: Felt252::ZERO, + c1: Felt252::ZERO, + c2: Felt252::ZERO, + c3: Felt252::ZERO + } + ),]) + }, + ModInputInstance { + index: 1, + p0: Felt252::ONE, + p1: Felt252::ONE, + p2: Felt252::ZERO, + p3: Felt252::ZERO, + values_ptr: 23023, + offsets_ptr: 23064, + n: 2, + batch: BTreeMap::from([( + 0, + ModInputMemoryVars { + a_offset: 0, + a0: Felt252::ONE, + a1: Felt252::ZERO, + a2: Felt252::ZERO, + a3: Felt252::ZERO, + b_offset: 8, + b0: Felt252::TWO, + b1: Felt252::ZERO, + b2: Felt252::ZERO, + b3: Felt252::ZERO, + c_offset: 20, + c0: Felt252::TWO, + c1: Felt252::ZERO, + c2: Felt252::ZERO, + c3: Felt252::ZERO + } + ),]) + }, + ModInputInstance { + index: 2, + p0: Felt252::ONE, + p1: Felt252::ONE, + p2: Felt252::ZERO, + p3: Felt252::ZERO, + values_ptr: 23023, + offsets_ptr: 23067, + n: 1, + batch: BTreeMap::from([( + 0, + ModInputMemoryVars { + a_offset: 8, + a0: Felt252::TWO, + a1: Felt252::ZERO, + a2: Felt252::ZERO, + a3: Felt252::ZERO, + b_offset: 28, + b0: Felt252::ONE, + b1: Felt252::ZERO, + b2: Felt252::ZERO, + b3: Felt252::ZERO, + c_offset: 24, + c0: Felt252::TWO, + c1: Felt252::ZERO, + c2: Felt252::ZERO, + c3: Felt252::ZERO + } + ),]) + } + ], + zero_value_address: 23019 + }) + ) + } +} From 34e5eafa9e12780c8d338108d51467c25d14bc3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Tue, 3 Sep 2024 12:06:52 -0300 Subject: [PATCH 40/68] Rename tset --- vm/src/vm/runners/builtin_runner/modulo.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vm/src/vm/runners/builtin_runner/modulo.rs b/vm/src/vm/runners/builtin_runner/modulo.rs index af38fffbf4..97ac68045c 100644 --- a/vm/src/vm/runners/builtin_runner/modulo.rs +++ b/vm/src/vm/runners/builtin_runner/modulo.rs @@ -682,7 +682,7 @@ fn apply_op(lhs: &BigUint, rhs: &BigUint, op: &Operation) -> Result Date: Tue, 3 Sep 2024 12:07:24 -0300 Subject: [PATCH 41/68] Add comment --- vm/src/vm/runners/builtin_runner/modulo.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/vm/src/vm/runners/builtin_runner/modulo.rs b/vm/src/vm/runners/builtin_runner/modulo.rs index 97ac68045c..b4aa0b7fa6 100644 --- a/vm/src/vm/runners/builtin_runner/modulo.rs +++ b/vm/src/vm/runners/builtin_runner/modulo.rs @@ -710,6 +710,7 @@ mod tests { runner.read_return_values(false).unwrap(); runner.finalize_segments().unwrap(); + // We compare against the execution of python cairo-run with the same layout let air_private_input = runner.get_air_private_input(); assert_eq!( air_private_input.0.get(&BuiltinName::add_mod).unwrap()[0], From 28eb15c4f25b8ce7c79e10b82d6a9a6fcab9ca3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Tue, 3 Sep 2024 16:33:48 -0300 Subject: [PATCH 42/68] Add debugging document --- docs/README.md | 1 + docs/debugging.md | 31 +++++++++++++++++++ .../air_private_inputs_differ.bash | 0 .../air_public_inputs_differ.bash | 0 {vm/src/tests => scripts}/memory_differ.bash | 0 {vm/src/tests => scripts}/memory_viewer.bash | 0 6 files changed, 32 insertions(+) create mode 100644 docs/debugging.md rename {vm/src/tests => scripts}/air_private_inputs_differ.bash (100%) rename {vm/src/tests => scripts}/air_public_inputs_differ.bash (100%) rename {vm/src/tests => scripts}/memory_differ.bash (100%) rename {vm/src/tests => scripts}/memory_viewer.bash (100%) diff --git a/docs/README.md b/docs/README.md index f25a1c762a..5d70a46fea 100644 --- a/docs/README.md +++ b/docs/README.md @@ -5,3 +5,4 @@ * [How to run a cairo program with custom hints](./hint_processor/builtin_hint_processor) * [References parsing](./references_parsing/) * [Tracer](./tracer/) +* [Debugging](./debugging.md) diff --git a/docs/debugging.md b/docs/debugging.md new file mode 100644 index 0000000000..e9aa2c0755 --- /dev/null +++ b/docs/debugging.md @@ -0,0 +1,31 @@ +# Debugging + +## Comparing with Cairo-Lang + +If you executed a Cairo0 proof program with both Rust and Python VM, you can use the following scripts to compare their output. They all require `delta` (modern diff) to be installed. If you don't have you can locally change it. + +No output when running a differ script implies that there are no differences. + +To compare the public inputs, run: +```bash +scripts/air_public_inputs_differ.bash file1 file2 +``` + +To compare the private inputs, run: +```bash +scripts/air_private_inputs_differ.bash file1 file2 +``` + +If you just want to visualize the memory, run: +```bash +scripts/memory_viewer.bash file +``` +It will output the memory in two columns: address and value + + +To compare the memory, run: +```bash +scripts/memory_differ.bash file1 file2 +``` + + diff --git a/vm/src/tests/air_private_inputs_differ.bash b/scripts/air_private_inputs_differ.bash similarity index 100% rename from vm/src/tests/air_private_inputs_differ.bash rename to scripts/air_private_inputs_differ.bash diff --git a/vm/src/tests/air_public_inputs_differ.bash b/scripts/air_public_inputs_differ.bash similarity index 100% rename from vm/src/tests/air_public_inputs_differ.bash rename to scripts/air_public_inputs_differ.bash diff --git a/vm/src/tests/memory_differ.bash b/scripts/memory_differ.bash similarity index 100% rename from vm/src/tests/memory_differ.bash rename to scripts/memory_differ.bash diff --git a/vm/src/tests/memory_viewer.bash b/scripts/memory_viewer.bash similarity index 100% rename from vm/src/tests/memory_viewer.bash rename to scripts/memory_viewer.bash From 20ce2aad54b6253c914c21a6a42920421f50c2b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Tue, 3 Sep 2024 16:38:48 -0300 Subject: [PATCH 43/68] Update cairo layout params file --- cairo_layout_params_file.json | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/cairo_layout_params_file.json b/cairo_layout_params_file.json index 36a8013ffc..f4d0d736f5 100644 --- a/cairo_layout_params_file.json +++ b/cairo_layout_params_file.json @@ -1,29 +1,29 @@ { "rc_units": 4, "log_diluted_units_per_step": 4, - "cpu_component_step": 1, + "cpu_component_step": 8, "memory_units_per_step": 8, "uses_pedersen_builtin": true, - "pedersen_ratio": 32, + "pedersen_ratio": 256, "uses_range_check_builtin": true, - "range_check_ratio": 16, + "range_check_ratio": 8, "uses_ecdsa_builtin": true, "ecdsa_ratio": 2048, "uses_bitwise_builtin": true, - "bitwise_ratio": 64, + "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": 32, - "uses_range_check96_builtin": false, - "range_check96_ratio": 0, - "range_check96_ratio_den": 0, - "uses_add_mod_builtin": false, - "add_mod_ratio": 0, - "add_mod_ratio_den": 0, - "uses_mul_mod_builtin": false, - "mul_mod_ratio": 0, - "mul_mod_ratio_den": 0 + "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 } From af9fadddb8610bf008536cbd3c1201e36281b524 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Tue, 3 Sep 2024 17:36:45 -0300 Subject: [PATCH 44/68] Remove duplicated range check --- vm/src/air_private_input.rs | 7 ------- 1 file changed, 7 deletions(-) diff --git a/vm/src/air_private_input.rs b/vm/src/air_private_input.rs index a5fc33205a..6275ebd6a1 100644 --- a/vm/src/air_private_input.rs +++ b/vm/src/air_private_input.rs @@ -31,8 +31,6 @@ pub struct AirPrivateInputSerializable { #[serde(skip_serializing_if = "Option::is_none")] poseidon: Option>, #[serde(skip_serializing_if = "Option::is_none")] - range_check96: Option>, - #[serde(skip_serializing_if = "Option::is_none")] add_mod: Option, #[serde(skip_serializing_if = "Option::is_none")] mul_mod: Option, @@ -167,7 +165,6 @@ impl AirPrivateInput { ec_op: self.0.get(&BuiltinName::ec_op).cloned(), keccak: self.0.get(&BuiltinName::keccak).cloned(), poseidon: self.0.get(&BuiltinName::poseidon).cloned(), - range_check96: self.0.get(&BuiltinName::range_check96).cloned(), add_mod: self .0 .get(&BuiltinName::add_mod) @@ -283,10 +280,6 @@ mod tests { )]), add_mod: None, mul_mod: None, - range_check96: Some(vec![PrivateInput::Value(PrivateInputValue { - index: 10000, - value: Felt252::from(8000), - })]), }; let private_input = AirPrivateInput::from(serializable_private_input.clone()); From fbf074d57ef1bb3ba60e705d1a42bf3fd0cf497d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Tue, 3 Sep 2024 17:47:41 -0300 Subject: [PATCH 45/68] Remove dup --- vm/src/vm/vm_memory/memory_segments.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/vm/src/vm/vm_memory/memory_segments.rs b/vm/src/vm/vm_memory/memory_segments.rs index a9987a170d..b6d7379786 100644 --- a/vm/src/vm/vm_memory/memory_segments.rs +++ b/vm/src/vm/vm_memory/memory_segments.rs @@ -238,10 +238,6 @@ impl MemorySegmentManager { Ok(memory_holes) } - pub fn has_zero_segment(&self) -> bool { - !self.zero_segment_index.is_zero() - } - /// Returns a list of addresses of memory cells that constitute the public memory. /// segment_offsets is the result of self.relocate_segments() pub fn get_public_memory_addresses( From 050c71afbd9314ad8fde831cb546aa05476ec1e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Tue, 17 Sep 2024 17:50:19 -0300 Subject: [PATCH 46/68] Remove debugging and scrippts (moveed to another branch) --- docs/README.md | 1 - docs/debugging.md | 31 -------------------------- scripts/air_private_inputs_differ.bash | 6 ----- scripts/air_public_inputs_differ.bash | 6 ----- scripts/memory_differ.bash | 6 ----- scripts/memory_viewer.bash | 5 ----- 6 files changed, 55 deletions(-) delete mode 100644 docs/debugging.md delete mode 100755 scripts/air_private_inputs_differ.bash delete mode 100755 scripts/air_public_inputs_differ.bash delete mode 100755 scripts/memory_differ.bash delete mode 100755 scripts/memory_viewer.bash diff --git a/docs/README.md b/docs/README.md index 5d70a46fea..f25a1c762a 100644 --- a/docs/README.md +++ b/docs/README.md @@ -5,4 +5,3 @@ * [How to run a cairo program with custom hints](./hint_processor/builtin_hint_processor) * [References parsing](./references_parsing/) * [Tracer](./tracer/) -* [Debugging](./debugging.md) diff --git a/docs/debugging.md b/docs/debugging.md deleted file mode 100644 index e9aa2c0755..0000000000 --- a/docs/debugging.md +++ /dev/null @@ -1,31 +0,0 @@ -# Debugging - -## Comparing with Cairo-Lang - -If you executed a Cairo0 proof program with both Rust and Python VM, you can use the following scripts to compare their output. They all require `delta` (modern diff) to be installed. If you don't have you can locally change it. - -No output when running a differ script implies that there are no differences. - -To compare the public inputs, run: -```bash -scripts/air_public_inputs_differ.bash file1 file2 -``` - -To compare the private inputs, run: -```bash -scripts/air_private_inputs_differ.bash file1 file2 -``` - -If you just want to visualize the memory, run: -```bash -scripts/memory_viewer.bash file -``` -It will output the memory in two columns: address and value - - -To compare the memory, run: -```bash -scripts/memory_differ.bash file1 file2 -``` - - diff --git a/scripts/air_private_inputs_differ.bash b/scripts/air_private_inputs_differ.bash deleted file mode 100755 index 8ce3a0215c..0000000000 --- a/scripts/air_private_inputs_differ.bash +++ /dev/null @@ -1,6 +0,0 @@ -#!/usr/bin/env bash - -delta \ - <(jq -S 'del(.memory_path,.trace_path)' "$1") \ - <(jq -S 'del(.memory_path,.trace_path)' "$2") \ - -s diff --git a/scripts/air_public_inputs_differ.bash b/scripts/air_public_inputs_differ.bash deleted file mode 100755 index 84637c350f..0000000000 --- a/scripts/air_public_inputs_differ.bash +++ /dev/null @@ -1,6 +0,0 @@ -#!/usr/bin/env bash - -delta \ - <(jq -S . "$1") \ - <(jq -S . "$2") \ - -s diff --git a/scripts/memory_differ.bash b/scripts/memory_differ.bash deleted file mode 100755 index b5c0f2649c..0000000000 --- a/scripts/memory_differ.bash +++ /dev/null @@ -1,6 +0,0 @@ -#!/usr/bin/env bash - -delta \ - <(hexdump -e '1/8 "%u " 4/8 "%x " "\n"' "$1" | sort -n) \ - <(hexdump -e '1/8 "%u " 4/8 "%x " "\n"' "$2" | sort -n) \ - -s diff --git a/scripts/memory_viewer.bash b/scripts/memory_viewer.bash deleted file mode 100755 index a6e74032f2..0000000000 --- a/scripts/memory_viewer.bash +++ /dev/null @@ -1,5 +0,0 @@ -#!/usr/bin/env bash -# -# outputs memory file in "ADDR VALUE" format, on cell per line - -hexdump -e '1/8 "%10u " 4/8 "%x " "\n"' "$1" | sort -n From 3f9e684bc03d1a891169c2732eb6fa2b2a4e443a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Tue, 17 Sep 2024 17:57:42 -0300 Subject: [PATCH 47/68] Add comment --- vm/src/vm/runners/builtin_runner/mod.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/vm/src/vm/runners/builtin_runner/mod.rs b/vm/src/vm/runners/builtin_runner/mod.rs index 17e290972f..a18878c6cb 100644 --- a/vm/src/vm/runners/builtin_runner/mod.rs +++ b/vm/src/vm/runners/builtin_runner/mod.rs @@ -196,6 +196,7 @@ impl BuiltinRunner { }; Ok(self.instances_per_component() as usize * components) } + // Dynamic layout allows for builtins with ratio 0 Some(0) => Ok(0), Some(ratio) => { let min_step = (ratio * self.instances_per_component()) as usize; From fca3cbfdcac24dcf0d000950de78eeeb0d05ea03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Wed, 18 Sep 2024 12:16:15 -0300 Subject: [PATCH 48/68] Add tests --- vm/src/vm/runners/builtin_runner/mod.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/vm/src/vm/runners/builtin_runner/mod.rs b/vm/src/vm/runners/builtin_runner/mod.rs index a18878c6cb..3be5781bfa 100644 --- a/vm/src/vm/runners/builtin_runner/mod.rs +++ b/vm/src/vm/runners/builtin_runner/mod.rs @@ -1066,6 +1066,26 @@ mod tests { assert_eq!(builtin.get_allocated_memory_units(&vm), Ok(256)); } + #[test] + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] + fn get_allocated_memory_units_zero_ratio() { + let builtin = BuiltinRunner::Keccak(KeccakBuiltinRunner::new(Some(0), true)); + let vm = vm!(); + assert_eq!(builtin.get_allocated_memory_units(&vm), Ok(0)); + } + + #[test] + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] + fn get_allocated_memory_units_none_ratio() { + let mut builtin = BuiltinRunner::Keccak(KeccakBuiltinRunner::new(None, true)); + let mut vm = vm!(); + + builtin.initialize_segments(&mut vm.segments); + vm.compute_segments_effective_sizes(); + + assert_eq!(builtin.get_allocated_memory_units(&vm), Ok(0)); + } + #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_range_check_usage_range_check() { From eb63b5c1384b7c9b75ae5529bd07ac516814423c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Wed, 18 Sep 2024 14:17:31 -0300 Subject: [PATCH 49/68] Add dynamic test to cairo-vm-cli --- cairo-vm-cli/src/main.rs | 13 +++++++++++++ cairo1-run/src/main.rs | 13 +++++++++++++ 2 files changed, 26 insertions(+) diff --git a/cairo-vm-cli/src/main.rs b/cairo-vm-cli/src/main.rs index 3a164f2c4b..74cf444810 100644 --- a/cairo-vm-cli/src/main.rs +++ b/cairo-vm-cli/src/main.rs @@ -417,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] diff --git a/cairo1-run/src/main.rs b/cairo1-run/src/main.rs index 1fa2e4b141..c4e00888d5 100644 --- a/cairo1-run/src/main.rs +++ b/cairo1-run/src/main.rs @@ -490,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] From a6f15111738ed5d0bc7bbf3b514cb14c85c18a4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Wed, 18 Sep 2024 14:58:20 -0300 Subject: [PATCH 50/68] Add parse test --- vm/src/types/layout.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/vm/src/types/layout.rs b/vm/src/types/layout.rs index 061eda2e36..1e1f070557 100644 --- a/vm/src/types/layout.rs +++ b/vm/src/types/layout.rs @@ -507,4 +507,40 @@ mod tests { }) ); } + + #[test] + fn parse_dynamic_instance() { + let cairo_layout_params_json = "{\n\ + \"rc_units\": 4,\n\ + \"log_diluted_units_per_step\": 4,\n\ + \"cpu_component_step\": 8,\n\ + \"memory_units_per_step\": 8,\n\ + \"uses_pedersen_builtin\": true,\n\ + \"pedersen_ratio\": 256,\n\ + \"uses_range_check_builtin\": true,\n\ + \"range_check_ratio\": 8,\n\ + \"uses_ecdsa_builtin\": true,\n\ + \"ecdsa_ratio\": 2048,\n\ + \"uses_bitwise_builtin\": true,\n\ + \"bitwise_ratio\": 16,\n\ + \"uses_ec_op_builtin\": true,\n\ + \"ec_op_ratio\": 1024,\n\ + \"uses_keccak_builtin\": true,\n\ + \"keccak_ratio\": 2048,\n\ + \"uses_poseidon_builtin\": true,\n\ + \"poseidon_ratio\": 256,\n\ + \"uses_range_check96_builtin\": true,\n\ + \"range_check96_ratio\": 8,\n\ + \"range_check96_ratio_den\": 1,\n\ + \"uses_add_mod_builtin\": true,\n\ + \"add_mod_ratio\": 128,\n\ + \"add_mod_ratio_den\": 1,\n\ + \"uses_mul_mod_builtin\": true,\n\ + \"mul_mod_ratio\": 256,\n\ + \"mul_mod_ratio_den\": 1\n\ + }\n\ + "; + + serde_json::from_str::(cairo_layout_params_json).unwrap(); + } } From f2c0abbc3cda9321bde706cda873e07977b7da68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Wed, 18 Sep 2024 15:11:10 -0300 Subject: [PATCH 51/68] Remove compare all dynamic --- Makefile | 23 ------ vm/src/tests/compare_vm_state_dyn.sh | 104 --------------------------- 2 files changed, 127 deletions(-) delete mode 100755 vm/src/tests/compare_vm_state_dyn.sh diff --git a/Makefile b/Makefile index da7bd5afc7..06cba4b0f1 100644 --- a/Makefile +++ b/Makefile @@ -123,26 +123,6 @@ $(BAD_TEST_DIR)/%.json: $(BAD_TEST_DIR)/%.cairo $(PRINT_TEST_DIR)/%.json: $(PRINT_TEST_DIR)/%.cairo cairo-compile $< --output $@ -# ======================= -# Run with dynamic layout -# ======================= - -CAIRO_DYN_MEM_PROOF:=$(patsubst $(TEST_PROOF_DIR)/%.json, $(TEST_PROOF_DIR)/%.dyn.memory, $(COMPILED_PROOF_TESTS)) -CAIRO_DYN_TRACE_PROOF:=$(patsubst $(TEST_PROOF_DIR)/%.json, $(TEST_PROOF_DIR)/%.dyn.trace, $(COMPILED_PROOF_TESTS)) -CAIRO_DYN_AIR_PUBLIC_INPUT:=$(patsubst $(TEST_PROOF_DIR)/%.json, $(TEST_PROOF_DIR)/%.dyn.air_public_input, $(COMPILED_PROOF_TESTS)) -CAIRO_DYN_AIR_PRIVATE_INPUT:=$(patsubst $(TEST_PROOF_DIR)/%.json, $(TEST_PROOF_DIR)/%.dyn.air_private_input, $(COMPILED_PROOF_TESTS)) - -CAIRO_DYN_RS_MEM_PROOF:=$(patsubst $(TEST_PROOF_DIR)/%.json, $(TEST_PROOF_DIR)/%.rs.dyn.memory, $(COMPILED_PROOF_TESTS)) -CAIRO_DYN_RS_TRACE_PROOF:=$(patsubst $(TEST_PROOF_DIR)/%.json, $(TEST_PROOF_DIR)/%.rs.dyn.trace, $(COMPILED_PROOF_TESTS)) -CAIRO_DYN_RS_AIR_PUBLIC_INPUT:=$(patsubst $(TEST_PROOF_DIR)/%.json, $(TEST_PROOF_DIR)/%.rs.dyn.air_public_input, $(COMPILED_PROOF_TESTS)) -CAIRO_DYN_RS_AIR_PRIVATE_INPUT:=$(patsubst $(TEST_PROOF_DIR)/%.json, $(TEST_PROOF_DIR)/%.rs.dyn.air_private_input, $(COMPILED_PROOF_TESTS)) - -$(TEST_PROOF_DIR)/%.rs.dyn.trace $(TEST_PROOF_DIR)/%.rs.dyn.memory $(TEST_PROOF_DIR)/%.rs.dyn.air_public_input $(TEST_PROOF_DIR)/%.rs.dyn.air_private_input: $(TEST_PROOF_DIR)/%.json $(RELBIN) - cargo run -p cairo-vm-cli --release -- --layout dynamic --proof_mode $< --trace_file $(@D)/$(*F).rs.dyn.trace --memory_file $(@D)/$(*F).rs.dyn.memory --air_public_input $(@D)/$(*F).rs.dyn.air_public_input --air_private_input $(@D)/$(*F).rs.dyn.air_private_input --cairo_layout_params_file cairo_layout_params_file.json - -$(TEST_PROOF_DIR)/%.dyn.trace $(TEST_PROOF_DIR)/%.dyn.memory $(TEST_PROOF_DIR)/%.dyn.air_public_input $(TEST_PROOF_DIR)/%.dyn.air_private_input: $(TEST_PROOF_DIR)/%.json cairo_layout_params_file.json - cairo-run --layout dynamic --proof_mode --program $< --trace_file $(@D)/$(*F).dyn.trace --air_public_input $(@D)/$(*F).dyn.air_public_input --memory_file $(@D)/$(*F).dyn.memory --air_private_input $(@D)/$(*F).dyn.air_private_input --cairo_layout_params_file cairo_layout_params_file.json - # ====================== # Test Cairo 1 Contracts # ====================== @@ -352,9 +332,6 @@ compare_pie: $(CAIRO_RS_PIE) $(CAIRO_PIE) compare_all_pie_outputs: $(CAIRO_RS_PIE) cd vm/src/tests; ./compare_all_pie_outputs.sh -compare_all_proof_dyn: $(COMPILED_DYN_PROOF_TESTS) $(CAIRO_DYN_RS_TRACE_PROOF) $(CAIRO_DYN_TRACE_PROOF) $(CAIRO_DYN_RS_MEM_PROOF) $(CAIRO_DYN_MEM_PROOF) $(CAIRO_DYN_RS_AIR_PUBLIC_INPUT) $(CAIRO_DYN_AIR_PUBLIC_INPUT) $(CAIRO_DYN_RS_AIR_PRIVATE_INPUT) $(CAIRO_DYN_AIR_PRIVATE_INPUT) - cd vm/src/tests; ./compare_vm_state_dyn.sh trace memory proof_mode air_public_input air_private_input - # Run with nightly enable the `doc_cfg` feature wich let us provide clear explaination about which parts of the code are behind a feature flag docs: RUSTDOCFLAGS="--cfg docsrs" cargo +nightly doc --verbose --release --locked --no-deps --all-features --open diff --git a/vm/src/tests/compare_vm_state_dyn.sh b/vm/src/tests/compare_vm_state_dyn.sh deleted file mode 100755 index 738b575ffd..0000000000 --- a/vm/src/tests/compare_vm_state_dyn.sh +++ /dev/null @@ -1,104 +0,0 @@ -#!/usr/bin/env sh - -# move to the directory where the script is located -cd $(dirname "$0") - -tests_path="../../../cairo_programs" -proof_tests_path="${tests_path}/proof_programs" -exit_code=0 -trace=false -memory=false -air_public_input=false -air_private_input=false -passed_tests=0 -failed_tests=0 - -for i in $@; do - case $i in - "trace") trace=true - echo "Requested trace comparison" - ;; - "memory") memory=true - echo "Requested memory comparison" - ;; - "proof_mode") tests_path=$proof_tests_path - echo "Requested proof mode usage" - ;; - "air_public_input") air_public_input=true - echo "Requested air_public_input comparison" - ;; - "air_private_input") air_private_input=true - echo "Requested air_private_input comparison" - ;; - *) - ;; - esac -done - -if $air_public_input; then - if [ $tests_path != $proof_tests_path ]; then - echo "Can't compare air_public_input without proof_mode" - exit 1 - fi -fi - -files=$(ls $tests_path) -EXIT_CODE=$? -if [ ${EXIT_CODE} != 0 ]; then - exit ${EXIT_CODE} -fi - -for file in $(ls $tests_path | grep .cairo$ | sed -E 's/\.cairo$//'); do - path_file="$tests_path/$file" - - if $trace; then - if ! diff -q $path_file.dyn.trace $path_file.rs.dyn.trace; then - echo "Traces for $file differ" - exit_code=1 - failed_tests=$((failed_tests + 1)) - else - passed_tests=$((passed_tests + 1)) - fi - fi - - if $memory; then - if ! ./memory_comparator.py $path_file.dyn.memory $path_file.rs.dyn.memory; then - echo "Memory differs for $file" - exit_code=1 - failed_tests=$((failed_tests + 1)) - else - passed_tests=$((passed_tests + 1)) - fi - fi - - if $air_public_input; then - if ! ./air_public_input_comparator.py $path_file.dyn.air_public_input $path_file.rs.dyn.air_public_input; then - echo "Air Public Input differs for $file" - exit_code=1 - failed_tests=$((failed_tests + 1)) - else - passed_tests=$((passed_tests + 1)) - fi - fi - - if $air_private_input; then - if ! ./air_private_input_comparator.py $path_file.dyn.air_private_input $path_file.rs.dyn.air_private_input; then - echo "Air Private Input differs for $file" - exit_code=1 - failed_tests=$((failed_tests + 1)) - else - passed_tests=$((passed_tests + 1)) - fi - fi -done - -if test $failed_tests != 0; then - echo "Comparisons: $failed_tests failed, $passed_tests passed, $((failed_tests + passed_tests)) total" -elif test $passed_tests = 0; then - echo "No tests ran!" - exit_code=2 -else - echo "All $passed_tests tests passed; no discrepancies found" -fi - -exit "${exit_code}" From 5b6868edce69c6d136ae409093adee8630988fe2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Wed, 18 Sep 2024 16:49:34 -0300 Subject: [PATCH 52/68] Add script for comparing with dynamic layouts --- .../tests/compare_outputs_dynamic_layout.sh | 158 ++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100755 vm/src/tests/compare_outputs_dynamic_layout.sh diff --git a/vm/src/tests/compare_outputs_dynamic_layout.sh b/vm/src/tests/compare_outputs_dynamic_layout.sh new file mode 100755 index 0000000000..a0bad11077 --- /dev/null +++ b/vm/src/tests/compare_outputs_dynamic_layout.sh @@ -0,0 +1,158 @@ +#!/usr/bin/env bash +# +# Compares programs with different dynamic layouts against cairo-lang + +# Setup constants +COMPILED_PROGRAMS_DIR="cairo_programs/proof_programs" +TEMP_FOLDER=$(mktemp -d) + +# Build temporary dynamic layout params files +cat < "$TEMP_FOLDER/all_cairo.json" +{ + "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 +} +EOF +cat < "$TEMP_FOLDER/double_all_cairo.json" +{ + "rc_units": 8, + "log_diluted_units_per_step": 8, + "cpu_component_step": 16, + "memory_units_per_step": 16, + "uses_pedersen_builtin": true, + "pedersen_ratio": 512, + "uses_range_check_builtin": true, + "range_check_ratio": 16, + "uses_ecdsa_builtin": true, + "ecdsa_ratio": 4096, + "uses_bitwise_builtin": true, + "bitwise_ratio": 32, + "uses_ec_op_builtin": true, + "ec_op_ratio": 2048, + "uses_keccak_builtin": true, + "keccak_ratio": 4096, + "uses_poseidon_builtin": true, + "poseidon_ratio": 512, + "uses_range_check96_builtin": true, + "range_check96_ratio": 16, + "range_check96_ratio_den": 1, + "uses_add_mod_builtin": true, + "add_mod_ratio": 256, + "add_mod_ratio_den": 1, + "uses_mul_mod_builtin": true, + "mul_mod_ratio": 512, + "mul_mod_ratio_den": 1 +} +EOF + +# Build cases to execute +CASES=( + "factorial.json;all_cairo" + "fibonacci.json;all_cairo" + "factorial.json;double_all_cairo" + "fibonacci.json;double_all_cairo" +) + +passed_tests=0 +failed_tests=0 +exit_code=0 + +for case in "${CASES[@]}"; do + IFS=";" read -r program layout <<< "$case" + + full_program="$COMPILED_PROGRAMS_DIR/$program" + full_layout="$TEMP_FOLDER/$layout.json" + + # Run cairo-vm + echo "Running cairo-vm with case: $case" + cargo run -p cairo-vm-cli --release -- "$full_program" \ + --layout "dynamic" --cairo_layout_params_file "$full_layout" --proof_mode \ + --trace_file program_rs.trace --memory_file program_rs.memory --air_public_input program_rs.air_public_input --air_private_input program_rs.air_private_input + + # Run cairo-lang + echo "Running cairo-lang with case: $case" + cairo-run --program "$full_program" \ + --layout "dynamic" --cairo_layout_params_file "$full_layout" --proof_mode \ + --trace_file program_py.trace --memory_file program_py.memory --air_public_input program_py.air_public_input --air_private_input program_py.air_private_input + + # Compare trace + echo "Running trace comparison for case: $case" + if ! diff -q program_rs.trace program_py.trace; then + echo "Trace differs for case: $case" + exit_code=1 + failed_tests=$((failed_tests + 1)) + else + passed_tests=$((passed_tests + 1)) + fi + + # Compare memory + echo "Running memory comparison for case: $case" + if ! ./vm/src/tests/memory_comparator.py program_rs.memory program_py.memory; then + echo "Memory differs for case: $case" + exit_code=1 + failed_tests=$((failed_tests + 1)) + else + passed_tests=$((passed_tests + 1)) + fi + + # Compare air public input + echo "Running air public input comparison for case: $case" + if ! ./vm/src/tests/air_public_input_comparator.py program_rs.air_public_input program_py.air_public_input; then + echo "Air public input differs for case: $case" + exit_code=1 + failed_tests=$((failed_tests + 1)) + else + passed_tests=$((passed_tests + 1)) + fi + + # Compare air private input + echo "Running air private input comparison for case: $case" + if ! ./vm/src/tests/air_private_input_comparator.py program_rs.air_private_input program_py.air_private_input; then + echo "Air private input differs for case: $case" + exit_code=1 + failed_tests=$((failed_tests + 1)) + else + passed_tests=$((passed_tests + 1)) + fi + + # Clean files generated by the script + echo "Cleaning files" + rm program_rs.* + rm program_py.* +done + +if test $failed_tests != 0; then + echo "Comparisons: $failed_tests failed, $passed_tests passed, $((failed_tests + passed_tests)) total" +elif test $passed_tests = 0; then + echo "No tests ran!" + exit_code=2 +else + echo "All $passed_tests tests passed; no discrepancies found" +fi + +exit "${exit_code}" From cdab848cd9d66a8fed78e786447d5d9d0e0c2cfc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Wed, 18 Sep 2024 16:53:55 -0300 Subject: [PATCH 53/68] Add tests to workflow --- .github/workflows/rust.yml | 39 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 0efa960359..08c422fead 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -725,6 +725,45 @@ jobs: - name: Run script run: ./vm/src/tests/compare_factorial_outputs_all_layouts.sh + compare-outputs-dynamic-layout: + name: Compare factorial outputs for all 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_layout.sh + compare-run-from-cairo-pie-all-outputs: name: Compare all outputs from running Cairo PIEs needs: [ build-programs, build-release, run-cairo-release ] From 20150558877fafac88d5a6d0d496e054466abee5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Wed, 18 Sep 2024 17:03:10 -0300 Subject: [PATCH 54/68] Delete logic changes They are going to be moved to another branch --- vm/src/vm/runners/builtin_runner/mod.rs | 45 ++++--------------------- 1 file changed, 6 insertions(+), 39 deletions(-) diff --git a/vm/src/vm/runners/builtin_runner/mod.rs b/vm/src/vm/runners/builtin_runner/mod.rs index 3be5781bfa..26d4a9026c 100644 --- a/vm/src/vm/runners/builtin_runner/mod.rs +++ b/vm/src/vm/runners/builtin_runner/mod.rs @@ -170,14 +170,6 @@ impl BuiltinRunner { pub fn get_allocated_memory_units( &self, vm: &VirtualMachine, - ) -> Result { - Ok(self.get_allocated_instances(vm)? * self.cells_per_instance() as usize) - } - - ///Returns the builtin's allocated instances - pub fn get_allocated_instances( - &self, - vm: &VirtualMachine, ) -> Result { match *self { BuiltinRunner::Output(_) | BuiltinRunner::SegmentArena(_) => Ok(0), @@ -187,17 +179,12 @@ impl BuiltinRunner { // Dynamic layout has the exact number of instances it needs (up to a power of 2). let instances: usize = self.get_used_cells(&vm.segments)? / self.cells_per_instance() as usize; - let needed_components = instances / self.instances_per_component() as usize; - - let components = if needed_components > 0 { - needed_components.next_power_of_two() - } else { - 0 - }; - Ok(self.instances_per_component() as usize * components) + let components = (instances / self.instances_per_component() as usize) + .next_power_of_two(); + Ok(self.cells_per_instance() as usize + * self.instances_per_component() as usize + * components) } - // Dynamic layout allows for builtins with ratio 0 - Some(0) => Ok(0), Some(ratio) => { let min_step = (ratio * self.instances_per_component()) as usize; if vm.current_step < min_step { @@ -208,7 +195,7 @@ impl BuiltinRunner { }; let value = safe_div_usize(vm.current_step, ratio as usize) .map_err(|_| MemoryError::ErrorCalculatingMemoryUnits)?; - Ok(value) + Ok(self.cells_per_instance() as usize * value) } } } @@ -1066,26 +1053,6 @@ mod tests { assert_eq!(builtin.get_allocated_memory_units(&vm), Ok(256)); } - #[test] - #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] - fn get_allocated_memory_units_zero_ratio() { - let builtin = BuiltinRunner::Keccak(KeccakBuiltinRunner::new(Some(0), true)); - let vm = vm!(); - assert_eq!(builtin.get_allocated_memory_units(&vm), Ok(0)); - } - - #[test] - #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] - fn get_allocated_memory_units_none_ratio() { - let mut builtin = BuiltinRunner::Keccak(KeccakBuiltinRunner::new(None, true)); - let mut vm = vm!(); - - builtin.initialize_segments(&mut vm.segments); - vm.compute_segments_effective_sizes(); - - assert_eq!(builtin.get_allocated_memory_units(&vm), Ok(0)); - } - #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_range_check_usage_range_check() { From c46ed763e677d63f0fbcaeea07de641903b86103 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Wed, 18 Sep 2024 17:07:22 -0300 Subject: [PATCH 55/68] Delete more logic changes --- vm/src/vm/runners/cairo_runner.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/vm/src/vm/runners/cairo_runner.rs b/vm/src/vm/runners/cairo_runner.rs index 1da8d2731d..4bd69535a9 100644 --- a/vm/src/vm/runners/cairo_runner.rs +++ b/vm/src/vm/runners/cairo_runner.rs @@ -1,6 +1,7 @@ use crate::{ air_private_input::AirPrivateInput, air_public_input::{PublicInput, PublicInputError}, + math_utils::safe_div_usize, stdlib::{ any::Any, collections::{HashMap, HashSet}, @@ -847,7 +848,10 @@ impl CairoRunner { diluted_pool_instance.n_bits, ); - let multiplier = builtin_runner.get_allocated_instances(&self.vm)?; + let multiplier = safe_div_usize( + self.vm.current_step, + builtin_runner.ratio().unwrap_or(1) as usize, + )?; used_units_by_builtins += used_units * multiplier; } From 7a44d4e9e887cc7e61f3d82a8e045fed014c7f49 Mon Sep 17 00:00:00 2001 From: Julian Gonzalez Calderon Date: Thu, 19 Sep 2024 13:48:14 -0300 Subject: [PATCH 56/68] Update rust.yml --- .github/workflows/rust.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 08c422fead..6dde21ab95 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -725,8 +725,8 @@ jobs: - name: Run script run: ./vm/src/tests/compare_factorial_outputs_all_layouts.sh - compare-outputs-dynamic-layout: - name: Compare factorial outputs for all layouts + compare-outputs-dynamic-layouts: + name: Compare outputs with dynamic layouts needs: [ build-programs, build-release ] runs-on: ubuntu-22.04 steps: @@ -762,7 +762,7 @@ jobs: fail-on-cache-miss: true - name: Run script - run: ./vm/src/tests/compare_outputs_dynamic_layout.sh + run: ./vm/src/tests/compare_outputs_dynamic_layouts.sh compare-run-from-cairo-pie-all-outputs: name: Compare all outputs from running Cairo PIEs From 859b7fb7fe57ab3ea061e04bdb991e70aa67fb4f Mon Sep 17 00:00:00 2001 From: Julian Gonzalez Calderon Date: Thu, 19 Sep 2024 13:49:43 -0300 Subject: [PATCH 57/68] Rename compare_outputs_dynamic_layout.sh to compare_outputs_dynamic_layouts.sh --- ...tputs_dynamic_layout.sh => compare_outputs_dynamic_layouts.sh} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename vm/src/tests/{compare_outputs_dynamic_layout.sh => compare_outputs_dynamic_layouts.sh} (100%) diff --git a/vm/src/tests/compare_outputs_dynamic_layout.sh b/vm/src/tests/compare_outputs_dynamic_layouts.sh similarity index 100% rename from vm/src/tests/compare_outputs_dynamic_layout.sh rename to vm/src/tests/compare_outputs_dynamic_layouts.sh From 47e441bb1af702e21faa957107e61638365852f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Thu, 19 Sep 2024 14:07:45 -0300 Subject: [PATCH 58/68] Update test script --- vm/src/tests/compare_outputs_dynamic_layouts.sh | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/vm/src/tests/compare_outputs_dynamic_layouts.sh b/vm/src/tests/compare_outputs_dynamic_layouts.sh index a0bad11077..d7d5a8bddb 100755 --- a/vm/src/tests/compare_outputs_dynamic_layouts.sh +++ b/vm/src/tests/compare_outputs_dynamic_layouts.sh @@ -2,11 +2,9 @@ # # Compares programs with different dynamic layouts against cairo-lang -# Setup constants -COMPILED_PROGRAMS_DIR="cairo_programs/proof_programs" -TEMP_FOLDER=$(mktemp -d) # Build temporary dynamic layout params files +TEMP_FOLDER=$(mktemp -d) cat < "$TEMP_FOLDER/all_cairo.json" { "rc_units": 4, @@ -72,10 +70,10 @@ EOF # Build cases to execute CASES=( - "factorial.json;all_cairo" - "fibonacci.json;all_cairo" - "factorial.json;double_all_cairo" - "fibonacci.json;double_all_cairo" + "cairo_programs/proof_programs/factorial.json;all_cairo" + "cairo_programs/proof_programs/fibonacci.json;all_cairo" + "cairo_programs/proof_programs/factorial.json;double_all_cairo" + "cairo_programs/proof_programs/fibonacci.json;double_all_cairo" ) passed_tests=0 @@ -85,7 +83,7 @@ exit_code=0 for case in "${CASES[@]}"; do IFS=";" read -r program layout <<< "$case" - full_program="$COMPILED_PROGRAMS_DIR/$program" + full_program="$program" full_layout="$TEMP_FOLDER/$layout.json" # Run cairo-vm From 60c5679f6c315de4c9787f49bdd8e8cde506d50e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Wed, 25 Sep 2024 18:30:56 -0300 Subject: [PATCH 59/68] Add more tests --- vm/src/tests/compare_outputs_dynamic_layouts.sh | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/vm/src/tests/compare_outputs_dynamic_layouts.sh b/vm/src/tests/compare_outputs_dynamic_layouts.sh index d7d5a8bddb..c47dccb00c 100755 --- a/vm/src/tests/compare_outputs_dynamic_layouts.sh +++ b/vm/src/tests/compare_outputs_dynamic_layouts.sh @@ -71,9 +71,17 @@ EOF # Build cases to execute CASES=( "cairo_programs/proof_programs/factorial.json;all_cairo" - "cairo_programs/proof_programs/fibonacci.json;all_cairo" "cairo_programs/proof_programs/factorial.json;double_all_cairo" + "cairo_programs/proof_programs/fibonacci.json;all_cairo" "cairo_programs/proof_programs/fibonacci.json;double_all_cairo" + "cairo_programs/proof_programs/bigint.json;all_cairo" + "cairo_programs/proof_programs/bigint.json;double_all_cairo" + "cairo_programs/proof_programs/dict.json;all_cairo" + "cairo_programs/proof_programs/dict.json;double_all_cairo" + "cairo_programs/proof_programs/sha256.json;all_cairo" + "cairo_programs/proof_programs/sha256.json;double_all_cairo" + "cairo_programs/proof_programs/keccak.json;all_cairo" + "cairo_programs/proof_programs/keccak.json;double_all_cairo" ) passed_tests=0 From 1a2067a8ec9f72faa112e641326b1605bd4a9aaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Wed, 25 Sep 2024 18:35:15 -0300 Subject: [PATCH 60/68] Rename parameter for clarity --- cairo-vm-cli/src/main.rs | 2 +- cairo1-run/src/cairo_run.rs | 6 +++--- cairo1-run/src/main.rs | 2 +- vm/src/cairo_run.rs | 12 +++++++----- vm/src/tests/cairo_run_test.rs | 2 +- vm/src/vm/runners/cairo_runner.rs | 14 +++++++------- 6 files changed, 20 insertions(+), 18 deletions(-) diff --git a/cairo-vm-cli/src/main.rs b/cairo-vm-cli/src/main.rs index 74cf444810..05e68c0f52 100644 --- a/cairo-vm-cli/src/main.rs +++ b/cairo-vm-cli/src/main.rs @@ -181,7 +181,7 @@ fn run(args: impl Iterator) -> Result<(), Error> { proof_mode: args.proof_mode, secure_run: args.secure_run, allow_missing_builtins: args.allow_missing_builtins, - cairo_layout_params, + dynamic_layout_params: cairo_layout_params, ..Default::default() }; diff --git a/cairo1-run/src/cairo_run.rs b/cairo1-run/src/cairo_run.rs index fb7f020467..bf654e9724 100644 --- a/cairo1-run/src/cairo_run.rs +++ b/cairo1-run/src/cairo_run.rs @@ -86,7 +86,7 @@ pub struct Cairo1RunConfig<'a> { pub relocate_mem: bool, /// Cairo layout chosen for the run pub layout: LayoutName, - pub cairo_layout_params: Option, + pub dynamic_layout_params: Option, /// Run in proof_mode pub proof_mode: bool, /// Should be true if either air_public_input or cairo_pie_output are needed @@ -107,7 +107,7 @@ impl Default for Cairo1RunConfig<'_> { proof_mode: false, finalize_builtins: false, append_return_values: false, - cairo_layout_params: None, + dynamic_layout_params: None, } } } @@ -250,7 +250,7 @@ pub fn cairo_run_program( let mut runner = CairoRunner::new_v2( &program, cairo_run_config.layout, - cairo_run_config.cairo_layout_params.clone(), + cairo_run_config.dynamic_layout_params.clone(), runner_mode, cairo_run_config.trace_enabled, )?; diff --git a/cairo1-run/src/main.rs b/cairo1-run/src/main.rs index c4e00888d5..b2477d4da7 100644 --- a/cairo1-run/src/main.rs +++ b/cairo1-run/src/main.rs @@ -173,7 +173,7 @@ fn run(args: impl Iterator) -> Result, 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, - cairo_layout_params, + dynamic_layout_params: cairo_layout_params, }; // Try to parse the file as a sierra program diff --git a/vm/src/cairo_run.rs b/vm/src/cairo_run.rs index 41bcebb00e..0e61856a3b 100644 --- a/vm/src/cairo_run.rs +++ b/vm/src/cairo_run.rs @@ -29,7 +29,9 @@ pub struct CairoRunConfig<'a> { pub trace_enabled: bool, pub relocate_mem: bool, pub layout: LayoutName, - pub cairo_layout_params: Option, + /// The `dynamic_layout_params` argument should only be used with dynamic layout. + /// It is ignored otherwise. + pub dynamic_layout_params: Option, pub proof_mode: bool, pub secure_run: Option, pub disable_trace_padding: bool, @@ -47,7 +49,7 @@ impl<'a> Default for CairoRunConfig<'a> { secure_run: None, disable_trace_padding: false, allow_missing_builtins: None, - cairo_layout_params: None, + dynamic_layout_params: None, } } } @@ -70,7 +72,7 @@ pub fn cairo_run_program_with_initial_scope( let mut cairo_runner = CairoRunner::new( program, cairo_run_config.layout, - cairo_run_config.cairo_layout_params.clone(), + cairo_run_config.dynamic_layout_params.clone(), cairo_run_config.proof_mode, cairo_run_config.trace_enabled, )?; @@ -157,7 +159,7 @@ pub fn cairo_run_pie( let mut cairo_runner = CairoRunner::new( &program, cairo_run_config.layout, - cairo_run_config.cairo_layout_params.clone(), + cairo_run_config.dynamic_layout_params.clone(), false, cairo_run_config.trace_enabled, )?; @@ -230,7 +232,7 @@ pub fn cairo_run_fuzzed_program( let mut cairo_runner = CairoRunner::new( &program, cairo_run_config.layout, - cairo_run_config.cairo_layout_params.clone(), + cairo_run_config.dynamic_layout_params.clone(), cairo_run_config.proof_mode, cairo_run_config.trace_enabled, )?; diff --git a/vm/src/tests/cairo_run_test.rs b/vm/src/tests/cairo_run_test.rs index 7f17ea62cf..55126c98df 100644 --- a/vm/src/tests/cairo_run_test.rs +++ b/vm/src/tests/cairo_run_test.rs @@ -1182,7 +1182,7 @@ fn run_program_with_custom_mod_builtin_params( let mut cairo_runner = CairoRunner::new( &program, cairo_run_config.layout, - cairo_run_config.cairo_layout_params, + cairo_run_config.dynamic_layout_params, cairo_run_config.proof_mode, cairo_run_config.trace_enabled, ) diff --git a/vm/src/vm/runners/cairo_runner.rs b/vm/src/vm/runners/cairo_runner.rs index 4bd69535a9..3a631fee77 100644 --- a/vm/src/vm/runners/cairo_runner.rs +++ b/vm/src/vm/runners/cairo_runner.rs @@ -172,12 +172,12 @@ pub enum RunnerMode { } impl CairoRunner { - /// The `cairo_layout_params` argument should only be used with dynamic layout. + /// The `dynamic_layout_params` argument should only be used with dynamic layout. /// It is ignored otherwise. pub fn new_v2( program: &Program, layout: LayoutName, - cairo_layout_params: Option, + dynamic_layout_params: Option, mode: RunnerMode, trace_enabled: bool, ) -> Result { @@ -194,11 +194,11 @@ impl CairoRunner { LayoutName::all_solidity => CairoLayout::all_solidity_instance(), LayoutName::dynamic => { debug_assert!( - cairo_layout_params.is_some(), + dynamic_layout_params.is_some(), "cairo layout params is missing while using dynamic layout" ); - let params = cairo_layout_params.ok_or(RunnerError::BadDynamicLayoutParams( + let params = dynamic_layout_params.ok_or(RunnerError::BadDynamicLayoutParams( "cairo layout param is missing".to_string(), ))?; @@ -233,7 +233,7 @@ impl CairoRunner { pub fn new( program: &Program, layout: LayoutName, - cairo_layout_params: Option, + dynamic_layout_params: Option, proof_mode: bool, trace_enabled: bool, ) -> Result { @@ -241,7 +241,7 @@ impl CairoRunner { Self::new_v2( program, layout, - cairo_layout_params, + dynamic_layout_params, RunnerMode::ProofModeCanonical, trace_enabled, ) @@ -249,7 +249,7 @@ impl CairoRunner { Self::new_v2( program, layout, - cairo_layout_params, + dynamic_layout_params, RunnerMode::ExecutionMode, trace_enabled, ) From a20a2ab2633b568ab50230a5a5e09013fc80a635 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Wed, 25 Sep 2024 18:38:29 -0300 Subject: [PATCH 61/68] Enable mod builtin only on feature with dynamic layout --- vm/src/types/instance_definitions/builtins_instance_def.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/vm/src/types/instance_definitions/builtins_instance_def.rs b/vm/src/types/instance_definitions/builtins_instance_def.rs index d0790d223b..cf7d420636 100644 --- a/vm/src/types/instance_definitions/builtins_instance_def.rs +++ b/vm/src/types/instance_definitions/builtins_instance_def.rs @@ -219,16 +219,22 @@ impl BuiltinsInstanceDef { let range_check96 = Some(RangeCheckInstanceDef { ratio: Some(params.range_check96_ratio), }); + #[cfg(feature = "mod_builtin")] let add_mod = Some(ModInstanceDef { ratio: Some(params.add_mod_ratio), word_bit_len: 96, batch_size: 1, }); + #[cfg(feature = "mod_builtin")] let mul_mod = Some(ModInstanceDef { ratio: Some(params.mul_mod_ratio), word_bit_len: 96, batch_size: 1, }); + #[cfg(not(feature = "mod_builtin"))] + let add_mod = None; + #[cfg(not(feature = "mod_builtin"))] + let mul_mod = None; BuiltinsInstanceDef { output: true, From 7bb8dd3f5a28b0ac0928e622b38aae8d06db211b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Wed, 25 Sep 2024 18:40:29 -0300 Subject: [PATCH 62/68] Remove debug assert --- vm/src/vm/runners/cairo_runner.rs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/vm/src/vm/runners/cairo_runner.rs b/vm/src/vm/runners/cairo_runner.rs index 3a631fee77..b146b26528 100644 --- a/vm/src/vm/runners/cairo_runner.rs +++ b/vm/src/vm/runners/cairo_runner.rs @@ -193,11 +193,6 @@ impl CairoRunner { LayoutName::all_cairo => CairoLayout::all_cairo_instance(), LayoutName::all_solidity => CairoLayout::all_solidity_instance(), LayoutName::dynamic => { - debug_assert!( - dynamic_layout_params.is_some(), - "cairo layout params is missing while using dynamic layout" - ); - let params = dynamic_layout_params.ok_or(RunnerError::BadDynamicLayoutParams( "cairo layout param is missing".to_string(), ))?; From ae0f9433abcdfa309ac25eba184e7bc80db358b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Wed, 25 Sep 2024 18:48:46 -0300 Subject: [PATCH 63/68] Refactor errors into variants --- vm/src/types/layout.rs | 54 ++++++++++++++++++++++--------- vm/src/vm/errors/runner_errors.rs | 6 ++-- vm/src/vm/runners/cairo_runner.rs | 5 ++- 3 files changed, 45 insertions(+), 20 deletions(-) diff --git a/vm/src/types/layout.rs b/vm/src/types/layout.rs index 1e1f070557..4cc407efb4 100644 --- a/vm/src/types/layout.rs +++ b/vm/src/types/layout.rs @@ -1,7 +1,11 @@ -use crate::types::layout_name::LayoutName; - -use super::instance_definitions::{ - builtins_instance_def::BuiltinsInstanceDef, diluted_pool_instance_def::DilutedPoolInstanceDef, +use crate::{types::layout_name::LayoutName, vm::errors::runner_errors::RunnerError}; + +use super::{ + builtin_name::BuiltinName, + instance_definitions::{ + builtins_instance_def::BuiltinsInstanceDef, + diluted_pool_instance_def::DilutedPoolInstanceDef, + }, }; pub(crate) const MEMORY_UNITS_PER_STEP: u32 = 8; @@ -214,38 +218,58 @@ pub struct RawCairoLayoutParams { } impl TryFrom for CairoLayoutParams { - type Error = &'static str; + type Error = RunnerError; fn try_from(value: RawCairoLayoutParams) -> Result { if !value.uses_pedersen_builtin && value.pedersen_ratio != 0 { - return Err("pedersen ratio should be 0 when disabled"); + return Err(RunnerError::BadDynamicLayoutBuiltinRatio( + BuiltinName::pedersen, + )); } if !value.uses_range_check_builtin && value.range_check_ratio != 0 { - return Err("range_check ratio should be 0 when disabled"); + return Err(RunnerError::BadDynamicLayoutBuiltinRatio( + BuiltinName::range_check, + )); } if !value.uses_ecdsa_builtin && value.ecdsa_ratio != 0 { - return Err("ecdsa ratio should be 0 when disabled"); + return Err(RunnerError::BadDynamicLayoutBuiltinRatio( + BuiltinName::ecdsa, + )); } if !value.uses_bitwise_builtin && value.bitwise_ratio != 0 { - return Err("bitwise ratio should be 0 when disabled"); + return Err(RunnerError::BadDynamicLayoutBuiltinRatio( + BuiltinName::bitwise, + )); } if !value.uses_ec_op_builtin && value.ec_op_ratio != 0 { - return Err("ec_op ratio should be 0 when disabled"); + return Err(RunnerError::BadDynamicLayoutBuiltinRatio( + BuiltinName::ec_op, + )); } if !value.uses_keccak_builtin && value.keccak_ratio != 0 { - return Err("keccak ratio should be 0 when disabled"); + return Err(RunnerError::BadDynamicLayoutBuiltinRatio( + BuiltinName::keccak, + )); } if !value.uses_poseidon_builtin && value.poseidon_ratio != 0 { - return Err("poseidon ratio should be 0 when disabled"); + return Err(RunnerError::BadDynamicLayoutBuiltinRatio( + BuiltinName::poseidon, + )); } if !value.uses_range_check96_builtin && value.range_check96_ratio != 0 { - return Err("range_check96 ratio should be 0 when disabled"); + return Err(RunnerError::BadDynamicLayoutBuiltinRatio( + BuiltinName::range_check96, + )); } if !value.uses_add_mod_builtin && value.add_mod_ratio != 0 { - return Err("add_mod ratio should be 0 when disabled"); + return Err(RunnerError::BadDynamicLayoutBuiltinRatio( + BuiltinName::add_mod, + )); } if !value.uses_mul_mod_builtin && value.mul_mod_ratio != 0 { - return Err("mul_mod ratio should be 0 when disabled"); + return Err(RunnerError::BadDynamicLayoutBuiltinRatio( + BuiltinName::mul_mod, + )); } Ok(CairoLayoutParams { diff --git a/vm/src/vm/errors/runner_errors.rs b/vm/src/vm/errors/runner_errors.rs index b0edf339f3..98c0787810 100644 --- a/vm/src/vm/errors/runner_errors.rs +++ b/vm/src/vm/errors/runner_errors.rs @@ -132,8 +132,10 @@ pub enum RunnerError { CairoPieProofMode, #[error("{0}: Invalid additional data")] InvalidAdditionalData(BuiltinName), - #[error("Bad dynamic layout params: {0}")] - BadDynamicLayoutParams(String), + #[error("dynamic layout params is missing")] + MissingDynamicLayoutParams, + #[error("dynamic layout {0} ratio should be 0 when disabled")] + BadDynamicLayoutBuiltinRatio(BuiltinName), } #[cfg(test)] diff --git a/vm/src/vm/runners/cairo_runner.rs b/vm/src/vm/runners/cairo_runner.rs index b146b26528..a283885d57 100644 --- a/vm/src/vm/runners/cairo_runner.rs +++ b/vm/src/vm/runners/cairo_runner.rs @@ -193,9 +193,8 @@ impl CairoRunner { LayoutName::all_cairo => CairoLayout::all_cairo_instance(), LayoutName::all_solidity => CairoLayout::all_solidity_instance(), LayoutName::dynamic => { - let params = dynamic_layout_params.ok_or(RunnerError::BadDynamicLayoutParams( - "cairo layout param is missing".to_string(), - ))?; + let params = + dynamic_layout_params.ok_or(RunnerError::MissingDynamicLayoutParams)?; CairoLayout::dynamic_instance(params) } From 4bcdf824b78f8df505912df0e5c12da658ab2755 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Fri, 27 Sep 2024 11:50:39 -0300 Subject: [PATCH 64/68] Fix failing test --- vm/src/types/layout.rs | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/vm/src/types/layout.rs b/vm/src/types/layout.rs index 4cc407efb4..5807994458 100644 --- a/vm/src/types/layout.rs +++ b/vm/src/types/layout.rs @@ -514,22 +514,30 @@ mod tests { layout.builtins.range_check96, Some(RangeCheckInstanceDef { ratio: Some(0) }) ); - assert_eq!( - layout.builtins.mul_mod, - Some(ModInstanceDef { - ratio: Some(32), - word_bit_len: 96, // hardcoded - batch_size: 1 // hardcoded - }) - ); - assert_eq!( - layout.builtins.add_mod, - Some(ModInstanceDef { - ratio: Some(0), - word_bit_len: 96, // hardcoded - batch_size: 1 // hardcoded - }) - ); + #[cfg(feature = "mod_builtin")] + { + assert_eq!( + layout.builtins.mul_mod, + Some(ModInstanceDef { + ratio: Some(32), + word_bit_len: 96, // hardcoded + batch_size: 1 // hardcoded + }), + ); + assert_eq!( + layout.builtins.add_mod, + Some(ModInstanceDef { + ratio: Some(0), + word_bit_len: 96, // hardcoded + batch_size: 1 // hardcoded + }) + ); + } + #[cfg(not(feature = "mod_builtin"))] + { + assert_eq!(layout.builtins.mul_mod, None,); + assert_eq!(layout.builtins.add_mod, None,); + } } #[test] From 7dcbbfaa4ccc6835823ce6f921d0f2a94f6f6d76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Fri, 27 Sep 2024 11:52:13 -0300 Subject: [PATCH 65/68] Move cairo_layout_params_file to test folder --- cairo-vm-cli/src/main.rs | 2 +- cairo1-run/src/main.rs | 2 +- .../src/tests/cairo_layout_params_file.json | 0 3 files changed, 2 insertions(+), 2 deletions(-) rename cairo_layout_params_file.json => vm/src/tests/cairo_layout_params_file.json (100%) diff --git a/cairo-vm-cli/src/main.rs b/cairo-vm-cli/src/main.rs index 05e68c0f52..51cb3a649a 100644 --- a/cairo-vm-cli/src/main.rs +++ b/cairo-vm-cli/src/main.rs @@ -423,7 +423,7 @@ mod tests { 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(), + "../vm/src/tests/cairo_layout_params_file.json".to_string(), ]); args.push("../cairo_programs/proof_programs/fibonacci.json".to_string()); diff --git a/cairo1-run/src/main.rs b/cairo1-run/src/main.rs index b2477d4da7..edad364df1 100644 --- a/cairo1-run/src/main.rs +++ b/cairo1-run/src/main.rs @@ -496,7 +496,7 @@ mod tests { 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(), + "../vm/src/tests/cairo_layout_params_file.json".to_string(), ]); args.push("../cairo_programs/cairo-1-programs/fibonacci.cairo".to_string()); diff --git a/cairo_layout_params_file.json b/vm/src/tests/cairo_layout_params_file.json similarity index 100% rename from cairo_layout_params_file.json rename to vm/src/tests/cairo_layout_params_file.json From c59117af97741c4b02bbde76a436066b804fab9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Fri, 27 Sep 2024 11:55:52 -0300 Subject: [PATCH 66/68] Document cairo_layout_param_file in the README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 0618a0fcea..a7ecf920dc 100644 --- a/README.md +++ b/README.md @@ -182,6 +182,8 @@ The cairo-vm-cli supports the following optional arguments: - `run_from_cairo_pie`: Runs a Cairo PIE instead of a compiled json file. The name of the file will be the first argument received by the CLI (as if it were to run a normal compiled program). Can only be used if proof_mode is not enabled. +- `cairo_layout_params_file`: Only used with dynamic layout. Receives the name of a json file with the dynamic layout parameters. + For example, to obtain the air public inputs from a fibonacci program run, we can run : ```bash From 2aab550073d76f834cb098bfa3c7a01d15fa17d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Fri, 27 Sep 2024 12:10:18 -0300 Subject: [PATCH 67/68] Fix clippy warning --- vm/src/types/layout.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/vm/src/types/layout.rs b/vm/src/types/layout.rs index 5807994458..b1f52e7698 100644 --- a/vm/src/types/layout.rs +++ b/vm/src/types/layout.rs @@ -314,16 +314,16 @@ where #[cfg(test)] mod tests { + use super::*; + #[cfg(feature = "mod_builtin")] + use crate::types::instance_definitions::mod_instance_def::ModInstanceDef; use crate::types::instance_definitions::{ bitwise_instance_def::BitwiseInstanceDef, ec_op_instance_def::EcOpInstanceDef, ecdsa_instance_def::EcdsaInstanceDef, keccak_instance_def::KeccakInstanceDef, - mod_instance_def::ModInstanceDef, pedersen_instance_def::PedersenInstanceDef, - poseidon_instance_def::PoseidonInstanceDef, + pedersen_instance_def::PedersenInstanceDef, poseidon_instance_def::PoseidonInstanceDef, range_check_instance_def::RangeCheckInstanceDef, }; - use super::*; - #[cfg(target_arch = "wasm32")] use wasm_bindgen_test::*; From 37d83fd100c809cabd7afef541559bdc6446b1c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Fri, 27 Sep 2024 16:11:09 -0300 Subject: [PATCH 68/68] Use mod_builtin feature in tests --- vm/src/tests/compare_outputs_dynamic_layouts.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vm/src/tests/compare_outputs_dynamic_layouts.sh b/vm/src/tests/compare_outputs_dynamic_layouts.sh index c47dccb00c..7077edaa44 100755 --- a/vm/src/tests/compare_outputs_dynamic_layouts.sh +++ b/vm/src/tests/compare_outputs_dynamic_layouts.sh @@ -96,7 +96,7 @@ for case in "${CASES[@]}"; do # Run cairo-vm echo "Running cairo-vm with case: $case" - cargo run -p cairo-vm-cli --release -- "$full_program" \ + cargo run -p cairo-vm-cli --features mod_builtin --release -- "$full_program" \ --layout "dynamic" --cairo_layout_params_file "$full_layout" --proof_mode \ --trace_file program_rs.trace --memory_file program_rs.memory --air_public_input program_rs.air_public_input --air_private_input program_rs.air_private_input