From 17716c48c381da94fa833ed3c4961f9f662b38da Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 20 Apr 2023 12:57:42 -0300 Subject: [PATCH 01/41] Add test --- src/vm/runners/cairo_runner.rs | 41 +++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/src/vm/runners/cairo_runner.rs b/src/vm/runners/cairo_runner.rs index 66085e9163..d4fe01d5f8 100644 --- a/src/vm/runners/cairo_runner.rs +++ b/src/vm/runners/cairo_runner.rs @@ -4481,35 +4481,54 @@ mod tests { ), Ok(()) ); + } - let mut new_cairo_runner = cairo_runner!(program); - let mut new_vm = vm!(true); //this true expression dictates that the trace is enabled + #[test] + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] + fn run_from_entrypoint_duck_duck() { + let program = Program::from_bytes( + include_bytes!("../../../cairo_programs/xor_func.json"), + None, + ) + .unwrap(); + let mut cairo_runner = cairo_runner!(program); + let mut vm = vm!(true); //this true expression dictates that the trace is enabled let mut hint_processor = BuiltinHintProcessor::new_empty(); - new_cairo_runner.initialize_builtins(&mut new_vm).unwrap(); - new_cairo_runner.initialize_segments(&mut new_vm, None); - - let fib_entrypoint = program + //this entrypoint tells which function to run in the cairo program + let main_entrypoint = program .identifiers - .get("__main__.evaluate_fib") + .get("__main__.xor_counters") .unwrap() .pc .unwrap(); + cairo_runner + .initialize_function_runner(&mut vm, false) + .unwrap(); + + vm.mark_address_range_as_accessed( + cairo_runner.program_base.unwrap(), + cairo_runner.program.data_len(), + ) + .unwrap(); assert_matches!( - new_cairo_runner.run_from_entrypoint( - fib_entrypoint, + cairo_runner.run_from_entrypoint( + main_entrypoint, &[ &mayberelocatable!(2).into(), &MaybeRelocatable::from((2, 0)).into() - ], + ], //bitwise_ptr true, None, - &mut new_vm, + &mut vm, &mut hint_processor, ), Ok(()) ); + + let memory_holes = cairo_runner.get_memory_holes(&vm).unwrap(); + dbg!(&memory_holes); } #[test] From e867c2b23529981c28f5d70850aa2e550f4a565b Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 20 Apr 2023 13:35:46 -0300 Subject: [PATCH 02/41] Ignore builtin segments in get_memory_holes --- src/vm/runners/cairo_runner.rs | 2 +- src/vm/vm_memory/memory_segments.rs | 20 +++++++++++++------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/vm/runners/cairo_runner.rs b/src/vm/runners/cairo_runner.rs index d4fe01d5f8..a64cec3a06 100644 --- a/src/vm/runners/cairo_runner.rs +++ b/src/vm/runners/cairo_runner.rs @@ -642,7 +642,7 @@ impl CairoRunner { /// Count the number of holes present in the segments. pub fn get_memory_holes(&self, vm: &VirtualMachine) -> Result { - vm.segments.get_memory_holes() + vm.segments.get_memory_holes(vm.builtin_runners.len()) } /// Check if there are enough trace cells to fill the entire diluted checks. diff --git a/src/vm/vm_memory/memory_segments.rs b/src/vm/vm_memory/memory_segments.rs index 492cbfc76d..e175722b6e 100644 --- a/src/vm/vm_memory/memory_segments.rs +++ b/src/vm/vm_memory/memory_segments.rs @@ -181,12 +181,18 @@ impl MemorySegmentManager { } } - pub fn get_memory_holes(&self) -> Result { + pub fn get_memory_holes(&self, builtin_count: usize) -> Result { let data = &self.memory.data; let mut memory_holes = 0; + let builtin_segments_start = 2; // program segment + execution segment + let builtin_segments_end = builtin_segments_start + builtin_count; // Count the memory holes for each segment by substracting the amount of accessed_addresses from the segment's size // Segments without accesses addresses are not accounted for when counting memory holes for i in 0..data.len() { + // Instead of marking all of the builtin segment's address as accessed, we just skip them when counting memory holes + if i > builtin_segments_start || i < builtin_segments_end { + continue + } let accessed_amount = match self.memory.get_amount_of_accessed_addresses_for_segment(i) { Some(accessed_amount) if accessed_amount > 0 => accessed_amount, @@ -594,7 +600,7 @@ mod tests { .memory .mark_as_accessed((0, 0).into()); assert_eq!( - memory_segment_manager.get_memory_holes(), + memory_segment_manager.get_memory_holes(0), Err(MemoryError::MissingSegmentUsedSizes), ); } @@ -611,7 +617,7 @@ mod tests { .mark_as_accessed((0, i).into()); } assert_eq!( - memory_segment_manager.get_memory_holes(), + memory_segment_manager.get_memory_holes(0), Err(MemoryError::SegmentHasMoreAccessedAddressesThanSize( 0, 3, 2 )), @@ -623,7 +629,7 @@ mod tests { fn get_memory_holes_empty() { let mut memory_segment_manager = MemorySegmentManager::new(); memory_segment_manager.segment_used_sizes = Some(Vec::new()); - assert_eq!(memory_segment_manager.get_memory_holes(), Ok(0),); + assert_eq!(memory_segment_manager.get_memory_holes(0), Ok(0),); } #[test] @@ -631,7 +637,7 @@ mod tests { fn get_memory_holes_empty2() { let mut memory_segment_manager = MemorySegmentManager::new(); memory_segment_manager.segment_used_sizes = Some(vec![4]); - assert_eq!(memory_segment_manager.get_memory_holes(), Ok(0),); + assert_eq!(memory_segment_manager.get_memory_holes(0), Ok(0),); } #[test] @@ -654,7 +660,7 @@ mod tests { .memory .mark_as_accessed((0, i).into()); } - assert_eq!(memory_segment_manager.get_memory_holes(), Ok(2),); + assert_eq!(memory_segment_manager.get_memory_holes(0), Ok(2),); } #[test] @@ -679,7 +685,7 @@ mod tests { .memory .mark_as_accessed((0, i).into()); } - assert_eq!(memory_segment_manager.get_memory_holes(), Ok(7),); + assert_eq!(memory_segment_manager.get_memory_holes(0), Ok(7),); } #[test] From c56049759c12dfce40d116aaf412c72623bd8634 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 20 Apr 2023 13:36:13 -0300 Subject: [PATCH 03/41] Improve test --- src/vm/runners/cairo_runner.rs | 20 +++++++------------- src/vm/vm_memory/memory_segments.rs | 2 +- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/src/vm/runners/cairo_runner.rs b/src/vm/runners/cairo_runner.rs index a64cec3a06..ab515e8dd2 100644 --- a/src/vm/runners/cairo_runner.rs +++ b/src/vm/runners/cairo_runner.rs @@ -4485,9 +4485,9 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] - fn run_from_entrypoint_duck_duck() { + fn run_from_entrypoint_bitwise_test_check_memory_holes() { let program = Program::from_bytes( - include_bytes!("../../../cairo_programs/xor_func.json"), + include_bytes!("../../../cairo_programs/bitwise_builtin_test.json"), None, ) .unwrap(); @@ -4498,7 +4498,7 @@ mod tests { //this entrypoint tells which function to run in the cairo program let main_entrypoint = program .identifiers - .get("__main__.xor_counters") + .get("__main__.main") .unwrap() .pc .unwrap(); @@ -4507,18 +4507,12 @@ mod tests { .initialize_function_runner(&mut vm, false) .unwrap(); - vm.mark_address_range_as_accessed( - cairo_runner.program_base.unwrap(), - cairo_runner.program.data_len(), - ) - .unwrap(); assert_matches!( cairo_runner.run_from_entrypoint( main_entrypoint, &[ - &mayberelocatable!(2).into(), - &MaybeRelocatable::from((2, 0)).into() - ], //bitwise_ptr + &MaybeRelocatable::from((2, 0)).into() //bitwise_ptr + ], true, None, &mut vm, @@ -4527,8 +4521,8 @@ mod tests { Ok(()) ); - let memory_holes = cairo_runner.get_memory_holes(&vm).unwrap(); - dbg!(&memory_holes); + // Check that memory_holes == 0 + assert!(cairo_runner.get_memory_holes(&vm).unwrap().is_zero()); } #[test] diff --git a/src/vm/vm_memory/memory_segments.rs b/src/vm/vm_memory/memory_segments.rs index e175722b6e..6cf277832c 100644 --- a/src/vm/vm_memory/memory_segments.rs +++ b/src/vm/vm_memory/memory_segments.rs @@ -191,7 +191,7 @@ impl MemorySegmentManager { for i in 0..data.len() { // Instead of marking all of the builtin segment's address as accessed, we just skip them when counting memory holes if i > builtin_segments_start || i < builtin_segments_end { - continue + continue; } let accessed_amount = match self.memory.get_amount_of_accessed_addresses_for_segment(i) { From ed5329fe8a3ab4b69d6e8bc50bf38fd2949204e2 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 20 Apr 2023 15:04:41 -0300 Subject: [PATCH 04/41] Add memory holes check for cairo_run_test tests --- src/tests/mod.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/tests/mod.rs b/src/tests/mod.rs index 3597d2a022..c01327b4c9 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -6,6 +6,7 @@ use crate::{ vm::trace::trace_entry::TraceEntry, }; +use num_traits::Zero; #[cfg(target_arch = "wasm32")] use wasm_bindgen_test::*; @@ -54,7 +55,7 @@ pub(self) fn run_program( assert!(res.err().unwrap().to_string().contains(error)); return; } - let (_runner, vm) = res.expect("Execution failed"); + let (runner, vm) = res.expect("Execution failed"); if let Some(trace) = trace { let expected_trace: Vec<_> = trace .iter() @@ -67,4 +68,6 @@ pub(self) fn run_program( assert_eq!(entry, expected); } } + // Check there are no memory holes + assert!(runner.get_memory_holes(&vm).unwrap().is_zero()); } From c15445bf448519a5d67ecc82841493b3ed9918ab Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 20 Apr 2023 15:29:04 -0300 Subject: [PATCH 05/41] Fix broken condition --- src/vm/vm_memory/memory_segments.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/vm/vm_memory/memory_segments.rs b/src/vm/vm_memory/memory_segments.rs index 6cf277832c..cd9df42fd0 100644 --- a/src/vm/vm_memory/memory_segments.rs +++ b/src/vm/vm_memory/memory_segments.rs @@ -190,7 +190,7 @@ impl MemorySegmentManager { // Segments without accesses addresses are not accounted for when counting memory holes for i in 0..data.len() { // Instead of marking all of the builtin segment's address as accessed, we just skip them when counting memory holes - if i > builtin_segments_start || i < builtin_segments_end { + if i > builtin_segments_start && i < builtin_segments_end { continue; } let accessed_amount = match self.memory.get_amount_of_accessed_addresses_for_segment(i) From f252960567b117a1351854017c7256d2e27d60ad Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 20 Apr 2023 15:35:28 -0300 Subject: [PATCH 06/41] Make memory_holes check optional & customizable --- src/tests/mod.rs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/tests/mod.rs b/src/tests/mod.rs index c01327b4c9..e67c9339b1 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -6,7 +6,6 @@ use crate::{ vm::trace::trace_entry::TraceEntry, }; -use num_traits::Zero; #[cfg(target_arch = "wasm32")] use wasm_bindgen_test::*; @@ -20,20 +19,20 @@ mod skip_instruction_test; //For simple programs that should just succeed and have no special needs. pub(self) fn run_program_simple(data: &[u8]) { - run_program(data, Some("all_cairo"), None, None) + run_program(data, Some("all_cairo"), None, None, Some(0)) } //For simple programs that should just succeed but using small layout. pub(self) fn run_program_small(data: &[u8]) { - run_program(data, Some("small"), None, None) + run_program(data, Some("small"), None, None, Some(0)) } pub(self) fn run_program_with_trace(data: &[u8], trace: &[(usize, usize, usize)]) { - run_program(data, Some("all_cairo"), Some(trace), None) + run_program(data, Some("all_cairo"), Some(trace), None, Some(0)) } pub(self) fn run_program_with_error(data: &[u8], error: &str) { - run_program(data, Some("all_cairo"), None, Some(error)) + run_program(data, Some("all_cairo"), None, Some(error), Some(0)) } pub(self) fn run_program( @@ -41,6 +40,7 @@ pub(self) fn run_program( layout: Option<&str>, trace: Option<&[(usize, usize, usize)]>, error: Option<&str>, + memory_holes: Option, ) { let mut hint_executor = BuiltinHintProcessor::new_empty(); let cairo_run_config = CairoRunConfig { @@ -68,6 +68,7 @@ pub(self) fn run_program( assert_eq!(entry, expected); } } - // Check there are no memory holes - assert!(runner.get_memory_holes(&vm).unwrap().is_zero()); + if let Some(holes) = memory_holes { + assert_eq!(runner.get_memory_holes(&vm).unwrap(), holes); + } } From 063f9c48b9769bdc759adbff430619e6caf24e6c Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 20 Apr 2023 15:57:19 -0300 Subject: [PATCH 07/41] Fix bounds --- src/vm/vm_memory/memory_segments.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/vm/vm_memory/memory_segments.rs b/src/vm/vm_memory/memory_segments.rs index cd9df42fd0..5fdda00126 100644 --- a/src/vm/vm_memory/memory_segments.rs +++ b/src/vm/vm_memory/memory_segments.rs @@ -184,13 +184,13 @@ impl MemorySegmentManager { pub fn get_memory_holes(&self, builtin_count: usize) -> Result { let data = &self.memory.data; let mut memory_holes = 0; - let builtin_segments_start = 2; // program segment + execution segment + let builtin_segments_start = 1; // program segment + execution segment let builtin_segments_end = builtin_segments_start + builtin_count; // Count the memory holes for each segment by substracting the amount of accessed_addresses from the segment's size // Segments without accesses addresses are not accounted for when counting memory holes for i in 0..data.len() { // Instead of marking all of the builtin segment's address as accessed, we just skip them when counting memory holes - if i > builtin_segments_start && i < builtin_segments_end { + if i > builtin_segments_start && i <= builtin_segments_end { continue; } let accessed_amount = match self.memory.get_amount_of_accessed_addresses_for_segment(i) From 6e3ed8bc0bd69d974fe631ce36999341a322cb96 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 20 Apr 2023 16:03:29 -0300 Subject: [PATCH 08/41] Add a test with deliberately created memory holes --- cairo_programs/memory_holes.cairo | 5 +++++ src/tests/cairo_run_test.rs | 7 +++++++ 2 files changed, 12 insertions(+) create mode 100644 cairo_programs/memory_holes.cairo diff --git a/cairo_programs/memory_holes.cairo b/cairo_programs/memory_holes.cairo new file mode 100644 index 0000000000..e8b74935b0 --- /dev/null +++ b/cairo_programs/memory_holes.cairo @@ -0,0 +1,5 @@ +func main() { + // Deliberately create memory holes + assert [ap + 5] = 1; + return (); +} diff --git a/src/tests/cairo_run_test.rs b/src/tests/cairo_run_test.rs index 403b006ee0..058e527ab4 100644 --- a/src/tests/cairo_run_test.rs +++ b/src/tests/cairo_run_test.rs @@ -1378,3 +1378,10 @@ fn cairo_run_uint256_improvements() { let program_data = include_bytes!("../../cairo_programs/uint256_improvements.json"); run_program_simple(program_data.as_slice()); } + +#[test] +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] +fn cairo_run_memory_holes() { + let program_data = include_bytes!("../../cairo_programs/memory_holes.json"); + run_program(program_data, Some("all_cairo"), None, None, Some(4)) +} From 9fa2dc895db5d35052f5d14956658be8791fecfd Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 20 Apr 2023 16:10:50 -0300 Subject: [PATCH 09/41] Fix test value --- src/tests/cairo_run_test.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tests/cairo_run_test.rs b/src/tests/cairo_run_test.rs index 058e527ab4..97c36a3603 100644 --- a/src/tests/cairo_run_test.rs +++ b/src/tests/cairo_run_test.rs @@ -1383,5 +1383,5 @@ fn cairo_run_uint256_improvements() { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn cairo_run_memory_holes() { let program_data = include_bytes!("../../cairo_programs/memory_holes.json"); - run_program(program_data, Some("all_cairo"), None, None, Some(4)) + run_program(program_data, Some("all_cairo"), None, None, Some(5)) } From d36bf3f00477af01e8cdf112e537432cba13b631 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 20 Apr 2023 16:32:37 -0300 Subject: [PATCH 10/41] Remove duplicated tests + add memory hole value to some tests --- src/tests/cairo_run_test.rs | 386 +----------------------------------- src/tests/mod.rs | 13 +- 2 files changed, 17 insertions(+), 382 deletions(-) diff --git a/src/tests/cairo_run_test.rs b/src/tests/cairo_run_test.rs index 97c36a3603..5dce3c2ede 100644 --- a/src/tests/cairo_run_test.rs +++ b/src/tests/cairo_run_test.rs @@ -187,7 +187,7 @@ fn compare_arrays() { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn compare_greater_array() { let program_data = include_bytes!("../../cairo_programs/compare_greater_array.json"); - run_program_simple(program_data.as_slice()); + run_program_simple_with_memory_holes(program_data.as_slice(), 177); } #[test] @@ -201,7 +201,7 @@ fn compare_lesser_array() { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn assert_le_felt_hint() { let program_data = include_bytes!("../../cairo_programs/assert_le_felt_hint.json"); - run_program_simple(program_data.as_slice()); + run_program_simple_with_memory_holes(program_data.as_slice(), 2); } #[test] @@ -271,7 +271,7 @@ fn split_felt() { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn math_cmp() { let program_data = include_bytes!("../../cairo_programs/math_cmp.json"); - run_program_simple(program_data.as_slice()); + run_program_simple_with_memory_holes(program_data.as_slice(), 372); } #[test] @@ -292,7 +292,7 @@ fn signed_div_rem() { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn assert_lt_felt() { let program_data = include_bytes!("../../cairo_programs/assert_lt_felt.json"); - run_program_simple(program_data.as_slice()); + run_program_simple_with_memory_holes(program_data.as_slice(), 8); } #[test] @@ -320,7 +320,7 @@ fn pow() { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn dict() { let program_data = include_bytes!("../../cairo_programs/dict.json"); - run_program_simple(program_data.as_slice()); + run_program_simple_with_memory_holes(program_data.as_slice(), 4); } #[test] @@ -404,7 +404,7 @@ fn secp_ec() { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn blake2s_hello_world_hash() { let program_data = include_bytes!("../../cairo_programs/blake2s_hello_world_hash.json"); - run_program_simple(program_data.as_slice()); + run_program_simple_with_memory_holes(program_data.as_slice(), 20); } #[test] @@ -425,7 +425,7 @@ fn unsafe_keccak() { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn blake2s_felts() { let program_data = include_bytes!("../../cairo_programs/blake2s_felts.json"); - run_program_simple(program_data.as_slice()); + run_program_simple_with_memory_holes(program_data.as_slice(), 139); } #[test] @@ -610,364 +610,6 @@ fn error_msg_attr_struct() { run_program_with_error(program_data.as_slice(), error_msg); } -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_test() { - let program_data = include_bytes!("../../cairo_programs/fibonacci.json"); - run_program_small(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_array_sum() { - let program_data = include_bytes!("../../cairo_programs/array_sum.json"); - run_program_small(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_big_struct() { - let program_data = include_bytes!("../../cairo_programs/big_struct.json"); - run_program_small(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_call_function_assign_param_by_name() { - let program_data = - include_bytes!("../../cairo_programs/call_function_assign_param_by_name.json"); - run_program_small(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_function_return() { - let program_data = include_bytes!("../../cairo_programs/function_return.json"); - run_program_small(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_function_return_if_print() { - let program_data = include_bytes!("../../cairo_programs/function_return_if_print.json"); - run_program_small(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_function_return_to_variable() { - let program_data = include_bytes!("../../cairo_programs/function_return_to_variable.json"); - run_program_small(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_if_and_prime() { - let program_data = include_bytes!("../../cairo_programs/if_and_prime.json"); - run_program_small(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_if_in_function() { - let program_data = include_bytes!("../../cairo_programs/if_in_function.json"); - run_program_small(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_if_list() { - let program_data = include_bytes!("../../cairo_programs/if_list.json"); - run_program_small(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_jmp() { - let program_data = include_bytes!("../../cairo_programs/jmp.json"); - run_program_small(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_jmp_if_condition() { - let program_data = include_bytes!("../../cairo_programs/jmp_if_condition.json"); - run_program_small(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_pointers() { - let program_data = include_bytes!("../../cairo_programs/pointers.json"); - run_program_small(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_print() { - let program_data = include_bytes!("../../cairo_programs/print.json"); - run_program_small(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_return() { - let program_data = include_bytes!("../../cairo_programs/return.json"); - run_program_small(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_reversed_register_instructions() { - let program_data = include_bytes!("../../cairo_programs/reversed_register_instructions.json"); - run_program_small(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_simple_print() { - let program_data = include_bytes!("../../cairo_programs/simple_print.json"); - run_program_small(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_test_addition_if() { - let program_data = include_bytes!("../../cairo_programs/test_addition_if.json"); - run_program_small(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_test_reverse_if() { - let program_data = include_bytes!("../../cairo_programs/test_reverse_if.json"); - run_program_small(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_test_subtraction_if() { - let program_data = include_bytes!("../../cairo_programs/test_subtraction_if.json"); - run_program_small(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_use_imported_module() { - let program_data = include_bytes!("../../cairo_programs/use_imported_module.json"); - run_program_simple(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_bitwise_output() { - let program_data = include_bytes!("../../cairo_programs/bitwise_output.json"); - run_program_simple(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_bitwise_recursion() { - let program_data = include_bytes!("../../cairo_programs/bitwise_recursion.json"); - run_program_simple(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_integration() { - let program_data = include_bytes!("../../cairo_programs/integration.json"); - run_program_simple(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_integration_with_alloc_locals() { - let program_data = include_bytes!("../../cairo_programs/integration_with_alloc_locals.json"); - run_program_simple(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_compare_arrays() { - let program_data = include_bytes!("../../cairo_programs/compare_arrays.json"); - run_program_simple(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_compare_greater_array() { - let program_data = include_bytes!("../../cairo_programs/compare_greater_array.json"); - run_program_simple(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_compare_lesser_array() { - let program_data = include_bytes!("../../cairo_programs/compare_lesser_array.json"); - run_program_simple(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_assert_le_felt_hint() { - let program_data = include_bytes!("../../cairo_programs/assert_le_felt_hint.json"); - run_program_simple(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_assert_250_bit_element_array() { - let program_data = include_bytes!("../../cairo_programs/assert_250_bit_element_array.json"); - run_program_simple(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_abs_value() { - let program_data = include_bytes!("../../cairo_programs/abs_value_array.json"); - run_program_simple(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_compare_different_arrays() { - let program_data = include_bytes!("../../cairo_programs/compare_different_arrays.json"); - run_program_simple(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_assert_nn() { - let program_data = include_bytes!("../../cairo_programs/assert_nn.json"); - run_program_simple(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_sqrt() { - let program_data = include_bytes!("../../cairo_programs/sqrt.json"); - run_program_simple(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_assert_not_zero() { - let program_data = include_bytes!("../../cairo_programs/assert_not_zero.json"); - run_program_simple(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_split_int() { - let program_data = include_bytes!("../../cairo_programs/split_int.json"); - run_program_simple(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_split_int_big() { - let program_data = include_bytes!("../../cairo_programs/split_int_big.json"); - run_program_simple(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_split_felt() { - let program_data = include_bytes!("../../cairo_programs/split_felt.json"); - run_program_simple(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_math_cmp() { - let program_data = include_bytes!("../../cairo_programs/math_cmp.json"); - run_program_simple(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_unsigned_div_rem() { - let program_data = include_bytes!("../../cairo_programs/unsigned_div_rem.json"); - run_program_simple(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_signed_div_rem() { - let program_data = include_bytes!("../../cairo_programs/signed_div_rem.json"); - run_program_simple(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_assert_lt_felt() { - let program_data = include_bytes!("../../cairo_programs/assert_lt_felt.json"); - run_program_simple(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_memcpy() { - let program_data = include_bytes!("../../cairo_programs/memcpy_test.json"); - run_program_simple(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_memset() { - let program_data = include_bytes!("../../cairo_programs/memset.json"); - run_program_simple(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_pow() { - let program_data = include_bytes!("../../cairo_programs/pow.json"); - run_program_simple(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_dict() { - let program_data = include_bytes!("../../cairo_programs/dict.json"); - run_program_simple(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_dict_update() { - let program_data = include_bytes!("../../cairo_programs/dict_update.json"); - run_program_simple(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_uint256() { - let program_data = include_bytes!("../../cairo_programs/uint256.json"); - run_program_simple(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_find_element() { - let program_data = include_bytes!("../../cairo_programs/find_element.json"); - run_program_simple(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_search_sorted_lower() { - let program_data = include_bytes!("../../cairo_programs/search_sorted_lower.json"); - run_program_simple(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_usort() { - let program_data = include_bytes!("../../cairo_programs/usort.json"); - run_program_simple(program_data.as_slice()); -} - #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn cairo_run_usort_bad() { @@ -1035,13 +677,6 @@ fn cairo_run_secp_ec() { run_program_simple(program_data.as_slice()); } -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_blake2s_hello_world_hash() { - let program_data = include_bytes!("../../cairo_programs/blake2s_hello_world_hash.json"); - run_program_simple(program_data.as_slice()); -} - #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn cairo_run_finalize_blake2s() { @@ -1055,13 +690,6 @@ fn cairo_run_unsafe_keccak() { run_program_simple(program_data.as_slice()); } -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_blake2s_felts() { - let program_data = include_bytes!("../../cairo_programs/blake2s_felts.json"); - run_program_simple(program_data.as_slice()); -} - #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn cairo_run_unsafe_keccak_finalize() { diff --git a/src/tests/mod.rs b/src/tests/mod.rs index e67c9339b1..f2f77ecd79 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -18,21 +18,28 @@ mod struct_test; mod skip_instruction_test; //For simple programs that should just succeed and have no special needs. +//Checks memory holes == 0 pub(self) fn run_program_simple(data: &[u8]) { run_program(data, Some("all_cairo"), None, None, Some(0)) } +//For simple programs that should just succeed and have no special needs. +//Checks memory holes +pub(self) fn run_program_simple_with_memory_holes(data: &[u8], holes: usize) { + run_program(data, Some("all_cairo"), None, None, Some(holes)) +} + //For simple programs that should just succeed but using small layout. pub(self) fn run_program_small(data: &[u8]) { - run_program(data, Some("small"), None, None, Some(0)) + run_program(data, Some("small"), None, None, None) } pub(self) fn run_program_with_trace(data: &[u8], trace: &[(usize, usize, usize)]) { - run_program(data, Some("all_cairo"), Some(trace), None, Some(0)) + run_program(data, Some("all_cairo"), Some(trace), None, None) } pub(self) fn run_program_with_error(data: &[u8], error: &str) { - run_program(data, Some("all_cairo"), None, Some(error), Some(0)) + run_program(data, Some("all_cairo"), None, Some(error), None) } pub(self) fn run_program( From ece99eb2bd4d6939f005cf8007959c3e35d8e577 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 20 Apr 2023 16:57:50 -0300 Subject: [PATCH 11/41] Add memory holes value + remove duplicated tests + fix typo --- src/tests/cairo_run_test.rs | 359 +++++++---------------------------- src/vm/errors/hint_errors.rs | 2 +- 2 files changed, 66 insertions(+), 295 deletions(-) diff --git a/src/tests/cairo_run_test.rs b/src/tests/cairo_run_test.rs index 5dce3c2ede..471f2db272 100644 --- a/src/tests/cairo_run_test.rs +++ b/src/tests/cairo_run_test.rs @@ -4,7 +4,7 @@ use crate::tests::*; #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn fibonacci() { let program_data = include_bytes!("../../cairo_programs/fibonacci.json"); - run_program_simple(program_data.as_slice()); + run_program_small(program_data.as_slice()); } #[test] @@ -82,7 +82,7 @@ fn jmp() { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn jmp_if_condition() { let program_data = include_bytes!("../../cairo_programs/jmp_if_condition.json"); - run_program_simple(program_data.as_slice()); + run_program_simple_with_memory_holes(program_data.as_slice(), 1); } #[test] @@ -187,14 +187,14 @@ fn compare_arrays() { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn compare_greater_array() { let program_data = include_bytes!("../../cairo_programs/compare_greater_array.json"); - run_program_simple_with_memory_holes(program_data.as_slice(), 177); + run_program_simple_with_memory_holes(program_data.as_slice(), 170); } #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn compare_lesser_array() { let program_data = include_bytes!("../../cairo_programs/compare_lesser_array.json"); - run_program_simple(program_data.as_slice()); + run_program_simple_with_memory_holes(program_data.as_slice(), 200); } #[test] @@ -313,7 +313,7 @@ fn memset() { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn pow() { let program_data = include_bytes!("../../cairo_programs/pow.json"); - run_program_simple(program_data.as_slice()); + run_program_simple_with_memory_holes(program_data.as_slice(), 6); } #[test] @@ -327,14 +327,14 @@ fn dict() { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn dict_update() { let program_data = include_bytes!("../../cairo_programs/dict_update.json"); - run_program_simple(program_data.as_slice()); + run_program_simple_with_memory_holes(program_data.as_slice(), 1); } #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn uint256() { let program_data = include_bytes!("../../cairo_programs/uint256.json"); - run_program_simple(program_data.as_slice()); + run_program_simple_with_memory_holes(program_data.as_slice(), 3514); } #[test] @@ -348,14 +348,14 @@ fn find_element() { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn search_sorted_lower() { let program_data = include_bytes!("../../cairo_programs/search_sorted_lower.json"); - run_program_simple(program_data.as_slice()); + run_program_simple_with_memory_holes(program_data.as_slice(), 4); } #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn usort() { let program_data = include_bytes!("../../cairo_programs/usort.json"); - run_program_simple(program_data.as_slice()); + run_program_simple_with_memory_holes(program_data.as_slice(), 3); } #[test] @@ -376,7 +376,7 @@ fn dict_squash() { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn set_add() { let program_data = include_bytes!("../../cairo_programs/set_add.json"); - run_program_simple(program_data.as_slice()); + run_program_simple_with_memory_holes(program_data.as_slice(), 2); } #[test] @@ -388,15 +388,15 @@ fn secp() { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn signature() { - let program_data = include_bytes!("../../cairo_programs/signature.json"); - run_program_simple(program_data.as_slice()); +fn secp_ec() { + let program_data = include_bytes!("../../cairo_programs/secp_ec.json"); + run_program_simple_with_memory_holes(program_data.as_slice(), 1752); } #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn secp_ec() { - let program_data = include_bytes!("../../cairo_programs/secp_ec.json"); +fn signature() { + let program_data = include_bytes!("../../cairo_programs/signature.json"); run_program_simple(program_data.as_slice()); } @@ -411,7 +411,7 @@ fn blake2s_hello_world_hash() { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn finalize_blake2s() { let program_data = include_bytes!("../../cairo_programs/finalize_blake2s.json"); - run_program_simple(program_data.as_slice()); + run_program_simple_with_memory_holes(program_data.as_slice(), 20); } #[test] @@ -460,21 +460,21 @@ fn keccak_copy_inputs() { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn cairo_finalize_keccak() { let program_data = include_bytes!("../../cairo_programs/cairo_finalize_keccak.json"); - run_program_simple(program_data.as_slice()); + run_program_simple_with_memory_holes(program_data.as_slice(), 50); } #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn operations_with_data_structures() { let program_data = include_bytes!("../../cairo_programs/operations_with_data_structures.json"); - run_program_simple(program_data.as_slice()); + run_program_simple_with_memory_holes(program_data.as_slice(), 72); } #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn sha256() { let program_data = include_bytes!("../../cairo_programs/sha256.json"); - run_program_simple(program_data.as_slice()); + run_program_simple_with_memory_holes(program_data.as_slice(), 2); } #[test] @@ -482,42 +482,42 @@ fn sha256() { fn math_cmp_and_pow_integration_tests() { let program_data = include_bytes!("../../cairo_programs/math_cmp_and_pow_integration_tests.json"); - run_program_simple(program_data.as_slice()); + run_program_simple_with_memory_holes(program_data.as_slice(), 1213); } #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn uint256_integration_tests() { let program_data = include_bytes!("../../cairo_programs/uint256_integration_tests.json"); - run_program_simple(program_data.as_slice()); + run_program_simple_with_memory_holes(program_data.as_slice(), 1027); } #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn set_integration_tests() { let program_data = include_bytes!("../../cairo_programs/set_integration_tests.json"); - run_program_simple(program_data.as_slice()); + run_program_simple_with_memory_holes(program_data.as_slice(), 20); } #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn memory_integration_tests() { let program_data = include_bytes!("../../cairo_programs/memory_integration_tests.json"); - run_program_simple(program_data.as_slice()); + run_program_simple_with_memory_holes(program_data.as_slice(), 5); } #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn dict_integration_tests() { let program_data = include_bytes!("../../cairo_programs/dict_integration_tests.json"); - run_program_simple(program_data.as_slice()); + run_program_simple_with_memory_holes(program_data.as_slice(), 32); } #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn secp_integration_tests() { let program_data = include_bytes!("../../cairo_programs/secp_integration_tests.json"); - run_program_simple(program_data.as_slice()); + run_program_simple_with_memory_holes(program_data.as_slice(), 1752); } #[test] @@ -531,7 +531,7 @@ fn keccak_integration_tests() { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn blake2s_integration_tests() { let program_data = include_bytes!("../../cairo_programs/blake2s_integration_tests.json"); - run_program_simple(program_data.as_slice()); + run_program_simple_with_memory_holes(program_data.as_slice(), 409); } #[test] @@ -541,11 +541,18 @@ fn relocate_segments() { run_program_simple(program_data.as_slice()); } +#[test] +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] +fn relocate_segments_with_offset() { + let program_data = include_bytes!("../../cairo_programs/relocate_segments_with_offset.json"); + run_program_simple_with_memory_holes(program_data.as_slice(), 5); +} + #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn dict_store_cast_ptr() { let program_data = include_bytes!("../../cairo_programs/dict_store_cast_ptr.json"); - run_program_simple(program_data.as_slice()); + run_program_simple_with_memory_holes(program_data.as_slice(), 1); } #[test] @@ -567,7 +574,7 @@ fn bad_usort() { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn bad_dict_new() { let program_data = include_bytes!("../../cairo_programs/bad_programs/bad_dict_new.json"); - let error_msg = "Dict Error: Tried to create a dict whithout an initial dict"; + let error_msg = "Dict Error: Tried to create a dict without an initial dict"; run_program_with_error(program_data.as_slice(), error_msg); } @@ -612,257 +619,21 @@ fn error_msg_attr_struct() { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_usort_bad() { - let program_data = include_bytes!("../../cairo_programs/bad_programs/bad_usort.json"); - let error_msg = "unexpected verify multiplicity fail: positions length != 0"; - run_program_with_error(program_data.as_slice(), error_msg); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_dict_write_bad() { - let program_data = include_bytes!("../../cairo_programs/bad_programs/bad_dict_new.json"); - let error_msg = "Dict Error: Tried to create a dict whithout an initial dict"; - run_program_with_error(program_data.as_slice(), error_msg); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_dict_update_bad() { - let program_data = include_bytes!("../../cairo_programs/bad_programs/bad_dict_update.json"); - let error_msg = - "Dict Error: Got the wrong value for dict_update, expected value: 3, got: 5 for key: 2"; - run_program_with_error(program_data.as_slice(), error_msg); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_squash_dict() { - let program_data = include_bytes!("../../cairo_programs/squash_dict.json"); - run_program_simple(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_dict_squash() { - let program_data = include_bytes!("../../cairo_programs/dict_squash.json"); - run_program_simple(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_set_add() { - let program_data = include_bytes!("../../cairo_programs/set_add.json"); - run_program_simple(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_secp() { - let program_data = include_bytes!("../../cairo_programs/secp.json"); - run_program_simple(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_signature() { - let program_data = include_bytes!("../../cairo_programs/signature.json"); - run_program_simple(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_secp_ec() { - let program_data = include_bytes!("../../cairo_programs/secp_ec.json"); - run_program_simple(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_finalize_blake2s() { - let program_data = include_bytes!("../../cairo_programs/finalize_blake2s.json"); - run_program_simple(program_data.as_slice()); -} -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_unsafe_keccak() { - let program_data = include_bytes!("../../cairo_programs/unsafe_keccak.json"); - run_program_simple(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_unsafe_keccak_finalize() { - let program_data = include_bytes!("../../cairo_programs/unsafe_keccak_finalize.json"); - run_program_simple(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_keccak_add_uint256() { - let program_data = include_bytes!("../../cairo_programs/keccak_add_uint256.json"); - run_program_simple(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_private_keccak() { - let program_data = include_bytes!("../../cairo_programs/_keccak.json"); - run_program_simple(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_keccak_copy_inputs() { - let program_data = include_bytes!("../../cairo_programs/keccak_copy_inputs.json"); - run_program_simple(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_finalize_keccak() { - let program_data = include_bytes!("../../cairo_programs/cairo_finalize_keccak.json"); - run_program_simple(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_operations_with_data() { - let program_data = include_bytes!("../../cairo_programs/operations_with_data_structures.json"); - run_program_simple(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_sha256() { - let program_data = include_bytes!("../../cairo_programs/sha256.json"); - run_program_simple(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_math_cmp_and_pow_integration() { - let program_data = - include_bytes!("../../cairo_programs/math_cmp_and_pow_integration_tests.json"); - run_program_simple(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_uint256_integration() { - let program_data = include_bytes!("../../cairo_programs/uint256_integration_tests.json"); - run_program_simple(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_set_integration() { - let program_data = include_bytes!("../../cairo_programs/set_integration_tests.json"); - run_program_simple(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_memory_module_integration() { - let program_data = include_bytes!("../../cairo_programs/memory_integration_tests.json"); - run_program_simple(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_dict_integration() { - let program_data = include_bytes!("../../cairo_programs/dict_integration_tests.json"); - run_program_simple(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_secp_integration() { - let program_data = include_bytes!("../../cairo_programs/secp_integration_tests.json"); - run_program_simple(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_keccak_integration() { - let program_data = include_bytes!("../../cairo_programs/keccak_integration_tests.json"); - run_program_simple(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_blake2s_integration() { - let program_data = include_bytes!("../../cairo_programs/blake2s_integration_tests.json"); - run_program_simple(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_relocate_segments() { - let program_data = include_bytes!("../../cairo_programs/relocate_segments.json"); - run_program_small(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_error_msg_attr() { - let program_data = include_bytes!("../../cairo_programs/bad_programs/error_msg_attr.json"); - let error_msg = "SafeUint256: addition overflow"; - run_program_with_error(program_data.as_slice(), error_msg); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_error_msg_attr_ap_based_reference() { - let program_data = - include_bytes!("../../cairo_programs/bad_programs/error_msg_attr_tempvar.json"); - #[cfg(feature = "std")] - let error_msg = "Error message: SafeUint256: addition overflow: {x} (Cannot evaluate ap-based or complex references: ['x'])\ncairo_programs/bad_programs/error_msg_attr_tempvar.cairo:4:9: Error at pc=0:2:\nAn ASSERT_EQ instruction failed: 3 != 2.\n assert x = 2;\n ^***********^\n"; - #[cfg(not(feature = "std"))] - let error_msg = "Error message: SafeUint256: addition overflow: {x} (Cannot evaluate ap-based or complex references: ['x'])\ncairo_programs/bad_programs/error_msg_attr_tempvar.cairo:4:9: Error at pc=0:2:\nAn ASSERT_EQ instruction failed: 3 != 2.\n"; - run_program_with_error(program_data.as_slice(), error_msg); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_error_msg_attr_complex_reference() { - let program_data = - include_bytes!("../../cairo_programs/bad_programs/error_msg_attr_struct.json"); - let error_msg = "Error message: Cats cannot have more than nine lives: {cat} (Cannot evaluate ap-based or complex references: ['cat'])"; - run_program_with_error(program_data.as_slice(), error_msg); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_dict_store_cast_pointer() { - let program_data = include_bytes!("../../cairo_programs/dict_store_cast_ptr.json"); - run_program_simple(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_verify_signature_hint() { - let program_data = include_bytes!("../../cairo_programs/common_signature.json"); - run_program_simple(program_data.as_slice()); -} - -#[test] -#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_poseidon_builtin() { +fn poseidon_builtin() { let program_data = include_bytes!("../../cairo_programs/poseidon_builtin.json"); run_program_simple(program_data.as_slice()); } #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_ec_op() { +fn ec_op() { let program_data = include_bytes!("../../cairo_programs/ec_op.json"); run_program_simple(program_data.as_slice()); } #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_poseidon_hash() { +fn poseidon_hash() { let program_data = include_bytes!("../../cairo_programs/poseidon_hash.json"); run_program_simple(program_data.as_slice()); } @@ -876,140 +647,140 @@ fn cairo_chained_run_ec_op() { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_keccak_builtin() { +fn keccak_builtin() { let program_data = include_bytes!("../../cairo_programs/keccak_builtin.json"); run_program_simple(program_data.as_slice()); } #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_keccak_uint256() { +fn keccak_uint256() { let program_data = include_bytes!("../../cairo_programs/keccak_uint256.json"); run_program_simple(program_data.as_slice()); } #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_recover_y() { +fn recover_y() { let program_data = include_bytes!("../../cairo_programs/recover_y.json"); run_program_simple(program_data.as_slice()); } #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_math_integration() { +fn math_integration() { let program_data = include_bytes!("../../cairo_programs/math_integration_tests.json"); - run_program_simple(program_data.as_slice()); + run_program_simple_with_memory_holes(program_data.as_slice(), 109); } #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_is_quad_residue_test() { +fn is_quad_residue_test() { let program_data = include_bytes!("../../cairo_programs/is_quad_residue_test.json"); - run_program_simple(program_data.as_slice()); + run_program_simple_with_memory_holes(program_data.as_slice(), 6); } #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_mul_s_inv() { +fn mul_s_inv() { let program_data = include_bytes!("../../cairo_programs/mul_s_inv.json"); run_program_simple(program_data.as_slice()); } #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_keccak_alternative_hint() { +fn keccak_alternative_hint() { let program_data = include_bytes!("../../cairo_programs/_keccak_alternative_hint.json"); - run_program_simple(program_data.as_slice()); + run_program_simple_with_memory_holes(program_data.as_slice(), 23); } #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_uint384() { +fn uint384() { let program_data = include_bytes!("../../cairo_programs/uint384.json"); - run_program_simple(program_data.as_slice()); + run_program_simple_with_memory_holes(program_data.as_slice(), 74); } #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_uint384_extension() { +fn uint384_extension() { let program_data = include_bytes!("../../cairo_programs/uint384_extension.json"); - run_program_simple(program_data.as_slice()); + run_program_simple_with_memory_holes(program_data.as_slice(), 20); } #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_ed25519_field() { +fn ed25519_field() { let program_data = include_bytes!("../../cairo_programs/ed25519_field.json"); run_program_simple(program_data.as_slice()); } #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_ed25519_ec() { +fn ed25519_ec() { let program_data = include_bytes!("../../cairo_programs/ed25519_ec.json"); run_program_simple(program_data.as_slice()); } #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_efficient_secp256r1_ec() { +fn efficient_secp256r1_ec() { let program_data = include_bytes!("../../cairo_programs/efficient_secp256r1_ec.json"); run_program_simple(program_data.as_slice()); } #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_garaga() { +fn garaga() { let program_data = include_bytes!("../../cairo_programs/garaga.json"); run_program_simple(program_data.as_slice()); } #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_div_mod_n() { +fn div_mod_n() { let program_data = include_bytes!("../../cairo_programs/div_mod_n.json"); run_program_simple(program_data.as_slice()); } #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_is_zero() { +fn is_zero() { let program_data = include_bytes!("../../cairo_programs/is_zero.json"); run_program_simple(program_data.as_slice()); } #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_is_zero_pack() { +fn is_zero_pack() { let program_data = include_bytes!("../../cairo_programs/is_zero_pack.json"); run_program_simple(program_data.as_slice()); } #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_n_bit() { +fn n_bit() { let program_data = include_bytes!("../../cairo_programs/n_bit.json"); run_program_simple(program_data.as_slice()); } #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_highest_bitlen() { +fn highest_bitlen() { let program_data = include_bytes!("../../cairo_programs/highest_bitlen.json"); run_program_simple(program_data.as_slice()); } #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_uint256_improvements() { +fn uint256_improvements() { let program_data = include_bytes!("../../cairo_programs/uint256_improvements.json"); - run_program_simple(program_data.as_slice()); + run_program_simple_with_memory_holes(program_data.as_slice(), 40); } #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] -fn cairo_run_memory_holes() { +fn memory_holes() { let program_data = include_bytes!("../../cairo_programs/memory_holes.json"); - run_program(program_data, Some("all_cairo"), None, None, Some(5)) + run_program_simple_with_memory_holes(program_data.as_slice(), 5) } diff --git a/src/vm/errors/hint_errors.rs b/src/vm/errors/hint_errors.rs index 7b98bce2c4..56d782032e 100644 --- a/src/vm/errors/hint_errors.rs +++ b/src/vm/errors/hint_errors.rs @@ -73,7 +73,7 @@ pub enum HintError { InvalidTrackingGroup(usize, usize), #[error("Expected relocatable for ap, got {0}")] InvalidApValue(MaybeRelocatable), - #[error("Dict Error: Tried to create a dict whithout an initial dict")] + #[error("Dict Error: Tried to create a dict without an initial dict")] NoInitialDict, #[error("squash_dict_inner fail: couldnt find key {0} in accesses_indices")] NoKeyInAccessIndices(Felt252), From 1f867fb831ff4db7fd235bdc2354ff84ad41ae0f Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 20 Apr 2023 17:03:46 -0300 Subject: [PATCH 12/41] Fix test values --- src/tests/cairo_run_test.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/tests/cairo_run_test.rs b/src/tests/cairo_run_test.rs index 471f2db272..335ff54753 100644 --- a/src/tests/cairo_run_test.rs +++ b/src/tests/cairo_run_test.rs @@ -446,7 +446,7 @@ fn keccak_add_uint256() { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn keccak() { let program_data = include_bytes!("../../cairo_programs/_keccak.json"); - run_program_simple(program_data.as_slice()); + run_program_simple_with_memory_holes(program_data.as_slice(), 21); } #[test] @@ -517,14 +517,14 @@ fn dict_integration_tests() { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn secp_integration_tests() { let program_data = include_bytes!("../../cairo_programs/secp_integration_tests.json"); - run_program_simple_with_memory_holes(program_data.as_slice(), 1752); + run_program_simple_with_memory_holes(program_data.as_slice(), 4626); } #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn keccak_integration_tests() { let program_data = include_bytes!("../../cairo_programs/keccak_integration_tests.json"); - run_program_simple(program_data.as_slice()); + run_program_simple_with_memory_holes(program_data.as_slice(), 507); } #[test] From ac9c795d6de05851d5f519c6fe5e6d376d36b03c Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 20 Apr 2023 17:07:58 -0300 Subject: [PATCH 13/41] Add changelog entry --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 79b253f64a..2ca91413e3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ #### Upcoming Changes +* Fix `CairoRunner::get_memory_holes` + + * Skip builtin segements when counting memory holes + * Check amount of memory holes for all tests in cairo_run_test + * Remove duplicated tests in cairo_run_test + * Add methor `Program::data_len(&self) -> usize` to get the number of data cells in a given program [#1022](https://github.com/lambdaclass/cairo-rs/pull/1022) * Add missing hint on uint256_improvements lib [#1013](https://github.com/lambdaclass/cairo-rs/pull/1013): From 356a1874f6ce95a3af80aa801777ffaefe814f39 Mon Sep 17 00:00:00 2001 From: Federica Date: Thu, 20 Apr 2023 17:22:56 -0300 Subject: [PATCH 14/41] Link PR in Changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ca91413e3..dc853ed7a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ #### Upcoming Changes -* Fix `CairoRunner::get_memory_holes` +* Fix `CairoRunner::get_memory_holes` [#1027](https://github.com/lambdaclass/cairo-rs/pull/1027): * Skip builtin segements when counting memory holes * Check amount of memory holes for all tests in cairo_run_test From 67e26223c8085971a9fe00d920d3bf77eea29f84 Mon Sep 17 00:00:00 2001 From: Federica Date: Fri, 21 Apr 2023 10:56:13 -0300 Subject: [PATCH 15/41] Mark breaking change in changelog --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 639546e416..625d7bbd6c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,11 +2,12 @@ #### Upcoming Changes -* Fix `CairoRunner::get_memory_holes` [#1027](https://github.com/lambdaclass/cairo-rs/pull/1027): +* BREAKING CHANGE: Fix `CairoRunner::get_memory_holes` [#1027](https://github.com/lambdaclass/cairo-rs/pull/1027): * Skip builtin segements when counting memory holes * Check amount of memory holes for all tests in cairo_run_test * Remove duplicated tests in cairo_run_test + * BREAKING CHANGE: `MemorySegmentManager.get_memory_holes` now also receives the amount of builtins in the vm. Signature is now `pub fn get_memory_holes(&self, builtin_count: usize) -> Result` * Implement hints on field_arithmetic lib[#985](https://github.com/lambdaclass/cairo-rs/pull/983) From e1147457dac70beb9f81097ee460191b4acf6364 Mon Sep 17 00:00:00 2001 From: Federica Date: Fri, 21 Apr 2023 11:00:11 -0300 Subject: [PATCH 16/41] fmt --- src/vm/runners/cairo_runner.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/vm/runners/cairo_runner.rs b/src/vm/runners/cairo_runner.rs index 37a24bcbf2..68447a58cb 100644 --- a/src/vm/runners/cairo_runner.rs +++ b/src/vm/runners/cairo_runner.rs @@ -4529,7 +4529,8 @@ mod tests { //this entrypoint tells which function to run in the cairo program let main_entrypoint = program - .shared_program_data.identifiers + .shared_program_data + .identifiers .get("__main__.main") .unwrap() .pc From 3ecf88227405f951e808700fd61a8b637f3b5581 Mon Sep 17 00:00:00 2001 From: Federica Date: Fri, 21 Apr 2023 11:19:15 -0300 Subject: [PATCH 17/41] Fix test value --- src/tests/cairo_run_test.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tests/cairo_run_test.rs b/src/tests/cairo_run_test.rs index 6ebcbe6d81..406b878ecd 100644 --- a/src/tests/cairo_run_test.rs +++ b/src/tests/cairo_run_test.rs @@ -782,7 +782,7 @@ fn highest_bitlen() { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn uint256_improvements() { let program_data = include_bytes!("../../cairo_programs/uint256_improvements.json"); - run_program_simple_with_memory_holes(program_data.as_slice(), 40); + run_program_simple_with_memory_holes(program_data.as_slice(), 108); } #[test] From 342efdbd2f386dc83c3308064e3fbbef11f4ffe1 Mon Sep 17 00:00:00 2001 From: Federica Date: Fri, 21 Apr 2023 12:43:18 -0300 Subject: [PATCH 18/41] Fix codecov-patch diff --- src/vm/runners/cairo_runner.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/vm/runners/cairo_runner.rs b/src/vm/runners/cairo_runner.rs index 68447a58cb..d040a3e8f4 100644 --- a/src/vm/runners/cairo_runner.rs +++ b/src/vm/runners/cairo_runner.rs @@ -4540,8 +4540,8 @@ mod tests { .initialize_function_runner(&mut vm, false) .unwrap(); - assert_matches!( - cairo_runner.run_from_entrypoint( + assert!(cairo_runner + .run_from_entrypoint( main_entrypoint, &[ &MaybeRelocatable::from((2, 0)).into() //bitwise_ptr @@ -4550,9 +4550,8 @@ mod tests { None, &mut vm, &mut hint_processor, - ), - Ok(()) - ); + ) + .is_ok()); // Check that memory_holes == 0 assert!(cairo_runner.get_memory_holes(&vm).unwrap().is_zero()); From 67e3b70c69f33d2f5d3b000c14e90966a9420a8d Mon Sep 17 00:00:00 2001 From: Federica Date: Fri, 21 Apr 2023 15:16:01 -0300 Subject: [PATCH 19/41] Add hint code --- src/hint_processor/builtin_hint_processor/hint_code.rs | 8 ++++++++ src/hint_processor/builtin_hint_processor/mod.rs | 1 + 2 files changed, 9 insertions(+) diff --git a/src/hint_processor/builtin_hint_processor/hint_code.rs b/src/hint_processor/builtin_hint_processor/hint_code.rs index 070e18e602..35410ad399 100644 --- a/src/hint_processor/builtin_hint_processor/hint_code.rs +++ b/src/hint_processor/builtin_hint_processor/hint_code.rs @@ -500,6 +500,14 @@ a = pack(ids.a, PRIME) b = pack(ids.b, PRIME) value = res = div_mod(a, b, N)"#; +pub const DIV_MOD_N_PACKED_DIVMOD_CAIRO_N: &str = r#"from starkware.cairo.common.cairo_secp.secp_utils import pack +from starkware.python.math_utils import div_mod, safe_div + +N = pack(ids.n, PRIME) +x = pack(ids.x, PRIME) % N +s = pack(ids.s, PRIME) % N, +value = res = div_mod(x, s, N)"#; + pub const DIV_MOD_N_SAFE_DIV: &str = r#"value = k = safe_div(res * b - a, N)"#; pub const GET_FELT_BIT_LENGTH: &str = r#"x = ids.x diff --git a/src/hint_processor/builtin_hint_processor/mod.rs b/src/hint_processor/builtin_hint_processor/mod.rs index d83801f382..2d3d637beb 100644 --- a/src/hint_processor/builtin_hint_processor/mod.rs +++ b/src/hint_processor/builtin_hint_processor/mod.rs @@ -29,3 +29,4 @@ pub mod uint256_utils; pub mod uint384; pub mod uint384_extension; pub mod usort; +pub mod ec_recover; From 4779307df762794a1315a3d614fa1bbd7bfa8c8c Mon Sep 17 00:00:00 2001 From: Federica Date: Fri, 21 Apr 2023 16:32:56 -0300 Subject: [PATCH 20/41] Add integration test --- cairo_programs/ec_recover.cairo | 27 +++++++++++++++++++ .../builtin_hint_processor_definition.rs | 3 +++ .../builtin_hint_processor/hint_code.rs | 2 +- src/tests/cairo_run_test.rs | 7 +++++ 4 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 cairo_programs/ec_recover.cairo diff --git a/cairo_programs/ec_recover.cairo b/cairo_programs/ec_recover.cairo new file mode 100644 index 0000000000..83606c8992 --- /dev/null +++ b/cairo_programs/ec_recover.cairo @@ -0,0 +1,27 @@ +%builtins range_check +from starkware.cairo.common.cairo_secp.bigint import BigInt3, nondet_bigint3 + +func test_div_mod_n_packed_hint{range_check_ptr: felt}() { + + tempvar n = BigInt3(177, 0, 0); + tempvar x = BigInt3(25, 0, 0); + tempvar s = BigInt3(5, 0, 0); + %{ + from starkware.cairo.common.cairo_secp.secp_utils import pack + from starkware.python.math_utils import div_mod, safe_div + + N = pack(ids.n, PRIME) + x = pack(ids.x, PRIME) % N + s = pack(ids.s, PRIME) % N + value = res = div_mod(x, s, N) + %} + + let (res) = nondet_bigint3(); + assert res = BigInt3(5,0,0); + return(); +} + +func main{range_check_ptr: felt}() { + test_div_mod_n_packed_hint(); + return(); +} diff --git a/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs b/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs index ea49e3b71d..e2015497df 100644 --- a/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs +++ b/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs @@ -83,6 +83,8 @@ use felt::Felt252; #[cfg(feature = "skip_next_instruction_hint")] use crate::hint_processor::builtin_hint_processor::skip_next_instruction::skip_next_instruction; +use super::ec_recover::div_mod_n_packed_cairo_n; + pub struct HintProcessorData { pub code: String, pub ap_tracking: ApTracking, @@ -558,6 +560,7 @@ impl HintProcessor for BuiltinHintProcessor { } hint_code::QUAD_BIT => quad_bit(vm, &hint_data.ids_data, &hint_data.ap_tracking), hint_code::DI_BIT => di_bit(vm, &hint_data.ids_data, &hint_data.ap_tracking), + hint_code::DIV_MOD_N_PACKED_DIVMOD_CAIRO_N => div_mod_n_packed_cairo_n(vm, exec_scopes, &hint_data.ids_data, &hint_data.ap_tracking), #[cfg(feature = "skip_next_instruction_hint")] hint_code::SKIP_NEXT_INSTRUCTION => skip_next_instruction(vm), code => Err(HintError::UnknownHint(code.to_string())), diff --git a/src/hint_processor/builtin_hint_processor/hint_code.rs b/src/hint_processor/builtin_hint_processor/hint_code.rs index 35410ad399..37d050d402 100644 --- a/src/hint_processor/builtin_hint_processor/hint_code.rs +++ b/src/hint_processor/builtin_hint_processor/hint_code.rs @@ -505,7 +505,7 @@ from starkware.python.math_utils import div_mod, safe_div N = pack(ids.n, PRIME) x = pack(ids.x, PRIME) % N -s = pack(ids.s, PRIME) % N, +s = pack(ids.s, PRIME) % N value = res = div_mod(x, s, N)"#; pub const DIV_MOD_N_SAFE_DIV: &str = r#"value = k = safe_div(res * b - a, N)"#; diff --git a/src/tests/cairo_run_test.rs b/src/tests/cairo_run_test.rs index 406b878ecd..3121e9c71f 100644 --- a/src/tests/cairo_run_test.rs +++ b/src/tests/cairo_run_test.rs @@ -791,3 +791,10 @@ fn memory_holes() { let program_data = include_bytes!("../../cairo_programs/memory_holes.json"); run_program_simple_with_memory_holes(program_data.as_slice(), 5) } + +#[test] +#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] +fn ec_recover() { + let program_data = include_bytes!("../../cairo_programs/ec_recover.json"); + run_program_simple(program_data.as_slice()); +} From cbe3b5d505c1816ab245647fc528968a183e2281 Mon Sep 17 00:00:00 2001 From: Federica Date: Fri, 21 Apr 2023 16:35:16 -0300 Subject: [PATCH 21/41] Add untracked file --- .../builtin_hint_processor_definition.rs | 7 +- .../builtin_hint_processor/ec_recover.rs | 99 +++++++++++++++++++ .../builtin_hint_processor/mod.rs | 2 +- 3 files changed, 106 insertions(+), 2 deletions(-) create mode 100644 src/hint_processor/builtin_hint_processor/ec_recover.rs diff --git a/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs b/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs index e2015497df..b3509e5299 100644 --- a/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs +++ b/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs @@ -560,7 +560,12 @@ impl HintProcessor for BuiltinHintProcessor { } hint_code::QUAD_BIT => quad_bit(vm, &hint_data.ids_data, &hint_data.ap_tracking), hint_code::DI_BIT => di_bit(vm, &hint_data.ids_data, &hint_data.ap_tracking), - hint_code::DIV_MOD_N_PACKED_DIVMOD_CAIRO_N => div_mod_n_packed_cairo_n(vm, exec_scopes, &hint_data.ids_data, &hint_data.ap_tracking), + hint_code::DIV_MOD_N_PACKED_DIVMOD_CAIRO_N => div_mod_n_packed_cairo_n( + vm, + exec_scopes, + &hint_data.ids_data, + &hint_data.ap_tracking, + ), #[cfg(feature = "skip_next_instruction_hint")] hint_code::SKIP_NEXT_INSTRUCTION => skip_next_instruction(vm), code => Err(HintError::UnknownHint(code.to_string())), diff --git a/src/hint_processor/builtin_hint_processor/ec_recover.rs b/src/hint_processor/builtin_hint_processor/ec_recover.rs new file mode 100644 index 0000000000..b89d29c48b --- /dev/null +++ b/src/hint_processor/builtin_hint_processor/ec_recover.rs @@ -0,0 +1,99 @@ +use super::secp::{bigint_utils::BigInt3, secp_utils::pack}; +use crate::stdlib::{collections::HashMap, prelude::*}; +use crate::{ + hint_processor::hint_processor_definition::HintReference, + math_utils::div_mod, + serde::deserialize_program::ApTracking, + types::exec_scope::ExecutionScopes, + vm::{errors::hint_errors::HintError, vm_core::VirtualMachine}, +}; +use num_integer::Integer; + +/* Implements Hint: +%{ + from starkware.cairo.common.cairo_secp.secp_utils import pack + from starkware.python.math_utils import div_mod, safe_div + + N = pack(ids.n, PRIME) + x = pack(ids.x, PRIME) % N + s = pack(ids.s, PRIME) % N, + value = res = div_mod(x, s, N) +%} + */ +pub fn div_mod_n_packed_cairo_n( + vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + ids_data: &HashMap, + ap_tracking: &ApTracking, +) -> Result<(), HintError> { + let n = pack(BigInt3::from_var_name("n", vm, ids_data, ap_tracking)?); + let x = pack(BigInt3::from_var_name("x", vm, ids_data, ap_tracking)?).mod_floor(&n); + let s = pack(BigInt3::from_var_name("s", vm, ids_data, ap_tracking)?).mod_floor(&n); + + let value = div_mod(&x, &s, &n); + exec_scopes.insert_value("value", value.clone()); + exec_scopes.insert_value("res", value); + Ok(()) +} + +#[cfg(test)] +mod tests { + use num_bigint::BigInt; + + use super::*; + use crate::hint_processor::builtin_hint_processor::hint_code; + use crate::hint_processor::hint_processor_definition::HintReference; + use crate::utils::test_utils::*; + use crate::vm::errors::memory_errors::MemoryError; + use crate::vm::vm_core::VirtualMachine; + use crate::vm::vm_memory::memory::Memory; + use crate::vm::vm_memory::memory_segments::MemorySegmentManager; + use crate::{ + any_box, + hint_processor::{ + builtin_hint_processor::builtin_hint_processor_definition::{ + BuiltinHintProcessor, HintProcessorData, + }, + hint_processor_definition::HintProcessor, + }, + types::{exec_scope::ExecutionScopes, relocatable::MaybeRelocatable}, + }; + + #[test] + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] + fn run_div_mod_n_packed_cairo_n_ok() { + let mut vm = vm!(); + let mut exec_scopes = ExecutionScopes::new(); + + vm.run_context.fp = 8; + let ids_data = non_continuous_ids_data![("n", -8), ("x", -5), ("s", -2)]; + + vm.segments = segments![ + //n + ((1, 0), 177), + ((1, 1), 0), + ((1, 2), 0), + //x + ((1, 3), 25), + ((1, 4), 0), + ((1, 5), 0), + //s + ((1, 6), 5), + ((1, 7), 0), + ((1, 8), 0) + ]; + run_hint!( + vm, + ids_data, + hint_code::DIV_MOD_N_PACKED_DIVMOD_CAIRO_N, + &mut exec_scopes + ) + .unwrap(); + //assert!(run_hint!(vm, ids_data, hint_code::DIV_MOD_N_PACKED_DIVMOD_CAIRO_N, &mut exec_scopes).is_ok()); + + check_scope!( + &exec_scopes, + [("value", BigInt::from(5)), ("res", BigInt::from(5))] + ); + } +} diff --git a/src/hint_processor/builtin_hint_processor/mod.rs b/src/hint_processor/builtin_hint_processor/mod.rs index 2d3d637beb..5ad8a86014 100644 --- a/src/hint_processor/builtin_hint_processor/mod.rs +++ b/src/hint_processor/builtin_hint_processor/mod.rs @@ -4,6 +4,7 @@ pub mod builtin_hint_processor_definition; pub mod cairo_keccak; pub mod dict_hint_utils; pub mod dict_manager; +pub mod ec_recover; pub mod ec_utils; pub mod field_arithmetic; pub mod find_element_hint; @@ -29,4 +30,3 @@ pub mod uint256_utils; pub mod uint384; pub mod uint384_extension; pub mod usort; -pub mod ec_recover; From a71552d10fd38161456fe7105fb9776eef539278 Mon Sep 17 00:00:00 2001 From: Federica Date: Fri, 21 Apr 2023 16:46:25 -0300 Subject: [PATCH 22/41] Add changelog entry --- CHANGELOG.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 23858c7600..cd9b9a3032 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,22 @@ #### Upcoming Changes +* Implement hint on ec_recover.json whitelist [#1032](https://github.com/lambdaclass/cairo-rs/pull/1032): + + `BuiltinHintProcessor` now supports the following hint: + + ```python + %{ + from starkware.cairo.common.cairo_secp.secp_utils import pack + from starkware.python.math_utils import div_mod, safe_div + + N = pack(ids.n, PRIME) + x = pack(ids.x, PRIME) % N + s = pack(ids.s, PRIME) % N, + value = res = div_mod(x, s, N) + %} + ``` + * BREAKING CHANGE: Fix `CairoRunner::get_memory_holes` [#1027](https://github.com/lambdaclass/cairo-rs/pull/1027): * Skip builtin segements when counting memory holes From 75bb911dcbf9ada20a89f8bf551893b1e8ff03d0 Mon Sep 17 00:00:00 2001 From: Federica Date: Fri, 21 Apr 2023 16:53:11 -0300 Subject: [PATCH 23/41] Rename hint --- .../builtin_hint_processor_definition.rs | 4 ++-- .../builtin_hint_processor/ec_recover.rs | 6 +++--- .../builtin_hint_processor/hint_code.rs | 14 ++++++-------- 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs b/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs index b3509e5299..8b399761f4 100644 --- a/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs +++ b/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs @@ -83,7 +83,7 @@ use felt::Felt252; #[cfg(feature = "skip_next_instruction_hint")] use crate::hint_processor::builtin_hint_processor::skip_next_instruction::skip_next_instruction; -use super::ec_recover::div_mod_n_packed_cairo_n; +use super::ec_recover::ec_recover_divmod_n_packed; pub struct HintProcessorData { pub code: String, @@ -560,7 +560,7 @@ impl HintProcessor for BuiltinHintProcessor { } hint_code::QUAD_BIT => quad_bit(vm, &hint_data.ids_data, &hint_data.ap_tracking), hint_code::DI_BIT => di_bit(vm, &hint_data.ids_data, &hint_data.ap_tracking), - hint_code::DIV_MOD_N_PACKED_DIVMOD_CAIRO_N => div_mod_n_packed_cairo_n( + hint_code::EC_RECOVER_DIV_MOD_N_PACKED => ec_recover_divmod_n_packed( vm, exec_scopes, &hint_data.ids_data, diff --git a/src/hint_processor/builtin_hint_processor/ec_recover.rs b/src/hint_processor/builtin_hint_processor/ec_recover.rs index b89d29c48b..0cabb2f858 100644 --- a/src/hint_processor/builtin_hint_processor/ec_recover.rs +++ b/src/hint_processor/builtin_hint_processor/ec_recover.rs @@ -20,7 +20,7 @@ use num_integer::Integer; value = res = div_mod(x, s, N) %} */ -pub fn div_mod_n_packed_cairo_n( +pub fn ec_recover_divmod_n_packed( vm: &mut VirtualMachine, exec_scopes: &mut ExecutionScopes, ids_data: &HashMap, @@ -61,7 +61,7 @@ mod tests { #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] - fn run_div_mod_n_packed_cairo_n_ok() { + fn run_ec_recover_divmod_n_packed_ok() { let mut vm = vm!(); let mut exec_scopes = ExecutionScopes::new(); @@ -85,7 +85,7 @@ mod tests { run_hint!( vm, ids_data, - hint_code::DIV_MOD_N_PACKED_DIVMOD_CAIRO_N, + hint_code::EC_RECOVER_DIV_MOD_N_PACKED, &mut exec_scopes ) .unwrap(); diff --git a/src/hint_processor/builtin_hint_processor/hint_code.rs b/src/hint_processor/builtin_hint_processor/hint_code.rs index 37d050d402..cb6a0d454c 100644 --- a/src/hint_processor/builtin_hint_processor/hint_code.rs +++ b/src/hint_processor/builtin_hint_processor/hint_code.rs @@ -500,14 +500,6 @@ a = pack(ids.a, PRIME) b = pack(ids.b, PRIME) value = res = div_mod(a, b, N)"#; -pub const DIV_MOD_N_PACKED_DIVMOD_CAIRO_N: &str = r#"from starkware.cairo.common.cairo_secp.secp_utils import pack -from starkware.python.math_utils import div_mod, safe_div - -N = pack(ids.n, PRIME) -x = pack(ids.x, PRIME) % N -s = pack(ids.s, PRIME) % N -value = res = div_mod(x, s, N)"#; - pub const DIV_MOD_N_SAFE_DIV: &str = r#"value = k = safe_div(res * b - a, N)"#; pub const GET_FELT_BIT_LENGTH: &str = r#"x = ids.x @@ -922,6 +914,12 @@ pub const QUAD_BIT: &str = r#"ids.quad_bit = ( pub const DI_BIT: &str = r#"ids.dibit = ((ids.scalar_u >> ids.m) & 1) + 2 * ((ids.scalar_v >> ids.m) & 1)"#; +pub const EC_RECOVER_DIV_MOD_N_PACKED: &str = r#"from starkware.cairo.common.cairo_secp.secp_utils import pack + from starkware.python.math_utils import div_mod, safe_div + N = pack(ids.n, PRIME) + x = pack(ids.x, PRIME) % N + s = pack(ids.s, PRIME) % N + value = res = div_mod(x, s, N)"#; #[cfg(feature = "skip_next_instruction_hint")] pub const SKIP_NEXT_INSTRUCTION: &str = "skip_next_instruction()"; From 23c7d8f3166d76c39a0b420ee50ce29e843da03d Mon Sep 17 00:00:00 2001 From: fmoletta <99273364+fmoletta@users.noreply.github.com> Date: Fri, 21 Apr 2023 17:00:10 -0300 Subject: [PATCH 24/41] Fix test --- .../builtin_hint_processor/ec_recover.rs | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/hint_processor/builtin_hint_processor/ec_recover.rs b/src/hint_processor/builtin_hint_processor/ec_recover.rs index 0cabb2f858..e55e4a23a1 100644 --- a/src/hint_processor/builtin_hint_processor/ec_recover.rs +++ b/src/hint_processor/builtin_hint_processor/ec_recover.rs @@ -82,14 +82,8 @@ mod tests { ((1, 7), 0), ((1, 8), 0) ]; - run_hint!( - vm, - ids_data, - hint_code::EC_RECOVER_DIV_MOD_N_PACKED, - &mut exec_scopes - ) - .unwrap(); - //assert!(run_hint!(vm, ids_data, hint_code::DIV_MOD_N_PACKED_DIVMOD_CAIRO_N, &mut exec_scopes).is_ok()); + + assert!(run_hint!(vm, ids_data, hint_code::DIV_MOD_N_PACKED_DIVMOD_CAIRO_N, &mut exec_scopes).is_ok()); check_scope!( &exec_scopes, From 992c7fe1eb7148d247f7498766a8b9627e016e85 Mon Sep 17 00:00:00 2001 From: fmoletta <99273364+fmoletta@users.noreply.github.com> Date: Fri, 21 Apr 2023 17:01:01 -0300 Subject: [PATCH 25/41] Fix test --- src/hint_processor/builtin_hint_processor/ec_recover.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hint_processor/builtin_hint_processor/ec_recover.rs b/src/hint_processor/builtin_hint_processor/ec_recover.rs index e55e4a23a1..d0427656df 100644 --- a/src/hint_processor/builtin_hint_processor/ec_recover.rs +++ b/src/hint_processor/builtin_hint_processor/ec_recover.rs @@ -83,7 +83,7 @@ mod tests { ((1, 8), 0) ]; - assert!(run_hint!(vm, ids_data, hint_code::DIV_MOD_N_PACKED_DIVMOD_CAIRO_N, &mut exec_scopes).is_ok()); + assert!(run_hint!(vm, ids_data, hint_code::EC_RECOVER_DIV_MOD_N_PACKED, &mut exec_scopes).is_ok()); check_scope!( &exec_scopes, From b4340525744e74179cf97f177bc6bf964ca0fcda Mon Sep 17 00:00:00 2001 From: Federica Date: Fri, 21 Apr 2023 17:03:26 -0300 Subject: [PATCH 26/41] Add hint code + hint impl + unit test --- .../builtin_hint_processor_definition.rs | 5 +- .../builtin_hint_processor/ec_recover.rs | 65 ++++++++++++++++++- .../builtin_hint_processor/hint_code.rs | 6 ++ 3 files changed, 72 insertions(+), 4 deletions(-) diff --git a/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs b/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs index 8b399761f4..22e25054fb 100644 --- a/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs +++ b/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs @@ -83,7 +83,7 @@ use felt::Felt252; #[cfg(feature = "skip_next_instruction_hint")] use crate::hint_processor::builtin_hint_processor::skip_next_instruction::skip_next_instruction; -use super::ec_recover::ec_recover_divmod_n_packed; +use super::ec_recover::{ec_recover_divmod_n_packed, ec_recover_sub_a_b}; pub struct HintProcessorData { pub code: String, @@ -566,6 +566,9 @@ impl HintProcessor for BuiltinHintProcessor { &hint_data.ids_data, &hint_data.ap_tracking, ), + hint_code::EC_RECOVER_SUB_A_B => { + ec_recover_sub_a_b(vm, exec_scopes, &hint_data.ids_data, &hint_data.ap_tracking) + } #[cfg(feature = "skip_next_instruction_hint")] hint_code::SKIP_NEXT_INSTRUCTION => skip_next_instruction(vm), code => Err(HintError::UnknownHint(code.to_string())), diff --git a/src/hint_processor/builtin_hint_processor/ec_recover.rs b/src/hint_processor/builtin_hint_processor/ec_recover.rs index 0cabb2f858..14f9691e4c 100644 --- a/src/hint_processor/builtin_hint_processor/ec_recover.rs +++ b/src/hint_processor/builtin_hint_processor/ec_recover.rs @@ -36,6 +36,31 @@ pub fn ec_recover_divmod_n_packed( Ok(()) } +/* Implements Hint: +%{ + from starkware.cairo.common.cairo_secp.secp_utils import pack + from starkware.python.math_utils import div_mod, safe_div + + a = pack(ids.x, PRIME) + b = pack(ids.s, PRIME) + value = res = a - b +%} + */ +pub fn ec_recover_sub_a_b( + vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + ids_data: &HashMap, + ap_tracking: &ApTracking, +) -> Result<(), HintError> { + let a = pack(BigInt3::from_var_name("a", vm, ids_data, ap_tracking)?); + let b = pack(BigInt3::from_var_name("b", vm, ids_data, ap_tracking)?); + + let value = a - b; + exec_scopes.insert_value("value", value.clone()); + exec_scopes.insert_value("res", value); + Ok(()) +} + #[cfg(test)] mod tests { use num_bigint::BigInt; @@ -82,18 +107,52 @@ mod tests { ((1, 7), 0), ((1, 8), 0) ]; - run_hint!( + + assert!(run_hint!( vm, ids_data, hint_code::EC_RECOVER_DIV_MOD_N_PACKED, &mut exec_scopes ) - .unwrap(); - //assert!(run_hint!(vm, ids_data, hint_code::DIV_MOD_N_PACKED_DIVMOD_CAIRO_N, &mut exec_scopes).is_ok()); + .is_ok()); check_scope!( &exec_scopes, [("value", BigInt::from(5)), ("res", BigInt::from(5))] ); } + + #[test] + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] + fn run_ec_recover_sub_a_b_ok() { + let mut vm = vm!(); + let mut exec_scopes = ExecutionScopes::new(); + + vm.run_context.fp = 8; + let ids_data = non_continuous_ids_data![("a", -8), ("b", -5)]; + + vm.segments = segments![ + //a + ((1, 0), 100), + ((1, 1), 0), + ((1, 2), 0), + //b + ((1, 3), 25), + ((1, 4), 0), + ((1, 5), 0), + ]; + + assert!(run_hint!( + vm, + ids_data, + hint_code::EC_RECOVER_SUB_A_B, + &mut exec_scopes + ) + .is_ok()); + + check_scope!( + &exec_scopes, + [("value", BigInt::from(75)), ("res", BigInt::from(75))] + ); + } } diff --git a/src/hint_processor/builtin_hint_processor/hint_code.rs b/src/hint_processor/builtin_hint_processor/hint_code.rs index cb6a0d454c..38cde35918 100644 --- a/src/hint_processor/builtin_hint_processor/hint_code.rs +++ b/src/hint_processor/builtin_hint_processor/hint_code.rs @@ -921,5 +921,11 @@ pub const EC_RECOVER_DIV_MOD_N_PACKED: &str = r#"from starkware.cairo.common.cai x = pack(ids.x, PRIME) % N s = pack(ids.s, PRIME) % N value = res = div_mod(x, s, N)"#; +pub const EC_RECOVER_SUB_A_B: &str = r#"from starkware.cairo.common.cairo_secp.secp_utils import pack +from starkware.python.math_utils import div_mod, safe_div + +a = pack(ids.x, PRIME) +b = pack(ids.s, PRIME) +value = res = a - b"#; #[cfg(feature = "skip_next_instruction_hint")] pub const SKIP_NEXT_INSTRUCTION: &str = "skip_next_instruction()"; From ad46d43879e54b0245eb67543a14bb44715619d0 Mon Sep 17 00:00:00 2001 From: Federica Date: Fri, 21 Apr 2023 17:12:27 -0300 Subject: [PATCH 27/41] fmt --- src/hint_processor/builtin_hint_processor/ec_recover.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/hint_processor/builtin_hint_processor/ec_recover.rs b/src/hint_processor/builtin_hint_processor/ec_recover.rs index d0427656df..7165702fdf 100644 --- a/src/hint_processor/builtin_hint_processor/ec_recover.rs +++ b/src/hint_processor/builtin_hint_processor/ec_recover.rs @@ -83,7 +83,13 @@ mod tests { ((1, 8), 0) ]; - assert!(run_hint!(vm, ids_data, hint_code::EC_RECOVER_DIV_MOD_N_PACKED, &mut exec_scopes).is_ok()); + assert!(run_hint!( + vm, + ids_data, + hint_code::EC_RECOVER_DIV_MOD_N_PACKED, + &mut exec_scopes + ) + .is_ok()); check_scope!( &exec_scopes, From c078fc2fcc528b84e94d81b4a4f32d08aeaaaa9b Mon Sep 17 00:00:00 2001 From: Federica Date: Fri, 21 Apr 2023 17:16:38 -0300 Subject: [PATCH 28/41] Add integration test --- cairo_programs/ec_recover.cairo | 23 +++++++++++++++++++ .../builtin_hint_processor/hint_code.rs | 4 ++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/cairo_programs/ec_recover.cairo b/cairo_programs/ec_recover.cairo index 83606c8992..b066663ba2 100644 --- a/cairo_programs/ec_recover.cairo +++ b/cairo_programs/ec_recover.cairo @@ -6,6 +6,7 @@ func test_div_mod_n_packed_hint{range_check_ptr: felt}() { tempvar n = BigInt3(177, 0, 0); tempvar x = BigInt3(25, 0, 0); tempvar s = BigInt3(5, 0, 0); + %{ from starkware.cairo.common.cairo_secp.secp_utils import pack from starkware.python.math_utils import div_mod, safe_div @@ -18,10 +19,32 @@ func test_div_mod_n_packed_hint{range_check_ptr: felt}() { let (res) = nondet_bigint3(); assert res = BigInt3(5,0,0); + + return(); +} + +func test_sub_a_b_hint{range_check_ptr: felt}() { + + tempvar a = BigInt3(100, 0, 0); + tempvar b = BigInt3(25, 0, 0); + + %{ + from starkware.cairo.common.cairo_secp.secp_utils import pack + from starkware.python.math_utils import div_mod, safe_div + + a = pack(ids.a, PRIME) + b = pack(ids.b, PRIME) + value = res = a - b + %} + + let (res) = nondet_bigint3(); + assert res = BigInt3(75,0,0); + return(); } func main{range_check_ptr: felt}() { test_div_mod_n_packed_hint(); + test_sub_a_b_hint(); return(); } diff --git a/src/hint_processor/builtin_hint_processor/hint_code.rs b/src/hint_processor/builtin_hint_processor/hint_code.rs index 38cde35918..265706fa89 100644 --- a/src/hint_processor/builtin_hint_processor/hint_code.rs +++ b/src/hint_processor/builtin_hint_processor/hint_code.rs @@ -924,8 +924,8 @@ pub const EC_RECOVER_DIV_MOD_N_PACKED: &str = r#"from starkware.cairo.common.cai pub const EC_RECOVER_SUB_A_B: &str = r#"from starkware.cairo.common.cairo_secp.secp_utils import pack from starkware.python.math_utils import div_mod, safe_div -a = pack(ids.x, PRIME) -b = pack(ids.s, PRIME) +a = pack(ids.a, PRIME) +b = pack(ids.b, PRIME) value = res = a - b"#; #[cfg(feature = "skip_next_instruction_hint")] pub const SKIP_NEXT_INSTRUCTION: &str = "skip_next_instruction()"; From ad87b73e76777671c019fe941616c45731888a3f Mon Sep 17 00:00:00 2001 From: Federica Date: Fri, 21 Apr 2023 17:18:56 -0300 Subject: [PATCH 29/41] Add changelog entry --- CHANGELOG.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index cd9b9a3032..f9c73e7bf0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,23 @@ #### Upcoming Changes +* Implement hint on ec_recover.json whitelist [#1036](https://github.com/lambdaclass/cairo-rs/pull/1036): + + `BuiltinHintProcessor` now supports the following hint: + + ```python + + %{ + from starkware.cairo.common.cairo_secp.secp_utils import pack + from starkware.python.math_utils import div_mod, safe_div + + a = pack(ids.a, PRIME) + b = pack(ids.b, PRIME) + value = res = a - b + %} + + ``` + * Implement hint on ec_recover.json whitelist [#1032](https://github.com/lambdaclass/cairo-rs/pull/1032): `BuiltinHintProcessor` now supports the following hint: From 1ad171667ca0028aa889f70da8b31ad4cd9abd5d Mon Sep 17 00:00:00 2001 From: Federica Date: Fri, 21 Apr 2023 17:31:58 -0300 Subject: [PATCH 30/41] Implement hint + add tests --- cairo_programs/ec_recover.cairo | 25 +++++++ .../builtin_hint_processor_definition.rs | 5 +- .../builtin_hint_processor/ec_recover.rs | 73 +++++++++++++++++++ .../builtin_hint_processor/hint_code.rs | 9 +++ 4 files changed, 111 insertions(+), 1 deletion(-) diff --git a/cairo_programs/ec_recover.cairo b/cairo_programs/ec_recover.cairo index b066663ba2..b5b630861c 100644 --- a/cairo_programs/ec_recover.cairo +++ b/cairo_programs/ec_recover.cairo @@ -43,8 +43,33 @@ func test_sub_a_b_hint{range_check_ptr: felt}() { return(); } +func test_product_mod_hint{range_check_ptr: felt}() { + + tempvar a = BigInt3(60, 0, 0); + tempvar b = BigInt3(2, 0, 0); + tempvar m = BigInt3(100, 0, 0); + + %{ + from starkware.cairo.common.cairo_secp.secp_utils import pack + from starkware.python.math_utils import div_mod, safe_div + + a = pack(ids.a, PRIME) + b = pack(ids.b, PRIME) + product = a * b + m = pack(ids.m, PRIME) + + value = res = product % m + %} + + let (res) = nondet_bigint3(); + assert res = BigInt3(20,0,0); + + return(); +} + func main{range_check_ptr: felt}() { test_div_mod_n_packed_hint(); test_sub_a_b_hint(); + test_product_mod_hint(); return(); } diff --git a/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs b/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs index 22e25054fb..e83e23efa6 100644 --- a/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs +++ b/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs @@ -83,7 +83,7 @@ use felt::Felt252; #[cfg(feature = "skip_next_instruction_hint")] use crate::hint_processor::builtin_hint_processor::skip_next_instruction::skip_next_instruction; -use super::ec_recover::{ec_recover_divmod_n_packed, ec_recover_sub_a_b}; +use super::ec_recover::{ec_recover_divmod_n_packed, ec_recover_product_mod, ec_recover_sub_a_b}; pub struct HintProcessorData { pub code: String, @@ -569,6 +569,9 @@ impl HintProcessor for BuiltinHintProcessor { hint_code::EC_RECOVER_SUB_A_B => { ec_recover_sub_a_b(vm, exec_scopes, &hint_data.ids_data, &hint_data.ap_tracking) } + hint_code::EC_RECOVER_PRODUCT_MOD => { + ec_recover_product_mod(vm, exec_scopes, &hint_data.ids_data, &hint_data.ap_tracking) + } #[cfg(feature = "skip_next_instruction_hint")] hint_code::SKIP_NEXT_INSTRUCTION => skip_next_instruction(vm), code => Err(HintError::UnknownHint(code.to_string())), diff --git a/src/hint_processor/builtin_hint_processor/ec_recover.rs b/src/hint_processor/builtin_hint_processor/ec_recover.rs index 14f9691e4c..d2e0f7197f 100644 --- a/src/hint_processor/builtin_hint_processor/ec_recover.rs +++ b/src/hint_processor/builtin_hint_processor/ec_recover.rs @@ -61,6 +61,37 @@ pub fn ec_recover_sub_a_b( Ok(()) } +/* Implements Hint: +%{ + from starkware.cairo.common.cairo_secp.secp_utils import pack + from starkware.python.math_utils import div_mod, safe_div + + a = pack(ids.a, PRIME) + b = pack(ids.b, PRIME) + product = a * b + m = pack(ids.m, PRIME) + + value = res = product % m +%} + */ +pub fn ec_recover_product_mod( + vm: &mut VirtualMachine, + exec_scopes: &mut ExecutionScopes, + ids_data: &HashMap, + ap_tracking: &ApTracking, +) -> Result<(), HintError> { + let a = pack(BigInt3::from_var_name("a", vm, ids_data, ap_tracking)?); + let b = pack(BigInt3::from_var_name("b", vm, ids_data, ap_tracking)?); + let m = pack(BigInt3::from_var_name("m", vm, ids_data, ap_tracking)?); + + let product = a * b; + exec_scopes.insert_value("product", product.clone()); + let value = product.mod_floor(&m); + exec_scopes.insert_value("value", value.clone()); + exec_scopes.insert_value("res", value); + Ok(()) +} + #[cfg(test)] mod tests { use num_bigint::BigInt; @@ -155,4 +186,46 @@ mod tests { [("value", BigInt::from(75)), ("res", BigInt::from(75))] ); } + + #[test] + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] + fn run_ec_recover_product_mod_ok() { + let mut vm = vm!(); + let mut exec_scopes = ExecutionScopes::new(); + + vm.run_context.fp = 8; + let ids_data = non_continuous_ids_data![("a", -8), ("b", -5), ("m", -2)]; + + vm.segments = segments![ + //a + ((1, 0), 60), + ((1, 1), 0), + ((1, 2), 0), + //b + ((1, 3), 2), + ((1, 4), 0), + ((1, 5), 0), + //m + ((1, 6), 100), + ((1, 7), 0), + ((1, 8), 0) + ]; + + assert!(run_hint!( + vm, + ids_data, + hint_code::EC_RECOVER_DIV_MOD_N_PACKED, + &mut exec_scopes + ) + .is_ok()); + + check_scope!( + &exec_scopes, + [ + ("value", BigInt::from(20)), + ("res", BigInt::from(20)), + ("product", BigInt::from(120)) + ] + ); + } } diff --git a/src/hint_processor/builtin_hint_processor/hint_code.rs b/src/hint_processor/builtin_hint_processor/hint_code.rs index 265706fa89..2c72c4c6cb 100644 --- a/src/hint_processor/builtin_hint_processor/hint_code.rs +++ b/src/hint_processor/builtin_hint_processor/hint_code.rs @@ -927,5 +927,14 @@ from starkware.python.math_utils import div_mod, safe_div a = pack(ids.a, PRIME) b = pack(ids.b, PRIME) value = res = a - b"#; +pub const EC_RECOVER_PRODUCT_MOD: &str = r#"from starkware.cairo.common.cairo_secp.secp_utils import pack +from starkware.python.math_utils import div_mod, safe_div + +a = pack(ids.a, PRIME) +b = pack(ids.b, PRIME) +product = a * b +m = pack(ids.m, PRIME) + +value = res = product % m"#; #[cfg(feature = "skip_next_instruction_hint")] pub const SKIP_NEXT_INSTRUCTION: &str = "skip_next_instruction()"; From 2f60b81e9bf880fb63c6018917cfedab78441013 Mon Sep 17 00:00:00 2001 From: Federica Date: Fri, 21 Apr 2023 17:35:04 -0300 Subject: [PATCH 31/41] Add changelog entry --- CHANGELOG.md | 18 +++++++++++++++++- .../builtin_hint_processor/ec_recover.rs | 2 +- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f9c73e7bf0..3e49b9ab9e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,24 @@ `BuiltinHintProcessor` now supports the following hint: ```python + %{ + from starkware.cairo.common.cairo_secp.secp_utils import pack + from starkware.python.math_utils import div_mod, safe_div + + a = pack(ids.a, PRIME) + b = pack(ids.b, PRIME) + product = a * b + m = pack(ids.m, PRIME) + value = res = product % m + %} + ``` + +* Implement hint on ec_recover.json whitelist [#1036](https://github.com/lambdaclass/cairo-rs/pull/1036): + + `BuiltinHintProcessor` now supports the following hint: + + ```python %{ from starkware.cairo.common.cairo_secp.secp_utils import pack from starkware.python.math_utils import div_mod, safe_div @@ -16,7 +33,6 @@ b = pack(ids.b, PRIME) value = res = a - b %} - ``` * Implement hint on ec_recover.json whitelist [#1032](https://github.com/lambdaclass/cairo-rs/pull/1032): diff --git a/src/hint_processor/builtin_hint_processor/ec_recover.rs b/src/hint_processor/builtin_hint_processor/ec_recover.rs index d2e0f7197f..1ad08893b3 100644 --- a/src/hint_processor/builtin_hint_processor/ec_recover.rs +++ b/src/hint_processor/builtin_hint_processor/ec_recover.rs @@ -214,7 +214,7 @@ mod tests { assert!(run_hint!( vm, ids_data, - hint_code::EC_RECOVER_DIV_MOD_N_PACKED, + hint_code::EC_RECOVER_PRODUCT_MOD, &mut exec_scopes ) .is_ok()); From d183b6b2a4a0662c1f70e7ad2b05fd2a6d3b635c Mon Sep 17 00:00:00 2001 From: Federica Date: Fri, 21 Apr 2023 17:41:31 -0300 Subject: [PATCH 32/41] Remove indent from hint string --- src/hint_processor/builtin_hint_processor/hint_code.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/hint_processor/builtin_hint_processor/hint_code.rs b/src/hint_processor/builtin_hint_processor/hint_code.rs index c308a9ec11..da39726330 100644 --- a/src/hint_processor/builtin_hint_processor/hint_code.rs +++ b/src/hint_processor/builtin_hint_processor/hint_code.rs @@ -927,11 +927,11 @@ pub const QUAD_BIT: &str = r#"ids.quad_bit = ( pub const DI_BIT: &str = r#"ids.dibit = ((ids.scalar_u >> ids.m) & 1) + 2 * ((ids.scalar_v >> ids.m) & 1)"#; pub const EC_RECOVER_DIV_MOD_N_PACKED: &str = r#"from starkware.cairo.common.cairo_secp.secp_utils import pack - from starkware.python.math_utils import div_mod, safe_div +from starkware.python.math_utils import div_mod, safe_div - N = pack(ids.n, PRIME) - x = pack(ids.x, PRIME) % N - s = pack(ids.s, PRIME) % N - value = res = div_mod(x, s, N)"#; +N = pack(ids.n, PRIME) +x = pack(ids.x, PRIME) % N +s = pack(ids.s, PRIME) % N +value = res = div_mod(x, s, N)"#; #[cfg(feature = "skip_next_instruction_hint")] pub const SKIP_NEXT_INSTRUCTION: &str = "skip_next_instruction()"; From 6cd45c2a9191a836eb727f041f3de37cf490699a Mon Sep 17 00:00:00 2001 From: Federica Date: Fri, 21 Apr 2023 18:04:23 -0300 Subject: [PATCH 33/41] Add wasm import --- src/hint_processor/builtin_hint_processor/ec_recover.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/hint_processor/builtin_hint_processor/ec_recover.rs b/src/hint_processor/builtin_hint_processor/ec_recover.rs index 7165702fdf..927b0a9f0f 100644 --- a/src/hint_processor/builtin_hint_processor/ec_recover.rs +++ b/src/hint_processor/builtin_hint_processor/ec_recover.rs @@ -59,6 +59,9 @@ mod tests { types::{exec_scope::ExecutionScopes, relocatable::MaybeRelocatable}, }; + #[cfg(target_arch = "wasm32")] + use wasm_bindgen_test::*; + #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn run_ec_recover_divmod_n_packed_ok() { From d4da292d0dd2f500d1b6077d926542b7e7d76d93 Mon Sep 17 00:00:00 2001 From: Federica Date: Fri, 21 Apr 2023 18:14:12 -0300 Subject: [PATCH 34/41] Fix prev hint + implement new hint --- .../builtin_hint_processor/ec_recover.rs | 19 ++++++++++++++++++- .../builtin_hint_processor/hint_code.rs | 1 + 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/hint_processor/builtin_hint_processor/ec_recover.rs b/src/hint_processor/builtin_hint_processor/ec_recover.rs index 9fbce9addc..c6035e54ad 100644 --- a/src/hint_processor/builtin_hint_processor/ec_recover.rs +++ b/src/hint_processor/builtin_hint_processor/ec_recover.rs @@ -7,6 +7,7 @@ use crate::{ types::exec_scope::ExecutionScopes, vm::{errors::hint_errors::HintError, vm_core::VirtualMachine}, }; +use num_bigint::BigInt; use num_integer::Integer; /* Implements Hint: @@ -86,12 +87,27 @@ pub fn ec_recover_product_mod( let product = a * b; exec_scopes.insert_value("product", product.clone()); + exec_scopes.insert_value("m", m.clone()); let value = product.mod_floor(&m); exec_scopes.insert_value("value", value.clone()); exec_scopes.insert_value("res", value); Ok(()) } +/* Implements Hint: +%{ + value = k = product // m +%} + */ +pub fn ec_recover_product_div_m(exec_scopes: &mut ExecutionScopes) -> Result<(), HintError> { + let product: &BigInt = exec_scopes.get_ref("product")?; + let m: &BigInt = exec_scopes.get_ref("m")?; + let value = product.div_floor(m); + exec_scopes.insert_value("k", value.clone()); + exec_scopes.insert_value("value", value); + Ok(()) +} + #[cfg(test)] mod tests { use num_bigint::BigInt; @@ -227,7 +243,8 @@ mod tests { [ ("value", BigInt::from(20)), ("res", BigInt::from(20)), - ("product", BigInt::from(120)) + ("product", BigInt::from(120)), + ("m", BigInt::from(100)) ] ); } diff --git a/src/hint_processor/builtin_hint_processor/hint_code.rs b/src/hint_processor/builtin_hint_processor/hint_code.rs index 5a54d00702..226aec5604 100644 --- a/src/hint_processor/builtin_hint_processor/hint_code.rs +++ b/src/hint_processor/builtin_hint_processor/hint_code.rs @@ -948,5 +948,6 @@ product = a * b m = pack(ids.m, PRIME) value = res = product % m"#; +pub const EC_RECOVER_PRODUCT_DIV_M: &str = "value = k = product // m"; #[cfg(feature = "skip_next_instruction_hint")] pub const SKIP_NEXT_INSTRUCTION: &str = "skip_next_instruction()"; From c9bb1ec7d117c24c193c14ca4784fe6f5831f4cc Mon Sep 17 00:00:00 2001 From: Federica Date: Fri, 21 Apr 2023 18:23:44 -0300 Subject: [PATCH 35/41] Add tests --- cairo_programs/ec_recover.cairo | 11 +++++++-- .../builtin_hint_processor_definition.rs | 6 ++++- .../builtin_hint_processor/ec_recover.rs | 24 +++++++++++++++++++ 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/cairo_programs/ec_recover.cairo b/cairo_programs/ec_recover.cairo index b5b630861c..d33d058d13 100644 --- a/cairo_programs/ec_recover.cairo +++ b/cairo_programs/ec_recover.cairo @@ -43,7 +43,7 @@ func test_sub_a_b_hint{range_check_ptr: felt}() { return(); } -func test_product_mod_hint{range_check_ptr: felt}() { +func test_product_hints{range_check_ptr: felt}() { tempvar a = BigInt3(60, 0, 0); tempvar b = BigInt3(2, 0, 0); @@ -64,12 +64,19 @@ func test_product_mod_hint{range_check_ptr: felt}() { let (res) = nondet_bigint3(); assert res = BigInt3(20,0,0); + %{ + value = k = product // m + %} + + let (k) = nondet_bigint3(); + assert k = BigInt3(1,0,0); + return(); } func main{range_check_ptr: felt}() { test_div_mod_n_packed_hint(); test_sub_a_b_hint(); - test_product_mod_hint(); + test_product_hints(); return(); } diff --git a/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs b/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs index 1bdf107307..41094fbd96 100644 --- a/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs +++ b/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs @@ -84,7 +84,10 @@ use felt::Felt252; #[cfg(feature = "skip_next_instruction_hint")] use crate::hint_processor::builtin_hint_processor::skip_next_instruction::skip_next_instruction; -use super::ec_recover::{ec_recover_divmod_n_packed, ec_recover_product_mod, ec_recover_sub_a_b}; +use super::ec_recover::{ + ec_recover_divmod_n_packed, ec_recover_product_div_m, ec_recover_product_mod, + ec_recover_sub_a_b, +}; pub struct HintProcessorData { pub code: String, @@ -582,6 +585,7 @@ impl HintProcessor for BuiltinHintProcessor { hint_code::EC_RECOVER_PRODUCT_MOD => { ec_recover_product_mod(vm, exec_scopes, &hint_data.ids_data, &hint_data.ap_tracking) } + hint_code::EC_RECOVER_PRODUCT_DIV_M => ec_recover_product_div_m(exec_scopes), #[cfg(feature = "skip_next_instruction_hint")] hint_code::SKIP_NEXT_INSTRUCTION => skip_next_instruction(vm), code => Err(HintError::UnknownHint(code.to_string())), diff --git a/src/hint_processor/builtin_hint_processor/ec_recover.rs b/src/hint_processor/builtin_hint_processor/ec_recover.rs index c6035e54ad..b6c767b130 100644 --- a/src/hint_processor/builtin_hint_processor/ec_recover.rs +++ b/src/hint_processor/builtin_hint_processor/ec_recover.rs @@ -248,4 +248,28 @@ mod tests { ] ); } + + #[test] + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] + fn run_ec_recover_product_div_m_ok() { + let mut vm = vm!(); + let mut exec_scopes = ExecutionScopes::new(); + exec_scopes.insert_value("product", BigInt::from(250)); + exec_scopes.insert_value("m", BigInt::from(100)); + + let ids_data = ids_data!["none"]; + + assert!(run_hint!( + vm, + ids_data, + hint_code::EC_RECOVER_PRODUCT_DIV_M, + &mut exec_scopes + ) + .is_ok()); + + check_scope!( + &exec_scopes, + [("value", BigInt::from(2)), ("k", BigInt::from(2))] + ); + } } From b446c390593d0207343b950114ebe6f9039779b3 Mon Sep 17 00:00:00 2001 From: Federica Date: Fri, 21 Apr 2023 18:25:36 -0300 Subject: [PATCH 36/41] Add changelog entry --- CHANGELOG.md | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f980cca15..3fdcd36865 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,17 @@ #### Upcoming Changes -* Implement hint on ec_recover.json whitelist [#1036](https://github.com/lambdaclass/cairo-rs/pull/1036): +* Implement hint on ec_recover.json whitelist [#1036](https://github.com/lambdaclass/cairo-rs/pull/1038): + + `BuiltinHintProcessor` now supports the following hint: + + ```python + %{ + value = k = product // m + %} + ``` + +* Implement hint on ec_recover.json whitelist [#1036](https://github.com/lambdaclass/cairo-rs/pull/1037): `BuiltinHintProcessor` now supports the following hint: From 1545cc7fb9e472f7a5198adc117329108121f3ad Mon Sep 17 00:00:00 2001 From: fmoletta <99273364+fmoletta@users.noreply.github.com> Date: Fri, 21 Apr 2023 18:26:07 -0300 Subject: [PATCH 37/41] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f980cca15..6eb5df278d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ #### Upcoming Changes -* Implement hint on ec_recover.json whitelist [#1036](https://github.com/lambdaclass/cairo-rs/pull/1036): +* Implement hint on ec_recover.json whitelist [#1036](https://github.com/lambdaclass/cairo-rs/pull/1037): `BuiltinHintProcessor` now supports the following hint: From 050b6bf20ad5a26dbd178ae029b23ca04f161998 Mon Sep 17 00:00:00 2001 From: fmoletta <99273364+fmoletta@users.noreply.github.com> Date: Fri, 21 Apr 2023 18:26:31 -0300 Subject: [PATCH 38/41] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6eb5df278d..f1df5d3c95 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ #### Upcoming Changes -* Implement hint on ec_recover.json whitelist [#1036](https://github.com/lambdaclass/cairo-rs/pull/1037): +* Implement hint on ec_recover.json whitelist [#1037](https://github.com/lambdaclass/cairo-rs/pull/1037): `BuiltinHintProcessor` now supports the following hint: From 717ab6029215c07cf4fd7af0d9e2cd22113bf0a4 Mon Sep 17 00:00:00 2001 From: Federica Date: Fri, 21 Apr 2023 18:27:08 -0300 Subject: [PATCH 39/41] Fix Pr numbers --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3fdcd36865..dcbca0ba95 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ #### Upcoming Changes -* Implement hint on ec_recover.json whitelist [#1036](https://github.com/lambdaclass/cairo-rs/pull/1038): +* Implement hint on ec_recover.json whitelist [#1038](https://github.com/lambdaclass/cairo-rs/pull/1038): `BuiltinHintProcessor` now supports the following hint: @@ -12,7 +12,7 @@ %} ``` -* Implement hint on ec_recover.json whitelist [#1036](https://github.com/lambdaclass/cairo-rs/pull/1037): +* Implement hint on ec_recover.json whitelist [#1037](https://github.com/lambdaclass/cairo-rs/pull/1037): `BuiltinHintProcessor` now supports the following hint: From 51e065b2401fc175452dfaa8243238b60baf8876 Mon Sep 17 00:00:00 2001 From: fmoletta <99273364+fmoletta@users.noreply.github.com> Date: Fri, 21 Apr 2023 18:46:34 -0300 Subject: [PATCH 40/41] Update src/hint_processor/builtin_hint_processor/ec_recover.rs Co-authored-by: Mario Rugiero --- src/hint_processor/builtin_hint_processor/ec_recover.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/hint_processor/builtin_hint_processor/ec_recover.rs b/src/hint_processor/builtin_hint_processor/ec_recover.rs index b6c767b130..74f44da0da 100644 --- a/src/hint_processor/builtin_hint_processor/ec_recover.rs +++ b/src/hint_processor/builtin_hint_processor/ec_recover.rs @@ -86,9 +86,9 @@ pub fn ec_recover_product_mod( let m = pack(BigInt3::from_var_name("m", vm, ids_data, ap_tracking)?); let product = a * b; - exec_scopes.insert_value("product", product.clone()); - exec_scopes.insert_value("m", m.clone()); let value = product.mod_floor(&m); + exec_scopes.insert_value("product", product); + exec_scopes.insert_value("m", m); exec_scopes.insert_value("value", value.clone()); exec_scopes.insert_value("res", value); Ok(()) From 93e5145dbe599dc05278c4f90a70fa51a1c3464b Mon Sep 17 00:00:00 2001 From: Federica Date: Tue, 25 Apr 2023 12:00:21 -0300 Subject: [PATCH 41/41] Fix changelog --- CHANGELOG.md | 41 +++++++++++------------------------------ 1 file changed, 11 insertions(+), 30 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9ff9a65e1f..957c6f9dac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,6 @@ #### Upcoming Changes -<<<<<<< HEAD * Implement hint on ec_recover.json whitelist [#1038](https://github.com/lambdaclass/cairo-rs/pull/1038): `BuiltinHintProcessor` now supports the following hint: @@ -14,22 +13,11 @@ ``` * Implement hint on ec_recover.json whitelist [#1037](https://github.com/lambdaclass/cairo-rs/pull/1037): -======= -<<<<<<< HEAD -* Implement hint on ec_recover.json whitelist [#1037](https://github.com/lambdaclass/cairo-rs/pull/1037): -======= -* Implement hint for `starkware.cairo.common.cairo_keccak.keccak.finalize_keccak` as described by whitelist `starknet/security/whitelists/cairo_keccak.json` [#1041](https://github.com/lambdaclass/cairo-rs/pull/1041) ->>>>>>> 50c90d944bc090578824bb50bed1b1ada5a5fbc0 ->>>>>>> c729b9f3990195f4bf04d5fea7a0f6dd1ad29b61 `BuiltinHintProcessor` now supports the following hint: ```python %{ -<<<<<<< HEAD -======= -<<<<<<< HEAD ->>>>>>> c729b9f3990195f4bf04d5fea7a0f6dd1ad29b61 from starkware.cairo.common.cairo_secp.secp_utils import pack from starkware.python.math_utils import div_mod, safe_div @@ -39,9 +27,16 @@ m = pack(ids.m, PRIME) value = res = product % m -<<<<<<< HEAD -======= -======= + + %} + ``` + +* Implement hint for `starkware.cairo.common.cairo_keccak.keccak.finalize_keccak` as described by whitelist `starknet/security/whitelists/cairo_keccak.json` [#1041](https://github.com/lambdaclass/cairo-rs/pull/1041) + + `BuiltinHintProcessor` now supports the following hint: + + ```python + %{ # Add dummy pairs of input and output. _keccak_state_size_felts = int(ids.KECCAK_STATE_SIZE_FELTS) _block_size = int(ids.BLOCK_SIZE) @@ -50,8 +45,6 @@ inp = [0] * _keccak_state_size_felts padding = (inp + keccak_func(inp)) * _block_size segments.write_arg(ids.keccak_ptr_end, padding) ->>>>>>> 50c90d944bc090578824bb50bed1b1ada5a5fbc0 ->>>>>>> c729b9f3990195f4bf04d5fea7a0f6dd1ad29b61 %} ``` @@ -60,10 +53,7 @@ `BuiltinHintProcessor` now supports the following hint: ```python -<<<<<<< HEAD -======= ->>>>>>> c729b9f3990195f4bf04d5fea7a0f6dd1ad29b61 %{ from starkware.cairo.common.cairo_secp.secp_utils import pack from starkware.python.math_utils import div_mod, safe_div @@ -72,10 +62,7 @@ b = pack(ids.b, PRIME) value = res = a - b %} -<<<<<<< HEAD -======= ->>>>>>> c729b9f3990195f4bf04d5fea7a0f6dd1ad29b61 ``` * Implement hint on ec_recover.json whitelist [#1032](https://github.com/lambdaclass/cairo-rs/pull/1032): @@ -94,10 +81,6 @@ %} ``` -<<<<<<< HEAD -======= -<<<<<<< HEAD -======= * Implement hints on field_arithmetic lib (Part 2) [#1004](https://github.com/lambdaclass/cairo-rs/pull/1004) `BuiltinHintProcessor` now supports the following hint: @@ -156,8 +139,6 @@ ids.x_inverse_mod_p.high = x_inverse_mod_p_split[1] ``` ->>>>>>> fb731257da6fc2842abff103fccab964ae87bb38 ->>>>>>> c729b9f3990195f4bf04d5fea7a0f6dd1ad29b61 * BREAKING CHANGE: Fix `CairoRunner::get_memory_holes` [#1027](https://github.com/lambdaclass/cairo-rs/pull/1027): * Skip builtin segements when counting memory holes @@ -898,4 +879,4 @@ * `pub fn from_vm_error(runner: &CairoRunner, error: VirtualMachineError, pc: usize) -> Self` is now `pub fn from_vm_error(runner: &CairoRunner, vm: &VirtualMachine, error: VirtualMachineError) -> Self` * `pub fn get_location(pc: &usize, runner: &CairoRunner) -> Option` is now `pub fn get_location(pc: usize, runner: &CairoRunner) -> Option` * `pub fn decode_instruction(encoded_instr: i64, mut imm: Option) -> Result` is now `pub fn decode_instruction(encoded_instr: i64, mut imm: Option<&BigInt>) -> Result` - * `VmExcepion` field's string format now mirror their cairo-lang conterparts. + * `VmExcepion` field's string format now mirror their cairo-lang conterparts. \ No newline at end of file