10#ifndef BOTAN_MP_CORE_OPS_H_
11#define BOTAN_MP_CORE_OPS_H_
13#include <botan/assert.h>
14#include <botan/exceptn.h>
15#include <botan/mem_ops.h>
16#include <botan/types.h>
17#include <botan/internal/ct_utils.h>
18#include <botan/internal/mp_asmi.h>
34 for(
size_t i = 0; i != size; ++i) {
37 x[i] = mask.select(b, a);
38 y[i] = mask.select(a, b);
52 for(
size_t i = 0; i != size; ++i) {
56 return (mask &
carry);
64inline constexpr auto bigint_cnd_sub(W cnd, W x[],
const W y[],
size_t size) -> W {
69 for(
size_t i = 0; i != size; ++i) {
73 return (mask &
carry);
85 W
carry = mask.if_set_return(1);
86 for(
size_t i = 0; i != size; ++i) {
88 x[i] = mask.select(z, x[i]);
96inline constexpr auto bigint_add2(W x[],
size_t x_size,
const W y[],
size_t y_size) -> W {
101 const size_t blocks = y_size - (y_size % 8);
103 for(
size_t i = 0; i != blocks; i += 8) {
107 for(
size_t i = blocks; i != y_size; ++i) {
111 for(
size_t i = y_size; i != x_size; ++i) {
122inline constexpr auto bigint_add3(W z[],
const W x[],
size_t x_size,
const W y[],
size_t y_size) -> W {
123 if(x_size < y_size) {
129 const size_t blocks = y_size - (y_size % 8);
131 for(
size_t i = 0; i != blocks; i += 8) {
135 for(
size_t i = blocks; i != y_size; ++i) {
139 for(
size_t i = y_size; i != x_size; ++i) {
150inline constexpr auto bigint_sub2(W x[],
size_t x_size,
const W y[],
size_t y_size) -> W {
155 const size_t blocks = y_size - (y_size % 8);
157 for(
size_t i = 0; i != blocks; i += 8) {
161 for(
size_t i = blocks; i != y_size; ++i) {
162 x[i] =
word_sub(x[i], y[i], &borrow);
165 for(
size_t i = y_size; i != x_size; ++i) {
166 x[i] =
word_sub(x[i],
static_cast<W
>(0), &borrow);
179 for(
size_t i = 0; i != y_size; ++i) {
180 x[i] =
word_sub(y[i], x[i], &borrow);
194inline constexpr auto bigint_sub3(W z[],
const W x[],
size_t x_size,
const W y[],
size_t y_size) -> W {
199 const size_t blocks = y_size - (y_size % 8);
201 for(
size_t i = 0; i != blocks; i += 8) {
202 borrow =
word8_sub3(z + i, x + i, y + i, borrow);
205 for(
size_t i = blocks; i != y_size; ++i) {
206 z[i] =
word_sub(x[i], y[i], &borrow);
209 for(
size_t i = y_size; i != x_size; ++i) {
210 z[i] =
word_sub(x[i],
static_cast<W
>(0), &borrow);
230 const size_t blocks = N - (N % 8);
232 for(
size_t i = 0; i != blocks; i += 8) {
233 borrow =
word8_sub3(z + i, x + i, p + i, borrow);
236 for(
size_t i = blocks; i != N; ++i) {
237 z[i] =
word_sub(x[i], p[i], &borrow);
240 borrow = (x0 - borrow) > x0;
255template <
size_t N, WordType W>
259 for(
size_t i = 0; i != N; ++i) {
260 z[i] =
word_sub(x[i], y[i], &borrow);
263 borrow = (x0 - borrow) > x0;
290 const size_t blocks = N - (N % 8);
292 for(
size_t i = 0; i != blocks; i += 8) {
293 borrow0 =
word8_sub3(ws0 + i, x + i, y + i, borrow0);
294 borrow1 =
word8_sub3(ws1 + i, y + i, x + i, borrow1);
297 for(
size_t i = blocks; i != N; ++i) {
298 ws0[i] =
word_sub(x[i], y[i], &borrow0);
299 ws1[i] =
word_sub(y[i], x[i], &borrow1);
309inline constexpr void bigint_shl1(W x[],
size_t x_size,
size_t x_words,
size_t shift) {
313 copy_mem(x + word_shift, x, x_words);
320 for(
size_t i = word_shift; i != x_size; ++i) {
322 x[i] = (w << bit_shift) |
carry;
328inline constexpr void bigint_shr1(W x[],
size_t x_size,
size_t shift) {
332 const size_t top = x_size >= word_shift ? (x_size - word_shift) : 0;
337 clear_mem(x + top, std::min(word_shift, x_size));
344 for(
size_t i = 0; i != top; ++i) {
345 const W w = x[top - i - 1];
346 x[top - i - 1] = (w >> bit_shift) |
carry;
352inline constexpr void bigint_shl2(W y[],
const W x[],
size_t x_size,
size_t shift) {
356 copy_mem(y + word_shift, x, x_size);
362 for(
size_t i = word_shift; i != x_size + word_shift + 1; ++i) {
364 y[i] = (w << bit_shift) |
carry;
370inline constexpr void bigint_shr2(W y[],
const W x[],
size_t x_size,
size_t shift) {
373 const size_t new_size = x_size < word_shift ? 0 : (x_size - word_shift);
376 copy_mem(y, x + word_shift, new_size);
383 for(
size_t i = new_size; i > 0; --i) {
385 y[i - 1] = (w >> bit_shift) |
carry;
394[[nodiscard]]
inline constexpr auto bigint_linmul2(W x[],
size_t x_size, W y) -> W {
397 for(
size_t i = 0; i != x_size; ++i) {
406 const size_t blocks = x_size - (x_size % 8);
410 for(
size_t i = 0; i != blocks; i += 8) {
414 for(
size_t i = blocks; i != x_size; ++i) {
428inline constexpr int32_t
bigint_cmp(
const W x[],
size_t x_size,
const W y[],
size_t y_size) {
429 static_assert(
sizeof(W) >=
sizeof(uint32_t),
"Size assumption");
431 const W LT =
static_cast<W
>(-1);
435 const size_t common_elems = std::min(x_size, y_size);
439 for(
size_t i = 0; i != common_elems; i++) {
443 result = is_eq.select(result, is_lt.select(LT, GT));
446 if(x_size < y_size) {
448 for(
size_t i = x_size; i != y_size; i++) {
454 }
else if(y_size < x_size) {
456 for(
size_t i = y_size; i != x_size; i++) {
466 return static_cast<int32_t
>(result);
475inline 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)
477 const size_t common_elems = std::min(x_size, y_size);
481 for(
size_t i = 0; i != common_elems; i++) {
484 is_lt = eq.select_mask(is_lt, lt);
487 if(x_size < y_size) {
489 for(
size_t i = x_size; i != y_size; i++) {
494 }
else if(y_size < x_size) {
496 for(
size_t i = y_size; i != x_size; i++) {
509 const size_t common_elems = std::min(x_size, y_size);
513 for(
size_t i = 0; i != common_elems; i++) {
514 diff |= (x[i] ^ y[i]);
518 if(x_size < y_size) {
519 for(
size_t i = x_size; i != y_size; i++) {
522 }
else if(y_size < x_size) {
523 for(
size_t i = y_size; i != x_size; i++) {
540 n <<= WordInfo<W>::bits;
542 return static_cast<W
>(n / d);
554 if(high_top_bit || high >= d) {
586 BOTAN_ARG_CHECK(a % 2 == 1,
"Cannot compute Montgomery inverse of an even integer");
611template <
size_t S, WordType W,
size_t N>
613 static_assert(S < WordInfo<W>::bits,
"Shift too large");
616 for(
size_t i = 0; i != N; ++i) {
618 x[i] = (w << S) |
carry;
625template <
size_t S, WordType W,
size_t N>
627 static_assert(S < WordInfo<W>::bits,
"Shift too large");
630 for(
size_t i = 0; i != N; ++i) {
631 const W w = x[N - 1 - i];
632 x[N - 1 - i] = (w >> S) |
carry;
640template <WordType W,
size_t N>
643 const constexpr size_t C = N - 1;
649 const constexpr size_t S = (C + NPW - 1) / NPW;
651 auto hex2int = [](
char c) -> int8_t {
652 if(c >=
'0' && c <=
'9') {
653 return static_cast<int8_t
>(c -
'0');
654 }
else if(c >=
'a' && c <=
'f') {
655 return static_cast<int8_t
>(c -
'a' + 10);
656 }
else if(c >=
'A' && c <=
'F') {
657 return static_cast<int8_t
>(c -
'A' + 10);
663 std::array<W, S> r = {0};
665 for(
size_t i = 0; i != C; ++i) {
666 const int8_t c = hex2int(s[i]);
698template <
size_t N, WordType W>
699constexpr inline void comba_mul(W z[2 * N],
const W x[N],
const W y[N]) {
700 if(!std::is_constant_evaluated()) {
701 if constexpr(std::same_as<W, word> && N == 4) {
704 if constexpr(std::same_as<W, word> && N == 6) {
707 if constexpr(std::same_as<W, word> && N == 7) {
710 if constexpr(std::same_as<W, word> && N == 8) {
713 if constexpr(std::same_as<W, word> && N == 9) {
716 if constexpr(std::same_as<W, word> && N == 16) {
723 for(
size_t i = 0; i != 2 * N; ++i) {
724 const size_t start = i + 1 < N ? 0 : i + 1 - N;
725 const size_t end = std::min(N, i + 1);
727 for(
size_t j = start; j != end; ++j) {
728 accum.
mul(x[j], y[i - j]);
734template <
size_t N, WordType W>
735constexpr inline void comba_sqr(W z[2 * N],
const W x[N]) {
736 if(!std::is_constant_evaluated()) {
737 if constexpr(std::same_as<W, word> && N == 4) {
740 if constexpr(std::same_as<W, word> && N == 6) {
743 if constexpr(std::same_as<W, word> && N == 7) {
746 if constexpr(std::same_as<W, word> && N == 8) {
749 if constexpr(std::same_as<W, word> && N == 9) {
752 if constexpr(std::same_as<W, word> && N == 16) {
759 for(
size_t i = 0; i != 2 * N; ++i) {
760 const size_t start = i + 1 < N ? 0 : i + 1 - N;
761 const size_t end = std::min(N, i + 1);
763 for(
size_t j = start; j != end; ++j) {
764 accum.
mul(x[j], x[i - j]);
787 word r[],
const word z[],
size_t z_size,
const word p[],
size_t p_size,
word p_dash,
word ws[]);
805 word r[],
const word z[],
const word p[],
size_t p_size,
word p_dash,
word ws[],
size_t ws_size) {
806 const size_t z_size = 2 * p_size;
808 BOTAN_ARG_CHECK(ws_size >= p_size,
"Montgomery reduction workspace too small");
812 }
else if(p_size == 6) {
814 }
else if(p_size == 8) {
816 }
else if(p_size == 12) {
818 }
else if(p_size == 16) {
820 }
else if(p_size == 24) {
822 }
else if(p_size == 32) {
860void 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);
872template <WordType W,
size_t N, W C>
874 static_assert(N >= 2);
876 std::array<W, N> hi = {};
881 for(
size_t i = 0; i != N; ++i) {
886 word carry_c[2] = {0};
893 std::array<W, N> r = {};
908#if defined(BOTAN_MP_USE_X86_64_ASM) && defined(__GNUC__) && !defined(__clang__)
909 if constexpr(N == 4 && std::same_as<W, uint64_t>) {
910 if(!std::is_constant_evaluated()) {
912 movq 0(%[x]), %[borrow]
913 subq %[p0], %[borrow]
914 movq %[borrow], 0(%[r])
915 movq 16(%[x]), %[borrow]
917 movq %[borrow], 8(%[r])
918 movq 16(%[x]), %[borrow]
920 movq %[borrow], 16(%[r])
921 movq 24(%[x]), %[borrow]
923 movq %[borrow], 24(%[r])
924 sbbq %[borrow],%[borrow]
927 : [borrow] "=r"(borrow)
928 : [x]
"r"(hi.data()), [p0]
"r"(P0), [r]
"r"(r.data()),
"0"(borrow)
938 r[0] =
word_sub(hi[0], P0, &borrow);
939 for(
size_t i = 1; i != N; ++i) {
951template <
size_t WindowBits,
typename W,
size_t N>
953 static_assert(WindowBits >= 1 && WindowBits <= 7);
955 constexpr uint8_t WindowMask =
static_cast<uint8_t
>(1 << WindowBits) - 1;
957 constexpr size_t W_bits =
sizeof(W) * 8;
958 const auto bit_shift = offset % W_bits;
959 const auto word_offset = words.size() - 1 - (offset / W_bits);
961 const bool single_byte_window = bit_shift <= (W_bits - WindowBits) || word_offset == 0;
963 const auto w0 = words[word_offset];
965 if(single_byte_window) {
966 return (w0 >> bit_shift) & WindowMask;
969 const auto w1 = words[word_offset - 1];
970 const auto combined = ((w0 >> bit_shift) | (w1 << (W_bits - bit_shift)));
971 return combined & WindowMask;
#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 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 copy_mem(T *out, const T *in, size_t n)
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 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)
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 bigint_cnd_add(W cnd, W x[], const W y[], size_t size)
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_modop_vartime(W n1, W n0, W d) -> W
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 void clear_mem(T *ptr, size_t n)
constexpr auto bigint_divop_vartime(W n1, W n0, W d) -> W
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