3 * $Id: mpx-ksqr.c,v 1.7 2002/10/09 00:36:03 mdw Exp $
5 * Karatsuba-based squaring 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-ksqr.c,v $
33 * Revision 1.7 2002/10/09 00:36:03 mdw
34 * Fix bounds on workspace for Karatsuba operations.
36 * Revision 1.6 2000/10/08 15:48:35 mdw
37 * Rename Karatsuba constants now that we have @gfx_kmul@ too.
39 * Revision 1.5 2000/10/08 12:11:01 mdw
40 * Use @mpx_ueq@ instead of @MPX_UCMP@.
42 * Revision 1.4 2000/07/29 17:04:02 mdw
43 * Remove useless header `mpscan.h'.
45 * Revision 1.3 2000/06/17 11:42:54 mdw
46 * Moved the Karatsuba macros into a separate file for better sharing.
47 * Fixed some comments. Use an improved technique so that all the
48 * operations are squarings.
50 * Revision 1.2 1999/12/13 15:35:01 mdw
51 * Simplify and improve.
53 * Revision 1.1 1999/12/11 10:57:43 mdw
54 * Karatsuba squaring algorithm.
58 /*----- Header files ------------------------------------------------------*/
64 #include "karatsuba.h"
66 /*----- Tweakables --------------------------------------------------------*/
73 /*----- Main code ---------------------------------------------------------*/
75 /* --- @mpx_ksqr@ --- *
77 * Arguments: @mpw *dv, *dvl@ = pointer to destination buffer
78 * @const mpw *av, *avl@ = pointer to first argument
79 * @mpw *sv, *svl@ = pointer to scratch workspace
83 * Use: Squares a multiprecision integers using something similar to
84 * Karatsuba's multiplication algorithm. This is rather faster
85 * than traditional long multiplication (e.g., @mpx_umul@) on
86 * large numbers, although more expensive on small ones, and
87 * rather simpler than full-blown Karatsuba multiplication.
89 * The destination must be three times as large as the larger
90 * argument. The scratch space must be five times as large as
91 * the larger argument.
94 void mpx_ksqr(mpw *dv, mpw *dvl,
95 const mpw *av, const mpw *avl,
101 /* --- Dispose of easy cases to @mpx_usqr@ --- *
103 * Karatsuba is only a win on large numbers, because of all the
104 * recursiveness and bookkeeping. The recursive calls make a quick check
105 * to see whether to bottom out to @mpx_usqr@ which should help quite a
106 * lot, but sometimes the only way to know is to make sure...
111 if (avl - av <= MPK_THRESH) {
112 mpx_usqr(dv, dvl, av, avl);
116 /* --- How the algorithm works --- *
118 * The identity for squaring is known to all schoolchildren.
119 * Let %$A = xb + y$%. Then %$A^2 = x^2 b^2 + 2 x y b + y^2$%. Now,
120 * %$(x + y)^2 - x^2 - y^2 = 2 x y$%, which means I only need to do three
124 /* --- First things --- *
126 * Sort out where to break the factor in half.
129 m = (avl - av + 1) >> 1;
132 /* --- Sort out everything --- */
135 mpw *svm = sv + m, *svn = svm + m, *ssv = svn + 4;
139 assert(rdv + m + 4 < dvl);
141 UADD2(sv, svm, av, avm, avm, avl);
143 mpx_ksqr(tdv, rdv + m + 4, sv, svm + 1, ssv, svl);
145 mpx_usqr(tdv, rdv + m + 4, sv, svm + 1);
148 mpx_ksqr(sv, ssv, avm, avl, ssv, svl);
150 mpx_usqr(sv, ssv, avm, avl);
151 MPX_COPY(rdv + m + 1, dvl, svm + 1, svn);
152 UADD(rdv, sv, svm + 1);
156 mpx_ksqr(sv, ssv, av, avm, ssv, svl);
158 mpx_usqr(sv, ssv, av, avm);
159 MPX_COPY(dv, tdv, sv, svm);
165 /*----- Test rig ----------------------------------------------------------*/
169 #include <mLib/alloc.h>
170 #include <mLib/testrig.h>
172 #define ALLOC(v, vl, sz) do { \
174 mpw *_vv = xmalloc(MPWS(_sz)); \
175 mpw *_vvl = _vv + _sz; \
180 #define LOAD(v, vl, d) do { \
181 const dstr *_d = (d); \
183 ALLOC(_v, _vl, MPW_RQ(_d->len)); \
184 mpx_loadb(_v, _vl, _d->buf, _d->len); \
189 #define MAX(x, y) ((x) > (y) ? (x) : (y))
191 static void dumpmp(const char *msg, const mpw *v, const mpw *vl)
196 fprintf(stderr, " %08lx", (unsigned long)*--vl);
200 static int usqr(dstr *v)
215 mpx_ksqr(d, dl, a, al, s, sl);
216 if (!mpx_ueq(d, dl, c, cl)) {
217 fprintf(stderr, "\n*** usqr failed\n");
219 dumpmp("expected", c, cl);
220 dumpmp(" result", d, dl);
224 free(a); free(c); free(d); free(s);
228 static test_chunk defs[] = {
229 { "usqr", usqr, { &type_hex, &type_hex, 0 } },
233 int main(int argc, char *argv[])
235 test_run(argc, argv, defs, SRCDIR"/tests/mpx");
241 /*----- That's all, folks -------------------------------------------------*/