-
Notifications
You must be signed in to change notification settings - Fork 21
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
Use extended Euclidean algorithm for modular inverse #7
Closed
Closed
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e353dce
Use extended Euclidean algorithm for modular inverse
swenson 9aff754
Address @alex's comments:
swenson 4a91e6c
q is never 0, so don't need to check
swenson 44c90ac
Remove comment about quadratic arithmetic in Python
swenson 714b598
Change back to scalar division
swenson 3efca04
Delete pow2
swenson 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 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 |
---|---|---|
|
@@ -39,22 +39,20 @@ def pow2(x, p): | |
p -= 1 | ||
return x | ||
|
||
|
||
def inv(z): | ||
"""$= z^{-1} \mod q$, for z != 0""" | ||
# Adapted from curve25519_athlon.c in djb's Curve25519. | ||
z2 = z * z % q # 2 | ||
z9 = pow2(z2, 2) * z % q # 9 | ||
z11 = z9 * z2 % q # 11 | ||
z2_5_0 = (z11 * z11) % q * z9 % q # 31 == 2^5 - 2^0 | ||
z2_10_0 = pow2(z2_5_0, 5) * z2_5_0 % q # 2^10 - 2^0 | ||
z2_20_0 = pow2(z2_10_0, 10) * z2_10_0 % q # ... | ||
z2_40_0 = pow2(z2_20_0, 20) * z2_20_0 % q | ||
z2_50_0 = pow2(z2_40_0, 10) * z2_10_0 % q | ||
z2_100_0 = pow2(z2_50_0, 50) * z2_50_0 % q | ||
z2_200_0 = pow2(z2_100_0, 100) * z2_100_0 % q | ||
z2_250_0 = pow2(z2_200_0, 50) * z2_50_0 % q # 2^250 - 2^0 | ||
return pow2(z2_250_0, 5) * z11 % q # 2^255 - 2^5 + 11 = q - 2 | ||
""" | ||
Use the extended Euclidean algorithm to find the inverse mod q. | ||
|
||
See, for example, H. Cohen, A Course in Computational Algebraic Number | ||
Theory, Algorithm 1.3.6 | ||
""" | ||
d, div = z, q | ||
u = 1 | ||
r = 0 | ||
while div != 0: | ||
(qq, div), d = divmod(d, div), div | ||
r, u = u - qq * r, r | ||
return u | ||
|
||
|
||
d = -121665 * inv(121666) | ||
|
@@ -92,7 +90,7 @@ def scalarmult(P, e): | |
if e == 0: | ||
return (0, 1) | ||
|
||
Q = scalarmult(P, e // 2) | ||
Q = scalarmult(P, e / 2) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why this change? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure how that wound up in there. I'll change it back. |
||
Q = edwards(Q, Q) | ||
|
||
if e & 1: | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pow2
can be deleted now