chiark / gitweb /
Add some more vectors, and a whinge about how Skipjack test vectors are.
[catacomb] / mpx-kmul.c
1 /* -*-c-*-
2  *
3  * $Id: mpx-kmul.c,v 1.5 2000/07/29 17:04:02 mdw Exp $
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 $
33  * Revision 1.5  2000/07/29 17:04:02  mdw
34  * Remove useless header `mpscan.h'.
35  *
36  * Revision 1.4  2000/06/17 11:42:11  mdw
37  * Moved the Karatsuba macros into a separate file for better sharing.
38  * Fixed some comments.
39  *
40  * Revision 1.3  1999/12/13 15:35:01  mdw
41  * Simplify and improve.
42  *
43  * Revision 1.2  1999/12/11 10:58:02  mdw
44  * Remove tweakable comments.
45  *
46  * Revision 1.1  1999/12/10 23:23:51  mdw
47  * Karatsuba-Ofman multiplication algorithm.
48  *
49  */
50
51 /*----- Header files ------------------------------------------------------*/
52
53 #include <assert.h>
54 #include <stdio.h>
55
56 #include "mpx.h"
57 #include "mpx-kmac.h"
58
59 /*----- Tweakables --------------------------------------------------------*/
60
61 #ifdef TEST_RIG
62 #  undef KARATSUBA_CUTOFF
63 #  define KARATSUBA_CUTOFF 2
64 #endif
65
66 /*----- Main code ---------------------------------------------------------*/
67
68 /* --- @mpx_kmul@ --- *
69  *
70  * Arguments:   @mpw *dv, *dvl@ = pointer to destination buffer
71  *              @const mpw *av, *avl@ = pointer to first argument
72  *              @const mpw *bv, *bvl@ = pointer to second argument
73  *              @mpw *sv, *svl@ = pointer to scratch workspace
74  *
75  * Returns:     ---
76  *
77  * Use:         Multiplies two multiprecision integers using Karatsuba's
78  *              algorithm.  This is rather faster than traditional long
79  *              multiplication (e.g., @mpx_umul@) on large numbers, although
80  *              more expensive on small ones.
81  *
82  *              The destination must be twice as large as the larger
83  *              argument.  The scratch space must be twice as large as the
84  *              larger argument, plus the magic number @KARATSUBA_SLOP@.
85  */
86
87 void mpx_kmul(mpw *dv, mpw *dvl,
88               const mpw *av, const mpw *avl,
89               const mpw *bv, const mpw *bvl,
90               mpw *sv, mpw *svl)
91 {
92   const mpw *avm, *bvm;
93   size_t m;
94
95   /* --- Dispose of easy cases to @mpx_umul@ --- *
96    *
97    * Karatsuba is only a win on large numbers, because of all the
98    * recursiveness and bookkeeping.  The recursive calls make a quick check
99    * to see whether to bottom out to @mpx_umul@ which should help quite a
100    * lot, but sometimes the only way to know is to make sure...
101    */
102
103   MPX_SHRINK(av, avl);
104   MPX_SHRINK(bv, bvl);
105
106   if (avl - av <= KARATSUBA_CUTOFF || bvl - bv <= KARATSUBA_CUTOFF) {
107     mpx_umul(dv, dvl, av, avl, bv, bvl);
108     return;
109   }
110
111   /* --- How the algorithm works --- *
112    *
113    * Let %$A = xb + y$% and %$B = ub + v$%.  Then, simply by expanding,
114    * %$AB = x u b^2 + b(x v + y u) + y v$%.  That's not helped any, because
115    * I've got four multiplications, each four times easier than the one I
116    * started with.  However, note that I can rewrite the coefficient of %$b$%
117    * as %$xv + yu = (x + y)(u + v) - xu - yv$%.  The terms %$xu$% and %$yv$%
118    * I've already calculated, and that leaves only one more multiplication to
119    * do.  So now I have three multiplications, each four times easier, and
120    * that's a win.
121    */
122
123   /* --- First things --- *
124    *
125    * Sort out where to break the factors in half.  I'll choose the midpoint
126    * of the largest one, since this minimizes the amount of work I have to do
127    * most effectively.
128    */
129
130   if (avl - av > bvl - bv) {
131     m = (avl - av + 1) >> 1;
132     avm = av + m;
133     if (bvl - bv > m)
134       bvm = bv + m;
135     else
136       bvm = bvl;
137   } else {
138     m = (bvl - bv + 1) >> 1;
139     bvm = bv + m;
140     if (avl - av > m)
141       avm = av + m;
142     else
143       avm = avl;
144   }
145
146   assert(((void)"Destination too small for Karatsuba multiply",
147           dvl - dv >= 4 * m));
148   assert(((void)"Not enough workspace for Karatsuba multiply",
149           svl - sv >= 4 * m));
150
151   /* --- Sort out the middle term --- */
152
153   {
154     mpw *bsv = sv + m + 1, *ssv = bsv + m + 1;
155     mpw *rdv = dv + m, *rdvl = rdv + 2 * (m + 2);
156
157     UADD2(sv, bsv, av, avm, avm, avl);
158     UADD2(bsv, ssv, bv, bvm, bvm, bvl);
159     if (m > KARATSUBA_CUTOFF)
160       mpx_kmul(rdv, rdvl, sv, bsv, bsv, ssv, ssv, svl);
161     else
162       mpx_umul(rdv, rdvl, sv, bsv, bsv, ssv);
163   }
164
165   /* --- Sort out the other two terms --- */
166
167   {
168     mpw *svm = sv + m, *svn = svm + m, *ssv = svn + 4;
169     mpw *tdv = dv + m;
170     mpw *rdv = tdv + m;
171
172     if (avl == avm || bvl == bvm)
173       MPX_ZERO(rdv + m + 1, dvl);
174     else {
175       if (m > KARATSUBA_CUTOFF)
176         mpx_kmul(sv, ssv, avm, avl, bvm, bvl, ssv, svl);
177       else
178         mpx_umul(sv, ssv, avm, avl, bvm, bvl);
179       MPX_COPY(rdv + m + 1, dvl, svm + 1, svn);
180       UADD(rdv, sv, svm + 1);
181       USUB(tdv, sv, svn);
182     }
183
184     if (m > KARATSUBA_CUTOFF)
185       mpx_kmul(sv, ssv, av, avm, bv, bvm, ssv, svl);
186     else
187       mpx_umul(sv, ssv, av, avm, bv, bvm);
188     MPX_COPY(dv, tdv, sv, svm);
189     USUB(tdv, sv, svn);
190     UADD(tdv, svm, svn);
191   }
192 }
193
194 /*----- Test rig ----------------------------------------------------------*/
195
196 #ifdef TEST_RIG
197
198 #include <mLib/alloc.h>
199 #include <mLib/testrig.h>
200
201 #define ALLOC(v, vl, sz) do {                                           \
202   size_t _sz = (sz);                                                    \
203   mpw *_vv = xmalloc(MPWS(_sz));                                        \
204   mpw *_vvl = _vv + _sz;                                                \
205   (v) = _vv;                                                            \
206   (vl) = _vvl;                                                          \
207 } while (0)
208
209 #define LOAD(v, vl, d) do {                                             \
210   const dstr *_d = (d);                                                 \
211   mpw *_v, *_vl;                                                        \
212   ALLOC(_v, _vl, MPW_RQ(_d->len));                                      \
213   mpx_loadb(_v, _vl, _d->buf, _d->len);                                 \
214   (v) = _v;                                                             \
215   (vl) = _vl;                                                           \
216 } while (0)
217
218 #define MAX(x, y) ((x) > (y) ? (x) : (y))
219
220 static void dumpmp(const char *msg, const mpw *v, const mpw *vl)
221 {
222   fputs(msg, stderr);
223   MPX_SHRINK(v, vl);
224   while (v < vl)
225     fprintf(stderr, " %08lx", (unsigned long)*--vl);
226   fputc('\n', stderr);
227 }
228
229 static int umul(dstr *v)
230 {
231   mpw *a, *al;
232   mpw *b, *bl;
233   mpw *c, *cl;
234   mpw *d, *dl;
235   mpw *s, *sl;
236   size_t m;
237   int ok = 1;
238
239   LOAD(a, al, &v[0]);
240   LOAD(b, bl, &v[1]);
241   LOAD(c, cl, &v[2]);
242   m = MAX(al - a, bl - b) + 1;
243   ALLOC(d, dl, 2 * m);
244   ALLOC(s, sl, 2 * m + 32);
245
246   mpx_kmul(d, dl, a, al, b, bl, s, sl);
247   if (MPX_UCMP(d, dl, !=, c, cl)) {
248     fprintf(stderr, "\n*** umul failed\n");
249     dumpmp("       a", a, al);
250     dumpmp("       b", b, bl);
251     dumpmp("expected", c, cl);
252     dumpmp("  result", d, dl);
253     ok = 0;
254   }
255
256   free(a); free(b); free(c); free(d); free(s);
257   return (ok);
258 }
259
260 static test_chunk defs[] = {
261   { "umul", umul, { &type_hex, &type_hex, &type_hex, 0 } },
262   { 0, 0, { 0 } }
263 };
264
265 int main(int argc, char *argv[])
266 {
267   test_run(argc, argv, defs, SRCDIR"/tests/mpx");
268   return (0);
269 }
270
271 #endif
272
273 /*----- That's all, folks -------------------------------------------------*/