Coverage Report

Created: 2021-02-21 07:20

/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
78.5k
   {
42
78.5k
   BOTAN_ARG_CHECK(g < m_params->p(), "Montgomery base too big");
43
44
78.5k
   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
78.5k
   const size_t window_size = (static_cast<size_t>(1) << m_window_bits);
48
49
78.5k
   m_g.reserve(window_size);
50
51
78.5k
   m_g.push_back(Montgomery_Int(m_params, m_params->R1(), false));
52
53
78.5k
   m_g.push_back(Montgomery_Int(m_params, g));
54
55
1.09M
   for(size_t i = 2; i != window_size; ++i)
56
1.01M
      {
57
1.01M
      m_g.push_back(m_g[1] * m_g[i - 1]);
58
1.01M
      }
59
60
   // Resize each element to exactly p words
61
1.24M
   for(size_t i = 0; i != window_size; ++i)
62
1.17M
      {
63
1.17M
      m_g[i].fix_size();
64
1.17M
      if(const_time)
65
24.4k
         m_g[i].const_time_poison();
66
1.17M
      }
67
78.5k
   }
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
125k
   {
75
125k
   BOTAN_ASSERT_NOMSG(g.size() % 2 == 0); // actually a power of 2
76
77
125k
   const size_t words = output.size();
78
79
125k
   clear_mem(output.data(), output.size());
80
81
1.13M
   for(size_t i = 0; i != g.size(); i += 2)
82
1.00M
      {
83
1.00M
      const secure_vector<word>& vec_0 = g[i  ].repr().get_word_vector();
84
1.00M
      const secure_vector<word>& vec_1 = g[i+1].repr().get_word_vector();
85
86
1.00M
      BOTAN_ASSERT_NOMSG(vec_0.size() >= words && vec_1.size() >= words);
87
88
1.00M
      const auto mask_0 = CT::Mask<word>::is_equal(nibble, i);
89
1.00M
      const auto mask_1 = CT::Mask<word>::is_equal(nibble, i+1);
90
91
17.3M
      for(size_t w = 0; w != words; ++w)
92
16.2M
         {
93
16.2M
         output[w] |= mask_0.if_set_return(vec_0[w]);
94
16.2M
         output[w] |= mask_1.if_set_return(vec_1[w]);
95
16.2M
         }
96
1.00M
      }
97
125k
   }
98
99
}
100
101
BigInt Montgomery_Exponentation_State::exponentiation(const BigInt& scalar, size_t max_k_bits) const
102
1.28k
   {
103
1.28k
   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.28k
   const size_t exp_nibbles = (max_k_bits + m_window_bits - 1) / m_window_bits;
107
108
1.28k
   if(exp_nibbles == 0)
109
6
      return 1;
110
111
1.28k
   secure_vector<word> e_bits(m_params->p_words());
112
1.28k
   secure_vector<word> ws;
113
114
1.28k
   const_time_lookup(e_bits, m_g, scalar.get_substring(m_window_bits*(exp_nibbles-1), m_window_bits));
115
1.28k
   Montgomery_Int x(m_params, e_bits.data(), e_bits.size(), false);
116
117
125k
   for(size_t i = exp_nibbles - 1; i > 0; --i)
118
124k
      {
119
124k
      x.square_this_n_times(ws, m_window_bits);
120
124k
      const_time_lookup(e_bits, m_g, scalar.get_substring(m_window_bits*(i-1), m_window_bits));
121
124k
      x.mul_by(e_bits, ws);
122
124k
      }
123
124
1.28k
   x.const_time_unpoison();
125
1.28k
   return x.value();
126
1.28k
   }
127
128
BigInt Montgomery_Exponentation_State::exponentiation_vartime(const BigInt& scalar) const
129
76.9k
   {
130
76.9k
   const size_t exp_nibbles = (scalar.bits() + m_window_bits - 1) / m_window_bits;
131
132
76.9k
   secure_vector<word> ws;
133
134
76.9k
   if(exp_nibbles == 0)
135
0
      return 1;
136
137
76.9k
   Montgomery_Int x = m_g[scalar.get_substring(m_window_bits*(exp_nibbles-1), m_window_bits)];
138
139
1.83M
   for(size_t i = exp_nibbles - 1; i > 0; --i)
140
1.76M
      {
141
1.76M
      x.square_this_n_times(ws, m_window_bits);
142
143
1.76M
      const uint32_t nibble = scalar.get_substring(m_window_bits*(i-1), m_window_bits);
144
1.76M
      if(nibble > 0)
145
876k
         x.mul_by(m_g[nibble], ws);
146
1.76M
      }
147
148
76.9k
   x.const_time_unpoison();
149
76.9k
   return x.value();
150
76.9k
   }
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
78.5k
   {
158
78.5k
   return std::make_shared<const Montgomery_Exponentation_State>(params, g, window_bits, const_time);
159
78.5k
   }
160
161
BigInt monty_execute(const Montgomery_Exponentation_State& precomputed_state,
162
                     const BigInt& k, size_t max_k_bits)
163
1.28k
   {
164
1.28k
   return precomputed_state.exponentiation(k, max_k_bits);
165
1.28k
   }
166
167
BigInt monty_execute_vartime(const Montgomery_Exponentation_State& precomputed_state,
168
                             const BigInt& k)
169
76.9k
   {
170
76.9k
   return precomputed_state.exponentiation_vartime(k);
171
76.9k
   }
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
124
   {
179
124
   if(z1.is_negative() || z2.is_negative())
180
0
      throw Invalid_Argument("multi_exponentiate exponents must be positive");
181
182
124
   const size_t z_bits = round_up(std::max(z1.bits(), z2.bits()), 2);
183
184
124
   secure_vector<word> ws;
185
186
124
   const Montgomery_Int one(params_p, params_p->R1(), false);
187
   //const Montgomery_Int one(params_p, 1);
188
189
124
   const Montgomery_Int x1(params_p, x_bn);
190
124
   const Montgomery_Int x2 = x1.square(ws);
191
124
   const Montgomery_Int x3 = x2.mul(x1, ws);
192
193
124
   const Montgomery_Int y1(params_p, y_bn);
194
124
   const Montgomery_Int y2 = y1.square(ws);
195
124
   const Montgomery_Int y3 = y2.mul(y1, ws);
196
197
124
   const Montgomery_Int y1x1 = y1.mul(x1, ws);
198
124
   const Montgomery_Int y1x2 = y1.mul(x2, ws);
199
124
   const Montgomery_Int y1x3 = y1.mul(x3, ws);
200
201
124
   const Montgomery_Int y2x1 = y2.mul(x1, ws);
202
124
   const Montgomery_Int y2x2 = y2.mul(x2, ws);
203
124
   const Montgomery_Int y2x3 = y2.mul(x3, ws);
204
205
124
   const Montgomery_Int y3x1 = y3.mul(x1, ws);
206
124
   const Montgomery_Int y3x2 = y3.mul(x2, ws);
207
124
   const Montgomery_Int y3x3 = y3.mul(x3, ws);
208
209
124
   const Montgomery_Int* M[16] = {
210
124
      &one,
211
124
      &x1,                    // 0001
212
124
      &x2,                    // 0010
213
124
      &x3,                    // 0011
214
124
      &y1,                    // 0100
215
124
      &y1x1,
216
124
      &y1x2,
217
124
      &y1x3,
218
124
      &y2,                    // 1000
219
124
      &y2x1,
220
124
      &y2x2,
221
124
      &y2x3,
222
124
      &y3,                    // 1100
223
124
      &y3x1,
224
124
      &y3x2,
225
124
      &y3x3
226
124
   };
227
228
124
   Montgomery_Int H = one;
229
230
38.9k
   for(size_t i = 0; i != z_bits; i += 2)
231
38.8k
      {
232
38.8k
      if(i > 0)
233
38.7k
         {
234
38.7k
         H.square_this(ws);
235
38.7k
         H.square_this(ws);
236
38.7k
         }
237
238
38.8k
      const uint32_t z1_b = z1.get_substring(z_bits - i - 2, 2);
239
38.8k
      const uint32_t z2_b = z2.get_substring(z_bits - i - 2, 2);
240
241
38.8k
      const uint32_t z12 = (4*z2_b) + z1_b;
242
243
38.8k
      H.mul_by(*M[z12], ws);
244
38.8k
      }
245
246
124
   return H.value();
247
124
   }
248
249
}
250