Coverage Report

Created: 2021-04-07 06:07

/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
73.7k
   {
42
73.7k
   BOTAN_ARG_CHECK(g < m_params->p(), "Montgomery base too big");
43
44
73.7k
   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
73.7k
   const size_t window_size = (static_cast<size_t>(1) << m_window_bits);
48
49
73.7k
   m_g.reserve(window_size);
50
51
73.7k
   m_g.push_back(Montgomery_Int(m_params, m_params->R1(), false));
52
53
73.7k
   m_g.push_back(Montgomery_Int(m_params, g));
54
55
1.02M
   for(size_t i = 2; i != window_size; ++i)
56
951k
      {
57
951k
      m_g.push_back(m_g[1] * m_g[i - 1]);
58
951k
      }
59
60
   // Resize each element to exactly p words
61
1.17M
   for(size_t i = 0; i != window_size; ++i)
62
1.09M
      {
63
1.09M
      m_g[i].fix_size();
64
1.09M
      if(const_time)
65
27.5k
         m_g[i].const_time_poison();
66
1.09M
      }
67
73.7k
   }
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
124k
   {
75
124k
   BOTAN_ASSERT_NOMSG(g.size() % 2 == 0); // actually a power of 2
76
77
124k
   const size_t words = output.size();
78
79
124k
   clear_mem(output.data(), output.size());
80
81
1.11M
   for(size_t i = 0; i != g.size(); i += 2)
82
992k
      {
83
992k
      const secure_vector<word>& vec_0 = g[i  ].repr().get_word_vector();
84
992k
      const secure_vector<word>& vec_1 = g[i+1].repr().get_word_vector();
85
86
992k
      BOTAN_ASSERT_NOMSG(vec_0.size() >= words && vec_1.size() >= words);
87
88
992k
      const auto mask_0 = CT::Mask<word>::is_equal(nibble, i);
89
992k
      const auto mask_1 = CT::Mask<word>::is_equal(nibble, i+1);
90
91
15.1M
      for(size_t w = 0; w != words; ++w)
92
14.1M
         {
93
14.1M
         output[w] |= mask_0.if_set_return(vec_0[w]);
94
14.1M
         output[w] |= mask_1.if_set_return(vec_1[w]);
95
14.1M
         }
96
992k
      }
97
124k
   }
98
99
}
100
101
BigInt Montgomery_Exponentation_State::exponentiation(const BigInt& scalar, size_t max_k_bits) const
102
1.49k
   {
103
1.49k
   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.49k
   const size_t exp_nibbles = (max_k_bits + m_window_bits - 1) / m_window_bits;
107
108
1.49k
   if(exp_nibbles == 0)
109
5
      return 1;
110
111
1.48k
   secure_vector<word> e_bits(m_params->p_words());
112
1.48k
   secure_vector<word> ws;
113
114
1.48k
   const_time_lookup(e_bits, m_g, scalar.get_substring(m_window_bits*(exp_nibbles-1), m_window_bits));
115
1.48k
   Montgomery_Int x(m_params, e_bits.data(), e_bits.size(), false);
116
117
124k
   for(size_t i = exp_nibbles - 1; i > 0; --i)
118
122k
      {
119
122k
      x.square_this_n_times(ws, m_window_bits);
120
122k
      const_time_lookup(e_bits, m_g, scalar.get_substring(m_window_bits*(i-1), m_window_bits));
121
122k
      x.mul_by(e_bits, ws);
122
122k
      }
123
124
1.48k
   x.const_time_unpoison();
125
1.48k
   return x.value();
126
1.48k
   }
127
128
BigInt Montgomery_Exponentation_State::exponentiation_vartime(const BigInt& scalar) const
129
72.0k
   {
130
72.0k
   const size_t exp_nibbles = (scalar.bits() + m_window_bits - 1) / m_window_bits;
131
132
72.0k
   secure_vector<word> ws;
133
134
72.0k
   if(exp_nibbles == 0)
135
0
      return 1;
136
137
72.0k
   Montgomery_Int x = m_g[scalar.get_substring(m_window_bits*(exp_nibbles-1), m_window_bits)];
138
139
1.84M
   for(size_t i = exp_nibbles - 1; i > 0; --i)
140
1.77M
      {
141
1.77M
      x.square_this_n_times(ws, m_window_bits);
142
143
1.77M
      const uint32_t nibble = scalar.get_substring(m_window_bits*(i-1), m_window_bits);
144
1.77M
      if(nibble > 0)
145
874k
         x.mul_by(m_g[nibble], ws);
146
1.77M
      }
147
148
72.0k
   x.const_time_unpoison();
149
72.0k
   return x.value();
150
72.0k
   }
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
73.7k
   {
158
73.7k
   return std::make_shared<const Montgomery_Exponentation_State>(params, g, window_bits, const_time);
159
73.7k
   }
160
161
BigInt monty_execute(const Montgomery_Exponentation_State& precomputed_state,
162
                     const BigInt& k, size_t max_k_bits)
163
1.49k
   {
164
1.49k
   return precomputed_state.exponentiation(k, max_k_bits);
165
1.49k
   }
166
167
BigInt monty_execute_vartime(const Montgomery_Exponentation_State& precomputed_state,
168
                             const BigInt& k)
169
72.0k
   {
170
72.0k
   return precomputed_state.exponentiation_vartime(k);
171
72.0k
   }
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
36.1k
   for(size_t i = 0; i != z_bits; i += 2)
231
36.0k
      {
232
36.0k
      if(i > 0)
233
35.9k
         {
234
35.9k
         H.square_this(ws);
235
35.9k
         H.square_this(ws);
236
35.9k
         }
237
238
36.0k
      const uint32_t z1_b = z1.get_substring(z_bits - i - 2, 2);
239
36.0k
      const uint32_t z2_b = z2.get_substring(z_bits - i - 2, 2);
240
241
36.0k
      const uint32_t z12 = (4*z2_b) + z1_b;
242
243
36.0k
      H.mul_by(*M[z12], ws);
244
36.0k
      }
245
246
109
   return H.value();
247
109
   }
248
249
}
250