From 89d0d1b95be1cd8f8853d714872f2c7e89518b80 Mon Sep 17 00:00:00 2001 From: Jack Lloyd Date: Thu, 11 Apr 2024 20:05:22 -0400 Subject: [PATCH] Add constexpr shift_right mp helper --- src/lib/math/mp/mp_core.h | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/lib/math/mp/mp_core.h b/src/lib/math/mp/mp_core.h index 27307767579..eef1e985ef0 100644 --- a/src/lib/math/mp/mp_core.h +++ b/src/lib/math/mp/mp_core.h @@ -845,15 +845,29 @@ inline constexpr W shift_left(std::array& x) { static_assert(S < WordInfo::bits, "Shift too large"); W carry = 0; - for(size_t j = 0; j != N; ++j) { - const W w = x[j]; - x[j] = (w << S) | carry; + for(size_t i = 0; i != N; ++i) { + const W w = x[i]; + x[i] = (w << S) | carry; carry = w >> (WordInfo::bits - S); } return carry; } +template +inline consteval W shift_right(std::array& x) { + static_assert(S < WordInfo::bits, "Shift too large"); + + W carry = 0; + for(size_t i = 0; i != N; ++i) { + const W w = x[N - 1 - i]; + x[N - 1 - i] = (w >> S) | carry; + carry = w << (WordInfo::bits - S); + } + + return carry; +} + template consteval auto hex_to_words(const char (&s)[N]) { // Char count includes null terminator which we ignore