Botan 2.13.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- Class List
- Class Index
- Class Hierarchy
- Class Members

Public Member Functions | List of all members
Botan::Montgomery_Int Class Referencefinal

`#include <monty.h>`

## Public Member Functions
Montgomery_Int & | add (const Montgomery_Int &other, secure_vector< word > &ws)
Montgomery_Int | additive_inverse () const
void | const_time_poison () const
void | const_time_unpoison () const
void | fix_size ()
bool | is_one () const
bool | is_zero () const
| Montgomery_Int (std::shared_ptr< const Montgomery_Params > params)
| Montgomery_Int (std::shared_ptr< const Montgomery_Params > params, const BigInt &v, bool redc_needed=true)
| Montgomery_Int (std::shared_ptr< const Montgomery_Params > params, const uint8_t bits[], size_t len, bool redc_needed=true)
| Montgomery_Int (std::shared_ptr< const Montgomery_Params > params, const word words[], size_t len, bool redc_needed=true)
Montgomery_Int | mul (const Montgomery_Int &other, secure_vector< word > &ws) const
Montgomery_Int & | mul_by (const Montgomery_Int &other, secure_vector< word > &ws)
Montgomery_Int & | mul_by (const secure_vector< word > &other, secure_vector< word > &ws)
Montgomery_Int & | mul_by_2 (secure_vector< word > &ws)
Montgomery_Int & | mul_by_3 (secure_vector< word > &ws)
Montgomery_Int & | mul_by_4 (secure_vector< word > &ws)
Montgomery_Int & | mul_by_8 (secure_vector< word > &ws)
Montgomery_Int | multiplicative_inverse () const
bool | operator!= (const Montgomery_Int &other) const
Montgomery_Int | operator* (const Montgomery_Int &other) const
Montgomery_Int & | operator*= (const Montgomery_Int &other)
Montgomery_Int & | operator*= (const secure_vector< word > &other)
Montgomery_Int | operator+ (const Montgomery_Int &other) const
Montgomery_Int & | operator+= (const Montgomery_Int &other)
Montgomery_Int | operator- (const Montgomery_Int &other) const
Montgomery_Int & | operator-= (const Montgomery_Int &other)
bool | operator== (const Montgomery_Int &other) const
const BigInt & | repr () const
std::vector< uint8_t > | serialize () const
size_t | size () const
Montgomery_Int | square (secure_vector< word > &ws) const
Montgomery_Int & | square_this (secure_vector< word > &ws)
Montgomery_Int & | square_this_n_times (secure_vector< word > &ws, size_t n)
Montgomery_Int & | sub (const Montgomery_Int &other, secure_vector< word > &ws)
BigInt | value () const

## Detailed Description

The Montgomery representation of an integer

Definition at line 21 of file monty.h.

## Constructor & Destructor Documentation

Botan::Montgomery_Int::Montgomery_Int | ( | std::shared_ptr< const Montgomery_Params > | params | ) |
| inline

Create a zero-initialized Montgomery_Int

Definition at line 27 of file monty.h.

Referenced by additive_inverse(), mul(), multiplicative_inverse(), operator*(), operator+(), operator-(), and square().

27 : m_params(params) {}

Botan::Montgomery_Int::Montgomery_Int | ( | std::shared_ptr< const Montgomery_Params > | params,
|  | const BigInt & | v,
|  | bool | redc_needed = `true`
| ) |  |

Create a Montgomery_Int

Definition at line 223 of file monty.cpp.

References BOTAN_ASSERT_NOMSG.

225 :
226 m_params(params)
227 {
228 if(redc_needed == false)
229 {
230 m_v = v;
231 }
232 else
233 {
234 BOTAN_ASSERT_NOMSG(m_v < m_params->p());
235 secure_vector<word> ws;
236 m_v = m_params->mul(v, m_params->R2(), ws);
237 }
238 }
BOTAN_ASSERT_NOMSG
#define BOTAN_ASSERT_NOMSG(expr)
Definition: assert.h:68

Botan::Montgomery_Int::Montgomery_Int | ( | std::shared_ptr< const Montgomery_Params > | params,
|  | const uint8_t | bits[],
|  | size_t | len,
|  | bool | redc_needed = `true`
| ) |  |

Create a Montgomery_Int

Definition at line 240 of file monty.cpp.

References BOTAN_ASSERT_NOMSG.

242 :
243 m_params(params),
244 m_v(bits, len)
245 {
246 if(redc_needed)
247 {
248 BOTAN_ASSERT_NOMSG(m_v < m_params->p());
249 secure_vector<word> ws;
250 m_v = m_params->mul(m_v, m_params->R2(), ws);
251 }
252 }
BOTAN_ASSERT_NOMSG
#define BOTAN_ASSERT_NOMSG(expr)
Definition: assert.h:68

Botan::Montgomery_Int::Montgomery_Int | ( | std::shared_ptr< const Montgomery_Params > | params,
|  | const word | words[],
|  | size_t | len,
|  | bool | redc_needed = `true`
| ) |  |

Create a Montgomery_Int

Definition at line 254 of file monty.cpp.

References BOTAN_ASSERT_NOMSG.

256 :
257 m_params(params),
258 m_v(words, len)
259 {
260 if(redc_needed)
261 {
262 BOTAN_ASSERT_NOMSG(m_v < m_params->p());
263 secure_vector<word> ws;
264 m_v = m_params->mul(m_v, m_params->R2(), ws);
265 }
266 }
BOTAN_ASSERT_NOMSG
#define BOTAN_ASSERT_NOMSG(expr)
Definition: assert.h:68

## Member Function Documentation

Montgomery_Int & Botan::Montgomery_Int::add | ( | const Montgomery_Int & | other,
|  | secure_vector< word > & | ws
| ) |  |

Definition at line 339 of file monty.cpp.

References Botan::BigInt::mod_add().

Referenced by operator+=().

340 {
341 m_v.mod_add(other.m_v, m_params->p(), ws);
342 return (*this);
343 }
Botan::BigInt::mod_add
BigInt & mod_add(const BigInt &y, const BigInt &mod, secure_vector< word > &ws)
Definition: big_ops2.cpp:50

Montgomery_Int Botan::Montgomery_Int::additive_inverse | ( |  | ) | const

Definition at line 420 of file monty.cpp.

References Montgomery_Int().

421 {
422 return Montgomery_Int(m_params, m_params->p()) - (*this);
423 }
Botan::Montgomery_Int::Montgomery_Int
Montgomery_Int(std::shared_ptr< const Montgomery_Params > params)
Definition: monty.h:27

void Botan::Montgomery_Int::const_time_poison | ( |  | ) | const
| inline

Definition at line 118 of file monty.h.

118 { m_v.const_time_poison(); }
Botan::BigInt::const_time_poison
void const_time_poison() const
Definition: bigint.h:732

void Botan::Montgomery_Int::const_time_unpoison | ( |  | ) | const
| inline

Definition at line 119 of file monty.h.

119 { return m_v.const_time_unpoison(); }
Botan::BigInt::const_time_unpoison
void const_time_unpoison() const
Definition: bigint.h:733

void Botan::Montgomery_Int::fix_size | ( |  | ) |

Definition at line 268 of file monty.cpp.

References Botan::BigInt::get_word_vector(), and Botan::BigInt::sig_words().

269 {
270 const size_t p_words = m_params->p_words();
271
272 if(m_v.sig_words() > p_words)
273 throw Internal_Error("Montgomery_Int::fix_size v too large");
274
275 secure_vector<word>& w = m_v.get_word_vector();
276
277 if(w.size() != p_words)
278 {
279 w.resize(p_words);
280 w.shrink_to_fit();
281 }
282 }
Botan::BigInt::sig_words
size_t sig_words() const
Definition: bigint.h:584
Botan::BigInt::get_word_vector
secure_vector< word > & get_word_vector()
Definition: bigint.h:623
Botan::OCSP::Response_Status_Code::Internal_Error

bool Botan::Montgomery_Int::is_one | ( |  | ) | const

Definition at line 301 of file monty.cpp.

302 {
303 return m_v == m_params->R1();
304 }

bool Botan::Montgomery_Int::is_zero | ( |  | ) | const

Definition at line 306 of file monty.cpp.

References Botan::BigInt::is_zero().

307 {
308 return m_v.is_zero();
309 }
Botan::BigInt::is_zero
bool is_zero() const
Definition: bigint.h:419

Montgomery_Int Botan::Montgomery_Int::mul | ( | const Montgomery_Int & | other,
|  | secure_vector< word > & | ws
| ) |  | const

Definition at line 363 of file monty.cpp.

References Montgomery_Int().

Referenced by Botan::monty_multi_exp().

365 {
366 return Montgomery_Int(m_params, m_params->mul(m_v, other.m_v, ws), false);
367 }
Botan::Montgomery_Int::Montgomery_Int
Montgomery_Int(std::shared_ptr< const Montgomery_Params > params)
Definition: monty.h:27

Montgomery_Int & Botan::Montgomery_Int::mul_by | ( | const Montgomery_Int & | other,
|  | secure_vector< word > & | ws
| ) |  |

Definition at line 369 of file monty.cpp.

Referenced by Botan::monty_multi_exp(), and operator*=().

371 {
372 m_params->mul_by(m_v, other.m_v, ws);
373 return (*this);
374 }

Montgomery_Int & Botan::Montgomery_Int::mul_by | ( | const secure_vector< word > & | other,
|  | secure_vector< word > & | ws
| ) |  |

Definition at line 376 of file monty.cpp.

378 {
379 m_params->mul_by(m_v, other, ws);
380 return (*this);
381 }

Montgomery_Int & Botan::Montgomery_Int::mul_by_2 | ( | secure_vector< word > & | ws | ) |

Definition at line 425 of file monty.cpp.

References Botan::BigInt::mod_mul().

426 {
427 m_v.mod_mul(2, m_params->p(), ws);
428 return (*this);
429 }
Botan::BigInt::mod_mul
BigInt & mod_mul(uint8_t y, const BigInt &mod, secure_vector< word > &ws)
Definition: big_ops2.cpp:139

Montgomery_Int & Botan::Montgomery_Int::mul_by_3 | ( | secure_vector< word > & | ws | ) |

Definition at line 431 of file monty.cpp.

References Botan::BigInt::mod_mul().

432 {
433 m_v.mod_mul(3, m_params->p(), ws);
434 return (*this);
435 }
Botan::BigInt::mod_mul
BigInt & mod_mul(uint8_t y, const BigInt &mod, secure_vector< word > &ws)
Definition: big_ops2.cpp:139

Montgomery_Int & Botan::Montgomery_Int::mul_by_4 | ( | secure_vector< word > & | ws | ) |

Definition at line 437 of file monty.cpp.

References Botan::BigInt::mod_mul().

438 {
439 m_v.mod_mul(4, m_params->p(), ws);
440 return (*this);
441 }
Botan::BigInt::mod_mul
BigInt & mod_mul(uint8_t y, const BigInt &mod, secure_vector< word > &ws)
Definition: big_ops2.cpp:139

Montgomery_Int & Botan::Montgomery_Int::mul_by_8 | ( | secure_vector< word > & | ws | ) |

Definition at line 443 of file monty.cpp.

References Botan::BigInt::mod_mul().

444 {
445 m_v.mod_mul(8, m_params->p(), ws);
446 return (*this);
447 }
Botan::BigInt::mod_mul
BigInt & mod_mul(uint8_t y, const BigInt &mod, secure_vector< word > &ws)
Definition: big_ops2.cpp:139

Montgomery_Int Botan::Montgomery_Int::multiplicative_inverse | ( |  | ) | const

Definition at line 413 of file monty.cpp.

References Montgomery_Int().

414 {
415 secure_vector<word> ws;
416 const BigInt iv = m_params->mul(m_params->inv_mod_p(m_v), m_params->R3(), ws);
417 return Montgomery_Int(m_params, iv, false);
418 }
Botan::Montgomery_Int::Montgomery_Int
Montgomery_Int(std::shared_ptr< const Montgomery_Params > params)
Definition: monty.h:27

bool Botan::Montgomery_Int::operator!= | ( | const Montgomery_Int & | other | ) | const
| inline

Definition at line 51 of file monty.h.

51 { return (m_v != other.m_v); }

Montgomery_Int Botan::Montgomery_Int::operator* | ( | const Montgomery_Int & | other | ) | const

Definition at line 357 of file monty.cpp.

References Montgomery_Int().

358 {
359 secure_vector<word> ws;
360 return Montgomery_Int(m_params, m_params->mul(m_v, other.m_v, ws), false);
361 }
Botan::Montgomery_Int::Montgomery_Int
Montgomery_Int(std::shared_ptr< const Montgomery_Params > params)
Definition: monty.h:27

Montgomery_Int & Botan::Montgomery_Int::operator*= | ( | const Montgomery_Int & | other | ) |

Definition at line 383 of file monty.cpp.

References mul_by().

384 {
385 secure_vector<word> ws;
386 return mul_by(other, ws);
387 }
Botan::Montgomery_Int::mul_by
Montgomery_Int & mul_by(const Montgomery_Int &other, secure_vector< word > &ws)
Definition: monty.cpp:369

Montgomery_Int & Botan::Montgomery_Int::operator*= | ( | const secure_vector< word > & | other | ) |

Definition at line 389 of file monty.cpp.

References mul_by().

390 {
391 secure_vector<word> ws;
392 return mul_by(other, ws);
393 }
Botan::Montgomery_Int::mul_by
Montgomery_Int & mul_by(const Montgomery_Int &other, secure_vector< word > &ws)
Definition: monty.cpp:369

Montgomery_Int Botan::Montgomery_Int::operator+ | ( | const Montgomery_Int & | other | ) | const

Definition at line 317 of file monty.cpp.

References Botan::BigInt::mod_add(), and Montgomery_Int().

318 {
319 secure_vector<word> ws;
320 BigInt z = m_v;
321 z.mod_add(other.m_v, m_params->p(), ws);
322 return Montgomery_Int(m_params, z, false);
323 }
Botan::Montgomery_Int::Montgomery_Int
Montgomery_Int(std::shared_ptr< const Montgomery_Params > params)
Definition: monty.h:27
Botan::BigInt::mod_add
BigInt & mod_add(const BigInt &y, const BigInt &mod, secure_vector< word > &ws)
Definition: big_ops2.cpp:50

Montgomery_Int & Botan::Montgomery_Int::operator+= | ( | const Montgomery_Int & | other | ) |

Definition at line 333 of file monty.cpp.

References add().

334 {
335 secure_vector<word> ws;
336 return this->add(other, ws);
337 }
Botan::Montgomery_Int::add
Montgomery_Int & add(const Montgomery_Int &other, secure_vector< word > &ws)
Definition: monty.cpp:339

Montgomery_Int Botan::Montgomery_Int::operator- | ( | const Montgomery_Int & | other | ) | const

Definition at line 325 of file monty.cpp.

References Botan::BigInt::mod_sub(), and Montgomery_Int().

326 {
327 secure_vector<word> ws;
328 BigInt z = m_v;
329 z.mod_sub(other.m_v, m_params->p(), ws);
330 return Montgomery_Int(m_params, z, false);
331 }
Botan::Montgomery_Int::Montgomery_Int
Montgomery_Int(std::shared_ptr< const Montgomery_Params > params)
Definition: monty.h:27
Botan::BigInt::mod_sub
BigInt & mod_sub(const BigInt &y, const BigInt &mod, secure_vector< word > &ws)
Definition: big_ops2.cpp:93

Montgomery_Int & Botan::Montgomery_Int::operator-= | ( | const Montgomery_Int & | other | ) |

Definition at line 345 of file monty.cpp.

References sub().

346 {
347 secure_vector<word> ws;
348 return this->sub(other, ws);
349 }
Botan::Montgomery_Int::sub
Montgomery_Int & sub(const Montgomery_Int &other, secure_vector< word > &ws)
Definition: monty.cpp:351

bool Botan::Montgomery_Int::operator== | ( | const Montgomery_Int & | other | ) | const

Definition at line 284 of file monty.cpp.

285 {
286 return m_v == other.m_v && m_params->p() == other.m_params->p();
287 }

const BigInt& Botan::Montgomery_Int::repr | ( |  | ) | const
| inline

Return the Montgomery representation

Definition at line 69 of file monty.h.

69 { return m_v; }

std::vector< uint8_t > Botan::Montgomery_Int::serialize | ( |  | ) | const

Definition at line 289 of file monty.cpp.

References Botan::BigInt::encode_1363(), size(), and value().

290 {
291 std::vector<uint8_t> v(size());
292 BigInt::encode_1363(v.data(), v.size(), value());
293 return v;
294 }
Botan::Montgomery_Int::size
size_t size() const
Definition: monty.cpp:296
Botan::BigInt::encode_1363
static secure_vector< uint8_t > encode_1363(const BigInt &n, size_t bytes)
Definition: big_code.cpp:111
Botan::Montgomery_Int::value
BigInt value() const
Definition: monty.cpp:311

size_t Botan::Montgomery_Int::size | ( |  | ) | const

Definition at line 296 of file monty.cpp.

Referenced by serialize().

297 {
298 return m_params->p().bytes();
299 }

Montgomery_Int Botan::Montgomery_Int::square | ( | secure_vector< word > & | ws | ) | const

Definition at line 408 of file monty.cpp.

References Montgomery_Int().

Referenced by Botan::monty_multi_exp().

409 {
410 return Montgomery_Int(m_params, m_params->sqr(m_v, ws), false);
411 }
Botan::Montgomery_Int::Montgomery_Int
Montgomery_Int(std::shared_ptr< const Montgomery_Params > params)
Definition: monty.h:27

Montgomery_Int & Botan::Montgomery_Int::square_this | ( | secure_vector< word > & | ws | ) |

Definition at line 402 of file monty.cpp.

Referenced by Botan::monty_multi_exp().

403 {
404 m_params->square_this(m_v, ws);
405 return (*this);
406 }

Montgomery_Int & Botan::Montgomery_Int::square_this_n_times | ( | secure_vector< word > & | ws,
|  | size_t | n
| ) |  |

Definition at line 395 of file monty.cpp.

396 {
397 for(size_t i = 0; i != n; ++i)
398 m_params->square_this(m_v, ws);
399 return (*this);
400 }

Montgomery_Int & Botan::Montgomery_Int::sub | ( | const Montgomery_Int & | other,
|  | secure_vector< word > & | ws
| ) |  |

Definition at line 351 of file monty.cpp.

References Botan::BigInt::mod_sub().

Referenced by operator-=().

352 {
353 m_v.mod_sub(other.m_v, m_params->p(), ws);
354 return (*this);
355 }
Botan::BigInt::mod_sub
BigInt & mod_sub(const BigInt &y, const BigInt &mod, secure_vector< word > &ws)
Definition: big_ops2.cpp:93

BigInt Botan::Montgomery_Int::value | ( |  | ) | const

Return the value to normal mod-p space

Definition at line 311 of file monty.cpp.

Referenced by Botan::monty_multi_exp(), and serialize().

312 {
313 secure_vector<word> ws;
314 return m_params->redc(m_v, ws);
315 }

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

- src/lib/math/numbertheory/monty.h
- src/lib/math/numbertheory/monty.cpp