Botan  2.11.0
Crypto and TLS for C++11
Public Member Functions | List of all members
Botan::Montgomery_Params Class Referencefinal

#include <monty.h>

Public Member Functions

BigInt inv_mod_p (const BigInt &x) const
 
 Montgomery_Params (const BigInt &p, const Modular_Reducer &mod_p)
 
 Montgomery_Params (const BigInt &p)
 
BigInt mul (const BigInt &x, const BigInt &y, secure_vector< word > &ws) const
 
BigInt mul (const BigInt &x, const secure_vector< word > &y, secure_vector< word > &ws) const
 
void mul_by (BigInt &x, const secure_vector< word > &y, secure_vector< word > &ws) const
 
void mul_by (BigInt &x, const BigInt &y, secure_vector< word > &ws) 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
 
BigInt sqr (const BigInt &x, secure_vector< word > &ws) const
 
void square_this (BigInt &x, secure_vector< word > &ws) const
 

Detailed Description

Parameters for Montgomery Reduction

Definition at line 129 of file monty.h.

Constructor & Destructor Documentation

◆ Montgomery_Params() [1/2]

Botan::Montgomery_Params::Montgomery_Params ( const BigInt p,
const Modular_Reducer 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 13 of file monty.cpp.

References Botan::BigInt::is_even(), Botan::monty_inverse(), Botan::Modular_Reducer::multiply(), p(), Botan::BigInt::power_of_2(), Botan::Modular_Reducer::reduce(), Botan::BigInt::sig_words(), Botan::Modular_Reducer::square(), and Botan::BigInt::word_at().

15  {
16  if(p.is_even() || p < 3)
17  throw Invalid_Argument("Montgomery_Params invalid modulus");
18 
19  m_p = p;
20  m_p_words = m_p.sig_words();
21  m_p_dash = monty_inverse(m_p.word_at(0));
22 
23  const BigInt r = BigInt::power_of_2(m_p_words * BOTAN_MP_WORD_BITS);
24 
25  m_r1 = mod_p.reduce(r);
26  m_r2 = mod_p.square(m_r1);
27  m_r3 = mod_p.multiply(m_r1, m_r2);
28  }
const BigInt & p() const
Definition: monty.h:144
bool is_even() const
Definition: bigint.h:402
word word_at(size_t n) const
Definition: bigint.h:511
static BigInt power_of_2(size_t n)
Definition: bigint.h:756
BigInt & square(secure_vector< word > &ws)
Definition: big_ops2.cpp:231
size_t sig_words() const
Definition: bigint.h:589
word monty_inverse(word a)
Definition: numthry.cpp:384

◆ Montgomery_Params() [2/2]

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

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

Definition at line 30 of file monty.cpp.

References Botan::BigInt::is_even(), Botan::BigInt::is_negative(), Botan::monty_inverse(), Botan::Modular_Reducer::multiply(), p(), Botan::BigInt::power_of_2(), Botan::Modular_Reducer::reduce(), Botan::BigInt::sig_words(), Botan::Modular_Reducer::square(), and Botan::BigInt::word_at().

31  {
32 
33  if(p.is_negative() || p.is_even())
34  throw Invalid_Argument("Montgomery_Params invalid modulus");
35 
36  m_p = p;
37  m_p_words = m_p.sig_words();
38  m_p_dash = monty_inverse(m_p.word_at(0));
39 
40  const BigInt r = BigInt::power_of_2(m_p_words * BOTAN_MP_WORD_BITS);
41 
42  Modular_Reducer mod_p(p);
43 
44  m_r1 = mod_p.reduce(r);
45  m_r2 = mod_p.square(m_r1);
46  m_r3 = mod_p.multiply(m_r1, m_r2);
47  }
bool is_negative() const
Definition: bigint.h:530
const BigInt & p() const
Definition: monty.h:144
bool is_even() const
Definition: bigint.h:402
word word_at(size_t n) const
Definition: bigint.h:511
static BigInt power_of_2(size_t n)
Definition: bigint.h:756
BigInt & square(secure_vector< word > &ws)
Definition: big_ops2.cpp:231
size_t sig_words() const
Definition: bigint.h:589
word monty_inverse(word a)
Definition: numthry.cpp:384

Member Function Documentation

◆ inv_mod_p()

BigInt Botan::Montgomery_Params::inv_mod_p ( const BigInt x) const

Definition at line 49 of file monty.cpp.

References Botan::ct_inverse_mod_odd_modulus(), and p().

50  {
51  return ct_inverse_mod_odd_modulus(x, p());
52  }
const BigInt & p() const
Definition: monty.h:144
BigInt ct_inverse_mod_odd_modulus(const BigInt &n, const BigInt &mod)
Definition: numthry.cpp:165

◆ mul() [1/2]

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

Definition at line 71 of file monty.cpp.

References Botan::bigint_monty_redc(), Botan::bigint_mul(), BOTAN_DEBUG_ASSERT, Botan::BigInt::data(), Botan::BigInt::Positive, Botan::BigInt::sig_words(), and Botan::BigInt::size().

73  {
74  const size_t output_size = 2*m_p_words + 2;
75 
76  if(ws.size() < output_size)
77  ws.resize(output_size);
78 
79  BOTAN_DEBUG_ASSERT(x.sig_words() <= m_p_words);
80  BOTAN_DEBUG_ASSERT(y.sig_words() <= m_p_words);
81 
82  BigInt z(BigInt::Positive, output_size);
83  bigint_mul(z.mutable_data(), z.size(),
84  x.data(), x.size(), std::min(m_p_words, x.size()),
85  y.data(), y.size(), std::min(m_p_words, y.size()),
86  ws.data(), ws.size());
87 
88  bigint_monty_redc(z.mutable_data(),
89  m_p.data(), m_p_words, m_p_dash,
90  ws.data(), ws.size());
91 
92  return z;
93  }
const word * data() const
Definition: bigint.h:623
#define BOTAN_DEBUG_ASSERT(expr)
Definition: assert.h:123
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:298
void bigint_monty_redc(word z[], const word p[], size_t p_size, word p_dash, word workspace[], size_t ws_size)
Definition: mp_monty.cpp:109

◆ mul() [2/2]

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

Definition at line 95 of file monty.cpp.

References Botan::bigint_monty_redc(), Botan::bigint_mul(), BOTAN_DEBUG_ASSERT, Botan::BigInt::data(), Botan::BigInt::mutable_data(), Botan::BigInt::Positive, Botan::BigInt::sig_words(), and Botan::BigInt::size().

98  {
99  const size_t output_size = 2*m_p_words + 2;
100  if(ws.size() < output_size)
101  ws.resize(output_size);
102  BigInt z(BigInt::Positive, output_size);
103 
104  BOTAN_DEBUG_ASSERT(x.sig_words() <= m_p_words);
105 
106  bigint_mul(z.mutable_data(), z.size(),
107  x.data(), x.size(), std::min(m_p_words, x.size()),
108  y.data(), y.size(), std::min(m_p_words, y.size()),
109  ws.data(), ws.size());
110 
111  bigint_monty_redc(z.mutable_data(),
112  m_p.data(), m_p_words, m_p_dash,
113  ws.data(), ws.size());
114 
115  return z;
116  }
const word * data() const
Definition: bigint.h:623
#define BOTAN_DEBUG_ASSERT(expr)
Definition: assert.h:123
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:298
void bigint_monty_redc(word z[], const word p[], size_t p_size, word p_dash, word workspace[], size_t ws_size)
Definition: mp_monty.cpp:109

◆ mul_by() [1/2]

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

Definition at line 118 of file monty.cpp.

References Botan::bigint_monty_redc(), Botan::bigint_mul(), BOTAN_DEBUG_ASSERT, Botan::copy_mem(), Botan::BigInt::data(), Botan::BigInt::grow_to(), Botan::BigInt::mutable_data(), Botan::BigInt::sig_words(), and Botan::BigInt::size().

121  {
122  const size_t output_size = 2*m_p_words + 2;
123 
124  if(ws.size() < 2*output_size)
125  ws.resize(2*output_size);
126 
127  word* z_data = &ws[0];
128  word* ws_data = &ws[output_size];
129 
130  BOTAN_DEBUG_ASSERT(x.sig_words() <= m_p_words);
131 
132  bigint_mul(z_data, output_size,
133  x.data(), x.size(), std::min(m_p_words, x.size()),
134  y.data(), y.size(), std::min(m_p_words, y.size()),
135  ws_data, output_size);
136 
137  bigint_monty_redc(z_data,
138  m_p.data(), m_p_words, m_p_dash,
139  ws_data, output_size);
140 
141  if(x.size() < output_size)
142  x.grow_to(output_size);
143  copy_mem(x.mutable_data(), z_data, output_size);
144  }
const word * data() const
Definition: bigint.h:623
#define BOTAN_DEBUG_ASSERT(expr)
Definition: assert.h:123
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:122
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:298
void bigint_monty_redc(word z[], const word p[], size_t p_size, word p_dash, word workspace[], size_t ws_size)
Definition: mp_monty.cpp:109

◆ mul_by() [2/2]

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

Definition at line 146 of file monty.cpp.

References Botan::bigint_monty_redc(), Botan::bigint_mul(), BOTAN_DEBUG_ASSERT, Botan::copy_mem(), Botan::BigInt::data(), Botan::BigInt::grow_to(), Botan::BigInt::mutable_data(), Botan::BigInt::sig_words(), and Botan::BigInt::size().

149  {
150  const size_t output_size = 2*m_p_words + 2;
151 
152  if(ws.size() < 2*output_size)
153  ws.resize(2*output_size);
154 
155  word* z_data = &ws[0];
156  word* ws_data = &ws[output_size];
157 
158  BOTAN_DEBUG_ASSERT(x.sig_words() <= m_p_words);
159 
160  bigint_mul(z_data, output_size,
161  x.data(), x.size(), std::min(m_p_words, x.size()),
162  y.data(), y.size(), std::min(m_p_words, y.size()),
163  ws_data, output_size);
164 
165  bigint_monty_redc(z_data,
166  m_p.data(), m_p_words, m_p_dash,
167  ws_data, output_size);
168 
169  if(x.size() < output_size)
170  x.grow_to(output_size);
171  copy_mem(x.mutable_data(), z_data, output_size);
172  }
const word * data() const
Definition: bigint.h:623
#define BOTAN_DEBUG_ASSERT(expr)
Definition: assert.h:123
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:122
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:298
void bigint_monty_redc(word z[], const word p[], size_t p_size, word p_dash, word workspace[], size_t ws_size)
Definition: mp_monty.cpp:109

◆ p()

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

Definition at line 144 of file monty.h.

Referenced by inv_mod_p(), and Montgomery_Params().

144 { return m_p; }

◆ p_dash()

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

Definition at line 149 of file monty.h.

149 { return m_p_dash; }

◆ p_words()

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

Definition at line 151 of file monty.h.

151 { return m_p_words; }

◆ R1()

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

Definition at line 145 of file monty.h.

145 { return m_r1; }

◆ R2()

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

Definition at line 146 of file monty.h.

146 { return m_r2; }

◆ R3()

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

Definition at line 147 of file monty.h.

147 { return m_r3; }

◆ redc()

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

Definition at line 54 of file monty.cpp.

References Botan::bigint_monty_redc(), Botan::BigInt::data(), Botan::BigInt::grow_to(), and Botan::BigInt::mutable_data().

55  {
56  const size_t output_size = 2*m_p_words + 2;
57 
58  if(ws.size() < output_size)
59  ws.resize(output_size);
60 
61  BigInt z = x;
62  z.grow_to(output_size);
63 
64  bigint_monty_redc(z.mutable_data(),
65  m_p.data(), m_p_words, m_p_dash,
66  ws.data(), ws.size());
67 
68  return z;
69  }
const word * data() const
Definition: bigint.h:623
void bigint_monty_redc(word z[], const word p[], size_t p_size, word p_dash, word workspace[], size_t ws_size)
Definition: mp_monty.cpp:109

◆ sqr()

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

Definition at line 174 of file monty.cpp.

References Botan::bigint_monty_redc(), Botan::bigint_sqr(), BOTAN_DEBUG_ASSERT, Botan::BigInt::data(), Botan::BigInt::mutable_data(), Botan::BigInt::Positive, Botan::BigInt::sig_words(), and Botan::BigInt::size().

175  {
176  const size_t output_size = 2*m_p_words + 2;
177 
178  if(ws.size() < output_size)
179  ws.resize(output_size);
180 
181  BigInt z(BigInt::Positive, output_size);
182 
183  BOTAN_DEBUG_ASSERT(x.sig_words() <= m_p_words);
184 
185  bigint_sqr(z.mutable_data(), z.size(),
186  x.data(), x.size(), std::min(m_p_words, x.size()),
187  ws.data(), ws.size());
188 
189  bigint_monty_redc(z.mutable_data(),
190  m_p.data(), m_p_words, m_p_dash,
191  ws.data(), ws.size());
192 
193  return z;
194  }
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:357
const word * data() const
Definition: bigint.h:623
#define BOTAN_DEBUG_ASSERT(expr)
Definition: assert.h:123
void bigint_monty_redc(word z[], const word p[], size_t p_size, word p_dash, word workspace[], size_t ws_size)
Definition: mp_monty.cpp:109

◆ square_this()

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

Definition at line 196 of file monty.cpp.

References Botan::bigint_monty_redc(), Botan::bigint_sqr(), BOTAN_DEBUG_ASSERT, Botan::copy_mem(), Botan::BigInt::data(), Botan::BigInt::grow_to(), Botan::BigInt::mutable_data(), Botan::BigInt::sig_words(), and Botan::BigInt::size().

198  {
199  const size_t output_size = 2*m_p_words + 2;
200 
201  if(ws.size() < 2*output_size)
202  ws.resize(2*output_size);
203 
204  word* z_data = &ws[0];
205  word* ws_data = &ws[output_size];
206 
207  BOTAN_DEBUG_ASSERT(x.sig_words() <= m_p_words);
208 
209  bigint_sqr(z_data, output_size,
210  x.data(), x.size(), std::min(m_p_words, x.size()),
211  ws_data, output_size);
212 
213  bigint_monty_redc(z_data,
214  m_p.data(), m_p_words, m_p_dash,
215  ws_data, output_size);
216 
217  if(x.size() < output_size)
218  x.grow_to(output_size);
219  copy_mem(x.mutable_data(), z_data, output_size);
220  }
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:357
const word * data() const
Definition: bigint.h:623
#define BOTAN_DEBUG_ASSERT(expr)
Definition: assert.h:123
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:122
void bigint_monty_redc(word z[], const word p[], size_t p_size, word p_dash, word workspace[], size_t ws_size)
Definition: mp_monty.cpp:109

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