Botan: Botan::Montgomery_Exponentiator Class Reference

Botan 1.10.12

- Main Page
- Related Pages
- 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
BigInt | execute () const
| Montgomery_Exponentiator (const BigInt &, Power_Mod::Usage_Hints)
void | set_base (const BigInt &)
void | set_exponent (const BigInt &)

## 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 127 of file powm_mnt.cpp.

References Botan::inverse_mod(), Botan::BigInt::is_even(), Botan::BigInt::is_positive(), Botan::BigInt::Power2, and Botan::BigInt::sig_words().

129 {
130 // Montgomery reduction only works for positive odd moduli
131 if(!mod.is_positive() || mod.is_even())
132 throw Invalid_Argument("Montgomery_Exponentiator: invalid modulus");
133
134 window_bits = 0;
135 this->hints = hints;
136 modulus = mod;
137 exp_bits = 0;
138
139 mod_words = modulus.sig_words();
140
141 BigInt r(BigInt::Power2, mod_words * BOTAN_MP_WORD_BITS);
142 mod_prime = (((r * inverse_mod(r, mod)) - 1) / mod).word_at(0);
143
144 R_mod = r % modulus;
145
146 R2 = (R_mod * R_mod) % modulus;
147 }
Botan::BigInt::sig_words
size_t sig_words() const
Definition: bigint.h:290
Botan::Invalid_Argument
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
Botan::inverse_mod
BigInt inverse_mod(const BigInt &n, const BigInt &mod)
Definition: numthry.cpp:202
Botan::BigInt::Power2
Definition: bigint.h:38

## Member Function Documentation

Modular_Exponentiator* Botan::Montgomery_Exponentiator::copy | ( |  | ) | const
| inlinevirtual

Implements Botan::Modular_Exponentiator.

Definition at line 49 of file def_powm.h.

References R2.

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

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

Implements Botan::Modular_Exponentiator.

Definition at line 77 of file powm_mnt.cpp.

References Botan::BigInt::assign(), Botan::bigint_monty_mul(), Botan::bigint_monty_redc(), Botan::bigint_monty_sqr(), Botan::BigInt::data(), Botan::BigInt::get_reg(), Botan::BigInt::get_substring(), Botan::MemoryRegion< T >::resize(), Botan::BigInt::sig_words(), Botan::MemoryRegion< T >::size(), Botan::BigInt::size(), and Botan::zeroise().

78 {
79 const size_t exp_nibbles = (exp_bits + window_bits - 1) / window_bits;
80
81 BigInt x = R_mod;
82 SecureVector<word> z(2 * (mod_words + 1));
83 SecureVector<word> workspace(2 * (mod_words + 1));
84
85 for(size_t i = exp_nibbles; i > 0; --i)
86 {
87 for(size_t k = 0; k != window_bits; ++k)
88 {
89 zeroise(z);
90
91 bigint_monty_sqr(&z[0], z.size(),
92 x.data(), x.size(), x.sig_words(),
93 modulus.data(), mod_words, mod_prime,
94 &workspace[0]);
95
96 x.assign(&z[0], mod_words + 1);
97 }
98
99 const u32bit nibble = exp.get_substring(window_bits*(i-1), window_bits);
100
101 const BigInt& y = g[nibble];
102
103 zeroise(z);
104 bigint_monty_mul(&z[0], z.size(),
105 x.data(), x.size(), x.sig_words(),
106 y.data(), y.size(), y.sig_words(),
107 modulus.data(), mod_words, mod_prime,
108 &workspace[0]);
109
110 x.assign(&z[0], mod_words + 1);
111 }
112
113 x.get_reg().resize(2*mod_words+1);
114
115 bigint_monty_redc(&x[0], x.size(),
116 modulus.data(), mod_words, mod_prime,
117 &workspace[0]);
118
119 x.get_reg().resize(mod_words+1);
120
121 return x;
122 }
Botan::bigint_monty_sqr
void bigint_monty_sqr(word z[], size_t z_size, const word x[], size_t x_size, size_t x_sw, const word p[], size_t p_size, word p_dash, word workspace[])
Definition: mp_monty.cpp:84
Botan::BigInt::get_substring
u32bit get_substring(size_t offset, size_t length) const
Definition: bigint.cpp:168
Botan::bigint_monty_redc
void bigint_monty_redc(word z[], size_t z_size, const word p[], size_t p_size, word p_dash, word workspace[])
Definition: mp_monty.cpp:21
Botan::BigInt::data
const word * data() const
Definition: bigint.h:317
Botan::bigint_monty_mul
void bigint_monty_mul(word z[], size_t z_size, const word x[], size_t x_size, size_t x_sw, const word y[], size_t y_size, size_t y_sw, const word p[], size_t p_size, word p_dash, word workspace[])
Definition: mp_monty.cpp:69
Botan::zeroise
void zeroise(MemoryRegion< T > &vec)
Definition: secmem.h:415
Botan::u32bit
unsigned int u32bit
Definition: types.h:32

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

Implements Botan::Modular_Exponentiator.

Definition at line 26 of file powm_mnt.cpp.

References Botan::bigint_monty_mul(), Botan::BigInt::bits(), Botan::BigInt::data(), Botan::BigInt::sig_words(), Botan::MemoryRegion< T >::size(), Botan::BigInt::size(), Botan::Power_Mod::window_bits(), and Botan::zeroise().

27 {
28 window_bits = Power_Mod::window_bits(exp.bits(), base.bits(), hints);
29
30 g.resize((1 << window_bits));
31
32 SecureVector<word> z(2 * (mod_words + 1));
33 SecureVector<word> workspace(z.size());
34
35 g[0] = 1;
36
37 bigint_monty_mul(&z[0], z.size(),
38 g[0].data(), g[0].size(), g[0].sig_words(),
39 R2.data(), R2.size(), R2.sig_words(),
40 modulus.data(), mod_words, mod_prime,
41 &workspace[0]);
42
43 g[0].assign(&z[0], mod_words + 1);
44
45 g[1] = (base >= modulus) ? (base % modulus) : base;
46
47 bigint_monty_mul(&z[0], z.size(),
48 g[1].data(), g[1].size(), g[1].sig_words(),
49 R2.data(), R2.size(), R2.sig_words(),
50 modulus.data(), mod_words, mod_prime,
51 &workspace[0]);
52
53 g[1].assign(&z[0], mod_words + 1);
54
55 const BigInt& x = g[1];
56 const size_t x_sig = x.sig_words();
57
58 for(size_t i = 1; i != g.size(); ++i)
59 {
60 const BigInt& y = g[i-1];
61 const size_t y_sig = y.sig_words();
62
63 zeroise(z);
64 bigint_monty_mul(&z[0], z.size(),
65 x.data(), x.size(), x_sig,
66 y.data(), y.size(), y_sig,
67 modulus.data(), mod_words, mod_prime,
68 &workspace[0]);
69
70 g[i].assign(&z[0], mod_words + 1);
71 }
72 }
Botan::BigInt::sig_words
size_t sig_words() const
Definition: bigint.h:290
Botan::BigInt::size
size_t size() const
Definition: bigint.h:284
Botan::BigInt::bits
size_t bits() const
Definition: bigint.cpp:253
Botan::BigInt::data
const word * data() const
Definition: bigint.h:317
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:119
Botan::bigint_monty_mul
void bigint_monty_mul(word z[], size_t z_size, const word x[], size_t x_size, size_t x_sw, const word y[], size_t y_size, size_t y_sw, const word p[], size_t p_size, word p_dash, word workspace[])
Definition: mp_monty.cpp:69
Botan::zeroise
void zeroise(MemoryRegion< T > &vec)
Definition: secmem.h:415

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

Implements Botan::Modular_Exponentiator.

Definition at line 17 of file powm_mnt.cpp.

References Botan::BigInt::bits().

18 {
19 this->exp = exp;
20 exp_bits = exp.bits();
21 }
Botan::BigInt::bits
size_t bits() const
Definition: bigint.cpp:253

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

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