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 * Karatsuba recursive multiplication algorithm (cf. Knuth, The Art of
158 * Computer Programming, Vol. 2)
162 * r is 2*n2 words in size,
163 * a and b are both n2 words in size.
164 * n2 must be a power of 2.
165 * We multiply and return the result.
166 * t must be 2*n2 words in size
169 * a[0]*b[0]+a[1]*b[1]+(a[0]-a[1])*(b[1]-b[0])
172 /* dnX may not be positive, but n2/2+dnX has to be */
173 void bn_mul_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2,
174 int dna, int dnb, BN_ULONG *t)
176 int n = n2 / 2, c1, c2;
177 int tna = n + dna, tnb = n + dnb;
178 unsigned int neg, zero;
184 bn_mul_comba4(r, a, b);
189 * Only call bn_mul_comba 8 if n2 == 8 and the two arrays are complete
192 if (n2 == 8 && dna == 0 && dnb == 0) {
193 bn_mul_comba8(r, a, b);
196 # endif /* BN_MUL_COMBA */
197 /* Else do normal multiply */
198 if (n2 < BN_MUL_RECURSIVE_SIZE_NORMAL) {
199 bn_mul_normal(r, a, n2 + dna, b, n2 + dnb);
201 memset(&r[2 * n2 + dna + dnb], 0,
202 sizeof(BN_ULONG) * -(dna + dnb));
205 /* r=(a[0]-a[1])*(b[1]-b[0]) */
206 c1 = bn_cmp_part_words(a, &(a[n]), tna, n - tna);
207 c2 = bn_cmp_part_words(&(b[n]), b, tnb, tnb - n);
209 switch (c1 * 3 + c2) {
211 bn_sub_part_words(t, &(a[n]), a, tna, tna - n); /* - */
212 bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); /* - */
218 bn_sub_part_words(t, &(a[n]), a, tna, tna - n); /* - */
219 bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n); /* + */
228 bn_sub_part_words(t, a, &(a[n]), tna, n - tna); /* + */
229 bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); /* - */
236 bn_sub_part_words(t, a, &(a[n]), tna, n - tna);
237 bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n);
242 if (n == 4 && dna == 0 && dnb == 0) { /* XXX: bn_mul_comba4 could take
243 * extra args to do this well */
245 bn_mul_comba4(&(t[n2]), t, &(t[n]));
247 memset(&t[n2], 0, sizeof(*t) * 8);
249 bn_mul_comba4(r, a, b);
250 bn_mul_comba4(&(r[n2]), &(a[n]), &(b[n]));
251 } else if (n == 8 && dna == 0 && dnb == 0) { /* XXX: bn_mul_comba8 could
252 * take extra args to do
255 bn_mul_comba8(&(t[n2]), t, &(t[n]));
257 memset(&t[n2], 0, sizeof(*t) * 16);
259 bn_mul_comba8(r, a, b);
260 bn_mul_comba8(&(r[n2]), &(a[n]), &(b[n]));
262 # endif /* BN_MUL_COMBA */
266 bn_mul_recursive(&(t[n2]), t, &(t[n]), n, 0, 0, p);
268 memset(&t[n2], 0, sizeof(*t) * n2);
269 bn_mul_recursive(r, a, b, n, 0, 0, p);
270 bn_mul_recursive(&(r[n2]), &(a[n]), &(b[n]), n, dna, dnb, p);
274 * t[32] holds (a[0]-a[1])*(b[1]-b[0]), c1 is the sign
275 * r[10] holds (a[0]*b[0])
276 * r[32] holds (b[1]*b[1])
279 c1 = (int)(bn_add_words(t, r, &(r[n2]), n2));
281 if (neg) { /* if t[32] is negative */
282 c1 -= (int)(bn_sub_words(&(t[n2]), t, &(t[n2]), n2));
284 /* Might have a carry */
285 c1 += (int)(bn_add_words(&(t[n2]), &(t[n2]), t, n2));
289 * t[32] holds (a[0]-a[1])*(b[1]-b[0])+(a[0]*b[0])+(a[1]*b[1])
290 * r[10] holds (a[0]*b[0])
291 * r[32] holds (b[1]*b[1])
292 * c1 holds the carry bits
294 c1 += (int)(bn_add_words(&(r[n]), &(r[n]), &(t[n2]), n2));
298 ln = (lo + c1) & BN_MASK2;
302 * The overflow will stop before we over write words we should not
305 if (ln < (BN_ULONG)c1) {
309 ln = (lo + 1) & BN_MASK2;
317 * n+tn is the word length t needs to be n*4 is size, as does r
319 /* tnX may not be negative but less than n */
320 void bn_mul_part_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n,
321 int tna, int tnb, BN_ULONG *t)
323 int i, j, n2 = n * 2;
328 bn_mul_normal(r, a, n + tna, b, n + tnb);
332 /* r=(a[0]-a[1])*(b[1]-b[0]) */
333 c1 = bn_cmp_part_words(a, &(a[n]), tna, n - tna);
334 c2 = bn_cmp_part_words(&(b[n]), b, tnb, tnb - n);
336 switch (c1 * 3 + c2) {
338 bn_sub_part_words(t, &(a[n]), a, tna, tna - n); /* - */
339 bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); /* - */
343 bn_sub_part_words(t, &(a[n]), a, tna, tna - n); /* - */
344 bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n); /* + */
351 bn_sub_part_words(t, a, &(a[n]), tna, n - tna); /* + */
352 bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb); /* - */
357 bn_sub_part_words(t, a, &(a[n]), tna, n - tna);
358 bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n);
362 * The zero case isn't yet implemented here. The speedup would probably
367 bn_mul_comba4(&(t[n2]), t, &(t[n]));
368 bn_mul_comba4(r, a, b);
369 bn_mul_normal(&(r[n2]), &(a[n]), tn, &(b[n]), tn);
370 memset(&r[n2 + tn * 2], 0, sizeof(*r) * (n2 - tn * 2));
374 bn_mul_comba8(&(t[n2]), t, &(t[n]));
375 bn_mul_comba8(r, a, b);
376 bn_mul_normal(&(r[n2]), &(a[n]), tna, &(b[n]), tnb);
377 memset(&r[n2 + tna + tnb], 0, sizeof(*r) * (n2 - tna - tnb));
380 bn_mul_recursive(&(t[n2]), t, &(t[n]), n, 0, 0, p);
381 bn_mul_recursive(r, a, b, n, 0, 0, p);
384 * If there is only a bottom half to the number, just do it
391 bn_mul_recursive(&(r[n2]), &(a[n]), &(b[n]),
392 i, tna - i, tnb - i, p);
393 memset(&r[n2 + i * 2], 0, sizeof(*r) * (n2 - i * 2));
394 } else if (j > 0) { /* eg, n == 16, i == 8 and tn == 11 */
395 bn_mul_part_recursive(&(r[n2]), &(a[n]), &(b[n]),
396 i, tna - i, tnb - i, p);
397 memset(&(r[n2 + tna + tnb]), 0,
398 sizeof(BN_ULONG) * (n2 - tna - tnb));
399 } else { /* (j < 0) eg, n == 16, i == 8 and tn == 5 */
401 memset(&r[n2], 0, sizeof(*r) * n2);
402 if (tna < BN_MUL_RECURSIVE_SIZE_NORMAL
403 && tnb < BN_MUL_RECURSIVE_SIZE_NORMAL) {
404 bn_mul_normal(&(r[n2]), &(a[n]), tna, &(b[n]), tnb);
409 * these simplified conditions work exclusively because
410 * difference between tna and tnb is 1 or 0
412 if (i < tna || i < tnb) {
413 bn_mul_part_recursive(&(r[n2]),
415 i, tna - i, tnb - i, p);
417 } else if (i == tna || i == tnb) {
418 bn_mul_recursive(&(r[n2]),
420 i, tna - i, tnb - i, p);
429 * t[32] holds (a[0]-a[1])*(b[1]-b[0]), c1 is the sign
430 * r[10] holds (a[0]*b[0])
431 * r[32] holds (b[1]*b[1])
434 c1 = (int)(bn_add_words(t, r, &(r[n2]), n2));
436 if (neg) { /* if t[32] is negative */
437 c1 -= (int)(bn_sub_words(&(t[n2]), t, &(t[n2]), n2));
439 /* Might have a carry */
440 c1 += (int)(bn_add_words(&(t[n2]), &(t[n2]), t, n2));
444 * t[32] holds (a[0]-a[1])*(b[1]-b[0])+(a[0]*b[0])+(a[1]*b[1])
445 * r[10] holds (a[0]*b[0])
446 * r[32] holds (b[1]*b[1])
447 * c1 holds the carry bits
449 c1 += (int)(bn_add_words(&(r[n]), &(r[n]), &(t[n2]), n2));
453 ln = (lo + c1) & BN_MASK2;
457 * The overflow will stop before we over write words we should not
460 if (ln < (BN_ULONG)c1) {
464 ln = (lo + 1) & BN_MASK2;
472 * a and b must be the same size, which is n2.
473 * r needs to be n2 words and t needs to be n2*2
475 void bn_mul_low_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n2,
480 bn_mul_recursive(r, a, b, n, 0, 0, &(t[0]));
481 if (n >= BN_MUL_LOW_RECURSIVE_SIZE_NORMAL) {
482 bn_mul_low_recursive(&(t[0]), &(a[0]), &(b[n]), n, &(t[n2]));
483 bn_add_words(&(r[n]), &(r[n]), &(t[0]), n);
484 bn_mul_low_recursive(&(t[0]), &(a[n]), &(b[0]), n, &(t[n2]));
485 bn_add_words(&(r[n]), &(r[n]), &(t[0]), n);
487 bn_mul_low_normal(&(t[0]), &(a[0]), &(b[n]), n);
488 bn_mul_low_normal(&(t[n]), &(a[n]), &(b[0]), n);
489 bn_add_words(&(r[n]), &(r[n]), &(t[0]), n);
490 bn_add_words(&(r[n]), &(r[n]), &(t[n]), n);
493 #endif /* BN_RECURSION */
495 int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
500 #if defined(BN_MUL_COMBA) || defined(BN_RECURSION)
515 if ((al == 0) || (bl == 0)) {
522 if ((r == a) || (r == b)) {
523 if ((rr = BN_CTX_get(ctx)) == NULL)
528 #if defined(BN_MUL_COMBA) || defined(BN_RECURSION)
535 if (bn_wexpand(rr, 8) == NULL)
538 bn_mul_comba4(rr->d, a->d, b->d);
543 if (bn_wexpand(rr, 16) == NULL)
546 bn_mul_comba8(rr->d, a->d, b->d);
550 #endif /* BN_MUL_COMBA */
552 if ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL)) {
553 if (i >= -1 && i <= 1) {
555 * Find out the power of two lower or equal to the longest of the
559 j = BN_num_bits_word((BN_ULONG)al);
562 j = BN_num_bits_word((BN_ULONG)bl);
565 assert(j <= al || j <= bl);
570 if (al > j || bl > j) {
571 if (bn_wexpand(t, k * 4) == NULL)
573 if (bn_wexpand(rr, k * 4) == NULL)
575 bn_mul_part_recursive(rr->d, a->d, b->d,
576 j, al - j, bl - j, t->d);
577 } else { /* al <= j || bl <= j */
579 if (bn_wexpand(t, k * 2) == NULL)
581 if (bn_wexpand(rr, k * 2) == NULL)
583 bn_mul_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d);
589 if (i == 1 && !BN_get_flags(b, BN_FLG_STATIC_DATA)) {
590 BIGNUM *tmp_bn = (BIGNUM *)b;
591 if (bn_wexpand(tmp_bn, al) == NULL)
596 } else if (i == -1 && !BN_get_flags(a, BN_FLG_STATIC_DATA)) {
597 BIGNUM *tmp_bn = (BIGNUM *)a;
598 if (bn_wexpand(tmp_bn, bl) == NULL)
605 /* symmetric and > 4 */
607 j = BN_num_bits_word((BN_ULONG)al);
611 if (al == j) { /* exact multiple */
612 if (bn_wexpand(t, k * 2) == NULL)
614 if (bn_wexpand(rr, k * 2) == NULL)
616 bn_mul_recursive(rr->d, a->d, b->d, al, t->d);
618 if (bn_wexpand(t, k * 4) == NULL)
620 if (bn_wexpand(rr, k * 4) == NULL)
622 bn_mul_part_recursive(rr->d, a->d, b->d, al - j, j, t->d);
629 #endif /* BN_RECURSION */
630 if (bn_wexpand(rr, top) == NULL)
633 bn_mul_normal(rr->d, a->d, al, b->d, bl);
635 #if defined(BN_MUL_COMBA) || defined(BN_RECURSION)
638 rr->neg = a->neg ^ b->neg;
640 if (r != rr && BN_copy(r, rr) == NULL)
650 void bn_mul_normal(BN_ULONG *r, BN_ULONG *a, int na, BN_ULONG *b, int nb)
668 (void)bn_mul_words(r, a, na, 0);
671 rr[0] = bn_mul_words(r, a, na, b[0]);
676 rr[1] = bn_mul_add_words(&(r[1]), a, na, b[1]);
679 rr[2] = bn_mul_add_words(&(r[2]), a, na, b[2]);
682 rr[3] = bn_mul_add_words(&(r[3]), a, na, b[3]);
685 rr[4] = bn_mul_add_words(&(r[4]), a, na, b[4]);
692 void bn_mul_low_normal(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n)
694 bn_mul_words(r, a, n, b[0]);
699 bn_mul_add_words(&(r[1]), a, n, b[1]);
702 bn_mul_add_words(&(r[2]), a, n, b[2]);
705 bn_mul_add_words(&(r[3]), a, n, b[3]);
708 bn_mul_add_words(&(r[4]), a, n, b[4]);