-
Notifications
You must be signed in to change notification settings - Fork 190
Newhint#17 & Newhint#18 imports of SECP256R1_ALPHA and SECP256R1_N #1026
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
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
2a3314e
WIP newhint#17 and newhint#18
mfachal af18fc9
WIP cairo program
mfachal 6f52d05
add secp256r1 testing programs and newhint#17 and #18
mfachal 528a706
Merge branch 'main' into newhint17-newhint18-imports
mfachal 591fde7
update changelog
mfachal 25fd1df
remove program using unimplemented hint
mfachal c2f3dad
Merge branch 'main' into newhint17-newhint18-imports
mfachal eae84e3
Merge branch 'main' into newhint17-newhint18-imports
mfachal 16fd07e
add correct constants to test program
mfachal 3aff500
Merge branch 'newhint17-newhint18-imports' of mfachal:lambdaclass/cai…
mfachal 45c8ee1
Merge branch 'main' into newhint17-newhint18-imports
mfachal 51af1c3
fix broken test when executing hint #25 with different context
mfachal bb7d059
Merge branch 'main' of mfachal:lambdaclass/cairo-rs
mfachal 7dd4ce3
Merge branch 'main' into newhint17-newhint18-imports
mfachal 03926eb
Update CHANGELOG.md
mfachal 88992db
Merge branch 'main' into newhint17-newhint18-imports
mfachal 736db30
Merge branch 'main' into newhint17-newhint18-imports
mfachal 927b9da
merge
mfachal 4cf1757
add unit test for import alpha
mfachal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,75 @@ | ||
%builtins range_check | ||
|
||
// Sources: https://github.com/myBraavos/efficient-secp256r1/blob/main/src/secp256r1/signature.cairo#L48 | ||
// Sources: https://github.com/myBraavos/efficient-secp256r1/blob/main/src/secp256r1/ec.cairo#L32 | ||
|
||
from starkware.cairo.common.cairo_secp.bigint import BigInt3, nondet_bigint3, BASE, bigint_mul | ||
|
||
// N = 0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551 | ||
const N0 = 0x179e84f3b9cac2fc632551; | ||
const N1 = 0x3ffffffffffef39beab69c; | ||
const N2 = 0xffffffff00000000fffff; | ||
|
||
func div_mod_n{range_check_ptr}(a: BigInt3, b: BigInt3) -> (res: BigInt3) { | ||
%{ from starkware.cairo.common.cairo_secp.secp256r1_utils import SECP256R1_N as N %} | ||
// Hint 24 | ||
%{ | ||
from starkware.cairo.common.cairo_secp.secp_utils import pack | ||
from starkware.python.math_utils import div_mod, safe_div | ||
|
||
a = pack(ids.a, PRIME) | ||
b = pack(ids.b, PRIME) | ||
value = res = div_mod(a, b, N) | ||
%} | ||
let (res) = nondet_bigint3(); | ||
// Hint 25 | ||
%{ | ||
value = k_plus_one = safe_div(res * b - a, N) + 1 | ||
%} | ||
let (k_plus_one) = nondet_bigint3(); | ||
let k = BigInt3(d0=k_plus_one.d0 - 1, d1=k_plus_one.d1, d2=k_plus_one.d2); | ||
|
||
let (res_b) = bigint_mul(res, b); | ||
let n = BigInt3(N0, N1, N2); | ||
let (k_n) = bigint_mul(k, n); | ||
|
||
// We should now have res_b = k_n + a. Since the numbers are in unreduced form, | ||
// we should handle the carry. | ||
|
||
tempvar carry1 = (res_b.d0 - k_n.d0 - a.d0) / BASE; | ||
assert [range_check_ptr + 0] = carry1 + 2 ** 127; | ||
|
||
tempvar carry2 = (res_b.d1 - k_n.d1 - a.d1 + carry1) / BASE; | ||
assert [range_check_ptr + 1] = carry2 + 2 ** 127; | ||
|
||
tempvar carry3 = (res_b.d2 - k_n.d2 - a.d2 + carry2) / BASE; | ||
assert [range_check_ptr + 2] = carry3 + 2 ** 127; | ||
|
||
tempvar carry4 = (res_b.d3 - k_n.d3 + carry3) / BASE; | ||
assert [range_check_ptr + 3] = carry4 + 2 ** 127; | ||
|
||
assert res_b.d4 - k_n.d4 + carry4 = 0; | ||
|
||
let range_check_ptr = range_check_ptr + 4; | ||
|
||
return (res=res); | ||
} | ||
|
||
func test_div_mod_n{range_check_ptr: felt}() { | ||
let a: BigInt3 = BigInt3(100, 99, 98); | ||
let b: BigInt3 = BigInt3(10, 9, 8); | ||
|
||
let (res) = div_mod_n(a, b); | ||
|
||
assert res = BigInt3( | ||
17710125265123803206911742, 47938808641831879622633720, 16714845192957993827873659 | ||
); | ||
|
||
return (); | ||
} | ||
|
||
func main{range_check_ptr: felt}() { | ||
test_div_mod_n(); | ||
|
||
return (); | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.