2 #ifdef BN_MP_KARATSUBA_MUL_C
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis
5 * LibTomMath is a library that provides multiple-precision
6 * integer arithmetic as well as number theoretic functionality.
8 * The library was designed directly after the MPI library by
9 * Michael Fromberger but has been written from scratch with
10 * additional optimizations in place.
12 * The library is free for all purposes without any express
15 * Tom St Denis, tomstdenis@gmail.com, http://libtom.org
18 /* c = |a| * |b| using Karatsuba Multiplication using
19 * three half size multiplications
21 * Let B represent the radix [e.g. 2**DIGIT_BIT] and
22 * let n represent half of the number of digits in
29 a1b1 * B**2n + ((a1 + a0)(b1 + b0) - (a0b0 + a1b1)) * B + a0b0
31 * Note that a1b1 and a0b0 are used twice and only need to be
32 * computed once. So in total three half size (half # of
33 * digit) multiplications are performed, a0b0, a1b1 and
36 * Note that a multiplication of half the digits requires
37 * 1/4th the number of single precision multiplications so in
38 * total after one call 25% of the single precision multiplications
39 * are saved. Note also that the call to mp_mul can end up back
40 * in this function if the a0, a1, b0, or b1 are above the threshold.
41 * This is known as divide-and-conquer and leads to the famous
42 * O(N**lg(3)) or O(N**1.584) work which is asymptopically lower than
43 * the standard O(N**2) that the baseline/comba methods use.
44 * Generally though the overhead of this method doesn't pay off
45 * until a certain size (N ~ 80) is reached.
47 int mp_karatsuba_mul (mp_int * a, mp_int * b, mp_int * c)
49 mp_int x0, x1, y0, y1, t1, x0y0, x1y1;
52 /* default the return code to an error */
56 B = MIN (a->used, b->used);
58 /* now divide in two */
61 /* init copy all the temps */
62 if (mp_init_size (&x0, B) != MP_OKAY)
64 if (mp_init_size (&x1, a->used - B) != MP_OKAY)
66 if (mp_init_size (&y0, B) != MP_OKAY)
68 if (mp_init_size (&y1, b->used - B) != MP_OKAY)
72 if (mp_init_size (&t1, B * 2) != MP_OKAY)
74 if (mp_init_size (&x0y0, B * 2) != MP_OKAY)
76 if (mp_init_size (&x1y1, B * 2) != MP_OKAY)
79 /* now shift the digits */
80 x0.used = y0.used = B;
81 x1.used = a->used - B;
82 y1.used = b->used - B;
86 register mp_digit *tmpa, *tmpb, *tmpx, *tmpy;
88 /* we copy the digits directly instead of using higher level functions
89 * since we also need to shift the digits
96 for (x = 0; x < B; x++) {
102 for (x = B; x < a->used; x++) {
107 for (x = B; x < b->used; x++) {
112 /* only need to clamp the lower words since by definition the
113 * upper words x1/y1 must have a known number of digits
118 /* now calc the products x0y0 and x1y1 */
119 /* after this x0 is no longer required, free temp [x0==t2]! */
120 if (mp_mul (&x0, &y0, &x0y0) != MP_OKAY)
121 goto X1Y1; /* x0y0 = x0*y0 */
122 if (mp_mul (&x1, &y1, &x1y1) != MP_OKAY)
123 goto X1Y1; /* x1y1 = x1*y1 */
125 /* now calc x1+x0 and y1+y0 */
126 if (s_mp_add (&x1, &x0, &t1) != MP_OKAY)
127 goto X1Y1; /* t1 = x1 - x0 */
128 if (s_mp_add (&y1, &y0, &x0) != MP_OKAY)
129 goto X1Y1; /* t2 = y1 - y0 */
130 if (mp_mul (&t1, &x0, &t1) != MP_OKAY)
131 goto X1Y1; /* t1 = (x1 + x0) * (y1 + y0) */
134 if (mp_add (&x0y0, &x1y1, &x0) != MP_OKAY)
135 goto X1Y1; /* t2 = x0y0 + x1y1 */
136 if (s_mp_sub (&t1, &x0, &t1) != MP_OKAY)
137 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 */
142 if (mp_lshd (&x1y1, B * 2) != MP_OKAY)
143 goto X1Y1; /* x1y1 = x1y1 << 2*B */
145 if (mp_add (&x0y0, &t1, &t1) != MP_OKAY)
146 goto X1Y1; /* t1 = x0y0 + t1 */
147 if (mp_add (&t1, &x1y1, c) != MP_OKAY)
148 goto X1Y1; /* t1 = x0y0 + t1 + x1y1 */
150 /* Algorithm succeeded set the return code to MP_OKAY */
153 X1Y1:mp_clear (&x1y1);
154 X0Y0:mp_clear (&x0y0);
165 /* $Source: /cvs/libtom/libtommath/bn_mp_karatsuba_mul.c,v $ */
166 /* $Revision: 1.6 $ */
167 /* $Date: 2006/12/28 01:25:13 $ */