Coverage Report

Created: 2023-06-07 06:59

/src/botan/src/lib/math/mp/mp_karat.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
* Multiplication and Squaring
3
* (C) 1999-2010,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/mp_core.h>
10
11
#include <botan/exceptn.h>
12
#include <botan/mem_ops.h>
13
#include <botan/internal/ct_utils.h>
14
15
namespace Botan {
16
17
/*
18
* Simple O(N^2) Multiplication
19
*/
20
120
void basecase_mul(word z[], size_t z_size, const word x[], size_t x_size, const word y[], size_t y_size) {
21
120
   if(z_size < x_size + y_size) {
22
0
      throw Invalid_Argument("basecase_mul z_size too small");
23
0
   }
24
25
120
   const size_t x_size_8 = x_size - (x_size % 8);
26
27
120
   clear_mem(z, z_size);
28
29
2.02k
   for(size_t i = 0; i != y_size; ++i) {
30
1.90k
      const word y_i = y[i];
31
32
1.90k
      word carry = 0;
33
34
3.75k
      for(size_t j = 0; j != x_size_8; j += 8) {
35
1.85k
         carry = word8_madd3(z + i + j, x + j, y_i, carry);
36
1.85k
      }
37
38
7.68k
      for(size_t j = x_size_8; j != x_size; ++j) {
39
5.78k
         z[i + j] = word_madd3(x[j], y_i, z[i + j], &carry);
40
5.78k
      }
41
42
1.90k
      z[x_size + i] = carry;
43
1.90k
   }
44
120
}
45
46
0
void basecase_sqr(word z[], size_t z_size, const word x[], size_t x_size) {
47
0
   if(z_size < 2 * x_size) {
48
0
      throw Invalid_Argument("basecase_sqr z_size too small");
49
0
   }
50
51
0
   const size_t x_size_8 = x_size - (x_size % 8);
52
53
0
   clear_mem(z, z_size);
54
55
0
   for(size_t i = 0; i != x_size; ++i) {
56
0
      const word x_i = x[i];
57
58
0
      word carry = 0;
59
60
0
      for(size_t j = 0; j != x_size_8; j += 8) {
61
0
         carry = word8_madd3(z + i + j, x + j, x_i, carry);
62
0
      }
63
64
0
      for(size_t j = x_size_8; j != x_size; ++j) {
65
0
         z[i + j] = word_madd3(x[j], x_i, z[i + j], &carry);
66
0
      }
67
68
0
      z[x_size + i] = carry;
69
0
   }
70
0
}
71
72
namespace {
73
74
const size_t KARATSUBA_MULTIPLY_THRESHOLD = 32;
75
const size_t KARATSUBA_SQUARE_THRESHOLD = 32;
76
77
/*
78
* Karatsuba Multiplication Operation
79
*/
80
20
void karatsuba_mul(word z[], const word x[], const word y[], size_t N, word workspace[]) {
81
20
   if(N < KARATSUBA_MULTIPLY_THRESHOLD || N % 2) {
82
15
      switch(N) {
83
0
         case 6:
84
0
            return bigint_comba_mul6(z, x, y);
85
0
         case 8:
86
0
            return bigint_comba_mul8(z, x, y);
87
0
         case 9:
88
0
            return bigint_comba_mul9(z, x, y);
89
6
         case 16:
90
6
            return bigint_comba_mul16(z, x, y);
91
0
         case 24:
92
0
            return bigint_comba_mul24(z, x, y);
93
9
         default:
94
9
            return basecase_mul(z, 2 * N, x, N, y, N);
95
15
      }
96
15
   }
97
98
5
   const size_t N2 = N / 2;
99
100
5
   const word* x0 = x;
101
5
   const word* x1 = x + N2;
102
5
   const word* y0 = y;
103
5
   const word* y1 = y + N2;
104
5
   word* z0 = z;
105
5
   word* z1 = z + N;
106
107
5
   word* ws0 = workspace;
108
5
   word* ws1 = workspace + N;
109
110
5
   clear_mem(workspace, 2 * N);
111
112
   /*
113
   * If either of cmp0 or cmp1 is zero then z0 or z1 resp is zero here,
114
   * resulting in a no-op - z0*z1 will be equal to zero so we don't need to do
115
   * anything, clear_mem above already set the correct result.
116
   *
117
   * However we ignore the result of the comparisons and always perform the
118
   * subtractions and recursively multiply to avoid the timing channel.
119
   */
120
121
   // First compute (X_lo - X_hi)*(Y_hi - Y_lo)
122
5
   const auto cmp0 = bigint_sub_abs(z0, x0, x1, N2, workspace);
123
5
   const auto cmp1 = bigint_sub_abs(z1, y1, y0, N2, workspace);
124
5
   const auto neg_mask = ~(cmp0 ^ cmp1);
125
126
5
   karatsuba_mul(ws0, z0, z1, N2, ws1);
127
128
   // Compute X_lo * Y_lo
129
5
   karatsuba_mul(z0, x0, y0, N2, ws1);
130
131
   // Compute X_hi * Y_hi
132
5
   karatsuba_mul(z1, x1, y1, N2, ws1);
133
134
5
   const word ws_carry = bigint_add3_nc(ws1, z0, N, z1, N);
135
5
   word z_carry = bigint_add2_nc(z + N2, N, ws1, N);
136
137
5
   z_carry += bigint_add2_nc(z + N + N2, N2, &ws_carry, 1);
138
5
   bigint_add2_nc(z + N + N2, N2, &z_carry, 1);
139
140
5
   clear_mem(workspace + N, N2);
141
142
5
   bigint_cnd_add_or_sub(neg_mask, z + N2, workspace, 2 * N - N2);
143
5
}
144
145
/*
146
* Karatsuba Squaring Operation
147
*/
148
0
void karatsuba_sqr(word z[], const word x[], size_t N, word workspace[]) {
149
0
   if(N < KARATSUBA_SQUARE_THRESHOLD || N % 2) {
150
0
      switch(N) {
151
0
         case 6:
152
0
            return bigint_comba_sqr6(z, x);
153
0
         case 8:
154
0
            return bigint_comba_sqr8(z, x);
155
0
         case 9:
156
0
            return bigint_comba_sqr9(z, x);
157
0
         case 16:
158
0
            return bigint_comba_sqr16(z, x);
159
0
         case 24:
160
0
            return bigint_comba_sqr24(z, x);
161
0
         default:
162
0
            return basecase_sqr(z, 2 * N, x, N);
163
0
      }
164
0
   }
165
166
0
   const size_t N2 = N / 2;
167
168
0
   const word* x0 = x;
169
0
   const word* x1 = x + N2;
170
0
   word* z0 = z;
171
0
   word* z1 = z + N;
172
173
0
   word* ws0 = workspace;
174
0
   word* ws1 = workspace + N;
175
176
0
   clear_mem(workspace, 2 * N);
177
178
   // See comment in karatsuba_mul
179
0
   bigint_sub_abs(z0, x0, x1, N2, workspace);
180
0
   karatsuba_sqr(ws0, z0, N2, ws1);
181
182
0
   karatsuba_sqr(z0, x0, N2, ws1);
183
0
   karatsuba_sqr(z1, x1, N2, ws1);
184
185
0
   const word ws_carry = bigint_add3_nc(ws1, z0, N, z1, N);
186
0
   word z_carry = bigint_add2_nc(z + N2, N, ws1, N);
187
188
0
   z_carry += bigint_add2_nc(z + N + N2, N2, &ws_carry, 1);
189
0
   bigint_add2_nc(z + N + N2, N2, &z_carry, 1);
190
191
   /*
192
   * This is only actually required if cmp (result of bigint_sub_abs) is != 0,
193
   * however if cmp==0 then ws0[0:N] == 0 and avoiding the jump hides a
194
   * timing channel.
195
   */
196
0
   bigint_sub2(z + N2, 2 * N - N2, ws0, N);
197
0
}
198
199
/*
200
* Pick a good size for the Karatsuba multiply
201
*/
202
5
size_t karatsuba_size(size_t z_size, size_t x_size, size_t x_sw, size_t y_size, size_t y_sw) {
203
5
   if(x_sw > x_size || x_sw > y_size || y_sw > x_size || y_sw > y_size) {
204
0
      return 0;
205
0
   }
206
207
5
   if(((x_size == x_sw) && (x_size % 2)) || ((y_size == y_sw) && (y_size % 2))) {
208
0
      return 0;
209
0
   }
210
211
5
   const size_t start = (x_sw > y_sw) ? x_sw : y_sw;
212
5
   const size_t end = (x_size < y_size) ? x_size : y_size;
213
214
5
   if(start == end) {
215
0
      if(start % 2) {
216
0
         return 0;
217
0
      }
218
0
      return start;
219
0
   }
220
221
8
   for(size_t j = start; j <= end; ++j) {
222
8
      if(j % 2) {
223
3
         continue;
224
3
      }
225
226
5
      if(2 * j > z_size) {
227
0
         return 0;
228
0
      }
229
230
5
      if(x_sw <= j && j <= x_size && y_sw <= j && j <= y_size) {
231
5
         if(j % 4 == 2 && (j + 2) <= x_size && (j + 2) <= y_size && 2 * (j + 2) <= z_size) {
232
3
            return j + 2;
233
3
         }
234
2
         return j;
235
5
      }
236
5
   }
237
238
0
   return 0;
239
5
}
240
241
/*
242
* Pick a good size for the Karatsuba squaring
243
*/
244
0
size_t karatsuba_size(size_t z_size, size_t x_size, size_t x_sw) {
245
0
   if(x_sw == x_size) {
246
0
      if(x_sw % 2) {
247
0
         return 0;
248
0
      }
249
0
      return x_sw;
250
0
   }
251
252
0
   for(size_t j = x_sw; j <= x_size; ++j) {
253
0
      if(j % 2) {
254
0
         continue;
255
0
      }
256
257
0
      if(2 * j > z_size) {
258
0
         return 0;
259
0
      }
260
261
0
      if(j % 4 == 2 && (j + 2) <= x_size && 2 * (j + 2) <= z_size) {
262
0
         return j + 2;
263
0
      }
264
0
      return j;
265
0
   }
266
267
0
   return 0;
268
0
}
269
270
template <size_t SZ>
271
1.12k
inline bool sized_for_comba_mul(size_t x_sw, size_t x_size, size_t y_sw, size_t y_size, size_t z_size) {
272
1.12k
   return (x_sw <= SZ && x_size >= SZ && y_sw <= SZ && y_size >= SZ && z_size >= 2 * SZ);
273
1.12k
}
mp_karat.cpp:bool Botan::(anonymous namespace)::sized_for_comba_mul<4ul>(unsigned long, unsigned long, unsigned long, unsigned long, unsigned long)
Line
Count
Source
271
230
inline bool sized_for_comba_mul(size_t x_sw, size_t x_size, size_t y_sw, size_t y_size, size_t z_size) {
272
230
   return (x_sw <= SZ && x_size >= SZ && y_sw <= SZ && y_size >= SZ && z_size >= 2 * SZ);
273
230
}
mp_karat.cpp:bool Botan::(anonymous namespace)::sized_for_comba_mul<6ul>(unsigned long, unsigned long, unsigned long, unsigned long, unsigned long)
Line
Count
Source
271
212
inline bool sized_for_comba_mul(size_t x_sw, size_t x_size, size_t y_sw, size_t y_size, size_t z_size) {
272
212
   return (x_sw <= SZ && x_size >= SZ && y_sw <= SZ && y_size >= SZ && z_size >= 2 * SZ);
273
212
}
mp_karat.cpp:bool Botan::(anonymous namespace)::sized_for_comba_mul<8ul>(unsigned long, unsigned long, unsigned long, unsigned long, unsigned long)
Line
Count
Source
271
195
inline bool sized_for_comba_mul(size_t x_sw, size_t x_size, size_t y_sw, size_t y_size, size_t z_size) {
272
195
   return (x_sw <= SZ && x_size >= SZ && y_sw <= SZ && y_size >= SZ && z_size >= 2 * SZ);
273
195
}
mp_karat.cpp:bool Botan::(anonymous namespace)::sized_for_comba_mul<9ul>(unsigned long, unsigned long, unsigned long, unsigned long, unsigned long)
Line
Count
Source
271
177
inline bool sized_for_comba_mul(size_t x_sw, size_t x_size, size_t y_sw, size_t y_size, size_t z_size) {
272
177
   return (x_sw <= SZ && x_size >= SZ && y_sw <= SZ && y_size >= SZ && z_size >= 2 * SZ);
273
177
}
mp_karat.cpp:bool Botan::(anonymous namespace)::sized_for_comba_mul<16ul>(unsigned long, unsigned long, unsigned long, unsigned long, unsigned long)
Line
Count
Source
271
166
inline bool sized_for_comba_mul(size_t x_sw, size_t x_size, size_t y_sw, size_t y_size, size_t z_size) {
272
166
   return (x_sw <= SZ && x_size >= SZ && y_sw <= SZ && y_size >= SZ && z_size >= 2 * SZ);
273
166
}
mp_karat.cpp:bool Botan::(anonymous namespace)::sized_for_comba_mul<24ul>(unsigned long, unsigned long, unsigned long, unsigned long, unsigned long)
Line
Count
Source
271
142
inline bool sized_for_comba_mul(size_t x_sw, size_t x_size, size_t y_sw, size_t y_size, size_t z_size) {
272
142
   return (x_sw <= SZ && x_size >= SZ && y_sw <= SZ && y_size >= SZ && z_size >= 2 * SZ);
273
142
}
274
275
template <size_t SZ>
276
0
inline bool sized_for_comba_sqr(size_t x_sw, size_t x_size, size_t z_size) {
277
0
   return (x_sw <= SZ && x_size >= SZ && z_size >= 2 * SZ);
278
0
}
Unexecuted instantiation: mp_karat.cpp:bool Botan::(anonymous namespace)::sized_for_comba_sqr<4ul>(unsigned long, unsigned long, unsigned long)
Unexecuted instantiation: mp_karat.cpp:bool Botan::(anonymous namespace)::sized_for_comba_sqr<6ul>(unsigned long, unsigned long, unsigned long)
Unexecuted instantiation: mp_karat.cpp:bool Botan::(anonymous namespace)::sized_for_comba_sqr<8ul>(unsigned long, unsigned long, unsigned long)
Unexecuted instantiation: mp_karat.cpp:bool Botan::(anonymous namespace)::sized_for_comba_sqr<9ul>(unsigned long, unsigned long, unsigned long)
Unexecuted instantiation: mp_karat.cpp:bool Botan::(anonymous namespace)::sized_for_comba_sqr<16ul>(unsigned long, unsigned long, unsigned long)
Unexecuted instantiation: mp_karat.cpp:bool Botan::(anonymous namespace)::sized_for_comba_sqr<24ul>(unsigned long, unsigned long, unsigned long)
279
280
}  // namespace
281
282
void bigint_mul(word z[],
283
                size_t z_size,
284
                const word x[],
285
                size_t x_size,
286
                size_t x_sw,
287
                const word y[],
288
                size_t y_size,
289
                size_t y_sw,
290
                word workspace[],
291
230
                size_t ws_size) {
292
230
   clear_mem(z, z_size);
293
294
230
   if(x_sw == 1) {
295
0
      bigint_linmul3(z, y, y_sw, x[0]);
296
230
   } else if(y_sw == 1) {
297
0
      bigint_linmul3(z, x, x_sw, y[0]);
298
230
   } else if(sized_for_comba_mul<4>(x_sw, x_size, y_sw, y_size, z_size)) {
299
18
      bigint_comba_mul4(z, x, y);
300
212
   } else if(sized_for_comba_mul<6>(x_sw, x_size, y_sw, y_size, z_size)) {
301
17
      bigint_comba_mul6(z, x, y);
302
195
   } else if(sized_for_comba_mul<8>(x_sw, x_size, y_sw, y_size, z_size)) {
303
18
      bigint_comba_mul8(z, x, y);
304
177
   } else if(sized_for_comba_mul<9>(x_sw, x_size, y_sw, y_size, z_size)) {
305
11
      bigint_comba_mul9(z, x, y);
306
166
   } else if(sized_for_comba_mul<16>(x_sw, x_size, y_sw, y_size, z_size)) {
307
24
      bigint_comba_mul16(z, x, y);
308
142
   } else if(sized_for_comba_mul<24>(x_sw, x_size, y_sw, y_size, z_size)) {
309
26
      bigint_comba_mul24(z, x, y);
310
116
   } else if(x_sw < KARATSUBA_MULTIPLY_THRESHOLD || y_sw < KARATSUBA_MULTIPLY_THRESHOLD || !workspace) {
311
111
      basecase_mul(z, z_size, x, x_sw, y, y_sw);
312
111
   } else {
313
5
      const size_t N = karatsuba_size(z_size, x_size, x_sw, y_size, y_sw);
314
315
5
      if(N && z_size >= 2 * N && ws_size >= 2 * N) {
316
5
         karatsuba_mul(z, x, y, N, workspace);
317
5
      } else {
318
0
         basecase_mul(z, z_size, x, x_sw, y, y_sw);
319
0
      }
320
5
   }
321
230
}
322
323
/*
324
* Squaring Algorithm Dispatcher
325
*/
326
0
void bigint_sqr(word z[], size_t z_size, const word x[], size_t x_size, size_t x_sw, word workspace[], size_t ws_size) {
327
0
   clear_mem(z, z_size);
328
329
0
   BOTAN_ASSERT(z_size / 2 >= x_sw, "Output size is sufficient");
330
331
0
   if(x_sw == 1) {
332
0
      bigint_linmul3(z, x, x_sw, x[0]);
333
0
   } else if(sized_for_comba_sqr<4>(x_sw, x_size, z_size)) {
334
0
      bigint_comba_sqr4(z, x);
335
0
   } else if(sized_for_comba_sqr<6>(x_sw, x_size, z_size)) {
336
0
      bigint_comba_sqr6(z, x);
337
0
   } else if(sized_for_comba_sqr<8>(x_sw, x_size, z_size)) {
338
0
      bigint_comba_sqr8(z, x);
339
0
   } else if(sized_for_comba_sqr<9>(x_sw, x_size, z_size)) {
340
0
      bigint_comba_sqr9(z, x);
341
0
   } else if(sized_for_comba_sqr<16>(x_sw, x_size, z_size)) {
342
0
      bigint_comba_sqr16(z, x);
343
0
   } else if(sized_for_comba_sqr<24>(x_sw, x_size, z_size)) {
344
0
      bigint_comba_sqr24(z, x);
345
0
   } else if(x_size < KARATSUBA_SQUARE_THRESHOLD || !workspace) {
346
0
      basecase_sqr(z, z_size, x, x_sw);
347
0
   } else {
348
0
      const size_t N = karatsuba_size(z_size, x_size, x_sw);
349
350
0
      if(N && z_size >= 2 * N && ws_size >= 2 * N) {
351
0
         karatsuba_sqr(z, x, N, workspace);
352
0
      } else {
353
0
         basecase_sqr(z, z_size, x, x_sw);
354
0
      }
355
0
   }
356
0
}
357
358
}  // namespace Botan