Botan 3.10.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 44 of file monty.cpp.

44 :
45 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 47 of file monty.cpp.

47 :
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:44

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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: