-
Notifications
You must be signed in to change notification settings - Fork 569
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
Faster pcurves reductions for P-256 and P-384 #4147
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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 |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* (C) 2024 Jack Lloyd | ||
* | ||
* Botan is released under the Simplified BSD License (see license.txt) | ||
*/ | ||
|
||
#ifndef BOTAN_PCURVES_SOLINAS_REDC_HELPER_H_ | ||
#define BOTAN_PCURVES_SOLINAS_REDC_HELPER_H_ | ||
|
||
#include <botan/internal/mp_core.h> | ||
|
||
namespace Botan { | ||
|
||
/* | ||
Helpers for modular reduction of Solinas primes, such as P-256 and P-384. | ||
|
||
Instead of explicitly forming the various integers and adding/subtracting them | ||
row-by-row, we compute the entire sum in one pass, column by column. To prevent | ||
overflow/underflow the accumulator is a signed 64-bit integer, while the various | ||
limbs are (at least for all NIST curves aside from P-192) 32 bit integers. | ||
|
||
For more background on Solinas primes / Solinas reduction see | ||
|
||
* J. Solinas 'Generalized Mersenne Numbers' | ||
<https://cacr.uwaterloo.ca/techreports/1999/corr99-39.pdf> | ||
* NIST SP 800-186 Appendix G.1 | ||
<https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-186.pdf> | ||
* Handbook of Elliptic And Hyperelliptic Curve Cryptography § 10.4.3 | ||
|
||
*/ | ||
|
||
template <WordType W> | ||
constexpr uint32_t get_uint32(const W xw[], size_t i) { | ||
static_assert(WordInfo<W>::bits == 32 || WordInfo<W>::bits == 64); | ||
|
||
if constexpr(WordInfo<W>::bits == 32) { | ||
return xw[i]; | ||
} else { | ||
return static_cast<uint32_t>(xw[i / 2] >> ((i % 2) * 32)); | ||
} | ||
} | ||
|
||
template <WordType W, size_t N> | ||
class SolinasAccum { | ||
public: | ||
static_assert(WordInfo<W>::bits == 32 || WordInfo<W>::bits == 64); | ||
|
||
static constexpr size_t N32 = N * (WordInfo<W>::bits / 32); | ||
|
||
SolinasAccum(std::array<W, N>& r) : m_r(r), m_S(0), m_idx(0) {} | ||
|
||
void accum(int64_t v) { | ||
BOTAN_DEBUG_ASSERT(m_idx < N32); | ||
|
||
m_S += v; | ||
const uint32_t r = static_cast<uint32_t>(m_S); | ||
m_S >>= 32; | ||
|
||
if constexpr(WordInfo<W>::bits == 32) { | ||
m_r[m_idx] = r; | ||
} else { | ||
m_r[m_idx / 2] |= static_cast<uint64_t>(r) << (32 * (m_idx % 2)); | ||
} | ||
|
||
m_idx += 1; | ||
} | ||
|
||
W final_carry(int64_t C) { | ||
BOTAN_DEBUG_ASSERT(m_idx == N32); | ||
m_S += C; | ||
BOTAN_DEBUG_ASSERT(m_S >= 0); | ||
return static_cast<W>(m_S); | ||
} | ||
|
||
private: | ||
std::array<W, N>& m_r; | ||
int64_t m_S; | ||
size_t m_idx; | ||
}; | ||
|
||
} // namespace Botan | ||
|
||
#endif |
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.
If you decide to adopt my suggestion, it would be worthwhile to replace this by a constexpr multiplication (
bigmul(4, Params::P)
) to save a magic string/number, if we have that somewhere already.