Skip to content

Commit

Permalink
32bit fix attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
FAlbertDev committed Mar 8, 2024
1 parent f21cb0c commit 79b0964
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions src/lib/pubkey/curve448/curve448_utils/curve448_scalar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,25 @@ constexpr size_t WORDS_C = words_for_bits(28 * 8);

/// @return (q,r) so that x = q*2^446 + r, r < L
template <size_t S>
std::pair<std::array<word, S - WORDS_446 + 1>, std::array<word, WORDS_446>> div_mod_2_446(std::span<const word, S> x) {
static_assert(S >= WORDS_446, "Input too small");
std::array<word, WORDS_446> r;
for(size_t i = 0; i < WORDS_446; ++i) {
r[i] = x[i];
auto div_mod_2_446(std::span<const word, S> x) {
if constexpr(S < WORDS_446) {
std::array<word, WORDS_446> r = {0};
copy_mem(std::span(r).template first<S>(), x);
return std::make_pair(std::array<word, 1>({0}), r);
} else {
std::array<word, WORDS_446> r;
copy_mem(r, std::span(x).template first<WORDS_446>());
// Clear the two most significant bits
r[WORDS_446 - 1] &= ~(word(0b11) << (sizeof(word) * 8 - 2));

constexpr size_t word_shift = 446 / (sizeof(word) * 8);
constexpr size_t bit_shift = 446 % (sizeof(word) * 8);

std::array<word, S - WORDS_446 + 1> q;
bigint_shr2(q.data(), x.data(), x.size(), word_shift, bit_shift);

return std::make_pair(q, r);
}
// Clear the two most significant bits
r[WORDS_446 - 1] &= ~(word(0b11) << (sizeof(word) * 8 - 2));

constexpr size_t word_shift = 446 / (sizeof(word) * 8);
constexpr size_t bit_shift = 446 % (sizeof(word) * 8);

std::array<word, S - WORDS_446 + 1> q;
bigint_shr2(q.data(), x.data(), x.size(), word_shift, bit_shift);

return {q, r};
}

template <size_t S>
Expand Down

0 comments on commit 79b0964

Please sign in to comment.