Coverage Report

Created: 2020-05-23 13:54

/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
187M
   {
31
187M
   const auto mask = CT::Mask<word>::expand(cnd);
32
187M
33
6.71G
   for(size_t i = 0; i != size; ++i)
34
6.52G
      {
35
6.52G
      const word a = x[i];
36
6.52G
      const word b = y[i];
37
6.52G
      x[i] = mask.select(b, a);
38
6.52G
      y[i] = mask.select(a, b);
39
6.52G
      }
40
187M
   }
41
42
inline word bigint_cnd_add(word cnd, word x[], word x_size,
43
                           const word y[], size_t y_size)
44
205M
   {
45
205M
   BOTAN_ASSERT(x_size >= y_size, "Expected sizes");
46
205M
47
205M
   const auto mask = CT::Mask<word>::expand(cnd);
48
205M
49
205M
   word carry = 0;
50
205M
51
205M
   const size_t blocks = y_size - (y_size % 8);
52
205M
   word z[8] = { 0 };
53
205M
54
381M
   for(size_t i = 0; i != blocks; i += 8)
55
176M
      {
56
176M
      carry = word8_add3(z, x + i, y + i, carry);
57
176M
      mask.select_n(x + i, z, x + i, 8);
58
176M
      }
59
205M
60
897M
   for(size_t i = blocks; i != y_size; ++i)
61
691M
      {
62
691M
      z[0] = word_add(x[i], y[i], &carry);
63
691M
      x[i] = mask.select(z[0], x[i]);
64
691M
      }
65
205M
66
301M
   for(size_t i = y_size; i != x_size; ++i)
67
95.3M
      {
68
95.3M
      z[0] = word_add(x[i], 0, &carry);
69
95.3M
      x[i] = mask.select(z[0], x[i]);
70
95.3M
      }
71
205M
72
205M
   return mask.if_set_return(carry);
73
205M
   }
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
114M
   {
81
114M
   return bigint_cnd_add(cnd, x, size, y, size);
82
114M
   }
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
204M
   {
92
204M
   BOTAN_ASSERT(x_size >= y_size, "Expected sizes");
93
204M
94
204M
   const auto mask = CT::Mask<word>::expand(cnd);
95
204M
96
204M
   word carry = 0;
97
204M
98
204M
   const size_t blocks = y_size - (y_size % 8);
99
204M
   word z[8] = { 0 };
100
204M
101
449M
   for(size_t i = 0; i != blocks; i += 8)
102
244M
      {
103
244M
      carry = word8_sub3(z, x + i, y + i, carry);
104
244M
      mask.select_n(x + i, z, x + i, 8);
105
244M
      }
106
204M
107
485M
   for(size_t i = blocks; i != y_size; ++i)
108
280M
      {
109
280M
      z[0] = word_sub(x[i], y[i], &carry);
110
280M
      x[i] = mask.select(z[0], x[i]);
111
280M
      }
112
204M
113
204M
   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
204M
119
204M
   return mask.if_set_return(carry);
120
204M
   }
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
204M
   {
128
204M
   return bigint_cnd_sub(cnd, x, size, y, size);
129
204M
   }
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
1.90M
   {
141
1.90M
   const size_t blocks = size - (size % 8);
142
1.90M
143
1.90M
   word carry = 0;
144
1.90M
   word borrow = 0;
145
1.90M
146
1.90M
   word t0[8] = { 0 };
147
1.90M
   word t1[8] = { 0 };
148
1.90M
149
15.2M
   for(size_t i = 0; i != blocks; i += 8)
150
13.3M
      {
151
13.3M
      carry = word8_add3(t0, x + i, y + i, carry);
152
13.3M
      borrow = word8_sub3(t1, x + i, y + i, borrow);
153
13.3M
154
120M
      for(size_t j = 0; j != 8; ++j)
155
106M
         x[i+j] = mask.select(t0[j], t1[j]);
156
13.3M
      }
157
1.90M
158
1.91M
   for(size_t i = blocks; i != size; ++i)
159
16.3k
      {
160
16.3k
      const word a = word_add(x[i], y[i], &carry);
161
16.3k
      const word s = word_sub(x[i], y[i], &borrow);
162
16.3k
163
16.3k
      x[i] = mask.select(a, s);
164
16.3k
      }
165
1.90M
   }
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
115M
   {
180
115M
   const size_t blocks = size - (size % 8);
181
115M
182
115M
   word carry = 0;
183
115M
   word borrow = 0;
184
115M
185
115M
   word t0[8] = { 0 };
186
115M
   word t1[8] = { 0 };
187
115M
188
178M
   for(size_t i = 0; i != blocks; i += 8)
189
63.1M
      {
190
63.1M
      carry = word8_add3(t0, x + i, y + i, carry);
191
63.1M
      borrow = word8_sub3(t1, x + i, z + i, borrow);
192
63.1M
193
568M
      for(size_t j = 0; j != 8; ++j)
194
505M
         x[i+j] = mask.select(t0[j], t1[j]);
195
63.1M
      }
196
115M
197
427M
   for(size_t i = blocks; i != size; ++i)
198
312M
      {
199
312M
      t0[0] = word_add(x[i], y[i], &carry);
200
312M
      t1[0] = word_sub(x[i], z[i], &borrow);
201
312M
      x[i] = mask.select(t0[0], t1[0]);
202
312M
      }
203
115M
204
115M
   return mask.select(carry, borrow);
205
115M
   }
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
38.1M
   {
214
38.1M
   const auto mask = CT::Mask<word>::expand(cnd);
215
38.1M
216
38.1M
   word carry = mask.if_set_return(1);
217
580M
   for(size_t i = 0; i != size; ++i)
218
542M
      {
219
542M
      const word z = word_add(~x[i], 0, &carry);
220
542M
      x[i] = mask.select(z, x[i]);
221
542M
      }
222
38.1M
   }
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
31.3M
   {
229
31.3M
   word carry = 0;
230
31.3M
231
31.3M
   BOTAN_ASSERT(x_size >= y_size, "Expected sizes");
232
31.3M
233
31.3M
   const size_t blocks = y_size - (y_size % 8);
234
31.3M
235
76.6M
   for(size_t i = 0; i != blocks; i += 8)
236
45.2M
      carry = word8_add2(x + i, y + i, carry);
237
31.3M
238
74.3M
   for(size_t i = blocks; i != y_size; ++i)
239
42.9M
      x[i] = word_add(x[i], y[i], &carry);
240
31.3M
241
736M
   for(size_t i = y_size; i != x_size; ++i)
242
705M
      x[i] = word_add(x[i], 0, &carry);
243
31.3M
244
31.3M
   return carry;
245
31.3M
   }
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
148M
   {
254
148M
   if(x_size < y_size)
255
127k
      { return bigint_add3_nc(z, y, y_size, x, x_size); }
256
148M
257
148M
   word carry = 0;
258
148M
259
148M
   const size_t blocks = y_size - (y_size % 8);
260
148M
261
320M
   for(size_t i = 0; i != blocks; i += 8)
262
171M
      carry = word8_add3(z + i, x + i, y + i, carry);
263
148M
264
314M
   for(size_t i = blocks; i != y_size; ++i)
265
165M
      z[i] = word_add(x[i], y[i], &carry);
266
148M
267
149M
   for(size_t i = y_size; i != x_size; ++i)
268
501k
      z[i] = word_add(x[i], 0, &carry);
269
148M
270
148M
   return carry;
271
148M
   }
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
9.36M
   {
283
9.36M
   x[x_size] += bigint_add2_nc(x, x_size, y, y_size);
284
9.36M
   }
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.27M
   {
293
1.27M
   z[x_size > y_size ? x_size : y_size] +=
294
1.27M
      bigint_add3_nc(z, x, x_size, y, y_size);
295
1.27M
   }
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
99.1M
   {
303
99.1M
   word borrow = 0;
304
99.1M
305
99.1M
   BOTAN_ASSERT(x_size >= y_size, "Expected sizes");
306
99.1M
307
99.1M
   const size_t blocks = y_size - (y_size % 8);
308
99.1M
309
152M
   for(size_t i = 0; i != blocks; i += 8)
310
53.1M
      borrow = word8_sub2(x + i, y + i, borrow);
311
99.1M
312
571M
   for(size_t i = blocks; i != y_size; ++i)
313
472M
      x[i] = word_sub(x[i], y[i], &borrow);
314
99.1M
315
296M
   for(size_t i = y_size; i != x_size; ++i)
316
197M
      x[i] = word_sub(x[i], 0, &borrow);
317
99.1M
318
99.1M
   return borrow;
319
99.1M
   }
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
603k
   {
326
603k
   word borrow = 0;
327
603k
328
603k
   const size_t blocks = y_size - (y_size % 8);
329
603k
330
2.86M
   for(size_t i = 0; i != blocks; i += 8)
331
2.26M
      borrow = word8_sub2_rev(x + i, y + i, borrow);
332
603k
333
2.58M
   for(size_t i = blocks; i != y_size; ++i)
334
1.97M
      x[i] = word_sub(y[i], x[i], &borrow);
335
603k
336
603k
   BOTAN_ASSERT(borrow == 0, "y must be greater than x");
337
603k
   }
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
438M
   {
346
438M
   word borrow = 0;
347
438M
348
438M
   BOTAN_ASSERT(x_size >= y_size, "Expected sizes");
349
438M
350
438M
   const size_t blocks = y_size - (y_size % 8);
351
438M
352
1.20G
   for(size_t i = 0; i != blocks; i += 8)
353
771M
      borrow = word8_sub3(z + i, x + i, y + i, borrow);
354
438M
355
1.42G
   for(size_t i = blocks; i != y_size; ++i)
356
984M
      z[i] = word_sub(x[i], y[i], &borrow);
357
438M
358
1.64G
   for(size_t i = y_size; i != x_size; ++i)
359
1.20G
      z[i] = word_sub(x[i], 0, &borrow);
360
438M
361
438M
   return borrow;
362
438M
   }
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
9.22M
   {
381
9.22M
   // Subtract in both direction then conditional copy out the result
382
9.22M
383
9.22M
   word* ws0 = ws;
384
9.22M
   word* ws1 = ws + N;
385
9.22M
386
9.22M
   word borrow0 = 0;
387
9.22M
   word borrow1 = 0;
388
9.22M
389
9.22M
   const size_t blocks = N - (N % 8);
390
9.22M
391
31.1M
   for(size_t i = 0; i != blocks; i += 8)
392
21.9M
      {
393
21.9M
      borrow0 = word8_sub3(ws0 + i, x + i, y + i, borrow0);
394
21.9M
      borrow1 = word8_sub3(ws1 + i, y + i, x + i, borrow1);
395
21.9M
      }
396
9.22M
397
9.25M
   for(size_t i = blocks; i != N; ++i)
398
33.2k
      {
399
33.2k
      ws0[i] = word_sub(x[i], y[i], &borrow0);
400
33.2k
      ws1[i] = word_sub(y[i], x[i], &borrow1);
401
33.2k
      }
402
9.22M
403
9.22M
   return CT::conditional_copy_mem(borrow0, z, ws1, ws0, N);
404
9.22M
   }
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
4.55M
   {
412
4.55M
   copy_mem(x + word_shift, x, x_words);
413
4.55M
   clear_mem(x, word_shift);
414
4.55M
415
4.55M
   const auto carry_mask = CT::Mask<word>::expand(bit_shift);
416
4.55M
   const size_t carry_shift = carry_mask.if_set_return(BOTAN_MP_WORD_BITS - bit_shift);
417
4.55M
418
4.55M
   word carry = 0;
419
24.7M
   for(size_t i = word_shift; i != x_size; ++i)
420
20.1M
      {
421
20.1M
      const word w = x[i];
422
20.1M
      x[i] = (w << bit_shift) | carry;
423
20.1M
      carry = carry_mask.if_set_return(w >> carry_shift);
424
20.1M
      }
425
4.55M
   }
426
427
inline void bigint_shr1(word x[], size_t x_size,
428
                        size_t word_shift, size_t bit_shift)
429
99.8M
   {
430
99.8M
   const size_t top = x_size >= word_shift ? (x_size - word_shift) : 0;
431
99.8M
432
99.8M
   if(top > 0)
433
99.6M
      copy_mem(x, x + word_shift, top);
434
99.8M
   clear_mem(x + top, std::min(word_shift, x_size));
435
99.8M
436
99.8M
   const auto carry_mask = CT::Mask<word>::expand(bit_shift);
437
99.8M
   const size_t carry_shift = carry_mask.if_set_return(BOTAN_MP_WORD_BITS - bit_shift);
438
99.8M
439
99.8M
   word carry = 0;
440
99.8M
441
1.95G
   for(size_t i = 0; i != top; ++i)
442
1.85G
      {
443
1.85G
      const word w = x[top - i - 1];
444
1.85G
      x[top-i-1] = (w >> bit_shift) | carry;
445
1.85G
      carry = carry_mask.if_set_return(w << carry_shift);
446
1.85G
      }
447
99.8M
   }
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.27M
   {
452
2.27M
   copy_mem(y + word_shift, x, x_size);
453
2.27M
454
2.27M
   const auto carry_mask = CT::Mask<word>::expand(bit_shift);
455
2.27M
   const size_t carry_shift = carry_mask.if_set_return(BOTAN_MP_WORD_BITS - bit_shift);
456
2.27M
457
2.27M
   word carry = 0;
458
13.3M
   for(size_t i = word_shift; i != x_size + word_shift + 1; ++i)
459
11.0M
      {
460
11.0M
      const word w = y[i];
461
11.0M
      y[i] = (w << bit_shift) | carry;
462
11.0M
      carry = carry_mask.if_set_return(w >> carry_shift);
463
11.0M
      }
464
2.27M
   }
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
130M
   {
469
130M
   const size_t new_size = x_size < word_shift ? 0 : (x_size - word_shift);
470
130M
471
130M
   if(new_size > 0)
472
130M
      copy_mem(y, x + word_shift, new_size);
473
130M
474
130M
   const auto carry_mask = CT::Mask<word>::expand(bit_shift);
475
130M
   const size_t carry_shift = carry_mask.if_set_return(BOTAN_MP_WORD_BITS - bit_shift);
476
130M
477
130M
   word carry = 0;
478
1.42G
   for(size_t i = new_size; i > 0; --i)
479
1.29G
      {
480
1.29G
      word w = y[i-1];
481
1.29G
      y[i-1] = (w >> bit_shift) | carry;
482
1.29G
      carry = carry_mask.if_set_return(w << carry_shift);
483
1.29G
      }
484
130M
   }
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
191M
   {
491
191M
   const size_t blocks = x_size - (x_size % 8);
492
191M
493
191M
   word carry = 0;
494
191M
495
1.02G
   for(size_t i = 0; i != blocks; i += 8)
496
838M
      carry = word8_linmul2(x + i, y, carry);
497
191M
498
297M
   for(size_t i = blocks; i != x_size; ++i)
499
106M
      x[i] = word_madd2(x[i], y, &carry);
500
191M
501
191M
   return carry;
502
191M
   }
503
504
inline void bigint_linmul3(word z[], const word x[], size_t x_size, word y)
505
5.62M
   {
506
5.62M
   const size_t blocks = x_size - (x_size % 8);
507
5.62M
508
5.62M
   word carry = 0;
509
5.62M
510
35.7M
   for(size_t i = 0; i != blocks; i += 8)
511
30.1M
      carry = word8_linmul3(z + i, x + i, y, carry);
512
5.62M
513
22.1M
   for(size_t i = blocks; i != x_size; ++i)
514
16.5M
      z[i] = word_madd2(x[i], y, &carry);
515
5.62M
516
5.62M
   z[x_size] = carry;
517
5.62M
   }
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
17.9M
   {
528
17.9M
   static_assert(sizeof(word) >= sizeof(uint32_t), "Size assumption");
529
17.9M
530
17.9M
   const word LT = static_cast<word>(-1);
531
17.9M
   const word EQ = 0;
532
17.9M
   const word GT = 1;
533
17.9M
534
17.9M
   const size_t common_elems = std::min(x_size, y_size);
535
17.9M
536
17.9M
   word result = EQ; // until found otherwise
537
17.9M
538
412M
   for(size_t i = 0; i != common_elems; i++)
539
394M
      {
540
394M
      const auto is_eq = CT::Mask<word>::is_equal(x[i], y[i]);
541
394M
      const auto is_lt = CT::Mask<word>::is_lt(x[i], y[i]);
542
394M
543
394M
      result = is_eq.select(result, is_lt.select(LT, GT));
544
394M
      }
545
17.9M
546
17.9M
   if(x_size < y_size)
547
6.20M
      {
548
6.20M
      word mask = 0;
549
44.5M
      for(size_t i = x_size; i != y_size; i++)
550
38.3M
         mask |= y[i];
551
6.20M
552
6.20M
      // If any bits were set in high part of y, then x < y
553
6.20M
      result = CT::Mask<word>::is_zero(mask).select(result, LT);
554
6.20M
      }
555
11.7M
   else if(y_size < x_size)
556
318k
      {
557
318k
      word mask = 0;
558
4.84M
      for(size_t i = y_size; i != x_size; i++)
559
4.53M
         mask |= x[i];
560
318k
561
318k
      // If any bits were set in high part of x, then x > y
562
318k
      result = CT::Mask<word>::is_zero(mask).select(result, GT);
563
318k
      }
564
17.9M
565
17.9M
   CT::unpoison(result);
566
17.9M
   BOTAN_DEBUG_ASSERT(result == LT || result == GT || result == EQ);
567
17.9M
   return static_cast<int32_t>(result);
568
17.9M
   }
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
125M
   {
580
125M
   const size_t common_elems = std::min(x_size, y_size);
581
125M
582
125M
   auto is_lt = CT::Mask<word>::expand(lt_or_equal);
583
125M
584
977M
   for(size_t i = 0; i != common_elems; i++)
585
852M
      {
586
852M
      const auto eq = CT::Mask<word>::is_equal(x[i], y[i]);
587
852M
      const auto lt = CT::Mask<word>::is_lt(x[i], y[i]);
588
852M
      is_lt = eq.select_mask(is_lt, lt);
589
852M
      }
590
125M
591
125M
   if(x_size < y_size)
592
136k
      {
593
136k
      word mask = 0;
594
1.46M
      for(size_t i = x_size; i != y_size; i++)
595
1.32M
         mask |= y[i];
596
136k
      // If any bits were set in high part of y, then is_lt should be forced true
597
136k
      is_lt |= CT::Mask<word>::expand(mask);
598
136k
      }
599
125M
   else if(y_size < x_size)
600
248k
      {
601
248k
      word mask = 0;
602
1.26M
      for(size_t i = y_size; i != x_size; i++)
603
1.01M
         mask |= x[i];
604
248k
605
248k
      // If any bits were set in high part of x, then is_lt should be false
606
248k
      is_lt &= CT::Mask<word>::is_zero(mask);
607
248k
      }
608
125M
609
125M
   return is_lt;
610
125M
   }
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
210k
   {
616
210k
   const size_t common_elems = std::min(x_size, y_size);
617
210k
618
210k
   word diff = 0;
619
210k
620
2.02M
   for(size_t i = 0; i != common_elems; i++)
621
1.81M
      {
622
1.81M
      diff |= (x[i] ^ y[i]);
623
1.81M
      }
624
210k
625
210k
   // If any bits were set in high part of x/y, then they are not equal
626
210k
   if(x_size < y_size)
627
4.85k
      {
628
16.6k
      for(size_t i = x_size; i != y_size; i++)
629
11.7k
         diff |= y[i];
630
4.85k
      }
631
205k
   else if(y_size < x_size)
632
2.11k
      {
633
9.49k
      for(size_t i = y_size; i != x_size; i++)
634
7.38k
         diff |= x[i];
635
2.11k
      }
636
210k
637
210k
   return CT::Mask<word>::is_zero(diff);
638
210k
   }
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
12.8M
   {
658
12.8M
   const int32_t relative_size = bigint_cmp(x, x_size, y, y_size);
659
12.8M
660
12.8M
   // Swap if relative_size == -1
661
12.8M
   const bool need_swap = relative_size < 0;
662
12.8M
   CT::conditional_swap_ptr(need_swap, x, y);
663
12.8M
   CT::conditional_swap(need_swap, x_size, y_size);
664
12.8M
665
12.8M
   /*
666
12.8M
   * We know at this point that x >= y so if y_size is larger than
667
12.8M
   * x_size, we are guaranteed they are just leading zeros which can
668
12.8M
   * be ignored
669
12.8M
   */
670
12.8M
   y_size = std::min(x_size, y_size);
671
12.8M
672
12.8M
   bigint_sub3(z, x, x_size, y, y_size);
673
12.8M
674
12.8M
   return relative_size;
675
12.8M
   }
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
65.0M
   {
689
65.0M
   // is t < s or not?
690
65.0M
   const auto is_lt = bigint_ct_is_lt(t, mod_sw, s, mod_sw);
691
65.0M
692
65.0M
   // ws = p - s
693
65.0M
   const word borrow = bigint_sub3(ws, mod, mod_sw, s, mod_sw);
694
65.0M
695
65.0M
   // Compute either (t - s) or (t + (p - s)) depending on mask
696
65.0M
   const word carry = bigint_cnd_addsub(is_lt, t, ws, s, mod_sw);
697
65.0M
698
65.0M
   BOTAN_DEBUG_ASSERT(borrow == 0 && carry == 0);
699
65.0M
   BOTAN_UNUSED(carry, borrow);
700
65.0M
   }
701
702
template<size_t N>
703
inline void bigint_mod_sub_n(word t[], const word s[], const word mod[], word ws[])
704
50.0M
   {
705
50.0M
   // is t < s or not?
706
50.0M
   const auto is_lt = bigint_ct_is_lt(t, N, s, N);
707
50.0M
708
50.0M
   // ws = p - s
709
50.0M
   const word borrow = bigint_sub3(ws, mod, N, s, N);
710
50.0M
711
50.0M
   // Compute either (t - s) or (t + (p - s)) depending on mask
712
50.0M
   const word carry = bigint_cnd_addsub(is_lt, t, ws, s, N);
713
50.0M
714
50.0M
   BOTAN_DEBUG_ASSERT(borrow == 0 && carry == 0);
715
50.0M
   BOTAN_UNUSED(carry, borrow);
716
50.0M
   }
void Botan::bigint_mod_sub_n<4ul>(unsigned long*, unsigned long const*, unsigned long const*, unsigned long*)
Line
Count
Source
704
25.7M
   {
705
25.7M
   // is t < s or not?
706
25.7M
   const auto is_lt = bigint_ct_is_lt(t, N, s, N);
707
25.7M
708
25.7M
   // ws = p - s
709
25.7M
   const word borrow = bigint_sub3(ws, mod, N, s, N);
710
25.7M
711
25.7M
   // Compute either (t - s) or (t + (p - s)) depending on mask
712
25.7M
   const word carry = bigint_cnd_addsub(is_lt, t, ws, s, N);
713
25.7M
714
25.7M
   BOTAN_DEBUG_ASSERT(borrow == 0 && carry == 0);
715
25.7M
   BOTAN_UNUSED(carry, borrow);
716
25.7M
   }
void Botan::bigint_mod_sub_n<6ul>(unsigned long*, unsigned long const*, unsigned long const*, unsigned long*)
Line
Count
Source
704
24.3M
   {
705
24.3M
   // is t < s or not?
706
24.3M
   const auto is_lt = bigint_ct_is_lt(t, N, s, N);
707
24.3M
708
24.3M
   // ws = p - s
709
24.3M
   const word borrow = bigint_sub3(ws, mod, N, s, N);
710
24.3M
711
24.3M
   // Compute either (t - s) or (t + (p - s)) depending on mask
712
24.3M
   const word carry = bigint_cnd_addsub(is_lt, t, ws, s, N);
713
24.3M
714
24.3M
   BOTAN_DEBUG_ASSERT(borrow == 0 && carry == 0);
715
24.3M
   BOTAN_UNUSED(carry, borrow);
716
24.3M
   }
717
718
/**
719
* Compute ((n1<<bits) + n0) / d
720
*/
721
inline word bigint_divop(word n1, word n0, word d)
722
2.65M
   {
723
2.65M
   if(d == 0)
724
0
      throw Invalid_Argument("bigint_divop divide by zero");
725
2.65M
726
2.65M
#if defined(BOTAN_HAS_MP_DWORD)
727
2.65M
   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