2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 #include "internal/cryptlib.h"
14 #if defined(OPENSSL_NO_ASM) || !defined(OPENSSL_BN_ASM_PART_WORDS)
16 * Here follows specialised variants of bn_add_words() and bn_sub_words().
17 * They have the property performing operations on arrays of different sizes.
18 * The sizes of those arrays is expressed through cl, which is the common
19 * length ( basically, min(len(a),len(b)) ), and dl, which is the delta
20 * between the two lengths, calculated as len(a)-len(b). All lengths are the
21 * number of BN_ULONGs... For the operations that require a result array as
22 * parameter, it must have the length cl+abs(dl). These functions should
23 * probably end up in bn_asm.c as soon as there are assembler counterparts
24 * for the systems that use assembler files.
27 BN_ULONG bn_sub_part_words(BN_ULONG *r,
28 const BN_ULONG *a, const BN_ULONG *b,
34 c = bn_sub_words(r, a, b, cl);
46 r[0] = (0 - t - c) & BN_MASK2;
53 r[1] = (0 - t - c) & BN_MASK2;
60 r[2] = (0 - t - c) & BN_MASK2;
67 r[3] = (0 - t - c) & BN_MASK2;
80 r[0] = (t - c) & BN_MASK2;
87 r[1] = (t - c) & BN_MASK2;
94 r[2] = (t - c) & BN_MASK2;
101 r[3] = (t - c) & BN_MASK2;
113 switch (save_dl - dl) {
159 * Karatsuba recursive multiplication algorithm (cf. Knuth, The Art of
160 * Computer Programming, Vol. 2)
164 * r is 2*n2 words in size,
165 * a and b are both n2 words in size.
166 * n2 must be a power of 2.
167 * We multiply and return the result.
168 * t must be 2*n2 words in size
171 * a[0]*b[0]+a[1]*b[1]+(a[0]-a[1])*(b[1]-b[0])
174 /* dnX may not be positive, but n2/2+dnX has to be */
175 void bn_mul_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2,
176 int dna, int dnb, BN_ULONG *t)
178 int n = n2 / 2, c1, c2;
179 int tna = n + dna, tnb = n + dnb;
180 unsigned int neg, zero;
186 bn_mul_comba4(r, a, b);
191 * Only call bn_mul_comba 8 if n2 == 8 and the two arrays are complete
194 if (n2 == 8 && dna == 0 && dnb == 0) {
195 bn_mul_comba8(r, a, b);
198 # endif /* BN_MUL_COMBA */
199 /* Else do normal multiply */
200 if (n2 < BN_MUL_RECURSIVE_SIZE_NORMAL) {
201 bn_mul_normal(r, a, n2 + dna, b, n2 + dnb);
203 memset(&r[2 * n2 + dna + dnb], 0,
204 sizeof(BN_ULONG) * -(dna + dnb));
207 /* r=(a[0]-a[1])*(b[1]-b[0]) */
208 c1 = bn_cmp_part_words(a, &(a[n]), tna, n - tna);
209 c2 = bn_cmp_part_words(&(b[n]), b, tnb, tnb - n);
211 switch (c1 * 3 + c2) {
213 bn_sub_part_words(t, &(a[n]), a, tna, tna - n); /* - */
214 bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); /* - */
220 bn_sub_part_words(t, &(a[n]), a, tna, tna - n); /* - */
221 bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n); /* + */
230 bn_sub_part_words(t, a, &(a[n]), tna, n - tna); /* + */
231 bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); /* - */
238 bn_sub_part_words(t, a, &(a[n]), tna, n - tna);
239 bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n);
244 if (n == 4 && dna == 0 && dnb == 0) { /* XXX: bn_mul_comba4 could take
245 * extra args to do this well */
247 bn_mul_comba4(&(t[n2]), t, &(t[n]));
249 memset(&t[n2], 0, sizeof(*t) * 8);
251 bn_mul_comba4(r, a, b);
252 bn_mul_comba4(&(r[n2]), &(a[n]), &(b[n]));
253 } else if (n == 8 && dna == 0 && dnb == 0) { /* XXX: bn_mul_comba8 could
254 * take extra args to do
257 bn_mul_comba8(&(t[n2]), t, &(t[n]));
259 memset(&t[n2], 0, sizeof(*t) * 16);
261 bn_mul_comba8(r, a, b);
262 bn_mul_comba8(&(r[n2]), &(a[n]), &(b[n]));
264 # endif /* BN_MUL_COMBA */
268 bn_mul_recursive(&(t[n2]), t, &(t[n]), n, 0, 0, p);
270 memset(&t[n2], 0, sizeof(*t) * n2);
271 bn_mul_recursive(r, a, b, n, 0, 0, p);
272 bn_mul_recursive(&(r[n2]), &(a[n]), &(b[n]), n, dna, dnb, p);
276 * t[32] holds (a[0]-a[1])*(b[1]-b[0]), c1 is the sign
277 * r[10] holds (a[0]*b[0])
278 * r[32] holds (b[1]*b[1])
281 c1 = (int)(bn_add_words(t, r, &(r[n2]), n2));
283 if (neg) { /* if t[32] is negative */
284 c1 -= (int)(bn_sub_words(&(t[n2]), t, &(t[n2]), n2));
286 /* Might have a carry */
287 c1 += (int)(bn_add_words(&(t[n2]), &(t[n2]), t, n2));
291 * t[32] holds (a[0]-a[1])*(b[1]-b[0])+(a[0]*b[0])+(a[1]*b[1])
292 * r[10] holds (a[0]*b[0])
293 * r[32] holds (b[1]*b[1])
294 * c1 holds the carry bits
296 c1 += (int)(bn_add_words(&(r[n]), &(r[n]), &(t[n2]), n2));
300 ln = (lo + c1) & BN_MASK2;
304 * The overflow will stop before we over write words we should not
307 if (ln < (BN_ULONG)c1) {
311 ln = (lo + 1) & BN_MASK2;
319 * n+tn is the word length t needs to be n*4 is size, as does r
321 /* tnX may not be negative but less than n */
322 void bn_mul_part_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n,
323 int tna, int tnb, BN_ULONG *t)
325 int i, j, n2 = n * 2;
330 bn_mul_normal(r, a, n + tna, b, n + tnb);
334 /* r=(a[0]-a[1])*(b[1]-b[0]) */
335 c1 = bn_cmp_part_words(a, &(a[n]), tna, n - tna);
336 c2 = bn_cmp_part_words(&(b[n]), b, tnb, tnb - n);
338 switch (c1 * 3 + c2) {
340 bn_sub_part_words(t, &(a[n]), a, tna, tna - n); /* - */
341 bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); /* - */
345 bn_sub_part_words(t, &(a[n]), a, tna, tna - n); /* - */
346 bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n); /* + */
353 bn_sub_part_words(t, a, &(a[n]), tna, n - tna); /* + */
354 bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); /* - */
359 bn_sub_part_words(t, a, &(a[n]), tna, n - tna);
360 bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n);
364 * The zero case isn't yet implemented here. The speedup would probably
369 bn_mul_comba4(&(t[n2]), t, &(t[n]));
370 bn_mul_comba4(r, a, b);
371 bn_mul_normal(&(r[n2]), &(a[n]), tn, &(b[n]), tn);
372 memset(&r[n2 + tn * 2], 0, sizeof(*r) * (n2 - tn * 2));
376 bn_mul_comba8(&(t[n2]), t, &(t[n]));
377 bn_mul_comba8(r, a, b);
378 bn_mul_normal(&(r[n2]), &(a[n]), tna, &(b[n]), tnb);
379 memset(&r[n2 + tna + tnb], 0, sizeof(*r) * (n2 - tna - tnb));
382 bn_mul_recursive(&(t[n2]), t, &(t[n]), n, 0, 0, p);
383 bn_mul_recursive(r, a, b, n, 0, 0, p);
386 * If there is only a bottom half to the number, just do it
393 bn_mul_recursive(&(r[n2]), &(a[n]), &(b[n]),
394 i, tna - i, tnb - i, p);
395 memset(&r[n2 + i * 2], 0, sizeof(*r) * (n2 - i * 2));
396 } else if (j > 0) { /* eg, n == 16, i == 8 and tn == 11 */
397 bn_mul_part_recursive(&(r[n2]), &(a[n]), &(b[n]),
398 i, tna - i, tnb - i, p);
399 memset(&(r[n2 + tna + tnb]), 0,
400 sizeof(BN_ULONG) * (n2 - tna - tnb));
401 } else { /* (j < 0) eg, n == 16, i == 8 and tn == 5 */
403 memset(&r[n2], 0, sizeof(*r) * n2);
404 if (tna < BN_MUL_RECURSIVE_SIZE_NORMAL
405 && tnb < BN_MUL_RECURSIVE_SIZE_NORMAL) {
406 bn_mul_normal(&(r[n2]), &(a[n]), tna, &(b[n]), tnb);
411 * these simplified conditions work exclusively because
412 * difference between tna and tnb is 1 or 0
414 if (i < tna || i < tnb) {
415 bn_mul_part_recursive(&(r[n2]),
417 i, tna - i, tnb - i, p);
419 } else if (i == tna || i == tnb) {
420 bn_mul_recursive(&(r[n2]),
422 i, tna - i, tnb - i, p);
431 * t[32] holds (a[0]-a[1])*(b[1]-b[0]), c1 is the sign
432 * r[10] holds (a[0]*b[0])
433 * r[32] holds (b[1]*b[1])
436 c1 = (int)(bn_add_words(t, r, &(r[n2]), n2));
438 if (neg) { /* if t[32] is negative */
439 c1 -= (int)(bn_sub_words(&(t[n2]), t, &(t[n2]), n2));
441 /* Might have a carry */
442 c1 += (int)(bn_add_words(&(t[n2]), &(t[n2]), t, n2));
446 * t[32] holds (a[0]-a[1])*(b[1]-b[0])+(a[0]*b[0])+(a[1]*b[1])
447 * r[10] holds (a[0]*b[0])
448 * r[32] holds (b[1]*b[1])
449 * c1 holds the carry bits
451 c1 += (int)(bn_add_words(&(r[n]), &(r[n]), &(t[n2]), n2));
455 ln = (lo + c1) & BN_MASK2;
459 * The overflow will stop before we over write words we should not
462 if (ln < (BN_ULONG)c1) {
466 ln = (lo + 1) & BN_MASK2;
474 * a and b must be the same size, which is n2.
475 * r needs to be n2 words and t needs to be n2*2
477 void bn_mul_low_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2,
482 bn_mul_recursive(r, a, b, n, 0, 0, &(t[0]));
483 if (n >= BN_MUL_LOW_RECURSIVE_SIZE_NORMAL) {
484 bn_mul_low_recursive(&(t[0]), &(a[0]), &(b[n]), n, &(t[n2]));
485 bn_add_words(&(r[n]), &(r[n]), &(t[0]), n);
486 bn_mul_low_recursive(&(t[0]), &(a[n]), &(b[0]), n, &(t[n2]));
487 bn_add_words(&(r[n]), &(r[n]), &(t[0]), n);
489 bn_mul_low_normal(&(t[0]), &(a[0]), &(b[n]), n);
490 bn_mul_low_normal(&(t[n]), &(a[n]), &(b[0]), n);
491 bn_add_words(&(r[n]), &(r[n]), &(t[0]), n);
492 bn_add_words(&(r[n]), &(r[n]), &(t[n]), n);
495 #endif /* BN_RECURSION */
497 int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
499 int ret = bn_mul_fixed_top(r, a, b, ctx);
507 int bn_mul_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
512 #if defined(BN_MUL_COMBA) || defined(BN_RECURSION)
527 if ((al == 0) || (bl == 0)) {
534 if ((r == a) || (r == b)) {
535 if ((rr = BN_CTX_get(ctx)) == NULL)
540 #if defined(BN_MUL_COMBA) || defined(BN_RECURSION)
547 if (bn_wexpand(rr, 8) == NULL)
550 bn_mul_comba4(rr->d, a->d, b->d);
555 if (bn_wexpand(rr, 16) == NULL)
558 bn_mul_comba8(rr->d, a->d, b->d);
562 #endif /* BN_MUL_COMBA */
564 if ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL)) {
565 if (i >= -1 && i <= 1) {
567 * Find out the power of two lower or equal to the longest of the
571 j = BN_num_bits_word((BN_ULONG)al);
574 j = BN_num_bits_word((BN_ULONG)bl);
577 assert(j <= al || j <= bl);
582 if (al > j || bl > j) {
583 if (bn_wexpand(t, k * 4) == NULL)
585 if (bn_wexpand(rr, k * 4) == NULL)
587 bn_mul_part_recursive(rr->d, a->d, b->d,
588 j, al - j, bl - j, t->d);
589 } else { /* al <= j || bl <= j */
591 if (bn_wexpand(t, k * 2) == NULL)
593 if (bn_wexpand(rr, k * 2) == NULL)
595 bn_mul_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d);
601 #endif /* BN_RECURSION */
602 if (bn_wexpand(rr, top) == NULL)
605 bn_mul_normal(rr->d, a->d, al, b->d, bl);
607 #if defined(BN_MUL_COMBA) || defined(BN_RECURSION)
610 rr->neg = a->neg ^ b->neg;
611 rr->flags |= BN_FLG_FIXED_TOP;
612 if (r != rr && BN_copy(r, rr) == NULL)
622 void bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b, int nb)
640 (void)bn_mul_words(r, a, na, 0);
643 rr[0] = bn_mul_words(r, a, na, b[0]);
648 rr[1] = bn_mul_add_words(&(r[1]), a, na, b[1]);
651 rr[2] = bn_mul_add_words(&(r[2]), a, na, b[2]);
654 rr[3] = bn_mul_add_words(&(r[3]), a, na, b[3]);
657 rr[4] = bn_mul_add_words(&(r[4]), a, na, b[4]);
664 void bn_mul_low_normal(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n)
666 bn_mul_words(r, a, n, b[0]);
671 bn_mul_add_words(&(r[1]), a, n, b[1]);
674 bn_mul_add_words(&(r[2]), a, n, b[2]);
677 bn_mul_add_words(&(r[3]), a, n, b[3]);
680 bn_mul_add_words(&(r[4]), a, n, b[4]);