10#include <botan/internal/mp_core.h>
12#include <botan/assert.h>
14#if defined(BOTAN_HAS_MP_MULX)
15 #include <botan/internal/mp_mulx.h>
32 while(lower < bound) {
33 const size_t upper = bound - lower;
36 accum.mul(ws[lower], p[upper]);
37 accum.mul(ws[lower + 1], p[upper - 1]);
38 accum.mul(ws[lower + 2], p[upper - 2]);
39 accum.mul(ws[lower + 3], p[upper - 3]);
40 accum.mul(ws[lower + 4], p[upper - 4]);
41 accum.mul(ws[lower + 5], p[upper - 5]);
42 accum.mul(ws[lower + 6], p[upper - 6]);
43 accum.mul(ws[lower + 7], p[upper - 7]);
44 accum.mul(ws[lower + 8], p[upper - 8]);
45 accum.mul(ws[lower + 9], p[upper - 9]);
46 accum.mul(ws[lower + 10], p[upper - 10]);
47 accum.mul(ws[lower + 11], p[upper - 11]);
48 accum.mul(ws[lower + 12], p[upper - 12]);
49 accum.mul(ws[lower + 13], p[upper - 13]);
50 accum.mul(ws[lower + 14], p[upper - 14]);
51 accum.mul(ws[lower + 15], p[upper - 15]);
53 }
else if(upper >= 8) {
54 accum.mul(ws[lower], p[upper]);
55 accum.mul(ws[lower + 1], p[upper - 1]);
56 accum.mul(ws[lower + 2], p[upper - 2]);
57 accum.mul(ws[lower + 3], p[upper - 3]);
58 accum.mul(ws[lower + 4], p[upper - 4]);
59 accum.mul(ws[lower + 5], p[upper - 5]);
60 accum.mul(ws[lower + 6], p[upper - 6]);
61 accum.mul(ws[lower + 7], p[upper - 7]);
63 }
else if(upper >= 4) {
64 accum.mul(ws[lower], p[upper]);
65 accum.mul(ws[lower + 1], p[upper - 1]);
66 accum.mul(ws[lower + 2], p[upper - 2]);
67 accum.mul(ws[lower + 3], p[upper - 3]);
69 }
else if(upper >= 2) {
70 accum.mul(ws[lower], p[upper]);
71 accum.mul(ws[lower + 1], p[upper - 1]);
74 accum.mul(ws[lower], p[upper]);
95 word r[],
const word z[],
size_t z_size,
const word p[],
size_t p_size,
word p_dash,
word ws[]) {
96 BOTAN_ARG_CHECK(z_size >= 2 * p_size && p_size > 0,
"Invalid sizes for bigint_monty_redc_generic");
104 for(
size_t i = 1; i != p_size; ++i) {
105 mul_rev_range(accum, ws, p, i);
110 for(
size_t i = 0; i != p_size - 1; ++i) {
111 mul_rev_range(accum, &ws[i + 1], &p[i], p_size - (i + 1));
112 accum.
add(z[p_size + i]);
116 accum.
add(z[2 * p_size - 1]);
118 ws[p_size - 1] = accum.
extract();
145 BOTAN_ARG_CHECK(ws_size >= 4 * p_size && p_size > 0,
"Invalid workspace for Montgomery multiplication");
147#if defined(BOTAN_HAS_MP_MULX)
154 word* mul_ws = ws + 2 * p_size;
155 bigint_mul(prod, 2 * p_size, x, p_size, p_size, y, p_size, p_size, mul_ws, 2 * p_size);
160 BOTAN_ARG_CHECK(ws_size >= 4 * p_size && p_size > 0,
"Invalid workspace for Montgomery squaring");
164#if defined(BOTAN_HAS_MP_MULX)
170 word* mul_ws = ws + 2 * p_size;
171 bigint_sqr(prod, 2 * p_size, x, p_size, p_size, mul_ws, 2 * p_size);
#define BOTAN_ARG_CHECK(expr, msg)
constexpr W monty_step(W p0, W p_dash)
#define BOTAN_FORCE_INLINE
BOTAN_TEST_API void bigint_monty_mul(word z[], const word x[], const word y[], const word p[], size_t p_size, word p_dash, word ws[], size_t ws_size)
void bigint_mulx_monty_mul(word z[], const word x[], const word y[], const word p[], size_t N, word p_dash, word ws[])
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)
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_monty_redc(word r[], const word z[], const word p[], size_t p_size, word p_dash, word ws[], size_t ws_size)
constexpr void bigint_monty_maybe_sub(size_t N, W z[], W x0, const W x[], const W p[])
BOTAN_TEST_API void bigint_monty_sqr(word z[], const word x[], const word p[], size_t p_size, word p_dash, word ws[], size_t ws_size)
void bigint_mulx_monty_sqr(word z[], const word x[], const word p[], size_t N, word p_dash, word ws[])
bool use_mulx_monty(size_t p_size)
std::conditional_t< HasNative64BitRegisters, std::uint64_t, uint32_t > word
The native machine word, used as the limb type for multiprecision integers.