3 * $Id: mpx-kmul.c,v 1.9 2004/03/27 17:54:12 mdw Exp $
5 * Karatsuba's multiplication algorithm
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Revision history --------------------------------------------------*
32 * $Log: mpx-kmul.c,v $
33 * Revision 1.9 2004/03/27 17:54:12 mdw
34 * Standard curves and curve checking.
36 * Revision 1.8 2002/10/09 00:36:03 mdw
37 * Fix bounds on workspace for Karatsuba operations.
39 * Revision 1.7 2000/10/08 15:48:35 mdw
40 * Rename Karatsuba constants now that we have @gfx_kmul@ too.
42 * Revision 1.6 2000/10/08 12:11:01 mdw
43 * Use @mpx_ueq@ instead of @MPX_UCMP@.
45 * Revision 1.5 2000/07/29 17:04:02 mdw
46 * Remove useless header `mpscan.h'.
48 * Revision 1.4 2000/06/17 11:42:11 mdw
49 * Moved the Karatsuba macros into a separate file for better sharing.
50 * Fixed some comments.
52 * Revision 1.3 1999/12/13 15:35:01 mdw
53 * Simplify and improve.
55 * Revision 1.2 1999/12/11 10:58:02 mdw
56 * Remove tweakable comments.
58 * Revision 1.1 1999/12/10 23:23:51 mdw
59 * Karatsuba-Ofman multiplication algorithm.
63 /*----- Header files ------------------------------------------------------*/
69 #include "karatsuba.h"
71 /*----- Tweakables --------------------------------------------------------*/
75 # define MPK_THRESH 4 /* Smallest possible correct value */
78 /*----- Main code ---------------------------------------------------------*/
80 /* --- @mpx_kmul@ --- *
82 * Arguments: @mpw *dv, *dvl@ = pointer to destination buffer
83 * @const mpw *av, *avl@ = pointer to first argument
84 * @const mpw *bv, *bvl@ = pointer to second argument
85 * @mpw *sv, *svl@ = pointer to scratch workspace
89 * Use: Multiplies two multiprecision integers using Karatsuba's
90 * algorithm. This is rather faster than traditional long
91 * multiplication (e.g., @mpx_umul@) on large numbers, although
92 * more expensive on small ones.
94 * The destination must be three times as large as the larger
95 * argument. The scratch space must be five times as large as
96 * the larger argument.
99 void mpx_kmul(mpw
*dv
, mpw
*dvl
,
100 const mpw
*av
, const mpw
*avl
,
101 const mpw
*bv
, const mpw
*bvl
,
104 const mpw
*avm
, *bvm
;
107 /* --- Dispose of easy cases to @mpx_umul@ --- *
109 * Karatsuba is only a win on large numbers, because of all the
110 * recursiveness and bookkeeping. The recursive calls make a quick check
111 * to see whether to bottom out to @mpx_umul@ which should help quite a
112 * lot, but sometimes the only way to know is to make sure...
118 if (avl
- av
<= MPK_THRESH
|| bvl
- bv
<= MPK_THRESH
) {
119 mpx_umul(dv
, dvl
, av
, avl
, bv
, bvl
);
123 /* --- How the algorithm works --- *
125 * Let %$A = xb + y$% and %$B = ub + v$%. Then, simply by expanding,
126 * %$AB = x u b^2 + b(x v + y u) + y v$%. That's not helped any, because
127 * I've got four multiplications, each four times easier than the one I
128 * started with. However, note that I can rewrite the coefficient of %$b$%
129 * as %$xv + yu = (x + y)(u + v) - xu - yv$%. The terms %$xu$% and %$yv$%
130 * I've already calculated, and that leaves only one more multiplication to
131 * do. So now I have three multiplications, each four times easier, and
135 /* --- First things --- *
137 * Sort out where to break the factors in half. I'll choose the midpoint
138 * of the larger one, since this minimizes the amount of work I have to do
142 if (avl
- av
> bvl
- bv
) {
143 m
= (avl
- av
+ 1) >> 1;
150 m
= (bvl
- bv
+ 1) >> 1;
158 /* --- Sort out the middle term --- */
161 mpw
*bsv
= sv
+ m
+ 1, *ssv
= bsv
+ m
+ 1;
162 mpw
*rdv
= dv
+ m
, *rdvl
= rdv
+ 2 * (m
+ 2);
166 UADD2(sv
, bsv
, av
, avm
, avm
, avl
);
167 UADD2(bsv
, ssv
, bv
, bvm
, bvm
, bvl
);
169 mpx_kmul(rdv
, rdvl
, sv
, bsv
, bsv
, ssv
, ssv
, svl
);
171 mpx_umul(rdv
, rdvl
, sv
, bsv
, bsv
, ssv
);
174 /* --- Sort out the other two terms --- */
177 mpw
*svm
= sv
+ m
, *svn
= svm
+ m
, *ssv
= svn
+ 4;
181 if (avl
== avm
|| bvl
== bvm
)
182 MPX_ZERO(rdv
+ m
+ 1, dvl
);
185 mpx_kmul(sv
, ssv
, avm
, avl
, bvm
, bvl
, ssv
, svl
);
187 mpx_umul(sv
, ssv
, avm
, avl
, bvm
, bvl
);
188 MPX_COPY(rdv
+ m
+ 1, dvl
, svm
+ 1, svn
);
189 UADD(rdv
, sv
, svm
+ 1);
194 mpx_kmul(sv
, ssv
, av
, avm
, bv
, bvm
, ssv
, svl
);
196 mpx_umul(sv
, ssv
, av
, avm
, bv
, bvm
);
197 MPX_COPY(dv
, tdv
, sv
, svm
);
203 /*----- Test rig ----------------------------------------------------------*/
207 #include <mLib/alloc.h>
208 #include <mLib/testrig.h>
210 #define ALLOC(v, vl, sz) do { \
212 mpw *_vv = xmalloc(MPWS(_sz)); \
213 mpw *_vvl = _vv + _sz; \
218 #define LOAD(v, vl, d) do { \
219 const dstr *_d = (d); \
221 ALLOC(_v, _vl, MPW_RQ(_d->len)); \
222 mpx_loadb(_v, _vl, _d->buf, _d->len); \
227 #define MAX(x, y) ((x) > (y) ? (x) : (y))
229 static void dumpmp(const char *msg
, const mpw
*v
, const mpw
*vl
)
234 fprintf(stderr
, " %08lx", (unsigned long)*--vl
);
238 static int umul(dstr
*v
)
251 m
= MAX(al
- a
, bl
- b
) + 1;
255 mpx_kmul(d
, dl
, a
, al
, b
, bl
, s
, sl
);
256 if (!mpx_ueq(d
, dl
, c
, cl
)) {
257 fprintf(stderr
, "\n*** umul failed\n");
260 dumpmp("expected", c
, cl
);
261 dumpmp(" result", d
, dl
);
265 free(a
); free(b
); free(c
); free(d
); free(s
);
269 static test_chunk defs
[] = {
270 { "umul", umul
, { &type_hex
, &type_hex
, &type_hex
, 0 } },
274 int main(int argc
, char *argv
[])
276 test_run(argc
, argv
, defs
, SRCDIR
"/tests/mpx");
282 /*----- That's all, folks -------------------------------------------------*/