From ae87c315396e7456fb847f9f739254c4801101d2 Mon Sep 17 00:00:00 2001 From: Pedro Fontana Date: Mon, 5 Jun 2023 19:58:50 -0300 Subject: [PATCH] Fix Uint256 unsigned div rem Hints (#1203) * Fix bug * Fix U256 inserts * Add integration test * Remove dbg * Update CHANGELOG.md * Add UINT256_EXPANDED_UNSIGNED_DIV_REM test * Fix memeory holes --- CHANGELOG.md | 2 ++ cairo_programs/uint256.cairo | 8 ++++++++ cairo_programs/uint256_improvements.cairo | 10 ++++++++++ .../builtin_hint_processor/uint256_utils.rs | 8 ++++---- src/tests/cairo_run_test.rs | 4 ++-- 5 files changed, 26 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fe809d3269..119cff2e95 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,10 +1,12 @@ ## 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) * bugfix: Fix deserialization of scientific notation with fractional values [#1202](https://github.com/lambdaclass/cairo-rs/pull/1202) * 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) 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), 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 (); } diff --git a/src/hint_processor/builtin_hint_processor/uint256_utils.rs b/src/hint_processor/builtin_hint_processor/uint256_utils.rs index ad92a1a05d..246c837e9a 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("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)?; 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]