7#include <botan/internal/monty.h>
9#include <botan/reducer.h>
10#include <botan/internal/mp_core.h>
57 const size_t output_size = m_p_words + 1;
59 if(ws.size() < output_size) {
60 ws.resize(output_size);
72 const size_t output_size = 2 * m_p_words + 2;
74 if(ws.size() < output_size) {
75 ws.resize(output_size);
86 std::min(m_p_words, x.
size()),
89 std::min(m_p_words, y.
size()),
99 const size_t output_size = 2 * m_p_words + 2;
100 if(ws.size() < output_size) {
101 ws.resize(output_size);
111 std::min(m_p_words, x.
size()),
114 std::min(m_p_words, y.size()),
124 const size_t output_size = 2 * m_p_words;
126 if(ws.size() < 2 * output_size) {
127 ws.resize(2 * output_size);
130 word* z_data = &ws[0];
131 word* ws_data = &ws[output_size];
139 std::min(m_p_words, x.
size()),
142 std::min(m_p_words, y.size()),
148 if(x.
size() < output_size) {
155 const size_t output_size = 2 * m_p_words;
157 if(ws.size() < 2 * output_size) {
158 ws.resize(2 * output_size);
161 word* z_data = &ws[0];
162 word* ws_data = &ws[output_size];
170 std::min(m_p_words, x.
size()),
173 std::min(m_p_words, y.
size()),
179 if(x.
size() < output_size) {
186 const size_t output_size = 2 * m_p_words;
188 if(ws.size() < output_size) {
189 ws.resize(output_size);
204 const size_t output_size = 2 * m_p_words;
206 if(ws.size() < 2 * output_size) {
207 ws.resize(2 * output_size);
210 word* z_data = &ws[0];
211 word* ws_data = &ws[output_size];
219 if(x.
size() < output_size) {
229 if(redc_needed ==
false) {
234 m_v = m_params->mul(v, m_params->R2(), ws);
239 const uint8_t bits[],
242 m_params(params), m_v(bits, len) {
246 m_v = m_params->mul(m_v, m_params->R2(), ws);
254 m_params(std::move(params)) {
260 m_v = m_params->mul(m_v, m_params->R2(), ws);
265 const size_t p_words = m_params->p_words();
275 return m_v == other.m_v && m_params->p() == other.m_params->p();
283 return m_params->p().bytes();
287 return m_v == m_params->R1();
296 return m_params->redc(m_v, ws);
302 z.
mod_add(other.m_v, m_params->p(), ws);
309 z.
mod_sub(other.m_v, m_params->p(), ws);
315 return this->
add(other, ws);
319 m_v.
mod_add(other.m_v, m_params->p(), ws);
325 return this->
sub(other, ws);
329 m_v.
mod_sub(other.m_v, m_params->p(), ws);
335 return Montgomery_Int(m_params, m_params->mul(m_v, other.m_v, ws),
false);
339 return Montgomery_Int(m_params, m_params->mul(m_v, other.m_v, ws),
false);
343 m_params->mul_by(m_v, other.m_v, ws);
348 m_params->mul_by(m_v, other, ws);
363 for(
size_t i = 0; i != n; ++i) {
364 m_params->square_this(m_v, ws);
370 m_params->square_this(m_v, ws);
384 const BigInt iv = m_params->mul(m_params->inv_mod_p(m_v), m_params->R3(), ws);
393 m_v.
mod_mul(2, m_params->p(), ws);
398 m_v.
mod_mul(3, m_params->p(), ws);
403 m_v.
mod_mul(4, m_params->p(), ws);
408 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
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
T serialize(size_t len) const
static BigInt with_capacity(size_t n)
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 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)
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
#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)
BigInt inverse_mod(const BigInt &n, const BigInt &mod)