Botan: src/math/numbertheory/powm_mnt.cpp Source File

Botan 1.10.16

powm_mnt.cpp
Go to the documentation of this file.
1 /*
2 * Montgomery Exponentiation
3 * (C) 1999-2010 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7
8 #include <botan/internal/def_powm.h>
9 #include <botan/numthry.h>
10 #include <botan/internal/mp_core.h>
11
12 namespace Botan {
13
14 /*
15 * Set the exponent
16 */
17 void Montgomery_Exponentiator::set_exponent(const BigInt& exp)
18 {
19 this->exp = exp;
20 exp_bits = exp.bits();
21 }
22
23 /*
24 * Set the base
25 */
26 void Montgomery_Exponentiator::set_base(const BigInt& base)
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 }
73
74 /*
75 * Compute the result
76 */
77 BigInt Montgomery_Exponentiator::execute() const
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 }
123
124 /*
125 * Montgomery_Exponentiator Constructor
126 */
127 Montgomery_Exponentiator::Montgomery_Exponentiator(const BigInt& mod,
128 Power_Mod::Usage_Hints hints)
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 }
148
149 }
Botan::MemoryRegion::resize
void resize(size_t n)
Definition: secmem.h:211
Botan::BigInt::assign
void assign(const word x[], size_t length)
Definition: bigint.h:337
Botan::BigInt
Definition: bigint.h:22
Botan::BigInt::bits
size_t bits() const
Definition: bigint.cpp:253
Botan::BigInt::get_substring
u32bit get_substring(size_t offset, size_t length) const
Definition: bigint.cpp:168
Botan::Montgomery_Exponentiator::set_exponent
void set_exponent(const BigInt &)
Definition: powm_mnt.cpp:17
Botan::BigInt::get_reg
SecureVector< word > & get_reg()
Definition: bigint.h:325
Botan::Invalid_Argument
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
Botan::BigInt::is_even
bool is_even() const
Definition: bigint.h:158
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::data
const word * data() const
Definition: bigint.h:317
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::Montgomery_Exponentiator::Montgomery_Exponentiator
Montgomery_Exponentiator(const BigInt &, Power_Mod::Usage_Hints)
Definition: powm_mnt.cpp:127
Botan::BigInt::size
size_t size() const
Definition: bigint.h:284
Botan
Definition: algo_base.h:14
Botan::BigInt::sig_words
size_t sig_words() const
Definition: bigint.h:290
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::MemoryRegion::size
size_t size() const
Definition: secmem.h:29
Botan::inverse_mod
BigInt inverse_mod(const BigInt &n, const BigInt &mod)
Definition: numthry.cpp:314
Botan::Power_Mod::Usage_Hints
Usage_Hints
Definition: pow_mod.h:35
Botan::Montgomery_Exponentiator::set_base
void set_base(const BigInt &)
Definition: powm_mnt.cpp:26
Botan::SecureVector< word >
Botan::BigInt::Power2
Definition: bigint.h:38
Botan::zeroise
void zeroise(MemoryRegion< T > &vec)
Definition: secmem.h:415
Botan::BigInt::is_positive
bool is_positive() const
Definition: bigint.h:251
Botan::u32bit
unsigned int u32bit
Definition: types.h:32
Botan::Montgomery_Exponentiator::execute
BigInt execute() const
Definition: powm_mnt.cpp:77