Coverage Report

Created: 2021-05-04 09:02

/src/botan/src/lib/math/numbertheory/monty.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
* (C) 2018 Jack Lloyd
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6
7
#include <botan/internal/monty.h>
8
#include <botan/reducer.h>
9
#include <botan/internal/mp_core.h>
10
11
namespace Botan {
12
13
word monty_inverse(word a)
14
23.5k
   {
15
23.5k
   if(a % 2 == 0)
16
0
      throw Invalid_Argument("monty_inverse only valid for odd integers");
17
18
   /*
19
   * From "A New Algorithm for Inversion mod p^k" by Çetin Kaya Koç
20
   * https://eprint.iacr.org/2017/411.pdf sections 5 and 7.
21
   */
22
23
23.5k
   word b = 1;
24
23.5k
   word r = 0;
25
26
1.53M
   for(size_t i = 0; i != BOTAN_MP_WORD_BITS; ++i)
27
1.51M
      {
28
1.51M
      const word bi = b % 2;
29
1.51M
      r >>= 1;
30
1.51M
      r += bi << (BOTAN_MP_WORD_BITS - 1);
31
32
1.51M
      b -= a * bi;
33
1.51M
      b >>= 1;
34
1.51M
      }
35
36
   // Now invert in addition space
37
23.5k
   r = (MP_WORD_MAX - r) + 1;
38
39
23.5k
   return r;
40
23.5k
   }
41
42
Montgomery_Params::Montgomery_Params(const BigInt& p,
43
                                     const Modular_Reducer& mod_p)
44
16.1k
   {
45
16.1k
   if(p.is_even() || p < 3)
46
23
      throw Invalid_Argument("Montgomery_Params invalid modulus");
47
48
16.0k
   m_p = p;
49
16.0k
   m_p_words = m_p.sig_words();
50
16.0k
   m_p_dash = monty_inverse(m_p.word_at(0));
51
52
16.0k
   const BigInt r = BigInt::power_of_2(m_p_words * BOTAN_MP_WORD_BITS);
53
54
16.0k
   m_r1 = mod_p.reduce(r);
55
16.0k
   m_r2 = mod_p.square(m_r1);
56
16.0k
   m_r3 = mod_p.multiply(m_r1, m_r2);
57
16.0k
   }
58
59
Montgomery_Params::Montgomery_Params(const BigInt& p)
60
6.72k
   {
61
6.72k
   if(p.is_even() || p < 3)
62
3
      throw Invalid_Argument("Montgomery_Params invalid modulus");
63
64
6.71k
   m_p = p;
65
6.71k
   m_p_words = m_p.sig_words();
66
6.71k
   m_p_dash = monty_inverse(m_p.word_at(0));
67
68
6.71k
   const BigInt r = BigInt::power_of_2(m_p_words * BOTAN_MP_WORD_BITS);
69
70
   // It might be faster to use ct_modulo here vs setting up Barrett reduction?
71
6.71k
   Modular_Reducer mod_p(p);
72
73
6.71k
   m_r1 = mod_p.reduce(r);
74
6.71k
   m_r2 = mod_p.square(m_r1);
75
6.71k
   m_r3 = mod_p.multiply(m_r1, m_r2);
76
6.71k
   }
77
78
BigInt Montgomery_Params::inv_mod_p(const BigInt& x) const
79
0
   {
80
   // TODO use Montgomery inverse here?
81
0
   return inverse_mod(x, p());
82
0
   }
83
84
BigInt Montgomery_Params::redc(const BigInt& x, secure_vector<word>& ws) const
85
72.3k
   {
86
72.3k
   const size_t output_size = 2*m_p_words + 2;
87
88
72.3k
   if(ws.size() < output_size)
89
72.3k
      ws.resize(output_size);
90
91
72.3k
   BigInt z = x;
92
72.3k
   z.grow_to(output_size);
93
94
72.3k
   bigint_monty_redc(z.mutable_data(),
95
72.3k
                     m_p.data(), m_p_words, m_p_dash,
96
72.3k
                     ws.data(), ws.size());
97
98
72.3k
   return z;
99
72.3k
   }
100
101
BigInt Montgomery_Params::mul(const BigInt& x, const BigInt& y,
102
                              secure_vector<word>& ws) const
103
999k
   {
104
999k
   const size_t output_size = 2*m_p_words + 2;
105
106
999k
   if(ws.size() < output_size)
107
998k
      ws.resize(output_size);
108
109
999k
   BOTAN_DEBUG_ASSERT(x.sig_words() <= m_p_words);
110
999k
   BOTAN_DEBUG_ASSERT(y.sig_words() <= m_p_words);
111
112
999k
   BigInt z = BigInt::with_capacity(output_size);
113
999k
   bigint_mul(z.mutable_data(), z.size(),
114
999k
              x.data(), x.size(), std::min(m_p_words, x.size()),
115
999k
              y.data(), y.size(), std::min(m_p_words, y.size()),
116
999k
              ws.data(), ws.size());
117
118
999k
   bigint_monty_redc(z.mutable_data(),
119
999k
                     m_p.data(), m_p_words, m_p_dash,
120
999k
                     ws.data(), ws.size());
121
122
999k
   return z;
123
999k
   }
124
125
BigInt Montgomery_Params::mul(const BigInt& x,
126
                              const secure_vector<word>& y,
127
                              secure_vector<word>& ws) const
128
0
   {
129
0
   const size_t output_size = 2*m_p_words + 2;
130
0
   if(ws.size() < output_size)
131
0
      ws.resize(output_size);
132
0
   BigInt z = BigInt::with_capacity(output_size);
133
134
0
   BOTAN_DEBUG_ASSERT(x.sig_words() <= m_p_words);
135
136
0
   bigint_mul(z.mutable_data(), z.size(),
137
0
              x.data(), x.size(), std::min(m_p_words, x.size()),
138
0
              y.data(), y.size(), std::min(m_p_words, y.size()),
139
0
              ws.data(), ws.size());
140
141
0
   bigint_monty_redc(z.mutable_data(),
142
0
                     m_p.data(), m_p_words, m_p_dash,
143
0
                     ws.data(), ws.size());
144
145
0
   return z;
146
0
   }
147
148
void Montgomery_Params::mul_by(BigInt& x,
149
                               const secure_vector<word>& y,
150
                               secure_vector<word>& ws) const
151
128k
   {
152
128k
   const size_t output_size = 2*m_p_words + 2;
153
154
128k
   if(ws.size() < 2*output_size)
155
0
      ws.resize(2*output_size);
156
157
128k
   word* z_data = &ws[0];
158
128k
   word* ws_data = &ws[output_size];
159
160
128k
   BOTAN_DEBUG_ASSERT(x.sig_words() <= m_p_words);
161
162
128k
   bigint_mul(z_data, output_size,
163
128k
              x.data(), x.size(), std::min(m_p_words, x.size()),
164
128k
              y.data(), y.size(), std::min(m_p_words, y.size()),
165
128k
              ws_data, output_size);
166
167
128k
   bigint_monty_redc(z_data,
168
128k
                     m_p.data(), m_p_words, m_p_dash,
169
128k
                     ws_data, output_size);
170
171
128k
   if(x.size() < output_size)
172
0
      x.grow_to(output_size);
173
128k
   copy_mem(x.mutable_data(), z_data, output_size);
174
128k
   }
175
176
void Montgomery_Params::mul_by(BigInt& x,
177
                               const BigInt& y,
178
                               secure_vector<word>& ws) const
179
911k
   {
180
911k
   const size_t output_size = 2*m_p_words + 2;
181
182
911k
   if(ws.size() < 2*output_size)
183
91
      ws.resize(2*output_size);
184
185
911k
   word* z_data = &ws[0];
186
911k
   word* ws_data = &ws[output_size];
187
188
911k
   BOTAN_DEBUG_ASSERT(x.sig_words() <= m_p_words);
189
190
911k
   bigint_mul(z_data, output_size,
191
911k
              x.data(), x.size(), std::min(m_p_words, x.size()),
192
911k
              y.data(), y.size(), std::min(m_p_words, y.size()),
193
911k
              ws_data, output_size);
194
195
911k
   bigint_monty_redc(z_data,
196
911k
                     m_p.data(), m_p_words, m_p_dash,
197
911k
                     ws_data, output_size);
198
199
911k
   if(x.size() < output_size)
200
72
      x.grow_to(output_size);
201
911k
   copy_mem(x.mutable_data(), z_data, output_size);
202
911k
   }
203
204
BigInt Montgomery_Params::sqr(const BigInt& x, secure_vector<word>& ws) const
205
218
   {
206
218
   const size_t output_size = 2*m_p_words + 2;
207
208
218
   if(ws.size() < output_size)
209
109
      ws.resize(output_size);
210
211
218
   BigInt z = BigInt::with_capacity(output_size);
212
213
218
   BOTAN_DEBUG_ASSERT(x.sig_words() <= m_p_words);
214
215
218
   bigint_sqr(z.mutable_data(), z.size(),
216
218
              x.data(), x.size(), std::min(m_p_words, x.size()),
217
218
              ws.data(), ws.size());
218
219
218
   bigint_monty_redc(z.mutable_data(),
220
218
                     m_p.data(), m_p_words, m_p_dash,
221
218
                     ws.data(), ws.size());
222
223
218
   return z;
224
218
   }
225
226
void Montgomery_Params::square_this(BigInt& x,
227
                                    secure_vector<word>& ws) const
228
6.73M
   {
229
6.73M
   const size_t output_size = 2*m_p_words + 2;
230
231
6.73M
   if(ws.size() < 2*output_size)
232
26.1k
      ws.resize(2*output_size);
233
234
6.73M
   word* z_data = &ws[0];
235
6.73M
   word* ws_data = &ws[output_size];
236
237
6.73M
   BOTAN_DEBUG_ASSERT(x.sig_words() <= m_p_words);
238
239
6.73M
   bigint_sqr(z_data, output_size,
240
6.73M
              x.data(), x.size(), std::min(m_p_words, x.size()),
241
6.73M
              ws_data, output_size);
242
243
6.73M
   bigint_monty_redc(z_data,
244
6.73M
                     m_p.data(), m_p_words, m_p_dash,
245
6.73M
                     ws_data, output_size);
246
247
6.73M
   if(x.size() < output_size)
248
1.54k
      x.grow_to(output_size);
249
6.73M
   copy_mem(x.mutable_data(), z_data, output_size);
250
6.73M
   }
251
252
Montgomery_Int::Montgomery_Int(const std::shared_ptr<const Montgomery_Params> params,
253
                               const BigInt& v,
254
                               bool redc_needed) :
255
   m_params(params)
256
1.07M
   {
257
1.07M
   if(redc_needed == false)
258
999k
      {
259
999k
      m_v = v;
260
999k
      }
261
72.6k
   else
262
72.6k
      {
263
72.6k
      BOTAN_ASSERT_NOMSG(m_v < m_params->p());
264
72.6k
      secure_vector<word> ws;
265
72.6k
      m_v = m_params->mul(v, m_params->R2(), ws);
266
72.6k
      }
267
1.07M
   }
268
269
Montgomery_Int::Montgomery_Int(std::shared_ptr<const Montgomery_Params> params,
270
                               const uint8_t bits[], size_t len,
271
                               bool redc_needed) :
272
   m_params(params),
273
   m_v(bits, len)
274
0
   {
275
0
   if(redc_needed)
276
0
      {
277
0
      BOTAN_ASSERT_NOMSG(m_v < m_params->p());
278
0
      secure_vector<word> ws;
279
0
      m_v = m_params->mul(m_v, m_params->R2(), ws);
280
0
      }
281
0
   }
282
283
Montgomery_Int::Montgomery_Int(std::shared_ptr<const Montgomery_Params> params,
284
                               const word words[], size_t len,
285
                               bool redc_needed) :
286
   m_params(params)
287
1.57k
   {
288
1.57k
   m_v.set_words(words, len);
289
290
1.57k
   if(redc_needed)
291
0
      {
292
0
      BOTAN_ASSERT_NOMSG(m_v < m_params->p());
293
0
      secure_vector<word> ws;
294
0
      m_v = m_params->mul(m_v, m_params->R2(), ws);
295
0
      }
296
1.57k
   }
297
298
void Montgomery_Int::fix_size()
299
1.07M
   {
300
1.07M
   const size_t p_words = m_params->p_words();
301
302
1.07M
   if(m_v.sig_words() > p_words)
303
3
      throw Internal_Error("Montgomery_Int::fix_size v too large");
304
305
1.07M
   m_v.grow_to(p_words);
306
1.07M
   }
307
308
bool Montgomery_Int::operator==(const Montgomery_Int& other) const
309
0
   {
310
0
   return m_v == other.m_v && m_params->p() == other.m_params->p();
311
0
   }
312
313
std::vector<uint8_t> Montgomery_Int::serialize() const
314
0
   {
315
0
   std::vector<uint8_t> v(size());
316
0
   BigInt::encode_1363(v.data(), v.size(), value());
317
0
   return v;
318
0
   }
319
320
size_t Montgomery_Int::size() const
321
0
   {
322
0
   return m_params->p().bytes();
323
0
   }
324
325
bool Montgomery_Int::is_one() const
326
0
   {
327
0
   return m_v == m_params->R1();
328
0
   }
329
330
bool Montgomery_Int::is_zero() const
331
0
   {
332
0
   return m_v.is_zero();
333
0
   }
334
335
BigInt Montgomery_Int::value() const
336
72.3k
   {
337
72.3k
   secure_vector<word> ws;
338
72.3k
   return m_params->redc(m_v, ws);
339
72.3k
   }
340
341
Montgomery_Int Montgomery_Int::operator+(const Montgomery_Int& other) const
342
0
   {
343
0
   secure_vector<word> ws;
344
0
   BigInt z = m_v;
345
0
   z.mod_add(other.m_v, m_params->p(), ws);
346
0
   return Montgomery_Int(m_params, z, false);
347
0
   }
348
349
Montgomery_Int Montgomery_Int::operator-(const Montgomery_Int& other) const
350
0
   {
351
0
   secure_vector<word> ws;
352
0
   BigInt z = m_v;
353
0
   z.mod_sub(other.m_v, m_params->p(), ws);
354
0
   return Montgomery_Int(m_params, z, false);
355
0
   }
356
357
Montgomery_Int& Montgomery_Int::operator+=(const Montgomery_Int& other)
358
0
   {
359
0
   secure_vector<word> ws;
360
0
   return this->add(other, ws);
361
0
   }
362
363
Montgomery_Int& Montgomery_Int::add(const Montgomery_Int& other, secure_vector<word>& ws)
364
0
   {
365
0
   m_v.mod_add(other.m_v, m_params->p(), ws);
366
0
   return (*this);
367
0
   }
368
369
Montgomery_Int& Montgomery_Int::operator-=(const Montgomery_Int& other)
370
0
   {
371
0
   secure_vector<word> ws;
372
0
   return this->sub(other, ws);
373
0
   }
374
375
Montgomery_Int& Montgomery_Int::sub(const Montgomery_Int& other, secure_vector<word>& ws)
376
0
   {
377
0
   m_v.mod_sub(other.m_v, m_params->p(), ws);
378
0
   return (*this);
379
0
   }
380
381
Montgomery_Int Montgomery_Int::operator*(const Montgomery_Int& other) const
382
925k
   {
383
925k
   secure_vector<word> ws;
384
925k
   return Montgomery_Int(m_params, m_params->mul(m_v, other.m_v, ws), false);
385
925k
   }
386
387
Montgomery_Int Montgomery_Int::mul(const Montgomery_Int& other,
388
                                   secure_vector<word>& ws) const
389
1.19k
   {
390
1.19k
   return Montgomery_Int(m_params, m_params->mul(m_v, other.m_v, ws), false);
391
1.19k
   }
392
393
Montgomery_Int& Montgomery_Int::mul_by(const Montgomery_Int& other,
394
                                       secure_vector<word>& ws)
395
911k
   {
396
911k
   m_params->mul_by(m_v, other.m_v, ws);
397
911k
   return (*this);
398
911k
   }
399
400
Montgomery_Int& Montgomery_Int::mul_by(const secure_vector<word>& other,
401
                                       secure_vector<word>& ws)
402
128k
   {
403
128k
   m_params->mul_by(m_v, other, ws);
404
128k
   return (*this);
405
128k
   }
406
407
Montgomery_Int& Montgomery_Int::operator*=(const Montgomery_Int& other)
408
0
   {
409
0
   secure_vector<word> ws;
410
0
   return mul_by(other, ws);
411
0
   }
412
413
Montgomery_Int& Montgomery_Int::operator*=(const secure_vector<word>& other)
414
0
   {
415
0
   secure_vector<word> ws;
416
0
   return mul_by(other, ws);
417
0
   }
418
419
Montgomery_Int& Montgomery_Int::square_this_n_times(secure_vector<word>& ws, size_t n)
420
1.99M
   {
421
8.66M
   for(size_t i = 0; i != n; ++i)
422
6.66M
      m_params->square_this(m_v, ws);
423
1.99M
   return (*this);
424
1.99M
   }
425
426
Montgomery_Int& Montgomery_Int::square_this(secure_vector<word>& ws)
427
70.1k
   {
428
70.1k
   m_params->square_this(m_v, ws);
429
70.1k
   return (*this);
430
70.1k
   }
431
432
Montgomery_Int Montgomery_Int::square(secure_vector<word>& ws) const
433
218
   {
434
218
   return Montgomery_Int(m_params, m_params->sqr(m_v, ws), false);
435
218
   }
436
437
Montgomery_Int Montgomery_Int::cube(secure_vector<word>& ws) const
438
0
   {
439
0
   return Montgomery_Int(m_params, m_params->sqr(m_v, ws), false);
440
0
   }
441
442
Montgomery_Int Montgomery_Int::multiplicative_inverse() const
443
0
   {
444
0
   secure_vector<word> ws;
445
0
   const BigInt iv = m_params->mul(m_params->inv_mod_p(m_v), m_params->R3(), ws);
446
0
   return Montgomery_Int(m_params, iv, false);
447
0
   }
448
449
Montgomery_Int Montgomery_Int::additive_inverse() const
450
0
   {
451
0
   return Montgomery_Int(m_params, m_params->p()) - (*this);
452
0
   }
453
454
Montgomery_Int& Montgomery_Int::mul_by_2(secure_vector<word>& ws)
455
0
   {
456
0
   m_v.mod_mul(2, m_params->p(), ws);
457
0
   return (*this);
458
0
   }
459
460
Montgomery_Int& Montgomery_Int::mul_by_3(secure_vector<word>& ws)
461
0
   {
462
0
   m_v.mod_mul(3, m_params->p(), ws);
463
0
   return (*this);
464
0
   }
465
466
Montgomery_Int& Montgomery_Int::mul_by_4(secure_vector<word>& ws)
467
0
   {
468
0
   m_v.mod_mul(4, m_params->p(), ws);
469
0
   return (*this);
470
0
   }
471
472
Montgomery_Int& Montgomery_Int::mul_by_8(secure_vector<word>& ws)
473
0
   {
474
0
   m_v.mod_mul(8, m_params->p(), ws);
475
0
   return (*this);
476
0
   }
477
478
}