Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Uint256 unsigned div rem Hints #1203

Merged
merged 8 commits into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
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