a86e33af |
1 | /* -*-c-*- |
2 | * |
432c4e18 |
3 | * $Id: mpx-kmul.c,v 1.9 2004/03/27 17:54:12 mdw Exp $ |
a86e33af |
4 | * |
5 | * Karatsuba's multiplication algorithm |
6 | * |
7 | * (c) 1999 Straylight/Edgeware |
8 | */ |
9 | |
10 | /*----- Licensing notice --------------------------------------------------* |
11 | * |
12 | * This file is part of Catacomb. |
13 | * |
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. |
18 | * |
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. |
23 | * |
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, |
27 | * MA 02111-1307, USA. |
28 | */ |
29 | |
30 | /*----- Revision history --------------------------------------------------* |
31 | * |
32 | * $Log: mpx-kmul.c,v $ |
432c4e18 |
33 | * Revision 1.9 2004/03/27 17:54:12 mdw |
34 | * Standard curves and curve checking. |
35 | * |
dd22938e |
36 | * Revision 1.8 2002/10/09 00:36:03 mdw |
37 | * Fix bounds on workspace for Karatsuba operations. |
38 | * |
52cdaca9 |
39 | * Revision 1.7 2000/10/08 15:48:35 mdw |
40 | * Rename Karatsuba constants now that we have @gfx_kmul@ too. |
41 | * |
c9060100 |
42 | * Revision 1.6 2000/10/08 12:11:01 mdw |
43 | * Use @mpx_ueq@ instead of @MPX_UCMP@. |
44 | * |
07dc33b2 |
45 | * Revision 1.5 2000/07/29 17:04:02 mdw |
46 | * Remove useless header `mpscan.h'. |
47 | * |
7d5fa32a |
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. |
51 | * |
4468424e |
52 | * Revision 1.3 1999/12/13 15:35:01 mdw |
53 | * Simplify and improve. |
54 | * |
1b756626 |
55 | * Revision 1.2 1999/12/11 10:58:02 mdw |
56 | * Remove tweakable comments. |
57 | * |
a86e33af |
58 | * Revision 1.1 1999/12/10 23:23:51 mdw |
59 | * Karatsuba-Ofman multiplication algorithm. |
60 | * |
61 | */ |
62 | |
63 | /*----- Header files ------------------------------------------------------*/ |
64 | |
4468424e |
65 | #include <assert.h> |
a86e33af |
66 | #include <stdio.h> |
67 | |
68 | #include "mpx.h" |
52cdaca9 |
69 | #include "karatsuba.h" |
a86e33af |
70 | |
71 | /*----- Tweakables --------------------------------------------------------*/ |
72 | |
a86e33af |
73 | #ifdef TEST_RIG |
52cdaca9 |
74 | # undef MPK_THRESH |
dd22938e |
75 | # define MPK_THRESH 4 /* Smallest possible correct value */ |
a86e33af |
76 | #endif |
77 | |
a86e33af |
78 | /*----- Main code ---------------------------------------------------------*/ |
79 | |
80 | /* --- @mpx_kmul@ --- * |
81 | * |
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 |
86 | * |
87 | * Returns: --- |
88 | * |
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. |
93 | * |
dd22938e |
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. |
a86e33af |
97 | */ |
98 | |
99 | void mpx_kmul(mpw *dv, mpw *dvl, |
100 | const mpw *av, const mpw *avl, |
101 | const mpw *bv, const mpw *bvl, |
102 | mpw *sv, mpw *svl) |
103 | { |
104 | const mpw *avm, *bvm; |
105 | size_t m; |
106 | |
107 | /* --- Dispose of easy cases to @mpx_umul@ --- * |
108 | * |
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... |
113 | */ |
114 | |
115 | MPX_SHRINK(av, avl); |
116 | MPX_SHRINK(bv, bvl); |
117 | |
52cdaca9 |
118 | if (avl - av <= MPK_THRESH || bvl - bv <= MPK_THRESH) { |
a86e33af |
119 | mpx_umul(dv, dvl, av, avl, bv, bvl); |
120 | return; |
121 | } |
122 | |
123 | /* --- How the algorithm works --- * |
124 | * |
7d5fa32a |
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$% |
a86e33af |
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 |
132 | * that's a win. |
133 | */ |
134 | |
135 | /* --- First things --- * |
136 | * |
137 | * Sort out where to break the factors in half. I'll choose the midpoint |
52cdaca9 |
138 | * of the larger one, since this minimizes the amount of work I have to do |
a86e33af |
139 | * most effectively. |
140 | */ |
141 | |
142 | if (avl - av > bvl - bv) { |
143 | m = (avl - av + 1) >> 1; |
144 | avm = av + m; |
145 | if (bvl - bv > m) |
146 | bvm = bv + m; |
147 | else |
148 | bvm = bvl; |
149 | } else { |
150 | m = (bvl - bv + 1) >> 1; |
151 | bvm = bv + m; |
152 | if (avl - av > m) |
153 | avm = av + m; |
154 | else |
155 | avm = avl; |
156 | } |
157 | |
4468424e |
158 | /* --- Sort out the middle term --- */ |
a86e33af |
159 | |
160 | { |
4468424e |
161 | mpw *bsv = sv + m + 1, *ssv = bsv + m + 1; |
162 | mpw *rdv = dv + m, *rdvl = rdv + 2 * (m + 2); |
163 | |
432c4e18 |
164 | assert(rdvl <= dvl); |
165 | assert(ssv <= svl); |
4468424e |
166 | UADD2(sv, bsv, av, avm, avm, avl); |
167 | UADD2(bsv, ssv, bv, bvm, bvm, bvl); |
52cdaca9 |
168 | if (m > MPK_THRESH) |
a86e33af |
169 | mpx_kmul(rdv, rdvl, sv, bsv, bsv, ssv, ssv, svl); |
170 | else |
171 | mpx_umul(rdv, rdvl, sv, bsv, bsv, ssv); |
a86e33af |
172 | } |
173 | |
174 | /* --- Sort out the other two terms --- */ |
175 | |
176 | { |
4468424e |
177 | mpw *svm = sv + m, *svn = svm + m, *ssv = svn + 4; |
a86e33af |
178 | mpw *tdv = dv + m; |
179 | mpw *rdv = tdv + m; |
180 | |
4468424e |
181 | if (avl == avm || bvl == bvm) |
182 | MPX_ZERO(rdv + m + 1, dvl); |
183 | else { |
52cdaca9 |
184 | if (m > MPK_THRESH) |
4468424e |
185 | mpx_kmul(sv, ssv, avm, avl, bvm, bvl, ssv, svl); |
186 | else |
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); |
190 | USUB(tdv, sv, svn); |
191 | } |
192 | |
52cdaca9 |
193 | if (m > MPK_THRESH) |
a86e33af |
194 | mpx_kmul(sv, ssv, av, avm, bv, bvm, ssv, svl); |
195 | else |
196 | mpx_umul(sv, ssv, av, avm, bv, bvm); |
4468424e |
197 | MPX_COPY(dv, tdv, sv, svm); |
198 | USUB(tdv, sv, svn); |
199 | UADD(tdv, svm, svn); |
a86e33af |
200 | } |
201 | } |
202 | |
203 | /*----- Test rig ----------------------------------------------------------*/ |
204 | |
205 | #ifdef TEST_RIG |
206 | |
207 | #include <mLib/alloc.h> |
208 | #include <mLib/testrig.h> |
209 | |
a86e33af |
210 | #define ALLOC(v, vl, sz) do { \ |
211 | size_t _sz = (sz); \ |
212 | mpw *_vv = xmalloc(MPWS(_sz)); \ |
213 | mpw *_vvl = _vv + _sz; \ |
214 | (v) = _vv; \ |
215 | (vl) = _vvl; \ |
216 | } while (0) |
217 | |
218 | #define LOAD(v, vl, d) do { \ |
219 | const dstr *_d = (d); \ |
220 | mpw *_v, *_vl; \ |
221 | ALLOC(_v, _vl, MPW_RQ(_d->len)); \ |
222 | mpx_loadb(_v, _vl, _d->buf, _d->len); \ |
223 | (v) = _v; \ |
224 | (vl) = _vl; \ |
225 | } while (0) |
226 | |
227 | #define MAX(x, y) ((x) > (y) ? (x) : (y)) |
228 | |
229 | static void dumpmp(const char *msg, const mpw *v, const mpw *vl) |
230 | { |
231 | fputs(msg, stderr); |
232 | MPX_SHRINK(v, vl); |
233 | while (v < vl) |
234 | fprintf(stderr, " %08lx", (unsigned long)*--vl); |
235 | fputc('\n', stderr); |
236 | } |
237 | |
238 | static int umul(dstr *v) |
239 | { |
240 | mpw *a, *al; |
241 | mpw *b, *bl; |
242 | mpw *c, *cl; |
243 | mpw *d, *dl; |
244 | mpw *s, *sl; |
245 | size_t m; |
246 | int ok = 1; |
247 | |
248 | LOAD(a, al, &v[0]); |
249 | LOAD(b, bl, &v[1]); |
250 | LOAD(c, cl, &v[2]); |
251 | m = MAX(al - a, bl - b) + 1; |
dd22938e |
252 | ALLOC(d, dl, 3 * m); |
253 | ALLOC(s, sl, 5 * m); |
a86e33af |
254 | |
255 | mpx_kmul(d, dl, a, al, b, bl, s, sl); |
c9060100 |
256 | if (!mpx_ueq(d, dl, c, cl)) { |
a86e33af |
257 | fprintf(stderr, "\n*** umul failed\n"); |
258 | dumpmp(" a", a, al); |
259 | dumpmp(" b", b, bl); |
260 | dumpmp("expected", c, cl); |
261 | dumpmp(" result", d, dl); |
262 | ok = 0; |
263 | } |
264 | |
265 | free(a); free(b); free(c); free(d); free(s); |
266 | return (ok); |
267 | } |
268 | |
269 | static test_chunk defs[] = { |
270 | { "umul", umul, { &type_hex, &type_hex, &type_hex, 0 } }, |
271 | { 0, 0, { 0 } } |
272 | }; |
273 | |
274 | int main(int argc, char *argv[]) |
275 | { |
276 | test_run(argc, argv, defs, SRCDIR"/tests/mpx"); |
277 | return (0); |
278 | } |
279 | |
280 | #endif |
281 | |
282 | /*----- That's all, folks -------------------------------------------------*/ |