From 1d23afb415ee5c9f6163bb20095f17a475f1a346 Mon Sep 17 00:00:00 2001 From: fmoletta <99273364+fmoletta@users.noreply.github.com> Date: Fri, 12 Apr 2024 18:45:30 -0300 Subject: [PATCH] refactor: Remove unused code & use constants whenever possible for builtin instance definitions (#1707) * Reduce BitwiseInstanceDef * Cleanup module * Fixes * Yeet CpuInstanceDef * Update tests * Simplify ec op instance def * Clean Ecdsa instance def * Clean Keccak instance def * Update tests * Clean Pedersen instance def * Remove instances_per_component field * Use the constants instead of storing a value for n_input cells and cells_per_instance for all builtins * Extract memory_units_per_step into a constant * Remove underscore from used field * Remove unused field _cpu_component_step * Remove unused field _n_trace_columns * Impl Default for PoseidonInstanceDef * Fix test * Fix test * fmt * Add changelog entry * Fix field name --- CHANGELOG.md | 2 + .../builtin_hint_processor/blake2s_utils.rs | 1 - .../cairo_keccak/keccak_hints.rs | 2 +- .../builtin_hint_processor/ec_utils.rs | 1 - .../field_arithmetic.rs | 1 - .../builtin_hint_processor/garaga.rs | 1 - .../builtin_hint_processor/keccak_utils.rs | 1 - .../builtin_hint_processor/poseidon_utils.rs | 1 - .../builtin_hint_processor/pow_utils.rs | 2 +- .../builtin_hint_processor/segments.rs | 1 - .../builtin_hint_processor/set.rs | 2 +- .../builtin_hint_processor/sha256_utils.rs | 1 - .../builtin_hint_processor/signature.rs | 12 +- .../builtin_hint_processor/uint256_utils.rs | 5 +- .../builtin_hint_processor/uint384.rs | 5 +- .../uint384_extension.rs | 1 - .../builtin_hint_processor/vrf/fq.rs | 1 - .../bitwise_instance_def.rs | 50 +-- .../builtins_instance_def.rs | 18 +- .../instance_definitions/cpu_instance_def.rs | 27 -- .../ec_op_instance_def.rs | 55 +--- .../ecdsa_instance_def.rs | 63 +--- .../keccak_instance_def.rs | 61 +--- vm/src/types/instance_definitions/mod.rs | 1 - .../instance_definitions/mod_instance_def.rs | 2 + .../pedersen_instance_def.rs | 76 +---- .../poseidon_instance_def.rs | 7 +- .../range_check_instance_def.rs | 6 +- vm/src/types/layout.rs | 173 +++------- vm/src/utils.rs | 2 +- vm/src/vm/runners/builtin_runner/bitwise.rs | 87 ++--- vm/src/vm/runners/builtin_runner/ec_op.rs | 73 ++--- vm/src/vm/runners/builtin_runner/hash.rs | 17 +- vm/src/vm/runners/builtin_runner/keccak.rs | 127 +++----- vm/src/vm/runners/builtin_runner/mod.rs | 303 ++++++------------ vm/src/vm/runners/builtin_runner/modulo.rs | 27 +- vm/src/vm/runners/builtin_runner/poseidon.rs | 16 +- .../vm/runners/builtin_runner/range_check.rs | 11 +- .../runners/builtin_runner/segment_arena.rs | 8 +- vm/src/vm/runners/builtin_runner/signature.rs | 79 ++--- vm/src/vm/runners/cairo_runner.rs | 53 ++- vm/src/vm/vm_core.rs | 19 +- vm/src/vm/vm_memory/memory.rs | 5 +- 43 files changed, 402 insertions(+), 1004 deletions(-) delete mode 100644 vm/src/types/instance_definitions/cpu_instance_def.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 3dac6b636f..f51c9cb46c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ #### Upcoming Changes +* refactor: Remove unused code & use constants whenever possible for builtin instance definitions[#1707](https://github.com/lambdaclass/cairo-vm/pull/1707) + * fix(BREAKING): Use program builtins in `initialize_main_entrypoint` & `read_return_values`[#1703](https://github.com/lambdaclass/cairo-vm/pull/1703) * `initialize_main_entrypoint` now iterates over the program builtins when building the stack & inserts 0 for any missing builtin * `read_return_values` now only computes the final stack of the builtins in the program diff --git a/vm/src/hint_processor/builtin_hint_processor/blake2s_utils.rs b/vm/src/hint_processor/builtin_hint_processor/blake2s_utils.rs index b095bcc69d..e56258018e 100644 --- a/vm/src/hint_processor/builtin_hint_processor/blake2s_utils.rs +++ b/vm/src/hint_processor/builtin_hint_processor/blake2s_utils.rs @@ -301,7 +301,6 @@ mod tests { hint_processor_definition::HintProcessorLogic, }, relocatable, - types::exec_scope::ExecutionScopes, utils::test_utils::*, vm::errors::memory_errors::MemoryError, }; diff --git a/vm/src/hint_processor/builtin_hint_processor/cairo_keccak/keccak_hints.rs b/vm/src/hint_processor/builtin_hint_processor/cairo_keccak/keccak_hints.rs index 0beea39ee9..202cba60a0 100644 --- a/vm/src/hint_processor/builtin_hint_processor/cairo_keccak/keccak_hints.rs +++ b/vm/src/hint_processor/builtin_hint_processor/cairo_keccak/keccak_hints.rs @@ -375,7 +375,7 @@ mod tests { }, hint_processor_definition::{HintProcessorLogic, HintReference}, }, - types::{exec_scope::ExecutionScopes, relocatable::Relocatable}, + types::relocatable::Relocatable, utils::test_utils::*, vm::vm_core::VirtualMachine, }; diff --git a/vm/src/hint_processor/builtin_hint_processor/ec_utils.rs b/vm/src/hint_processor/builtin_hint_processor/ec_utils.rs index 9530141f6d..2b8b0e7685 100644 --- a/vm/src/hint_processor/builtin_hint_processor/ec_utils.rs +++ b/vm/src/hint_processor/builtin_hint_processor/ec_utils.rs @@ -220,7 +220,6 @@ mod tests { use crate::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::HintProcessorData; use crate::hint_processor::hint_processor_definition::HintProcessorLogic; use crate::relocatable; - use crate::types::exec_scope::ExecutionScopes; use crate::types::relocatable::Relocatable; use num_traits::Zero; diff --git a/vm/src/hint_processor/builtin_hint_processor/field_arithmetic.rs b/vm/src/hint_processor/builtin_hint_processor/field_arithmetic.rs index 6f5430a387..9c4ce28fe3 100644 --- a/vm/src/hint_processor/builtin_hint_processor/field_arithmetic.rs +++ b/vm/src/hint_processor/builtin_hint_processor/field_arithmetic.rs @@ -274,7 +274,6 @@ mod tests { }, hint_processor_definition::HintProcessorLogic, }, - types::exec_scope::ExecutionScopes, utils::test_utils::*, vm::vm_core::VirtualMachine, }; diff --git a/vm/src/hint_processor/builtin_hint_processor/garaga.rs b/vm/src/hint_processor/builtin_hint_processor/garaga.rs index 37f5a33025..7c1322fff9 100644 --- a/vm/src/hint_processor/builtin_hint_processor/garaga.rs +++ b/vm/src/hint_processor/builtin_hint_processor/garaga.rs @@ -29,7 +29,6 @@ mod tests { use crate::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::BuiltinHintProcessor; use crate::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::HintProcessorData; use crate::hint_processor::hint_processor_definition::HintProcessorLogic; - use crate::types::exec_scope::ExecutionScopes; use crate::Felt252; use crate::{hint_processor::builtin_hint_processor::hint_code, utils::test_utils::*}; diff --git a/vm/src/hint_processor/builtin_hint_processor/keccak_utils.rs b/vm/src/hint_processor/builtin_hint_processor/keccak_utils.rs index 86eec72eee..69b593e36f 100644 --- a/vm/src/hint_processor/builtin_hint_processor/keccak_utils.rs +++ b/vm/src/hint_processor/builtin_hint_processor/keccak_utils.rs @@ -297,7 +297,6 @@ mod tests { }, hint_processor_definition::{HintProcessorLogic, HintReference}, }, - types::exec_scope::ExecutionScopes, utils::test_utils::*, vm::vm_core::VirtualMachine, }; diff --git a/vm/src/hint_processor/builtin_hint_processor/poseidon_utils.rs b/vm/src/hint_processor/builtin_hint_processor/poseidon_utils.rs index 02a6dd7f25..1cc19daa4d 100644 --- a/vm/src/hint_processor/builtin_hint_processor/poseidon_utils.rs +++ b/vm/src/hint_processor/builtin_hint_processor/poseidon_utils.rs @@ -57,7 +57,6 @@ mod tests { use crate::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::HintProcessorData; use crate::hint_processor::hint_processor_definition::HintProcessorLogic; use crate::hint_processor::hint_processor_definition::HintReference; - use crate::types::exec_scope::ExecutionScopes; use crate::vm::vm_core::VirtualMachine; use crate::{hint_processor::builtin_hint_processor::hint_code, utils::test_utils::*}; diff --git a/vm/src/hint_processor/builtin_hint_processor/pow_utils.rs b/vm/src/hint_processor/builtin_hint_processor/pow_utils.rs index a749f4b896..0a31277a18 100644 --- a/vm/src/hint_processor/builtin_hint_processor/pow_utils.rs +++ b/vm/src/hint_processor/builtin_hint_processor/pow_utils.rs @@ -45,7 +45,7 @@ mod tests { builtin_hint_processor::builtin_hint_processor_definition::HintProcessorData, hint_processor_definition::HintProcessorLogic, }, - types::{exec_scope::ExecutionScopes, relocatable::MaybeRelocatable}, + types::relocatable::MaybeRelocatable, utils::test_utils::*, vm::{errors::memory_errors::MemoryError, vm_core::VirtualMachine}, }; diff --git a/vm/src/hint_processor/builtin_hint_processor/segments.rs b/vm/src/hint_processor/builtin_hint_processor/segments.rs index c23c58d3d4..36576973d9 100644 --- a/vm/src/hint_processor/builtin_hint_processor/segments.rs +++ b/vm/src/hint_processor/builtin_hint_processor/segments.rs @@ -57,7 +57,6 @@ mod tests { }, hint_processor_definition::HintProcessorLogic, }, - types::exec_scope::ExecutionScopes, utils::test_utils::*, vm::vm_core::VirtualMachine, }; diff --git a/vm/src/hint_processor/builtin_hint_processor/set.rs b/vm/src/hint_processor/builtin_hint_processor/set.rs index 28576e4a9f..86fc4fdadb 100644 --- a/vm/src/hint_processor/builtin_hint_processor/set.rs +++ b/vm/src/hint_processor/builtin_hint_processor/set.rs @@ -66,7 +66,7 @@ mod tests { }, hint_processor_definition::HintProcessorLogic, }, - types::{exec_scope::ExecutionScopes, relocatable::MaybeRelocatable}, + types::relocatable::MaybeRelocatable, utils::test_utils::*, vm::vm_core::VirtualMachine, }; diff --git a/vm/src/hint_processor/builtin_hint_processor/sha256_utils.rs b/vm/src/hint_processor/builtin_hint_processor/sha256_utils.rs index 659c752036..a7e77bd42c 100644 --- a/vm/src/hint_processor/builtin_hint_processor/sha256_utils.rs +++ b/vm/src/hint_processor/builtin_hint_processor/sha256_utils.rs @@ -223,7 +223,6 @@ mod tests { }, hint_processor_definition::{HintProcessorLogic, HintReference}, }, - types::exec_scope::ExecutionScopes, utils::test_utils::*, vm::vm_core::VirtualMachine, }; diff --git a/vm/src/hint_processor/builtin_hint_processor/signature.rs b/vm/src/hint_processor/builtin_hint_processor/signature.rs index 23ffa70086..878a379b7a 100644 --- a/vm/src/hint_processor/builtin_hint_processor/signature.rs +++ b/vm/src/hint_processor/builtin_hint_processor/signature.rs @@ -52,9 +52,6 @@ mod tests { }, hint_processor_definition::HintProcessorLogic, }, - types::{ - exec_scope::ExecutionScopes, instance_definitions::ecdsa_instance_def::EcdsaInstanceDef, - }, utils::test_utils::*, vm::runners::builtin_runner::SignatureBuiltinRunner, }; @@ -67,8 +64,7 @@ mod tests { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn verify_ecdsa_signature_valid() { let mut vm = vm!(); - vm.builtin_runners = - vec![SignatureBuiltinRunner::new(&EcdsaInstanceDef::default(), true).into()]; + vm.builtin_runners = vec![SignatureBuiltinRunner::new(Some(512), true).into()]; vm.segments = segments![ ((1, 0), (0, 0)), ( @@ -94,8 +90,7 @@ mod tests { #[test] fn verify_ecdsa_signature_invalid_ecdsa_ptr() { let mut vm = vm!(); - vm.builtin_runners = - vec![SignatureBuiltinRunner::new(&EcdsaInstanceDef::default(), true).into()]; + vm.builtin_runners = vec![SignatureBuiltinRunner::new(Some(512), true).into()]; vm.segments = segments![ ((1, 0), (3, 0)), ( @@ -121,8 +116,7 @@ mod tests { #[test] fn verify_ecdsa_signature_invalid_input_cell() { let mut vm = vm!(); - vm.builtin_runners = - vec![SignatureBuiltinRunner::new(&EcdsaInstanceDef::default(), true).into()]; + vm.builtin_runners = vec![SignatureBuiltinRunner::new(Some(512), true).into()]; vm.segments = segments![ ((1, 0), (0, 3)), ( diff --git a/vm/src/hint_processor/builtin_hint_processor/uint256_utils.rs b/vm/src/hint_processor/builtin_hint_processor/uint256_utils.rs index 30fc0d6396..9c91fafa8a 100644 --- a/vm/src/hint_processor/builtin_hint_processor/uint256_utils.rs +++ b/vm/src/hint_processor/builtin_hint_processor/uint256_utils.rs @@ -507,10 +507,7 @@ mod tests { }, hint_processor_definition::HintProcessorLogic, }, - types::{ - exec_scope::ExecutionScopes, - relocatable::{MaybeRelocatable, Relocatable}, - }, + types::relocatable::{MaybeRelocatable, Relocatable}, utils::test_utils::*, vm::{errors::memory_errors::MemoryError, vm_core::VirtualMachine}, }; diff --git a/vm/src/hint_processor/builtin_hint_processor/uint384.rs b/vm/src/hint_processor/builtin_hint_processor/uint384.rs index c3a9f35683..a973fb9d3d 100644 --- a/vm/src/hint_processor/builtin_hint_processor/uint384.rs +++ b/vm/src/hint_processor/builtin_hint_processor/uint384.rs @@ -252,10 +252,7 @@ mod tests { }, hint_processor_definition::HintProcessorLogic, }, - types::{ - exec_scope::ExecutionScopes, - relocatable::{MaybeRelocatable, Relocatable}, - }, + types::relocatable::{MaybeRelocatable, Relocatable}, utils::test_utils::*, vm::{errors::memory_errors::MemoryError, vm_core::VirtualMachine}, }; diff --git a/vm/src/hint_processor/builtin_hint_processor/uint384_extension.rs b/vm/src/hint_processor/builtin_hint_processor/uint384_extension.rs index 85c3cf6af8..b7ee55bf7d 100644 --- a/vm/src/hint_processor/builtin_hint_processor/uint384_extension.rs +++ b/vm/src/hint_processor/builtin_hint_processor/uint384_extension.rs @@ -73,7 +73,6 @@ mod tests { use crate::hint_processor::builtin_hint_processor::hint_code; use crate::hint_processor::builtin_hint_processor::secp::bigint_utils::Uint768; use crate::hint_processor::hint_processor_definition::HintProcessorLogic; - use crate::types::exec_scope::ExecutionScopes; use crate::utils::test_utils::*; use assert_matches::assert_matches; diff --git a/vm/src/hint_processor/builtin_hint_processor/vrf/fq.rs b/vm/src/hint_processor/builtin_hint_processor/vrf/fq.rs index 91ca9e7f8e..c31047d8e0 100644 --- a/vm/src/hint_processor/builtin_hint_processor/vrf/fq.rs +++ b/vm/src/hint_processor/builtin_hint_processor/vrf/fq.rs @@ -123,7 +123,6 @@ mod tests { use crate::hint_processor::builtin_hint_processor::hint_code; use crate::hint_processor::hint_processor_definition::HintProcessorLogic; use crate::types::errors::math_errors::MathError; - use crate::types::exec_scope::ExecutionScopes; use crate::utils::test_utils::*; use assert_matches::assert_matches; diff --git a/vm/src/types/instance_definitions/bitwise_instance_def.rs b/vm/src/types/instance_definitions/bitwise_instance_def.rs index e59c219fd3..f5e3ce4354 100644 --- a/vm/src/types/instance_definitions/bitwise_instance_def.rs +++ b/vm/src/types/instance_definitions/bitwise_instance_def.rs @@ -2,34 +2,22 @@ use serde::Serialize; pub(crate) const CELLS_PER_BITWISE: u32 = 5; pub(crate) const INPUT_CELLS_PER_BITWISE: u32 = 2; +pub(crate) const TOTAL_N_BITS: u32 = 251; #[derive(Serialize, Clone, Debug, PartialEq)] pub(crate) struct BitwiseInstanceDef { pub(crate) ratio: Option, - pub(crate) total_n_bits: u32, } -impl BitwiseInstanceDef { - pub(crate) fn default() -> Self { - BitwiseInstanceDef { - ratio: Some(256), - total_n_bits: 251, - } +impl Default for BitwiseInstanceDef { + fn default() -> Self { + BitwiseInstanceDef { ratio: Some(256) } } +} +impl BitwiseInstanceDef { pub(crate) fn new(ratio: Option) -> Self { - BitwiseInstanceDef { - ratio, - total_n_bits: 251, - } - } - - pub(crate) fn _cells_per_builtin(&self) -> u32 { - CELLS_PER_BITWISE - } - - pub(crate) fn _range_check_units_per_builtin(&self) -> u32 { - 0 + BitwiseInstanceDef { ratio } } } @@ -40,37 +28,17 @@ mod tests { #[cfg(target_arch = "wasm32")] use wasm_bindgen_test::*; - #[test] - #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] - fn get_range_check_units_per_builtin() { - let builtin_instance = BitwiseInstanceDef::default(); - assert_eq!(builtin_instance._range_check_units_per_builtin(), 0); - } - - #[test] - #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] - fn get_cells_per_builtin() { - let builtin_instance = BitwiseInstanceDef::default(); - assert_eq!(builtin_instance._cells_per_builtin(), 5); - } - #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn test_new() { - let builtin_instance = BitwiseInstanceDef { - ratio: Some(8), - total_n_bits: 251, - }; + let builtin_instance = BitwiseInstanceDef { ratio: Some(8) }; assert_eq!(BitwiseInstanceDef::new(Some(8)), builtin_instance); } #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn test_default() { - let builtin_instance = BitwiseInstanceDef { - ratio: Some(256), - total_n_bits: 251, - }; + let builtin_instance = BitwiseInstanceDef { ratio: Some(256) }; assert_eq!(BitwiseInstanceDef::default(), builtin_instance); } } diff --git a/vm/src/types/instance_definitions/builtins_instance_def.rs b/vm/src/types/instance_definitions/builtins_instance_def.rs index b54cd3c849..a6d0bc44cc 100644 --- a/vm/src/types/instance_definitions/builtins_instance_def.rs +++ b/vm/src/types/instance_definitions/builtins_instance_def.rs @@ -6,6 +6,8 @@ use super::{ range_check_instance_def::RangeCheckInstanceDef, }; +pub(crate) const BUILTIN_INSTANCES_PER_COMPONENT: u32 = 1; + use serde::Serialize; #[derive(Serialize, Debug, PartialEq)] @@ -75,7 +77,7 @@ impl BuiltinsInstanceDef { pub(crate) fn recursive() -> BuiltinsInstanceDef { BuiltinsInstanceDef { output: true, - pedersen: Some(PedersenInstanceDef::new(Some(128), 1)), + pedersen: Some(PedersenInstanceDef::new(Some(128))), range_check: Some(RangeCheckInstanceDef::default()), ecdsa: None, bitwise: Some(BitwiseInstanceDef::new(Some(8))), @@ -91,7 +93,7 @@ impl BuiltinsInstanceDef { pub(crate) fn starknet() -> BuiltinsInstanceDef { BuiltinsInstanceDef { output: true, - pedersen: Some(PedersenInstanceDef::new(Some(32), 1)), + pedersen: Some(PedersenInstanceDef::new(Some(32))), range_check: Some(RangeCheckInstanceDef::new(Some(16))), ecdsa: Some(EcdsaInstanceDef::new(Some(2048))), bitwise: Some(BitwiseInstanceDef::new(Some(64))), @@ -107,12 +109,12 @@ impl BuiltinsInstanceDef { pub(crate) fn starknet_with_keccak() -> BuiltinsInstanceDef { BuiltinsInstanceDef { output: true, - pedersen: Some(PedersenInstanceDef::new(Some(32), 1)), + pedersen: Some(PedersenInstanceDef::new(Some(32))), range_check: Some(RangeCheckInstanceDef::new(Some(16))), ecdsa: Some(EcdsaInstanceDef::new(Some(2048))), bitwise: Some(BitwiseInstanceDef::new(Some(64))), ec_op: Some(EcOpInstanceDef::new(Some(1024))), - keccak: Some(KeccakInstanceDef::new(Some(2048), vec![200; 8])), + keccak: Some(KeccakInstanceDef::new(Some(2048))), poseidon: Some(PoseidonInstanceDef::default()), range_check96: None, add_mod: None, @@ -123,7 +125,7 @@ impl BuiltinsInstanceDef { pub(crate) fn recursive_large_output() -> BuiltinsInstanceDef { BuiltinsInstanceDef { output: true, - pedersen: Some(PedersenInstanceDef::new(Some(128), 1)), + pedersen: Some(PedersenInstanceDef::new(Some(128))), range_check: Some(RangeCheckInstanceDef::default()), ecdsa: None, bitwise: Some(BitwiseInstanceDef::new(Some(8))), @@ -139,12 +141,12 @@ impl BuiltinsInstanceDef { pub(crate) fn all_cairo() -> BuiltinsInstanceDef { BuiltinsInstanceDef { output: true, - pedersen: Some(PedersenInstanceDef::new(Some(256), 1)), + pedersen: Some(PedersenInstanceDef::new(Some(256))), range_check: Some(RangeCheckInstanceDef::default()), ecdsa: Some(EcdsaInstanceDef::new(Some(2048))), bitwise: Some(BitwiseInstanceDef::new(Some(16))), ec_op: Some(EcOpInstanceDef::new(Some(1024))), - keccak: Some(KeccakInstanceDef::new(Some(2048), vec![200; 8])), + keccak: Some(KeccakInstanceDef::new(Some(2048))), poseidon: Some(PoseidonInstanceDef::new(Some(256))), range_check96: Some(RangeCheckInstanceDef::new(Some(8))), #[cfg(feature = "mod_builtin")] @@ -177,7 +179,7 @@ impl BuiltinsInstanceDef { pub(crate) fn dynamic() -> BuiltinsInstanceDef { BuiltinsInstanceDef { output: true, - pedersen: Some(PedersenInstanceDef::new(None, 4)), + pedersen: Some(PedersenInstanceDef::new(None)), range_check: Some(RangeCheckInstanceDef::new(None)), ecdsa: Some(EcdsaInstanceDef::new(None)), bitwise: Some(BitwiseInstanceDef::new(None)), diff --git a/vm/src/types/instance_definitions/cpu_instance_def.rs b/vm/src/types/instance_definitions/cpu_instance_def.rs deleted file mode 100644 index 19d22643a1..0000000000 --- a/vm/src/types/instance_definitions/cpu_instance_def.rs +++ /dev/null @@ -1,27 +0,0 @@ -use serde::Serialize; - -#[derive(Serialize, Debug, PartialEq)] -pub(crate) struct CpuInstanceDef { - pub(crate) _safe_call: bool, -} - -impl CpuInstanceDef { - pub(crate) fn default() -> Self { - CpuInstanceDef { _safe_call: true } - } -} - -#[cfg(test)] -mod tests { - use super::CpuInstanceDef; - - #[cfg(target_arch = "wasm32")] - use wasm_bindgen_test::*; - - #[test] - #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] - fn test_default() { - let cpu_instance = CpuInstanceDef::default(); - assert!(cpu_instance._safe_call) - } -} diff --git a/vm/src/types/instance_definitions/ec_op_instance_def.rs b/vm/src/types/instance_definitions/ec_op_instance_def.rs index 75e8039dba..3f4bc6ac2a 100644 --- a/vm/src/types/instance_definitions/ec_op_instance_def.rs +++ b/vm/src/types/instance_definitions/ec_op_instance_def.rs @@ -2,37 +2,22 @@ use serde::Serialize; pub(crate) const CELLS_PER_EC_OP: u32 = 7; pub(crate) const INPUT_CELLS_PER_EC_OP: u32 = 5; +pub(crate) const SCALAR_HEIGHT: u32 = 256; #[derive(Serialize, Clone, Debug, PartialEq)] pub(crate) struct EcOpInstanceDef { pub(crate) ratio: Option, - pub(crate) scalar_height: u32, - pub(crate) _scalar_bits: u32, } -impl EcOpInstanceDef { - pub(crate) fn default() -> Self { - EcOpInstanceDef { - ratio: Some(256), - scalar_height: 256, - _scalar_bits: 252, - } +impl Default for EcOpInstanceDef { + fn default() -> Self { + EcOpInstanceDef { ratio: Some(256) } } +} +impl EcOpInstanceDef { pub(crate) fn new(ratio: Option) -> Self { - EcOpInstanceDef { - ratio, - scalar_height: 256, - _scalar_bits: 252, - } - } - - pub(crate) fn _cells_per_builtin(&self) -> u32 { - CELLS_PER_EC_OP - } - - pub(crate) fn _range_check_units_per_builtin(&self) -> u32 { - 0 + EcOpInstanceDef { ratio } } } @@ -43,39 +28,17 @@ mod tests { #[cfg(target_arch = "wasm32")] use wasm_bindgen_test::*; - #[test] - #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] - fn get_range_check_units_per_builtin() { - let builtin_instance = EcOpInstanceDef::default(); - assert_eq!(builtin_instance._range_check_units_per_builtin(), 0); - } - - #[test] - #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] - fn get_cells_per_builtin() { - let builtin_instance = EcOpInstanceDef::default(); - assert_eq!(builtin_instance._cells_per_builtin(), 7); - } - #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn test_new() { - let builtin_instance = EcOpInstanceDef { - ratio: Some(8), - scalar_height: 256, - _scalar_bits: 252, - }; + let builtin_instance = EcOpInstanceDef { ratio: Some(8) }; assert_eq!(EcOpInstanceDef::new(Some(8)), builtin_instance); } #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn test_default() { - let builtin_instance = EcOpInstanceDef { - ratio: Some(256), - scalar_height: 256, - _scalar_bits: 252, - }; + let builtin_instance = EcOpInstanceDef { ratio: Some(256) }; assert_eq!(EcOpInstanceDef::default(), builtin_instance); } } diff --git a/vm/src/types/instance_definitions/ecdsa_instance_def.rs b/vm/src/types/instance_definitions/ecdsa_instance_def.rs index 518e5f5288..42f2ad6beb 100644 --- a/vm/src/types/instance_definitions/ecdsa_instance_def.rs +++ b/vm/src/types/instance_definitions/ecdsa_instance_def.rs @@ -1,44 +1,23 @@ use serde::Serialize; pub(crate) const CELLS_PER_SIGNATURE: u32 = 2; -pub(crate) const _INPUTCELLS_PER_SIGNATURE: u32 = 2; -#[derive(Serialize, Debug, PartialEq)] +#[derive(Serialize, Clone, Debug, PartialEq)] pub(crate) struct EcdsaInstanceDef { pub(crate) ratio: Option, - pub(crate) _repetitions: u32, - pub(crate) _height: u32, - pub(crate) _n_hash_bits: u32, } -impl EcdsaInstanceDef { - pub(crate) fn default() -> Self { - EcdsaInstanceDef { - ratio: Some(512), - _repetitions: 1, - _height: 256, - _n_hash_bits: 251, - } +impl Default for EcdsaInstanceDef { + fn default() -> Self { + EcdsaInstanceDef { ratio: Some(512) } } +} +impl EcdsaInstanceDef { pub(crate) fn new(ratio: Option) -> Self { - EcdsaInstanceDef { - ratio, - _repetitions: 1, - _height: 256, - _n_hash_bits: 251, - } - } - - pub(crate) fn _cells_per_builtin(&self) -> u32 { - CELLS_PER_SIGNATURE - } - - pub(crate) fn _range_check_units_per_builtin(&self) -> u32 { - 0 + EcdsaInstanceDef { ratio } } } - #[cfg(test)] mod tests { use super::*; @@ -46,41 +25,17 @@ mod tests { #[cfg(target_arch = "wasm32")] use wasm_bindgen_test::*; - #[test] - #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] - fn get_range_check_units_per_builtin() { - let builtin_instance = EcdsaInstanceDef::default(); - assert_eq!(builtin_instance._range_check_units_per_builtin(), 0); - } - - #[test] - #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] - fn get_cells_per_builtin() { - let builtin_instance = EcdsaInstanceDef::default(); - assert_eq!(builtin_instance._cells_per_builtin(), 2); - } - #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn test_new() { - let builtin_instance = EcdsaInstanceDef { - ratio: Some(8), - _repetitions: 1, - _height: 256, - _n_hash_bits: 251, - }; + let builtin_instance = EcdsaInstanceDef { ratio: Some(8) }; assert_eq!(EcdsaInstanceDef::new(Some(8)), builtin_instance); } #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn test_default() { - let builtin_instance = EcdsaInstanceDef { - ratio: Some(512), - _repetitions: 1, - _height: 256, - _n_hash_bits: 251, - }; + let builtin_instance = EcdsaInstanceDef { ratio: Some(512) }; assert_eq!(EcdsaInstanceDef::default(), builtin_instance); } } diff --git a/vm/src/types/instance_definitions/keccak_instance_def.rs b/vm/src/types/instance_definitions/keccak_instance_def.rs index 655c361fcc..5746050067 100644 --- a/vm/src/types/instance_definitions/keccak_instance_def.rs +++ b/vm/src/types/instance_definitions/keccak_instance_def.rs @@ -1,39 +1,25 @@ use crate::stdlib::prelude::*; use serde::Serialize; +pub(crate) const INPUT_CELLS_PER_KECCAK: u32 = 8; +pub(crate) const CELLS_PER_KECCAK: u32 = 16; +pub(crate) const KECCAK_INSTANCES_PER_COMPONENT: u32 = 16; + #[derive(Serialize, Clone, Debug, PartialEq)] pub(crate) struct KeccakInstanceDef { pub(crate) ratio: Option, - pub(crate) _state_rep: Vec, - pub(crate) _instance_per_component: u32, } impl Default for KeccakInstanceDef { fn default() -> Self { - Self { - // ratio should be equal to 2 ** 11 -> 2048 - ratio: Some(2048), - _state_rep: vec![200; 8], - _instance_per_component: 16, - } + // ratio should be equal to 2 ** 11 -> 2048 + KeccakInstanceDef { ratio: Some(2048) } } } impl KeccakInstanceDef { - pub(crate) fn new(ratio: Option, _state_rep: Vec) -> Self { - Self { - ratio, - _state_rep, - ..Default::default() - } - } - - pub(crate) fn cells_per_builtin(&self) -> u32 { - 2 * self._state_rep.len() as u32 - } - - pub(crate) fn _range_check_units_per_builtin(&self) -> u32 { - 0 + pub(crate) fn new(ratio: Option) -> Self { + KeccakInstanceDef { ratio } } } @@ -44,42 +30,17 @@ mod tests { #[cfg(target_arch = "wasm32")] use wasm_bindgen_test::*; - #[test] - #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] - fn get_range_check_units_per_builtin() { - let builtin_instance = KeccakInstanceDef::default(); - assert_eq!(builtin_instance._range_check_units_per_builtin(), 0); - } - - #[test] - #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] - fn get_cells_per_builtin() { - let builtin_instance = KeccakInstanceDef::default(); - assert_eq!(builtin_instance.cells_per_builtin(), 16); - } - #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn test_new() { - let builtin_instance = KeccakInstanceDef { - ratio: Some(2048), - _state_rep: vec![200; 8], - _instance_per_component: 16, - }; - assert_eq!( - KeccakInstanceDef::new(Some(2048), vec![200; 8]), - builtin_instance - ); + let builtin_instance = KeccakInstanceDef { ratio: Some(2048) }; + assert_eq!(KeccakInstanceDef::new(Some(2048)), builtin_instance); } #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn test_default() { - let builtin_instance = KeccakInstanceDef { - ratio: Some(2048), - _state_rep: vec![200; 8], - _instance_per_component: 16, - }; + let builtin_instance = KeccakInstanceDef { ratio: Some(2048) }; assert_eq!(KeccakInstanceDef::default(), builtin_instance); } } diff --git a/vm/src/types/instance_definitions/mod.rs b/vm/src/types/instance_definitions/mod.rs index 691be29847..8f1bba2198 100644 --- a/vm/src/types/instance_definitions/mod.rs +++ b/vm/src/types/instance_definitions/mod.rs @@ -1,6 +1,5 @@ pub mod bitwise_instance_def; pub mod builtins_instance_def; -pub mod cpu_instance_def; pub mod diluted_pool_instance_def; pub mod ec_op_instance_def; pub mod ecdsa_instance_def; diff --git a/vm/src/types/instance_definitions/mod_instance_def.rs b/vm/src/types/instance_definitions/mod_instance_def.rs index 50bd8184c1..0b84077d33 100644 --- a/vm/src/types/instance_definitions/mod_instance_def.rs +++ b/vm/src/types/instance_definitions/mod_instance_def.rs @@ -2,6 +2,8 @@ use serde::Serialize; pub(crate) const N_WORDS: usize = 4; +pub(crate) const CELLS_PER_MOD: u32 = 7; + #[derive(Serialize, Debug, PartialEq, Clone)] pub(crate) struct ModInstanceDef { pub(crate) ratio: Option, diff --git a/vm/src/types/instance_definitions/pedersen_instance_def.rs b/vm/src/types/instance_definitions/pedersen_instance_def.rs index d6273f35f1..5f5bc0e564 100644 --- a/vm/src/types/instance_definitions/pedersen_instance_def.rs +++ b/vm/src/types/instance_definitions/pedersen_instance_def.rs @@ -1,48 +1,22 @@ -use num_bigint::{BigInt, Sign}; use serde::Serialize; pub(crate) const CELLS_PER_HASH: u32 = 3; pub(crate) const INPUT_CELLS_PER_HASH: u32 = 2; -#[derive(Serialize, Debug, PartialEq)] +#[derive(Serialize, Clone, Debug, PartialEq)] pub(crate) struct PedersenInstanceDef { pub(crate) ratio: Option, - pub(crate) _repetitions: u32, - pub(crate) _element_height: u32, - pub(crate) _element_bits: u32, - pub(crate) _n_inputs: u32, - pub(crate) _hash_limit: BigInt, } -impl PedersenInstanceDef { - pub(crate) fn default() -> Self { - PedersenInstanceDef { - ratio: Some(8), - _repetitions: 4, - _element_height: 256, - _element_bits: 252, - _n_inputs: 2, - _hash_limit: BigInt::new(Sign::Plus, vec![1, 0, 0, 0, 0, 0, 17, 134217728]), - } - } - - pub(crate) fn new(ratio: Option, _repetitions: u32) -> Self { - PedersenInstanceDef { - ratio, - _repetitions, - _element_height: 256, - _element_bits: 252, - _n_inputs: 2, - _hash_limit: BigInt::new(Sign::Plus, vec![1, 0, 0, 0, 0, 0, 17, 134217728]), - } - } - - pub(crate) fn _cells_per_builtin(&self) -> u32 { - CELLS_PER_HASH +impl Default for PedersenInstanceDef { + fn default() -> Self { + PedersenInstanceDef { ratio: Some(8) } } +} - pub(crate) fn _range_check_units_per_builtin(&self) -> u32 { - 0 +impl PedersenInstanceDef { + pub(crate) fn new(ratio: Option) -> Self { + PedersenInstanceDef { ratio } } } @@ -53,45 +27,17 @@ mod tests { #[cfg(target_arch = "wasm32")] use wasm_bindgen_test::*; - #[test] - #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] - fn get_range_check_units_per_builtin() { - let builtin_instance = PedersenInstanceDef::default(); - assert_eq!(builtin_instance._range_check_units_per_builtin(), 0); - } - - #[test] - #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] - fn get_cells_per_builtin() { - let builtin_instance = PedersenInstanceDef::default(); - assert_eq!(builtin_instance._cells_per_builtin(), 3); - } - #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn test_new() { - let builtin_instance = PedersenInstanceDef { - ratio: Some(10), - _repetitions: 2, - _element_height: 256, - _element_bits: 252, - _n_inputs: 2, - _hash_limit: BigInt::new(Sign::Plus, vec![1, 0, 0, 0, 0, 0, 17, 134217728]), - }; - assert_eq!(PedersenInstanceDef::new(Some(10), 2), builtin_instance); + let builtin_instance = PedersenInstanceDef { ratio: Some(10) }; + assert_eq!(PedersenInstanceDef::new(Some(10)), builtin_instance); } #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn test_default() { - let builtin_instance = PedersenInstanceDef { - ratio: Some(8), - _repetitions: 4, - _element_height: 256, - _element_bits: 252, - _n_inputs: 2, - _hash_limit: BigInt::new(Sign::Plus, vec![1, 0, 0, 0, 0, 0, 17, 134217728]), - }; + let builtin_instance = PedersenInstanceDef { ratio: Some(8) }; assert_eq!(PedersenInstanceDef::default(), builtin_instance); } } diff --git a/vm/src/types/instance_definitions/poseidon_instance_def.rs b/vm/src/types/instance_definitions/poseidon_instance_def.rs index fa478c4c3d..ae52df75fe 100644 --- a/vm/src/types/instance_definitions/poseidon_instance_def.rs +++ b/vm/src/types/instance_definitions/poseidon_instance_def.rs @@ -8,11 +8,12 @@ pub(crate) struct PoseidonInstanceDef { pub(crate) ratio: Option, } -impl PoseidonInstanceDef { - pub(crate) fn default() -> Self { +impl Default for PoseidonInstanceDef { + fn default() -> Self { PoseidonInstanceDef { ratio: Some(32) } } - +} +impl PoseidonInstanceDef { pub(crate) fn new(ratio: Option) -> Self { PoseidonInstanceDef { ratio } } diff --git a/vm/src/types/instance_definitions/range_check_instance_def.rs b/vm/src/types/instance_definitions/range_check_instance_def.rs index de9fa51da3..4524fca9bb 100644 --- a/vm/src/types/instance_definitions/range_check_instance_def.rs +++ b/vm/src/types/instance_definitions/range_check_instance_def.rs @@ -6,11 +6,13 @@ pub(crate) struct RangeCheckInstanceDef { pub(crate) ratio: Option, } -impl RangeCheckInstanceDef { - pub(crate) fn default() -> Self { +impl Default for RangeCheckInstanceDef { + fn default() -> Self { RangeCheckInstanceDef { ratio: Some(8) } } +} +impl RangeCheckInstanceDef { pub(crate) fn new(ratio: Option) -> Self { RangeCheckInstanceDef { ratio } } diff --git a/vm/src/types/layout.rs b/vm/src/types/layout.rs index f9a2f8376f..3533f817fe 100644 --- a/vm/src/types/layout.rs +++ b/vm/src/types/layout.rs @@ -1,163 +1,120 @@ use crate::stdlib::prelude::*; use super::instance_definitions::{ - builtins_instance_def::BuiltinsInstanceDef, cpu_instance_def::CpuInstanceDef, - diluted_pool_instance_def::DilutedPoolInstanceDef, + builtins_instance_def::BuiltinsInstanceDef, diluted_pool_instance_def::DilutedPoolInstanceDef, }; +pub(crate) const MEMORY_UNITS_PER_STEP: u32 = 8; + use serde::Serialize; #[derive(Serialize, Debug)] pub struct CairoLayout { - pub(crate) _name: String, - pub(crate) _cpu_component_step: u32, + pub(crate) name: String, pub(crate) rc_units: u32, pub(crate) builtins: BuiltinsInstanceDef, - pub(crate) _public_memory_fraction: u32, - pub(crate) _memory_units_per_step: u32, + pub(crate) public_memory_fraction: u32, pub(crate) diluted_pool_instance_def: Option, - pub(crate) _n_trace_colums: u32, - pub(crate) _cpu_instance_def: CpuInstanceDef, } impl CairoLayout { pub(crate) fn plain_instance() -> CairoLayout { CairoLayout { - _name: String::from("plain"), - _cpu_component_step: 1, + name: String::from("plain"), rc_units: 16, builtins: BuiltinsInstanceDef::plain(), - _public_memory_fraction: 4, - _memory_units_per_step: 8, + public_memory_fraction: 4, diluted_pool_instance_def: None, - _n_trace_colums: 8, - _cpu_instance_def: CpuInstanceDef::default(), } } pub(crate) fn small_instance() -> CairoLayout { CairoLayout { - _name: String::from("small"), - _cpu_component_step: 1, + name: String::from("small"), rc_units: 16, builtins: BuiltinsInstanceDef::small(), - _public_memory_fraction: 4, - _memory_units_per_step: 8, + public_memory_fraction: 4, diluted_pool_instance_def: None, - _n_trace_colums: 25, - _cpu_instance_def: CpuInstanceDef::default(), } } pub(crate) fn dex_instance() -> CairoLayout { CairoLayout { - _name: String::from("dex"), - _cpu_component_step: 1, + name: String::from("dex"), rc_units: 4, builtins: BuiltinsInstanceDef::dex(), - _public_memory_fraction: 4, - _memory_units_per_step: 8, + public_memory_fraction: 4, diluted_pool_instance_def: None, - _n_trace_colums: 22, - _cpu_instance_def: CpuInstanceDef::default(), } } pub(crate) fn recursive_instance() -> CairoLayout { CairoLayout { - _name: String::from("recursive"), - _cpu_component_step: 1, + name: String::from("recursive"), rc_units: 4, builtins: BuiltinsInstanceDef::recursive(), - _public_memory_fraction: 8, - _memory_units_per_step: 8, + public_memory_fraction: 8, diluted_pool_instance_def: Some(DilutedPoolInstanceDef::default()), - _n_trace_colums: 10, - _cpu_instance_def: CpuInstanceDef::default(), } } pub(crate) fn starknet_instance() -> CairoLayout { CairoLayout { - _name: String::from("starknet"), - _cpu_component_step: 1, + name: String::from("starknet"), rc_units: 4, builtins: BuiltinsInstanceDef::starknet(), - _public_memory_fraction: 8, - _memory_units_per_step: 8, + public_memory_fraction: 8, diluted_pool_instance_def: Some(DilutedPoolInstanceDef::new(2, 4, 16)), - _n_trace_colums: 10, - _cpu_instance_def: CpuInstanceDef::default(), } } pub(crate) fn starknet_with_keccak_instance() -> CairoLayout { CairoLayout { - _name: String::from("starknet_with_keccak"), - _cpu_component_step: 1, + name: String::from("starknet_with_keccak"), rc_units: 4, builtins: BuiltinsInstanceDef::starknet_with_keccak(), - _public_memory_fraction: 8, - _memory_units_per_step: 8, + public_memory_fraction: 8, diluted_pool_instance_def: Some(DilutedPoolInstanceDef::default()), - _n_trace_colums: 15, - _cpu_instance_def: CpuInstanceDef::default(), } } pub(crate) fn recursive_large_output_instance() -> CairoLayout { CairoLayout { - _name: String::from("recursive_large_output"), - _cpu_component_step: 1, + name: String::from("recursive_large_output"), rc_units: 4, builtins: BuiltinsInstanceDef::recursive_large_output(), - _public_memory_fraction: 8, - _memory_units_per_step: 8, + public_memory_fraction: 8, diluted_pool_instance_def: Some(DilutedPoolInstanceDef::default()), - _n_trace_colums: 12, - _cpu_instance_def: CpuInstanceDef::default(), } } pub(crate) fn all_cairo_instance() -> CairoLayout { CairoLayout { - _name: String::from("all_cairo"), - _cpu_component_step: 1, + name: String::from("all_cairo"), rc_units: 4, builtins: BuiltinsInstanceDef::all_cairo(), - _public_memory_fraction: 8, - _memory_units_per_step: 8, + public_memory_fraction: 8, diluted_pool_instance_def: Some(DilutedPoolInstanceDef::default()), - _n_trace_colums: 11, - _cpu_instance_def: CpuInstanceDef::default(), } } pub(crate) fn all_solidity_instance() -> CairoLayout { CairoLayout { - _name: String::from("all_solidity"), - _cpu_component_step: 1, + name: String::from("all_solidity"), rc_units: 8, builtins: BuiltinsInstanceDef::all_solidity(), - _public_memory_fraction: 8, - _memory_units_per_step: 8, + public_memory_fraction: 8, diluted_pool_instance_def: Some(DilutedPoolInstanceDef::default()), - _n_trace_colums: 27, - _cpu_instance_def: CpuInstanceDef::default(), } } pub(crate) fn dynamic_instance() -> CairoLayout { CairoLayout { - _name: String::from("dynamic"), - _cpu_component_step: 1, + name: String::from("dynamic"), rc_units: 16, builtins: BuiltinsInstanceDef::dynamic(), - _public_memory_fraction: 8, - _memory_units_per_step: 8, + public_memory_fraction: 8, diluted_pool_instance_def: Some(DilutedPoolInstanceDef::default()), - _n_trace_colums: 73, - _cpu_instance_def: CpuInstanceDef::default(), } } } @@ -174,15 +131,11 @@ mod tests { fn get_plain_instance() { let layout = CairoLayout::plain_instance(); let builtins = BuiltinsInstanceDef::plain(); - assert_eq!(&layout._name, "plain"); - assert_eq!(layout._cpu_component_step, 1); + assert_eq!(&layout.name, "plain"); assert_eq!(layout.rc_units, 16); assert_eq!(layout.builtins, builtins); - assert_eq!(layout._public_memory_fraction, 4); - assert_eq!(layout._memory_units_per_step, 8); + assert_eq!(layout.public_memory_fraction, 4); assert_eq!(layout.diluted_pool_instance_def, None); - assert_eq!(layout._n_trace_colums, 8); - assert_eq!(layout._cpu_instance_def, CpuInstanceDef::default()); } #[test] @@ -190,15 +143,11 @@ mod tests { fn get_small_instance() { let layout = CairoLayout::small_instance(); let builtins = BuiltinsInstanceDef::small(); - assert_eq!(&layout._name, "small"); - assert_eq!(layout._cpu_component_step, 1); + assert_eq!(&layout.name, "small"); assert_eq!(layout.rc_units, 16); assert_eq!(layout.builtins, builtins); - assert_eq!(layout._public_memory_fraction, 4); - assert_eq!(layout._memory_units_per_step, 8); + assert_eq!(layout.public_memory_fraction, 4); assert_eq!(layout.diluted_pool_instance_def, None); - assert_eq!(layout._n_trace_colums, 25); - assert_eq!(layout._cpu_instance_def, CpuInstanceDef::default()); } #[test] @@ -206,140 +155,108 @@ mod tests { fn get_dex_instance() { let layout = CairoLayout::dex_instance(); let builtins = BuiltinsInstanceDef::dex(); - assert_eq!(&layout._name, "dex"); - assert_eq!(layout._cpu_component_step, 1); + assert_eq!(&layout.name, "dex"); assert_eq!(layout.rc_units, 4); assert_eq!(layout.builtins, builtins); - assert_eq!(layout._public_memory_fraction, 4); - assert_eq!(layout._memory_units_per_step, 8); + assert_eq!(layout.public_memory_fraction, 4); assert_eq!(layout.diluted_pool_instance_def, None); - assert_eq!(layout._n_trace_colums, 22); - assert_eq!(layout._cpu_instance_def, CpuInstanceDef::default()); } #[test] fn get_recursive_instance() { let layout = CairoLayout::recursive_instance(); let builtins = BuiltinsInstanceDef::recursive(); - assert_eq!(&layout._name, "recursive"); - assert_eq!(layout._cpu_component_step, 1); + assert_eq!(&layout.name, "recursive"); assert_eq!(layout.rc_units, 4); assert_eq!(layout.builtins, builtins); - assert_eq!(layout._public_memory_fraction, 8); - assert_eq!(layout._memory_units_per_step, 8); + assert_eq!(layout.public_memory_fraction, 8); assert_eq!( layout.diluted_pool_instance_def, Some(DilutedPoolInstanceDef::default()) ); - assert_eq!(layout._n_trace_colums, 10); - assert_eq!(layout._cpu_instance_def, CpuInstanceDef::default()); } #[test] fn get_starknet_instance() { let layout = CairoLayout::starknet_instance(); let builtins = BuiltinsInstanceDef::starknet(); - assert_eq!(&layout._name, "starknet"); - assert_eq!(layout._cpu_component_step, 1); + assert_eq!(&layout.name, "starknet"); assert_eq!(layout.rc_units, 4); assert_eq!(layout.builtins, builtins); - assert_eq!(layout._public_memory_fraction, 8); - assert_eq!(layout._memory_units_per_step, 8); + assert_eq!(layout.public_memory_fraction, 8); assert_eq!( layout.diluted_pool_instance_def, Some(DilutedPoolInstanceDef::new(2, 4, 16)) ); - assert_eq!(layout._n_trace_colums, 10); - assert_eq!(layout._cpu_instance_def, CpuInstanceDef::default()); } #[test] fn get_starknet_with_keccak_instance() { let layout = CairoLayout::starknet_with_keccak_instance(); let builtins = BuiltinsInstanceDef::starknet_with_keccak(); - assert_eq!(&layout._name, "starknet_with_keccak"); - assert_eq!(layout._cpu_component_step, 1); + assert_eq!(&layout.name, "starknet_with_keccak"); assert_eq!(layout.rc_units, 4); assert_eq!(layout.builtins, builtins); - assert_eq!(layout._public_memory_fraction, 8); - assert_eq!(layout._memory_units_per_step, 8); + assert_eq!(layout.public_memory_fraction, 8); assert_eq!( layout.diluted_pool_instance_def, Some(DilutedPoolInstanceDef::default()) ); - assert_eq!(layout._n_trace_colums, 15); - assert_eq!(layout._cpu_instance_def, CpuInstanceDef::default()); } #[test] fn get_recursive_large_output_instance() { let layout = CairoLayout::recursive_large_output_instance(); let builtins = BuiltinsInstanceDef::recursive_large_output(); - assert_eq!(&layout._name, "recursive_large_output"); - assert_eq!(layout._cpu_component_step, 1); + assert_eq!(&layout.name, "recursive_large_output"); assert_eq!(layout.rc_units, 4); assert_eq!(layout.builtins, builtins); - assert_eq!(layout._public_memory_fraction, 8); - assert_eq!(layout._memory_units_per_step, 8); + assert_eq!(layout.public_memory_fraction, 8); assert_eq!( layout.diluted_pool_instance_def, Some(DilutedPoolInstanceDef::default()) ); - assert_eq!(layout._n_trace_colums, 12); - assert_eq!(layout._cpu_instance_def, CpuInstanceDef::default()); } #[test] fn get_all_cairo_instance() { let layout = CairoLayout::all_cairo_instance(); let builtins = BuiltinsInstanceDef::all_cairo(); - assert_eq!(&layout._name, "all_cairo"); - assert_eq!(layout._cpu_component_step, 1); + assert_eq!(&layout.name, "all_cairo"); assert_eq!(layout.rc_units, 4); assert_eq!(layout.builtins, builtins); - assert_eq!(layout._public_memory_fraction, 8); - assert_eq!(layout._memory_units_per_step, 8); + assert_eq!(layout.public_memory_fraction, 8); assert_eq!( layout.diluted_pool_instance_def, Some(DilutedPoolInstanceDef::default()) ); - assert_eq!(layout._n_trace_colums, 11); - assert_eq!(layout._cpu_instance_def, CpuInstanceDef::default()); } #[test] fn get_all_solidity_instance() { let layout = CairoLayout::all_solidity_instance(); let builtins = BuiltinsInstanceDef::all_solidity(); - assert_eq!(&layout._name, "all_solidity"); - assert_eq!(layout._cpu_component_step, 1); + assert_eq!(&layout.name, "all_solidity"); assert_eq!(layout.rc_units, 8); assert_eq!(layout.builtins, builtins); - assert_eq!(layout._public_memory_fraction, 8); - assert_eq!(layout._memory_units_per_step, 8); + assert_eq!(layout.public_memory_fraction, 8); assert_eq!( layout.diluted_pool_instance_def, Some(DilutedPoolInstanceDef::default()) ); - assert_eq!(layout._n_trace_colums, 27); - assert_eq!(layout._cpu_instance_def, CpuInstanceDef::default()); } #[test] fn get_dynamic_instance() { let layout = CairoLayout::dynamic_instance(); let builtins = BuiltinsInstanceDef::dynamic(); - assert_eq!(&layout._name, "dynamic"); - assert_eq!(layout._cpu_component_step, 1); + assert_eq!(&layout.name, "dynamic"); assert_eq!(layout.rc_units, 16); assert_eq!(layout.builtins, builtins); - assert_eq!(layout._public_memory_fraction, 8); - assert_eq!(layout._memory_units_per_step, 8); + assert_eq!(layout.public_memory_fraction, 8); assert_eq!( layout.diluted_pool_instance_def, Some(DilutedPoolInstanceDef::default()) ); - assert_eq!(layout._n_trace_colums, 73); - assert_eq!(layout._cpu_instance_def, CpuInstanceDef::default()); } } diff --git a/vm/src/utils.rs b/vm/src/utils.rs index 4ec902dcfb..adb68cbc07 100644 --- a/vm/src/utils.rs +++ b/vm/src/utils.rs @@ -440,7 +440,7 @@ pub mod test_utils { macro_rules! exec_scopes_ref { () => { - &mut ExecutionScopes::new() + &mut crate::types::exec_scope::ExecutionScopes::new() }; } pub(crate) use exec_scopes_ref; diff --git a/vm/src/vm/runners/builtin_runner/bitwise.rs b/vm/src/vm/runners/builtin_runner/bitwise.rs index 2a049690a2..e5901194db 100644 --- a/vm/src/vm/runners/builtin_runner/bitwise.rs +++ b/vm/src/vm/runners/builtin_runner/bitwise.rs @@ -3,9 +3,7 @@ use crate::stdlib::{boxed::Box, vec::Vec}; use crate::Felt252; use crate::{ types::{ - instance_definitions::bitwise_instance_def::{ - BitwiseInstanceDef, CELLS_PER_BITWISE, INPUT_CELLS_PER_BITWISE, - }, + instance_definitions::bitwise_instance_def::{CELLS_PER_BITWISE, TOTAL_N_BITS}, relocatable::{MaybeRelocatable, Relocatable}, }, vm::{ @@ -19,25 +17,17 @@ use num_integer::div_ceil; pub struct BitwiseBuiltinRunner { ratio: Option, pub base: usize, - pub(crate) cells_per_instance: u32, - pub(crate) n_input_cells: u32, - bitwise_builtin: BitwiseInstanceDef, pub(crate) stop_ptr: Option, pub(crate) included: bool, - pub(crate) instances_per_component: u32, } impl BitwiseBuiltinRunner { - pub(crate) fn new(instance_def: &BitwiseInstanceDef, included: bool) -> Self { + pub(crate) fn new(ratio: Option, included: bool) -> Self { BitwiseBuiltinRunner { base: 0, - ratio: instance_def.ratio, - cells_per_instance: CELLS_PER_BITWISE, - n_input_cells: INPUT_CELLS_PER_BITWISE, - bitwise_builtin: instance_def.clone(), + ratio, stop_ptr: None, included, - instances_per_component: 1, } } @@ -66,7 +56,7 @@ impl BitwiseBuiltinRunner { address: Relocatable, memory: &Memory, ) -> Result, RunnerError> { - let index = address.offset % self.cells_per_instance as usize; + let index = address.offset % CELLS_PER_BITWISE as usize; if index <= 1 { return Ok(None); } @@ -86,7 +76,7 @@ impl BitwiseBuiltinRunner { if limbs[3] & LEADING_BITS != 0 { return Err(RunnerError::IntegerBiggerThanPowerOfTwo(Box::new(( x_addr, - self.bitwise_builtin.total_n_bits, + TOTAL_N_BITS, *x, )))); } @@ -124,7 +114,7 @@ impl BitwiseBuiltinRunner { } pub fn get_used_diluted_check_units(&self, diluted_spacing: u32, diluted_n_bits: u32) -> usize { - let total_n_bits = self.bitwise_builtin.total_n_bits; + let total_n_bits = TOTAL_N_BITS; let mut partition = Vec::with_capacity(total_n_bits as usize); for i in (0..total_n_bits).step_by((diluted_spacing * diluted_n_bits) as usize) { for j in 0..diluted_spacing { @@ -146,7 +136,7 @@ impl BitwiseBuiltinRunner { segments: &MemorySegmentManager, ) -> Result { let used_cells = self.get_used_cells(segments)?; - Ok(div_ceil(used_cells, self.cells_per_instance as usize)) + Ok(div_ceil(used_cells, CELLS_PER_BITWISE as usize)) } pub fn air_private_input(&self, memory: &Memory) -> Vec { @@ -194,7 +184,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_used_instances() { - let builtin = BitwiseBuiltinRunner::new(&BitwiseInstanceDef::new(Some(10)), true); + let builtin = BitwiseBuiltinRunner::new(Some(10), true); let mut vm = vm!(); @@ -213,8 +203,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn final_stack() { - let mut builtin: BuiltinRunner = - BitwiseBuiltinRunner::new(&BitwiseInstanceDef::new(Some(10)), true).into(); + let mut builtin: BuiltinRunner = BitwiseBuiltinRunner::new(Some(10), true).into(); let mut vm = vm!(); @@ -238,8 +227,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn final_stack_error_stop_pointer() { - let mut builtin: BuiltinRunner = - BitwiseBuiltinRunner::new(&BitwiseInstanceDef::new(Some(10)), true).into(); + let mut builtin: BuiltinRunner = BitwiseBuiltinRunner::new(Some(10), true).into(); let mut vm = vm!(); @@ -267,8 +255,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn final_stack_error_when_notincluded() { - let mut builtin: BuiltinRunner = - BitwiseBuiltinRunner::new(&BitwiseInstanceDef::new(Some(10)), false).into(); + let mut builtin: BuiltinRunner = BitwiseBuiltinRunner::new(Some(10), false).into(); let mut vm = vm!(); @@ -292,8 +279,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn final_stack_error_non_relocatable() { - let mut builtin: BuiltinRunner = - BitwiseBuiltinRunner::new(&BitwiseInstanceDef::new(Some(10)), true).into(); + let mut builtin: BuiltinRunner = BitwiseBuiltinRunner::new(Some(10), true).into(); let mut vm = vm!(); @@ -317,8 +303,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_used_cells_and_allocated_size_test() { - let builtin: BuiltinRunner = - BitwiseBuiltinRunner::new(&BitwiseInstanceDef::new(Some(10)), true).into(); + let builtin: BuiltinRunner = BitwiseBuiltinRunner::new(Some(10), true).into(); let mut vm = vm!(); @@ -364,8 +349,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_allocated_memory_units() { - let builtin: BuiltinRunner = - BitwiseBuiltinRunner::new(&BitwiseInstanceDef::new(Some(10)), true).into(); + let builtin: BuiltinRunner = BitwiseBuiltinRunner::new(Some(10), true).into(); let mut vm = vm!(); @@ -410,7 +394,7 @@ mod tests { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn deduce_memory_cell_bitwise_for_preset_memory_valid_and() { let memory = memory![((0, 5), 10), ((0, 6), 12), ((0, 7), 0)]; - let builtin = BitwiseBuiltinRunner::new(&BitwiseInstanceDef::default(), true); + let builtin = BitwiseBuiltinRunner::new(Some(256), true); let result = builtin.deduce_memory_cell(Relocatable::from((0, 7)), &memory); assert_eq!(result, Ok(Some(MaybeRelocatable::from(Felt252::from(8))))); } @@ -419,7 +403,7 @@ mod tests { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn deduce_memory_cell_bitwise_for_preset_memory_valid_xor() { let memory = memory![((0, 5), 10), ((0, 6), 12), ((0, 8), 0)]; - let builtin = BitwiseBuiltinRunner::new(&BitwiseInstanceDef::default(), true); + let builtin = BitwiseBuiltinRunner::new(Some(256), true); let result = builtin.deduce_memory_cell(Relocatable::from((0, 8)), &memory); assert_eq!(result, Ok(Some(MaybeRelocatable::from(Felt252::from(6))))); } @@ -428,7 +412,7 @@ mod tests { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn deduce_memory_cell_bitwise_for_preset_memory_valid_or() { let memory = memory![((0, 5), 10), ((0, 6), 12), ((0, 9), 0)]; - let builtin = BitwiseBuiltinRunner::new(&BitwiseInstanceDef::default(), true); + let builtin = BitwiseBuiltinRunner::new(Some(256), true); let result = builtin.deduce_memory_cell(Relocatable::from((0, 9)), &memory); assert_eq!(result, Ok(Some(MaybeRelocatable::from(Felt252::from(14))))); } @@ -437,7 +421,7 @@ mod tests { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn deduce_memory_cell_bitwise_for_preset_memory_incorrect_offset() { let memory = memory![((0, 3), 10), ((0, 4), 12), ((0, 5), 0)]; - let builtin = BitwiseBuiltinRunner::new(&BitwiseInstanceDef::default(), true); + let builtin = BitwiseBuiltinRunner::new(Some(256), true); let result = builtin.deduce_memory_cell(Relocatable::from((0, 5)), &memory); assert_eq!(result, Ok(None)); } @@ -446,7 +430,7 @@ mod tests { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn deduce_memory_cell_bitwise_for_preset_memory_no_values_to_operate() { let memory = memory![((0, 5), 12), ((0, 7), 0)]; - let builtin = BitwiseBuiltinRunner::new(&BitwiseInstanceDef::default(), true); + let builtin = BitwiseBuiltinRunner::new(Some(256), true); let result = builtin.deduce_memory_cell(Relocatable::from((0, 5)), &memory); assert_eq!(result, Ok(None)); } @@ -454,10 +438,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_used_cells_missing_segment_used_sizes() { - let builtin = BuiltinRunner::Bitwise(BitwiseBuiltinRunner::new( - &BitwiseInstanceDef::default(), - true, - )); + let builtin = BuiltinRunner::Bitwise(BitwiseBuiltinRunner::new(Some(256), true)); let vm = vm!(); assert_eq!( @@ -469,10 +450,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_used_cells_empty() { - let builtin = BuiltinRunner::Bitwise(BitwiseBuiltinRunner::new( - &BitwiseInstanceDef::default(), - true, - )); + let builtin = BuiltinRunner::Bitwise(BitwiseBuiltinRunner::new(Some(256), true)); let mut vm = vm!(); vm.segments.segment_used_sizes = Some(vec![0]); @@ -482,10 +460,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_used_cells() { - let builtin = BuiltinRunner::Bitwise(BitwiseBuiltinRunner::new( - &BitwiseInstanceDef::default(), - true, - )); + let builtin = BuiltinRunner::Bitwise(BitwiseBuiltinRunner::new(Some(256), true)); let mut vm = vm!(); vm.segments.segment_used_sizes = Some(vec![4]); @@ -495,38 +470,28 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_used_diluted_check_units_a() { - let builtin = BuiltinRunner::Bitwise(BitwiseBuiltinRunner::new( - &BitwiseInstanceDef::default(), - true, - )); + let builtin = BuiltinRunner::Bitwise(BitwiseBuiltinRunner::new(Some(256), true)); assert_eq!(builtin.get_used_diluted_check_units(12, 2), 535); } #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_used_diluted_check_units_b() { - let builtin = BuiltinRunner::Bitwise(BitwiseBuiltinRunner::new( - &BitwiseInstanceDef::default(), - true, - )); + let builtin = BuiltinRunner::Bitwise(BitwiseBuiltinRunner::new(Some(256), true)); assert_eq!(builtin.get_used_diluted_check_units(30, 56), 150); } #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_used_diluted_check_units_c() { - let builtin = BuiltinRunner::Bitwise(BitwiseBuiltinRunner::new( - &BitwiseInstanceDef::default(), - true, - )); + let builtin = BuiltinRunner::Bitwise(BitwiseBuiltinRunner::new(Some(256), true)); assert_eq!(builtin.get_used_diluted_check_units(50, 25), 250); } #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_air_private_input() { - let builtin: BuiltinRunner = - BitwiseBuiltinRunner::new(&BitwiseInstanceDef::default(), true).into(); + let builtin: BuiltinRunner = BitwiseBuiltinRunner::new(Some(256), true).into(); let segments = segments![ ((0, 0), 0), diff --git a/vm/src/vm/runners/builtin_runner/ec_op.rs b/vm/src/vm/runners/builtin_runner/ec_op.rs index 2c9be94793..8dd50f0ffa 100644 --- a/vm/src/vm/runners/builtin_runner/ec_op.rs +++ b/vm/src/vm/runners/builtin_runner/ec_op.rs @@ -2,7 +2,7 @@ use crate::air_private_input::{PrivateInput, PrivateInputEcOp}; use crate::stdlib::{borrow::Cow, prelude::*}; use crate::stdlib::{cell::RefCell, collections::HashMap}; use crate::types::instance_definitions::ec_op_instance_def::{ - EcOpInstanceDef, CELLS_PER_EC_OP, INPUT_CELLS_PER_EC_OP, + CELLS_PER_EC_OP, INPUT_CELLS_PER_EC_OP, SCALAR_HEIGHT, }; use crate::types::relocatable::{MaybeRelocatable, Relocatable}; use crate::vm::errors::memory_errors::MemoryError; @@ -17,26 +17,18 @@ use starknet_types_core::curve::ProjectivePoint; pub struct EcOpBuiltinRunner { ratio: Option, pub base: usize, - pub(crate) cells_per_instance: u32, - pub(crate) n_input_cells: u32, - ec_op_builtin: EcOpInstanceDef, pub(crate) stop_ptr: Option, pub(crate) included: bool, - pub(crate) instances_per_component: u32, cache: RefCell>, } impl EcOpBuiltinRunner { - pub(crate) fn new(instance_def: &EcOpInstanceDef, included: bool) -> Self { + pub(crate) fn new(ratio: Option, included: bool) -> Self { EcOpBuiltinRunner { base: 0, - ratio: instance_def.ratio, - n_input_cells: INPUT_CELLS_PER_EC_OP, - cells_per_instance: CELLS_PER_EC_OP, - ec_op_builtin: instance_def.clone(), + ratio, stop_ptr: None, included, - instances_per_component: 1, cache: RefCell::new(HashMap::new()), } } @@ -115,9 +107,7 @@ impl EcOpBuiltinRunner { let beta_high: Felt252 = Felt252::from(0x6f21413efbe40de150e596d72f7a8c5_u128); let beta: Felt252 = (beta_high * (Felt252::ONE + Felt252::from(u128::MAX))) + beta_low; - let index = address - .offset - .mod_floor(&(self.cells_per_instance as usize)); + let index = address.offset.mod_floor(&(CELLS_PER_EC_OP as usize)); //Index should be an output cell if index != OUTPUT_INDICES.0 && index != OUTPUT_INDICES.1 { return Ok(None); @@ -132,8 +122,8 @@ impl EcOpBuiltinRunner { //All input cells should be filled, and be integer values //If an input cell is not filled, return None - let mut input_cells = Vec::<&Felt252>::with_capacity(self.n_input_cells as usize); - for i in 0..self.n_input_cells as usize { + let mut input_cells = Vec::<&Felt252>::with_capacity(INPUT_CELLS_PER_EC_OP as usize); + for i in 0..INPUT_CELLS_PER_EC_OP as usize { match memory.get(&(instance + i)?) { None => return Ok(None), Some(addr) => { @@ -174,7 +164,7 @@ impl EcOpBuiltinRunner { (input_cells[0].to_owned(), input_cells[1].to_owned()), (input_cells[2].to_owned(), input_cells[3].to_owned()), input_cells[4], - self.ec_op_builtin.scalar_height, + SCALAR_HEIGHT, )?; self.cache.borrow_mut().insert(x_addr, result.0); self.cache.borrow_mut().insert( @@ -182,7 +172,7 @@ impl EcOpBuiltinRunner { .map_err(|_| RunnerError::Memory(MemoryError::ExpectedInteger(Box::new(x_addr))))?, result.1, ); - match index - self.n_input_cells as usize { + match index - INPUT_CELLS_PER_EC_OP as usize { 0 => Ok(Some(MaybeRelocatable::Int(result.0))), _ => Ok(Some(MaybeRelocatable::Int(result.1))), //Default case corresponds to 1, as there are no other possible cases @@ -200,7 +190,7 @@ impl EcOpBuiltinRunner { segments: &MemorySegmentManager, ) -> Result { let used_cells = self.get_used_cells(segments)?; - Ok(div_ceil(used_cells, self.cells_per_instance as usize)) + Ok(div_ceil(used_cells, CELLS_PER_EC_OP as usize)) } pub fn format_ec_op_error( @@ -274,7 +264,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_used_instances() { - let builtin = EcOpBuiltinRunner::new(&EcOpInstanceDef::new(Some(10)), true); + let builtin = EcOpBuiltinRunner::new(Some(10), true); let mut vm = vm!(); vm.segments.segment_used_sizes = Some(vec![1]); @@ -285,8 +275,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn final_stack() { - let mut builtin: BuiltinRunner = - EcOpBuiltinRunner::new(&EcOpInstanceDef::new(Some(10)), true).into(); + let mut builtin: BuiltinRunner = EcOpBuiltinRunner::new(Some(10), true).into(); let mut vm = vm!(); @@ -310,8 +299,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn final_stack_error_stop_pointer() { - let mut builtin: BuiltinRunner = - EcOpBuiltinRunner::new(&EcOpInstanceDef::new(Some(10)), true).into(); + let mut builtin: BuiltinRunner = EcOpBuiltinRunner::new(Some(10), true).into(); let mut vm = vm!(); @@ -339,8 +327,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn final_stack_error_when_notincluded() { - let mut builtin: BuiltinRunner = - EcOpBuiltinRunner::new(&EcOpInstanceDef::new(Some(10)), false).into(); + let mut builtin: BuiltinRunner = EcOpBuiltinRunner::new(Some(10), false).into(); let mut vm = vm!(); @@ -364,8 +351,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn final_stack_error_non_relocatable() { - let mut builtin: BuiltinRunner = - EcOpBuiltinRunner::new(&EcOpInstanceDef::new(Some(10)), true).into(); + let mut builtin: BuiltinRunner = EcOpBuiltinRunner::new(Some(10), true).into(); let mut vm = vm!(); @@ -389,8 +375,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_used_cells_and_allocated_size_test() { - let builtin: BuiltinRunner = - EcOpBuiltinRunner::new(&EcOpInstanceDef::new(Some(10)), true).into(); + let builtin: BuiltinRunner = EcOpBuiltinRunner::new(Some(10), true).into(); let mut vm = vm!(); @@ -435,8 +420,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_allocated_memory_units() { - let builtin: BuiltinRunner = - EcOpBuiltinRunner::new(&EcOpInstanceDef::new(Some(10)), true).into(); + let builtin: BuiltinRunner = EcOpBuiltinRunner::new(Some(10), true).into(); let mut vm = vm!(); @@ -655,7 +639,7 @@ mod tests { ) ) ]; - let builtin = EcOpBuiltinRunner::new(&EcOpInstanceDef::default(), true); + let builtin = EcOpBuiltinRunner::new(Some(256), true); let result = builtin.deduce_memory_cell(Relocatable::from((3, 6)), &memory); assert_eq!( @@ -701,7 +685,7 @@ mod tests { ) ]; - let builtin = EcOpBuiltinRunner::new(&EcOpInstanceDef::default(), true); + let builtin = EcOpBuiltinRunner::new(Some(256), true); let result = builtin.deduce_memory_cell(Relocatable::from((3, 6)), &memory); assert_eq!(result, Ok(None)); } @@ -747,7 +731,7 @@ mod tests { ) ) ]; - let builtin = EcOpBuiltinRunner::new(&EcOpInstanceDef::default(), true); + let builtin = EcOpBuiltinRunner::new(Some(256), true); let result = builtin.deduce_memory_cell(Relocatable::from((3, 3)), &memory); assert_eq!(result, Ok(None)); @@ -788,7 +772,7 @@ mod tests { ) ) ]; - let builtin = EcOpBuiltinRunner::new(&EcOpInstanceDef::default(), true); + let builtin = EcOpBuiltinRunner::new(Some(256), true); assert_eq!( builtin.deduce_memory_cell(Relocatable::from((3, 6)), &memory), @@ -801,8 +785,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_used_cells_missing_segment_used_sizes() { - let builtin = - BuiltinRunner::EcOp(EcOpBuiltinRunner::new(&EcOpInstanceDef::default(), true)); + let builtin = BuiltinRunner::EcOp(EcOpBuiltinRunner::new(Some(256), true)); let vm = vm!(); assert_eq!( @@ -814,8 +797,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_used_cells_empty() { - let builtin = - BuiltinRunner::EcOp(EcOpBuiltinRunner::new(&EcOpInstanceDef::default(), true)); + let builtin = BuiltinRunner::EcOp(EcOpBuiltinRunner::new(Some(256), true)); let mut vm = vm!(); vm.segments.segment_used_sizes = Some(vec![0]); @@ -825,8 +807,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_used_cells() { - let builtin = - BuiltinRunner::EcOp(EcOpBuiltinRunner::new(&EcOpInstanceDef::default(), true)); + let builtin = BuiltinRunner::EcOp(EcOpBuiltinRunner::new(Some(256), true)); let mut vm = vm!(); vm.segments.segment_used_sizes = Some(vec![4]); @@ -836,15 +817,14 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn initial_stackincluded_test() { - let ec_op_builtin: BuiltinRunner = - EcOpBuiltinRunner::new(&EcOpInstanceDef::default(), true).into(); + let ec_op_builtin: BuiltinRunner = EcOpBuiltinRunner::new(Some(256), true).into(); assert_eq!(ec_op_builtin.initial_stack(), vec![mayberelocatable!(0, 0)]) } #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn initial_stack_notincluded_test() { - let ec_op_builtin = EcOpBuiltinRunner::new(&EcOpInstanceDef::default(), false); + let ec_op_builtin = EcOpBuiltinRunner::new(Some(256), false); assert_eq!(ec_op_builtin.initial_stack(), Vec::new()) } @@ -902,8 +882,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_air_private_input() { - let builtin: BuiltinRunner = - EcOpBuiltinRunner::new(&EcOpInstanceDef::default(), true).into(); + let builtin: BuiltinRunner = EcOpBuiltinRunner::new(Some(256), true).into(); let segments = segments![ ((0, 0), 0), diff --git a/vm/src/vm/runners/builtin_runner/hash.rs b/vm/src/vm/runners/builtin_runner/hash.rs index c9b437403f..a6d045ade2 100644 --- a/vm/src/vm/runners/builtin_runner/hash.rs +++ b/vm/src/vm/runners/builtin_runner/hash.rs @@ -1,9 +1,7 @@ use crate::air_private_input::{PrivateInput, PrivateInputPair}; use crate::stdlib::{cell::RefCell, prelude::*}; use crate::types::errors::math_errors::MathError; -use crate::types::instance_definitions::pedersen_instance_def::{ - CELLS_PER_HASH, INPUT_CELLS_PER_HASH, -}; +use crate::types::instance_definitions::pedersen_instance_def::CELLS_PER_HASH; use crate::types::relocatable::{MaybeRelocatable, Relocatable}; use crate::vm::errors::memory_errors::MemoryError; use crate::vm::errors::runner_errors::RunnerError; @@ -18,11 +16,8 @@ use starknet_crypto::{pedersen_hash, FieldElement}; pub struct HashBuiltinRunner { pub base: usize, ratio: Option, - pub(crate) cells_per_instance: u32, - pub(crate) n_input_cells: u32, pub(crate) stop_ptr: Option, pub(crate) included: bool, - pub(crate) instances_per_component: u32, // This act as a cache to optimize calls to deduce_memory_cell // Therefore need interior mutability // 1 at position 'n' means offset 'n' relative to base pointer @@ -35,12 +30,9 @@ impl HashBuiltinRunner { HashBuiltinRunner { base: 0, ratio, - cells_per_instance: CELLS_PER_HASH, - n_input_cells: INPUT_CELLS_PER_HASH, stop_ptr: None, verified_addresses: RefCell::new(Vec::new()), included, - instances_per_component: 1, } } @@ -69,10 +61,7 @@ impl HashBuiltinRunner { address: Relocatable, memory: &Memory, ) -> Result, RunnerError> { - if address - .offset - .mod_floor(&(self.cells_per_instance as usize)) - != 2 + if address.offset.mod_floor(&(CELLS_PER_HASH as usize)) != 2 || *self .verified_addresses .borrow() @@ -131,7 +120,7 @@ impl HashBuiltinRunner { segments: &MemorySegmentManager, ) -> Result { let used_cells = self.get_used_cells(segments)?; - Ok(div_ceil(used_cells, self.cells_per_instance as usize)) + Ok(div_ceil(used_cells, CELLS_PER_HASH as usize)) } pub fn get_additional_data(&self) -> BuiltinAdditionalData { diff --git a/vm/src/vm/runners/builtin_runner/keccak.rs b/vm/src/vm/runners/builtin_runner/keccak.rs index cbd5ad0dc5..93daf92f82 100644 --- a/vm/src/vm/runners/builtin_runner/keccak.rs +++ b/vm/src/vm/runners/builtin_runner/keccak.rs @@ -1,44 +1,43 @@ use crate::air_private_input::{PrivateInput, PrivateInputKeccakState}; use crate::math_utils::safe_div_usize; use crate::stdlib::{cell::RefCell, collections::HashMap, prelude::*}; -use crate::types::instance_definitions::keccak_instance_def::KeccakInstanceDef; +use crate::types::instance_definitions::keccak_instance_def::{ + CELLS_PER_KECCAK, INPUT_CELLS_PER_KECCAK, +}; use crate::types::relocatable::{MaybeRelocatable, Relocatable}; use crate::vm::errors::memory_errors::MemoryError; use crate::vm::errors::runner_errors::RunnerError; use crate::vm::vm_memory::memory::Memory; use crate::vm::vm_memory::memory_segments::MemorySegmentManager; use crate::Felt252; +use lazy_static::lazy_static; use num_bigint::BigUint; use num_integer::div_ceil; use super::KECCAK_BUILTIN_NAME; const KECCAK_FELT_BYTE_SIZE: usize = 25; // 200 / 8 +const BITS: u32 = 200; +lazy_static! { + static ref KECCAK_INPUT_MAX: Felt252 = Felt252::TWO.pow(BITS); +} #[derive(Debug, Clone)] pub struct KeccakBuiltinRunner { ratio: Option, pub base: usize, - pub(crate) cells_per_instance: u32, - pub(crate) n_input_cells: u32, pub(crate) stop_ptr: Option, pub(crate) included: bool, - state_rep: Vec, - pub(crate) instances_per_component: u32, cache: RefCell>, } impl KeccakBuiltinRunner { - pub(crate) fn new(instance_def: &KeccakInstanceDef, included: bool) -> Self { + pub(crate) fn new(ratio: Option, included: bool) -> Self { KeccakBuiltinRunner { base: 0, - ratio: instance_def.ratio, - n_input_cells: instance_def._state_rep.len() as u32, - cells_per_instance: instance_def.cells_per_builtin(), + ratio, stop_ptr: None, included, - instances_per_component: instance_def._instance_per_component, - state_rep: instance_def._state_rep.clone(), cache: RefCell::new(HashMap::new()), } } @@ -68,19 +67,19 @@ impl KeccakBuiltinRunner { address: Relocatable, memory: &Memory, ) -> Result, RunnerError> { - let index = address.offset % self.cells_per_instance as usize; - if index < self.n_input_cells as usize { + let index = address.offset % CELLS_PER_KECCAK as usize; + if index < INPUT_CELLS_PER_KECCAK as usize { return Ok(None); } if let Some(felt) = self.cache.borrow().get(&address) { return Ok(Some(felt.into())); } let first_input_addr = (address - index)?; - let first_output_addr = (first_input_addr + self.n_input_cells as usize)?; + let first_output_addr = (first_input_addr + INPUT_CELLS_PER_KECCAK as usize)?; let mut input_felts = vec![]; - for i in 0..self.n_input_cells as usize { + for i in 0..INPUT_CELLS_PER_KECCAK as usize { let m_index = (first_input_addr + i)?; let val = match memory.get(&m_index) { Some(value) => { @@ -90,10 +89,10 @@ impl KeccakBuiltinRunner { KECCAK_BUILTIN_NAME, (first_input_addr + i)?, ))))?; - if num >= &(Felt252::TWO.pow(self.state_rep[i])) { + if num >= &KECCAK_INPUT_MAX { return Err(RunnerError::IntegerBiggerThanPowerOfTwo(Box::new(( (first_input_addr + i)?, - self.state_rep[i], + BITS, *num, )))); } @@ -114,8 +113,8 @@ impl KeccakBuiltinRunner { let keccak_result = Self::keccak_f(&input_message)?; let mut start_index = 0_usize; - for (i, bits) in self.state_rep.iter().enumerate() { - let end_index = start_index + *bits as usize / 8; + for i in 0..INPUT_CELLS_PER_KECCAK { + let end_index = start_index + BITS as usize / 8; self.cache.borrow_mut().insert((first_output_addr + i)?, { let mut bytes = keccak_result[start_index..end_index].to_vec(); bytes.resize(32, 0); @@ -137,7 +136,7 @@ impl KeccakBuiltinRunner { segments: &MemorySegmentManager, ) -> Result { let used_cells = self.get_used_cells(segments)?; - Ok(div_ceil(used_cells, self.cells_per_instance as usize)) + Ok(div_ceil(used_cells, CELLS_PER_KECCAK as usize)) } pub fn get_used_diluted_check_units(&self, diluted_n_bits: u32) -> usize { @@ -169,7 +168,7 @@ impl KeccakBuiltinRunner { if let Some(segment) = memory.data.get(self.base) { let segment_len = segment.len(); for (index, off) in (0..segment_len) - .step_by(self.cells_per_instance as usize) + .step_by(CELLS_PER_KECCAK as usize) .enumerate() { // Add the input cells of each keccak instance to the private inputs @@ -231,8 +230,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_used_instances() { - let builtin: BuiltinRunner = - KeccakBuiltinRunner::new(&KeccakInstanceDef::new(Some(10), vec![200; 8]), true).into(); + let builtin: BuiltinRunner = KeccakBuiltinRunner::new(Some(10), true).into(); let mut vm = vm!(); vm.segments.segment_used_sizes = Some(vec![1]); @@ -243,8 +241,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn final_stack() { - let mut builtin: BuiltinRunner = - KeccakBuiltinRunner::new(&KeccakInstanceDef::new(Some(10), vec![200; 8]), true).into(); + let mut builtin: BuiltinRunner = KeccakBuiltinRunner::new(Some(10), true).into(); let mut vm = vm!(); @@ -268,8 +265,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn final_stack_error_stop_pointer() { - let mut builtin: BuiltinRunner = - KeccakBuiltinRunner::new(&KeccakInstanceDef::new(Some(10), vec![200; 8]), true).into(); + let mut builtin: BuiltinRunner = KeccakBuiltinRunner::new(Some(10), true).into(); let mut vm = vm!(); @@ -296,8 +292,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn final_stack_error_when_not_included() { - let mut builtin: BuiltinRunner = - KeccakBuiltinRunner::new(&KeccakInstanceDef::new(Some(10), vec![200; 8]), false).into(); + let mut builtin: BuiltinRunner = KeccakBuiltinRunner::new(Some(10), false).into(); let mut vm = vm!(); @@ -321,8 +316,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn final_stack_error_non_relocatable() { - let mut builtin: BuiltinRunner = - KeccakBuiltinRunner::new(&KeccakInstanceDef::new(Some(10), vec![200; 8]), true).into(); + let mut builtin: BuiltinRunner = KeccakBuiltinRunner::new(Some(10), true).into(); let mut vm = vm!(); @@ -346,8 +340,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_used_cells_and_allocated_size_test() { - let builtin: BuiltinRunner = - KeccakBuiltinRunner::new(&KeccakInstanceDef::new(Some(10), vec![200; 8]), true).into(); + let builtin: BuiltinRunner = KeccakBuiltinRunner::new(Some(10), true).into(); let mut vm = vm!(); @@ -377,8 +370,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_allocated_memory_units() { - let builtin: BuiltinRunner = - KeccakBuiltinRunner::new(&KeccakInstanceDef::new(Some(10), vec![200; 8]), true).into(); + let builtin: BuiltinRunner = KeccakBuiltinRunner::new(Some(10), true).into(); let mut vm = vm!(); vm.current_step = 160; @@ -389,8 +381,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_used_cells_missing_segment_used_sizes() { - let builtin: BuiltinRunner = - KeccakBuiltinRunner::new(&KeccakInstanceDef::default(), true).into(); + let builtin: BuiltinRunner = KeccakBuiltinRunner::new(Some(2048), true).into(); let vm = vm!(); assert_eq!( @@ -402,8 +393,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_used_cells_empty() { - let builtin: BuiltinRunner = - KeccakBuiltinRunner::new(&KeccakInstanceDef::default(), true).into(); + let builtin: BuiltinRunner = KeccakBuiltinRunner::new(Some(2048), true).into(); let mut vm = vm!(); vm.segments.segment_used_sizes = Some(vec![0]); @@ -413,8 +403,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_used_cells() { - let builtin: BuiltinRunner = - KeccakBuiltinRunner::new(&KeccakInstanceDef::default(), true).into(); + let builtin: BuiltinRunner = KeccakBuiltinRunner::new(Some(2048), true).into(); let mut vm = vm!(); vm.segments.segment_used_sizes = Some(vec![4]); @@ -424,7 +413,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn initial_stackincluded_test() { - let keccak_builtin = KeccakBuiltinRunner::new(&KeccakInstanceDef::default(), true); + let keccak_builtin = KeccakBuiltinRunner::new(Some(2048), true); assert_eq!( keccak_builtin.initial_stack(), vec![mayberelocatable!(0, 0)] @@ -434,7 +423,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn initial_stack_notincluded_test() { - let keccak_builtin = KeccakBuiltinRunner::new(&KeccakInstanceDef::default(), false); + let keccak_builtin = KeccakBuiltinRunner::new(Some(2048), false); assert_eq!(keccak_builtin.initial_stack(), Vec::new()) } @@ -463,7 +452,7 @@ mod tests { ((0, 34), 0), ((0, 35), 0) ]; - let builtin = KeccakBuiltinRunner::new(&KeccakInstanceDef::default(), true); + let builtin = KeccakBuiltinRunner::new(Some(2048), true); let result = builtin.deduce_memory_cell(Relocatable::from((0, 25)), &memory); assert_eq!( @@ -484,7 +473,7 @@ mod tests { ((0, 7), 120), ((0, 8), 52) ]; - let builtin = KeccakBuiltinRunner::new(&KeccakInstanceDef::default(), true); + let builtin = KeccakBuiltinRunner::new(Some(2048), true); let result = builtin.deduce_memory_cell(Relocatable::from((0, 1)), &memory); assert_eq!(result, Ok(None)); } @@ -493,7 +482,7 @@ mod tests { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn deduce_memory_cell_offset_lt_input_cell_length_none() { let memory = memory![((0, 4), 32)]; - let builtin = KeccakBuiltinRunner::new(&KeccakInstanceDef::default(), true); + let builtin = KeccakBuiltinRunner::new(Some(2048), true); let result = builtin.deduce_memory_cell(Relocatable::from((0, 2)), &memory); assert_eq!(result, Ok(None)); } @@ -503,12 +492,9 @@ mod tests { fn deduce_memory_cell_expected_integer() { let memory = memory![((0, 0), (1, 2))]; - let mut builtin = KeccakBuiltinRunner::new(&KeccakInstanceDef::default(), true); - - builtin.n_input_cells = 1; - builtin.cells_per_instance = 100; + let builtin = KeccakBuiltinRunner::new(Some(2048), true); - let result = builtin.deduce_memory_cell(Relocatable::from((0, 1)), &memory); + let result = builtin.deduce_memory_cell(Relocatable::from((0, 9)), &memory); assert_eq!( result, @@ -524,37 +510,19 @@ mod tests { fn deduce_memory_cell_missing_input_cells() { let memory = memory![((0, 1), (1, 2))]; - let mut builtin = KeccakBuiltinRunner::new(&KeccakInstanceDef::default(), true); - - builtin.n_input_cells = 1; - builtin.cells_per_instance = 100; + let builtin = KeccakBuiltinRunner::new(Some(2048), true); let result = builtin.deduce_memory_cell(Relocatable::from((0, 1)), &memory); assert_eq!(result, Ok(None)); } - #[test] - #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] - fn deduce_memory_cell_input_cell() { - let memory = memory![((0, 0), (1, 2))]; - - let mut builtin = KeccakBuiltinRunner::new(&KeccakInstanceDef::default(), true); - - builtin.n_input_cells = 1; - builtin.cells_per_instance = 100; - - let result = builtin.deduce_memory_cell(Relocatable::from((0, 0)), &memory); - - assert_eq!(result, Ok(None)); - } - #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn deduce_memory_cell_get_memory_err() { let memory = memory![((0, 35), 0)]; - let builtin = KeccakBuiltinRunner::new(&KeccakInstanceDef::default(), true); + let builtin = KeccakBuiltinRunner::new(Some(2048), true); let result = builtin.deduce_memory_cell(Relocatable::from((0, 15)), &memory); @@ -564,8 +532,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn deduce_memory_cell_memory_int_larger_than_bits() { - let memory = memory![ - ((0, 16), 43), + let mut memory = memory![ ((0, 17), 199), ((0, 18), 0), ((0, 19), 0), @@ -587,8 +554,9 @@ mod tests { ((0, 35), 0) ]; - let keccak_instance = KeccakInstanceDef::new(Some(2048), vec![1; 8]); - let builtin = KeccakBuiltinRunner::new(&keccak_instance, true); + memory.insert((0, 16).into(), Felt252::MAX).unwrap(); + + let builtin = KeccakBuiltinRunner::new(Some(2048), true); let result = builtin.deduce_memory_cell(Relocatable::from((0, 25)), &memory); @@ -596,8 +564,8 @@ mod tests { result, Err(RunnerError::IntegerBiggerThanPowerOfTwo(Box::new(( (0, 16).into(), - 1, - 43.into() + BITS, + Felt252::MAX )))) ); } @@ -605,7 +573,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_used_diluted_check_units_result() { - let builtin = KeccakBuiltinRunner::new(&KeccakInstanceDef::default(), true); + let builtin = KeccakBuiltinRunner::new(Some(2048), true); let result: usize = builtin.get_used_diluted_check_units(16); @@ -623,8 +591,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_air_private_input() { - let builtin: BuiltinRunner = - KeccakBuiltinRunner::new(&KeccakInstanceDef::default(), true).into(); + let builtin: BuiltinRunner = KeccakBuiltinRunner::new(Some(2048), true).into(); let segments = segments![ ((0, 0), 0), diff --git a/vm/src/vm/runners/builtin_runner/mod.rs b/vm/src/vm/runners/builtin_runner/mod.rs index 75cc0c8983..e034ed46e4 100644 --- a/vm/src/vm/runners/builtin_runner/mod.rs +++ b/vm/src/vm/runners/builtin_runner/mod.rs @@ -2,6 +2,25 @@ use crate::air_private_input::PrivateInput; use crate::math_utils::safe_div_usize; use crate::serde::deserialize_program::BuiltinName; use crate::stdlib::prelude::*; +use crate::types::instance_definitions::bitwise_instance_def::{ + CELLS_PER_BITWISE, INPUT_CELLS_PER_BITWISE, +}; +use crate::types::instance_definitions::builtins_instance_def::BUILTIN_INSTANCES_PER_COMPONENT; +use crate::types::instance_definitions::ec_op_instance_def::{ + CELLS_PER_EC_OP, INPUT_CELLS_PER_EC_OP, +}; +use crate::types::instance_definitions::ecdsa_instance_def::CELLS_PER_SIGNATURE; +use crate::types::instance_definitions::keccak_instance_def::{ + CELLS_PER_KECCAK, INPUT_CELLS_PER_KECCAK, KECCAK_INSTANCES_PER_COMPONENT, +}; +use crate::types::instance_definitions::mod_instance_def::CELLS_PER_MOD; +use crate::types::instance_definitions::pedersen_instance_def::{ + CELLS_PER_HASH, INPUT_CELLS_PER_HASH, +}; +use crate::types::instance_definitions::poseidon_instance_def::{ + CELLS_PER_POSEIDON, INPUT_CELLS_PER_POSEIDON, +}; +use crate::types::instance_definitions::range_check_instance_def::CELLS_PER_RANGE_CHECK; use crate::types::relocatable::{MaybeRelocatable, Relocatable}; use crate::vm::errors::memory_errors::{self, InsufficientAllocatedCellsError, MemoryError}; use crate::vm::errors::runner_errors::RunnerError; @@ -22,6 +41,7 @@ mod segment_arena; mod signature; pub(crate) use self::range_check::{RC_N_PARTS_96, RC_N_PARTS_STANDARD}; +use self::segment_arena::ARENA_BUILTIN_SIZE; pub use bitwise::BitwiseBuiltinRunner; pub use ec_op::EcOpBuiltinRunner; pub use hash::HashBuiltinRunner; @@ -351,49 +371,38 @@ impl BuiltinRunner { fn cells_per_instance(&self) -> u32 { match self { - BuiltinRunner::Bitwise(builtin) => builtin.cells_per_instance, - BuiltinRunner::EcOp(builtin) => builtin.cells_per_instance, - BuiltinRunner::Hash(builtin) => builtin.cells_per_instance, - BuiltinRunner::RangeCheck(builtin) => builtin.cells_per_instance, - BuiltinRunner::RangeCheck96(builtin) => builtin.cells_per_instance, + BuiltinRunner::Bitwise(_) => CELLS_PER_BITWISE, + BuiltinRunner::EcOp(_) => CELLS_PER_EC_OP, + BuiltinRunner::Hash(_) => CELLS_PER_HASH, + BuiltinRunner::RangeCheck(_) | BuiltinRunner::RangeCheck96(_) => CELLS_PER_RANGE_CHECK, BuiltinRunner::Output(_) => 0, - BuiltinRunner::Keccak(builtin) => builtin.cells_per_instance, - BuiltinRunner::Signature(builtin) => builtin.cells_per_instance, - BuiltinRunner::Poseidon(builtin) => builtin.cells_per_instance, - BuiltinRunner::SegmentArena(builtin) => builtin.cells_per_instance, - BuiltinRunner::Mod(mod_builtin) => mod_builtin.cells_per_instance(), + BuiltinRunner::Keccak(_) => CELLS_PER_KECCAK, + BuiltinRunner::Signature(_) => CELLS_PER_SIGNATURE, + BuiltinRunner::Poseidon(_) => CELLS_PER_POSEIDON, + BuiltinRunner::SegmentArena(_) => ARENA_BUILTIN_SIZE, + BuiltinRunner::Mod(_) => CELLS_PER_MOD, } } fn n_input_cells(&self) -> u32 { match self { - BuiltinRunner::Bitwise(builtin) => builtin.n_input_cells, - BuiltinRunner::EcOp(builtin) => builtin.n_input_cells, - BuiltinRunner::Hash(builtin) => builtin.n_input_cells, - BuiltinRunner::RangeCheck(builtin) => builtin.n_input_cells, - BuiltinRunner::RangeCheck96(builtin) => builtin.n_input_cells, + BuiltinRunner::Bitwise(_) => INPUT_CELLS_PER_BITWISE, + BuiltinRunner::EcOp(_) => INPUT_CELLS_PER_EC_OP, + BuiltinRunner::Hash(_) => INPUT_CELLS_PER_HASH, + BuiltinRunner::RangeCheck(_) | BuiltinRunner::RangeCheck96(_) => CELLS_PER_RANGE_CHECK, BuiltinRunner::Output(_) => 0, - BuiltinRunner::Keccak(builtin) => builtin.n_input_cells, - BuiltinRunner::Signature(builtin) => builtin.n_input_cells, - BuiltinRunner::Poseidon(builtin) => builtin.n_input_cells, - BuiltinRunner::SegmentArena(builtin) => builtin.n_input_cells_per_instance, - BuiltinRunner::Mod(builtin) => builtin.n_input_cells(), + BuiltinRunner::Keccak(_) => INPUT_CELLS_PER_KECCAK, + BuiltinRunner::Signature(_) => CELLS_PER_SIGNATURE, + BuiltinRunner::Poseidon(_) => INPUT_CELLS_PER_POSEIDON, + BuiltinRunner::SegmentArena(_) => ARENA_BUILTIN_SIZE, + BuiltinRunner::Mod(_) => CELLS_PER_MOD, } } fn instances_per_component(&self) -> u32 { match self { - BuiltinRunner::Bitwise(builtin) => builtin.instances_per_component, - BuiltinRunner::EcOp(builtin) => builtin.instances_per_component, - BuiltinRunner::Hash(builtin) => builtin.instances_per_component, - BuiltinRunner::RangeCheck(builtin) => builtin.instances_per_component, - BuiltinRunner::RangeCheck96(builtin) => builtin.instances_per_component, - BuiltinRunner::Output(_) | BuiltinRunner::SegmentArena(_) => 1, - BuiltinRunner::Keccak(builtin) => builtin.instances_per_component, - BuiltinRunner::Signature(builtin) => builtin.instances_per_component, - BuiltinRunner::Poseidon(builtin) => builtin.instances_per_component, - // TODO: Placeholder till we see layout data - BuiltinRunner::Mod(_) => 1, + BuiltinRunner::Keccak(_) => KECCAK_INSTANCES_PER_COMPONENT, + _ => BUILTIN_INSTANCES_PER_COMPONENT, } } @@ -653,18 +662,10 @@ mod tests { use crate::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::BuiltinHintProcessor; use crate::relocatable; use crate::serde::deserialize_program::BuiltinName; - use crate::types::instance_definitions::ecdsa_instance_def::EcdsaInstanceDef; - use crate::types::instance_definitions::keccak_instance_def::KeccakInstanceDef; use crate::types::program::Program; use crate::vm::errors::memory_errors::InsufficientAllocatedCellsError; use crate::vm::runners::cairo_runner::CairoRunner; - use crate::{ - types::instance_definitions::{ - bitwise_instance_def::BitwiseInstanceDef, ec_op_instance_def::EcOpInstanceDef, - }, - utils::test_utils::*, - vm::vm_core::VirtualMachine, - }; + use crate::{utils::test_utils::*, vm::vm_core::VirtualMachine}; use assert_matches::assert_matches; #[cfg(target_arch = "wasm32")] @@ -673,9 +674,9 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_n_input_cells_bitwise() { - let bitwise = BitwiseBuiltinRunner::new(&BitwiseInstanceDef::new(Some(10)), true); + let bitwise = BitwiseBuiltinRunner::new(Some(10), true); let builtin: BuiltinRunner = bitwise.clone().into(); - assert_eq!(bitwise.n_input_cells, builtin.n_input_cells()) + assert_eq!(INPUT_CELLS_PER_BITWISE, builtin.n_input_cells()) } #[test] @@ -683,31 +684,23 @@ mod tests { fn get_n_input_cells_hash() { let hash = HashBuiltinRunner::new(Some(10), true); let builtin: BuiltinRunner = hash.clone().into(); - assert_eq!(hash.n_input_cells, builtin.n_input_cells()) - } - - #[test] - #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] - fn get_n_input_cells_range_check() { - let range_check = RangeCheckBuiltinRunner::::new(Some(10), true); - let builtin: BuiltinRunner = range_check.clone().into(); - assert_eq!(range_check.n_input_cells, builtin.n_input_cells()) + assert_eq!(INPUT_CELLS_PER_HASH, builtin.n_input_cells()) } #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_n_input_cells_ec_op() { - let ec_op = EcOpBuiltinRunner::new(&EcOpInstanceDef::default(), true); + let ec_op = EcOpBuiltinRunner::new(Some(256), true); let builtin: BuiltinRunner = ec_op.clone().into(); - assert_eq!(ec_op.n_input_cells, builtin.n_input_cells()) + assert_eq!(INPUT_CELLS_PER_EC_OP, builtin.n_input_cells()) } #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_n_input_cells_ecdsa() { - let signature = SignatureBuiltinRunner::new(&EcdsaInstanceDef::new(Some(10)), true); + let signature = SignatureBuiltinRunner::new(Some(10), true); let builtin: BuiltinRunner = signature.clone().into(); - assert_eq!(signature.n_input_cells, builtin.n_input_cells()) + assert_eq!(CELLS_PER_SIGNATURE, builtin.n_input_cells()) } #[test] @@ -721,9 +714,9 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_cells_per_instance_bitwise() { - let bitwise = BitwiseBuiltinRunner::new(&BitwiseInstanceDef::new(Some(10)), true); + let bitwise = BitwiseBuiltinRunner::new(Some(10), true); let builtin: BuiltinRunner = bitwise.clone().into(); - assert_eq!(bitwise.cells_per_instance, builtin.cells_per_instance()) + assert_eq!(CELLS_PER_BITWISE, builtin.cells_per_instance()) } #[test] @@ -731,31 +724,23 @@ mod tests { fn get_cells_per_instance_hash() { let hash = HashBuiltinRunner::new(Some(10), true); let builtin: BuiltinRunner = hash.clone().into(); - assert_eq!(hash.cells_per_instance, builtin.cells_per_instance()) - } - - #[test] - #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] - fn get_cells_per_instance_range_check() { - let range_check = RangeCheckBuiltinRunner::::new(Some(10), true); - let builtin: BuiltinRunner = range_check.clone().into(); - assert_eq!(range_check.cells_per_instance, builtin.cells_per_instance()) + assert_eq!(CELLS_PER_HASH, builtin.cells_per_instance()) } #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_cells_per_instance_ec_op() { - let ec_op = EcOpBuiltinRunner::new(&EcOpInstanceDef::default(), true); + let ec_op = EcOpBuiltinRunner::new(Some(256), true); let builtin: BuiltinRunner = ec_op.clone().into(); - assert_eq!(ec_op.cells_per_instance, builtin.cells_per_instance()) + assert_eq!(CELLS_PER_EC_OP, builtin.cells_per_instance()) } #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_cells_per_instance_ecdsa() { - let signature = SignatureBuiltinRunner::new(&EcdsaInstanceDef::new(Some(10)), true); + let signature = SignatureBuiltinRunner::new(Some(10), true); let builtin: BuiltinRunner = signature.clone().into(); - assert_eq!(signature.cells_per_instance, builtin.cells_per_instance()) + assert_eq!(CELLS_PER_SIGNATURE, builtin.cells_per_instance()) } #[test] @@ -766,18 +751,10 @@ mod tests { assert_eq!(0, builtin.cells_per_instance()) } - #[test] - #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] - fn get_cells_per_instance_keccak() { - let keccak = KeccakBuiltinRunner::new(&KeccakInstanceDef::default(), true); - let builtin: BuiltinRunner = keccak.clone().into(); - assert_eq!(keccak.cells_per_instance, builtin.cells_per_instance()) - } - #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_name_bitwise() { - let bitwise = BitwiseBuiltinRunner::new(&BitwiseInstanceDef::new(Some(10)), true); + let bitwise = BitwiseBuiltinRunner::new(Some(10), true); let builtin: BuiltinRunner = bitwise.into(); assert_eq!(BITWISE_BUILTIN_NAME, builtin.name()) } @@ -801,7 +778,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_name_ec_op() { - let ec_op = EcOpBuiltinRunner::new(&EcOpInstanceDef::default(), true); + let ec_op = EcOpBuiltinRunner::new(Some(256), true); let builtin: BuiltinRunner = ec_op.into(); assert_eq!(EC_OP_BUILTIN_NAME, builtin.name()) } @@ -809,7 +786,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_name_ecdsa() { - let signature = SignatureBuiltinRunner::new(&EcdsaInstanceDef::new(Some(10)), true); + let signature = SignatureBuiltinRunner::new(Some(10), true); let builtin: BuiltinRunner = signature.into(); assert_eq!(SIGNATURE_BUILTIN_NAME, builtin.name()) } @@ -825,10 +802,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_allocated_memory_units_bitwise_with_items() { - let builtin = BuiltinRunner::Bitwise(BitwiseBuiltinRunner::new( - &BitwiseInstanceDef::new(Some(10)), - true, - )); + let builtin = BuiltinRunner::Bitwise(BitwiseBuiltinRunner::new(Some(10), true)); let mut vm = vm!(); @@ -872,10 +846,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_allocated_memory_units_ec_op_with_items() { - let builtin = BuiltinRunner::EcOp(EcOpBuiltinRunner::new( - &EcOpInstanceDef::new(Some(10)), - true, - )); + let builtin = BuiltinRunner::EcOp(EcOpBuiltinRunner::new(Some(10), true)); let mut vm = vm!(); @@ -1009,10 +980,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_allocated_memory_units_keccak_with_items() { - let builtin = BuiltinRunner::Keccak(KeccakBuiltinRunner::new( - &KeccakInstanceDef::new(Some(10), vec![200; 8]), - true, - )); + let builtin = BuiltinRunner::Keccak(KeccakBuiltinRunner::new(Some(10), true)); let mut vm = vm!(); vm.current_step = 160; @@ -1021,10 +989,7 @@ mod tests { #[test] fn get_allocated_memory_units_keccak_min_steps_not_reached() { - let builtin = BuiltinRunner::Keccak(KeccakBuiltinRunner::new( - &KeccakInstanceDef::new(Some(10), vec![200; 8]), - true, - )); + let builtin = BuiltinRunner::Keccak(KeccakBuiltinRunner::new(Some(10), true)); let mut vm = vm!(); vm.current_step = 10; @@ -1072,10 +1037,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_allocated_memory_units_bitwise() { - let builtin = BuiltinRunner::Bitwise(BitwiseBuiltinRunner::new( - &BitwiseInstanceDef::default(), - true, - )); + let builtin = BuiltinRunner::Bitwise(BitwiseBuiltinRunner::new(Some(256), true)); let mut vm = vm!(); vm.current_step = 256; assert_eq!(builtin.get_allocated_memory_units(&vm), Ok(5)); @@ -1084,8 +1046,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_allocated_memory_units_ec_op() { - let builtin = - BuiltinRunner::EcOp(EcOpBuiltinRunner::new(&EcOpInstanceDef::default(), true)); + let builtin = BuiltinRunner::EcOp(EcOpBuiltinRunner::new(Some(256), true)); let mut vm = vm!(); vm.current_step = 256; assert_eq!(builtin.get_allocated_memory_units(&vm), Ok(7)); @@ -1094,10 +1055,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_allocated_memory_units_keccak() { - let builtin = BuiltinRunner::Keccak(KeccakBuiltinRunner::new( - &KeccakInstanceDef::default(), - true, - )); + let builtin = BuiltinRunner::Keccak(KeccakBuiltinRunner::new(Some(2048), true)); let mut vm = vm!(); vm.current_step = 32768; assert_eq!(builtin.get_allocated_memory_units(&vm), Ok(256)); @@ -1132,8 +1090,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_range_check_usage_ec_op() { - let builtin = - BuiltinRunner::EcOp(EcOpBuiltinRunner::new(&EcOpInstanceDef::default(), true)); + let builtin = BuiltinRunner::EcOp(EcOpBuiltinRunner::new(Some(256), true)); let memory = memory![((0, 0), 1), ((0, 1), 2), ((0, 2), 3), ((0, 3), 4)]; assert_eq!(builtin.get_range_check_usage(&memory), None); } @@ -1141,10 +1098,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_range_check_usage_bitwise() { - let builtin = BuiltinRunner::Bitwise(BitwiseBuiltinRunner::new( - &BitwiseInstanceDef::default(), - true, - )); + let builtin = BuiltinRunner::Bitwise(BitwiseBuiltinRunner::new(Some(256), true)); let memory = memory![((0, 0), 1), ((0, 1), 2), ((0, 2), 3), ((0, 3), 4)]; assert_eq!(builtin.get_range_check_usage(&memory), None); } @@ -1152,40 +1106,28 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_used_diluted_check_units_bitwise() { - let builtin = BuiltinRunner::Bitwise(BitwiseBuiltinRunner::new( - &BitwiseInstanceDef::default(), - true, - )); + let builtin = BuiltinRunner::Bitwise(BitwiseBuiltinRunner::new(Some(256), true)); assert_eq!(builtin.get_used_diluted_check_units(270, 7), 1255); } #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_used_diluted_check_units_keccak_zero_case() { - let builtin = BuiltinRunner::Keccak(KeccakBuiltinRunner::new( - &KeccakInstanceDef::default(), - true, - )); + let builtin = BuiltinRunner::Keccak(KeccakBuiltinRunner::new(Some(2048), true)); assert_eq!(builtin.get_used_diluted_check_units(270, 7), 0); } #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_used_diluted_check_units_keccak_non_zero_case() { - let builtin = BuiltinRunner::Keccak(KeccakBuiltinRunner::new( - &KeccakInstanceDef::default(), - true, - )); + let builtin = BuiltinRunner::Keccak(KeccakBuiltinRunner::new(Some(2048), true)); assert_eq!(builtin.get_used_diluted_check_units(0, 8), 32768); } #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_used_diluted_check_units_ec_op() { - let builtin = BuiltinRunner::EcOp(EcOpBuiltinRunner::new( - &EcOpInstanceDef::new(Some(10)), - true, - )); + let builtin = BuiltinRunner::EcOp(EcOpBuiltinRunner::new(Some(10), true)); assert_eq!(builtin.get_used_diluted_check_units(270, 7), 0); } @@ -1215,11 +1157,9 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_memory_segment_addresses_test() { - let bitwise_builtin: BuiltinRunner = - BitwiseBuiltinRunner::new(&BitwiseInstanceDef::default(), true).into(); + let bitwise_builtin: BuiltinRunner = BitwiseBuiltinRunner::new(Some(256), true).into(); assert_eq!(bitwise_builtin.get_memory_segment_addresses(), (0, None),); - let ec_op_builtin: BuiltinRunner = - EcOpBuiltinRunner::new(&EcOpInstanceDef::default(), true).into(); + let ec_op_builtin: BuiltinRunner = EcOpBuiltinRunner::new(Some(256), true).into(); assert_eq!(ec_op_builtin.get_memory_segment_addresses(), (0, None),); let hash_builtin: BuiltinRunner = HashBuiltinRunner::new(Some(8), true).into(); assert_eq!(hash_builtin.get_memory_segment_addresses(), (0, None),); @@ -1246,10 +1186,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn run_security_checks_empty_memory() { - let builtin = BuiltinRunner::Bitwise(BitwiseBuiltinRunner::new( - &BitwiseInstanceDef::default(), - true, - )); + let builtin = BuiltinRunner::Bitwise(BitwiseBuiltinRunner::new(Some(256), true)); let vm = vm!(); // Unused builtin shouldn't fail security checks assert_matches!(builtin.run_security_checks(&vm), Ok(())); @@ -1258,10 +1195,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn run_security_checks_empty_offsets() { - let builtin = BuiltinRunner::Bitwise(BitwiseBuiltinRunner::new( - &BitwiseInstanceDef::default(), - true, - )); + let builtin = BuiltinRunner::Bitwise(BitwiseBuiltinRunner::new(Some(256), true)); let mut vm = vm!(); vm.segments.memory.data = vec![vec![]]; @@ -1272,10 +1206,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn run_security_checks_bitwise_missing_memory_cells_with_offsets() { - let builtin = BuiltinRunner::Bitwise(BitwiseBuiltinRunner::new( - &BitwiseInstanceDef::default(), - true, - )); + let builtin = BuiltinRunner::Bitwise(BitwiseBuiltinRunner::new(Some(256), true)); let mut vm = vm!(); vm.segments.memory = memory![ ((0, 1), (0, 1)), @@ -1295,22 +1226,13 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn run_security_checks_bitwise_missing_memory_cells() { - let mut bitwise_builtin = BitwiseBuiltinRunner::new(&BitwiseInstanceDef::default(), true); - - bitwise_builtin.cells_per_instance = 2; - bitwise_builtin.n_input_cells = 5; + let bitwise_builtin = BitwiseBuiltinRunner::new(Some(256), true); let builtin: BuiltinRunner = bitwise_builtin.into(); let mut vm = vm!(); - vm.segments.memory = memory![ - ((0, 0), (0, 1)), - ((0, 1), (0, 2)), - ((0, 2), (0, 3)), - ((0, 3), (0, 4)), - ((0, 4), (0, 5)) - ]; + vm.segments.memory = memory![((0, 4), (0, 5))]; assert_matches!( builtin.run_security_checks(&vm), @@ -1421,8 +1343,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn run_security_checks_validate_auto_deductions() { - let builtin: BuiltinRunner = - BitwiseBuiltinRunner::new(&BitwiseInstanceDef::default(), true).into(); + let builtin: BuiltinRunner = BitwiseBuiltinRunner::new(Some(256), true).into(); let mut vm = vm!(); vm.segments @@ -1444,7 +1365,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn run_security_ec_op_check_memory_empty() { - let ec_op_builtin = EcOpBuiltinRunner::new(&EcOpInstanceDef::default(), true); + let ec_op_builtin = EcOpBuiltinRunner::new(Some(256), true); let builtin: BuiltinRunner = ec_op_builtin.into(); @@ -1458,7 +1379,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn run_security_ec_op_check_memory_1_element() { - let ec_op_builtin = EcOpBuiltinRunner::new(&EcOpInstanceDef::default(), true); + let ec_op_builtin = EcOpBuiltinRunner::new(Some(256), true); let builtin: BuiltinRunner = ec_op_builtin.into(); @@ -1476,7 +1397,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn run_security_ec_op_check_memory_3_elements() { - let ec_op_builtin = EcOpBuiltinRunner::new(&EcOpInstanceDef::default(), true); + let ec_op_builtin = EcOpBuiltinRunner::new(Some(256), true); let builtin: BuiltinRunner = ec_op_builtin.into(); @@ -1495,8 +1416,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn run_security_ec_op_missing_memory_cells_with_offsets() { - let builtin: BuiltinRunner = - EcOpBuiltinRunner::new(&EcOpInstanceDef::default(), true).into(); + let builtin: BuiltinRunner = EcOpBuiltinRunner::new(Some(256), true).into(); let mut vm = vm!(); vm.segments.memory = memory![ ((0, 1), (0, 1)), @@ -1518,7 +1438,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn run_security_ec_op_check_memory_gap() { - let ec_op_builtin = EcOpBuiltinRunner::new(&EcOpInstanceDef::default(), true); + let ec_op_builtin = EcOpBuiltinRunner::new(Some(256), true); let builtin: BuiltinRunner = ec_op_builtin.into(); @@ -1551,8 +1471,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_used_perm_range_check_units_bitwise() { - let builtin_runner: BuiltinRunner = - BitwiseBuiltinRunner::new(&BitwiseInstanceDef::default(), true).into(); + let builtin_runner: BuiltinRunner = BitwiseBuiltinRunner::new(Some(256), true).into(); let mut vm = vm!(); vm.current_step = 8; @@ -1565,8 +1484,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_used_perm_range_check_units_ec_op() { - let builtin_runner: BuiltinRunner = - EcOpBuiltinRunner::new(&EcOpInstanceDef::default(), true).into(); + let builtin_runner: BuiltinRunner = EcOpBuiltinRunner::new(Some(256), true).into(); let mut vm = vm!(); vm.current_step = 8; @@ -1617,11 +1535,9 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_ratio_tests() { - let bitwise_builtin: BuiltinRunner = - BitwiseBuiltinRunner::new(&BitwiseInstanceDef::default(), true).into(); + let bitwise_builtin: BuiltinRunner = BitwiseBuiltinRunner::new(Some(256), true).into(); assert_eq!(bitwise_builtin.ratio(), (Some(256)),); - let ec_op_builtin: BuiltinRunner = - EcOpBuiltinRunner::new(&EcOpInstanceDef::default(), true).into(); + let ec_op_builtin: BuiltinRunner = EcOpBuiltinRunner::new(Some(256), true).into(); assert_eq!(ec_op_builtin.ratio(), (Some(256)),); let hash_builtin: BuiltinRunner = HashBuiltinRunner::new(Some(8), true).into(); assert_eq!(hash_builtin.ratio(), (Some(8)),); @@ -1631,8 +1547,7 @@ mod tests { RangeCheckBuiltinRunner::::new(Some(8), true), ); assert_eq!(range_check_builtin.ratio(), (Some(8)),); - let keccak_builtin: BuiltinRunner = - KeccakBuiltinRunner::new(&KeccakInstanceDef::default(), true).into(); + let keccak_builtin: BuiltinRunner = KeccakBuiltinRunner::new(Some(2048), true).into(); assert_eq!(keccak_builtin.ratio(), (Some(2048)),); } @@ -1642,8 +1557,7 @@ mod tests { let mut vm = vm!(); vm.segments.segment_used_sizes = Some(vec![4]); - let bitwise_builtin: BuiltinRunner = - BitwiseBuiltinRunner::new(&BitwiseInstanceDef::default(), true).into(); + let bitwise_builtin: BuiltinRunner = BitwiseBuiltinRunner::new(Some(256), true).into(); assert_eq!(bitwise_builtin.get_used_instances(&vm.segments), Ok(1)); } @@ -1653,8 +1567,7 @@ mod tests { let mut vm = vm!(); vm.segments.segment_used_sizes = Some(vec![4]); - let ec_op_builtin: BuiltinRunner = - EcOpBuiltinRunner::new(&EcOpInstanceDef::default(), true).into(); + let ec_op_builtin: BuiltinRunner = EcOpBuiltinRunner::new(Some(256), true).into(); assert_eq!(ec_op_builtin.get_used_instances(&vm.segments), Ok(1)); } @@ -1693,25 +1606,16 @@ mod tests { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn runners_final_stack() { let mut builtins = vec![ - BuiltinRunner::Bitwise(BitwiseBuiltinRunner::new( - &BitwiseInstanceDef::default(), - false, - )), - BuiltinRunner::EcOp(EcOpBuiltinRunner::new(&EcOpInstanceDef::default(), false)), + BuiltinRunner::Bitwise(BitwiseBuiltinRunner::new(Some(256), false)), + BuiltinRunner::EcOp(EcOpBuiltinRunner::new(Some(256), false)), BuiltinRunner::Hash(HashBuiltinRunner::new(Some(1), false)), BuiltinRunner::Output(OutputBuiltinRunner::new(false)), BuiltinRunner::RangeCheck(RangeCheckBuiltinRunner::::new( Some(8), false, )), - BuiltinRunner::Keccak(KeccakBuiltinRunner::new( - &KeccakInstanceDef::default(), - false, - )), - BuiltinRunner::Signature(SignatureBuiltinRunner::new( - &EcdsaInstanceDef::default(), - false, - )), + BuiltinRunner::Keccak(KeccakBuiltinRunner::new(Some(2048), false)), + BuiltinRunner::Signature(SignatureBuiltinRunner::new(Some(512), false)), ]; let vm = vm!(); @@ -1724,25 +1628,16 @@ mod tests { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn runners_set_stop_ptr() { let builtins = vec![ - BuiltinRunner::Bitwise(BitwiseBuiltinRunner::new( - &BitwiseInstanceDef::default(), - false, - )), - BuiltinRunner::EcOp(EcOpBuiltinRunner::new(&EcOpInstanceDef::default(), false)), + BuiltinRunner::Bitwise(BitwiseBuiltinRunner::new(Some(256), false)), + BuiltinRunner::EcOp(EcOpBuiltinRunner::new(Some(256), false)), BuiltinRunner::Hash(HashBuiltinRunner::new(Some(1), false)), BuiltinRunner::Output(OutputBuiltinRunner::new(false)), BuiltinRunner::RangeCheck(RangeCheckBuiltinRunner::::new( Some(8), false, )), - BuiltinRunner::Keccak(KeccakBuiltinRunner::new( - &KeccakInstanceDef::default(), - false, - )), - BuiltinRunner::Signature(SignatureBuiltinRunner::new( - &EcdsaInstanceDef::default(), - false, - )), + BuiltinRunner::Keccak(KeccakBuiltinRunner::new(Some(2048), false)), + BuiltinRunner::Signature(SignatureBuiltinRunner::new(Some(512), false)), BuiltinRunner::Poseidon(PoseidonBuiltinRunner::new(Some(32), false)), BuiltinRunner::SegmentArena(SegmentArenaBuiltinRunner::new(false)), ]; diff --git a/vm/src/vm/runners/builtin_runner/modulo.rs b/vm/src/vm/runners/builtin_runner/modulo.rs index 924aa65a28..91254d4789 100644 --- a/vm/src/vm/runners/builtin_runner/modulo.rs +++ b/vm/src/vm/runners/builtin_runner/modulo.rs @@ -9,7 +9,7 @@ use crate::{ }, types::{ errors::math_errors::MathError, - instance_definitions::mod_instance_def::{ModInstanceDef, N_WORDS}, + instance_definitions::mod_instance_def::{ModInstanceDef, CELLS_PER_MOD, N_WORDS}, relocatable::{relocate_address, MaybeRelocatable, Relocatable}, }, vm::{ @@ -31,8 +31,6 @@ use num_traits::Zero; //The maximum n value that the function fill_memory accepts. const FILL_MEMORY_MAX: usize = 100000; -const INPUT_CELLS: usize = 7; - const VALUES_PTR_OFFSET: u32 = 4; const OFFSETS_PTR_OFFSET: u32 = 5; const N_OFFSET: u32 = 6; @@ -146,14 +144,6 @@ impl ModBuiltinRunner { self.instance_def.ratio } - pub fn cells_per_instance(&self) -> u32 { - INPUT_CELLS as u32 - } - - pub fn n_input_cells(&self) -> u32 { - INPUT_CELLS as u32 - } - pub fn batch_size(&self) -> usize { self.instance_def.batch_size } @@ -169,7 +159,7 @@ impl ModBuiltinRunner { segments: &MemorySegmentManager, ) -> Result { let used_cells = self.get_used_cells(segments)?; - Ok(div_ceil(used_cells, self.cells_per_instance() as usize)) + Ok(div_ceil(used_cells, CELLS_PER_MOD as usize)) } pub(crate) fn air_private_input(&self, segments: &MemorySegmentManager) -> Vec { @@ -179,8 +169,11 @@ impl ModBuiltinRunner { .unwrap_or_default(); let relocation_table = segments.relocate_segments().unwrap_or_default(); let mut instances = Vec::::new(); - for instance in 0..segment_size.checked_div(INPUT_CELLS).unwrap_or_default() { - let instance_addr_offset = instance * INPUT_CELLS; + for instance in 0..segment_size + .checked_div(CELLS_PER_MOD as usize) + .unwrap_or_default() + { + let instance_addr_offset = instance * CELLS_PER_MOD as usize; let values_ptr = segments .memory .get_relocatable( @@ -382,7 +375,7 @@ impl ModBuiltinRunner { } let n_instances = safe_div_usize(inputs.n, self.instance_def.batch_size)?; for instance in 1..n_instances { - let instance_ptr = (builtin_ptr + instance * INPUT_CELLS)?; + let instance_ptr = (builtin_ptr + instance * CELLS_PER_MOD as usize)?; for i in 0..N_WORDS { memory.insert_as_accessed((instance_ptr + i)?, &inputs.p_values[i])?; } @@ -600,12 +593,12 @@ impl ModBuiltinRunner { let segment_size = vm .get_segment_used_size(self.base) .ok_or(MemoryError::MissingSegmentUsedSizes)?; - let n_instances = div_ceil(segment_size, INPUT_CELLS); + let n_instances = div_ceil(segment_size, CELLS_PER_MOD as usize); let mut prev_inputs = Inputs::default(); for instance in 0..n_instances { let inputs = self.read_inputs( &vm.segments.memory, - (self.base as isize, instance * INPUT_CELLS).into(), + (self.base as isize, instance * CELLS_PER_MOD as usize).into(), )?; if !instance.is_zero() && prev_inputs.n > self.instance_def.batch_size { for i in 0..N_WORDS { diff --git a/vm/src/vm/runners/builtin_runner/poseidon.rs b/vm/src/vm/runners/builtin_runner/poseidon.rs index d2a4ce9c18..51b12cc73b 100644 --- a/vm/src/vm/runners/builtin_runner/poseidon.rs +++ b/vm/src/vm/runners/builtin_runner/poseidon.rs @@ -19,12 +19,9 @@ use super::POSEIDON_BUILTIN_NAME; pub struct PoseidonBuiltinRunner { pub base: usize, ratio: Option, - pub(crate) cells_per_instance: u32, - pub(crate) n_input_cells: u32, pub(crate) stop_ptr: Option, pub(crate) included: bool, cache: RefCell>, - pub(crate) instances_per_component: u32, } impl PoseidonBuiltinRunner { @@ -32,12 +29,9 @@ impl PoseidonBuiltinRunner { PoseidonBuiltinRunner { base: 0, ratio, - cells_per_instance: CELLS_PER_POSEIDON, - n_input_cells: INPUT_CELLS_PER_POSEIDON, stop_ptr: None, included, cache: RefCell::new(HashMap::new()), - instances_per_component: 1, } } @@ -68,19 +62,19 @@ impl PoseidonBuiltinRunner { address: Relocatable, memory: &Memory, ) -> Result, RunnerError> { - let index = address.offset % self.cells_per_instance as usize; - if index < self.n_input_cells as usize { + let index = address.offset % CELLS_PER_POSEIDON as usize; + if index < INPUT_CELLS_PER_POSEIDON as usize { return Ok(None); } if let Some(felt) = self.cache.borrow().get(&address) { return Ok(Some(felt.into())); } let first_input_addr = (address - index)?; - let first_output_addr = (first_input_addr + self.n_input_cells as usize)?; + let first_output_addr = (first_input_addr + INPUT_CELLS_PER_POSEIDON as usize)?; let mut input_felts = vec![]; - for i in 0..self.n_input_cells as usize { + for i in 0..INPUT_CELLS_PER_POSEIDON as usize { let m_index = (first_input_addr + i)?; let val = match memory.get(&m_index) { Some(value) => { @@ -121,7 +115,7 @@ impl PoseidonBuiltinRunner { segments: &MemorySegmentManager, ) -> Result { let used_cells = self.get_used_cells(segments)?; - Ok(div_ceil(used_cells, self.cells_per_instance as usize)) + Ok(div_ceil(used_cells, CELLS_PER_POSEIDON as usize)) } pub fn air_private_input(&self, memory: &Memory) -> Vec { diff --git a/vm/src/vm/runners/builtin_runner/range_check.rs b/vm/src/vm/runners/builtin_runner/range_check.rs index cb02e79b46..b9b49992ce 100644 --- a/vm/src/vm/runners/builtin_runner/range_check.rs +++ b/vm/src/vm/runners/builtin_runner/range_check.rs @@ -8,10 +8,7 @@ use crate::{ use crate::Felt252; use crate::{ - types::{ - instance_definitions::range_check_instance_def::CELLS_PER_RANGE_CHECK, - relocatable::{MaybeRelocatable, Relocatable}, - }, + types::relocatable::{MaybeRelocatable, Relocatable}, vm::{ errors::memory_errors::MemoryError, vm_memory::{ @@ -42,10 +39,7 @@ pub struct RangeCheckBuiltinRunner { ratio: Option, base: usize, pub(crate) stop_ptr: Option, - pub(crate) cells_per_instance: u32, - pub(crate) n_input_cells: u32, pub(crate) included: bool, - pub(crate) instances_per_component: u32, } impl RangeCheckBuiltinRunner { @@ -54,10 +48,7 @@ impl RangeCheckBuiltinRunner { ratio, base: 0, stop_ptr: None, - cells_per_instance: CELLS_PER_RANGE_CHECK, - n_input_cells: CELLS_PER_RANGE_CHECK, included, - instances_per_component: 1, } } diff --git a/vm/src/vm/runners/builtin_runner/segment_arena.rs b/vm/src/vm/runners/builtin_runner/segment_arena.rs index b505ac3813..5cc61fb706 100644 --- a/vm/src/vm/runners/builtin_runner/segment_arena.rs +++ b/vm/src/vm/runners/builtin_runner/segment_arena.rs @@ -8,7 +8,7 @@ use crate::{ use alloc::vec::Vec; use num_integer::div_ceil; -const ARENA_BUILTIN_SIZE: u32 = 3; +pub(crate) const ARENA_BUILTIN_SIZE: u32 = 3; // The size of the builtin segment at the time of its creation. const INITIAL_SEGMENT_SIZE: usize = ARENA_BUILTIN_SIZE as usize; @@ -16,8 +16,6 @@ const INITIAL_SEGMENT_SIZE: usize = ARENA_BUILTIN_SIZE as usize; pub struct SegmentArenaBuiltinRunner { base: Relocatable, pub(crate) included: bool, - pub(crate) cells_per_instance: u32, - pub(crate) n_input_cells_per_instance: u32, pub(crate) stop_ptr: Option, } @@ -26,8 +24,6 @@ impl SegmentArenaBuiltinRunner { SegmentArenaBuiltinRunner { base: Relocatable::from((0, 0)), included, - cells_per_instance: ARENA_BUILTIN_SIZE, - n_input_cells_per_instance: ARENA_BUILTIN_SIZE, stop_ptr: None, } } @@ -67,7 +63,7 @@ impl SegmentArenaBuiltinRunner { ) -> Result { Ok(div_ceil( self.get_used_cells(segments)?, - self.cells_per_instance as usize, + ARENA_BUILTIN_SIZE as usize, )) } diff --git a/vm/src/vm/runners/builtin_runner/signature.rs b/vm/src/vm/runners/builtin_runner/signature.rs index 3d5fa6ef45..9520ebcc63 100644 --- a/vm/src/vm/runners/builtin_runner/signature.rs +++ b/vm/src/vm/runners/builtin_runner/signature.rs @@ -7,10 +7,7 @@ use crate::types::instance_definitions::ecdsa_instance_def::CELLS_PER_SIGNATURE; use crate::vm::runners::cairo_pie::BuiltinAdditionalData; use crate::Felt252; use crate::{ - types::{ - instance_definitions::ecdsa_instance_def::EcdsaInstanceDef, - relocatable::{MaybeRelocatable, Relocatable}, - }, + types::relocatable::{MaybeRelocatable, Relocatable}, vm::{ errors::memory_errors::MemoryError, vm_memory::{ @@ -38,25 +35,17 @@ pub struct SignatureBuiltinRunner { pub(crate) included: bool, ratio: Option, base: usize, - pub(crate) cells_per_instance: u32, - pub(crate) n_input_cells: u32, - _total_n_bits: u32, pub(crate) stop_ptr: Option, - pub(crate) instances_per_component: u32, signatures: Rc>>, } impl SignatureBuiltinRunner { - pub(crate) fn new(instance_def: &EcdsaInstanceDef, included: bool) -> Self { + pub(crate) fn new(ratio: Option, included: bool) -> Self { SignatureBuiltinRunner { base: 0, included, - ratio: instance_def.ratio, - cells_per_instance: 2, - n_input_cells: 2, - _total_n_bits: 251, + ratio, stop_ptr: None, - instances_per_component: 1, signatures: Rc::new(RefCell::new(HashMap::new())), } } @@ -104,7 +93,7 @@ impl SignatureBuiltinRunner { self.base } pub fn add_validation_rule(&self, memory: &mut Memory) { - let cells_per_instance = self.cells_per_instance; + let cells_per_instance = CELLS_PER_SIGNATURE; let signatures = Rc::clone(&self.signatures); let rule: ValidationRule = ValidationRule(Box::new( move |memory: &Memory, addr: Relocatable| -> Result, MemoryError> { @@ -169,7 +158,7 @@ impl SignatureBuiltinRunner { segments: &MemorySegmentManager, ) -> Result { let used_cells = self.get_used_cells(segments)?; - Ok(div_ceil(used_cells, self.cells_per_instance as usize)) + Ok(div_ceil(used_cells, CELLS_PER_SIGNATURE as usize)) } pub fn get_additional_data(&self) -> BuiltinAdditionalData { @@ -227,7 +216,6 @@ mod tests { use super::*; use crate::{ relocatable, - types::instance_definitions::ecdsa_instance_def::EcdsaInstanceDef, utils::test_utils::*, vm::{ errors::{ @@ -246,8 +234,7 @@ mod tests { #[test] fn get_used_cells_and_allocated_size_valid() { - let builtin: BuiltinRunner = - SignatureBuiltinRunner::new(&EcdsaInstanceDef::new(Some(10)), true).into(); + let builtin: BuiltinRunner = SignatureBuiltinRunner::new(Some(10), true).into(); let mut vm = vm!(); vm.current_step = 110; vm.segments.segment_used_sizes = Some(vec![1]); @@ -257,7 +244,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn initialize_segments_for_ecdsa() { - let mut builtin = SignatureBuiltinRunner::new(&EcdsaInstanceDef::default(), true); + let mut builtin = SignatureBuiltinRunner::new(Some(512), true); let mut segments = MemorySegmentManager::new(); builtin.initialize_segments(&mut segments); assert_eq!(builtin.base, 0); @@ -266,8 +253,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_used_instances() { - let builtin: BuiltinRunner = - SignatureBuiltinRunner::new(&EcdsaInstanceDef::default(), true).into(); + let builtin: BuiltinRunner = SignatureBuiltinRunner::new(Some(512), true).into(); let mut vm = vm!(); vm.segments.segment_used_sizes = Some(vec![1]); @@ -278,8 +264,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn final_stack() { - let mut builtin: BuiltinRunner = - SignatureBuiltinRunner::new(&EcdsaInstanceDef::default(), true).into(); + let mut builtin: BuiltinRunner = SignatureBuiltinRunner::new(Some(512), true).into(); let mut vm = vm!(); @@ -303,8 +288,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn final_stack_error_stop_pointer() { - let mut builtin: BuiltinRunner = - SignatureBuiltinRunner::new(&EcdsaInstanceDef::default(), true).into(); + let mut builtin: BuiltinRunner = SignatureBuiltinRunner::new(Some(512), true).into(); let mut vm = vm!(); @@ -332,8 +316,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn final_stack_error_non_relocatable() { - let mut builtin: BuiltinRunner = - SignatureBuiltinRunner::new(&EcdsaInstanceDef::default(), true).into(); + let mut builtin: BuiltinRunner = SignatureBuiltinRunner::new(Some(512), true).into(); let mut vm = vm!(); @@ -357,10 +340,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_used_cells_missing_segment_used_sizes() { - let builtin = BuiltinRunner::Signature(SignatureBuiltinRunner::new( - &EcdsaInstanceDef::default(), - true, - )); + let builtin = BuiltinRunner::Signature(SignatureBuiltinRunner::new(Some(512), true)); let vm = vm!(); assert_eq!( @@ -372,10 +352,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_used_cells_empty() { - let builtin = BuiltinRunner::Signature(SignatureBuiltinRunner::new( - &EcdsaInstanceDef::default(), - true, - )); + let builtin = BuiltinRunner::Signature(SignatureBuiltinRunner::new(Some(512), true)); let mut vm = vm!(); vm.segments.segment_used_sizes = Some(vec![0]); @@ -385,10 +362,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_used_cells() { - let builtin = BuiltinRunner::Signature(SignatureBuiltinRunner::new( - &EcdsaInstanceDef::default(), - true, - )); + let builtin = BuiltinRunner::Signature(SignatureBuiltinRunner::new(Some(512), true)); let mut vm = vm!(); vm.segments.segment_used_sizes = Some(vec![4]); @@ -398,7 +372,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_initial_stack_for_range_check_with_base() { - let mut builtin = SignatureBuiltinRunner::new(&EcdsaInstanceDef::default(), true); + let mut builtin = SignatureBuiltinRunner::new(Some(512), true); builtin.base = 1; let initial_stack = builtin.initial_stack(); assert_eq!( @@ -411,7 +385,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn initial_stack_not_included_test() { - let ecdsa_builtin = SignatureBuiltinRunner::new(&EcdsaInstanceDef::default(), false); + let ecdsa_builtin = SignatureBuiltinRunner::new(Some(512), false); assert_eq!(ecdsa_builtin.initial_stack(), Vec::new()) } @@ -419,8 +393,7 @@ mod tests { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn deduce_memory_cell_test() { let memory = Memory::new(); - let builtin: BuiltinRunner = - SignatureBuiltinRunner::new(&EcdsaInstanceDef::default(), true).into(); + let builtin: BuiltinRunner = SignatureBuiltinRunner::new(Some(512), true).into(); let result = builtin.deduce_memory_cell(Relocatable::from((0, 5)), &memory); assert_eq!(result, Ok(None)); } @@ -428,22 +401,21 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn test_ratio() { - let builtin = SignatureBuiltinRunner::new(&EcdsaInstanceDef::default(), true); + let builtin = SignatureBuiltinRunner::new(Some(512), true); assert_eq!(builtin.ratio(), Some(512)); } #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn test_base() { - let builtin = SignatureBuiltinRunner::new(&EcdsaInstanceDef::default(), true); + let builtin = SignatureBuiltinRunner::new(Some(512), true); assert_eq!(builtin.base(), 0); } #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_allocated_memory_min_step_not_reached() { - let builtin: BuiltinRunner = - SignatureBuiltinRunner::new(&EcdsaInstanceDef::default(), true).into(); + let builtin: BuiltinRunner = SignatureBuiltinRunner::new(Some(512), true).into(); let mut vm = vm!(); vm.current_step = 500; assert_eq!( @@ -460,8 +432,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn get_used_cells_and_allocated_size_insufficient_allocated() { - let builtin: BuiltinRunner = - SignatureBuiltinRunner::new(&EcdsaInstanceDef::default(), true).into(); + let builtin: BuiltinRunner = SignatureBuiltinRunner::new(Some(512), true).into(); let mut vm = vm!(); vm.segments.segment_used_sizes = Some(vec![50]); vm.current_step = 512; @@ -480,8 +451,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn final_stack_invalid_stop_pointer() { - let mut builtin: BuiltinRunner = - SignatureBuiltinRunner::new(&EcdsaInstanceDef::default(), true).into(); + let mut builtin: BuiltinRunner = SignatureBuiltinRunner::new(Some(512), true).into(); let mut vm = vm!(); vm.segments = segments![((0, 0), (1, 0))]; assert_eq!( @@ -497,8 +467,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn final_stack_no_used_instances() { - let mut builtin: BuiltinRunner = - SignatureBuiltinRunner::new(&EcdsaInstanceDef::default(), true).into(); + let mut builtin: BuiltinRunner = SignatureBuiltinRunner::new(Some(512), true).into(); let mut vm = vm!(); vm.segments = segments![((0, 0), (0, 0))]; assert_eq!( @@ -509,7 +478,7 @@ mod tests { #[test] fn get_additional_info() { - let mut builtin = SignatureBuiltinRunner::new(&EcdsaInstanceDef::default(), true); + let mut builtin = SignatureBuiltinRunner::new(Some(512), true); let signatures = HashMap::from([( Relocatable::from((4, 0)), Signature { diff --git a/vm/src/vm/runners/cairo_runner.rs b/vm/src/vm/runners/cairo_runner.rs index f405e672af..c61c39b071 100644 --- a/vm/src/vm/runners/cairo_runner.rs +++ b/vm/src/vm/runners/cairo_runner.rs @@ -7,7 +7,7 @@ use crate::{ ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign}, prelude::*, }, - types::instance_definitions::keccak_instance_def::KeccakInstanceDef, + types::layout::MEMORY_UNITS_PER_STEP, vm::{ runners::builtin_runner::SegmentArenaBuiltinRunner, trace::trace_entry::{relocate_trace_register, RelocatedTraceEntry}, @@ -22,10 +22,6 @@ use crate::{ types::{ errors::{math_errors::MathError, program_errors::ProgramError}, exec_scope::ExecutionScopes, - instance_definitions::{ - bitwise_instance_def::BitwiseInstanceDef, ec_op_instance_def::EcOpInstanceDef, - ecdsa_instance_def::EcdsaInstanceDef, - }, layout::CairoLayout, program::Program, relocatable::{relocate_address, relocate_value, MaybeRelocatable, Relocatable}, @@ -303,28 +299,30 @@ impl CairoRunner { if let Some(instance_def) = self.layout.builtins.ecdsa.as_ref() { let included = program_builtins.remove(&BuiltinName::ecdsa); if included || self.is_proof_mode() { - builtin_runners.push(SignatureBuiltinRunner::new(instance_def, included).into()); + builtin_runners + .push(SignatureBuiltinRunner::new(instance_def.ratio, included).into()); } } if let Some(instance_def) = self.layout.builtins.bitwise.as_ref() { let included = program_builtins.remove(&BuiltinName::bitwise); if included || self.is_proof_mode() { - builtin_runners.push(BitwiseBuiltinRunner::new(instance_def, included).into()); + builtin_runners + .push(BitwiseBuiltinRunner::new(instance_def.ratio, included).into()); } } if let Some(instance_def) = self.layout.builtins.ec_op.as_ref() { let included = program_builtins.remove(&BuiltinName::ec_op); if included || self.is_proof_mode() { - builtin_runners.push(EcOpBuiltinRunner::new(instance_def, included).into()); + builtin_runners.push(EcOpBuiltinRunner::new(instance_def.ratio, included).into()); } } if let Some(instance_def) = self.layout.builtins.keccak.as_ref() { let included = program_builtins.remove(&BuiltinName::keccak); if included || self.is_proof_mode() { - builtin_runners.push(KeccakBuiltinRunner::new(instance_def, included).into()); + builtin_runners.push(KeccakBuiltinRunner::new(instance_def.ratio, included).into()); } } @@ -360,7 +358,7 @@ impl CairoRunner { if !program_builtins.is_empty() && !allow_missing_builtins { return Err(RunnerError::NoBuiltinForInstance(Box::new(( program_builtins.iter().map(|n| n.name()).collect(), - self.layout._name.clone(), + self.layout.name.clone(), )))); } @@ -406,19 +404,18 @@ impl CairoRunner { BuiltinName::output => vm .builtin_runners .push(OutputBuiltinRunner::new(true).into()), - BuiltinName::ecdsa => vm.builtin_runners.push( - SignatureBuiltinRunner::new(&EcdsaInstanceDef::new(Some(1)), true).into(), - ), - BuiltinName::bitwise => vm.builtin_runners.push( - BitwiseBuiltinRunner::new(&BitwiseInstanceDef::new(Some(1)), true).into(), - ), + BuiltinName::ecdsa => vm + .builtin_runners + .push(SignatureBuiltinRunner::new(Some(1), true).into()), + BuiltinName::bitwise => vm + .builtin_runners + .push(BitwiseBuiltinRunner::new(Some(1), true).into()), BuiltinName::ec_op => vm .builtin_runners - .push(EcOpBuiltinRunner::new(&EcOpInstanceDef::new(Some(1)), true).into()), - BuiltinName::keccak => vm.builtin_runners.push( - KeccakBuiltinRunner::new(&KeccakInstanceDef::new(Some(1), vec![200; 8]), true) - .into(), - ), + .push(EcOpBuiltinRunner::new(Some(1), true).into()), + BuiltinName::keccak => vm + .builtin_runners + .push(KeccakBuiltinRunner::new(Some(1), true).into()), BuiltinName::poseidon => vm .builtin_runners .push(PoseidonBuiltinRunner::new(Some(1), true).into()), @@ -1207,13 +1204,13 @@ impl CairoRunner { // Out of the memory units available per step, a fraction is used for public memory, and // four are used for the instruction. - let total_memory_units = instance._memory_units_per_step * vm_current_step_u32; + let total_memory_units = MEMORY_UNITS_PER_STEP * vm_current_step_u32; let (public_memory_units, rem) = - div_rem(total_memory_units, instance._public_memory_fraction); + div_rem(total_memory_units, instance.public_memory_fraction); if rem != 0 { return Err(MathError::SafeDivFailU32( total_memory_units, - instance._public_memory_fraction, + instance.public_memory_fraction, ) .into()); } @@ -1459,7 +1456,7 @@ impl CairoRunner { &self, vm: &VirtualMachine, ) -> Result { - let layout_name = self.get_layout()._name.as_str(); + let layout_name = self.get_layout().name.as_str(); let dyn_layout = match layout_name { "dynamic" => Some(self.get_layout()), _ => None, @@ -1638,7 +1635,6 @@ mod tests { hint_processor::builtin_hint_processor::builtin_hint_processor_definition::BuiltinHintProcessor, relocatable, serde::deserialize_program::{Identifier, ReferenceManager}, - types::instance_definitions::bitwise_instance_def::BitwiseInstanceDef, utils::test_utils::*, vm::trace::trace_entry::TraceEntry, }; @@ -3789,8 +3785,7 @@ mod tests { let mut vm = vm!(); vm.current_step = 8192; - vm.builtin_runners = - vec![BitwiseBuiltinRunner::new(&BitwiseInstanceDef::default(), true).into()]; + vm.builtin_runners = vec![BitwiseBuiltinRunner::new(Some(256), true).into()]; assert_matches!(cairo_runner.check_diluted_check_usage(&vm), Ok(())); } @@ -4858,7 +4853,7 @@ mod tests { cairo_runner.segments_finalized = false; let mut vm = vm!(); let output_builtin = OutputBuiltinRunner::new(true); - let bitwise_builtin = BitwiseBuiltinRunner::new(&BitwiseInstanceDef::default(), true); + let bitwise_builtin = BitwiseBuiltinRunner::new(Some(256), true); vm.builtin_runners.push(output_builtin.into()); vm.builtin_runners.push(bitwise_builtin.into()); cairo_runner.initialize_segments(&mut vm, None); diff --git a/vm/src/vm/vm_core.rs b/vm/src/vm/vm_core.rs index eac5544536..42a02b80fb 100644 --- a/vm/src/vm/vm_core.rs +++ b/vm/src/vm/vm_core.rs @@ -1256,9 +1256,6 @@ mod tests { }, relocatable, types::{ - instance_definitions::{ - bitwise_instance_def::BitwiseInstanceDef, ec_op_instance_def::EcOpInstanceDef, - }, instruction::{Op1Addr, Register}, relocatable::Relocatable, }, @@ -3380,7 +3377,7 @@ mod tests { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn deduce_memory_cell_bitwise_builtin_valid_and() { let mut vm = vm!(); - let builtin = BitwiseBuiltinRunner::new(&BitwiseInstanceDef::default(), true); + let builtin = BitwiseBuiltinRunner::new(Some(256), true); vm.builtin_runners.push(builtin.into()); vm.segments = segments![((0, 5), 10), ((0, 6), 12), ((0, 7), 0)]; assert_matches!( @@ -3418,7 +3415,7 @@ mod tests { opcode: Opcode::AssertEq, }; - let mut builtin = BitwiseBuiltinRunner::new(&BitwiseInstanceDef::default(), true); + let mut builtin = BitwiseBuiltinRunner::new(Some(256), true); builtin.base = 2; let mut vm = vm!(); @@ -3459,7 +3456,7 @@ mod tests { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn deduce_memory_cell_ec_op_builtin_valid() { let mut vm = vm!(); - let builtin = EcOpBuiltinRunner::new(&EcOpInstanceDef::default(), true); + let builtin = EcOpBuiltinRunner::new(Some(256), true); vm.builtin_runners.push(builtin.into()); vm.segments = segments![ @@ -3528,7 +3525,7 @@ mod tests { end */ fn verify_auto_deductions_for_ec_op_builtin_valid() { - let mut builtin = EcOpBuiltinRunner::new(&EcOpInstanceDef::default(), true); + let mut builtin = EcOpBuiltinRunner::new(Some(256), true); builtin.base = 3; let mut vm = vm!(); vm.builtin_runners.push(builtin.into()); @@ -3576,7 +3573,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn verify_auto_deductions_for_ec_op_builtin_valid_points_invalid_result() { - let mut builtin = EcOpBuiltinRunner::new(&EcOpInstanceDef::default(), true); + let mut builtin = EcOpBuiltinRunner::new(Some(256), true); builtin.base = 3; let mut vm = vm!(); vm.builtin_runners.push(builtin.into()); @@ -3647,7 +3644,7 @@ mod tests { end */ fn verify_auto_deductions_bitwise() { - let mut builtin = BitwiseBuiltinRunner::new(&BitwiseInstanceDef::default(), true); + let mut builtin = BitwiseBuiltinRunner::new(Some(256), true); builtin.base = 2; let mut vm = vm!(); vm.builtin_runners.push(builtin.into()); @@ -3670,7 +3667,7 @@ mod tests { end */ fn verify_auto_deductions_for_addr_bitwise() { - let mut builtin = BitwiseBuiltinRunner::new(&BitwiseInstanceDef::default(), true); + let mut builtin = BitwiseBuiltinRunner::new(Some(256), true); builtin.base = 2; let builtin: BuiltinRunner = builtin.into(); let mut vm = vm!(); @@ -3860,7 +3857,7 @@ mod tests { fn test_get_builtin_runners() { let mut vm = vm!(); let hash_builtin = HashBuiltinRunner::new(Some(8), true); - let bitwise_builtin = BitwiseBuiltinRunner::new(&BitwiseInstanceDef::default(), true); + let bitwise_builtin = BitwiseBuiltinRunner::new(Some(256), true); vm.builtin_runners.push(hash_builtin.into()); vm.builtin_runners.push(bitwise_builtin.into()); diff --git a/vm/src/vm/vm_memory/memory.rs b/vm/src/vm/vm_memory/memory.rs index c776fa14d3..2ea342573b 100644 --- a/vm/src/vm/vm_memory/memory.rs +++ b/vm/src/vm/vm_memory/memory.rs @@ -629,7 +629,6 @@ mod memory_tests { use super::*; use crate::{ felt_hex, relocatable, - types::instance_definitions::ecdsa_instance_def::EcdsaInstanceDef, utils::test_utils::*, vm::{ runners::builtin_runner::{ @@ -855,7 +854,7 @@ mod memory_tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn validate_existing_memory_for_invalid_signature() { - let mut builtin = SignatureBuiltinRunner::new(&EcdsaInstanceDef::default(), true); + let mut builtin = SignatureBuiltinRunner::new(Some(512), true); let mut segments = MemorySegmentManager::new(); builtin.initialize_segments(&mut segments); segments.memory = memory![ @@ -885,7 +884,7 @@ mod memory_tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn validate_existing_memory_for_valid_signature() { - let mut builtin = SignatureBuiltinRunner::new(&EcdsaInstanceDef::default(), true); + let mut builtin = SignatureBuiltinRunner::new(Some(512), true); let signature_r = felt_hex!("0x411494b501a98abd8262b0da1351e17899a0c4ef23dd2f96fec5ba847310b20");