Botan 2.1.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- Class List
- Class Index
- Class Hierarchy
- Class Members

Public Member Functions | List of all members
Botan::Montgomery_Exponentiator Class Reference

`#include <def_powm.h>`

Inheritance diagram for Botan::Montgomery_Exponentiator:

## Public Member Functions
Modular_Exponentiator * | copy () const override
BigInt | execute () const override
| Montgomery_Exponentiator (const BigInt &, Power_Mod::Usage_Hints)
void | set_base (const BigInt &) override
void | set_exponent (const BigInt &) override

## Detailed Description

Montgomery Exponentiator

Definition at line 42 of file def_powm.h.

## Constructor & Destructor Documentation

Botan::Montgomery_Exponentiator::Montgomery_Exponentiator | ( | const BigInt & | mod,
|  | Power_Mod::Usage_Hints | hints
| ) |  |

Definition at line 109 of file powm_mnt.cpp.

References Botan::BigInt::is_even(), Botan::BigInt::is_positive(), Botan::monty_inverse(), Botan::BigInt::power_of_2(), Botan::Modular_Reducer::reduce(), Botan::Modular_Reducer::square(), and Botan::BigInt::word_at().

Referenced by copy().

110 :
111 m_modulus(mod),
112 m_reducer(m_modulus),
113 m_mod_words(m_modulus.sig_words()),
114 m_window_bits(1),
115 m_hints(hints)
116 {
117 // Montgomery reduction only works for positive odd moduli
118 if(!m_modulus.is_positive() || m_modulus.is_even())
119 throw Invalid_Argument("Montgomery_Exponentiator: invalid modulus");
120
121 m_mod_prime = monty_inverse(mod.word_at(0));
122
123 const BigInt r = BigInt::power_of_2(m_mod_words * BOTAN_MP_WORD_BITS);
124 m_R_mod = m_reducer.reduce(r);
125 m_R2_mod = m_reducer.square(m_R_mod);
126 m_exp_bits = 0;
127 }
Botan::BigInt::sig_words
size_t sig_words() const
Definition: bigint.h:393
Botan::BigInt::is_even
bool is_even() const
Definition: bigint.h:232
Botan::BigInt::power_of_2
static BigInt power_of_2(size_t n)
Definition: bigint.h:492
Botan::Modular_Reducer::reduce
BigInt reduce(const BigInt &x) const
Definition: reducer.cpp:32
Botan::BigInt::is_positive
bool is_positive() const
Definition: bigint.h:354
Botan::monty_inverse
word monty_inverse(word input)
Definition: numthry.cpp:325
Botan::Modular_Reducer::square
BigInt square(const BigInt &x) const
Definition: reducer.h:39

## Member Function Documentation

Modular_Exponentiator* Botan::Montgomery_Exponentiator::copy | ( |  | ) | const
| inline override virtual

Implements Botan::Modular_Exponentiator.

Definition at line 49 of file def_powm.h.

References Montgomery_Exponentiator().

50 { return new Montgomery_Exponentiator(*this); }
Botan::Montgomery_Exponentiator::Montgomery_Exponentiator
Montgomery_Exponentiator(const BigInt &, Power_Mod::Usage_Hints)
Definition: powm_mnt.cpp:109

BigInt Botan::Montgomery_Exponentiator::execute | ( |  | ) | const
| override virtual

Implements Botan::Modular_Exponentiator.

Definition at line 67 of file powm_mnt.cpp.

References Botan::bigint_monty_mul(), Botan::bigint_monty_redc(), Botan::bigint_monty_sqr(), Botan::BigInt::data(), Botan::BigInt::get_substring(), Botan::BigInt::grow_to(), Botan::BigInt::mutable_data(), Botan::BigInt::Positive, and Botan::BigInt::size().

68 {
69 const size_t exp_nibbles = (m_exp_bits + m_window_bits - 1) / m_window_bits;
70
71 BigInt x = m_R_mod;
72
73 const size_t z_size = 2*(m_mod_words + 1);
74
75 BigInt z(BigInt::Positive, z_size);
76 secure_vector<word> workspace(z.size());
77
78 for(size_t i = exp_nibbles; i > 0; --i)
79 {
80 for(size_t k = 0; k != m_window_bits; ++k)
81 {
82 bigint_monty_sqr(z, x, m_modulus.data(), m_mod_words, m_mod_prime,
83 workspace.data());
84
85 x = z;
86 }
87
88 const uint32_t nibble = m_exp.get_substring(m_window_bits*(i-1), m_window_bits);
89
90 bigint_monty_mul(z, x, m_g[nibble],
91 m_modulus.data(), m_mod_words, m_mod_prime,
92 workspace.data());
93
94 x = z;
95 }
96
97 x.grow_to(2*m_mod_words + 1);
98
99 bigint_monty_redc(x.mutable_data(),
100 m_modulus.data(), m_mod_words, m_mod_prime,
101 workspace.data());
102
103 return x;
104 }
Botan::BigInt::Positive
Definition: bigint.h:34
Botan::bigint_monty_sqr
void bigint_monty_sqr(BigInt &z, const BigInt &x, const word p[], size_t p_size, word p_dash, word workspace[])
Definition: mp_monty.cpp:109
Botan::bigint_monty_mul
void bigint_monty_mul(BigInt &z, const BigInt &x, const BigInt &y, const word p[], size_t p_size, word p_dash, word workspace[])
Definition: mp_monty.cpp:97
Botan::BigInt::data
const word * data() const
Definition: bigint.h:425
Botan::BigInt::get_substring
uint32_t get_substring(size_t offset, size_t length) const
Definition: bigint.cpp:120
Botan::bigint_monty_redc
void bigint_monty_redc(word z[], const word p[], size_t p_size, word p_dash, word workspace[])
Definition: mp_monty.cpp:22

void Botan::Montgomery_Exponentiator::set_base | ( | const BigInt & | base | ) |
| override virtual

Implements Botan::Modular_Exponentiator.

Definition at line 27 of file powm_mnt.cpp.

References Botan::bigint_monty_mul(), Botan::BigInt::bits(), Botan::BigInt::data(), Botan::BigInt::Positive, Botan::Modular_Reducer::reduce(), Botan::BigInt::size(), and Botan::Power_Mod::window_bits().

28 {
29 m_window_bits = Power_Mod::window_bits(m_exp.bits(), base.bits(), m_hints);
30
31 m_g.resize((1 << m_window_bits));
32
33 BigInt z(BigInt::Positive, 2 * (m_mod_words + 1));
34 secure_vector<word> workspace(z.size());
35
36 m_g[0] = 1;
37
38 bigint_monty_mul(z, m_g[0], m_R2_mod,
39 m_modulus.data(), m_mod_words, m_mod_prime,
40 workspace.data());
41 m_g[0] = z;
42
43 m_g[1] = m_reducer.reduce(base);
44
45 bigint_monty_mul(z, m_g[1], m_R2_mod,
46 m_modulus.data(), m_mod_words, m_mod_prime,
47 workspace.data());
48
49 m_g[1] = z;
50
51 const BigInt& x = m_g[1];
52
53 for(size_t i = 2; i != m_g.size(); ++i)
54 {
55 const BigInt& y = m_g[i-1];
56
57 bigint_monty_mul(z, x, y, m_modulus.data(), m_mod_words, m_mod_prime,
58 workspace.data());
59
60 m_g[i] = z;
61 }
62 }
Botan::BigInt::Positive
Definition: bigint.h:34
Botan::BigInt::bits
size_t bits() const
Definition: bigint.cpp:184
Botan::bigint_monty_mul
void bigint_monty_mul(BigInt &z, const BigInt &x, const BigInt &y, const word p[], size_t p_size, word p_dash, word workspace[])
Definition: mp_monty.cpp:97
Botan::BigInt::data
const word * data() const
Definition: bigint.h:425
Botan::Modular_Reducer::reduce
BigInt reduce(const BigInt &x) const
Definition: reducer.cpp:32
Botan::Power_Mod::window_bits
static size_t window_bits(size_t exp_bits, size_t base_bits, Power_Mod::Usage_Hints hints)
Definition: pow_mod.cpp:117

void Botan::Montgomery_Exponentiator::set_exponent | ( | const BigInt & | exp | ) |
| override virtual

Implements Botan::Modular_Exponentiator.

Definition at line 18 of file powm_mnt.cpp.

References Botan::BigInt::bits().

19 {
20 m_exp = exp;
21 m_exp_bits = exp.bits();
22 }

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

- src/lib/math/numbertheory/def_powm.h
- src/lib/math/numbertheory/powm_mnt.cpp