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

#include <monty.h>

Public Member Functions

Montgomery_Intadd (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_Intmul_by (const Montgomery_Int &other, secure_vector< word > &ws)
 
Montgomery_Intmul_by (const secure_vector< word > &other, secure_vector< word > &ws)
 
Montgomery_Intmul_by_2 (secure_vector< word > &ws)
 
Montgomery_Intmul_by_3 (secure_vector< word > &ws)
 
Montgomery_Intmul_by_4 (secure_vector< word > &ws)
 
Montgomery_Intmul_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_Intoperator*= (const Montgomery_Int &other)
 
Montgomery_Intoperator*= (const secure_vector< word > &other)
 
Montgomery_Int operator+ (const Montgomery_Int &other) const
 
Montgomery_Intoperator+= (const Montgomery_Int &other)
 
Montgomery_Int operator- (const Montgomery_Int &other) const
 
Montgomery_Intoperator-= (const Montgomery_Int &other)
 
bool operator== (const Montgomery_Int &other) const
 
const BigIntrepr () const
 
std::vector< uint8_t > serialize () const
 
size_t size () const
 
Montgomery_Int square (secure_vector< word > &ws) const
 
Montgomery_Intsquare_this (secure_vector< word > &ws)
 
Montgomery_Intsquare_this_n_times (secure_vector< word > &ws, size_t n)
 
Montgomery_Intsub (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

◆ Montgomery_Int() [1/4]

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) {}

◆ Montgomery_Int() [2/4]

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  }
#define BOTAN_ASSERT_NOMSG(expr)
Definition: assert.h:68

◆ Montgomery_Int() [3/4]

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  }
#define BOTAN_ASSERT_NOMSG(expr)
Definition: assert.h:68

◆ Montgomery_Int() [4/4]

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  }
#define BOTAN_ASSERT_NOMSG(expr)
Definition: assert.h:68

Member Function Documentation

◆ add()

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  }
BigInt & mod_add(const BigInt &y, const BigInt &mod, secure_vector< word > &ws)
Definition: big_ops2.cpp:50

◆ additive_inverse()

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  }
Montgomery_Int(std::shared_ptr< const Montgomery_Params > params)
Definition: monty.h:27

◆ const_time_poison()

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

Definition at line 118 of file monty.h.

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

◆ const_time_unpoison()

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

Definition at line 119 of file monty.h.

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

◆ fix_size()

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  }
secure_vector< word > & get_word_vector()
Definition: bigint.h:623
size_t sig_words() const
Definition: bigint.h:584

◆ is_one()

bool Botan::Montgomery_Int::is_one ( ) const

Definition at line 301 of file monty.cpp.

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

◆ is_zero()

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  }
bool is_zero() const
Definition: bigint.h:419

◆ mul()

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  }
Montgomery_Int(std::shared_ptr< const Montgomery_Params > params)
Definition: monty.h:27

◆ mul_by() [1/2]

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  }

◆ mul_by() [2/2]

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  }

◆ mul_by_2()

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  }
BigInt & mod_mul(uint8_t y, const BigInt &mod, secure_vector< word > &ws)
Definition: big_ops2.cpp:139

◆ mul_by_3()

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  }
BigInt & mod_mul(uint8_t y, const BigInt &mod, secure_vector< word > &ws)
Definition: big_ops2.cpp:139

◆ mul_by_4()

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  }
BigInt & mod_mul(uint8_t y, const BigInt &mod, secure_vector< word > &ws)
Definition: big_ops2.cpp:139

◆ mul_by_8()

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  }
BigInt & mod_mul(uint8_t y, const BigInt &mod, secure_vector< word > &ws)
Definition: big_ops2.cpp:139

◆ multiplicative_inverse()

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  }
Montgomery_Int(std::shared_ptr< const Montgomery_Params > params)
Definition: monty.h:27

◆ operator!=()

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); }

◆ operator*()

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  }
Montgomery_Int(std::shared_ptr< const Montgomery_Params > params)
Definition: monty.h:27

◆ operator*=() [1/2]

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  }
Montgomery_Int & mul_by(const Montgomery_Int &other, secure_vector< word > &ws)
Definition: monty.cpp:369

◆ operator*=() [2/2]

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  }
Montgomery_Int & mul_by(const Montgomery_Int &other, secure_vector< word > &ws)
Definition: monty.cpp:369

◆ operator+()

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  }
Montgomery_Int(std::shared_ptr< const Montgomery_Params > params)
Definition: monty.h:27
BigInt & mod_add(const BigInt &y, const BigInt &mod, secure_vector< word > &ws)
Definition: big_ops2.cpp:50

◆ operator+=()

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  }
Montgomery_Int & add(const Montgomery_Int &other, secure_vector< word > &ws)
Definition: monty.cpp:339

◆ operator-()

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  }
Montgomery_Int(std::shared_ptr< const Montgomery_Params > params)
Definition: monty.h:27
BigInt & mod_sub(const BigInt &y, const BigInt &mod, secure_vector< word > &ws)
Definition: big_ops2.cpp:93

◆ operator-=()

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  }
Montgomery_Int & sub(const Montgomery_Int &other, secure_vector< word > &ws)
Definition: monty.cpp:351

◆ operator==()

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  }

◆ repr()

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

Return the Montgomery representation

Definition at line 69 of file monty.h.

69 { return m_v; }

◆ serialize()

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  }
size_t size() const
Definition: monty.cpp:296
BigInt value() const
Definition: monty.cpp:311
static secure_vector< uint8_t > encode_1363(const BigInt &n, size_t bytes)
Definition: big_code.cpp:111

◆ size()

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  }

◆ square()

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  }
Montgomery_Int(std::shared_ptr< const Montgomery_Params > params)
Definition: monty.h:27

◆ square_this()

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  }

◆ square_this_n_times()

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  }

◆ sub()

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  }
BigInt & mod_sub(const BigInt &y, const BigInt &mod, secure_vector< word > &ws)
Definition: big_ops2.cpp:93

◆ value()

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: