2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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) {
157 BN_ULONG bn_add_part_words(BN_ULONG *r,
158 const BN_ULONG *a, const BN_ULONG *b,
164 c = bn_add_words(r, a, b, cl);
176 l = (c + b[0]) & BN_MASK2;
182 l = (c + b[1]) & BN_MASK2;
188 l = (c + b[2]) & BN_MASK2;
194 l = (c + b[3]) & BN_MASK2;
206 switch (dl - save_dl) {
248 t = (a[0] + c) & BN_MASK2;
254 t = (a[1] + c) & BN_MASK2;
260 t = (a[2] + c) & BN_MASK2;
266 t = (a[3] + c) & BN_MASK2;
278 switch (save_dl - dl) {
323 * Karatsuba recursive multiplication algorithm (cf. Knuth, The Art of
324 * Computer Programming, Vol. 2)
328 * r is 2*n2 words in size,
329 * a and b are both n2 words in size.
330 * n2 must be a power of 2.
331 * We multiply and return the result.
332 * t must be 2*n2 words in size
335 * a[0]*b[0]+a[1]*b[1]+(a[0]-a[1])*(b[1]-b[0])
338 /* dnX may not be positive, but n2/2+dnX has to be */
339 void bn_mul_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2,
340 int dna, int dnb, BN_ULONG *t)
342 int n = n2 / 2, c1, c2;
343 int tna = n + dna, tnb = n + dnb;
344 unsigned int neg, zero;
350 bn_mul_comba4(r, a, b);
355 * Only call bn_mul_comba 8 if n2 == 8 and the two arrays are complete
358 if (n2 == 8 && dna == 0 && dnb == 0) {
359 bn_mul_comba8(r, a, b);
362 # endif /* BN_MUL_COMBA */
363 /* Else do normal multiply */
364 if (n2 < BN_MUL_RECURSIVE_SIZE_NORMAL) {
365 bn_mul_normal(r, a, n2 + dna, b, n2 + dnb);
367 memset(&r[2 * n2 + dna + dnb], 0,
368 sizeof(BN_ULONG) * -(dna + dnb));
371 /* r=(a[0]-a[1])*(b[1]-b[0]) */
372 c1 = bn_cmp_part_words(a, &(a[n]), tna, n - tna);
373 c2 = bn_cmp_part_words(&(b[n]), b, tnb, tnb - n);
375 switch (c1 * 3 + c2) {
377 bn_sub_part_words(t, &(a[n]), a, tna, tna - n); /* - */
378 bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); /* - */
384 bn_sub_part_words(t, &(a[n]), a, tna, tna - n); /* - */
385 bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n); /* + */
394 bn_sub_part_words(t, a, &(a[n]), tna, n - tna); /* + */
395 bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); /* - */
402 bn_sub_part_words(t, a, &(a[n]), tna, n - tna);
403 bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n);
408 if (n == 4 && dna == 0 && dnb == 0) { /* XXX: bn_mul_comba4 could take
409 * extra args to do this well */
411 bn_mul_comba4(&(t[n2]), t, &(t[n]));
413 memset(&t[n2], 0, sizeof(*t) * 8);
415 bn_mul_comba4(r, a, b);
416 bn_mul_comba4(&(r[n2]), &(a[n]), &(b[n]));
417 } else if (n == 8 && dna == 0 && dnb == 0) { /* XXX: bn_mul_comba8 could
418 * take extra args to do
421 bn_mul_comba8(&(t[n2]), t, &(t[n]));
423 memset(&t[n2], 0, sizeof(*t) * 16);
425 bn_mul_comba8(r, a, b);
426 bn_mul_comba8(&(r[n2]), &(a[n]), &(b[n]));
428 # endif /* BN_MUL_COMBA */
432 bn_mul_recursive(&(t[n2]), t, &(t[n]), n, 0, 0, p);
434 memset(&t[n2], 0, sizeof(*t) * n2);
435 bn_mul_recursive(r, a, b, n, 0, 0, p);
436 bn_mul_recursive(&(r[n2]), &(a[n]), &(b[n]), n, dna, dnb, p);
440 * t[32] holds (a[0]-a[1])*(b[1]-b[0]), c1 is the sign
441 * r[10] holds (a[0]*b[0])
442 * r[32] holds (b[1]*b[1])
445 c1 = (int)(bn_add_words(t, r, &(r[n2]), n2));
447 if (neg) { /* if t[32] is negative */
448 c1 -= (int)(bn_sub_words(&(t[n2]), t, &(t[n2]), n2));
450 /* Might have a carry */
451 c1 += (int)(bn_add_words(&(t[n2]), &(t[n2]), t, n2));
455 * t[32] holds (a[0]-a[1])*(b[1]-b[0])+(a[0]*b[0])+(a[1]*b[1])
456 * r[10] holds (a[0]*b[0])
457 * r[32] holds (b[1]*b[1])
458 * c1 holds the carry bits
460 c1 += (int)(bn_add_words(&(r[n]), &(r[n]), &(t[n2]), n2));
464 ln = (lo + c1) & BN_MASK2;
468 * The overflow will stop before we over write words we should not
471 if (ln < (BN_ULONG)c1) {
475 ln = (lo + 1) & BN_MASK2;
483 * n+tn is the word length t needs to be n*4 is size, as does r
485 /* tnX may not be negative but less than n */
486 void bn_mul_part_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n,
487 int tna, int tnb, BN_ULONG *t)
489 int i, j, n2 = n * 2;
494 bn_mul_normal(r, a, n + tna, b, n + tnb);
498 /* r=(a[0]-a[1])*(b[1]-b[0]) */
499 c1 = bn_cmp_part_words(a, &(a[n]), tna, n - tna);
500 c2 = bn_cmp_part_words(&(b[n]), b, tnb, tnb - n);
502 switch (c1 * 3 + c2) {
504 bn_sub_part_words(t, &(a[n]), a, tna, tna - n); /* - */
505 bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); /* - */
510 bn_sub_part_words(t, &(a[n]), a, tna, tna - n); /* - */
511 bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n); /* + */
519 bn_sub_part_words(t, a, &(a[n]), tna, n - tna); /* + */
520 bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); /* - */
526 bn_sub_part_words(t, a, &(a[n]), tna, n - tna);
527 bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n);
531 * The zero case isn't yet implemented here. The speedup would probably
536 bn_mul_comba4(&(t[n2]), t, &(t[n]));
537 bn_mul_comba4(r, a, b);
538 bn_mul_normal(&(r[n2]), &(a[n]), tn, &(b[n]), tn);
539 memset(&r[n2 + tn * 2], 0, sizeof(*r) * (n2 - tn * 2));
543 bn_mul_comba8(&(t[n2]), t, &(t[n]));
544 bn_mul_comba8(r, a, b);
545 bn_mul_normal(&(r[n2]), &(a[n]), tna, &(b[n]), tnb);
546 memset(&r[n2 + tna + tnb], 0, sizeof(*r) * (n2 - tna - tnb));
549 bn_mul_recursive(&(t[n2]), t, &(t[n]), n, 0, 0, p);
550 bn_mul_recursive(r, a, b, n, 0, 0, p);
553 * If there is only a bottom half to the number, just do it
560 bn_mul_recursive(&(r[n2]), &(a[n]), &(b[n]),
561 i, tna - i, tnb - i, p);
562 memset(&r[n2 + i * 2], 0, sizeof(*r) * (n2 - i * 2));
563 } else if (j > 0) { /* eg, n == 16, i == 8 and tn == 11 */
564 bn_mul_part_recursive(&(r[n2]), &(a[n]), &(b[n]),
565 i, tna - i, tnb - i, p);
566 memset(&(r[n2 + tna + tnb]), 0,
567 sizeof(BN_ULONG) * (n2 - tna - tnb));
568 } else { /* (j < 0) eg, n == 16, i == 8 and tn == 5 */
570 memset(&r[n2], 0, sizeof(*r) * n2);
571 if (tna < BN_MUL_RECURSIVE_SIZE_NORMAL
572 && tnb < BN_MUL_RECURSIVE_SIZE_NORMAL) {
573 bn_mul_normal(&(r[n2]), &(a[n]), tna, &(b[n]), tnb);
578 * these simplified conditions work exclusively because
579 * difference between tna and tnb is 1 or 0
581 if (i < tna || i < tnb) {
582 bn_mul_part_recursive(&(r[n2]),
584 i, tna - i, tnb - i, p);
586 } else if (i == tna || i == tnb) {
587 bn_mul_recursive(&(r[n2]),
589 i, tna - i, tnb - i, p);
598 * t[32] holds (a[0]-a[1])*(b[1]-b[0]), c1 is the sign
599 * r[10] holds (a[0]*b[0])
600 * r[32] holds (b[1]*b[1])
603 c1 = (int)(bn_add_words(t, r, &(r[n2]), n2));
605 if (neg) { /* if t[32] is negative */
606 c1 -= (int)(bn_sub_words(&(t[n2]), t, &(t[n2]), n2));
608 /* Might have a carry */
609 c1 += (int)(bn_add_words(&(t[n2]), &(t[n2]), t, n2));
613 * t[32] holds (a[0]-a[1])*(b[1]-b[0])+(a[0]*b[0])+(a[1]*b[1])
614 * r[10] holds (a[0]*b[0])
615 * r[32] holds (b[1]*b[1])
616 * c1 holds the carry bits
618 c1 += (int)(bn_add_words(&(r[n]), &(r[n]), &(t[n2]), n2));
622 ln = (lo + c1) & BN_MASK2;
626 * The overflow will stop before we over write words we should not
629 if (ln < (BN_ULONG)c1) {
633 ln = (lo + 1) & BN_MASK2;
641 * a and b must be the same size, which is n2.
642 * r needs to be n2 words and t needs to be n2*2
644 void bn_mul_low_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2,
649 bn_mul_recursive(r, a, b, n, 0, 0, &(t[0]));
650 if (n >= BN_MUL_LOW_RECURSIVE_SIZE_NORMAL) {
651 bn_mul_low_recursive(&(t[0]), &(a[0]), &(b[n]), n, &(t[n2]));
652 bn_add_words(&(r[n]), &(r[n]), &(t[0]), n);
653 bn_mul_low_recursive(&(t[0]), &(a[n]), &(b[0]), n, &(t[n2]));
654 bn_add_words(&(r[n]), &(r[n]), &(t[0]), n);
656 bn_mul_low_normal(&(t[0]), &(a[0]), &(b[n]), n);
657 bn_mul_low_normal(&(t[n]), &(a[n]), &(b[0]), n);
658 bn_add_words(&(r[n]), &(r[n]), &(t[0]), n);
659 bn_add_words(&(r[n]), &(r[n]), &(t[n]), n);
664 * a and b must be the same size, which is n2.
665 * r needs to be n2 words and t needs to be n2*2
666 * l is the low words of the output.
669 void bn_mul_high(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, BN_ULONG *l, int n2,
675 BN_ULONG ll, lc, *lp, *mp;
679 /* Calculate (al-ah)*(bh-bl) */
681 c1 = bn_cmp_words(&(a[0]), &(a[n]), n);
682 c2 = bn_cmp_words(&(b[n]), &(b[0]), n);
683 switch (c1 * 3 + c2) {
685 bn_sub_words(&(r[0]), &(a[n]), &(a[0]), n);
686 bn_sub_words(&(r[n]), &(b[0]), &(b[n]), n);
692 bn_sub_words(&(r[0]), &(a[n]), &(a[0]), n);
693 bn_sub_words(&(r[n]), &(b[n]), &(b[0]), n);
702 bn_sub_words(&(r[0]), &(a[0]), &(a[n]), n);
703 bn_sub_words(&(r[n]), &(b[0]), &(b[n]), n);
710 bn_sub_words(&(r[0]), &(a[0]), &(a[n]), n);
711 bn_sub_words(&(r[n]), &(b[n]), &(b[0]), n);
716 /* t[10] = (a[0]-a[1])*(b[1]-b[0]) */
717 /* r[10] = (a[1]*b[1]) */
720 bn_mul_comba8(&(t[0]), &(r[0]), &(r[n]));
721 bn_mul_comba8(r, &(a[n]), &(b[n]));
725 bn_mul_recursive(&(t[0]), &(r[0]), &(r[n]), n, 0, 0, &(t[n2]));
726 bn_mul_recursive(r, &(a[n]), &(b[n]), n, 0, 0, &(t[n2]));
731 * s1 == low(ah*bh)+low((al-ah)*(bh-bl))+low(al*bl)+high(al*bl)
732 * We know s0 and s1 so the only unknown is high(al*bl)
733 * high(al*bl) == s1 - low(ah*bh+s0+(al-ah)*(bh-bl))
734 * high(al*bl) == s1 - (r[0]+l[0]+t[0])
738 bn_add_words(lp, &(r[0]), &(l[0]), n);
744 neg = (int)(bn_sub_words(&(t[n2]), lp, &(t[0]), n));
746 bn_add_words(&(t[n2]), lp, &(t[0]), n);
751 bn_sub_words(&(t[n2 + n]), &(l[n]), &(t[n2]), n);
755 for (i = 0; i < n; i++)
756 lp[i] = ((~mp[i]) + 1) & BN_MASK2;
762 * t[10] = (a[0]-a[1])*(b[1]-b[0]) neg is the sign
763 * r[10] = (a[1]*b[1])
767 * R[21] = al*bl + ah*bh + (a[0]-a[1])*(b[1]-b[0])
771 * R[1]=t[3]+l[0]+r[0](+-)t[0] (have carry/borrow)
772 * R[2]=r[0]+t[3]+r[1](+-)t[1] (have carry/borrow)
773 * R[3]=r[1]+(carry/borrow)
777 c1 = (int)(bn_add_words(lp, &(t[n2 + n]), &(l[0]), n));
782 c1 += (int)(bn_add_words(&(t[n2]), lp, &(r[0]), n));
784 c1 -= (int)(bn_sub_words(&(t[n2]), &(t[n2]), &(t[0]), n));
786 c1 += (int)(bn_add_words(&(t[n2]), &(t[n2]), &(t[0]), n));
788 c2 = (int)(bn_add_words(&(r[0]), &(r[0]), &(t[n2 + n]), n));
789 c2 += (int)(bn_add_words(&(r[0]), &(r[0]), &(r[n]), n));
791 c2 -= (int)(bn_sub_words(&(r[0]), &(r[0]), &(t[n]), n));
793 c2 += (int)(bn_add_words(&(r[0]), &(r[0]), &(t[n]), n));
795 if (c1 != 0) { /* Add starting at r[0], could be +ve or -ve */
800 ll = (r[i] + lc) & BN_MASK2;
808 r[i++] = (ll - lc) & BN_MASK2;
813 if (c2 != 0) { /* Add starting at r[1] */
818 ll = (r[i] + lc) & BN_MASK2;
826 r[i++] = (ll - lc) & BN_MASK2;
832 #endif /* BN_RECURSION */
834 int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
839 #if defined(BN_MUL_COMBA) || defined(BN_RECURSION)
854 if ((al == 0) || (bl == 0)) {
861 if ((r == a) || (r == b)) {
862 if ((rr = BN_CTX_get(ctx)) == NULL)
867 #if defined(BN_MUL_COMBA) || defined(BN_RECURSION)
874 if (bn_wexpand(rr, 8) == NULL)
877 bn_mul_comba4(rr->d, a->d, b->d);
882 if (bn_wexpand(rr, 16) == NULL)
885 bn_mul_comba8(rr->d, a->d, b->d);
889 #endif /* BN_MUL_COMBA */
891 if ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL)) {
892 if (i >= -1 && i <= 1) {
894 * Find out the power of two lower or equal to the longest of the
898 j = BN_num_bits_word((BN_ULONG)al);
901 j = BN_num_bits_word((BN_ULONG)bl);
904 assert(j <= al || j <= bl);
909 if (al > j || bl > j) {
910 if (bn_wexpand(t, k * 4) == NULL)
912 if (bn_wexpand(rr, k * 4) == NULL)
914 bn_mul_part_recursive(rr->d, a->d, b->d,
915 j, al - j, bl - j, t->d);
916 } else { /* al <= j || bl <= j */
918 if (bn_wexpand(t, k * 2) == NULL)
920 if (bn_wexpand(rr, k * 2) == NULL)
922 bn_mul_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d);
928 #endif /* BN_RECURSION */
929 if (bn_wexpand(rr, top) == NULL)
932 bn_mul_normal(rr->d, a->d, al, b->d, bl);
934 #if defined(BN_MUL_COMBA) || defined(BN_RECURSION)
937 rr->neg = a->neg ^ b->neg;
939 if (r != rr && BN_copy(r, rr) == NULL)
949 void bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b, int nb)
967 (void)bn_mul_words(r, a, na, 0);
970 rr[0] = bn_mul_words(r, a, na, b[0]);
975 rr[1] = bn_mul_add_words(&(r[1]), a, na, b[1]);
978 rr[2] = bn_mul_add_words(&(r[2]), a, na, b[2]);
981 rr[3] = bn_mul_add_words(&(r[3]), a, na, b[3]);
984 rr[4] = bn_mul_add_words(&(r[4]), a, na, b[4]);
991 void bn_mul_low_normal(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n)
993 bn_mul_words(r, a, n, b[0]);
998 bn_mul_add_words(&(r[1]), a, n, b[1]);
1001 bn_mul_add_words(&(r[2]), a, n, b[2]);
1004 bn_mul_add_words(&(r[3]), a, n, b[3]);
1007 bn_mul_add_words(&(r[4]), a, n, b[4]);