Coverage Report

Created: 2021-05-04 09:02

/src/botan/src/lib/math/numbertheory/monty_exp.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
* Montgomery Exponentiation
3
* (C) 1999-2010,2012,2018 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/monty_exp.h>
10
#include <botan/internal/ct_utils.h>
11
#include <botan/internal/rounding.h>
12
#include <botan/numthry.h>
13
#include <botan/reducer.h>
14
#include <botan/internal/monty.h>
15
16
namespace Botan {
17
18
class Montgomery_Exponentation_State
19
   {
20
   public:
21
      Montgomery_Exponentation_State(std::shared_ptr<const Montgomery_Params> params,
22
                                     const BigInt& g,
23
                                     size_t window_bits,
24
                                     bool const_time);
25
26
      BigInt exponentiation(const BigInt& k, size_t max_k_bits) const;
27
28
      BigInt exponentiation_vartime(const BigInt& k) const;
29
   private:
30
      std::shared_ptr<const Montgomery_Params> m_params;
31
      std::vector<Montgomery_Int> m_g;
32
      size_t m_window_bits;
33
   };
34
35
Montgomery_Exponentation_State::Montgomery_Exponentation_State(std::shared_ptr<const Montgomery_Params> params,
36
                                                               const BigInt& g,
37
                                                               size_t window_bits,
38
                                                               bool const_time) :
39
   m_params(params),
40
   m_window_bits(window_bits == 0 ? 4 : window_bits)
41
72.4k
   {
42
72.4k
   BOTAN_ARG_CHECK(g < m_params->p(), "Montgomery base too big");
43
44
72.4k
   if(m_window_bits < 1 || m_window_bits > 12) // really even 8 is too large ...
45
0
      throw Invalid_Argument("Invalid window bits for Montgomery exponentiation");
46
47
72.4k
   const size_t window_size = (static_cast<size_t>(1) << m_window_bits);
48
49
72.4k
   m_g.reserve(window_size);
50
51
72.4k
   m_g.push_back(Montgomery_Int(m_params, m_params->R1(), false));
52
53
72.4k
   m_g.push_back(Montgomery_Int(m_params, g));
54
55
998k
   for(size_t i = 2; i != window_size; ++i)
56
925k
      {
57
925k
      m_g.push_back(m_g[1] * m_g[i - 1]);
58
925k
      }
59
60
   // Resize each element to exactly p words
61
1.14M
   for(size_t i = 0; i != window_size; ++i)
62
1.07M
      {
63
1.07M
      m_g[i].fix_size();
64
1.07M
      if(const_time)
65
28.9k
         m_g[i].const_time_poison();
66
1.07M
      }
67
72.4k
   }
68
69
namespace {
70
71
void const_time_lookup(secure_vector<word>& output,
72
                       const std::vector<Montgomery_Int>& g,
73
                       size_t nibble)
74
130k
   {
75
130k
   BOTAN_ASSERT_NOMSG(g.size() % 2 == 0); // actually a power of 2
76
77
130k
   const size_t words = output.size();
78
79
130k
   clear_mem(output.data(), output.size());
80
81
1.17M
   for(size_t i = 0; i != g.size(); i += 2)
82
1.04M
      {
83
1.04M
      const secure_vector<word>& vec_0 = g[i  ].repr().get_word_vector();
84
1.04M
      const secure_vector<word>& vec_1 = g[i+1].repr().get_word_vector();
85
86
1.04M
      BOTAN_ASSERT_NOMSG(vec_0.size() >= words && vec_1.size() >= words);
87
88
1.04M
      const auto mask_0 = CT::Mask<word>::is_equal(nibble, i);
89
1.04M
      const auto mask_1 = CT::Mask<word>::is_equal(nibble, i+1);
90
91
15.5M
      for(size_t w = 0; w != words; ++w)
92
14.5M
         {
93
14.5M
         output[w] |= mask_0.if_set_return(vec_0[w]);
94
14.5M
         output[w] |= mask_1.if_set_return(vec_1[w]);
95
14.5M
         }
96
1.04M
      }
97
130k
   }
98
99
}
100
101
BigInt Montgomery_Exponentation_State::exponentiation(const BigInt& scalar, size_t max_k_bits) const
102
1.58k
   {
103
1.58k
   BOTAN_DEBUG_ASSERT(scalar.bits() <= max_k_bits);
104
   // TODO add a const-time implementation of above assert and use it in release builds
105
106
1.58k
   const size_t exp_nibbles = (max_k_bits + m_window_bits - 1) / m_window_bits;
107
108
1.58k
   if(exp_nibbles == 0)
109
9
      return BigInt::one();
110
111
1.57k
   secure_vector<word> e_bits(m_params->p_words());
112
1.57k
   secure_vector<word> ws;
113
114
1.57k
   const_time_lookup(e_bits, m_g, scalar.get_substring(m_window_bits*(exp_nibbles-1), m_window_bits));
115
1.57k
   Montgomery_Int x(m_params, e_bits.data(), e_bits.size(), false);
116
117
130k
   for(size_t i = exp_nibbles - 1; i > 0; --i)
118
128k
      {
119
128k
      x.square_this_n_times(ws, m_window_bits);
120
128k
      const_time_lookup(e_bits, m_g, scalar.get_substring(m_window_bits*(i-1), m_window_bits));
121
128k
      x.mul_by(e_bits, ws);
122
128k
      }
123
124
1.57k
   x.const_time_unpoison();
125
1.57k
   return x.value();
126
1.57k
   }
127
128
BigInt Montgomery_Exponentation_State::exponentiation_vartime(const BigInt& scalar) const
129
70.6k
   {
130
70.6k
   const size_t exp_nibbles = (scalar.bits() + m_window_bits - 1) / m_window_bits;
131
132
70.6k
   secure_vector<word> ws;
133
134
70.6k
   if(exp_nibbles == 0)
135
0
      return BigInt::one();
136
137
70.6k
   Montgomery_Int x = m_g[scalar.get_substring(m_window_bits*(exp_nibbles-1), m_window_bits)];
138
139
1.94M
   for(size_t i = exp_nibbles - 1; i > 0; --i)
140
1.87M
      {
141
1.87M
      x.square_this_n_times(ws, m_window_bits);
142
143
1.87M
      const uint32_t nibble = scalar.get_substring(m_window_bits*(i-1), m_window_bits);
144
1.87M
      if(nibble > 0)
145
875k
         x.mul_by(m_g[nibble], ws);
146
1.87M
      }
147
148
70.6k
   x.const_time_unpoison();
149
70.6k
   return x.value();
150
70.6k
   }
151
152
std::shared_ptr<const Montgomery_Exponentation_State>
153
monty_precompute(std::shared_ptr<const Montgomery_Params> params,
154
                 const BigInt& g,
155
                 size_t window_bits,
156
                 bool const_time)
157
72.4k
   {
158
72.4k
   return std::make_shared<const Montgomery_Exponentation_State>(params, g, window_bits, const_time);
159
72.4k
   }
160
161
BigInt monty_execute(const Montgomery_Exponentation_State& precomputed_state,
162
                     const BigInt& k, size_t max_k_bits)
163
1.58k
   {
164
1.58k
   return precomputed_state.exponentiation(k, max_k_bits);
165
1.58k
   }
166
167
BigInt monty_execute_vartime(const Montgomery_Exponentation_State& precomputed_state,
168
                             const BigInt& k)
169
70.6k
   {
170
70.6k
   return precomputed_state.exponentiation_vartime(k);
171
70.6k
   }
172
173
BigInt monty_multi_exp(std::shared_ptr<const Montgomery_Params> params_p,
174
                       const BigInt& x_bn,
175
                       const BigInt& z1,
176
                       const BigInt& y_bn,
177
                       const BigInt& z2)
178
109
   {
179
109
   if(z1.is_negative() || z2.is_negative())
180
0
      throw Invalid_Argument("multi_exponentiate exponents must be positive");
181
182
109
   const size_t z_bits = round_up(std::max(z1.bits(), z2.bits()), 2);
183
184
109
   secure_vector<word> ws;
185
186
109
   const Montgomery_Int one(params_p, params_p->R1(), false);
187
   //const Montgomery_Int one(params_p, 1);
188
189
109
   const Montgomery_Int x1(params_p, x_bn);
190
109
   const Montgomery_Int x2 = x1.square(ws);
191
109
   const Montgomery_Int x3 = x2.mul(x1, ws);
192
193
109
   const Montgomery_Int y1(params_p, y_bn);
194
109
   const Montgomery_Int y2 = y1.square(ws);
195
109
   const Montgomery_Int y3 = y2.mul(y1, ws);
196
197
109
   const Montgomery_Int y1x1 = y1.mul(x1, ws);
198
109
   const Montgomery_Int y1x2 = y1.mul(x2, ws);
199
109
   const Montgomery_Int y1x3 = y1.mul(x3, ws);
200
201
109
   const Montgomery_Int y2x1 = y2.mul(x1, ws);
202
109
   const Montgomery_Int y2x2 = y2.mul(x2, ws);
203
109
   const Montgomery_Int y2x3 = y2.mul(x3, ws);
204
205
109
   const Montgomery_Int y3x1 = y3.mul(x1, ws);
206
109
   const Montgomery_Int y3x2 = y3.mul(x2, ws);
207
109
   const Montgomery_Int y3x3 = y3.mul(x3, ws);
208
209
109
   const Montgomery_Int* M[16] = {
210
109
      &one,
211
109
      &x1,                    // 0001
212
109
      &x2,                    // 0010
213
109
      &x3,                    // 0011
214
109
      &y1,                    // 0100
215
109
      &y1x1,
216
109
      &y1x2,
217
109
      &y1x3,
218
109
      &y2,                    // 1000
219
109
      &y2x1,
220
109
      &y2x2,
221
109
      &y2x3,
222
109
      &y3,                    // 1100
223
109
      &y3x1,
224
109
      &y3x2,
225
109
      &y3x3
226
109
   };
227
228
109
   Montgomery_Int H = one;
229
230
35.2k
   for(size_t i = 0; i != z_bits; i += 2)
231
35.1k
      {
232
35.1k
      if(i > 0)
233
35.0k
         {
234
35.0k
         H.square_this(ws);
235
35.0k
         H.square_this(ws);
236
35.0k
         }
237
238
35.1k
      const uint32_t z1_b = z1.get_substring(z_bits - i - 2, 2);
239
35.1k
      const uint32_t z2_b = z2.get_substring(z_bits - i - 2, 2);
240
241
35.1k
      const uint32_t z12 = (4*z2_b) + z1_b;
242
243
35.1k
      H.mul_by(*M[z12], ws);
244
35.1k
      }
245
246
109
   return H.value();
247
109
   }
248
249
}
250