1 #include "tommath_private.h"
2 #ifdef BN_S_MP_KARATSUBA_MUL_C
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
4 /* SPDX-License-Identifier: Unlicense */
6 /* c = |a| * |b| using Karatsuba Multiplication using
7 * three half size multiplications
9 * Let B represent the radix [e.g. 2**MP_DIGIT_BIT] and
10 * let n represent half of the number of digits in
17 a1b1 * B**2n + ((a1 + a0)(b1 + b0) - (a0b0 + a1b1)) * B + a0b0
19 * Note that a1b1 and a0b0 are used twice and only need to be
20 * computed once. So in total three half size (half # of
21 * digit) multiplications are performed, a0b0, a1b1 and
24 * Note that a multiplication of half the digits requires
25 * 1/4th the number of single precision multiplications so in
26 * total after one call 25% of the single precision multiplications
27 * are saved. Note also that the call to mp_mul can end up back
28 * in this function if the a0, a1, b0, or b1 are above the threshold.
29 * This is known as divide-and-conquer and leads to the famous
30 * O(N**lg(3)) or O(N**1.584) work which is asymptopically lower than
31 * the standard O(N**2) that the baseline/comba methods use.
32 * Generally though the overhead of this method doesn't pay off
33 * until a certain size (N ~ 80) is reached.
35 mp_err
s_mp_karatsuba_mul(const mp_int
*a
, const mp_int
*b
, mp_int
*c
)
37 mp_int x0
, x1
, y0
, y1
, t1
, x0y0
, x1y1
;
39 mp_err err
= MP_MEM
; /* default the return code to an error */
42 B
= MP_MIN(a
->used
, b
->used
);
44 /* now divide in two */
47 /* init copy all the temps */
48 if (mp_init_size(&x0
, B
) != MP_OKAY
) {
51 if (mp_init_size(&x1
, a
->used
- B
) != MP_OKAY
) {
54 if (mp_init_size(&y0
, B
) != MP_OKAY
) {
57 if (mp_init_size(&y1
, b
->used
- B
) != MP_OKAY
) {
62 if (mp_init_size(&t1
, B
* 2) != MP_OKAY
) {
65 if (mp_init_size(&x0y0
, B
* 2) != MP_OKAY
) {
68 if (mp_init_size(&x1y1
, B
* 2) != MP_OKAY
) {
72 /* now shift the digits */
73 x0
.used
= y0
.used
= B
;
74 x1
.used
= a
->used
- B
;
75 y1
.used
= b
->used
- B
;
79 mp_digit
*tmpa
, *tmpb
, *tmpx
, *tmpy
;
81 /* we copy the digits directly instead of using higher level functions
82 * since we also need to shift the digits
89 for (x
= 0; x
< B
; x
++) {
95 for (x
= B
; x
< a
->used
; x
++) {
100 for (x
= B
; x
< b
->used
; x
++) {
105 /* only need to clamp the lower words since by definition the
106 * upper words x1/y1 must have a known number of digits
111 /* now calc the products x0y0 and x1y1 */
112 /* after this x0 is no longer required, free temp [x0==t2]! */
113 if (mp_mul(&x0
, &y0
, &x0y0
) != MP_OKAY
) {
114 goto X1Y1
; /* x0y0 = x0*y0 */
116 if (mp_mul(&x1
, &y1
, &x1y1
) != MP_OKAY
) {
117 goto X1Y1
; /* x1y1 = x1*y1 */
120 /* now calc x1+x0 and y1+y0 */
121 if (s_mp_add(&x1
, &x0
, &t1
) != MP_OKAY
) {
122 goto X1Y1
; /* t1 = x1 - x0 */
124 if (s_mp_add(&y1
, &y0
, &x0
) != MP_OKAY
) {
125 goto X1Y1
; /* t2 = y1 - y0 */
127 if (mp_mul(&t1
, &x0
, &t1
) != MP_OKAY
) {
128 goto X1Y1
; /* t1 = (x1 + x0) * (y1 + y0) */
132 if (mp_add(&x0y0
, &x1y1
, &x0
) != MP_OKAY
) {
133 goto X1Y1
; /* t2 = x0y0 + x1y1 */
135 if (s_mp_sub(&t1
, &x0
, &t1
) != MP_OKAY
) {
136 goto X1Y1
; /* t1 = (x1+x0)*(y1+y0) - (x1y1 + x0y0) */
140 if (mp_lshd(&t1
, B
) != MP_OKAY
) {
141 goto X1Y1
; /* t1 = (x0y0 + x1y1 - (x1-x0)*(y1-y0))<<B */
143 if (mp_lshd(&x1y1
, B
* 2) != MP_OKAY
) {
144 goto X1Y1
; /* x1y1 = x1y1 << 2*B */
147 if (mp_add(&x0y0
, &t1
, &t1
) != MP_OKAY
) {
148 goto X1Y1
; /* t1 = x0y0 + t1 */
150 if (mp_add(&t1
, &x1y1
, c
) != MP_OKAY
) {
151 goto X1Y1
; /* t1 = x0y0 + t1 + x1y1 */
154 /* Algorithm succeeded set the return code to MP_OKAY */