Botan  2.4.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(1U << 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  m_g[i].shrink_to_fit();
62  m_g[i].grow_to(m_mod_words);
63  }
64  }
65 
66 /*
67 * Compute the result
68 */
70  {
71  const size_t exp_nibbles = (m_exp_bits + m_window_bits - 1) / m_window_bits;
72 
73  BigInt x = m_R_mod;
74 
75  const size_t z_size = 2*(m_mod_words + 1);
76 
77  BigInt z(BigInt::Positive, z_size);
78  secure_vector<word> workspace(z.size());
79  secure_vector<word> e(m_mod_words);
80 
81  for(size_t i = exp_nibbles; i > 0; --i)
82  {
83  for(size_t k = 0; k != m_window_bits; ++k)
84  {
85  bigint_monty_sqr(z, x, m_modulus.data(), m_mod_words, m_mod_prime,
86  workspace.data());
87 
88  x = z;
89  }
90 
91  const uint32_t nibble = m_exp.get_substring(m_window_bits*(i-1), m_window_bits);
92 
93  BigInt::const_time_lookup(e, m_g, nibble);
94 
95  bigint_mul(z.mutable_data(), z.size(),
96  x.data(), x.size(), x.sig_words(),
97  e.data(), m_mod_words, m_mod_words,
98  workspace.data());
99 
101  m_modulus.data(), m_mod_words, m_mod_prime,
102  workspace.data());
103 
104  x = z;
105  }
106 
107  x.grow_to(2*m_mod_words + 1);
108 
110  m_modulus.data(), m_mod_words, m_mod_prime,
111  workspace.data());
112 
113  return x;
114  }
115 
116 /*
117 * Montgomery_Exponentiator Constructor
118 */
120  Power_Mod::Usage_Hints hints) :
121  m_modulus(mod),
122  m_reducer(m_modulus),
123  m_mod_words(m_modulus.sig_words()),
124  m_window_bits(1),
125  m_hints(hints)
126  {
127  // Montgomery reduction only works for positive odd moduli
128  if(!m_modulus.is_positive() || m_modulus.is_even())
129  throw Invalid_Argument("Montgomery_Exponentiator: invalid modulus");
130 
131  m_mod_prime = monty_inverse(mod.word_at(0));
132 
133  const BigInt r = BigInt::power_of_2(m_mod_words * BOTAN_MP_WORD_BITS);
134  m_R_mod = m_reducer.reduce(r);
135  m_R2_mod = m_reducer.square(m_R_mod);
136  m_exp_bits = 0;
137  }
138 
139 }
static void const_time_lookup(secure_vector< word > &output, const std::vector< BigInt > &vec, size_t idx)
Definition: bigint.cpp:302
size_t bits() const
Definition: bigint.cpp:183
word * mutable_data()
Definition: bigint.h:424
BigInt execute() const override
Definition: powm_mnt.cpp:69
bool is_even() const
Definition: bigint.h:237
word word_at(size_t n) const
Definition: bigint.h:340
uint32_t get_substring(size_t offset, size_t length) const
Definition: bigint.cpp:119
void set_exponent(const BigInt &) override
Definition: powm_mnt.cpp:18
const word * data() const
Definition: bigint.h:430
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:108
Montgomery_Exponentiator(const BigInt &, Power_Mod::Usage_Hints)
Definition: powm_mnt.cpp:119
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
size_t size() const
Definition: bigint.h:392
static BigInt power_of_2(size_t n)
Definition: bigint.h:499
Definition: alg_id.cpp:13
size_t sig_words() const
Definition: bigint.h:398
static size_t window_bits(size_t exp_bits, size_t base_bits, Power_Mod::Usage_Hints hints)
Definition: pow_mod.cpp:102
void grow_to(size_t n)
Definition: bigint.cpp:260
BigInt reduce(const BigInt &x) const
Definition: reducer.cpp:32
void bigint_mul(BigInt &z, const BigInt &x, const BigInt &y, word workspace[])
Definition: mp_karat.cpp:253
void bigint_monty_redc(word z[], const word p[], size_t p_size, word p_dash, word workspace[])
Definition: mp_monty.cpp:22
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:88
void shrink_to_fit()
Definition: bigint.cpp:297
BigInt square(const BigInt &x) const
Definition: reducer.h:39
word monty_inverse(word input)
Definition: numthry.cpp:326
bool is_positive() const
Definition: bigint.h:359