Coverage Report

Created: 2021-10-13 08:49

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