-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: impl invModPUint512 hint (#599)
* feat: impl invModPUint512 hint * fix: code improvements + fix pack func * fix: inline split fn + fix integration tests * fix: loop optimization
- Loading branch information
1 parent
1259324
commit 8dc6682
Showing
8 changed files
with
322 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
integration_tests/cairo_zero_hint_tests/inv_mod_p_uint512.small.cairo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// The content of this file has been borrowed from LambdaClass Cairo VM in Rust | ||
// See https://raw.githubusercontent.com/lambdaclass/cairo-vm/db7fff18c9c1312024ebf4119fcdfeba23657b12/cairo_programs/inv_mod_p_uint512.cairo | ||
|
||
%builtins range_check | ||
|
||
from starkware.cairo.common.uint256 import Uint256 | ||
|
||
const P_low = 201385395114098847380338600778089168199; | ||
const P_high = 64323764613183177041862057485226039389; | ||
|
||
struct Uint512 { | ||
d0: felt, | ||
d1: felt, | ||
d2: felt, | ||
d3: felt, | ||
} | ||
|
||
func inv_mod_p_uint512{range_check_ptr}(x: Uint512) -> Uint256 { | ||
alloc_locals; | ||
local x_inverse_mod_p: Uint256; | ||
local p: Uint256 = Uint256(P_low, P_high); | ||
// To whitelist | ||
%{ | ||
def pack_512(u, num_bits_shift: int) -> int: | ||
limbs = (u.d0, u.d1, u.d2, u.d3) | ||
return sum(limb << (num_bits_shift * i) for i, limb in enumerate(limbs)) | ||
x = pack_512(ids.x, num_bits_shift = 128) | ||
p = ids.p.low + (ids.p.high << 128) | ||
x_inverse_mod_p = pow(x,-1, p) | ||
x_inverse_mod_p_split = (x_inverse_mod_p & ((1 << 128) - 1), x_inverse_mod_p >> 128) | ||
ids.x_inverse_mod_p.low = x_inverse_mod_p_split[0] | ||
ids.x_inverse_mod_p.high = x_inverse_mod_p_split[1] | ||
%} | ||
|
||
return x_inverse_mod_p; | ||
} | ||
|
||
func main{range_check_ptr: felt}() { | ||
let x = Uint512(101, 2, 15, 61); | ||
let y = inv_mod_p_uint512(x); | ||
assert y = Uint256(80275402838848031859800366538378848249, 5810892639608724280512701676461676039); | ||
return (); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.