Botan 2.19.1
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
 
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 22 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(params) {}

Referenced by additive_inverse(), mul(), multiplicative_inverse(), operator*(), operator+(), operator-(), and square().

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

226 :
227 m_params(params)
228 {
229 if(redc_needed == false)
230 {
231 m_v = v;
232 }
233 else
234 {
235 BOTAN_ASSERT_NOMSG(m_v < m_params->p());
236 secure_vector<word> ws;
237 m_v = m_params->mul(v, m_params->R2(), ws);
238 }
239 }
#define BOTAN_ASSERT_NOMSG(expr)
Definition: assert.h:68

References BOTAN_ASSERT_NOMSG.

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

243 :
244 m_params(params),
245 m_v(bits, len)
246 {
247 if(redc_needed)
248 {
249 BOTAN_ASSERT_NOMSG(m_v < m_params->p());
250 secure_vector<word> ws;
251 m_v = m_params->mul(m_v, m_params->R2(), ws);
252 }
253 }

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

257 :
258 m_params(params),
259 m_v(words, len)
260 {
261 if(redc_needed)
262 {
263 BOTAN_ASSERT_NOMSG(m_v < m_params->p());
264 secure_vector<word> ws;
265 m_v = m_params->mul(m_v, m_params->R2(), ws);
266 }
267 }

References BOTAN_ASSERT_NOMSG.

Member Function Documentation

◆ add()

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

Definition at line 334 of file monty.cpp.

335 {
336 m_v.mod_add(other.m_v, m_params->p(), ws);
337 return (*this);
338 }
BigInt & mod_add(const BigInt &y, const BigInt &mod, secure_vector< word > &ws)
Definition: big_ops2.cpp:50

References Botan::BigInt::mod_add().

Referenced by operator+=().

◆ additive_inverse()

Montgomery_Int Botan::Montgomery_Int::additive_inverse ( ) const

Definition at line 415 of file monty.cpp.

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

References Montgomery_Int().

◆ const_time_poison()

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

Definition at line 119 of file monty.h.

119{ m_v.const_time_poison(); }
void const_time_poison() const
Definition: bigint.h:739

◆ const_time_unpoison()

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

Definition at line 120 of file monty.h.

120{ return m_v.const_time_unpoison(); }
void const_time_unpoison() const
Definition: bigint.h:740

◆ fix_size()

void Botan::Montgomery_Int::fix_size ( )

Definition at line 269 of file monty.cpp.

270 {
271 const size_t p_words = m_params->p_words();
272
273 if(m_v.sig_words() > p_words)
274 throw Internal_Error("Montgomery_Int::fix_size v too large");
275
276 m_v.grow_to(p_words);
277 }
size_t sig_words() const
Definition: bigint.h:586
void grow_to(size_t n) const
Definition: bigint.h:636

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

◆ is_one()

bool Botan::Montgomery_Int::is_one ( ) const

Definition at line 296 of file monty.cpp.

297 {
298 return m_v == m_params->R1();
299 }

◆ is_zero()

bool Botan::Montgomery_Int::is_zero ( ) const

Definition at line 301 of file monty.cpp.

302 {
303 return m_v.is_zero();
304 }
bool is_zero() const
Definition: bigint.h:421

References Botan::BigInt::is_zero().

◆ mul()

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

Definition at line 358 of file monty.cpp.

360 {
361 return Montgomery_Int(m_params, m_params->mul(m_v, other.m_v, ws), false);
362 }

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

366 {
367 m_params->mul_by(m_v, other.m_v, ws);
368 return (*this);
369 }

Referenced by Botan::monty_multi_exp(), 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 371 of file monty.cpp.

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

◆ mul_by_2()

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

Definition at line 420 of file monty.cpp.

421 {
422 m_v.mod_mul(2, m_params->p(), ws);
423 return (*this);
424 }
BigInt & mod_mul(uint8_t y, const BigInt &mod, secure_vector< word > &ws)
Definition: big_ops2.cpp:120

References Botan::BigInt::mod_mul().

◆ mul_by_3()

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

Definition at line 426 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.

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

References Botan::BigInt::mod_mul().

◆ mul_by_8()

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

Definition at line 438 of file monty.cpp.

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

References Botan::BigInt::mod_mul().

◆ multiplicative_inverse()

Montgomery_Int Botan::Montgomery_Int::multiplicative_inverse ( ) const

Definition at line 408 of file monty.cpp.

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

References Montgomery_Int().

◆ operator!=()

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

Definition at line 52 of file monty.h.

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

◆ operator*()

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

Definition at line 352 of file monty.cpp.

353 {
354 secure_vector<word> ws;
355 return Montgomery_Int(m_params, m_params->mul(m_v, other.m_v, ws), false);
356 }

References Montgomery_Int().

◆ operator*=() [1/2]

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

Definition at line 378 of file monty.cpp.

379 {
380 secure_vector<word> ws;
381 return mul_by(other, ws);
382 }
Montgomery_Int & mul_by(const Montgomery_Int &other, secure_vector< word > &ws)
Definition: monty.cpp:364

References mul_by().

◆ operator*=() [2/2]

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

Definition at line 384 of file monty.cpp.

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

References mul_by().

◆ operator+()

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

Definition at line 312 of file monty.cpp.

313 {
314 secure_vector<word> ws;
315 BigInt z = m_v;
316 z.mod_add(other.m_v, m_params->p(), ws);
317 return Montgomery_Int(m_params, z, false);
318 }

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

◆ operator+=()

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

Definition at line 328 of file monty.cpp.

329 {
330 secure_vector<word> ws;
331 return this->add(other, ws);
332 }
Montgomery_Int & add(const Montgomery_Int &other, secure_vector< word > &ws)
Definition: monty.cpp:334

References add().

◆ operator-()

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

Definition at line 320 of file monty.cpp.

321 {
322 secure_vector<word> ws;
323 BigInt z = m_v;
324 z.mod_sub(other.m_v, m_params->p(), ws);
325 return Montgomery_Int(m_params, z, false);
326 }
BigInt & mod_sub(const BigInt &y, const BigInt &mod, secure_vector< word > &ws)
Definition: big_ops2.cpp:93

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

◆ operator-=()

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

Definition at line 340 of file monty.cpp.

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

References sub().

◆ operator==()

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

Definition at line 279 of file monty.cpp.

280 {
281 return m_v == other.m_v && m_params->p() == other.m_params->p();
282 }

◆ repr()

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

Return the Montgomery representation

Definition at line 70 of file monty.h.

70{ return m_v; }

◆ serialize()

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

Definition at line 284 of file monty.cpp.

285 {
286 std::vector<uint8_t> v(size());
287 BigInt::encode_1363(v.data(), v.size(), value());
288 return v;
289 }
static secure_vector< uint8_t > encode_1363(const BigInt &n, size_t bytes)
Definition: big_code.cpp:111
size_t size() const
Definition: monty.cpp:291
BigInt value() const
Definition: monty.cpp:306

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

◆ size()

size_t Botan::Montgomery_Int::size ( ) const

Definition at line 291 of file monty.cpp.

292 {
293 return m_params->p().bytes();
294 }

Referenced by serialize().

◆ square()

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

Definition at line 403 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 397 of file monty.cpp.

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

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

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

◆ sub()

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

Definition at line 346 of file monty.cpp.

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

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

307 {
308 secure_vector<word> ws;
309 return m_params->redc(m_v, ws);
310 }

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


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