Botan 3.3.0
Crypto and TLS for C&
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
 
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 28 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 33 of file monty.h.

33: 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 253 of file monty.cpp.

255 :
256 m_params(params) {
257 if(redc_needed == false) {
258 m_v = v;
259 } else {
260 BOTAN_ASSERT_NOMSG(m_v < m_params->p());
261 secure_vector<word> ws;
262 m_v = m_params->mul(v, m_params->R2(), ws);
263 }
264}
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:59

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

269 :
270 m_params(params), m_v(bits, len) {
271 if(redc_needed) {
272 BOTAN_ASSERT_NOMSG(m_v < m_params->p());
273 secure_vector<word> ws;
274 m_v = m_params->mul(m_v, m_params->R2(), ws);
275 }
276}

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

281 :
282 m_params(std::move(params)) {
283 m_v.set_words(words, len);
284
285 if(redc_needed) {
286 BOTAN_ASSERT_NOMSG(m_v < m_params->p());
287 secure_vector<word> ws;
288 m_v = m_params->mul(m_v, m_params->R2(), ws);
289 }
290}
void set_words(const word w[], size_t len)
Definition bigint.h:522

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

348 {
349 m_v.mod_add(other.m_v, m_params->p(), ws);
350 return (*this);
351}
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 418 of file monty.cpp.

418 {
419 return Montgomery_Int(m_params, m_params->p()) - (*this);
420}
Montgomery_Int(std::shared_ptr< const Montgomery_Params > params)
Definition monty.h:33

References Montgomery_Int().

◆ const_time_poison()

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

Definition at line 122 of file monty.h.

122{ m_v.const_time_poison(); }
void const_time_poison() const
Definition bigint.h:720

◆ const_time_unpoison()

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

Definition at line 124 of file monty.h.

124{ return m_v.const_time_unpoison(); }
void const_time_unpoison() const
Definition bigint.h:722

◆ cube()

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

Definition at line 408 of file monty.cpp.

408 {
409 return Montgomery_Int(m_params, m_params->sqr(m_v, ws), false);
410}

References Montgomery_Int().

◆ fix_size()

void Botan::Montgomery_Int::fix_size ( )

Definition at line 292 of file monty.cpp.

292 {
293 const size_t p_words = m_params->p_words();
294
295 if(m_v.sig_words() > p_words) {
296 throw Internal_Error("Montgomery_Int::fix_size v too large");
297 }
298
299 m_v.grow_to(p_words);
300}
size_t sig_words() const
Definition bigint.h:584
void grow_to(size_t n) const
Definition bigint.h:631

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

◆ is_one()

bool Botan::Montgomery_Int::is_one ( ) const

Definition at line 316 of file monty.cpp.

316 {
317 return m_v == m_params->R1();
318}

◆ is_zero()

bool Botan::Montgomery_Int::is_zero ( ) const

Definition at line 320 of file monty.cpp.

320 {
321 return m_v.is_zero();
322}
bool is_zero() const
Definition bigint.h:428

References Botan::BigInt::is_zero().

◆ mul()

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

Definition at line 368 of file monty.cpp.

368 {
369 return Montgomery_Int(m_params, m_params->mul(m_v, other.m_v, ws), false);
370}

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

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

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

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

427 {
428 m_v.mod_mul(3, m_params->p(), ws);
429 return (*this);
430}

References Botan::BigInt::mod_mul().

◆ mul_by_4()

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

Definition at line 432 of file monty.cpp.

432 {
433 m_v.mod_mul(4, m_params->p(), ws);
434 return (*this);
435}

References Botan::BigInt::mod_mul().

◆ mul_by_8()

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

Definition at line 437 of file monty.cpp.

437 {
438 m_v.mod_mul(8, m_params->p(), ws);
439 return (*this);
440}

References Botan::BigInt::mod_mul().

◆ multiplicative_inverse()

Montgomery_Int Botan::Montgomery_Int::multiplicative_inverse ( ) const

Definition at line 412 of file monty.cpp.

412 {
413 secure_vector<word> ws;
414 const BigInt iv = m_params->mul(m_params->inv_mod_p(m_v), m_params->R3(), ws);
415 return Montgomery_Int(m_params, iv, false);
416}

References Montgomery_Int().

◆ operator!=()

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

Definition at line 58 of file monty.h.

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

◆ operator*()

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

Definition at line 363 of file monty.cpp.

363 {
364 secure_vector<word> ws;
365 return Montgomery_Int(m_params, m_params->mul(m_v, other.m_v, ws), false);
366}

References Montgomery_Int().

◆ operator*=() [1/2]

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

Definition at line 382 of file monty.cpp.

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

References mul_by().

◆ operator*=() [2/2]

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

Definition at line 387 of file monty.cpp.

387 {
388 secure_vector<word> ws;
389 return mul_by(other, ws);
390}

References mul_by().

◆ operator+()

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

Definition at line 329 of file monty.cpp.

329 {
330 secure_vector<word> ws;
331 BigInt z = m_v;
332 z.mod_add(other.m_v, m_params->p(), ws);
333 return Montgomery_Int(m_params, z, false);
334}

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

◆ operator+=()

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

Definition at line 343 of file monty.cpp.

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

References add().

◆ operator-()

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

Definition at line 336 of file monty.cpp.

336 {
337 secure_vector<word> ws;
338 BigInt z = m_v;
339 z.mod_sub(other.m_v, m_params->p(), ws);
340 return Montgomery_Int(m_params, z, false);
341}
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 353 of file monty.cpp.

353 {
354 secure_vector<word> ws;
355 return this->sub(other, ws);
356}
Montgomery_Int & sub(const Montgomery_Int &other, secure_vector< word > &ws)
Definition monty.cpp:358

References sub().

◆ operator==()

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

Definition at line 302 of file monty.cpp.

302 {
303 return m_v == other.m_v && m_params->p() == other.m_params->p();
304}

◆ repr()

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

Return the Montgomery representation

Definition at line 76 of file monty.h.

76{ return m_v; }

◆ serialize()

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

Definition at line 306 of file monty.cpp.

306 {
307 std::vector<uint8_t> v(size());
308 BigInt::encode_1363(v.data(), v.size(), value());
309 return v;
310}
static secure_vector< uint8_t > encode_1363(const BigInt &n, size_t bytes)
Definition big_code.cpp:105
size_t size() const
Definition monty.cpp:312
BigInt value() const
Definition monty.cpp:324

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

◆ size()

size_t Botan::Montgomery_Int::size ( ) const

Definition at line 312 of file monty.cpp.

312 {
313 return m_params->p().bytes();
314}

Referenced by serialize().

◆ square()

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

Definition at line 404 of file monty.cpp.

404 {
405 return Montgomery_Int(m_params, m_params->sqr(m_v, ws), false);
406}

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

399 {
400 m_params->square_this(m_v, ws);
401 return (*this);
402}

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

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

◆ sub()

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

Definition at line 358 of file monty.cpp.

358 {
359 m_v.mod_sub(other.m_v, m_params->p(), ws);
360 return (*this);
361}

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

324 {
325 secure_vector<word> ws;
326 return m_params->redc(m_v, ws);
327}

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


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