7#include <botan/monty.h>
8#include <botan/reducer.h>
9#include <botan/internal/mp_core.h>
58 const size_t output_size = 2*m_p_words + 2;
60 if(ws.size() < output_size)
61 ws.resize(output_size);
67 m_p.
data(), m_p_words, m_p_dash,
68 ws.data(), ws.size());
76 const size_t output_size = 2*m_p_words + 2;
78 if(ws.size() < output_size)
79 ws.resize(output_size);
88 ws.data(), ws.size());
91 m_p.
data(), m_p_words, m_p_dash,
92 ws.data(), ws.size());
101 const size_t output_size = 2*m_p_words + 2;
102 if(ws.size() < output_size)
103 ws.resize(output_size);
110 y.data(), y.size(), std::min(m_p_words, y.size()),
111 ws.data(), ws.size());
114 m_p.
data(), m_p_words, m_p_dash,
115 ws.data(), ws.size());
124 const size_t output_size = 2*m_p_words + 2;
126 if(ws.size() < 2*output_size)
127 ws.resize(2*output_size);
129 word* z_data = &ws[0];
130 word* ws_data = &ws[output_size];
136 y.data(), y.size(), std::min(m_p_words, y.size()),
137 ws_data, output_size);
140 m_p.
data(), m_p_words, m_p_dash,
141 ws_data, output_size);
143 if(x.
size() < output_size)
152 const size_t output_size = 2*m_p_words + 2;
154 if(ws.size() < 2*output_size)
155 ws.resize(2*output_size);
157 word* z_data = &ws[0];
158 word* ws_data = &ws[output_size];
165 ws_data, output_size);
168 m_p.
data(), m_p_words, m_p_dash,
169 ws_data, output_size);
171 if(x.
size() < output_size)
178 const size_t output_size = 2*m_p_words + 2;
180 if(ws.size() < output_size)
181 ws.resize(output_size);
189 ws.data(), ws.size());
192 m_p.
data(), m_p_words, m_p_dash,
193 ws.data(), ws.size());
201 const size_t output_size = 2*m_p_words + 2;
203 if(ws.size() < 2*output_size)
204 ws.resize(2*output_size);
206 word* z_data = &ws[0];
207 word* ws_data = &ws[output_size];
213 ws_data, output_size);
216 m_p.
data(), m_p_words, m_p_dash,
217 ws_data, output_size);
219 if(x.
size() < output_size)
229 if(redc_needed ==
false)
237 m_v = m_params->mul(v, m_params->R2(), ws);
242 const uint8_t bits[],
size_t len,
251 m_v = m_params->mul(m_v, m_params->R2(), ws);
256 const word words[],
size_t len,
265 m_v = m_params->mul(m_v, m_params->R2(), ws);
271 const size_t p_words = m_params->p_words();
281 return m_v == other.m_v && m_params->p() == other.m_params->p();
286 std::vector<uint8_t> v(
size());
293 return m_params->p().bytes();
298 return m_v == m_params->R1();
309 return m_params->redc(m_v, ws);
316 z.
mod_add(other.m_v, m_params->p(), ws);
324 z.
mod_sub(other.m_v, m_params->p(), ws);
331 return this->
add(other, ws);
336 m_v.
mod_add(other.m_v, m_params->p(), ws);
343 return this->
sub(other, ws);
348 m_v.
mod_sub(other.m_v, m_params->p(), ws);
355 return Montgomery_Int(m_params, m_params->mul(m_v, other.m_v, ws),
false);
361 return Montgomery_Int(m_params, m_params->mul(m_v, other.m_v, ws),
false);
367 m_params->mul_by(m_v, other.m_v, ws);
374 m_params->mul_by(m_v, other, ws);
392 for(
size_t i = 0; i != n; ++i)
393 m_params->square_this(m_v, ws);
399 m_params->square_this(m_v, ws);
411 const BigInt iv = m_params->mul(m_params->inv_mod_p(m_v), m_params->R3(), ws);
422 m_v.
mod_mul(2, m_params->p(), ws);
428 m_v.
mod_mul(3, m_params->p(), ws);
434 m_v.
mod_mul(4, m_params->p(), ws);
440 m_v.
mod_mul(8, m_params->p(), ws);
#define BOTAN_ASSERT_NOMSG(expr)
#define BOTAN_DEBUG_ASSERT(expr)
BigInt & mod_mul(uint8_t y, const BigInt &mod, secure_vector< word > &ws)
void grow_to(size_t n) const
BigInt & mod_add(const BigInt &y, const BigInt &mod, secure_vector< word > &ws)
const word * data() const
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)
static secure_vector< uint8_t > encode_1363(const BigInt &n, size_t bytes)
BigInt square(const BigInt &x) const
BigInt multiply(const BigInt &x, const BigInt &y) const
BigInt reduce(const BigInt &x) const
Montgomery_Int & operator*=(const Montgomery_Int &other)
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 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)
Montgomery_Int multiplicative_inverse() const
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
void mul_by(BigInt &x, const secure_vector< word > &y, secure_vector< word > &ws) const
BigInt mul(const BigInt &x, const BigInt &y, secure_vector< word > &ws) const
BigInt sqr(const BigInt &x, secure_vector< word > &ws) const
BigInt inv_mod_p(const BigInt &x) const
Montgomery_Params(const BigInt &p, const Modular_Reducer &mod_p)
void square_this(BigInt &x, secure_vector< word > &ws) const
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_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)
word monty_inverse(word a)
void copy_mem(T *out, const T *in, size_t n)
void bigint_monty_redc(word z[], const word p[], size_t p_size, word p_dash, word workspace[], size_t ws_size)
BigInt inverse_mod(const BigInt &n, const BigInt &mod)
std::vector< T, secure_allocator< T > > secure_vector