-
Notifications
You must be signed in to change notification settings - Fork 574
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
Add fast iterated point doubling #4221
Merged
Merged
Changes from all commits
Commits
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
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 |
---|---|---|
|
@@ -176,7 +176,7 @@ class IntMod final { | |
return Self(Rep::redc(z)); | ||
} | ||
|
||
void square_n(size_t n) { | ||
constexpr void square_n(size_t n) { | ||
std::array<W, 2 * N> z; | ||
for(size_t i = 0; i != n; ++i) { | ||
comba_sqr<N>(z.data(), this->data()); | ||
|
@@ -698,14 +698,52 @@ class ProjectiveCurvePoint { | |
} | ||
|
||
constexpr Self dbl_n(size_t n) const { | ||
// TODO it is possible to optimize this by carrying over values from | ||
// the previous iteration into the next | ||
if constexpr(Self::A_is_minus_3) { | ||
/* | ||
Repeated doubling using an adaptation of Algorithm 3.23 in | ||
"Guide To Elliptic Curve Cryptography" | ||
Hankerson, Menezes, Vanstone | ||
|
||
Self pt = (*this); | ||
for(size_t i = 0; i != n; ++i) { | ||
pt = pt.dbl(); | ||
For A == -3 | ||
Cost: 2S + 1*2 + n*(4S + 4M + 2*2 + 1*3 + 4A) | ||
Naive doubling | ||
Cost: n*(4S + 4M + 2*2 + 1*3 + 5A + 1*4 + 1*8) | ||
|
||
TODO adapt this for A == 0 and/or generic A cases | ||
*/ | ||
|
||
// The inverse of 2 modulo P is (P/2)+1; this avoids a constexpr time | ||
// general inversion, which some compilers can't handle | ||
constexpr auto INV_2 = FieldElement::from_words(p_div_2_plus_1(Params::PW)); | ||
|
||
auto nx = x(); | ||
auto ny = y(); | ||
auto nz = z(); | ||
ny = ny.mul2(); | ||
auto w = nz.square().square(); | ||
Comment on lines
+719
to
+723
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. Does it make sense to early-return on 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 really in practice - we call this with |
||
|
||
while(n > 0) { | ||
const auto ny2 = ny.square(); | ||
const auto ny4 = ny2.square(); | ||
const auto t1 = (nx.square() - w).mul3(); | ||
const auto t2 = nx * ny2; | ||
nx = t1.square() - t2.mul2(); | ||
nz *= ny; | ||
ny = t1 * (t2 - nx).mul2() - ny4; | ||
n--; | ||
if(n > 0) { | ||
w *= ny4; | ||
} | ||
} | ||
ny *= INV_2; | ||
return Self(nx, ny, nz); | ||
} else { | ||
Self pt = (*this); | ||
for(size_t i = 0; i != n; ++i) { | ||
pt = pt.dbl(); | ||
} | ||
return pt; | ||
} | ||
return pt; | ||
} | ||
|
||
constexpr Self dbl() const { | ||
|
@@ -718,7 +756,7 @@ class ProjectiveCurvePoint { | |
if a == -3 then | ||
3*x^2 + a*z^4 == 3*x^2 - 3*z^4 == 3*(x^2-z^4) == 3*(x-z^2)*(x+z^2) | ||
|
||
Cost: 2M + 2A + 1*3 | ||
Cost: 1M + 1S + 2A + 1*3 | ||
*/ | ||
const auto z2 = z().square(); | ||
m = (x() - z2).mul3() * (x() + z2); | ||
|
@@ -731,7 +769,6 @@ class ProjectiveCurvePoint { | |
const auto z2 = z().square(); | ||
m = x().square().mul3() + A * z2.square(); | ||
} | ||
|
||
const auto y2 = y().square(); | ||
const auto s = x().mul4() * y2; | ||
const auto nx = m.square() - s.mul2(); | ||
|
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.
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.
In case you want to express this as a good-old function overload, I think you could do this:
... to avoid the
if constexpr
spanning the entirety of the function. No strong opinion on what is more idiomatic from my side. But, I generally think, the less indentation the better.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.
I expect in the future this will expand to cover other types of curves (A == 0 and/or generic A) in which case the single function will be cleaner.