Botan  2.1.0
Crypto and TLS for C++11
powm_mnt.cpp
Go to the documentation of this file.
1 /*
2 * Montgomery Exponentiation
3 * (C) 1999-2010,2012 Jack Lloyd
4 * 2016 Matthias Gierlings
5 *
6 * Botan is released under the Simplified BSD License (see license.txt)
7 */
8 
9 #include <botan/internal/def_powm.h>
10 #include <botan/numthry.h>
11 #include <botan/internal/mp_core.h>
12 
13 namespace Botan {
14 
15 /*
16 * Set the exponent
17 */
19  {
20  m_exp = exp;
21  m_exp_bits = exp.bits();
22  }
23 
24 /*
25 * Set the base
26 */
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  }
63 
64 /*
65 * Compute the result
66 */
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 
100  m_modulus.data(), m_mod_words, m_mod_prime,
101  workspace.data());
102 
103  return x;
104  }
105 
106 /*
107 * Montgomery_Exponentiator Constructor
108 */
110  Power_Mod::Usage_Hints hints) :
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  }
128 
129 }
word word_at(size_t n) const
Definition: bigint.h:335
bool is_even() const
Definition: bigint.h:232
word * mutable_data()
Definition: bigint.h:419
BigInt execute() const override
Definition: powm_mnt.cpp:67
size_t size() const
Definition: bigint.h:387
void set_exponent(const BigInt &) override
Definition: powm_mnt.cpp:18
size_t bits() const
Definition: bigint.cpp:184
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:121
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
Montgomery_Exponentiator(const BigInt &, Power_Mod::Usage_Hints)
Definition: powm_mnt.cpp:109
void set_base(const BigInt &) override
Definition: powm_mnt.cpp:27
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
const word * data() const
Definition: bigint.h:425
static BigInt power_of_2(size_t n)
Definition: bigint.h:492
Definition: alg_id.cpp:13
BigInt reduce(const BigInt &x) const
Definition: reducer.cpp:32
static size_t window_bits(size_t exp_bits, size_t base_bits, Power_Mod::Usage_Hints hints)
Definition: pow_mod.cpp:117
uint32_t get_substring(size_t offset, size_t length) const
Definition: bigint.cpp:120
void grow_to(size_t n)
Definition: bigint.cpp:261
bool is_positive() const
Definition: bigint.h:354
word monty_inverse(word input)
Definition: numthry.cpp:325
void bigint_monty_redc(word z[], const word p[], size_t p_size, word p_dash, word workspace[])
Definition: mp_monty.cpp:22
BigInt square(const BigInt &x) const
Definition: reducer.h:39