10#ifndef BOTAN_MP_CORE_OPS_H_
11#define BOTAN_MP_CORE_OPS_H_
13#include <botan/assert.h>
14#include <botan/types.h>
15#include <botan/internal/ct_utils.h>
16#include <botan/internal/mem_utils.h>
17#include <botan/internal/mp_asmi.h>
32 for(
size_t i = 0; i != size; ++i) {
35 x[i] = mask.select(b, a);
36 y[i] = mask.select(a, b);
50 for(
size_t i = 0; i != size; ++i) {
54 return (mask &
carry);
62inline constexpr auto bigint_cnd_sub(W cnd, W x[],
const W y[],
size_t size) -> W {
67 for(
size_t i = 0; i != size; ++i) {
71 return (mask &
carry);
83 W
carry = mask.if_set_return(1);
84 for(
size_t i = 0; i != size; ++i) {
86 x[i] = mask.select(z, x[i]);
94inline constexpr auto bigint_add2(W x[],
size_t x_size,
const W y[],
size_t y_size) -> W {
99 const size_t blocks = y_size - (y_size % 8);
101 for(
size_t i = 0; i != blocks; i += 8) {
105 for(
size_t i = blocks; i != y_size; ++i) {
109 for(
size_t i = y_size; i != x_size; ++i) {
120inline constexpr auto bigint_add3(W z[],
const W x[],
size_t x_size,
const W y[],
size_t y_size) -> W {
121 if(x_size < y_size) {
127 const size_t blocks = y_size - (y_size % 8);
129 for(
size_t i = 0; i != blocks; i += 8) {
133 for(
size_t i = blocks; i != y_size; ++i) {
137 for(
size_t i = y_size; i != x_size; ++i) {
148inline constexpr auto bigint_sub2(W x[],
size_t x_size,
const W y[],
size_t y_size) -> W {
153 const size_t blocks = y_size - (y_size % 8);
155 for(
size_t i = 0; i != blocks; i += 8) {
159 for(
size_t i = blocks; i != y_size; ++i) {
160 x[i] =
word_sub(x[i], y[i], &borrow);
163 for(
size_t i = y_size; i != x_size; ++i) {
164 x[i] =
word_sub(x[i],
static_cast<W
>(0), &borrow);
177 for(
size_t i = 0; i != y_size; ++i) {
178 x[i] =
word_sub(y[i], x[i], &borrow);
192inline constexpr auto bigint_sub3(W z[],
const W x[],
size_t x_size,
const W y[],
size_t y_size) -> W {
197 const size_t blocks = y_size - (y_size % 8);
199 for(
size_t i = 0; i != blocks; i += 8) {
200 borrow =
word8_sub3(z + i, x + i, y + i, borrow);
203 for(
size_t i = blocks; i != y_size; ++i) {
204 z[i] =
word_sub(x[i], y[i], &borrow);
207 for(
size_t i = y_size; i != x_size; ++i) {
208 z[i] =
word_sub(x[i],
static_cast<W
>(0), &borrow);
228 const size_t blocks = N - (N % 8);
230 for(
size_t i = 0; i != blocks; i += 8) {
231 borrow =
word8_sub3(z + i, x + i, p + i, borrow);
234 for(
size_t i = blocks; i != N; ++i) {
235 z[i] =
word_sub(x[i], p[i], &borrow);
238 borrow = (x0 - borrow) > x0;
253template <
size_t N, WordType W>
257 for(
size_t i = 0; i != N; ++i) {
258 z[i] =
word_sub(x[i], y[i], &borrow);
261 borrow = (x0 - borrow) > x0;
288 const size_t blocks = N - (N % 8);
290 for(
size_t i = 0; i != blocks; i += 8) {
291 borrow0 =
word8_sub3(ws0 + i, x + i, y + i, borrow0);
292 borrow1 =
word8_sub3(ws1 + i, y + i, x + i, borrow1);
295 for(
size_t i = blocks; i != N; ++i) {
296 ws0[i] =
word_sub(x[i], y[i], &borrow0);
297 ws1[i] =
word_sub(y[i], x[i], &borrow1);
307inline constexpr void bigint_shl1(W x[],
size_t x_size,
size_t x_words,
size_t shift) {
318 for(
size_t i = word_shift; i != x_size; ++i) {
320 x[i] = (w << bit_shift) |
carry;
326inline constexpr void bigint_shr1(W x[],
size_t x_size,
size_t shift) {
330 const size_t top = x_size >= word_shift ? (x_size - word_shift) : 0;
342 for(
size_t i = 0; i != top; ++i) {
343 const W w = x[top - i - 1];
344 x[top - i - 1] = (w >> bit_shift) |
carry;
350inline constexpr void bigint_shl2(W y[],
const W x[],
size_t x_size,
size_t shift) {
360 for(
size_t i = word_shift; i != x_size + word_shift + 1; ++i) {
362 y[i] = (w << bit_shift) |
carry;
368inline constexpr void bigint_shr2(W y[],
const W x[],
size_t x_size,
size_t shift) {
371 const size_t new_size = x_size < word_shift ? 0 : (x_size - word_shift);
381 for(
size_t i = new_size; i > 0; --i) {
383 y[i - 1] = (w >> bit_shift) |
carry;
392[[nodiscard]]
inline constexpr auto bigint_linmul2(W x[],
size_t x_size, W y) -> W {
395 for(
size_t i = 0; i != x_size; ++i) {
404 const size_t blocks = x_size - (x_size % 8);
408 for(
size_t i = 0; i != blocks; i += 8) {
412 for(
size_t i = blocks; i != x_size; ++i) {
426inline constexpr int32_t
bigint_cmp(
const W x[],
size_t x_size,
const W y[],
size_t y_size) {
427 static_assert(
sizeof(W) >=
sizeof(uint32_t),
"Size assumption");
429 const W LT =
static_cast<W
>(-1);
433 const size_t common_elems = std::min(x_size, y_size);
437 for(
size_t i = 0; i != common_elems; i++) {
441 result = is_eq.select(result, is_lt.select(LT, GT));
444 if(x_size < y_size) {
446 for(
size_t i = x_size; i != y_size; i++) {
452 }
else if(y_size < x_size) {
454 for(
size_t i = y_size; i != x_size; i++) {
464 return static_cast<int32_t
>(result);
473inline constexpr auto bigint_ct_is_lt(
const W x[],
size_t x_size,
const W y[],
size_t y_size,
bool lt_or_equal =
false)
475 const size_t common_elems = std::min(x_size, y_size);
479 for(
size_t i = 0; i != common_elems; i++) {
482 is_lt = eq.select_mask(is_lt, lt);
485 if(x_size < y_size) {
487 for(
size_t i = x_size; i != y_size; i++) {
492 }
else if(y_size < x_size) {
494 for(
size_t i = y_size; i != x_size; i++) {
507 const size_t common_elems = std::min(x_size, y_size);
511 for(
size_t i = 0; i != common_elems; i++) {
512 diff |= (x[i] ^ y[i]);
516 if(x_size < y_size) {
517 for(
size_t i = x_size; i != y_size; i++) {
520 }
else if(y_size < x_size) {
521 for(
size_t i = y_size; i != x_size; i++) {
529template <WordType W, W div>
533 if constexpr(div == 10 && std::same_as<W, uint32_t>) {
534 constexpr W magic = 0xCCCCCCCD;
535 constexpr size_t shift = 35;
536 return std::make_pair(magic, shift);
537 }
else if constexpr(div == 10 && std::same_as<W, uint64_t>) {
538 constexpr W magic = 0xCCCCCCCCCCCCCCCD;
539 constexpr size_t shift = 67;
540 return std::make_pair(magic, shift);
548 return static_cast<W
>(p >> shift);
572 return vartime_div_2to1_max_d(n1, n0);
580 if(!std::is_constant_evaluated()) {
581#if defined(BOTAN_MP_USE_X86_64_ASM)
582 if constexpr(std::same_as<W, uint64_t>) {
586 asm(
"divq %[v]" :
"=a"(quotient),
"=d"(remainder) : [v]
"r"(m_divisor),
"a"(n0),
"d"(n1));
591#if !defined(BOTAN_BUILD_COMPILER_IS_CLANGCL)
602 n <<= WordInfo<W>::bits;
604 return static_cast<W
>(n / m_divisor);
619 if(high_top_bit || high >= m_divisor) {
644 static inline constexpr W vartime_div_2to1_max_d(W n1, W n0) {
691 BOTAN_ARG_CHECK(a % 2 == 1,
"Cannot compute Montgomery inverse of an even integer");
701 for(
size_t i = 0; i != iter; ++i) {
711template <
size_t S, WordType W,
size_t N>
713 static_assert(N >= 1,
"Invalid input size");
714 static_assert(S < WordInfo<W>::bits,
"Shift too large");
718 for(
size_t i = N - 1; i != 0; --i) {
726template <
size_t S, WordType W,
size_t N>
728 static_assert(N >= 1,
"Invalid input size");
729 static_assert(S < WordInfo<W>::bits,
"Shift too large");
733 for(
size_t i = 0; i != N - 1; ++i) {
742template <WordType W,
size_t N>
745 const constexpr size_t C = N - 1;
751 const constexpr size_t S = (C + NPW - 1) / NPW;
753 auto hex2int = [](
char c) -> int8_t {
754 if(c >=
'0' && c <=
'9') {
755 return static_cast<int8_t
>(c -
'0');
756 }
else if(c >=
'a' && c <=
'f') {
757 return static_cast<int8_t
>(c -
'a' + 10);
758 }
else if(c >=
'A' && c <=
'F') {
759 return static_cast<int8_t
>(c -
'A' + 10);
765 std::array<W, S> r = {0};
767 for(
size_t i = 0; i != C; ++i) {
768 const int8_t c = hex2int(s[i]);
800template <
size_t N, WordType W>
801constexpr inline void comba_mul(W z[2 * N],
const W x[N],
const W y[N]) {
802 if(!std::is_constant_evaluated()) {
803 if constexpr(std::same_as<W, word> && N == 4) {
806 if constexpr(std::same_as<W, word> && N == 6) {
809 if constexpr(std::same_as<W, word> && N == 7) {
812 if constexpr(std::same_as<W, word> && N == 8) {
815 if constexpr(std::same_as<W, word> && N == 9) {
818 if constexpr(std::same_as<W, word> && N == 16) {
825 for(
size_t i = 0; i != 2 * N; ++i) {
826 const size_t start = i + 1 < N ? 0 : i + 1 - N;
827 const size_t end = std::min(N, i + 1);
829 for(
size_t j = start; j != end; ++j) {
830 accum.
mul(x[j], y[i - j]);
836template <
size_t N, WordType W>
837constexpr inline void comba_sqr(W z[2 * N],
const W x[N]) {
838 if(!std::is_constant_evaluated()) {
839 if constexpr(std::same_as<W, word> && N == 4) {
842 if constexpr(std::same_as<W, word> && N == 6) {
845 if constexpr(std::same_as<W, word> && N == 7) {
848 if constexpr(std::same_as<W, word> && N == 8) {
851 if constexpr(std::same_as<W, word> && N == 9) {
854 if constexpr(std::same_as<W, word> && N == 16) {
861 for(
size_t i = 0; i != 2 * N; ++i) {
862 const size_t start = i + 1 < N ? 0 : i + 1 - N;
863 const size_t end = std::min(N, i + 1);
865 for(
size_t j = start; j != end; ++j) {
866 accum.
mul(x[j], x[i - j]);
889 word r[],
const word z[],
size_t z_size,
const word p[],
size_t p_size,
word p_dash,
word ws[]);
907 word r[],
const word z[],
const word p[],
size_t p_size,
word p_dash,
word ws[],
size_t ws_size) {
908 const size_t z_size = 2 * p_size;
910 BOTAN_ARG_CHECK(ws_size >= p_size,
"Montgomery reduction workspace too small");
914 }
else if(p_size == 6) {
916 }
else if(p_size == 8) {
918 }
else if(p_size == 12) {
920 }
else if(p_size == 16) {
922 }
else if(p_size == 24) {
924 }
else if(p_size == 32) {
962void bigint_sqr(
word z[],
size_t z_size,
const word x[],
size_t x_size,
size_t x_sw,
word workspace[],
size_t ws_size);
974template <WordType W,
size_t N, W C>
976 static_assert(N >= 2);
978 std::array<W, N> hi = {};
983 for(
size_t i = 0; i != N; ++i) {
988 word carry_c[2] = {0};
995 std::array<W, N> r = {};
1010#if defined(BOTAN_MP_USE_X86_64_ASM) && defined(__GNUC__) && !defined(__clang__)
1011 if constexpr(N == 4 && std::same_as<W, uint64_t>) {
1012 if(!std::is_constant_evaluated()) {
1014 movq 0(%[x]), %[borrow]
1015 subq %[p0], %[borrow]
1016 movq %[borrow], 0(%[r])
1017 movq 16(%[x]), %[borrow]
1019 movq %[borrow], 8(%[r])
1020 movq 16(%[x]), %[borrow]
1022 movq %[borrow], 16(%[r])
1023 movq 24(%[x]), %[borrow]
1025 movq %[borrow], 24(%[r])
1026 sbbq %[borrow],%[borrow]
1029 : [borrow] "=r"(borrow)
1030 : [x]
"r"(hi.data()), [p0]
"r"(
P0), [r]
"r"(r.data()),
"0"(borrow)
1041 for(
size_t i = 1; i != N; ++i) {
1053template <
size_t WindowBits,
typename W,
size_t N>
1055 static_assert(WindowBits >= 1 && WindowBits <= 7);
1057 constexpr uint8_t WindowMask =
static_cast<uint8_t
>(1 << WindowBits) - 1;
1059 constexpr size_t W_bits =
sizeof(W) * 8;
1060 const auto bit_shift = offset % W_bits;
1061 const auto word_offset = words.size() - 1 - (offset / W_bits);
1063 const bool single_byte_window = bit_shift <= (W_bits - WindowBits) || word_offset == 0;
1065 const auto w0 = words[word_offset];
1067 if(single_byte_window) {
1068 return (w0 >> bit_shift) & WindowMask;
1071 const auto w1 = words[word_offset - 1];
1072 const auto combined = ((w0 >> bit_shift) | (w1 << (W_bits - bit_shift)));
1073 return combined & WindowMask;
#define BOTAN_ASSERT_NOMSG(expr)
#define BOTAN_DEBUG_ASSERT(expr)
#define BOTAN_ARG_CHECK(expr, msg)
#define BOTAN_ASSERT(expr, assertion_made)
static constexpr Mask< T > expand(T v)
static constexpr Mask< T > is_equal(T x, T y)
static constexpr Mask< T > is_lt(T x, T y)
static constexpr Mask< T > is_zero(T x)
constexpr W vartime_mod_2to1(W n1, W n0) const
constexpr W vartime_div_2to1(W n1, W n0) const
constexpr divide_precomp(W divisor)
constexpr void mul(W x, W y)
constexpr Mask< T > conditional_copy_mem(Mask< T > mask, T *dest, const T *if_set, const T *if_unset, size_t elems)
constexpr void unpoison(const T *p, size_t n)
constexpr Mask< T > conditional_assign_mem(T cnd, T *dest, const T *src, size_t elems)
constexpr void bigint_cnd_abs(W cnd, W x[], size_t size)
constexpr auto bigint_add2(W x[], size_t x_size, const W y[], size_t y_size) -> W
constexpr void bigint_linmul3(W z[], const W x[], size_t x_size, W y)
constexpr auto bigint_cnd_sub(W cnd, W x[], const W y[], size_t size) -> W
constexpr auto bigint_add3(W z[], const W x[], size_t x_size, const W y[], size_t y_size) -> W
constexpr void bigint_cnd_swap(W cnd, W x[], W y[], size_t size)
constexpr auto word8_sub3(W z[8], const W x[8], const W y[8], W carry) -> W
constexpr W shift_left(std::array< W, N > &x)
constexpr auto word_sub(W x, W y, W *carry) -> W
constexpr auto word_add(W x, W y, W *carry) -> W
constexpr void comba_sqr(W z[2 *N], const W x[N])
constexpr uint64_t carry_shift(const donna128 &a, size_t shift)
constexpr void bigint_shr2(W y[], const W x[], size_t x_size, size_t shift)
BOTAN_FUZZER_API void basecase_sqr(word z[], size_t z_size, const word x[], size_t x_size)
constexpr size_t read_window_bits(std::span< const W, N > words, size_t offset)
void bigint_comba_sqr4(word z[8], const word x[4])
void bigint_comba_sqr6(word z[12], const word x[6])
constexpr void bigint_shr1(W x[], size_t x_size, size_t shift)
constexpr auto word8_add3(W z[8], const W x[8], const W y[8], W carry) -> W
BOTAN_FUZZER_API void bigint_monty_redc_6(word r[6], const word z[12], const word p[6], word p_dash, word ws[6])
constexpr void comba_mul(W z[2 *N], const W x[N], const W y[N])
constexpr auto word8_sub2(W x[8], const W y[8], W carry) -> W
void bigint_comba_sqr7(word z[14], const word x[7])
void bigint_comba_mul4(word z[8], const word x[4], const word y[4])
constexpr auto word_madd2(W a, W b, W *c) -> W
void bigint_sqr(word z[], size_t z_size, const word x[], size_t x_size, size_t x_sw, word workspace[], size_t ws_size)
void bigint_comba_mul16(word z[32], const word x[16], const word y[16])
BOTAN_FUZZER_API void bigint_monty_redc_24(word r[24], const word z[48], const word p[24], word p_dash, word ws[24])
constexpr auto bigint_sub3(W z[], const W x[], size_t x_size, const W y[], size_t y_size) -> W
constexpr auto monty_inverse(W a) -> W
BOTAN_FUZZER_API void bigint_monty_redc_generic(word r[], const word z[], size_t z_size, const word p[], size_t p_size, word p_dash, word ws[])
void bigint_mul(word z[], size_t z_size, const word x[], size_t x_size, size_t x_sw, const word y[], size_t y_size, size_t y_sw, word workspace[], size_t ws_size)
void zeroize_buffer(T buf[], size_t n)
void bigint_comba_mul6(word z[12], const word x[6], const word y[6])
constexpr void bigint_shl1(W x[], size_t x_size, size_t x_words, size_t shift)
constexpr auto bigint_ct_is_eq(const W x[], size_t x_size, const W y[], size_t y_size) -> CT::Mask< W >
constexpr int32_t bigint_cmp(const W x[], size_t x_size, const W y[], size_t y_size)
consteval std::pair< W, size_t > div_magic()
BOTAN_FUZZER_API void bigint_monty_redc_4(word r[4], const word z[8], const word p[4], word p_dash, word ws[4])
void bigint_monty_redc_inplace(word z[], const word p[], size_t p_size, word p_dash, word ws[], size_t ws_size)
void bigint_monty_redc(word r[], const word z[], const word p[], size_t p_size, word p_dash, word ws[], size_t ws_size)
void bigint_comba_mul7(word z[14], const word x[7], const word y[7])
constexpr W divide_10(W x)
constexpr W bigint_cnd_add(W cnd, W x[], const W y[], size_t size)
void unchecked_copy_memory(T *out, const T *in, size_t n)
constexpr void bigint_monty_maybe_sub(size_t N, W z[], W x0, const W x[], const W p[])
constexpr void bigint_shl2(W y[], const W x[], size_t x_size, size_t shift)
void bigint_comba_mul9(word z[18], const word x[9], const word y[9])
BOTAN_FUZZER_API void bigint_monty_redc_12(word r[12], const word z[24], const word p[12], word p_dash, word ws[12])
void carry(int64_t &h0, int64_t &h1)
BOTAN_FUZZER_API void bigint_monty_redc_16(word r[16], const word z[32], const word p[16], word p_dash, word ws[16])
void bigint_comba_mul24(word z[48], const word x[24], const word y[24])
constexpr auto bigint_sub_abs(W z[], const W x[], const W y[], size_t N, W ws[]) -> CT::Mask< W >
constexpr auto bigint_ct_is_lt(const W x[], size_t x_size, const W y[], size_t y_size, bool lt_or_equal=false) -> CT::Mask< W >
constexpr std::array< W, N > redc_crandall(std::span< const W, 2 *N > z)
constexpr auto word8_add2(W x[8], const W y[8], W carry) -> W
constexpr auto bigint_sub2(W x[], size_t x_size, const W y[], size_t y_size) -> W
void bigint_comba_sqr8(word z[16], const word x[8])
constexpr auto hex_to_words(const char(&s)[N])
void bigint_comba_sqr16(word z[32], const word x[16])
constexpr void bigint_sub2_rev(W x[], const W y[], size_t y_size)
void bigint_comba_sqr9(word z[18], const word x[9])
constexpr auto word8_linmul3(W z[8], const W x[8], W y, W carry) -> W
BOTAN_FUZZER_API void basecase_mul(word z[], size_t z_size, const word x[], size_t x_size, const word y[], size_t y_size)
std::conditional_t< HasNative64BitRegisters, std::uint64_t, uint32_t > word
void bigint_comba_sqr24(word z[48], const word x[24])
void bigint_comba_mul8(word z[16], const word x[8], const word y[8])
BOTAN_FUZZER_API void bigint_monty_redc_8(word r[8], const word z[16], const word p[8], word p_dash, word ws[8])
constexpr W shift_right(std::array< W, N > &x)
BOTAN_FUZZER_API void bigint_monty_redc_32(word r[32], const word z[64], const word p[32], word p_dash, word ws[32])
constexpr auto word_madd3(W a, W b, W c, W *d) -> W
constexpr auto bigint_linmul2(W x[], size_t x_size, W y) -> W