Botan 3.7.1
Crypto and TLS for C&
Botan::Montgomery_Int Class Referencefinal

#include <monty.h>

Public Member Functions

void _const_time_poison () const
 
void _const_time_unpoison () const
 
const std::shared_ptr< const Montgomery_Params > & _params () const
 
Montgomery_Intadd (const Montgomery_Int &other, secure_vector< word > &ws)
 
Montgomery_Int additive_inverse () const
 
Montgomery_Int cube (secure_vector< word > &ws) const
 
void fix_size ()
 
bool is_one () const
 
bool is_zero () const
 
 Montgomery_Int (const std::shared_ptr< const Montgomery_Params > &params, const BigInt &v, bool redc_needed=true)
 
 Montgomery_Int (const 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)
 
 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)
 
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
 

Static Public Member Functions

static Montgomery_Int from_wide_int (const std::shared_ptr< const Montgomery_Params > &params, const BigInt &x)
 
static Montgomery_Int one (const std::shared_ptr< const Montgomery_Params > &params)
 

Detailed Description

The Montgomery representation of an integer

Definition at line 23 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 28 of file monty.h.

28: m_params(std::move(params)) {}

Referenced by additive_inverse(), cube(), from_wide_int(), mul(), one(), operator*(), operator+(), operator-(), and square().

◆ Montgomery_Int() [2/4]

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

Create a Montgomery_Int

Definition at line 270 of file monty.cpp.

272 :
273 m_params(params) {
274 if(redc_needed == false) {
275 m_v = v;
276 } else {
277 BOTAN_ASSERT_NOMSG(m_v < m_params->p());
279 m_v = m_params->mul(v, m_params->R2(), ws);
280 }
281}
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:59
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61

References BOTAN_ASSERT_NOMSG.

◆ Montgomery_Int() [3/4]

Botan::Montgomery_Int::Montgomery_Int ( const 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 283 of file monty.cpp.

286 :
287 m_params(params), m_v(bits, len) {
288 if(redc_needed) {
289 BOTAN_ASSERT_NOMSG(m_v < m_params->p());
291 m_v = m_params->mul(m_v, m_params->R2(), ws);
292 }
293}

References BOTAN_ASSERT_NOMSG.

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

298 :
299 m_params(std::move(params)) {
300 m_v.set_words(words, len);
301
302 if(redc_needed) {
303 BOTAN_ASSERT_NOMSG(m_v < m_params->p());
305 m_v = m_params->mul(m_v, m_params->R2(), ws);
306 }
307}
void set_words(const word w[], size_t len)
Definition bigint.h:552

References BOTAN_ASSERT_NOMSG, and Botan::BigInt::set_words().

Member Function Documentation

◆ _const_time_poison()

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

Definition at line 122 of file monty.h.

122{ CT::poison(m_v); }
constexpr void poison(const T *p, size_t n)
Definition ct_utils.h:53

◆ _const_time_unpoison()

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

Definition at line 124 of file monty.h.

124{ CT::unpoison(m_v); }
constexpr void unpoison(const T *p, size_t n)
Definition ct_utils.h:64

◆ _params()

const std::shared_ptr< const Montgomery_Params > & Botan::Montgomery_Int::_params ( ) const
inline

Definition at line 126 of file monty.h.

126{ return m_params; }

◆ add()

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

Definition at line 362 of file monty.cpp.

362 {
363 BOTAN_STATE_CHECK(other.m_params == m_params);
364 m_v.mod_add(other.m_v, m_params->p(), ws);
365 return (*this);
366}
#define BOTAN_STATE_CHECK(expr)
Definition assert.h:41
BigInt & mod_add(const BigInt &y, const BigInt &mod, secure_vector< word > &ws)
Definition big_ops2.cpp:45

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

Referenced by operator+=().

◆ additive_inverse()

Montgomery_Int Botan::Montgomery_Int::additive_inverse ( ) const

Definition at line 433 of file monty.cpp.

433 {
434 return Montgomery_Int(m_params, m_params->p()) - (*this);
435}
Montgomery_Int(std::shared_ptr< const Montgomery_Params > params)
Definition monty.h:28

References Montgomery_Int().

◆ cube()

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

Definition at line 429 of file monty.cpp.

429 {
430 return Montgomery_Int(m_params, m_params->sqr(m_v, ws), false);
431}

References Montgomery_Int().

◆ fix_size()

void Botan::Montgomery_Int::fix_size ( )

Definition at line 309 of file monty.cpp.

309 {
310 const size_t p_words = m_params->p_words();
311 BOTAN_DEBUG_ASSERT(m_v.sig_words() <= p_words);
312 m_v.grow_to(p_words);
313}
#define BOTAN_DEBUG_ASSERT(expr)
Definition assert.h:98
size_t sig_words() const
Definition bigint.h:616
void grow_to(size_t n) const
Definition bigint.h:667

References BOTAN_DEBUG_ASSERT, Botan::BigInt::grow_to(), and Botan::BigInt::sig_words().

◆ from_wide_int()

Montgomery_Int Botan::Montgomery_Int::from_wide_int ( const std::shared_ptr< const Montgomery_Params > & params,
const BigInt & x )
static

Wide reduction - input can be at most 2*bytes long

Definition at line 262 of file monty.cpp.

262 {
263 //BOTAN_ARG_CHECK(x < params->p() * params->p(), "Input too large");
264
266 auto redc_x = params->mul(params->redc(x, ws), params->R3(), ws);
267 return Montgomery_Int(params, redc_x, false);
268}

References Montgomery_Int(), and mul().

◆ is_one()

bool Botan::Montgomery_Int::is_one ( ) const

Definition at line 327 of file monty.cpp.

327 {
328 return m_v == m_params->R1();
329}

◆ is_zero()

bool Botan::Montgomery_Int::is_zero ( ) const

Definition at line 331 of file monty.cpp.

331 {
332 return m_v.is_zero();
333}
bool is_zero() const
Definition bigint.h:458

References Botan::BigInt::is_zero().

◆ mul()

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

Definition at line 386 of file monty.cpp.

386 {
387 BOTAN_STATE_CHECK(other.m_params == m_params);
388 return Montgomery_Int(m_params, m_params->mul(m_v, other.m_v, ws), false);
389}

References BOTAN_STATE_CHECK, and Montgomery_Int().

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

◆ mul_by() [1/2]

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

Definition at line 391 of file monty.cpp.

391 {
392 BOTAN_STATE_CHECK(other.m_params == m_params);
393 m_params->mul_by(m_v, other.m_v, ws);
394 return (*this);
395}

References BOTAN_STATE_CHECK.

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

◆ mul_by() [2/2]

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

Definition at line 397 of file monty.cpp.

397 {
398 m_params->mul_by(m_v, other, ws);
399 return (*this);
400}

◆ mul_by_2()

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

Definition at line 437 of file monty.cpp.

437 {
438 m_v.mod_mul(2, 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:113

References Botan::BigInt::mod_mul().

◆ mul_by_3()

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

Definition at line 442 of file monty.cpp.

442 {
443 m_v.mod_mul(3, m_params->p(), ws);
444 return (*this);
445}

References Botan::BigInt::mod_mul().

◆ mul_by_4()

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

Definition at line 447 of file monty.cpp.

447 {
448 m_v.mod_mul(4, m_params->p(), ws);
449 return (*this);
450}

References Botan::BigInt::mod_mul().

◆ mul_by_8()

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

Definition at line 452 of file monty.cpp.

452 {
453 m_v.mod_mul(8, m_params->p(), ws);
454 return (*this);
455}

References Botan::BigInt::mod_mul().

◆ one()

Montgomery_Int Botan::Montgomery_Int::one ( const std::shared_ptr< const Montgomery_Params > & params)
static

Definition at line 258 of file monty.cpp.

258 {
259 return Montgomery_Int(params, params->R1(), false);
260}

References Montgomery_Int().

◆ operator!=()

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

Definition at line 60 of file monty.h.

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

◆ operator*()

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

Definition at line 380 of file monty.cpp.

380 {
381 BOTAN_STATE_CHECK(other.m_params == m_params);
383 return Montgomery_Int(m_params, m_params->mul(m_v, other.m_v, ws), false);
384}

References BOTAN_STATE_CHECK, and Montgomery_Int().

◆ operator*=() [1/2]

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

Definition at line 402 of file monty.cpp.

402 {
403 BOTAN_STATE_CHECK(other.m_params == m_params);
405 return mul_by(other, ws);
406}
Montgomery_Int & mul_by(const Montgomery_Int &other, secure_vector< word > &ws)
Definition monty.cpp:391

References BOTAN_STATE_CHECK, and mul_by().

◆ operator*=() [2/2]

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

Definition at line 408 of file monty.cpp.

408 {
410 return mul_by(other, ws);
411}

References mul_by().

◆ operator+()

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

Definition at line 340 of file monty.cpp.

340 {
341 BOTAN_STATE_CHECK(other.m_params == m_params);
343 BigInt z = m_v;
344 z.mod_add(other.m_v, m_params->p(), ws);
345 return Montgomery_Int(m_params, z, false);
346}

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

◆ operator+=()

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

Definition at line 356 of file monty.cpp.

356 {
357 BOTAN_STATE_CHECK(other.m_params == m_params);
359 return this->add(other, ws);
360}
Montgomery_Int & add(const Montgomery_Int &other, secure_vector< word > &ws)
Definition monty.cpp:362

References add(), and BOTAN_STATE_CHECK.

◆ operator-()

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

Definition at line 348 of file monty.cpp.

348 {
349 BOTAN_STATE_CHECK(other.m_params == m_params);
351 BigInt z = m_v;
352 z.mod_sub(other.m_v, m_params->p(), ws);
353 return Montgomery_Int(m_params, z, false);
354}
BigInt & mod_sub(const BigInt &y, const BigInt &mod, secure_vector< word > &ws)
Definition big_ops2.cpp:90

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

◆ operator-=()

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

Definition at line 368 of file monty.cpp.

368 {
369 BOTAN_STATE_CHECK(other.m_params == m_params);
371 return this->sub(other, ws);
372}
Montgomery_Int & sub(const Montgomery_Int &other, secure_vector< word > &ws)
Definition monty.cpp:374

References BOTAN_STATE_CHECK, and sub().

◆ operator==()

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

Definition at line 315 of file monty.cpp.

315 {
316 return m_v == other.m_v && m_params->p() == other.m_params->p();
317}

◆ repr()

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

Return the Montgomery representation

Definition at line 78 of file monty.h.

78{ return m_v; }

◆ serialize()

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

Definition at line 319 of file monty.cpp.

319 {
320 return value().serialize();
321}
T serialize(size_t len) const
Definition bigint.h:712
BigInt value() const
Definition monty.cpp:335

References Botan::BigInt::serialize(), and value().

◆ size()

size_t Botan::Montgomery_Int::size ( ) const

Definition at line 323 of file monty.cpp.

323 {
324 return m_params->p().bytes();
325}

◆ square()

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

Definition at line 425 of file monty.cpp.

425 {
426 return Montgomery_Int(m_params, m_params->sqr(m_v, ws), false);
427}

References Montgomery_Int().

Referenced by Botan::monty_multi_exp().

◆ square_this()

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

Definition at line 420 of file monty.cpp.

420 {
421 m_params->square_this(m_v, ws);
422 return (*this);
423}

Referenced by Botan::monty_multi_exp().

◆ square_this_n_times()

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

Definition at line 413 of file monty.cpp.

413 {
414 for(size_t i = 0; i != n; ++i) {
415 m_params->square_this(m_v, ws);
416 }
417 return (*this);
418}

◆ sub()

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

Definition at line 374 of file monty.cpp.

374 {
375 BOTAN_STATE_CHECK(other.m_params == m_params);
376 m_v.mod_sub(other.m_v, m_params->p(), ws);
377 return (*this);
378}

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

Referenced by operator-=().

◆ value()

BigInt Botan::Montgomery_Int::value ( ) const

Return the value to normal mod-p space

Definition at line 335 of file monty.cpp.

335 {
337 return m_params->redc(m_v, ws);
338}

Referenced by Botan::DL_Group::multi_exponentiate(), Botan::passes_miller_rabin_test(), Botan::power_mod(), serialize(), and Botan::sqrt_modulo_prime().


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