Botan 3.13.0
Crypto and TLS for C&
Botan::Montgomery_Params Class Referencefinal

#include <monty.h>

Public Member Functions

 Montgomery_Params (const BigInt &p)
 Montgomery_Params (const BigInt &p, const Barrett_Reduction &mod_p)
void mul (BigInt &z, const BigInt &x, const BigInt &y, secure_vector< word > &ws) const
void mul (BigInt &z, const BigInt &x, std::span< const word > y, secure_vector< word > &ws) const
BigInt mul (const BigInt &x, const BigInt &y, secure_vector< word > &ws) const
void mul_by (BigInt &x, const BigInt &y, secure_vector< word > &ws) const
bool operator!= (const Montgomery_Params &other) const
bool operator== (const Montgomery_Params &other) const
const BigIntp () const
word p_dash () const
size_t p_words () const
const BigIntR1 () const
const BigIntR2 () const
const BigIntR3 () const
BigInt redc (const BigInt &x, secure_vector< word > &ws) const
void sqr (BigInt &z, const BigInt &x, secure_vector< word > &ws) const
void sqr (BigInt &z, std::span< const word > x, secure_vector< word > &ws) const
BigInt sqr (const BigInt &x, secure_vector< word > &ws) const

Detailed Description

Parameters for Montgomery Reduction

Definition at line 23 of file monty.h.

Constructor & Destructor Documentation

◆ Montgomery_Params() [1/2]

Botan::Montgomery_Params::Montgomery_Params ( const BigInt & p,
const Barrett_Reduction & mod_p )

Initialize a set of Montgomery reduction parameters. These values can be shared by all values in a specific Montgomery domain.

Definition at line 61 of file monty.cpp.

61 :
62 m_data(std::make_shared<Data>(p, mod_p)) {}
const BigInt & p() const
Definition monty.h:41

References p().

Referenced by Montgomery_Params(), operator!=(), and operator==().

◆ Montgomery_Params() [2/2]

Botan::Montgomery_Params::Montgomery_Params ( const BigInt & p)
explicit

Initialize a set of Montgomery reduction parameters. These values can be shared by all values in a specific Montgomery domain.

Definition at line 64 of file monty.cpp.

64 :
static Barrett_Reduction for_secret_modulus(const BigInt &m)
Definition barrett.cpp:23
Montgomery_Params(const BigInt &p, const Barrett_Reduction &mod_p)
Definition monty.cpp:61

References Montgomery_Params(), and p().

Member Function Documentation

◆ mul() [1/3]

void Botan::Montgomery_Params::mul ( BigInt & z,
const BigInt & x,
const BigInt & y,
secure_vector< word > & ws ) const

Definition at line 97 of file monty.cpp.

97 {
98 BOTAN_ARG_CHECK(&z != &x && &z != &y, "Montgomery_Params::mul output must not alias inputs");
99
100 const size_t p_size = this->p_words();
101
102 if(ws.size() < 2 * p_size) {
103 ws.resize(2 * p_size);
104 }
105
106 BOTAN_DEBUG_ASSERT(x.sig_words() <= p_size);
107 BOTAN_DEBUG_ASSERT(y.sig_words() <= p_size);
108
109 if(z.size() < 2 * p_size) {
110 z.grow_to(2 * p_size);
111 }
112
113 bigint_mul(z.mutable_data(),
114 z.size(),
115 x._data(),
116 x.size(),
117 std::min(p_size, x.size()),
118 y._data(),
119 y.size(),
120 std::min(p_size, y.size()),
121 ws.data(),
122 ws.size());
123
124 bigint_monty_redc_inplace(z.mutable_data(), this->p()._data(), p_size, this->p_dash(), ws.data(), ws.size());
125}
#define BOTAN_DEBUG_ASSERT(expr)
Definition assert.h:129
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:33
size_t p_words() const
Definition monty.h:51
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)
Definition mp_karat.cpp:283
void bigint_monty_redc_inplace(word z[], const word p[], size_t p_size, word p_dash, word ws[], size_t ws_size)
Definition mp_core.h:948

References Botan::BigInt::_data(), Botan::bigint_monty_redc_inplace(), Botan::bigint_mul(), BOTAN_ARG_CHECK, BOTAN_DEBUG_ASSERT, Botan::BigInt::grow_to(), Botan::BigInt::mutable_data(), p_words(), Botan::BigInt::sig_words(), and Botan::BigInt::size().

Referenced by Botan::Montgomery_Int::from_wide_int(), and mul().

◆ mul() [2/3]

void Botan::Montgomery_Params::mul ( BigInt & z,
const BigInt & x,
std::span< const word > y,
secure_vector< word > & ws ) const

Definition at line 127 of file monty.cpp.

127 {
128 BOTAN_ARG_CHECK(&z != &x, "Montgomery_Params::mul output must not alias x");
129 BOTAN_ARG_CHECK(!ranges_overlap(z._data(), z.size(), y.data(), y.size()),
130 "Montgomery_Params::mul output must not overlap y");
131
132 const size_t p_size = this->p_words();
133
134 if(ws.size() < 2 * p_size) {
135 ws.resize(2 * p_size);
136 }
137 if(z.size() < 2 * p_size) {
138 z.grow_to(2 * p_size);
139 }
140
141 BOTAN_DEBUG_ASSERT(x.sig_words() <= p_size);
142
143 bigint_mul(z.mutable_data(),
144 z.size(),
145 x._data(),
146 x.size(),
147 std::min(p_size, x.size()),
148 y.data(),
149 y.size(),
150 std::min(p_size, y.size()),
151 ws.data(),
152 ws.size());
153
154 bigint_monty_redc_inplace(z.mutable_data(), this->p()._data(), p_size, this->p_dash(), ws.data(), ws.size());
155}

References Botan::BigInt::_data(), Botan::bigint_monty_redc_inplace(), Botan::bigint_mul(), BOTAN_ARG_CHECK, BOTAN_DEBUG_ASSERT, Botan::BigInt::grow_to(), Botan::BigInt::mutable_data(), p_words(), Botan::BigInt::sig_words(), and Botan::BigInt::size().

◆ mul() [3/3]

BigInt Botan::Montgomery_Params::mul ( const BigInt & x,
const BigInt & y,
secure_vector< word > & ws ) const

Definition at line 90 of file monty.cpp.

90 {
91 const size_t p_size = this->p_words();
92 BigInt z = BigInt::with_capacity(2 * p_size);
93 this->mul(z, x, y, ws);
94 return z;
95}
static BigInt with_capacity(size_t n)
Definition bigint.cpp:51
void mul(BigInt &z, const BigInt &x, const BigInt &y, secure_vector< word > &ws) const
Definition monty.cpp:97

References mul(), p_words(), and Botan::BigInt::with_capacity().

◆ mul_by()

void Botan::Montgomery_Params::mul_by ( BigInt & x,
const BigInt & y,
secure_vector< word > & ws ) const

Definition at line 157 of file monty.cpp.

157 {
158 const size_t p_size = this->p_words();
159
160 if(ws.size() < 4 * p_size) {
161 ws.resize(4 * p_size);
162 }
163
164 word* z_data = ws.data();
165 word* ws_data = &ws[2 * p_size];
166
167 BOTAN_DEBUG_ASSERT(x.sig_words() <= p_size);
168
169 bigint_mul(z_data,
170 2 * p_size,
171 x._data(),
172 x.size(),
173 std::min(p_size, x.size()),
174 y._data(),
175 y.size(),
176 std::min(p_size, y.size()),
177 ws_data,
178 2 * p_size);
179
180 bigint_monty_redc_inplace(z_data, this->p()._data(), p_size, this->p_dash(), ws_data, 2 * p_size);
181
182 if(x.size() < 2 * p_size) {
183 x.grow_to(2 * p_size);
184 }
185 copy_mem(x.mutable_data(), z_data, 2 * p_size);
186}
word p_dash() const
Definition monty.h:49
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:144
std::conditional_t< HasNative64BitRegisters, std::uint64_t, uint32_t > word
The native machine word, used as the limb type for multiprecision integers.
Definition types.h:131

References Botan::BigInt::_data(), Botan::bigint_monty_redc_inplace(), Botan::bigint_mul(), BOTAN_DEBUG_ASSERT, Botan::copy_mem(), Botan::BigInt::grow_to(), Botan::BigInt::mutable_data(), p(), p_dash(), p_words(), Botan::BigInt::sig_words(), and Botan::BigInt::size().

◆ operator!=()

bool Botan::Montgomery_Params::operator!= ( const Montgomery_Params & other) const
inline

Definition at line 39 of file monty.h.

39{ return !((*this) == other); }

References Montgomery_Params().

◆ operator==()

bool Botan::Montgomery_Params::operator== ( const Montgomery_Params & other) const

Definition at line 67 of file monty.cpp.

67 {
68 if(this->m_data == other.m_data) {
69 return true;
70 }
71
72 return (this->m_data->p() == other.m_data->p());
73}

References Montgomery_Params().

◆ p()

const BigInt & Botan::Montgomery_Params::p ( ) const
inline

Definition at line 41 of file monty.h.

41{ return m_data->p(); }

Referenced by Montgomery_Params(), Montgomery_Params(), Botan::monty_precompute(), and mul_by().

◆ p_dash()

word Botan::Montgomery_Params::p_dash ( ) const
inline

Definition at line 49 of file monty.h.

49{ return m_data->p_dash(); }

Referenced by mul_by().

◆ p_words()

size_t Botan::Montgomery_Params::p_words ( ) const
inline

Definition at line 51 of file monty.h.

51{ return m_data->p_size(); }

Referenced by Botan::monty_multi_exp(), mul(), mul(), mul(), mul_by(), redc(), and sqr().

◆ R1()

const BigInt & Botan::Montgomery_Params::R1 ( ) const
inline

Definition at line 43 of file monty.h.

43{ return m_data->r1(); }

Referenced by Botan::Montgomery_Int::one().

◆ R2()

const BigInt & Botan::Montgomery_Params::R2 ( ) const
inline

Definition at line 45 of file monty.h.

45{ return m_data->r2(); }

◆ R3()

const BigInt & Botan::Montgomery_Params::R3 ( ) const
inline

Definition at line 47 of file monty.h.

47{ return m_data->r3(); }

Referenced by Botan::Montgomery_Int::from_wide_int().

◆ redc()

BigInt Botan::Montgomery_Params::redc ( const BigInt & x,
secure_vector< word > & ws ) const

Definition at line 75 of file monty.cpp.

75 {
76 const size_t p_size = this->p_words();
77
78 if(ws.size() < p_size) {
79 ws.resize(p_size);
80 }
81
82 BigInt z = x;
83 z.grow_to(2 * p_size);
84
85 bigint_monty_redc_inplace(z.mutable_data(), this->p()._data(), p_size, this->p_dash(), ws.data(), ws.size());
86
87 return z;
88}

References Botan::bigint_monty_redc_inplace(), Botan::BigInt::grow_to(), Botan::BigInt::mutable_data(), and p_words().

Referenced by Botan::Montgomery_Int::from_wide_int().

◆ sqr() [1/3]

void Botan::Montgomery_Params::sqr ( BigInt & z,
const BigInt & x,
secure_vector< word > & ws ) const

Definition at line 200 of file monty.cpp.

200 {
201 BOTAN_ARG_CHECK(&z != &x, "Montgomery_Params::sqr output must not alias input");
202 this->sqr(z, std::span{x._data(), x.size()}, ws);
203}
BigInt sqr(const BigInt &x, secure_vector< word > &ws) const
Definition monty.cpp:188

References Botan::BigInt::_data(), BOTAN_ARG_CHECK, Botan::BigInt::size(), and sqr().

◆ sqr() [2/3]

void Botan::Montgomery_Params::sqr ( BigInt & z,
std::span< const word > x,
secure_vector< word > & ws ) const

Definition at line 205 of file monty.cpp.

205 {
206 BOTAN_ARG_CHECK(!ranges_overlap(z._data(), z.size(), x.data(), x.size()),
207 "Montgomery_Params::sqr output must not overlap input");
208
209 const size_t p_size = this->p_words();
210
211 if(ws.size() < 2 * p_size) {
212 ws.resize(2 * p_size);
213 }
214
215 if(z.size() < 2 * p_size) {
216 z.grow_to(2 * p_size);
217 }
218
219 bigint_sqr(z.mutable_data(), z.size(), x.data(), x.size(), std::min(p_size, x.size()), ws.data(), ws.size());
220
221 bigint_monty_redc_inplace(z.mutable_data(), this->p()._data(), p_size, this->p_dash(), ws.data(), ws.size());
222}
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)
Definition mp_karat.cpp:327

References Botan::BigInt::_data(), Botan::bigint_monty_redc_inplace(), Botan::bigint_sqr(), BOTAN_ARG_CHECK, Botan::BigInt::grow_to(), Botan::BigInt::mutable_data(), p_words(), and Botan::BigInt::size().

◆ sqr() [3/3]

BigInt Botan::Montgomery_Params::sqr ( const BigInt & x,
secure_vector< word > & ws ) const

Definition at line 188 of file monty.cpp.

188 {
189 BOTAN_DEBUG_ASSERT(x.sig_words() <= this->p_words());
190 return this->sqr(std::span{x._data(), x.size()}, ws);
191}

References Botan::BigInt::_data(), BOTAN_DEBUG_ASSERT, Botan::BigInt::sig_words(), Botan::BigInt::size(), and sqr().

Referenced by sqr(), and sqr().


The documentation for this class was generated from the following files: