3 * $Id: mpx-ksqr.c,v 1.1 1999/12/11 10:57:43 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.1 1999/12/11 10:57:43 mdw
34 * Karatsuba squaring algorithm.
38 /*----- Header files ------------------------------------------------------*/
44 /*----- Tweakables --------------------------------------------------------*/
47 # undef KARATSUBA_CUTOFF
48 # define KARATSUBA_CUTOFF 2
51 /*----- Addition macros ---------------------------------------------------*/
53 #define ULSL1(dv, av, avl) do { \
55 const mpw *_av = (av), *_avl = (avl); \
58 while (_av < _avl) { \
60 *_dv++ = MPW(_c | (_x << 1)); \
61 _c = MPW(_x >> (MPW_BITS - 1)); \
66 #define UADD(dv, av, avl) do { \
68 const mpw *_av = (av), *_avl = (avl); \
71 while (_av < _avl) { \
76 _x = (mpd)_a + (mpd)_b + _c; \
78 _c = _x >> MPW_BITS; \
81 mpd _x = (mpd)*_dv + (mpd)_c; \
83 _c = _x >> MPW_BITS; \
87 /*----- Main code ---------------------------------------------------------*/
89 /* --- @mpx_ksqr@ --- *
91 * Arguments: @mpw *dv, *dvl@ = pointer to destination buffer
92 * @const mpw *av, *avl@ = pointer to first argument
93 * @mpw *sv, *svl@ = pointer to scratch workspace
97 * Use: Squares a multiprecision integers using something similar to
98 * Karatsuba's multiplication algorithm. This is rather faster
99 * than traditional long multiplication (e.g., @mpx_umul@) on
100 * large numbers, although more expensive on small ones, and
101 * rather simpler than full-blown Karatsuba multiplication.
103 * The destination must be twice as large as the argument. The
104 * scratch space must be twice as large as the argument, plus
105 * the magic number @KARATSUBA_SLOP@.
108 void mpx_ksqr(mpw
*dv
, mpw
*dvl
,
109 const mpw
*av
, const mpw
*avl
,
115 /* --- Dispose of easy cases to @mpx_usqr@ --- *
117 * Karatsuba is only a win on large numbers, because of all the
118 * recursiveness and bookkeeping. The recursive calls make a quick check
119 * to see whether to bottom out to @mpx_usqr@ which should help quite a
120 * lot, but sometimes the only way to know is to make sure...
125 if (avl
- av
<= KARATSUBA_CUTOFF
) {
126 mpx_usqr(dv
, dvl
, av
, avl
);
130 /* --- How the algorithm works --- *
132 * Unlike Karatsuba's identity for multiplication which isn't particularly
133 * obvious, the identity for multiplication is known to all schoolchildren.
134 * Let %$A = xb + y$%. Then %$A^2 = x^2 b^x + 2 x y b + y^2$%. So now I
135 * have three multiplications, each four times easier, and that's a win.
138 /* --- First things --- *
140 * Sort out where to break the factor in half.
143 m
= (avl
- av
+ 1) >> 1;
146 /* --- Sort out everything --- */
149 mpw
*ssv
= sv
+ 2 * m
;
153 /* --- The cross term in the middle needs a multiply --- *
155 * This isn't actually true, since %$x y = ((x + y)^2 - (x - y)^2)/4%.
156 * But that's two squarings, versus one multiplication.
159 if (m
> KARATSUBA_CUTOFF
)
160 mpx_kmul(sv
, ssv
, av
, avm
, avm
, avl
, ssv
, svl
);
162 mpx_umul(sv
, ssv
, av
, avm
, avm
, avl
);
165 MPX_ZERO(rdv
+ m
+ 1, dvl
);
167 if (m
> KARATSUBA_CUTOFF
)
168 mpx_ksqr(sv
, ssv
, avm
, avl
, ssv
, svl
);
170 mpx_usqr(sv
, ssv
, avm
, avl
);
173 if (m
> KARATSUBA_CUTOFF
)
174 mpx_ksqr(sv
, ssv
, av
, avm
, ssv
, svl
);
176 mpx_usqr(sv
, ssv
, av
, avm
);
181 /*----- Test rig ----------------------------------------------------------*/
185 #include <mLib/alloc.h>
186 #include <mLib/testrig.h>
190 #define ALLOC(v, vl, sz) do { \
192 mpw *_vv = xmalloc(MPWS(_sz)); \
193 mpw *_vvl = _vv + _sz; \
198 #define LOAD(v, vl, d) do { \
199 const dstr *_d = (d); \
201 ALLOC(_v, _vl, MPW_RQ(_d->len)); \
202 mpx_loadb(_v, _vl, _d->buf, _d->len); \
207 #define MAX(x, y) ((x) > (y) ? (x) : (y))
209 static void dumpmp(const char *msg
, const mpw
*v
, const mpw
*vl
)
214 fprintf(stderr
, " %08lx", (unsigned long)*--vl
);
218 static int usqr(dstr
*v
)
231 ALLOC(s
, sl
, 2 * m
+ 32);
233 mpx_ksqr(d
, dl
, a
, al
, s
, sl
);
234 if (MPX_UCMP(d
, dl
, !=, c
, cl
)) {
235 fprintf(stderr
, "\n*** usqr failed\n");
237 dumpmp("expected", c
, cl
);
238 dumpmp(" result", d
, dl
);
242 free(a
); free(c
); free(d
); free(s
);
246 static test_chunk defs
[] = {
247 { "usqr", usqr
, { &type_hex
, &type_hex
, 0 } },
251 int main(int argc
, char *argv
[])
253 test_run(argc
, argv
, defs
, SRCDIR
"/tests/mpx");
259 /*----- That's all, folks -------------------------------------------------*/