7#include <botan/internal/monty.h>
9#include <botan/numthry.h>
10#include <botan/reducer.h>
11#include <botan/internal/mp_core.h>
46 m_r1 = mod_p.reduce(r);
48 m_r3 = mod_p.multiply(m_r1, m_r2);
52 const size_t output_size = m_p_words + 1;
54 if(ws.size() < output_size) {
55 ws.resize(output_size);
67 const size_t output_size = 2 * m_p_words;
69 if(ws.size() < output_size) {
70 ws.resize(output_size);
80 this->
mul(z, x, y, ws);
85 const size_t output_size = 2 * m_p_words;
87 if(ws.size() < output_size) {
88 ws.resize(output_size);
94 if(z.
size() < output_size) {
102 std::min(m_p_words, x.
size()),
105 std::min(m_p_words, y.
size()),
114 this->
mul(z, x, y, ws);
119 const size_t output_size = 2 * m_p_words;
120 if(ws.size() < output_size) {
121 ws.resize(output_size);
123 if(z.
size() < output_size) {
133 std::min(m_p_words, x.
size()),
136 std::min(m_p_words, y.size()),
144 const size_t output_size = 2 * m_p_words;
146 if(ws.size() < 2 * output_size) {
147 ws.resize(2 * output_size);
150 word* z_data = &ws[0];
151 word* ws_data = &ws[output_size];
159 std::min(m_p_words, x.
size()),
162 std::min(m_p_words, y.size()),
168 if(x.
size() < output_size) {
175 const size_t output_size = 2 * m_p_words;
177 if(ws.size() < 2 * output_size) {
178 ws.resize(2 * output_size);
181 word* z_data = &ws[0];
182 word* ws_data = &ws[output_size];
190 std::min(m_p_words, x.
size()),
193 std::min(m_p_words, y.
size()),
199 if(x.
size() < output_size) {
221 const size_t output_size = 2 * m_p_words;
223 if(ws.size() < output_size) {
224 ws.resize(output_size);
227 if(z.
size() < output_size) {
237 const size_t output_size = 2 * m_p_words;
239 if(ws.size() < 2 * output_size) {
240 ws.resize(2 * output_size);
243 word* z_data = &ws[0];
244 word* ws_data = &ws[output_size];
252 if(x.
size() < output_size) {
266 auto redc_x = params->
mul(params->redc(x, ws), params->R3(), ws);
274 if(redc_needed ==
false) {
279 m_v = m_params->mul(v, m_params->R2(), ws);
284 const uint8_t bits[],
287 m_params(params), m_v(bits, len) {
291 m_v = m_params->mul(m_v, m_params->R2(), ws);
299 m_params(std::move(params)) {
305 m_v = m_params->mul(m_v, m_params->R2(), ws);
310 const size_t p_words = m_params->p_words();
316 return m_v == other.m_v && m_params->p() == other.m_params->p();
324 return m_params->p().bytes();
328 return m_v == m_params->R1();
337 return m_params->redc(m_v, ws);
344 z.
mod_add(other.m_v, m_params->p(), ws);
352 z.
mod_sub(other.m_v, m_params->p(), ws);
359 return this->
add(other, ws);
364 m_v.
mod_add(other.m_v, m_params->p(), ws);
371 return this->
sub(other, ws);
376 m_v.
mod_sub(other.m_v, m_params->p(), ws);
383 return Montgomery_Int(m_params, m_params->mul(m_v, other.m_v, ws),
false);
388 return Montgomery_Int(m_params, m_params->mul(m_v, other.m_v, ws),
false);
393 m_params->mul_by(m_v, other.m_v, ws);
398 m_params->mul_by(m_v, other, ws);
414 for(
size_t i = 0; i != n; ++i) {
415 m_params->square_this(m_v, ws);
421 m_params->square_this(m_v, ws);
438 m_v.
mod_mul(2, m_params->p(), ws);
443 m_v.
mod_mul(3, m_params->p(), ws);
448 m_v.
mod_mul(4, m_params->p(), ws);
453 m_v.
mod_mul(8, m_params->p(), ws);
#define BOTAN_ASSERT_NOMSG(expr)
#define BOTAN_DEBUG_ASSERT(expr)
#define BOTAN_STATE_CHECK(expr)
BigInt & mod_mul(uint8_t y, const BigInt &mod, secure_vector< word > &ws)
void grow_to(size_t n) const
void set_words(const word w[], size_t len)
BigInt & mod_add(const BigInt &y, const BigInt &mod, secure_vector< word > &ws)
word word_at(size_t n) const
BigInt & mod_sub(const BigInt &y, const BigInt &mod, secure_vector< word > &ws)
static BigInt power_of_2(size_t n)
const word * _data() const
BigInt & square(secure_vector< word > &ws)
T serialize(size_t len) const
static BigInt with_capacity(size_t n)
BigInt square(const BigInt &x) const
static Modular_Reducer for_secret_modulus(const BigInt &m)
BigInt multiply(const BigInt &x, const BigInt &y) const
BigInt reduce(const BigInt &x) const
Montgomery_Int & operator*=(const Montgomery_Int &other)
static Montgomery_Int one(const std::shared_ptr< const Montgomery_Params > ¶ms)
Montgomery_Int(std::shared_ptr< const Montgomery_Params > params)
bool operator==(const Montgomery_Int &other) const
Montgomery_Int & add(const Montgomery_Int &other, secure_vector< word > &ws)
Montgomery_Int & operator+=(const Montgomery_Int &other)
Montgomery_Int & operator-=(const Montgomery_Int &other)
Montgomery_Int square(secure_vector< word > &ws) const
Montgomery_Int cube(secure_vector< word > &ws) const
Montgomery_Int additive_inverse() const
Montgomery_Int operator*(const Montgomery_Int &other) const
Montgomery_Int & mul_by_3(secure_vector< word > &ws)
Montgomery_Int & mul_by_8(secure_vector< word > &ws)
Montgomery_Int & mul_by_2(secure_vector< word > &ws)
Montgomery_Int & sub(const Montgomery_Int &other, secure_vector< word > &ws)
Montgomery_Int operator-(const Montgomery_Int &other) const
Montgomery_Int operator+(const Montgomery_Int &other) const
Montgomery_Int & square_this_n_times(secure_vector< word > &ws, size_t n)
Montgomery_Int & mul_by_4(secure_vector< word > &ws)
Montgomery_Int & square_this(secure_vector< word > &ws)
static Montgomery_Int from_wide_int(const std::shared_ptr< const Montgomery_Params > ¶ms, const BigInt &x)
Montgomery_Int mul(const Montgomery_Int &other, secure_vector< word > &ws) const
Montgomery_Int & mul_by(const Montgomery_Int &other, secure_vector< word > &ws)
std::vector< uint8_t > serialize() const
BigInt redc(const BigInt &x, secure_vector< word > &ws) const
BigInt sqr(const BigInt &x, secure_vector< word > &ws) const
void mul(BigInt &z, const BigInt &x, const BigInt &y, secure_vector< word > &ws) const
void redc_in_place(BigInt &x, secure_vector< word > &ws) const
Montgomery_Params(const BigInt &p, const Modular_Reducer &mod_p)
void mul_by(BigInt &x, std::span< const word > y, secure_vector< word > &ws) const
void square_this(BigInt &x, secure_vector< word > &ws) const
#define BOTAN_MP_WORD_BITS
void bigint_monty_redc(word z[], const word p[], size_t p_size, word p_dash, word ws[], size_t ws_size)
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)
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)
std::vector< T, secure_allocator< T > > secure_vector
constexpr void copy_mem(T *out, const T *in, size_t n)