Coverage Report

Created: 2020-06-30 13:58

/src/botan/build/include/botan/internal/mp_core.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
* MPI Algorithms
3
* (C) 1999-2010,2018 Jack Lloyd
4
*     2006 Luca Piccarreta
5
*     2016 Matthias Gierlings
6
*
7
* Botan is released under the Simplified BSD License (see license.txt)
8
*/
9
10
#ifndef BOTAN_MP_CORE_OPS_H_
11
#define BOTAN_MP_CORE_OPS_H_
12
13
#include <botan/types.h>
14
#include <botan/exceptn.h>
15
#include <botan/mem_ops.h>
16
#include <botan/internal/mp_asmi.h>
17
#include <botan/internal/ct_utils.h>
18
#include <algorithm>
19
20
namespace Botan {
21
22
const word MP_WORD_MAX = ~static_cast<word>(0);
23
24
/*
25
* If cond == 0, does nothing.
26
* If cond > 0, swaps x[0:size] with y[0:size]
27
* Runs in constant time
28
*/
29
inline void bigint_cnd_swap(word cnd, word x[], word y[], size_t size)
30
201M
   {
31
201M
   const auto mask = CT::Mask<word>::expand(cnd);
32
201M
33
7.17G
   for(size_t i = 0; i != size; ++i)
34
6.97G
      {
35
6.97G
      const word a = x[i];
36
6.97G
      const word b = y[i];
37
6.97G
      x[i] = mask.select(b, a);
38
6.97G
      y[i] = mask.select(a, b);
39
6.97G
      }
40
201M
   }
41
42
inline word bigint_cnd_add(word cnd, word x[], word x_size,
43
                           const word y[], size_t y_size)
44
233M
   {
45
233M
   BOTAN_ASSERT(x_size >= y_size, "Expected sizes");
46
233M
47
233M
   const auto mask = CT::Mask<word>::expand(cnd);
48
233M
49
233M
   word carry = 0;
50
233M
51
233M
   const size_t blocks = y_size - (y_size % 8);
52
233M
   word z[8] = { 0 };
53
233M
54
407M
   for(size_t i = 0; i != blocks; i += 8)
55
174M
      {
56
174M
      carry = word8_add3(z, x + i, y + i, carry);
57
174M
      mask.select_n(x + i, z, x + i, 8);
58
174M
      }
59
233M
60
1.03G
   for(size_t i = blocks; i != y_size; ++i)
61
800M
      {
62
800M
      z[0] = word_add(x[i], y[i], &carry);
63
800M
      x[i] = mask.select(z[0], x[i]);
64
800M
      }
65
233M
66
341M
   for(size_t i = y_size; i != x_size; ++i)
67
107M
      {
68
107M
      z[0] = word_add(x[i], 0, &carry);
69
107M
      x[i] = mask.select(z[0], x[i]);
70
107M
      }
71
233M
72
233M
   return mask.if_set_return(carry);
73
233M
   }
74
75
/*
76
* If cond > 0 adds x[0:size] and y[0:size] and returns carry
77
* Runs in constant time
78
*/
79
inline word bigint_cnd_add(word cnd, word x[], const word y[], size_t size)
80
130M
   {
81
130M
   return bigint_cnd_add(cnd, x, size, y, size);
82
130M
   }
83
84
/*
85
* If cond > 0 subtracts x[0:size] and y[0:size] and returns borrow
86
* Runs in constant time
87
*/
88
inline word bigint_cnd_sub(word cnd,
89
                           word x[], size_t x_size,
90
                           const word y[], size_t y_size)
91
232M
   {
92
232M
   BOTAN_ASSERT(x_size >= y_size, "Expected sizes");
93
232M
94
232M
   const auto mask = CT::Mask<word>::expand(cnd);
95
232M
96
232M
   word carry = 0;
97
232M
98
232M
   const size_t blocks = y_size - (y_size % 8);
99
232M
   word z[8] = { 0 };
100
232M
101
494M
   for(size_t i = 0; i != blocks; i += 8)
102
261M
      {
103
261M
      carry = word8_sub3(z, x + i, y + i, carry);
104
261M
      mask.select_n(x + i, z, x + i, 8);
105
261M
      }
106
232M
107
563M
   for(size_t i = blocks; i != y_size; ++i)
108
330M
      {
109
330M
      z[0] = word_sub(x[i], y[i], &carry);
110
330M
      x[i] = mask.select(z[0], x[i]);
111
330M
      }
112
232M
113
232M
   for(size_t i = y_size; i != x_size; ++i)
114
0
      {
115
0
      z[0] = word_sub(x[i], 0, &carry);
116
0
      x[i] = mask.select(z[0], x[i]);
117
0
      }
118
232M
119
232M
   return mask.if_set_return(carry);
120
232M
   }
121
122
/*
123
* If cond > 0 adds x[0:size] and y[0:size] and returns carry
124
* Runs in constant time
125
*/
126
inline word bigint_cnd_sub(word cnd, word x[], const word y[], size_t size)
127
232M
   {
128
232M
   return bigint_cnd_sub(cnd, x, size, y, size);
129
232M
   }
130
131
132
/*
133
* Equivalent to
134
*   bigint_cnd_add( mask, x, y, size);
135
*   bigint_cnd_sub(~mask, x, y, size);
136
*
137
* Mask must be either 0 or all 1 bits
138
*/
139
inline void bigint_cnd_add_or_sub(CT::Mask<word> mask, word x[], const word y[], size_t size)
140
2.10M
   {
141
2.10M
   const size_t blocks = size - (size % 8);
142
2.10M
143
2.10M
   word carry = 0;
144
2.10M
   word borrow = 0;
145
2.10M
146
2.10M
   word t0[8] = { 0 };
147
2.10M
   word t1[8] = { 0 };
148
2.10M
149
17.0M
   for(size_t i = 0; i != blocks; i += 8)
150
14.9M
      {
151
14.9M
      carry = word8_add3(t0, x + i, y + i, carry);
152
14.9M
      borrow = word8_sub3(t1, x + i, y + i, borrow);
153
14.9M
154
134M
      for(size_t j = 0; j != 8; ++j)
155
119M
         x[i+j] = mask.select(t0[j], t1[j]);
156
14.9M
      }
157
2.10M
158
2.11M
   for(size_t i = blocks; i != size; ++i)
159
18.6k
      {
160
18.6k
      const word a = word_add(x[i], y[i], &carry);
161
18.6k
      const word s = word_sub(x[i], y[i], &borrow);
162
18.6k
163
18.6k
      x[i] = mask.select(a, s);
164
18.6k
      }
165
2.10M
   }
166
167
/*
168
* Equivalent to
169
*   bigint_cnd_add( mask, x, size, y, size);
170
*   bigint_cnd_sub(~mask, x, size, z, size);
171
*
172
* Mask must be either 0 or all 1 bits
173
*
174
* Returns the carry or borrow resp
175
*/
176
inline word bigint_cnd_addsub(CT::Mask<word> mask, word x[],
177
                              const word y[], const word z[],
178
                              size_t size)
179
132M
   {
180
132M
   const size_t blocks = size - (size % 8);
181
132M
182
132M
   word carry = 0;
183
132M
   word borrow = 0;
184
132M
185
132M
   word t0[8] = { 0 };
186
132M
   word t1[8] = { 0 };
187
132M
188
204M
   for(size_t i = 0; i != blocks; i += 8)
189
72.6M
      {
190
72.6M
      carry = word8_add3(t0, x + i, y + i, carry);
191
72.6M
      borrow = word8_sub3(t1, x + i, z + i, borrow);
192
72.6M
193
653M
      for(size_t j = 0; j != 8; ++j)
194
581M
         x[i+j] = mask.select(t0[j], t1[j]);
195
72.6M
      }
196
132M
197
491M
   for(size_t i = blocks; i != size; ++i)
198
358M
      {
199
358M
      t0[0] = word_add(x[i], y[i], &carry);
200
358M
      t1[0] = word_sub(x[i], z[i], &borrow);
201
358M
      x[i] = mask.select(t0[0], t1[0]);
202
358M
      }
203
132M
204
132M
   return mask.select(carry, borrow);
205
132M
   }
206
207
/*
208
* 2s complement absolute value
209
* If cond > 0 sets x to ~x + 1
210
* Runs in constant time
211
*/
212
inline void bigint_cnd_abs(word cnd, word x[], size_t size)
213
43.3M
   {
214
43.3M
   const auto mask = CT::Mask<word>::expand(cnd);
215
43.3M
216
43.3M
   word carry = mask.if_set_return(1);
217
597M
   for(size_t i = 0; i != size; ++i)
218
553M
      {
219
553M
      const word z = word_add(~x[i], 0, &carry);
220
553M
      x[i] = mask.select(z, x[i]);
221
553M
      }
222
43.3M
   }
223
224
/**
225
* Two operand addition with carry out
226
*/
227
inline word bigint_add2_nc(word x[], size_t x_size, const word y[], size_t y_size)
228
34.5M
   {
229
34.5M
   word carry = 0;
230
34.5M
231
34.5M
   BOTAN_ASSERT(x_size >= y_size, "Expected sizes");
232
34.5M
233
34.5M
   const size_t blocks = y_size - (y_size % 8);
234
34.5M
235
85.1M
   for(size_t i = 0; i != blocks; i += 8)
236
50.6M
      carry = word8_add2(x + i, y + i, carry);
237
34.5M
238
82.1M
   for(size_t i = blocks; i != y_size; ++i)
239
47.6M
      x[i] = word_add(x[i], y[i], &carry);
240
34.5M
241
788M
   for(size_t i = y_size; i != x_size; ++i)
242
754M
      x[i] = word_add(x[i], 0, &carry);
243
34.5M
244
34.5M
   return carry;
245
34.5M
   }
246
247
/**
248
* Three operand addition with carry out
249
*/
250
inline word bigint_add3_nc(word z[],
251
                           const word x[], size_t x_size,
252
                           const word y[], size_t y_size)
253
169M
   {
254
169M
   if(x_size < y_size)
255
153k
      { return bigint_add3_nc(z, y, y_size, x, x_size); }
256
169M
257
169M
   word carry = 0;
258
169M
259
169M
   const size_t blocks = y_size - (y_size % 8);
260
169M
261
363M
   for(size_t i = 0; i != blocks; i += 8)
262
194M
      carry = word8_add3(z + i, x + i, y + i, carry);
263
169M
264
358M
   for(size_t i = blocks; i != y_size; ++i)
265
189M
      z[i] = word_add(x[i], y[i], &carry);
266
169M
267
169M
   for(size_t i = y_size; i != x_size; ++i)
268
623k
      z[i] = word_add(x[i], 0, &carry);
269
169M
270
169M
   return carry;
271
169M
   }
272
273
/**
274
* Two operand addition
275
* @param x the first operand (and output)
276
* @param x_size size of x
277
* @param y the second operand
278
* @param y_size size of y (must be >= x_size)
279
*/
280
inline void bigint_add2(word x[], size_t x_size,
281
                        const word y[], size_t y_size)
282
10.6M
   {
283
10.6M
   x[x_size] += bigint_add2_nc(x, x_size, y, y_size);
284
10.6M
   }
285
286
/**
287
* Three operand addition
288
*/
289
inline void bigint_add3(word z[],
290
                        const word x[], size_t x_size,
291
                        const word y[], size_t y_size)
292
1.52M
   {
293
1.52M
   z[x_size > y_size ? x_size : y_size] +=
294
1.52M
      bigint_add3_nc(z, x, x_size, y, y_size);
295
1.52M
   }
296
297
/**
298
* Two operand subtraction
299
*/
300
inline word bigint_sub2(word x[], size_t x_size,
301
                        const word y[], size_t y_size)
302
111M
   {
303
111M
   word borrow = 0;
304
111M
305
111M
   BOTAN_ASSERT(x_size >= y_size, "Expected sizes");
306
111M
307
111M
   const size_t blocks = y_size - (y_size % 8);
308
111M
309
168M
   for(size_t i = 0; i != blocks; i += 8)
310
56.9M
      borrow = word8_sub2(x + i, y + i, borrow);
311
111M
312
646M
   for(size_t i = blocks; i != y_size; ++i)
313
534M
      x[i] = word_sub(x[i], y[i], &borrow);
314
111M
315
331M
   for(size_t i = y_size; i != x_size; ++i)
316
220M
      x[i] = word_sub(x[i], 0, &borrow);
317
111M
318
111M
   return borrow;
319
111M
   }
320
321
/**
322
* Two operand subtraction, x = y - x; assumes y >= x
323
*/
324
inline void bigint_sub2_rev(word x[], const word y[], size_t y_size)
325
656k
   {
326
656k
   word borrow = 0;
327
656k
328
656k
   const size_t blocks = y_size - (y_size % 8);
329
656k
330
3.09M
   for(size_t i = 0; i != blocks; i += 8)
331
2.44M
      borrow = word8_sub2_rev(x + i, y + i, borrow);
332
656k
333
2.98M
   for(size_t i = blocks; i != y_size; ++i)
334
2.32M
      x[i] = word_sub(y[i], x[i], &borrow);
335
656k
336
656k
   BOTAN_ASSERT(borrow == 0, "y must be greater than x");
337
656k
   }
338
339
/**
340
* Three operand subtraction
341
*/
342
inline word bigint_sub3(word z[],
343
                        const word x[], size_t x_size,
344
                        const word y[], size_t y_size)
345
489M
   {
346
489M
   word borrow = 0;
347
489M
348
489M
   BOTAN_ASSERT(x_size >= y_size, "Expected sizes");
349
489M
350
489M
   const size_t blocks = y_size - (y_size % 8);
351
489M
352
1.32G
   for(size_t i = 0; i != blocks; i += 8)
353
839M
      borrow = word8_sub3(z + i, x + i, y + i, borrow);
354
489M
355
1.62G
   for(size_t i = blocks; i != y_size; ++i)
356
1.13G
      z[i] = word_sub(x[i], y[i], &borrow);
357
489M
358
1.76G
   for(size_t i = y_size; i != x_size; ++i)
359
1.27G
      z[i] = word_sub(x[i], 0, &borrow);
360
489M
361
489M
   return borrow;
362
489M
   }
363
364
/**
365
* Return abs(x-y), ie if x >= y, then compute z = x - y
366
* Otherwise compute z = y - x
367
* No borrow is possible since the result is always >= 0
368
*
369
* Returns ~0 if x >= y or 0 if x < y
370
* @param z output array of at least N words
371
* @param x input array of N words
372
* @param y input array of N words
373
* @param N length of x and y
374
* @param ws array of at least 2*N words
375
*/
376
inline CT::Mask<word>
377
bigint_sub_abs(word z[],
378
               const word x[], const word y[], size_t N,
379
               word ws[])
380
10.0M
   {
381
10.0M
   // Subtract in both direction then conditional copy out the result
382
10.0M
383
10.0M
   word* ws0 = ws;
384
10.0M
   word* ws1 = ws + N;
385
10.0M
386
10.0M
   word borrow0 = 0;
387
10.0M
   word borrow1 = 0;
388
10.0M
389
10.0M
   const size_t blocks = N - (N % 8);
390
10.0M
391
34.2M
   for(size_t i = 0; i != blocks; i += 8)
392
24.2M
      {
393
24.2M
      borrow0 = word8_sub3(ws0 + i, x + i, y + i, borrow0);
394
24.2M
      borrow1 = word8_sub3(ws1 + i, y + i, x + i, borrow1);
395
24.2M
      }
396
10.0M
397
10.0M
   for(size_t i = blocks; i != N; ++i)
398
39.5k
      {
399
39.5k
      ws0[i] = word_sub(x[i], y[i], &borrow0);
400
39.5k
      ws1[i] = word_sub(y[i], x[i], &borrow1);
401
39.5k
      }
402
10.0M
403
10.0M
   return CT::conditional_copy_mem(borrow0, z, ws1, ws0, N);
404
10.0M
   }
405
406
/*
407
* Shift Operations
408
*/
409
inline void bigint_shl1(word x[], size_t x_size, size_t x_words,
410
                        size_t word_shift, size_t bit_shift)
411
5.18M
   {
412
5.18M
   copy_mem(x + word_shift, x, x_words);
413
5.18M
   clear_mem(x, word_shift);
414
5.18M
415
5.18M
   const auto carry_mask = CT::Mask<word>::expand(bit_shift);
416
5.18M
   const size_t carry_shift = carry_mask.if_set_return(BOTAN_MP_WORD_BITS - bit_shift);
417
5.18M
418
5.18M
   word carry = 0;
419
28.0M
   for(size_t i = word_shift; i != x_size; ++i)
420
22.8M
      {
421
22.8M
      const word w = x[i];
422
22.8M
      x[i] = (w << bit_shift) | carry;
423
22.8M
      carry = carry_mask.if_set_return(w >> carry_shift);
424
22.8M
      }
425
5.18M
   }
426
427
inline void bigint_shr1(word x[], size_t x_size,
428
                        size_t word_shift, size_t bit_shift)
429
113M
   {
430
113M
   const size_t top = x_size >= word_shift ? (x_size - word_shift) : 0;
431
113M
432
113M
   if(top > 0)
433
113M
      copy_mem(x, x + word_shift, top);
434
113M
   clear_mem(x + top, std::min(word_shift, x_size));
435
113M
436
113M
   const auto carry_mask = CT::Mask<word>::expand(bit_shift);
437
113M
   const size_t carry_shift = carry_mask.if_set_return(BOTAN_MP_WORD_BITS - bit_shift);
438
113M
439
113M
   word carry = 0;
440
113M
441
2.05G
   for(size_t i = 0; i != top; ++i)
442
1.94G
      {
443
1.94G
      const word w = x[top - i - 1];
444
1.94G
      x[top-i-1] = (w >> bit_shift) | carry;
445
1.94G
      carry = carry_mask.if_set_return(w << carry_shift);
446
1.94G
      }
447
113M
   }
448
449
inline void bigint_shl2(word y[], const word x[], size_t x_size,
450
                        size_t word_shift, size_t bit_shift)
451
2.59M
   {
452
2.59M
   copy_mem(y + word_shift, x, x_size);
453
2.59M
454
2.59M
   const auto carry_mask = CT::Mask<word>::expand(bit_shift);
455
2.59M
   const size_t carry_shift = carry_mask.if_set_return(BOTAN_MP_WORD_BITS - bit_shift);
456
2.59M
457
2.59M
   word carry = 0;
458
15.1M
   for(size_t i = word_shift; i != x_size + word_shift + 1; ++i)
459
12.5M
      {
460
12.5M
      const word w = y[i];
461
12.5M
      y[i] = (w << bit_shift) | carry;
462
12.5M
      carry = carry_mask.if_set_return(w >> carry_shift);
463
12.5M
      }
464
2.59M
   }
465
466
inline void bigint_shr2(word y[], const word x[], size_t x_size,
467
                        size_t word_shift, size_t bit_shift)
468
148M
   {
469
148M
   const size_t new_size = x_size < word_shift ? 0 : (x_size - word_shift);
470
148M
471
148M
   if(new_size > 0)
472
148M
      copy_mem(y, x + word_shift, new_size);
473
148M
474
148M
   const auto carry_mask = CT::Mask<word>::expand(bit_shift);
475
148M
   const size_t carry_shift = carry_mask.if_set_return(BOTAN_MP_WORD_BITS - bit_shift);
476
148M
477
148M
   word carry = 0;
478
1.62G
   for(size_t i = new_size; i > 0; --i)
479
1.47G
      {
480
1.47G
      word w = y[i-1];
481
1.47G
      y[i-1] = (w >> bit_shift) | carry;
482
1.47G
      carry = carry_mask.if_set_return(w << carry_shift);
483
1.47G
      }
484
148M
   }
485
486
/*
487
* Linear Multiply - returns the carry
488
*/
489
inline word BOTAN_WARN_UNUSED_RESULT bigint_linmul2(word x[], size_t x_size, word y)
490
206M
   {
491
206M
   const size_t blocks = x_size - (x_size % 8);
492
206M
493
206M
   word carry = 0;
494
206M
495
1.11G
   for(size_t i = 0; i != blocks; i += 8)
496
906M
      carry = word8_linmul2(x + i, y, carry);
497
206M
498
329M
   for(size_t i = blocks; i != x_size; ++i)
499
122M
      x[i] = word_madd2(x[i], y, &carry);
500
206M
501
206M
   return carry;
502
206M
   }
503
504
inline void bigint_linmul3(word z[], const word x[], size_t x_size, word y)
505
6.17M
   {
506
6.17M
   const size_t blocks = x_size - (x_size % 8);
507
6.17M
508
6.17M
   word carry = 0;
509
6.17M
510
37.9M
   for(size_t i = 0; i != blocks; i += 8)
511
31.7M
      carry = word8_linmul3(z + i, x + i, y, carry);
512
6.17M
513
25.0M
   for(size_t i = blocks; i != x_size; ++i)
514
18.8M
      z[i] = word_madd2(x[i], y, &carry);
515
6.17M
516
6.17M
   z[x_size] = carry;
517
6.17M
   }
518
519
/**
520
* Compare x and y
521
* Return -1 if x < y
522
* Return 0 if x == y
523
* Return 1 if x > y
524
*/
525
inline int32_t bigint_cmp(const word x[], size_t x_size,
526
                          const word y[], size_t y_size)
527
20.2M
   {
528
20.2M
   static_assert(sizeof(word) >= sizeof(uint32_t), "Size assumption");
529
20.2M
530
20.2M
   const word LT = static_cast<word>(-1);
531
20.2M
   const word EQ = 0;
532
20.2M
   const word GT = 1;
533
20.2M
534
20.2M
   const size_t common_elems = std::min(x_size, y_size);
535
20.2M
536
20.2M
   word result = EQ; // until found otherwise
537
20.2M
538
450M
   for(size_t i = 0; i != common_elems; i++)
539
430M
      {
540
430M
      const auto is_eq = CT::Mask<word>::is_equal(x[i], y[i]);
541
430M
      const auto is_lt = CT::Mask<word>::is_lt(x[i], y[i]);
542
430M
543
430M
      result = is_eq.select(result, is_lt.select(LT, GT));
544
430M
      }
545
20.2M
546
20.2M
   if(x_size < y_size)
547
7.04M
      {
548
7.04M
      word mask = 0;
549
49.0M
      for(size_t i = x_size; i != y_size; i++)
550
42.0M
         mask |= y[i];
551
7.04M
552
7.04M
      // If any bits were set in high part of y, then x < y
553
7.04M
      result = CT::Mask<word>::is_zero(mask).select(result, LT);
554
7.04M
      }
555
13.1M
   else if(y_size < x_size)
556
365k
      {
557
365k
      word mask = 0;
558
5.78M
      for(size_t i = y_size; i != x_size; i++)
559
5.42M
         mask |= x[i];
560
365k
561
365k
      // If any bits were set in high part of x, then x > y
562
365k
      result = CT::Mask<word>::is_zero(mask).select(result, GT);
563
365k
      }
564
20.2M
565
20.2M
   CT::unpoison(result);
566
20.2M
   BOTAN_DEBUG_ASSERT(result == LT || result == GT || result == EQ);
567
20.2M
   return static_cast<int32_t>(result);
568
20.2M
   }
569
570
/**
571
* Compare x and y
572
* Return ~0 if x[0:x_size] < y[0:y_size] or 0 otherwise
573
* If lt_or_equal is true, returns ~0 also for x == y
574
*/
575
inline CT::Mask<word>
576
bigint_ct_is_lt(const word x[], size_t x_size,
577
                const word y[], size_t y_size,
578
                bool lt_or_equal = false)
579
143M
   {
580
143M
   const size_t common_elems = std::min(x_size, y_size);
581
143M
582
143M
   auto is_lt = CT::Mask<word>::expand(lt_or_equal);
583
143M
584
1.12G
   for(size_t i = 0; i != common_elems; i++)
585
978M
      {
586
978M
      const auto eq = CT::Mask<word>::is_equal(x[i], y[i]);
587
978M
      const auto lt = CT::Mask<word>::is_lt(x[i], y[i]);
588
978M
      is_lt = eq.select_mask(is_lt, lt);
589
978M
      }
590
143M
591
143M
   if(x_size < y_size)
592
151k
      {
593
151k
      word mask = 0;
594
1.55M
      for(size_t i = x_size; i != y_size; i++)
595
1.40M
         mask |= y[i];
596
151k
      // If any bits were set in high part of y, then is_lt should be forced true
597
151k
      is_lt |= CT::Mask<word>::expand(mask);
598
151k
      }
599
143M
   else if(y_size < x_size)
600
283k
      {
601
283k
      word mask = 0;
602
1.42M
      for(size_t i = y_size; i != x_size; i++)
603
1.13M
         mask |= x[i];
604
283k
605
283k
      // If any bits were set in high part of x, then is_lt should be false
606
283k
      is_lt &= CT::Mask<word>::is_zero(mask);
607
283k
      }
608
143M
609
143M
   return is_lt;
610
143M
   }
611
612
inline CT::Mask<word>
613
bigint_ct_is_eq(const word x[], size_t x_size,
614
                const word y[], size_t y_size)
615
272k
   {
616
272k
   const size_t common_elems = std::min(x_size, y_size);
617
272k
618
272k
   word diff = 0;
619
272k
620
3.15M
   for(size_t i = 0; i != common_elems; i++)
621
2.88M
      {
622
2.88M
      diff |= (x[i] ^ y[i]);
623
2.88M
      }
624
272k
625
272k
   // If any bits were set in high part of x/y, then they are not equal
626
272k
   if(x_size < y_size)
627
6.01k
      {
628
19.8k
      for(size_t i = x_size; i != y_size; i++)
629
13.7k
         diff |= y[i];
630
6.01k
      }
631
266k
   else if(y_size < x_size)
632
2.56k
      {
633
11.5k
      for(size_t i = y_size; i != x_size; i++)
634
9.00k
         diff |= x[i];
635
2.56k
      }
636
272k
637
272k
   return CT::Mask<word>::is_zero(diff);
638
272k
   }
639
640
/**
641
* Set z to abs(x-y), ie if x >= y, then compute z = x - y
642
* Otherwise compute z = y - x
643
* No borrow is possible since the result is always >= 0
644
*
645
* Return the relative size of x vs y (-1, 0, 1)
646
*
647
* @param z output array of max(x_size,y_size) words
648
* @param x input param
649
* @param x_size length of x
650
* @param y input param
651
* @param y_size length of y
652
*/
653
inline int32_t
654
bigint_sub_abs(word z[],
655
               const word x[], size_t x_size,
656
               const word y[], size_t y_size)
657
14.6M
   {
658
14.6M
   const int32_t relative_size = bigint_cmp(x, x_size, y, y_size);
659
14.6M
660
14.6M
   // Swap if relative_size == -1
661
14.6M
   const bool need_swap = relative_size < 0;
662
14.6M
   CT::conditional_swap_ptr(need_swap, x, y);
663
14.6M
   CT::conditional_swap(need_swap, x_size, y_size);
664
14.6M
665
14.6M
   /*
666
14.6M
   * We know at this point that x >= y so if y_size is larger than
667
14.6M
   * x_size, we are guaranteed they are just leading zeros which can
668
14.6M
   * be ignored
669
14.6M
   */
670
14.6M
   y_size = std::min(x_size, y_size);
671
14.6M
672
14.6M
   bigint_sub3(z, x, x_size, y, y_size);
673
14.6M
674
14.6M
   return relative_size;
675
14.6M
   }
676
677
/**
678
* Set t to t-s modulo mod
679
*
680
* @param t first integer
681
* @param s second integer
682
* @param mod the modulus
683
* @param mod_sw size of t, s, and mod
684
* @param ws workspace of size mod_sw
685
*/
686
inline void
687
bigint_mod_sub(word t[], const word s[], const word mod[], size_t mod_sw, word ws[])
688
74.5M
   {
689
74.5M
   // is t < s or not?
690
74.5M
   const auto is_lt = bigint_ct_is_lt(t, mod_sw, s, mod_sw);
691
74.5M
692
74.5M
   // ws = p - s
693
74.5M
   const word borrow = bigint_sub3(ws, mod, mod_sw, s, mod_sw);
694
74.5M
695
74.5M
   // Compute either (t - s) or (t + (p - s)) depending on mask
696
74.5M
   const word carry = bigint_cnd_addsub(is_lt, t, ws, s, mod_sw);
697
74.5M
698
74.5M
   BOTAN_DEBUG_ASSERT(borrow == 0 && carry == 0);
699
74.5M
   BOTAN_UNUSED(carry, borrow);
700
74.5M
   }
701
702
template<size_t N>
703
inline void bigint_mod_sub_n(word t[], const word s[], const word mod[], word ws[])
704
57.8M
   {
705
57.8M
   // is t < s or not?
706
57.8M
   const auto is_lt = bigint_ct_is_lt(t, N, s, N);
707
57.8M
708
57.8M
   // ws = p - s
709
57.8M
   const word borrow = bigint_sub3(ws, mod, N, s, N);
710
57.8M
711
57.8M
   // Compute either (t - s) or (t + (p - s)) depending on mask
712
57.8M
   const word carry = bigint_cnd_addsub(is_lt, t, ws, s, N);
713
57.8M
714
57.8M
   BOTAN_DEBUG_ASSERT(borrow == 0 && carry == 0);
715
57.8M
   BOTAN_UNUSED(carry, borrow);
716
57.8M
   }
void Botan::bigint_mod_sub_n<4ul>(unsigned long*, unsigned long const*, unsigned long const*, unsigned long*)
Line
Count
Source
704
29.5M
   {
705
29.5M
   // is t < s or not?
706
29.5M
   const auto is_lt = bigint_ct_is_lt(t, N, s, N);
707
29.5M
708
29.5M
   // ws = p - s
709
29.5M
   const word borrow = bigint_sub3(ws, mod, N, s, N);
710
29.5M
711
29.5M
   // Compute either (t - s) or (t + (p - s)) depending on mask
712
29.5M
   const word carry = bigint_cnd_addsub(is_lt, t, ws, s, N);
713
29.5M
714
29.5M
   BOTAN_DEBUG_ASSERT(borrow == 0 && carry == 0);
715
29.5M
   BOTAN_UNUSED(carry, borrow);
716
29.5M
   }
void Botan::bigint_mod_sub_n<6ul>(unsigned long*, unsigned long const*, unsigned long const*, unsigned long*)
Line
Count
Source
704
28.2M
   {
705
28.2M
   // is t < s or not?
706
28.2M
   const auto is_lt = bigint_ct_is_lt(t, N, s, N);
707
28.2M
708
28.2M
   // ws = p - s
709
28.2M
   const word borrow = bigint_sub3(ws, mod, N, s, N);
710
28.2M
711
28.2M
   // Compute either (t - s) or (t + (p - s)) depending on mask
712
28.2M
   const word carry = bigint_cnd_addsub(is_lt, t, ws, s, N);
713
28.2M
714
28.2M
   BOTAN_DEBUG_ASSERT(borrow == 0 && carry == 0);
715
28.2M
   BOTAN_UNUSED(carry, borrow);
716
28.2M
   }
717
718
/**
719
* Compute ((n1<<bits) + n0) / d
720
*/
721
inline word bigint_divop(word n1, word n0, word d)
722
2.98M
   {
723
2.98M
   if(d == 0)
724
0
      throw Invalid_Argument("bigint_divop divide by zero");
725
2.98M
726
2.98M
#if defined(BOTAN_HAS_MP_DWORD)
727
2.98M
   return ((static_cast<dword>(n1) << BOTAN_MP_WORD_BITS) | n0) / d;
728
#else
729
730
   word high = n1 % d;
731
   word quotient = 0;
732
733
   for(size_t i = 0; i != BOTAN_MP_WORD_BITS; ++i)
734
      {
735
      const word high_top_bit = high >> (BOTAN_MP_WORD_BITS-1);
736
737
      high <<= 1;
738
      high |= (n0 >> (BOTAN_MP_WORD_BITS-1-i)) & 1;
739
      quotient <<= 1;
740
741
      if(high_top_bit || high >= d)
742
         {
743
         high -= d;
744
         quotient |= 1;
745
         }
746
      }
747
748
   return quotient;
749
#endif
750
   }
751
752
/**
753
* Compute ((n1<<bits) + n0) % d
754
*/
755
inline word bigint_modop(word n1, word n0, word d)
756
1.02k
   {
757
1.02k
   if(d == 0)
758
0
      throw Invalid_Argument("bigint_modop divide by zero");
759
1.02k
760
1.02k
#if defined(BOTAN_HAS_MP_DWORD)
761
1.02k
   return ((static_cast<dword>(n1) << BOTAN_MP_WORD_BITS) | n0) % d;
762
#else
763
   word z = bigint_divop(n1, n0, d);
764
   word dummy = 0;
765
   z = word_madd2(z, d, &dummy);
766
   return (n0-z);
767
#endif
768
   }
769
770
/*
771
* Comba Multiplication / Squaring
772
*/
773
void bigint_comba_mul4(word z[8], const word x[4], const word y[4]);
774
void bigint_comba_mul6(word z[12], const word x[6], const word y[6]);
775
void bigint_comba_mul8(word z[16], const word x[8], const word y[8]);
776
void bigint_comba_mul9(word z[18], const word x[9], const word y[9]);
777
void bigint_comba_mul16(word z[32], const word x[16], const word y[16]);
778
void bigint_comba_mul24(word z[48], const word x[24], const word y[24]);
779
780
void bigint_comba_sqr4(word out[8], const word in[4]);
781
void bigint_comba_sqr6(word out[12], const word in[6]);
782
void bigint_comba_sqr8(word out[16], const word in[8]);
783
void bigint_comba_sqr9(word out[18], const word in[9]);
784
void bigint_comba_sqr16(word out[32], const word in[16]);
785
void bigint_comba_sqr24(word out[48], const word in[24]);
786
787
/**
788
* Montgomery Reduction
789
* @param z integer to reduce, of size exactly 2*(p_size+1).
790
           Output is in the first p_size+1 words, higher
791
           words are set to zero.
792
* @param p modulus
793
* @param p_size size of p
794
* @param p_dash Montgomery value
795
* @param workspace array of at least 2*(p_size+1) words
796
* @param ws_size size of workspace in words
797
*/
798
void bigint_monty_redc(word z[],
799
                       const word p[], size_t p_size,
800
                       word p_dash,
801
                       word workspace[],
802
                       size_t ws_size);
803
804
/*
805
* High Level Multiplication/Squaring Interfaces
806
*/
807
808
void bigint_mul(word z[], size_t z_size,
809
                const word x[], size_t x_size, size_t x_sw,
810
                const word y[], size_t y_size, size_t y_sw,
811
                word workspace[], size_t ws_size);
812
813
void bigint_sqr(word z[], size_t z_size,
814
                const word x[], size_t x_size, size_t x_sw,
815
                word workspace[], size_t ws_size);
816
817
}
818
819
#endif