-
Notifications
You must be signed in to change notification settings - Fork 573
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add specialized reduction for P256 in pcurves
For 32-bit x86, this reduction results in point arithmetic operations that are 25-35% faster than when using Montgomery. Sadly for 64-bit x86 it is at best about even with using Montgomery, and for Clang 64-bit it's even somewhat slower.
- Loading branch information
Showing
3 changed files
with
178 additions
and
4 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
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,65 @@ | ||
/* | ||
* (C) 2024 Jack Lloyd | ||
* | ||
* Botan is released under the Simplified BSD License (see license.txt) | ||
*/ | ||
|
||
#ifndef BOTAN_PCURVES_NIST_REDC_HELPER_H_ | ||
#define BOTAN_PCURVES_NIST_REDC_HELPER_H_ | ||
|
||
#include <botan/internal/mp_core.h> | ||
|
||
namespace Botan { | ||
|
||
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 SumAccum { | ||
public: | ||
static_assert(WordInfo<W>::bits == 32 || WordInfo<W>::bits == 64); | ||
|
||
static constexpr size_t N32 = N * (WordInfo<W>::bits / 32); | ||
|
||
SumAccum(std::array<W, N>& r) : m_r(r), m_S(0), m_idx(0) {} | ||
|
||
void accum(int64_t v) { | ||
BOTAN_STATE_CHECK(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_STATE_CHECK(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 |
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