Botan 3.4.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 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());
233 secure_vector<word> ws;
234 m_v = m_params->mul(v, m_params->R2(), ws);
235 }
236}
#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 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());
245 secure_vector<word> ws;
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());
259 secure_vector<word> ws;
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: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 320 of file monty.cpp.

320 {
321 m_v.mod_add(other.m_v, m_params->p(), ws);
322 return (*this);
323}
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 390 of file monty.cpp.

390 {
391 return Montgomery_Int(m_params, m_params->p()) - (*this);
392}
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:721

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

◆ cube()

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

Definition at line 380 of file monty.cpp.

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

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

288 {
289 return m_v == m_params->R1();
290}

◆ is_zero()

bool Botan::Montgomery_Int::is_zero ( ) const

Definition at line 292 of file monty.cpp.

292 {
293 return m_v.is_zero();
294}
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 340 of file monty.cpp.

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

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

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

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

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

◆ mul_by_2()

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

Definition at line 394 of file monty.cpp.

394 {
395 m_v.mod_mul(2, m_params->p(), ws);
396 return (*this);
397}
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 399 of file monty.cpp.

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

References Botan::BigInt::mod_mul().

◆ mul_by_4()

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

Definition at line 404 of file monty.cpp.

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

References Botan::BigInt::mod_mul().

◆ mul_by_8()

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

Definition at line 409 of file monty.cpp.

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

References Botan::BigInt::mod_mul().

◆ multiplicative_inverse()

Montgomery_Int Botan::Montgomery_Int::multiplicative_inverse ( ) const

Definition at line 384 of file monty.cpp.

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

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

335 {
336 secure_vector<word> ws;
337 return Montgomery_Int(m_params, m_params->mul(m_v, other.m_v, ws), false);
338}

References Montgomery_Int().

◆ operator*=() [1/2]

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

Definition at line 354 of file monty.cpp.

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

References mul_by().

◆ operator*=() [2/2]

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

Definition at line 359 of file monty.cpp.

359 {
360 secure_vector<word> ws;
361 return mul_by(other, ws);
362}

References mul_by().

◆ operator+()

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

Definition at line 301 of file monty.cpp.

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

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

◆ operator+=()

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

Definition at line 315 of file monty.cpp.

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

References add().

◆ operator-()

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

Definition at line 308 of file monty.cpp.

308 {
309 secure_vector<word> ws;
310 BigInt z = m_v;
311 z.mod_sub(other.m_v, m_params->p(), ws);
312 return Montgomery_Int(m_params, z, false);
313}
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 325 of file monty.cpp.

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

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 std::vector<uint8_t> v(size());
280 BigInt::encode_1363(v.data(), v.size(), value());
281 return v;
282}
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:284
BigInt value() const
Definition monty.cpp:296

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

◆ size()

size_t Botan::Montgomery_Int::size ( ) const

Definition at line 284 of file monty.cpp.

284 {
285 return m_params->p().bytes();
286}

Referenced by serialize().

◆ square()

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

Definition at line 376 of file monty.cpp.

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

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

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

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

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

◆ sub()

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

Definition at line 330 of file monty.cpp.

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

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

296 {
297 secure_vector<word> ws;
298 return m_params->redc(m_v, ws);
299}

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


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