Botan 3.9.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 45 of file monty.cpp.

45 :
46 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 48 of file monty.cpp.

48 :
static Barrett_Reduction for_secret_modulus(const BigInt &m)
Definition barrett.cpp:22
Montgomery_Params(const BigInt &p, const Barrett_Reduction &mod_p)
Definition monty.cpp:45

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

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

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

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

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

74 {
75 const size_t p_size = this->p_words();
76 BigInt z = BigInt::with_capacity(2 * p_size);
77 this->mul(z, x, y, ws);
78 return z;
79}
static BigInt with_capacity(size_t n)
Definition bigint.cpp:50
void mul(BigInt &z, const BigInt &x, const BigInt &y, secure_vector< word > &ws) const
Definition monty.cpp:81

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

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

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

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

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

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

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

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

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

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

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: