From 79b0964612777701bb15952a1ff60db8630c74d7 Mon Sep 17 00:00:00 2001 From: Fabian Albert Date: Fri, 8 Mar 2024 11:29:20 +0100 Subject: [PATCH] 32bit fix attempt --- .../curve448_utils/curve448_scalar.cpp | 33 ++++++++++--------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/src/lib/pubkey/curve448/curve448_utils/curve448_scalar.cpp b/src/lib/pubkey/curve448/curve448_utils/curve448_scalar.cpp index 49304952320..719b642741d 100644 --- a/src/lib/pubkey/curve448/curve448_utils/curve448_scalar.cpp +++ b/src/lib/pubkey/curve448/curve448_utils/curve448_scalar.cpp @@ -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 -std::pair, std::array> div_mod_2_446(std::span x) { - static_assert(S >= WORDS_446, "Input too small"); - std::array r; - for(size_t i = 0; i < WORDS_446; ++i) { - r[i] = x[i]; +auto div_mod_2_446(std::span x) { + if constexpr(S < WORDS_446) { + std::array r = {0}; + copy_mem(std::span(r).template first(), x); + return std::make_pair(std::array({0}), r); + } else { + std::array r; + copy_mem(r, std::span(x).template first()); + // 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 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 q; - bigint_shr2(q.data(), x.data(), x.size(), word_shift, bit_shift); - - return {q, r}; } template