10#ifndef BOTAN_MP_CORE_OPS_H_
11#define BOTAN_MP_CORE_OPS_H_
13#include <botan/exceptn.h>
14#include <botan/mem_ops.h>
15#include <botan/types.h>
16#include <botan/internal/ct_utils.h>
17#include <botan/internal/mp_asmi.h>
33 for(
size_t i = 0; i != size; ++i) {
36 x[i] = mask.select(
b, a);
37 y[i] = mask.select(a,
b);
42inline constexpr W
bigint_cnd_add(W cnd, W x[],
size_t x_size,
const W y[],
size_t y_size) {
49 const size_t blocks = y_size - (y_size % 8);
52 for(
size_t i = 0; i != blocks; i += 8) {
54 mask.select_n(x + i, z, x + i, 8);
57 for(
size_t i = blocks; i != y_size; ++i) {
59 x[i] = mask.select(z[0], x[i]);
62 for(
size_t i = y_size; i != x_size; ++i) {
64 x[i] = mask.select(z[0], x[i]);
67 return mask.if_set_return(
carry);
84inline constexpr auto bigint_cnd_sub(W cnd, W x[],
size_t x_size,
const W y[],
size_t y_size) -> W {
91 const size_t blocks = y_size - (y_size % 8);
94 for(
size_t i = 0; i != blocks; i += 8) {
96 mask.select_n(x + i, z, x + i, 8);
99 for(
size_t i = blocks; i != y_size; ++i) {
101 x[i] = mask.select(z[0], x[i]);
104 for(
size_t i = y_size; i != x_size; ++i) {
106 x[i] = mask.select(z[0], x[i]);
109 return mask.if_set_return(
carry);
117inline constexpr auto bigint_cnd_sub(W cnd, W x[],
const W y[],
size_t size) -> W {
130 const size_t blocks = size - (size % 8);
138 for(
size_t i = 0; i != blocks; i += 8) {
140 borrow =
word8_sub3(t1, x + i, y + i, borrow);
144 for(
size_t i = blocks; i != size; ++i) {
146 const W s =
word_sub(x[i], y[i], &borrow);
163 const size_t blocks = size - (size % 8);
171 for(
size_t i = 0; i != blocks; i += 8) {
173 borrow =
word8_sub3(t1, x + i, z + i, borrow);
174 mask.select_n(x + i, t0, t1, 8);
177 for(
size_t i = blocks; i != size; ++i) {
179 t1[0] =
word_sub(x[i], z[i], &borrow);
180 x[i] = mask.select(t0[0], t1[0]);
183 return mask.select(
carry, borrow);
195 W
carry = mask.if_set_return(1);
196 for(
size_t i = 0; i != size; ++i) {
198 x[i] = mask.select(z, x[i]);
206inline constexpr auto bigint_add2_nc(W x[],
size_t x_size,
const W y[],
size_t y_size) -> W {
211 const size_t blocks = y_size - (y_size % 8);
213 for(
size_t i = 0; i != blocks; i += 8) {
217 for(
size_t i = blocks; i != y_size; ++i) {
221 for(
size_t i = y_size; i != x_size; ++i) {
232inline constexpr auto bigint_add3_nc(W z[],
const W x[],
size_t x_size,
const W y[],
size_t y_size) -> W {
233 if(x_size < y_size) {
239 const size_t blocks = y_size - (y_size % 8);
241 for(
size_t i = 0; i != blocks; i += 8) {
245 for(
size_t i = blocks; i != y_size; ++i) {
249 for(
size_t i = y_size; i != x_size; ++i) {
256template <WordType W,
size_t N>
257inline constexpr auto bigint_add(std::span<W, N> z, std::span<const W, N> x, std::span<const W, N> y) -> W {
258 if constexpr(N == 4) {
260 }
else if constexpr(N == 8) {
275inline constexpr void bigint_add2(W x[],
size_t x_size,
const W y[],
size_t y_size) {
283inline constexpr void bigint_add3(W z[],
const W x[],
size_t x_size,
const W y[],
size_t y_size) {
284 z[x_size > y_size ? x_size : y_size] +=
bigint_add3_nc(z, x, x_size, y, y_size);
291inline constexpr auto bigint_sub2(W x[],
size_t x_size,
const W y[],
size_t y_size) -> W {
296 const size_t blocks = y_size - (y_size % 8);
298 for(
size_t i = 0; i != blocks; i += 8) {
302 for(
size_t i = blocks; i != y_size; ++i) {
303 x[i] =
word_sub(x[i], y[i], &borrow);
306 for(
size_t i = y_size; i != x_size; ++i) {
307 x[i] =
word_sub(x[i],
static_cast<W
>(0), &borrow);
320 const size_t blocks = y_size - (y_size % 8);
322 for(
size_t i = 0; i != blocks; i += 8) {
326 for(
size_t i = blocks; i != y_size; ++i) {
327 x[i] =
word_sub(y[i], x[i], &borrow);
341inline constexpr auto bigint_sub3(W z[],
const W x[],
size_t x_size,
const W y[],
size_t y_size) -> W {
346 const size_t blocks = y_size - (y_size % 8);
348 for(
size_t i = 0; i != blocks; i += 8) {
349 borrow =
word8_sub3(z + i, x + i, y + i, borrow);
352 for(
size_t i = blocks; i != y_size; ++i) {
353 z[i] =
word_sub(x[i], y[i], &borrow);
356 for(
size_t i = y_size; i != x_size; ++i) {
357 z[i] =
word_sub(x[i],
static_cast<W
>(0), &borrow);
377 const size_t blocks = N - (N % 8);
379 for(
size_t i = 0; i != blocks; i += 8) {
380 borrow =
word8_sub3(z + i, x + i, p + i, borrow);
383 for(
size_t i = blocks; i != N; ++i) {
384 z[i] =
word_sub(x[i], p[i], &borrow);
387 borrow = (x0 - borrow) > x0;
402template <
size_t N, WordType W>
406 if constexpr(N == 4) {
408 }
else if constexpr(N == 8) {
411 const constexpr size_t blocks = N - (N % 8);
412 for(
size_t i = 0; i != blocks; i += 8) {
413 borrow =
word8_sub3(z + i, x + i, y + i, borrow);
416 for(
size_t i = blocks; i != N; ++i) {
417 z[i] =
word_sub(x[i], y[i], &borrow);
421 borrow = (x0 - borrow) > x0;
448 const size_t blocks = N - (N % 8);
450 for(
size_t i = 0; i != blocks; i += 8) {
451 borrow0 =
word8_sub3(ws0 + i, x + i, y + i, borrow0);
452 borrow1 =
word8_sub3(ws1 + i, y + i, x + i, borrow1);
455 for(
size_t i = blocks; i != N; ++i) {
456 ws0[i] =
word_sub(x[i], y[i], &borrow0);
457 ws1[i] =
word_sub(y[i], x[i], &borrow1);
467inline constexpr void bigint_shl1(W x[],
size_t x_size,
size_t x_words,
size_t shift) {
471 copy_mem(x + word_shift, x, x_words);
478 for(
size_t i = word_shift; i != x_size; ++i) {
480 x[i] = (w << bit_shift) |
carry;
486inline constexpr void bigint_shr1(W x[],
size_t x_size,
size_t shift) {
490 const size_t top = x_size >= word_shift ? (x_size - word_shift) : 0;
495 clear_mem(x + top, std::min(word_shift, x_size));
502 for(
size_t i = 0; i != top; ++i) {
503 const W w = x[top - i - 1];
504 x[top - i - 1] = (w >> bit_shift) |
carry;
510inline constexpr void bigint_shl2(W y[],
const W x[],
size_t x_size,
size_t shift) {
514 copy_mem(y + word_shift, x, x_size);
520 for(
size_t i = word_shift; i != x_size + word_shift + 1; ++i) {
522 y[i] = (w << bit_shift) |
carry;
528inline constexpr void bigint_shr2(W y[],
const W x[],
size_t x_size,
size_t shift) {
531 const size_t new_size = x_size < word_shift ? 0 : (x_size - word_shift);
534 copy_mem(y, x + word_shift, new_size);
541 for(
size_t i = new_size; i > 0; --i) {
543 y[i - 1] = (w >> bit_shift) |
carry;
552[[nodiscard]]
inline constexpr auto bigint_linmul2(W x[],
size_t x_size, W y) -> W {
553 const size_t blocks = x_size - (x_size % 8);
557 for(
size_t i = 0; i != blocks; i += 8) {
561 for(
size_t i = blocks; i != x_size; ++i) {
570 const size_t blocks = x_size - (x_size % 8);
574 for(
size_t i = 0; i != blocks; i += 8) {
578 for(
size_t i = blocks; i != x_size; ++i) {
592inline constexpr int32_t
bigint_cmp(
const W x[],
size_t x_size,
const W y[],
size_t y_size) {
593 static_assert(
sizeof(W) >=
sizeof(uint32_t),
"Size assumption");
595 const W LT =
static_cast<W
>(-1);
599 const size_t common_elems = std::min(x_size, y_size);
603 for(
size_t i = 0; i != common_elems; i++) {
607 result = is_eq.select(result, is_lt.select(LT, GT));
610 if(x_size < y_size) {
612 for(
size_t i = x_size; i != y_size; i++) {
618 }
else if(y_size < x_size) {
620 for(
size_t i = y_size; i != x_size; i++) {
630 return static_cast<int32_t
>(result);
639inline 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)
641 const size_t common_elems = std::min(x_size, y_size);
645 for(
size_t i = 0; i != common_elems; i++) {
648 is_lt = eq.select_mask(is_lt, lt);
651 if(x_size < y_size) {
653 for(
size_t i = x_size; i != y_size; i++) {
658 }
else if(y_size < x_size) {
660 for(
size_t i = y_size; i != x_size; i++) {
673 const size_t common_elems = std::min(x_size, y_size);
677 for(
size_t i = 0; i != common_elems; i++) {
678 diff |= (x[i] ^ y[i]);
682 if(x_size < y_size) {
683 for(
size_t i = x_size; i != y_size; i++) {
686 }
else if(y_size < x_size) {
687 for(
size_t i = y_size; i != x_size; i++) {
709inline constexpr int32_t
bigint_sub_abs(W z[],
const W x[],
size_t x_size,
const W y[],
size_t y_size) {
710 const int32_t relative_size =
bigint_cmp(x, x_size, y, y_size);
713 const bool need_swap = relative_size < 0;
722 y_size = std::min(x_size, y_size);
726 return relative_size;
739inline constexpr void bigint_mod_sub(W t[],
const W s[],
const W mod[],
size_t mod_sw, W ws[]) {
744 const W borrow =
bigint_sub3(ws, mod, mod_sw, s, mod_sw);
749 if(!std::is_constant_evaluated()) {
756template <
size_t N, WordType W>
767 if(!std::is_constant_evaluated()) {
785 n <<= WordInfo<W>::bits;
787 return static_cast<W
>(n / d);
799 if(high_top_bit || high >= d) {
860template <
size_t S, WordType W,
size_t N>
862 static_assert(S < WordInfo<W>::bits,
"Shift too large");
865 for(
size_t i = 0; i != N; ++i) {
867 x[i] = (w << S) |
carry;
874template <
size_t S, WordType W,
size_t N>
876 static_assert(S < WordInfo<W>::bits,
"Shift too large");
879 for(
size_t i = 0; i != N; ++i) {
880 const W w = x[N - 1 - i];
881 x[N - 1 - i] = (w >> S) |
carry;
889template <WordType W,
size_t N>
892 const constexpr size_t C = N - 1;
898 const constexpr size_t S = (C + NPW - 1) / NPW;
900 auto hex2int = [](
char c) -> int8_t {
901 if(c >=
'0' && c <=
'9') {
902 return static_cast<int8_t
>(c -
'0');
903 }
else if(c >=
'a' && c <=
'f') {
904 return static_cast<int8_t
>(c -
'a' + 10);
905 }
else if(c >=
'A' && c <=
'F') {
906 return static_cast<int8_t
>(c -
'A' + 10);
912 std::array<W, S> r = {0};
914 for(
size_t i = 0; i != C; ++i) {
915 const int8_t c = hex2int(s[i]);
947template <
size_t N, WordType W>
948constexpr inline void comba_mul(W z[2 * N],
const W x[N],
const W y[N]) {
949 if(!std::is_constant_evaluated()) {
950 if constexpr(std::same_as<W, word> && N == 4) {
953 if constexpr(std::same_as<W, word> && N == 6) {
956 if constexpr(std::same_as<W, word> && N == 7) {
959 if constexpr(std::same_as<W, word> && N == 8) {
962 if constexpr(std::same_as<W, word> && N == 9) {
965 if constexpr(std::same_as<W, word> && N == 16) {
972 for(
size_t i = 0; i != 2 * N; ++i) {
973 const size_t start = i + 1 < N ? 0 : i + 1 - N;
974 const size_t end = std::min(N, i + 1);
976 for(
size_t j = start; j != end; ++j) {
977 accum.
mul(x[j], y[i - j]);
983template <
size_t N, WordType W>
984constexpr inline void comba_sqr(W z[2 * N],
const W x[N]) {
985 if(!std::is_constant_evaluated()) {
986 if constexpr(std::same_as<W, word> && N == 4) {
989 if constexpr(std::same_as<W, word> && N == 6) {
992 if constexpr(std::same_as<W, word> && N == 7) {
995 if constexpr(std::same_as<W, word> && N == 8) {
998 if constexpr(std::same_as<W, word> && N == 9) {
1001 if constexpr(std::same_as<W, word> && N == 16) {
1008 for(
size_t i = 0; i != 2 * N; ++i) {
1009 const size_t start = i + 1 < N ? 0 : i + 1 - N;
1010 const size_t end = std::min(N, i + 1);
1012 for(
size_t j = start; j != end; ++j) {
1013 accum.
mul(x[j], x[i - j]);
1047inline void bigint_monty_redc(word z[],
const word p[],
size_t p_size, word p_dash, word ws[],
size_t ws_size) {
1048 const size_t z_size = 2 * p_size;
1050 BOTAN_ARG_CHECK(ws_size >= p_size,
"Montgomery reduction workspace too small");
1054 }
else if(p_size == 6) {
1056 }
else if(p_size == 8) {
1058 }
else if(p_size == 16) {
1060 }
else if(p_size == 24) {
1062 }
else if(p_size == 32) {
1073void basecase_mul(word z[],
size_t z_size,
const word x[],
size_t x_size,
const word y[],
size_t y_size);
1079void basecase_sqr(word z[],
size_t z_size,
const word x[],
size_t x_size);
1095void 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);
1100template <WordType W,
size_t N, W C>
1102 static_assert(C % 2 == 1);
1104 for(
size_t i = 0; i != N; ++i) {
1121template <WordType W,
size_t N, W C>
1123 static_assert(N >= 2);
1125 std::array<W, N> hi = {};
1130 for(
size_t i = 0; i != N; ++i) {
1135 word carry_c[2] = {0};
1142 std::array<W, N> r = {};
#define BOTAN_DEBUG_ASSERT(expr)
#define BOTAN_ARG_CHECK(expr, msg)
#define BOTAN_ASSERT(expr, assertion_made)
constexpr void select_n(T output[], const T x[], const T y[], size_t len) const
static constexpr Mask< T > expand(T v)
constexpr T select(T x, T y) const
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 void conditional_swap_ptr(bool cnd, T &x, T &y)
constexpr void conditional_swap(bool cnd, T &x, T &y)
constexpr Mask< T > conditional_assign_mem(T cnd, T *sink, const T *src, size_t elems)
constexpr Mask< T > conditional_copy_mem(Mask< T > mask, T *to, const T *from0, const T *from1, size_t elems)
constexpr void unpoison(const T *p, size_t n)
constexpr void bigint_cnd_abs(W cnd, W x[], size_t size)
constexpr void bigint_linmul3(W z[], const W x[], size_t x_size, W y)
constexpr auto bigint_add(std::span< W, N > z, std::span< const W, N > x, std::span< const W, N > y) -> 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
void bigint_monty_redc(word z[], const word p[], size_t p_size, word p_dash, word ws[], size_t ws_size)
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)
void bigint_comba_sqr4(word z[8], const word x[4])
void bigint_comba_sqr6(word z[12], const word x[6])
BOTAN_FUZZER_API void bigint_monty_redc_24(word z[48], const word p[24], word p_dash, word ws[])
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
constexpr void bigint_mod_sub_n(W t[], const W s[], const W mod[], W ws[])
constexpr auto word4_sub3(W z[4], const W x[4], const W y[4], W carry) -> W
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])
constexpr auto word8_sub2_rev(W x[8], const W y[8], W carry) -> W
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
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)
constexpr auto bigint_cnd_addsub(CT::Mask< W > mask, W x[], const W y[], const W z[], size_t size) -> W
void bigint_comba_mul6(word z[12], const word x[6], const word y[6])
BOTAN_FUZZER_API void bigint_monty_redc_4(word z[8], const word p[4], word p_dash, word ws[])
constexpr void bigint_shl1(W x[], size_t x_size, size_t x_words, size_t shift)
constexpr auto word4_add3(W z[4], const W x[4], const W y[4], W carry) -> W
consteval std::array< W, N > crandall_p()
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)
void bigint_comba_mul7(word z[14], const word x[7], const word y[7])
constexpr auto bigint_cnd_sub(W cnd, W x[], size_t x_size, const W y[], size_t y_size) -> W
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])
constexpr W bigint_cnd_add(W cnd, W x[], size_t x_size, const W y[], size_t y_size)
void carry(int64_t &h0, int64_t &h1)
constexpr auto word8_linmul2(W x[8], W y, W carry) -> W
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 >
BOTAN_FUZZER_API void bigint_monty_redc_6(word z[12], const word p[6], word p_dash, word ws[])
constexpr void bigint_add2(W x[], size_t x_size, const W y[], size_t y_size)
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_cnd_add_or_sub(CT::Mask< W > mask, W x[], const W y[], size_t size)
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 bigint_add2_nc(W x[], size_t x_size, const W y[], size_t y_size) -> W
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)
constexpr void bigint_add3(W z[], const W x[], size_t x_size, const W y[], size_t y_size)
BOTAN_FUZZER_API void bigint_monty_redc_generic(word z[], size_t z_size, const word p[], size_t p_size, word p_dash, word ws[])
void bigint_comba_sqr24(word z[48], const word x[24])
constexpr void copy_mem(T *out, const T *in, size_t n)
BOTAN_FUZZER_API void bigint_monty_redc_32(word z[64], const word p[32], word p_dash, word ws[])
void bigint_comba_mul8(word z[16], const word x[8], const word y[8])
BOTAN_FUZZER_API void bigint_monty_redc_16(word z[32], const word p[16], word p_dash, word ws[])
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_8(word z[16], const word p[8], word p_dash, word ws[])
constexpr void bigint_mod_sub(W t[], const W s[], const W mod[], size_t mod_sw, W ws[])
constexpr auto word_madd3(W a, W b, W c, W *d) -> W
constexpr auto bigint_add3_nc(W z[], const W x[], size_t x_size, const W y[], size_t y_size) -> W
constexpr auto bigint_linmul2(W x[], size_t x_size, W y) -> W