Botan  2.11.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 222 of file monty.cpp.

References BOTAN_ASSERT_NOMSG.

224  :
225  m_params(params)
226  {
227  if(redc_needed == false)
228  {
229  m_v = v;
230  }
231  else
232  {
233  BOTAN_ASSERT_NOMSG(m_v < m_params->p());
234  secure_vector<word> ws;
235  m_v = m_params->mul(v, m_params->R2(), ws);
236  }
237  }
#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 239 of file monty.cpp.

References BOTAN_ASSERT_NOMSG.

241  :
242  m_params(params),
243  m_v(bits, len)
244  {
245  if(redc_needed)
246  {
247  BOTAN_ASSERT_NOMSG(m_v < m_params->p());
248  secure_vector<word> ws;
249  m_v = m_params->mul(m_v, m_params->R2(), ws);
250  }
251  }
#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 253 of file monty.cpp.

References BOTAN_ASSERT_NOMSG.

255  :
256  m_params(params),
257  m_v(words, len)
258  {
259  if(redc_needed)
260  {
261  BOTAN_ASSERT_NOMSG(m_v < m_params->p());
262  secure_vector<word> ws;
263  m_v = m_params->mul(m_v, m_params->R2(), ws);
264  }
265  }
#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 338 of file monty.cpp.

References Botan::BigInt::mod_add().

Referenced by operator+=().

339  {
340  m_v.mod_add(other.m_v, m_params->p(), ws);
341  return (*this);
342  }
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 419 of file monty.cpp.

References Montgomery_Int().

420  {
421  return Montgomery_Int(m_params, m_params->p()) - (*this);
422  }
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:737

◆ 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:738

◆ fix_size()

void Botan::Montgomery_Int::fix_size ( )

Definition at line 267 of file monty.cpp.

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

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

◆ is_one()

bool Botan::Montgomery_Int::is_one ( ) const

Definition at line 300 of file monty.cpp.

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

◆ is_zero()

bool Botan::Montgomery_Int::is_zero ( ) const

Definition at line 305 of file monty.cpp.

References Botan::BigInt::is_zero().

306  {
307  return m_v.is_zero();
308  }
bool is_zero() const
Definition: bigint.h:420

◆ mul()

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

Definition at line 362 of file monty.cpp.

References Montgomery_Int().

Referenced by Botan::monty_multi_exp().

364  {
365  return Montgomery_Int(m_params, m_params->mul(m_v, other.m_v, ws), false);
366  }
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 368 of file monty.cpp.

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

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

◆ mul_by() [2/2]

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

Definition at line 375 of file monty.cpp.

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

◆ mul_by_2()

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

Definition at line 424 of file monty.cpp.

References Botan::BigInt::mod_mul().

425  {
426  m_v.mod_mul(2, m_params->p(), ws);
427  return (*this);
428  }
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 430 of file monty.cpp.

References Botan::BigInt::mod_mul().

431  {
432  m_v.mod_mul(3, m_params->p(), ws);
433  return (*this);
434  }
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 436 of file monty.cpp.

References Botan::BigInt::mod_mul().

437  {
438  m_v.mod_mul(4, m_params->p(), ws);
439  return (*this);
440  }
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 442 of file monty.cpp.

References Botan::BigInt::mod_mul().

443  {
444  m_v.mod_mul(8, m_params->p(), ws);
445  return (*this);
446  }
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 412 of file monty.cpp.

References Montgomery_Int().

413  {
414  secure_vector<word> ws;
415  const BigInt iv = m_params->mul(m_params->inv_mod_p(m_v), m_params->R3(), ws);
416  return Montgomery_Int(m_params, iv, false);
417  }
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 356 of file monty.cpp.

References Montgomery_Int().

357  {
358  secure_vector<word> ws;
359  return Montgomery_Int(m_params, m_params->mul(m_v, other.m_v, ws), false);
360  }
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 382 of file monty.cpp.

References mul_by().

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

◆ operator*=() [2/2]

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

Definition at line 388 of file monty.cpp.

References mul_by().

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

◆ operator+()

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

Definition at line 316 of file monty.cpp.

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

317  {
318  secure_vector<word> ws;
319  BigInt z = m_v;
320  z.mod_add(other.m_v, m_params->p(), ws);
321  return Montgomery_Int(m_params, z, false);
322  }
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 332 of file monty.cpp.

References add().

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

◆ operator-()

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

Definition at line 324 of file monty.cpp.

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

325  {
326  secure_vector<word> ws;
327  BigInt z = m_v;
328  z.mod_sub(other.m_v, m_params->p(), ws);
329  return Montgomery_Int(m_params, z, false);
330  }
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 344 of file monty.cpp.

References sub().

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

◆ operator==()

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

Definition at line 283 of file monty.cpp.

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

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

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

289  {
290  std::vector<uint8_t> v(size());
291  BigInt::encode_1363(v.data(), v.size(), value());
292  return v;
293  }
size_t size() const
Definition: monty.cpp:295
BigInt value() const
Definition: monty.cpp:310
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 295 of file monty.cpp.

Referenced by serialize().

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

◆ square()

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

Definition at line 407 of file monty.cpp.

References Montgomery_Int().

Referenced by Botan::monty_multi_exp().

408  {
409  return Montgomery_Int(m_params, m_params->sqr(m_v, ws), false);
410  }
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 401 of file monty.cpp.

Referenced by Botan::monty_multi_exp().

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

◆ square_this_n_times()

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

Definition at line 394 of file monty.cpp.

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

◆ sub()

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

Definition at line 350 of file monty.cpp.

References Botan::BigInt::mod_sub().

Referenced by operator-=().

351  {
352  m_v.mod_sub(other.m_v, m_params->p(), ws);
353  return (*this);
354  }
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 310 of file monty.cpp.

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

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

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