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

uint256_mod_inv standard function with a hint #144

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all 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
16 changes: 16 additions & 0 deletions src/starkware/cairo/common/uint256.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,22 @@ func uint256_pow2{range_check_ptr}(exp: Uint256) -> (res: Uint256) {
}
}

// Computes modular inversion a^{-1} % div.
func uint256_mod_inv{range_check_ptr: felt}(a: Uint256, div: Uint256) -> (res: Uint256) {
alloc_locals;
local res: Uint256;
%{
a = (ids.a.high << 128) + ids.a.low
div = (ids.div.high << 128) + ids.div.low
res = pow(a, -1, div)
ids.res.low = res & ((1 << 128) - 1)
ids.res.high = res >> 128
%}
let (quotient_low, quotient_high, remainder) = uint256_mul_div_mod(a,res,div);
assert Uint256(low=1,high=0) = (remainder);
return (res=res);
}

// Computes the logical left shift of a uint256 integer.
func uint256_shl{range_check_ptr}(a: Uint256, b: Uint256) -> (res: Uint256) {
let (c) = uint256_pow2(b);
Expand Down