Botan 3.5.0
Crypto and TLS for C&
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
 
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)
 
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 26 of file monty.h.

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

Referenced by additive_inverse(), cube(), mul(), multiplicative_inverse(), 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 225 of file monty.cpp.

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

241 :
242 m_params(params), m_v(bits, len) {
243 if(redc_needed) {
244 BOTAN_ASSERT_NOMSG(m_v < m_params->p());
246 m_v = m_params->mul(m_v, m_params->R2(), ws);
247 }
248}

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

253 :
254 m_params(std::move(params)) {
255 m_v.set_words(words, len);
256
257 if(redc_needed) {
258 BOTAN_ASSERT_NOMSG(m_v < m_params->p());
260 m_v = m_params->mul(m_v, m_params->R2(), ws);
261 }
262}
void set_words(const word w[], size_t len)
Definition bigint.h:551

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

Member Function Documentation

◆ add()

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

Definition at line 318 of file monty.cpp.

318 {
319 m_v.mod_add(other.m_v, m_params->p(), ws);
320 return (*this);
321}
BigInt & mod_add(const BigInt &y, const BigInt &mod, secure_vector< word > &ws)
Definition big_ops2.cpp:45

References Botan::BigInt::mod_add().

Referenced by operator+=().

◆ additive_inverse()

Montgomery_Int Botan::Montgomery_Int::additive_inverse ( ) const

Definition at line 388 of file monty.cpp.

388 {
389 return Montgomery_Int(m_params, m_params->p()) - (*this);
390}
Montgomery_Int(std::shared_ptr< const Montgomery_Params > params)
Definition monty.h:26

References Montgomery_Int().

◆ const_time_poison()

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

Definition at line 115 of file monty.h.

115{ m_v.const_time_poison(); }
void const_time_poison() const
Definition bigint.h:807

◆ const_time_unpoison()

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

Definition at line 117 of file monty.h.

117{ return m_v.const_time_unpoison(); }
void const_time_unpoison() const
Definition bigint.h:809

◆ cube()

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

Definition at line 378 of file monty.cpp.

378 {
379 return Montgomery_Int(m_params, m_params->sqr(m_v, ws), false);
380}

References Montgomery_Int().

◆ fix_size()

void Botan::Montgomery_Int::fix_size ( )

Definition at line 264 of file monty.cpp.

264 {
265 const size_t p_words = m_params->p_words();
266
267 if(m_v.sig_words() > p_words) {
268 throw Internal_Error("Montgomery_Int::fix_size v too large");
269 }
270
271 m_v.grow_to(p_words);
272}
size_t sig_words() const
Definition bigint.h:615
void grow_to(size_t n) const
Definition bigint.h:666

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

◆ is_one()

bool Botan::Montgomery_Int::is_one ( ) const

Definition at line 286 of file monty.cpp.

286 {
287 return m_v == m_params->R1();
288}

◆ is_zero()

bool Botan::Montgomery_Int::is_zero ( ) const

Definition at line 290 of file monty.cpp.

290 {
291 return m_v.is_zero();
292}
bool is_zero() const
Definition bigint.h:457

References Botan::BigInt::is_zero().

◆ mul()

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

Definition at line 338 of file monty.cpp.

338 {
339 return Montgomery_Int(m_params, m_params->mul(m_v, other.m_v, ws), false);
340}

References Montgomery_Int().

Referenced by 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 342 of file monty.cpp.

342 {
343 m_params->mul_by(m_v, other.m_v, ws);
344 return (*this);
345}

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

347 {
348 m_params->mul_by(m_v, other, ws);
349 return (*this);
350}

◆ mul_by_2()

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

Definition at line 392 of file monty.cpp.

392 {
393 m_v.mod_mul(2, m_params->p(), ws);
394 return (*this);
395}
BigInt & mod_mul(uint8_t y, const BigInt &mod, secure_vector< word > &ws)
Definition big_ops2.cpp:119

References Botan::BigInt::mod_mul().

◆ mul_by_3()

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

Definition at line 397 of file monty.cpp.

397 {
398 m_v.mod_mul(3, m_params->p(), ws);
399 return (*this);
400}

References Botan::BigInt::mod_mul().

◆ mul_by_4()

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

Definition at line 402 of file monty.cpp.

402 {
403 m_v.mod_mul(4, m_params->p(), ws);
404 return (*this);
405}

References Botan::BigInt::mod_mul().

◆ mul_by_8()

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

Definition at line 407 of file monty.cpp.

407 {
408 m_v.mod_mul(8, m_params->p(), ws);
409 return (*this);
410}

References Botan::BigInt::mod_mul().

◆ multiplicative_inverse()

Montgomery_Int Botan::Montgomery_Int::multiplicative_inverse ( ) const

Definition at line 382 of file monty.cpp.

382 {
384 const BigInt iv = m_params->mul(m_params->inv_mod_p(m_v), m_params->R3(), ws);
385 return Montgomery_Int(m_params, iv, false);
386}

References Montgomery_Int().

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

333 {
335 return Montgomery_Int(m_params, m_params->mul(m_v, other.m_v, ws), false);
336}

References Montgomery_Int().

◆ operator*=() [1/2]

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

Definition at line 352 of file monty.cpp.

352 {
354 return mul_by(other, ws);
355}
Montgomery_Int & mul_by(const Montgomery_Int &other, secure_vector< word > &ws)
Definition monty.cpp:342

References mul_by().

◆ operator*=() [2/2]

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

Definition at line 357 of file monty.cpp.

357 {
359 return mul_by(other, ws);
360}

References mul_by().

◆ operator+()

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

Definition at line 299 of file monty.cpp.

299 {
301 BigInt z = m_v;
302 z.mod_add(other.m_v, m_params->p(), ws);
303 return Montgomery_Int(m_params, z, false);
304}

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

◆ operator+=()

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

Definition at line 313 of file monty.cpp.

313 {
315 return this->add(other, ws);
316}
Montgomery_Int & add(const Montgomery_Int &other, secure_vector< word > &ws)
Definition monty.cpp:318

References add().

◆ operator-()

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

Definition at line 306 of file monty.cpp.

306 {
308 BigInt z = m_v;
309 z.mod_sub(other.m_v, m_params->p(), ws);
310 return Montgomery_Int(m_params, z, false);
311}
BigInt & mod_sub(const BigInt &y, const BigInt &mod, secure_vector< word > &ws)
Definition big_ops2.cpp:90

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

◆ operator-=()

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

Definition at line 323 of file monty.cpp.

323 {
325 return this->sub(other, ws);
326}
Montgomery_Int & sub(const Montgomery_Int &other, secure_vector< word > &ws)
Definition monty.cpp:328

References sub().

◆ operator==()

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

Definition at line 274 of file monty.cpp.

274 {
275 return m_v == other.m_v && m_params->p() == other.m_params->p();
276}

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

278 {
279 return value().serialize();
280}
T serialize(size_t len) const
Definition bigint.h:711
BigInt value() const
Definition monty.cpp:294

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

◆ size()

size_t Botan::Montgomery_Int::size ( ) const

Definition at line 282 of file monty.cpp.

282 {
283 return m_params->p().bytes();
284}

◆ square()

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

Definition at line 374 of file monty.cpp.

374 {
375 return Montgomery_Int(m_params, m_params->sqr(m_v, ws), false);
376}

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

369 {
370 m_params->square_this(m_v, ws);
371 return (*this);
372}

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

362 {
363 for(size_t i = 0; i != n; ++i) {
364 m_params->square_this(m_v, ws);
365 }
366 return (*this);
367}

◆ sub()

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

Definition at line 328 of file monty.cpp.

328 {
329 m_v.mod_sub(other.m_v, m_params->p(), ws);
330 return (*this);
331}

References 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 294 of file monty.cpp.

294 {
296 return m_params->redc(m_v, ws);
297}

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


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