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

#include <monty.h>

Public Member Functions

void _const_time_poison () const
void _const_time_unpoison () const
 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 BigInt & p () const
size_t p_bits () const
word p_dash () const
word p_top_mu () const
size_t p_words () const
const BigInt & R1 () const
const BigInt & R2 () const
const BigInt & R3 () 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 67 of file monty.cpp.

67 :
68 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 70 of file monty.cpp.

70 :
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:67

References Montgomery_Params(), and p().

Member Function Documentation

◆ _const_time_poison()

void Botan::Montgomery_Params::_const_time_poison ( ) const
inline

Definition at line 60 of file monty.h.

60{ m_data->_const_time_poison(); }

◆ _const_time_unpoison()

void Botan::Montgomery_Params::_const_time_unpoison ( ) const
inline

Definition at line 62 of file monty.h.

62{ m_data->_const_time_unpoison(); }

◆ mul() [1/3]

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

Definition at line 103 of file monty.cpp.

103 {
104 BOTAN_ARG_CHECK(&z != &x && &z != &y, "Montgomery_Params::mul output must not alias inputs");
105
106 const size_t p_size = this->p_words();
107
108 if(ws.size() < 2 * p_size) {
109 ws.resize(2 * p_size);
110 }
111
112 BOTAN_DEBUG_ASSERT(x.sig_words() <= p_size);
113 BOTAN_DEBUG_ASSERT(y.sig_words() <= p_size);
114
115 if(z.size() < 2 * p_size) {
116 z.grow_to(2 * p_size);
117 }
118
119 bigint_mul(z.mutable_data(),
120 z.size(),
121 x._data(),
122 x.size(),
123 std::min(p_size, x.size()),
124 y._data(),
125 y.size(),
126 std::min(p_size, y.size()),
127 ws.data(),
128 ws.size());
129
130 bigint_monty_redc_inplace(z.mutable_data(), this->p()._data(), p_size, this->p_dash(), ws.data(), ws.size());
131}
#define BOTAN_DEBUG_ASSERT(expr)
Definition assert.h:136
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:33
size_t p_words() const
Definition monty.h:58
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:1111

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 133 of file monty.cpp.

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

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 96 of file monty.cpp.

96 {
97 const size_t p_size = this->p_words();
98 BigInt z = BigInt::with_capacity(2 * p_size);
99 this->mul(z, x, y, ws);
100 return z;
101}
static BigInt with_capacity(size_t n)
Definition bigint.cpp:52
void mul(BigInt &z, const BigInt &x, const BigInt &y, secure_vector< word > &ws) const
Definition monty.cpp:103

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 163 of file monty.cpp.

163 {
164 const size_t p_size = this->p_words();
165
166 if(ws.size() < 4 * p_size) {
167 ws.resize(4 * p_size);
168 }
169
170 word* z_data = ws.data();
171 word* ws_data = &ws[2 * p_size];
172
173 BOTAN_DEBUG_ASSERT(x.sig_words() <= p_size);
174
175 bigint_mul(z_data,
176 2 * p_size,
177 x._data(),
178 x.size(),
179 std::min(p_size, x.size()),
180 y._data(),
181 y.size(),
182 std::min(p_size, y.size()),
183 ws_data,
184 2 * p_size);
185
186 bigint_monty_redc_inplace(z_data, this->p()._data(), p_size, this->p_dash(), ws_data, 2 * p_size);
187
188 if(x.size() < 2 * p_size) {
189 x.grow_to(2 * p_size);
190 }
191 copy_mem(x.mutable_data(), z_data, 2 * p_size);
192}
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 73 of file monty.cpp.

73 {
74 if(this->m_data == other.m_data) {
75 return true;
76 }
77
78 return (this->m_data->p() == other.m_data->p());
79}

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 Botan::make_monty_exp_avx512(), Montgomery_Params(), Montgomery_Params(), Botan::monty_precompute(), and mul_by().

◆ p_bits()

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

Definition at line 56 of file monty.h.

56{ return m_data->p_bits(); }

Referenced by Botan::DL_Group::p_bits().

◆ 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_top_mu()

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

Return floor(2^(bits(p) - 1 + WB) / p) where WB is the word size in bits

Definition at line 54 of file monty.h.

54{ return m_data->p_top_mu(); }

◆ p_words()

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

Definition at line 58 of file monty.h.

58{ 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 81 of file monty.cpp.

81 {
82 const size_t p_size = this->p_words();
83
84 if(ws.size() < p_size) {
85 ws.resize(p_size);
86 }
87
88 BigInt z = x;
89 z.grow_to(2 * p_size);
90
91 bigint_monty_redc_inplace(z.mutable_data(), this->p()._data(), p_size, this->p_dash(), ws.data(), ws.size());
92
93 return z;
94}

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 206 of file monty.cpp.

206 {
207 BOTAN_ARG_CHECK(&z != &x, "Montgomery_Params::sqr output must not alias input");
208 this->sqr(z, std::span{x._data(), x.size()}, ws);
209}
BigInt sqr(const BigInt &x, secure_vector< word > &ws) const
Definition monty.cpp:194

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 211 of file monty.cpp.

211 {
212 BOTAN_ARG_CHECK(!ranges_overlap(z._data(), z.size(), x.data(), x.size()),
213 "Montgomery_Params::sqr output must not overlap input");
214
215 const size_t p_size = this->p_words();
216
217 if(ws.size() < 2 * p_size) {
218 ws.resize(2 * p_size);
219 }
220
221 if(z.size() < 2 * p_size) {
222 z.grow_to(2 * p_size);
223 }
224
225 bigint_sqr(z.mutable_data(), z.size(), x.data(), x.size(), std::min(p_size, x.size()), ws.data(), ws.size());
226
227 bigint_monty_redc_inplace(z.mutable_data(), this->p()._data(), p_size, this->p_dash(), ws.data(), ws.size());
228}
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 194 of file monty.cpp.

194 {
195 BOTAN_DEBUG_ASSERT(x.sig_words() <= this->p_words());
196 return this->sqr(std::span{x._data(), x.size()}, ws);
197}

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: