From 2a9ead5fb5339587214b34eb2045dfa209efb332 Mon Sep 17 00:00:00 2001 From: Pedro Fontana Date: Fri, 2 Jun 2023 19:51:31 -0300 Subject: [PATCH 1/7] Fix bug --- .../builtin_hint_processor/uint256_utils.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/hint_processor/builtin_hint_processor/uint256_utils.rs b/src/hint_processor/builtin_hint_processor/uint256_utils.rs index ad92a1a05d..10773e9e66 100644 --- a/src/hint_processor/builtin_hint_processor/uint256_utils.rs +++ b/src/hint_processor/builtin_hint_processor/uint256_utils.rs @@ -376,14 +376,14 @@ pub fn uint256_offseted_unsigned_div_rem( //ids.remainder.low = remainder & ((1 << 128) - 1) //ids.remainder.high = remainder >> 128 - let a = (a_high << 128_u32) + a_low; - let div = (div_high << 128_u32) + div_low; + let a = (a_high.to_biguint() << 128_u32) + a_low.to_biguint(); + let div = (div_high.to_biguint() << 128_u32) + div_low.to_biguint(); //a and div will always be positive numbers //Then, Rust div_rem equals Python divmod let (quotient, remainder) = div_rem(a, div); - let quotient = Uint256::from(quotient); - let remainder = Uint256::from(remainder); + let quotient = Uint256::from(Felt252::from(quotient)); + let remainder = Uint256::from(Felt252::from(remainder)); quotient.insert_from_var_name("quotient", vm, ids_data, ap_tracking)?; remainder.insert_from_var_name("remainder", vm, ids_data, ap_tracking)?; From d15a994b733511ea0bd54248bcbf745e45c22703 Mon Sep 17 00:00:00 2001 From: Pedro Fontana Date: Mon, 5 Jun 2023 18:05:12 -0300 Subject: [PATCH 2/7] Fix U256 inserts --- src/hint_processor/builtin_hint_processor/uint256_utils.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/hint_processor/builtin_hint_processor/uint256_utils.rs b/src/hint_processor/builtin_hint_processor/uint256_utils.rs index 10773e9e66..4cd4f9c5fb 100644 --- a/src/hint_processor/builtin_hint_processor/uint256_utils.rs +++ b/src/hint_processor/builtin_hint_processor/uint256_utils.rs @@ -67,6 +67,8 @@ impl<'a> Uint256<'a> { ap_tracking: &ApTracking, ) -> Result<(), HintError> { let addr = get_relocatable_from_var_name(var_name, vm, ids_data, ap_tracking)?; + dbg!(&self.low); + dbg!(&self.high); vm.insert_value(addr, self.low.into_owned())?; vm.insert_value((addr + 1)?, self.high.into_owned())?; @@ -382,8 +384,8 @@ pub fn uint256_offseted_unsigned_div_rem( //Then, Rust div_rem equals Python divmod let (quotient, remainder) = div_rem(a, div); - let quotient = Uint256::from(Felt252::from(quotient)); - let remainder = Uint256::from(Felt252::from(remainder)); + let quotient = Uint256::from("ient); + let remainder = Uint256::from(&remainder); quotient.insert_from_var_name("quotient", vm, ids_data, ap_tracking)?; remainder.insert_from_var_name("remainder", vm, ids_data, ap_tracking)?; From 3089a1ba5fa8584338a95c1ca044d3d362b25d1f Mon Sep 17 00:00:00 2001 From: Pedro Fontana Date: Mon, 5 Jun 2023 18:07:35 -0300 Subject: [PATCH 3/7] Add integration test --- cairo_programs/uint256.cairo | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/cairo_programs/uint256.cairo b/cairo_programs/uint256.cairo index 4a1e6a2e9d..b866b097a9 100644 --- a/cairo_programs/uint256.cairo +++ b/cairo_programs/uint256.cairo @@ -58,6 +58,14 @@ func main{range_check_ptr: felt}() { assert b_quotient = Uint256(1, 0); assert b_remainder = Uint256(340282366920938463463374607431768211377, 0); + let (c_quotient, c_remainder) = uint256_unsigned_div_rem( + Uint256(340282366920938463463374607431768211455, 340282366920938463463374607431768211455), + Uint256(1, 0), + ); + + assert c_quotient = Uint256(340282366920938463463374607431768211455, 340282366920938463463374607431768211455); + assert c_remainder = Uint256(0, 0); + let (a_quotient_low, a_quotient_high, a_remainder) = uint256_mul_div_mod( Uint256(89, 72), Uint256(3, 7), From b89fcb0973e4dbf0d28fe84dc0d2403574fd67eb Mon Sep 17 00:00:00 2001 From: Pedro Fontana Date: Mon, 5 Jun 2023 18:18:15 -0300 Subject: [PATCH 4/7] Remove dbg --- src/hint_processor/builtin_hint_processor/uint256_utils.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/hint_processor/builtin_hint_processor/uint256_utils.rs b/src/hint_processor/builtin_hint_processor/uint256_utils.rs index 4cd4f9c5fb..246c837e9a 100644 --- a/src/hint_processor/builtin_hint_processor/uint256_utils.rs +++ b/src/hint_processor/builtin_hint_processor/uint256_utils.rs @@ -67,8 +67,6 @@ impl<'a> Uint256<'a> { ap_tracking: &ApTracking, ) -> Result<(), HintError> { let addr = get_relocatable_from_var_name(var_name, vm, ids_data, ap_tracking)?; - dbg!(&self.low); - dbg!(&self.high); vm.insert_value(addr, self.low.into_owned())?; vm.insert_value((addr + 1)?, self.high.into_owned())?; From e1b23d4d6e77be11da73424f4b7b799bcea67152 Mon Sep 17 00:00:00 2001 From: Pedro Fontana Date: Mon, 5 Jun 2023 18:22:25 -0300 Subject: [PATCH 5/7] Update CHANGELOG.md --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 30eab4396c..16a54d207c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,10 @@ ## Cairo-VM Changelog #### Upcoming Changes +* fix: Fix hints `UINT256_UNSIGNED_DIV_REM` && `UINT256_EXPANDED_UNSIGNED_DIV_REM` [#1203](https://github.com/lambdaclass/cairo-rs/pull/1203) * feat: implement `mem_eq` function to test for equality of two ranges in memory [#1198](https://github.com/lambdaclass/cairo-rs/pull/1198) + * perf: use `mem_eq` in `set_add` [#1198](https://github.com/lambdaclass/cairo-rs/pull/1198) * feat: wrap big variants of `HintError`, `VirtualMachineError`, `RunnerError`, `MemoryError`, `MathError`, `InsufficientAllocatedCellsError` in `Box` [#1193](https://github.com/lambdaclass/cairo-rs/pull/1193) From 837be9a69765d10ae157060defeeb5a4b83b2d7c Mon Sep 17 00:00:00 2001 From: Pedro Fontana Date: Mon, 5 Jun 2023 18:28:08 -0300 Subject: [PATCH 6/7] Add UINT256_EXPANDED_UNSIGNED_DIV_REM test --- cairo_programs/uint256_improvements.cairo | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/cairo_programs/uint256_improvements.cairo b/cairo_programs/uint256_improvements.cairo index bba75d57dc..b21df0ad9b 100644 --- a/cairo_programs/uint256_improvements.cairo +++ b/cairo_programs/uint256_improvements.cairo @@ -314,6 +314,16 @@ func test_udiv_expanded{range_check_ptr}() { ); assert b_quotient = Uint256(1, 0); assert b_remainder = Uint256(340282366920938463463374607431768211377, 0); + + let (c_div_expanded) = uint256_expand(Uint256(1, 0)); + + let (c_quotient, c_remainder) = uint256_unsigned_div_rem_expanded( + Uint256(340282366920938463463374607431768211455, 340282366920938463463374607431768211455), + c_div_expanded, + ); + + assert c_quotient = Uint256(340282366920938463463374607431768211455, 340282366920938463463374607431768211455); + assert c_remainder = Uint256(0, 0); return (); } From 99a2445ffe13a31f0dc64a999b56d69aac305937 Mon Sep 17 00:00:00 2001 From: Pedro Fontana Date: Mon, 5 Jun 2023 19:31:22 -0300 Subject: [PATCH 7/7] Fix memeory holes --- src/tests/cairo_run_test.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/tests/cairo_run_test.rs b/src/tests/cairo_run_test.rs index 831dd734a5..6cc0267187 100644 --- a/src/tests/cairo_run_test.rs +++ b/src/tests/cairo_run_test.rs @@ -334,7 +334,7 @@ fn dict_update() { #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)] fn uint256() { let program_data = include_bytes!("../../cairo_programs/uint256.json"); - run_program_simple_with_memory_holes(program_data.as_slice(), 3514); + run_program_simple_with_memory_holes(program_data.as_slice(), 3534); } #[test] @@ -825,7 +825,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(), 108); + run_program_simple_with_memory_holes(program_data.as_slice(), 128); } #[test]