Skip to content

Commit

Permalink
Fix Uint256 unsigned div rem Hints (#1203)
Browse files Browse the repository at this point in the history
* Fix bug

* Fix U256 inserts

* Add integration test

* Remove dbg

* Update CHANGELOG.md

* Add UINT256_EXPANDED_UNSIGNED_DIV_REM test

* Fix memeory holes
  • Loading branch information
pefontana authored Jun 5, 2023
1 parent fc09e20 commit ae87c31
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
8 changes: 8 additions & 0 deletions cairo_programs/uint256.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
10 changes: 10 additions & 0 deletions cairo_programs/uint256_improvements.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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 ();
}

Expand Down
8 changes: 4 additions & 4 deletions src/hint_processor/builtin_hint_processor/uint256_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(&quotient);
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)?;
Expand Down
4 changes: 2 additions & 2 deletions src/tests/cairo_run_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit ae87c31

Please sign in to comment.