Botan 3.11.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 46 of file monty.cpp.

46 :
47 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 49 of file monty.cpp.

49 :
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:46

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

82 {
83 const size_t p_size = this->p_words();
84
85 if(ws.size() < 2 * p_size) {
86 ws.resize(2 * p_size);
87 }
88
89 BOTAN_DEBUG_ASSERT(x.sig_words() <= p_size);
90 BOTAN_DEBUG_ASSERT(y.sig_words() <= p_size);
91
92 if(z.size() < 2 * p_size) {
93 z.grow_to(2 * p_size);
94 }
95
96 bigint_mul(z.mutable_data(),
97 z.size(),
98 x._data(),
99 x.size(),
100 std::min(p_size, x.size()),
101 y._data(),
102 y.size(),
103 std::min(p_size, y.size()),
104 ws.data(),
105 ws.size());
106
107 bigint_monty_redc_inplace(z.mutable_data(), this->p()._data(), p_size, this->p_dash(), ws.data(), ws.size());
108}
#define BOTAN_DEBUG_ASSERT(expr)
Definition assert.h:129
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:931

References Botan::BigInt::_data(), Botan::bigint_monty_redc_inplace(), Botan::bigint_mul(), 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 110 of file monty.cpp.

110 {
111 const size_t p_size = this->p_words();
112
113 if(ws.size() < 2 * p_size) {
114 ws.resize(2 * p_size);
115 }
116 if(z.size() < 2 * p_size) {
117 z.grow_to(2 * p_size);
118 }
119
120 BOTAN_DEBUG_ASSERT(x.sig_words() <= p_size);
121
122 bigint_mul(z.mutable_data(),
123 z.size(),
124 x._data(),
125 x.size(),
126 std::min(p_size, x.size()),
127 y.data(),
128 y.size(),
129 std::min(p_size, y.size()),
130 ws.data(),
131 ws.size());
132
133 bigint_monty_redc_inplace(z.mutable_data(), this->p()._data(), p_size, this->p_dash(), ws.data(), ws.size());
134}

References Botan::BigInt::_data(), Botan::bigint_monty_redc_inplace(), Botan::bigint_mul(), 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 75 of file monty.cpp.

75 {
76 const size_t p_size = this->p_words();
77 BigInt z = BigInt::with_capacity(2 * p_size);
78 this->mul(z, x, y, ws);
79 return z;
80}
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:82

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

136 {
137 const size_t p_size = this->p_words();
138
139 if(ws.size() < 4 * p_size) {
140 ws.resize(4 * p_size);
141 }
142
143 word* z_data = ws.data();
144 word* ws_data = &ws[2 * p_size];
145
146 BOTAN_DEBUG_ASSERT(x.sig_words() <= p_size);
147
148 bigint_mul(z_data,
149 2 * p_size,
150 x._data(),
151 x.size(),
152 std::min(p_size, x.size()),
153 y._data(),
154 y.size(),
155 std::min(p_size, y.size()),
156 ws_data,
157 2 * p_size);
158
159 bigint_monty_redc_inplace(z_data, this->p()._data(), p_size, this->p_dash(), ws_data, 2 * p_size);
160
161 if(x.size() < 2 * p_size) {
162 x.grow_to(2 * p_size);
163 }
164 copy_mem(x.mutable_data(), z_data, 2 * p_size);
165}
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
Definition types.h:119

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

52 {
53 if(this->m_data == other.m_data) {
54 return true;
55 }
56
57 return (this->m_data->p() == other.m_data->p());
58}

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

60 {
61 const size_t p_size = this->p_words();
62
63 if(ws.size() < p_size) {
64 ws.resize(p_size);
65 }
66
67 BigInt z = x;
68 z.grow_to(2 * p_size);
69
70 bigint_monty_redc_inplace(z.mutable_data(), this->p()._data(), p_size, this->p_dash(), ws.data(), ws.size());
71
72 return z;
73}

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

179 {
180 this->sqr(z, std::span{x._data(), x.size()}, ws);
181}
BigInt sqr(const BigInt &x, secure_vector< word > &ws) const
Definition monty.cpp:167

References Botan::BigInt::_data(), 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 183 of file monty.cpp.

183 {
184 const size_t p_size = this->p_words();
185
186 if(ws.size() < 2 * p_size) {
187 ws.resize(2 * p_size);
188 }
189
190 if(z.size() < 2 * p_size) {
191 z.grow_to(2 * p_size);
192 }
193
194 bigint_sqr(z.mutable_data(), z.size(), x.data(), x.size(), std::min(p_size, x.size()), ws.data(), ws.size());
195
196 bigint_monty_redc_inplace(z.mutable_data(), this->p()._data(), p_size, this->p_dash(), ws.data(), ws.size());
197}
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_monty_redc_inplace(), Botan::bigint_sqr(), 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 167 of file monty.cpp.

167 {
168 BOTAN_DEBUG_ASSERT(x.sig_words() <= this->p_words());
169 return this->sqr(std::span{x._data(), x.size()}, ws);
170}

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: