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