• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 5123321399

30 May 2023 04:06PM UTC coverage: 92.213% (+0.004%) from 92.209%
5123321399

Pull #3558

github

web-flow
Merge dd72f7389 into 057bcbc35
Pull Request #3558: Add braces around all if/else statements

75602 of 81986 relevant lines covered (92.21%)

11859779.3 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.57
/src/lib/math/mp/mp_karat.cpp
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
void basecase_mul(word z[], size_t z_size, const word x[], size_t x_size, const word y[], size_t y_size) {
6,010,206✔
21
   if(z_size < x_size + y_size) {
6,010,206✔
22
      throw Invalid_Argument("basecase_mul z_size too small");
×
23
   }
24

25
   const size_t x_size_8 = x_size - (x_size % 8);
6,010,206✔
26

27
   clear_mem(z, z_size);
6,010,206✔
28

29
   for(size_t i = 0; i != y_size; ++i) {
60,365,756✔
30
      const word y_i = y[i];
54,355,550✔
31

32
      word carry = 0;
54,355,550✔
33

34
      for(size_t j = 0; j != x_size_8; j += 8) {
124,616,935✔
35
         carry = word8_madd3(z + i + j, x + j, y_i, carry);
70,261,385✔
36
      }
37

38
      for(size_t j = x_size_8; j != x_size; ++j) {
180,723,791✔
39
         z[i + j] = word_madd3(x[j], y_i, z[i + j], &carry);
126,368,241✔
40
      }
41

42
      z[x_size + i] = carry;
54,355,550✔
43
   }
44
}
6,010,206✔
45

46
void basecase_sqr(word z[], size_t z_size, const word x[], size_t x_size) {
5,451,297✔
47
   if(z_size < 2 * x_size) {
5,451,297✔
48
      throw Invalid_Argument("basecase_sqr z_size too small");
×
49
   }
50

51
   const size_t x_size_8 = x_size - (x_size % 8);
5,451,297✔
52

53
   clear_mem(z, z_size);
5,451,297✔
54

55
   for(size_t i = 0; i != x_size; ++i) {
46,272,986✔
56
      const word x_i = x[i];
40,821,689✔
57

58
      word carry = 0;
40,821,689✔
59

60
      for(size_t j = 0; j != x_size_8; j += 8) {
84,354,648✔
61
         carry = word8_madd3(z + i + j, x + j, x_i, carry);
43,532,959✔
62
      }
63

64
      for(size_t j = x_size_8; j != x_size; ++j) {
161,005,596✔
65
         z[i + j] = word_madd3(x[j], x_i, z[i + j], &carry);
120,183,907✔
66
      }
67

68
      z[x_size + i] = carry;
40,821,689✔
69
   }
70
}
5,451,297✔
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
void karatsuba_mul(word z[], const word x[], const word y[], size_t N, word workspace[]) {
12,909,469✔
81
   if(N < KARATSUBA_MULTIPLY_THRESHOLD || N % 2) {
12,909,469✔
82
      switch(N) {
9,394,260✔
83
         case 6:
×
84
            return bigint_comba_mul6(z, x, y);
9,394,260✔
85
         case 8:
×
86
            return bigint_comba_mul8(z, x, y);
×
87
         case 9:
×
88
            return bigint_comba_mul9(z, x, y);
×
89
         case 16:
7,717,239✔
90
            return bigint_comba_mul16(z, x, y);
7,717,239✔
91
         case 24:
1,671,717✔
92
            return bigint_comba_mul24(z, x, y);
1,671,717✔
93
         default:
5,304✔
94
            return basecase_mul(z, 2 * N, x, N, y, N);
5,304✔
95
      }
96
   }
97

98
   const size_t N2 = N / 2;
3,515,209✔
99

100
   const word* x0 = x;
3,515,209✔
101
   const word* x1 = x + N2;
3,515,209✔
102
   const word* y0 = y;
3,515,209✔
103
   const word* y1 = y + N2;
3,515,209✔
104
   word* z0 = z;
3,515,209✔
105
   word* z1 = z + N;
3,515,209✔
106

107
   word* ws0 = workspace;
3,515,209✔
108
   word* ws1 = workspace + N;
3,515,209✔
109

110
   clear_mem(workspace, 2 * N);
3,515,209✔
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
   const auto cmp0 = bigint_sub_abs(z0, x0, x1, N2, workspace);
3,515,209✔
123
   const auto cmp1 = bigint_sub_abs(z1, y1, y0, N2, workspace);
3,515,209✔
124
   const auto neg_mask = ~(cmp0 ^ cmp1);
3,515,209✔
125

126
   karatsuba_mul(ws0, z0, z1, N2, ws1);
3,515,209✔
127

128
   // Compute X_lo * Y_lo
129
   karatsuba_mul(z0, x0, y0, N2, ws1);
3,515,209✔
130

131
   // Compute X_hi * Y_hi
132
   karatsuba_mul(z1, x1, y1, N2, ws1);
3,515,209✔
133

134
   const word ws_carry = bigint_add3_nc(ws1, z0, N, z1, N);
3,515,209✔
135
   word z_carry = bigint_add2_nc(z + N2, N, ws1, N);
3,515,209✔
136

137
   z_carry += bigint_add2_nc(z + N + N2, N2, &ws_carry, 1);
3,515,209✔
138
   bigint_add2_nc(z + N + N2, N2, &z_carry, 1);
3,515,209✔
139

140
   clear_mem(workspace + N, N2);
3,515,209✔
141

142
   bigint_cnd_add_or_sub(neg_mask, z + N2, workspace, 2 * N - N2);
3,515,209✔
143
}
144

145
/*
146
* Karatsuba Squaring Operation
147
*/
148
void karatsuba_sqr(word z[], const word x[], size_t N, word workspace[]) {
42,473,347✔
149
   if(N < KARATSUBA_SQUARE_THRESHOLD || N % 2) {
42,473,347✔
150
      switch(N) {
30,946,879✔
151
         case 6:
×
152
            return bigint_comba_sqr6(z, x);
30,946,879✔
153
         case 8:
×
154
            return bigint_comba_sqr8(z, x);
×
155
         case 9:
×
156
            return bigint_comba_sqr9(z, x);
×
157
         case 16:
25,104,216✔
158
            return bigint_comba_sqr16(z, x);
25,104,216✔
159
         case 24:
4,957,425✔
160
            return bigint_comba_sqr24(z, x);
4,957,425✔
161
         default:
885,238✔
162
            return basecase_sqr(z, 2 * N, x, N);
885,238✔
163
      }
164
   }
165

166
   const size_t N2 = N / 2;
11,526,468✔
167

168
   const word* x0 = x;
11,526,468✔
169
   const word* x1 = x + N2;
11,526,468✔
170
   word* z0 = z;
11,526,468✔
171
   word* z1 = z + N;
11,526,468✔
172

173
   word* ws0 = workspace;
11,526,468✔
174
   word* ws1 = workspace + N;
11,526,468✔
175

176
   clear_mem(workspace, 2 * N);
11,526,468✔
177

178
   // See comment in karatsuba_mul
179
   bigint_sub_abs(z0, x0, x1, N2, workspace);
11,526,468✔
180
   karatsuba_sqr(ws0, z0, N2, ws1);
11,526,468✔
181

182
   karatsuba_sqr(z0, x0, N2, ws1);
11,526,468✔
183
   karatsuba_sqr(z1, x1, N2, ws1);
11,526,468✔
184

185
   const word ws_carry = bigint_add3_nc(ws1, z0, N, z1, N);
11,526,468✔
186
   word z_carry = bigint_add2_nc(z + N2, N, ws1, N);
11,526,468✔
187

188
   z_carry += bigint_add2_nc(z + N + N2, N2, &ws_carry, 1);
11,526,468✔
189
   bigint_add2_nc(z + N + N2, N2, &z_carry, 1);
11,526,468✔
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
   bigint_sub2(z + N2, 2 * N - N2, ws0, N);
11,526,468✔
197
}
198

199
/*
200
* Pick a good size for the Karatsuba multiply
201
*/
202
size_t karatsuba_size(size_t z_size, size_t x_size, size_t x_sw, size_t y_size, size_t y_sw) {
2,497,174✔
203
   if(x_sw > x_size || x_sw > y_size || y_sw > x_size || y_sw > y_size) {
2,497,174✔
204
      return 0;
205
   }
206

207
   if(((x_size == x_sw) && (x_size % 2)) || ((y_size == y_sw) && (y_size % 2))) {
2,497,117✔
208
      return 0;
209
   }
210

211
   const size_t start = (x_sw > y_sw) ? x_sw : y_sw;
2,496,519✔
212
   const size_t end = (x_size < y_size) ? x_size : y_size;
2,496,519✔
213

214
   if(start == end) {
2,496,519✔
215
      if(start % 2) {
1,456,240✔
216
         return 0;
217
      }
218
      return start;
1,456,240✔
219
   }
220

221
   for(size_t j = start; j <= end; ++j) {
1,173,195✔
222
      if(j % 2) {
1,173,195✔
223
         continue;
132,916✔
224
      }
225

226
      if(2 * j > z_size) {
1,040,279✔
227
         return 0;
228
      }
229

230
      if(x_sw <= j && j <= x_size && y_sw <= j && j <= y_size) {
907,753✔
231
         if(j % 4 == 2 && (j + 2) <= x_size && (j + 2) <= y_size && 2 * (j + 2) <= z_size) {
907,753✔
232
            return j + 2;
233
         }
234
         return j;
907,353✔
235
      }
236
   }
237

238
   return 0;
239
}
240

241
/*
242
* Pick a good size for the Karatsuba squaring
243
*/
244
size_t karatsuba_size(size_t z_size, size_t x_size, size_t x_sw) {
8,768,127✔
245
   if(x_sw == x_size) {
8,768,127✔
246
      if(x_sw % 2) {
5,442✔
247
         return 0;
248
      }
249
      return x_sw;
5,441✔
250
   }
251

252
   for(size_t j = x_sw; j <= x_size; ++j) {
9,636,868✔
253
      if(j % 2) {
9,636,868✔
254
         continue;
874,183✔
255
      }
256

257
      if(2 * j > z_size) {
8,762,685✔
258
         return 0;
259
      }
260

261
      if(j % 4 == 2 && (j + 2) <= x_size && 2 * (j + 2) <= z_size) {
7,888,502✔
262
         return j + 2;
×
263
      }
264
      return j;
265
   }
266

267
   return 0;
268
}
269

270
template <size_t SZ>
271
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) {
211,542,673✔
272
   return (x_sw <= SZ && x_size >= SZ && y_sw <= SZ && y_size >= SZ && z_size >= 2 * SZ);
88,132,016✔
273
}
274

275
template <size_t SZ>
276
inline bool sized_for_comba_sqr(size_t x_sw, size_t x_size, size_t z_size) {
297,068,155✔
277
   return (x_sw <= SZ && x_size >= SZ && z_size >= 2 * SZ);
89,032,224✔
278
}
279

280
}  // namespace
281

282
void bigint_mul(word z[],
85,074,138✔
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
                size_t ws_size) {
292
   clear_mem(z, z_size);
85,074,138✔
293

294
   if(x_sw == 1) {
85,074,138✔
295
      bigint_linmul3(z, y, y_sw, x[0]);
2,378,329✔
296
   } else if(y_sw == 1) {
82,695,809✔
297
      bigint_linmul3(z, x, x_sw, y[0]);
5✔
298
   } else if(sized_for_comba_mul<4>(x_sw, x_size, y_sw, y_size, z_size)) {
82,695,804✔
299
      bigint_comba_mul4(z, x, y);
33,723,689✔
300
   } else if(sized_for_comba_mul<6>(x_sw, x_size, y_sw, y_size, z_size)) {
48,972,115✔
301
      bigint_comba_mul6(z, x, y);
19,312,385✔
302
   } else if(sized_for_comba_mul<8>(x_sw, x_size, y_sw, y_size, z_size)) {
29,659,730✔
303
      bigint_comba_mul8(z, x, y);
5,276,249✔
304
   } else if(sized_for_comba_mul<9>(x_sw, x_size, y_sw, y_size, z_size)) {
24,383,481✔
305
      bigint_comba_mul9(z, x, y);
9,864,822✔
306
   } else if(sized_for_comba_mul<16>(x_sw, x_size, y_sw, y_size, z_size)) {
14,518,659✔
307
      bigint_comba_mul16(z, x, y);
3,205,775✔
308
   } else if(sized_for_comba_mul<24>(x_sw, x_size, y_sw, y_size, z_size)) {
11,312,884✔
309
      bigint_comba_mul24(z, x, y);
2,944,396✔
310
   } else if(x_sw < KARATSUBA_MULTIPLY_THRESHOLD || y_sw < KARATSUBA_MULTIPLY_THRESHOLD || !workspace) {
8,368,488✔
311
      basecase_mul(z, z_size, x, x_sw, y, y_sw);
5,871,314✔
312
   } else {
313
      const size_t N = karatsuba_size(z_size, x_size, x_sw, y_size, y_sw);
2,497,174✔
314

315
      if(N && z_size >= 2 * N && ws_size >= 2 * N) {
2,497,174✔
316
         karatsuba_mul(z, x, y, N, workspace);
2,363,842✔
317
      } else {
318
         basecase_mul(z, z_size, x, x_sw, y, y_sw);
133,332✔
319
      }
320
   }
321
}
85,074,138✔
322

323
/*
324
* Squaring Algorithm Dispatcher
325
*/
326
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) {
91,530,463✔
327
   clear_mem(z, z_size);
91,530,463✔
328

329
   BOTAN_ASSERT(z_size / 2 >= x_sw, "Output size is sufficient");
91,530,463✔
330

331
   if(x_sw == 1) {
91,530,463✔
332
      bigint_linmul3(z, x, x_sw, x[0]);
3,222,124✔
333
   } else if(sized_for_comba_sqr<4>(x_sw, x_size, z_size)) {
88,308,339✔
334
      bigint_comba_sqr4(z, x);
23,242,857✔
335
   } else if(sized_for_comba_sqr<6>(x_sw, x_size, z_size)) {
65,065,482✔
336
      bigint_comba_sqr6(z, x);
16,728,486✔
337
   } else if(sized_for_comba_sqr<8>(x_sw, x_size, z_size)) {
48,336,996✔
338
      bigint_comba_sqr8(z, x);
7,640,574✔
339
   } else if(sized_for_comba_sqr<9>(x_sw, x_size, z_size)) {
40,696,422✔
340
      bigint_comba_sqr9(z, x);
8,151,081✔
341
   } else if(sized_for_comba_sqr<16>(x_sw, x_size, z_size)) {
32,545,341✔
342
      bigint_comba_sqr16(z, x);
10,429,766✔
343
   } else if(sized_for_comba_sqr<24>(x_sw, x_size, z_size)) {
22,115,575✔
344
      bigint_comba_sqr24(z, x);
9,655,765✔
345
   } else if(x_size < KARATSUBA_SQUARE_THRESHOLD || !workspace) {
12,459,810✔
346
      basecase_sqr(z, z_size, x, x_sw);
3,691,683✔
347
   } else {
348
      const size_t N = karatsuba_size(z_size, x_size, x_sw);
8,768,127✔
349

350
      if(N && z_size >= 2 * N && ws_size >= 2 * N) {
8,768,127✔
351
         karatsuba_sqr(z, x, N, workspace);
7,893,943✔
352
      } else {
353
         basecase_sqr(z, z_size, x, x_sw);
874,184✔
354
      }
355
   }
356
}
91,530,463✔
357

358
}  // namespace Botan
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc