diff --git a/CHANGELOG.md b/CHANGELOG.md index e45de2e562..0864308e17 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,8 @@ * Add instructions to the proof_mode header to copy return values to the output segment before initiating the infinite loop * Output builtin is now always included when running cairo 1 programs in proof_mode +* feat: Make test_utils macros public [#1599](https://github.com/lambdaclass/cairo-vm/pull/1599). + * feat(BREAKING): Remove unecessary conversion functions between `Felt` & `BigUint`/`BigInt` [#1562](https://github.com/lambdaclass/cairo-vm/pull/1562) * Remove the following functions: * felt_from_biguint diff --git a/vm/src/hint_processor/builtin_hint_processor/bigint.rs b/vm/src/hint_processor/builtin_hint_processor/bigint.rs index 2069446fb7..293da76ea7 100644 --- a/vm/src/hint_processor/builtin_hint_processor/bigint.rs +++ b/vm/src/hint_processor/builtin_hint_processor/bigint.rs @@ -105,6 +105,7 @@ mod test { use crate::hint_processor::hint_processor_definition::{HintProcessorLogic, HintReference}; use crate::stdlib::collections::HashMap; use crate::types::exec_scope::ExecutionScopes; + use crate::utils::test_utils::bigint::*; use crate::utils::test_utils::*; use crate::vm::vm_core::VirtualMachine; use assert_matches::assert_matches; diff --git a/vm/src/hint_processor/builtin_hint_processor/secp/bigint_utils.rs b/vm/src/hint_processor/builtin_hint_processor/secp/bigint_utils.rs index 5b32f4e691..35f01784a2 100644 --- a/vm/src/hint_processor/builtin_hint_processor/secp/bigint_utils.rs +++ b/vm/src/hint_processor/builtin_hint_processor/secp/bigint_utils.rs @@ -192,6 +192,7 @@ mod tests { use crate::{any_box, felt_str}; use crate::types::relocatable::Relocatable; + use crate::utils::test_utils::bigint::*; use crate::utils::test_utils::*; use crate::vm::vm_core::VirtualMachine; diff --git a/vm/src/hint_processor/builtin_hint_processor/secp/ec_utils.rs b/vm/src/hint_processor/builtin_hint_processor/secp/ec_utils.rs index d4dbc3b910..60592c8f45 100644 --- a/vm/src/hint_processor/builtin_hint_processor/secp/ec_utils.rs +++ b/vm/src/hint_processor/builtin_hint_processor/secp/ec_utils.rs @@ -556,6 +556,7 @@ mod tests { hint_processor_definition::HintProcessorLogic, }, types::{exec_scope::ExecutionScopes, relocatable::Relocatable}, + utils::test_utils::bigint::*, utils::test_utils::*, vm::vm_core::VirtualMachine, }; diff --git a/vm/src/hint_processor/builtin_hint_processor/secp/field_utils.rs b/vm/src/hint_processor/builtin_hint_processor/secp/field_utils.rs index fb620f6eab..090edc9955 100644 --- a/vm/src/hint_processor/builtin_hint_processor/secp/field_utils.rs +++ b/vm/src/hint_processor/builtin_hint_processor/secp/field_utils.rs @@ -225,6 +225,7 @@ mod tests { exec_scope::ExecutionScopes, relocatable::{MaybeRelocatable, Relocatable}, }, + utils::test_utils::bigint::*, utils::test_utils::*, vm::errors::memory_errors::MemoryError, }; diff --git a/vm/src/hint_processor/builtin_hint_processor/secp/secp_utils.rs b/vm/src/hint_processor/builtin_hint_processor/secp/secp_utils.rs index 7f34ea4436..fc4d57eea2 100644 --- a/vm/src/hint_processor/builtin_hint_processor/secp/secp_utils.rs +++ b/vm/src/hint_processor/builtin_hint_processor/secp/secp_utils.rs @@ -92,7 +92,7 @@ mod tests { use super::*; use crate::stdlib::{collections::HashMap, string::ToString}; - use crate::utils::test_utils::*; + use crate::utils::test_utils::bigint::*; use assert_matches::assert_matches; use num_bigint::BigUint; diff --git a/vm/src/hint_processor/builtin_hint_processor/secp/signature.rs b/vm/src/hint_processor/builtin_hint_processor/secp/signature.rs index aee4450cc9..bb08ac7213 100644 --- a/vm/src/hint_processor/builtin_hint_processor/secp/signature.rs +++ b/vm/src/hint_processor/builtin_hint_processor/secp/signature.rs @@ -177,6 +177,7 @@ mod tests { hint_processor_definition::HintProcessorLogic, }, types::exec_scope::ExecutionScopes, + utils::test_utils::bigint::*, utils::test_utils::*, }; use assert_matches::assert_matches; diff --git a/vm/src/math_utils/mod.rs b/vm/src/math_utils/mod.rs index c464cb7228..6183a535fe 100644 --- a/vm/src/math_utils/mod.rs +++ b/vm/src/math_utils/mod.rs @@ -374,6 +374,7 @@ pub(crate) fn is_quad_residue(a: &BigUint, p: &BigUint) -> Result (usize, usize) { } } -#[cfg(test)] +#[cfg(any(test, feature = "test_utils"))] #[macro_use] pub mod test_utils { use crate::types::exec_scope::ExecutionScopes; use crate::types::relocatable::MaybeRelocatable; use crate::vm::trace::trace_entry::TraceEntry; + #[cfg(test)] + pub(crate) mod bigint { + #[macro_export] + macro_rules! bigint { + ($val : expr) => { + Into::::into($val) + }; + } + pub(crate) use bigint; + + #[macro_export] + macro_rules! bigint_str { + ($val: expr) => { + num_bigint::BigInt::parse_bytes($val.as_bytes(), 10).expect("Couldn't parse bytes") + }; + ($val: expr, $opt: expr) => { + num_bigint::BigInt::parse_bytes($val.as_bytes(), $opt) + .expect("Couldn't parse bytes") + }; + } + pub(crate) use bigint_str; + + #[macro_export] + macro_rules! biguint { + ($val : expr) => { + Into::::into($val as u128) + }; + } + pub(crate) use biguint; + } + #[macro_export] macro_rules! felt_hex { ($val: expr) => { @@ -70,33 +101,6 @@ pub mod test_utils { }; } - #[macro_export] - macro_rules! bigint { - ($val : expr) => { - Into::::into($val) - }; - } - pub(crate) use bigint; - - #[macro_export] - macro_rules! bigint_str { - ($val: expr) => { - num_bigint::BigInt::parse_bytes($val.as_bytes(), 10).expect("Couldn't parse bytes") - }; - ($val: expr, $opt: expr) => { - num_bigint::BigInt::parse_bytes($val.as_bytes(), $opt).expect("Couldn't parse bytes") - }; - } - pub(crate) use bigint_str; - - #[macro_export] - macro_rules! biguint { - ($val : expr) => { - Into::::into($val as u128) - }; - } - pub(crate) use biguint; - #[macro_export] macro_rules! biguint_str { ($val: expr) => { @@ -106,7 +110,7 @@ pub mod test_utils { num_bigint::BigUint::parse_bytes($val.as_bytes(), $opt).expect("Couldn't parse bytes") }; } - pub(crate) use biguint_str; + pub use biguint_str; impl From<(&str, u8)> for MaybeRelocatable { fn from((string, radix): (&str, u8)) -> Self { @@ -118,6 +122,7 @@ pub mod test_utils { } } + #[macro_export] macro_rules! segments { ($( (($si:expr, $off:expr), $val:tt) ),* $(,)? ) => { { @@ -133,8 +138,9 @@ pub mod test_utils { }; } - pub(crate) use segments; + pub use segments; + #[macro_export] macro_rules! memory { ( $( (($si:expr, $off:expr), $val:tt) ),* ) => { { @@ -144,8 +150,9 @@ pub mod test_utils { } }; } - pub(crate) use memory; + pub use memory; + #[macro_export] macro_rules! memory_from_memory { ($mem: expr, ( $( (($si:expr, $off:expr), $val:tt) ),* )) => { { @@ -155,8 +162,9 @@ pub mod test_utils { } }; } - pub(crate) use memory_from_memory; + pub use memory_from_memory; + #[macro_export] macro_rules! memory_inner { ($mem:expr, ($si:expr, $off:expr), ($sival:expr, $offval: expr)) => { let (k, v) = (($si, $off).into(), &mayberelocatable!($sival, $offval)); @@ -189,8 +197,9 @@ pub mod test_utils { } }; } - pub(crate) use memory_inner; + pub use memory_inner; + #[macro_export] macro_rules! check_memory { ( $mem: expr, $( (($si:expr, $off:expr), $val:tt) ),* $(,)? ) => { $( @@ -198,8 +207,9 @@ pub mod test_utils { )* }; } - pub(crate) use check_memory; + pub use check_memory; + #[macro_export] macro_rules! check_memory_address { ($mem:expr, ($si:expr, $off:expr), ($sival:expr, $offval: expr)) => { assert_eq!( @@ -214,29 +224,33 @@ pub mod test_utils { ) }; } - pub(crate) use check_memory_address; + pub use check_memory_address; + #[macro_export] macro_rules! mayberelocatable { ($val1 : expr, $val2 : expr) => { $crate::types::relocatable::MaybeRelocatable::from(($val1, $val2)) }; ($val1 : expr) => { - $crate::types::relocatable::MaybeRelocatable::from(crate::Felt252::from($val1 as i128)) + $crate::types::relocatable::MaybeRelocatable::from($crate::Felt252::from($val1 as i128)) }; } - pub(crate) use mayberelocatable; + pub use mayberelocatable; + #[macro_export] macro_rules! references { ($num: expr) => {{ - let mut references = crate::stdlib::collections::HashMap::::new(); + let mut references = + $crate::stdlib::collections::HashMap::::new(); for i in 0..$num { references.insert(i as usize, HintReference::new_simple((i as i32 - $num))); } references }}; } - pub(crate) use references; + pub use references; + #[macro_export] macro_rules! vm_with_range_check { () => {{ let mut vm = VirtualMachine::new(false); @@ -247,8 +261,9 @@ pub mod test_utils { vm }}; } - pub(crate) use vm_with_range_check; + pub use vm_with_range_check; + #[macro_export] macro_rules! cairo_runner { ($program:expr) => { CairoRunner::new(&$program, "all_cairo", false).unwrap() @@ -263,12 +278,16 @@ pub mod test_utils { CairoRunner::new(&program, $layout.to_string(), proof_mode).unwrap() }; } - pub(crate) use cairo_runner; + pub use cairo_runner; - pub(crate) use crate::stdlib::{collections::BTreeMap, sync::Arc}; + #[allow(unused_imports)] + pub(crate) use crate::stdlib::collections::BTreeMap; + pub(crate) use crate::stdlib::sync::Arc; pub(crate) use crate::types::program::HintsCollection; pub(crate) use crate::types::program::Program; pub(crate) use crate::types::program::SharedProgramData; + + #[macro_export] macro_rules! program { //Empty program () => { @@ -277,27 +296,27 @@ pub mod test_utils { //Program with builtins ( $( $builtin_name: expr ),* ) => {{ let shared_program_data = SharedProgramData { - data: crate::stdlib::vec::Vec::new(), + data: $crate::stdlib::vec::Vec::new(), hints_collection: HintsCollection::new(&BTreeMap::new(), 0).unwrap(), main: None, start: None, end: None, - error_message_attributes: crate::stdlib::vec::Vec::new(), + error_message_attributes: $crate::stdlib::vec::Vec::new(), instruction_locations: None, - identifiers: crate::stdlib::collections::HashMap::new(), + identifiers: $crate::stdlib::collections::HashMap::new(), reference_manager: Program::get_reference_list(&ReferenceManager { - references: crate::stdlib::vec::Vec::new(), + references: $crate::stdlib::vec::Vec::new(), }), }; Program { shared_program_data: Arc::new(shared_program_data), - constants: crate::stdlib::collections::HashMap::new(), + constants: $crate::stdlib::collections::HashMap::new(), builtins: vec![$( $builtin_name ),*], } }}; ($($field:ident = $value:expr),* $(,)?) => {{ - let program_flat = crate::utils::test_utils::ProgramFlat { + let program_flat = $crate::utils::test_utils::ProgramFlat { $( $field: $value, )* @@ -308,7 +327,7 @@ pub mod test_utils { }}; } - pub(crate) use program; + pub use program; pub(crate) struct ProgramFlat { pub(crate) data: crate::utils::Vec, @@ -381,6 +400,7 @@ pub mod test_utils { } } + #[macro_export] macro_rules! vm { () => {{ VirtualMachine::new(false) @@ -390,8 +410,9 @@ pub mod test_utils { VirtualMachine::new($use_trace) }}; } - pub(crate) use vm; + pub use vm; + #[macro_export] macro_rules! run_context { ( $vm: expr, $pc: expr, $ap: expr, $fp: expr ) => { $vm.run_context.pc = Relocatable::from((0, $pc)); @@ -399,38 +420,40 @@ pub mod test_utils { $vm.run_context.fp = $fp; }; } - pub(crate) use run_context; + pub use run_context; + #[macro_export] macro_rules! ids_data { ( $( $name: expr ),* ) => { { let ids_names = vec![$( $name ),*]; let references = references!(ids_names.len() as i32); - let mut ids_data = crate::stdlib::collections::HashMap::::new(); + let mut ids_data = $crate::stdlib::collections::HashMap::<$crate::stdlib::string::String, HintReference>::new(); for (i, name) in ids_names.iter().enumerate() { - ids_data.insert(crate::stdlib::string::ToString::to_string(name), references.get(&i).unwrap().clone()); + ids_data.insert($crate::stdlib::string::ToString::to_string(name), references.get(&i).unwrap().clone()); } ids_data } }; } - pub(crate) use ids_data; + pub use ids_data; + #[macro_export] macro_rules! non_continuous_ids_data { ( $( ($name: expr, $offset:expr) ),* $(,)? ) => { { - let mut ids_data = crate::stdlib::collections::HashMap::::new(); + let mut ids_data = $crate::stdlib::collections::HashMap::<$crate::stdlib::string::String, HintReference>::new(); $( - ids_data.insert(crate::stdlib::string::String::from($name), HintReference::new_simple($offset)); + ids_data.insert($crate::stdlib::string::String::from($name), HintReference::new_simple($offset)); )* ids_data } }; } - pub(crate) use non_continuous_ids_data; + pub use non_continuous_ids_data; #[track_caller] - pub(crate) fn trace_check( + pub fn trace_check( actual: &[TraceEntry], expected: &[(crate::utils::Relocatable, usize, usize)], ) { @@ -440,13 +463,15 @@ pub mod test_utils { } } + #[macro_export] macro_rules! exec_scopes_ref { () => { &mut ExecutionScopes::new() }; } - pub(crate) use exec_scopes_ref; + pub use exec_scopes_ref; + #[macro_export] macro_rules! run_hint { ($vm:expr, $ids_data:expr, $hint_code:expr, $exec_scopes:expr, $constants:expr) => {{ let hint_data = HintProcessorData::new_default($hint_code.to_string(), $ids_data); @@ -455,7 +480,7 @@ pub mod test_utils { }}; ($vm:expr, $ids_data:expr, $hint_code:expr, $exec_scopes:expr) => {{ let hint_data = HintProcessorData::new_default( - crate::stdlib::string::ToString::to_string($hint_code), + $crate::stdlib::string::ToString::to_string($hint_code), $ids_data, ); let mut hint_processor = BuiltinHintProcessor::new_empty(); @@ -463,12 +488,12 @@ pub mod test_utils { &mut $vm, $exec_scopes, &any_box!(hint_data), - &crate::stdlib::collections::HashMap::new(), + &$crate::stdlib::collections::HashMap::new(), ) }}; ($vm:expr, $ids_data:expr, $hint_code:expr) => {{ let hint_data = HintProcessorData::new_default( - crate::stdlib::string::ToString::to_string($hint_code), + $crate::stdlib::string::ToString::to_string($hint_code), $ids_data, ); let mut hint_processor = BuiltinHintProcessor::new_empty(); @@ -476,12 +501,13 @@ pub mod test_utils { &mut $vm, exec_scopes_ref!(), &any_box!(hint_data), - &crate::stdlib::collections::HashMap::new(), + &$crate::stdlib::collections::HashMap::new(), ) }}; } - pub(crate) use run_hint; + pub use run_hint; + #[macro_export] macro_rules! add_segments { ($vm:expr, $n:expr) => { for _ in 0..$n { @@ -489,8 +515,9 @@ pub mod test_utils { } }; } - pub(crate) use add_segments; + pub use add_segments; + #[macro_export] macro_rules! check_scope { ( $exec_scope: expr, [ $( ($name: expr, $val: expr)),*$(,)? ] $(,)? ) => { $( @@ -498,8 +525,9 @@ pub mod test_utils { )* }; } - pub(crate) use check_scope; + pub use check_scope; + #[macro_export] macro_rules! scope { () => { ExecutionScopes::new() }; ( $( ($name: expr, $val: expr)),* $(,)? ) => { @@ -515,8 +543,9 @@ pub mod test_utils { } }; } - pub(crate) use scope; + pub use scope; + #[macro_export] macro_rules! check_dictionary { ( $exec_scopes: expr, $tracker_num:expr, $( ($key:expr, $val:expr )),* ) => { $( @@ -534,8 +563,9 @@ pub mod test_utils { * }; } - pub(crate) use check_dictionary; + pub use check_dictionary; + #[macro_export] macro_rules! check_dict_ptr { ($exec_scopes: expr, $tracker_num: expr, ($i:expr, $off:expr)) => { assert_eq!( @@ -551,8 +581,9 @@ pub mod test_utils { ); }; } - pub(crate) use check_dict_ptr; + pub use check_dict_ptr; + #[macro_export] macro_rules! dict_manager { ($exec_scopes:expr, $tracker_num:expr, $( ($key:expr, $val:expr )),* ) => { let mut tracker = DictTracker::new_empty(relocatable!($tracker_num, 0)); @@ -561,18 +592,19 @@ pub mod test_utils { )* let mut dict_manager = DictManager::new(); dict_manager.trackers.insert(2, tracker); - $exec_scopes.insert_value("dict_manager", crate::stdlib::rc::Rc::new(core::cell::RefCell::new(dict_manager))) + $exec_scopes.insert_value("dict_manager", $crate::stdlib::rc::Rc::new(core::cell::RefCell::new(dict_manager))) }; ($exec_scopes:expr, $tracker_num:expr) => { let tracker = DictTracker::new_empty(relocatable!($tracker_num, 0)); let mut dict_manager = DictManager::new(); dict_manager.trackers.insert(2, tracker); - $exec_scopes.insert_value("dict_manager", crate::stdlib::rc::Rc::new(core::cell::RefCell::new(dict_manager))) + $exec_scopes.insert_value("dict_manager", $crate::stdlib::rc::Rc::new(core::cell::RefCell::new(dict_manager))) }; } - pub(crate) use dict_manager; + pub use dict_manager; + #[macro_export] macro_rules! dict_manager_default { ($exec_scopes:expr, $tracker_num:expr,$default:expr, $( ($key:expr, $val:expr )),* ) => { let mut tracker = DictTracker::new_default_dict(relocatable!($tracker_num, 0), &MaybeRelocatable::from($default), None); @@ -581,24 +613,26 @@ pub mod test_utils { )* let mut dict_manager = DictManager::new(); dict_manager.trackers.insert(2, tracker); - $exec_scopes.insert_value("dict_manager", crate::stdlib::rc::Rc::new(core::cell::RefCell::new(dict_manager))) + $exec_scopes.insert_value("dict_manager", $crate::stdlib::rc::Rc::new(core::cell::RefCell::new(dict_manager))) }; ($exec_scopes:expr, $tracker_num:expr,$default:expr) => { let tracker = DictTracker::new_default_dict(relocatable!($tracker_num, 0), &MaybeRelocatable::from($default), None); let mut dict_manager = DictManager::new(); dict_manager.trackers.insert(2, tracker); - $exec_scopes.insert_value("dict_manager", crate::stdlib::rc::Rc::new(core::cell::RefCell::new(dict_manager))) + $exec_scopes.insert_value("dict_manager", $crate::stdlib::rc::Rc::new(core::cell::RefCell::new(dict_manager))) }; } - pub(crate) use dict_manager_default; + pub use dict_manager_default; + #[macro_export] macro_rules! vec_data { ( $( ($val:tt) ),* ) => { vec![$( vec_data_inner!($val) ),*] }; } - pub(crate) use vec_data; + pub use vec_data; + #[macro_export] macro_rules! vec_data_inner { (( $val1:expr, $val2:expr )) => { mayberelocatable!($val1, $val2) @@ -607,7 +641,7 @@ pub mod test_utils { mayberelocatable!($val) }; } - pub(crate) use vec_data_inner; + pub use vec_data_inner; pub fn check_scope_value( scopes: &ExecutionScopes, diff --git a/vm/src/vm/vm_memory/memory.rs b/vm/src/vm/vm_memory/memory.rs index f50d5c11cd..b8dcda910e 100644 --- a/vm/src/vm/vm_memory/memory.rs +++ b/vm/src/vm/vm_memory/memory.rs @@ -17,7 +17,7 @@ pub struct ValidationRule( ); #[derive(Clone, Eq, Ord, PartialEq, PartialOrd, Debug)] -pub(crate) struct MemoryCell(MaybeRelocatable, bool); +pub struct MemoryCell(MaybeRelocatable, bool); impl MemoryCell { pub fn new(value: MaybeRelocatable) -> Self { @@ -92,8 +92,8 @@ impl AddressSet { } pub struct Memory { - pub(crate) data: Vec>>, - pub(crate) temp_data: Vec>>, + pub data: Vec>>, + pub temp_data: Vec>>, // relocation_rules's keys map to temp_data's indices and therefore begin at // zero; that is, segment_index = -1 maps to key 0, -2 to key 1... pub(crate) relocation_rules: HashMap, diff --git a/vm/src/vm/vm_memory/memory_segments.rs b/vm/src/vm/vm_memory/memory_segments.rs index cabba6266b..54564136f2 100644 --- a/vm/src/vm/vm_memory/memory_segments.rs +++ b/vm/src/vm/vm_memory/memory_segments.rs @@ -15,7 +15,7 @@ use crate::{ pub struct MemorySegmentManager { pub segment_sizes: HashMap, pub segment_used_sizes: Option>, - pub(crate) memory: Memory, + pub memory: Memory, // A map from segment index to a list of pairs (offset, page_id) that constitute the // public memory. Note that the offset is absolute (not based on the page_id). pub public_memory_offsets: HashMap>,