3 * $Id: mpx-ksqr.c,v 1.8 2004/04/08 01:36:15 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 /*----- Header files ------------------------------------------------------*/
36 #include "karatsuba.h"
38 /*----- Tweakables --------------------------------------------------------*/
45 /*----- Main code ---------------------------------------------------------*/
47 /* --- @mpx_ksqr@ --- *
49 * Arguments: @mpw *dv, *dvl@ = pointer to destination buffer
50 * @const mpw *av, *avl@ = pointer to first argument
51 * @mpw *sv, *svl@ = pointer to scratch workspace
55 * Use: Squares a multiprecision integers using something similar to
56 * Karatsuba's multiplication algorithm. This is rather faster
57 * than traditional long multiplication (e.g., @mpx_umul@) on
58 * large numbers, although more expensive on small ones, and
59 * rather simpler than full-blown Karatsuba multiplication.
61 * The destination must be three times as large as the larger
62 * argument. The scratch space must be five times as large as
63 * the larger argument.
66 void mpx_ksqr(mpw *dv, mpw *dvl,
67 const mpw *av, const mpw *avl,
73 /* --- Dispose of easy cases to @mpx_usqr@ --- *
75 * Karatsuba is only a win on large numbers, because of all the
76 * recursiveness and bookkeeping. The recursive calls make a quick check
77 * to see whether to bottom out to @mpx_usqr@ which should help quite a
78 * lot, but sometimes the only way to know is to make sure...
83 if (avl - av <= MPK_THRESH) {
84 mpx_usqr(dv, dvl, av, avl);
88 /* --- How the algorithm works --- *
90 * The identity for squaring is known to all schoolchildren.
91 * Let %$A = xb + y$%. Then %$A^2 = x^2 b^2 + 2 x y b + y^2$%. Now,
92 * %$(x + y)^2 - x^2 - y^2 = 2 x y$%, which means I only need to do three
96 /* --- First things --- *
98 * Sort out where to break the factor in half.
101 m = (avl - av + 1) >> 1;
104 /* --- Sort out everything --- */
107 mpw *svm = sv + m, *svn = svm + m, *ssv = svn + 4;
111 assert(rdv + m + 4 < dvl);
113 UADD2(sv, svm, av, avm, avm, avl);
115 mpx_ksqr(tdv, rdv + m + 4, sv, svm + 1, ssv, svl);
117 mpx_usqr(tdv, rdv + m + 4, sv, svm + 1);
120 mpx_ksqr(sv, ssv, avm, avl, ssv, svl);
122 mpx_usqr(sv, ssv, avm, avl);
123 MPX_COPY(rdv + m + 1, dvl, svm + 1, svn);
124 UADD(rdv, sv, svm + 1);
128 mpx_ksqr(sv, ssv, av, avm, ssv, svl);
130 mpx_usqr(sv, ssv, av, avm);
131 MPX_COPY(dv, tdv, sv, svm);
137 /*----- Test rig ----------------------------------------------------------*/
141 #include <mLib/alloc.h>
142 #include <mLib/testrig.h>
144 #define ALLOC(v, vl, sz) do { \
146 mpw *_vv = xmalloc(MPWS(_sz)); \
147 mpw *_vvl = _vv + _sz; \
152 #define LOAD(v, vl, d) do { \
153 const dstr *_d = (d); \
155 ALLOC(_v, _vl, MPW_RQ(_d->len)); \
156 mpx_loadb(_v, _vl, _d->buf, _d->len); \
161 #define MAX(x, y) ((x) > (y) ? (x) : (y))
163 static void dumpmp(const char *msg, const mpw *v, const mpw *vl)
168 fprintf(stderr, " %08lx", (unsigned long)*--vl);
172 static int usqr(dstr *v)
187 mpx_ksqr(d, dl, a, al, s, sl);
188 if (!mpx_ueq(d, dl, c, cl)) {
189 fprintf(stderr, "\n*** usqr failed\n");
191 dumpmp("expected", c, cl);
192 dumpmp(" result", d, dl);
196 free(a); free(c); free(d); free(s);
200 static test_chunk defs[] = {
201 { "usqr", usqr, { &type_hex, &type_hex, 0 } },
205 int main(int argc, char *argv[])
207 test_run(argc, argv, defs, SRCDIR"/tests/mpx");
213 /*----- That's all, folks -------------------------------------------------*/