7 #include <botan/monty.h>
8 #include <botan/reducer.h>
9 #include <botan/internal/mp_core.h>
57 const size_t output_size = 2*m_p_words + 2;
59 if(ws.size() < output_size)
60 ws.resize(output_size);
66 m_p.
data(), m_p_words, m_p_dash,
67 ws.data(), ws.size());
75 const size_t output_size = 2*m_p_words + 2;
77 if(ws.size() < output_size)
78 ws.resize(output_size);
87 ws.data(), ws.size());
90 m_p.
data(), m_p_words, m_p_dash,
91 ws.data(), ws.size());
100 const size_t output_size = 2*m_p_words + 2;
101 if(ws.size() < output_size)
102 ws.resize(output_size);
109 y.data(), y.size(), std::min(m_p_words, y.size()),
110 ws.data(), ws.size());
113 m_p.
data(), m_p_words, m_p_dash,
114 ws.data(), ws.size());
123 const size_t output_size = 2*m_p_words + 2;
125 if(ws.size() < 2*output_size)
126 ws.resize(2*output_size);
128 word* z_data = &ws[0];
129 word* ws_data = &ws[output_size];
135 y.data(), y.size(), std::min(m_p_words, y.size()),
136 ws_data, output_size);
139 m_p.
data(), m_p_words, m_p_dash,
140 ws_data, output_size);
142 if(x.
size() < output_size)
151 const size_t output_size = 2*m_p_words + 2;
153 if(ws.size() < 2*output_size)
154 ws.resize(2*output_size);
156 word* z_data = &ws[0];
157 word* ws_data = &ws[output_size];
164 ws_data, output_size);
167 m_p.
data(), m_p_words, m_p_dash,
168 ws_data, output_size);
170 if(x.
size() < output_size)
177 const size_t output_size = 2*m_p_words + 2;
179 if(ws.size() < output_size)
180 ws.resize(output_size);
188 ws.data(), ws.size());
191 m_p.
data(), m_p_words, m_p_dash,
192 ws.data(), ws.size());
200 const size_t output_size = 2*m_p_words + 2;
202 if(ws.size() < 2*output_size)
203 ws.resize(2*output_size);
205 word* z_data = &ws[0];
206 word* ws_data = &ws[output_size];
212 ws_data, output_size);
215 m_p.
data(), m_p_words, m_p_dash,
216 ws_data, output_size);
218 if(x.
size() < output_size)
228 if(redc_needed ==
false)
236 m_v = m_params->mul(v, m_params->R2(), ws);
241 const uint8_t bits[],
size_t len,
250 m_v = m_params->mul(m_v, m_params->R2(), ws);
255 const word words[],
size_t len,
264 m_v = m_params->mul(m_v, m_params->R2(), ws);
270 const size_t p_words = m_params->p_words();
277 if(w.size() != p_words)
286 return m_v == other.m_v && m_params->p() == other.m_params->p();
291 std::vector<uint8_t> v(
size());
298 return m_params->p().bytes();
303 return m_v == m_params->R1();
314 return m_params->redc(m_v, ws);
321 z.
mod_add(other.m_v, m_params->p(), ws);
329 z.
mod_sub(other.m_v, m_params->p(), ws);
336 return this->
add(other, ws);
341 m_v.
mod_add(other.m_v, m_params->p(), ws);
348 return this->
sub(other, ws);
353 m_v.
mod_sub(other.m_v, m_params->p(), ws);
360 return Montgomery_Int(m_params, m_params->mul(m_v, other.m_v, ws),
false);
366 return Montgomery_Int(m_params, m_params->mul(m_v, other.m_v, ws),
false);
372 m_params->mul_by(m_v, other.m_v, ws);
379 m_params->mul_by(m_v, other, ws);
397 for(
size_t i = 0; i != n; ++i)
398 m_params->square_this(m_v, ws);
404 m_params->square_this(m_v, ws);
416 const BigInt iv = m_params->mul(m_params->inv_mod_p(m_v), m_params->R3(), ws);
427 m_v.
mod_mul(2, m_params->p(), ws);
433 m_v.
mod_mul(3, m_params->p(), ws);
439 m_v.
mod_mul(4, m_params->p(), ws);
445 m_v.
mod_mul(8, m_params->p(), ws);
word word_at(size_t n) const
Montgomery_Int operator-(const Montgomery_Int &other) const
Montgomery_Int additive_inverse() const
Montgomery_Int & mul_by_8(secure_vector< word > &ws)
Montgomery_Int multiplicative_inverse() const
Montgomery_Int & mul_by(const Montgomery_Int &other, secure_vector< word > &ws)
Montgomery_Int(std::shared_ptr< const Montgomery_Params > params)
secure_vector< word > & get_word_vector()
BigInt & mod_sub(const BigInt &y, const BigInt &mod, secure_vector< word > &ws)
std::vector< uint8_t > serialize() const
BigInt ct_inverse_mod_odd_modulus(const BigInt &n, const BigInt &mod)
Montgomery_Int operator+(const Montgomery_Int &other) const
#define BOTAN_ASSERT_NOMSG(expr)
Montgomery_Int & operator-=(const Montgomery_Int &other)
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)
BigInt redc(const BigInt &x, secure_vector< word > &ws) const
Montgomery_Int square(secure_vector< word > &ws) const
BigInt mul(const BigInt &x, const BigInt &y, secure_vector< word > &ws) const
void mul_by(BigInt &x, const secure_vector< word > &y, secure_vector< word > &ws) const
Montgomery_Int & operator*=(const Montgomery_Int &other)
Montgomery_Int & mul_by_4(secure_vector< word > &ws)
std::vector< T, secure_allocator< T >> secure_vector
#define BOTAN_DEBUG_ASSERT(expr)
BigInt sqr(const BigInt &x, secure_vector< word > &ws) const
BigInt multiply(const BigInt &x, const BigInt &y) const
Montgomery_Int operator*(const Montgomery_Int &other) const
BigInt & mod_add(const BigInt &y, const BigInt &mod, secure_vector< word > &ws)
Montgomery_Int & sub(const Montgomery_Int &other, secure_vector< word > &ws)
Montgomery_Int & operator+=(const Montgomery_Int &other)
void square_this(BigInt &x, secure_vector< word > &ws) const
Montgomery_Int mul(const Montgomery_Int &other, secure_vector< word > &ws) const
const word * data() const
Montgomery_Params(const BigInt &p, const Modular_Reducer &mod_p)
static BigInt power_of_2(size_t n)
void copy_mem(T *out, const T *in, size_t n)
Montgomery_Int & mul_by_3(secure_vector< word > &ws)
BigInt reduce(const BigInt &x) const
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)
BigInt & mod_mul(uint8_t y, const BigInt &mod, secure_vector< word > &ws)
void bigint_monty_redc(word z[], const word p[], size_t p_size, word p_dash, word workspace[], size_t ws_size)
Montgomery_Int & add(const Montgomery_Int &other, secure_vector< word > &ws)
Montgomery_Int & square_this(secure_vector< word > &ws)
word monty_inverse(word a)
bool operator==(const Montgomery_Int &other) const
Montgomery_Int & square_this_n_times(secure_vector< word > &ws, size_t n)
void grow_to(size_t n) const
Montgomery_Int & mul_by_2(secure_vector< word > &ws)
static secure_vector< uint8_t > encode_1363(const BigInt &n, size_t bytes)
BigInt inv_mod_p(const BigInt &x) const
BigInt square(const BigInt &x) const