3 * $Id: mpx-ksqr.c,v 1.2 1999/12/13 15:35:01 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.2 1999/12/13 15:35:01 mdw
34 * Simplify and improve.
36 * Revision 1.1 1999/12/11 10:57:43 mdw
37 * Karatsuba squaring algorithm.
41 /*----- Header files ------------------------------------------------------*/
48 /*----- Tweakables --------------------------------------------------------*/
51 # undef KARATSUBA_CUTOFF
52 # define KARATSUBA_CUTOFF 2
55 /*----- Addition macros ---------------------------------------------------*/
57 #define ULSL1(dv, av, avl) do { \
59 const mpw *_av = (av), *_avl = (avl); \
62 while (_av < _avl) { \
64 *_dv++ = MPW(_c | (_x << 1)); \
65 _c = MPW(_x >> (MPW_BITS - 1)); \
70 #define UADD(dv, av, avl) do { \
72 const mpw *_av = (av), *_avl = (avl); \
75 while (_av < _avl) { \
80 _x = (mpd)_a + (mpd)_b + _c; \
82 _c = _x >> MPW_BITS; \
85 mpd _x = (mpd)*_dv + (mpd)_c; \
87 _c = _x >> MPW_BITS; \
91 /*----- Main code ---------------------------------------------------------*/
93 /* --- @mpx_ksqr@ --- *
95 * Arguments: @mpw *dv, *dvl@ = pointer to destination buffer
96 * @const mpw *av, *avl@ = pointer to first argument
97 * @mpw *sv, *svl@ = pointer to scratch workspace
101 * Use: Squares a multiprecision integers using something similar to
102 * Karatsuba's multiplication algorithm. This is rather faster
103 * than traditional long multiplication (e.g., @mpx_umul@) on
104 * large numbers, although more expensive on small ones, and
105 * rather simpler than full-blown Karatsuba multiplication.
107 * The destination must be twice as large as the argument. The
108 * scratch space must be twice as large as the argument, plus
109 * the magic number @KARATSUBA_SLOP@.
112 void mpx_ksqr(mpw
*dv
, mpw
*dvl
,
113 const mpw
*av
, const mpw
*avl
,
119 /* --- Dispose of easy cases to @mpx_usqr@ --- *
121 * Karatsuba is only a win on large numbers, because of all the
122 * recursiveness and bookkeeping. The recursive calls make a quick check
123 * to see whether to bottom out to @mpx_usqr@ which should help quite a
124 * lot, but sometimes the only way to know is to make sure...
129 if (avl
- av
<= KARATSUBA_CUTOFF
) {
130 mpx_usqr(dv
, dvl
, av
, avl
);
134 /* --- How the algorithm works --- *
136 * Unlike Karatsuba's identity for multiplication which isn't particularly
137 * obvious, the identity for multiplication is known to all schoolchildren.
138 * Let %$A = xb + y$%. Then %$A^2 = x^2 b^x + 2 x y b + y^2$%. So now I
139 * have three multiplications, each four times easier, and that's a win.
142 /* --- First things --- *
144 * Sort out where to break the factor in half.
147 m
= (avl
- av
+ 1) >> 1;
150 assert(((void)"Destination too small for Karatsuba square",
152 assert(((void)"Not enough workspace for Karatsuba square",
155 /* --- Sort out everything --- */
158 mpw
*svm
= sv
+ m
, *svn
= svm
+ m
, *ssv
= svn
+ 4;
162 /* --- The cross term in the middle needs a multiply --- *
164 * This isn't actually true, since %$x y = ((x + y)^2 - (x - y)^2)/4%.
165 * But that's two squarings, versus one multiplication.
168 if (m
> KARATSUBA_CUTOFF
)
169 mpx_kmul(sv
, ssv
, av
, avm
, avm
, avl
, ssv
, svl
);
171 mpx_umul(sv
, ssv
, av
, avm
, avm
, avl
);
174 if (m
> KARATSUBA_CUTOFF
)
175 mpx_ksqr(sv
, ssv
, avm
, avl
, ssv
, svl
);
177 mpx_usqr(sv
, ssv
, avm
, avl
);
178 MPX_COPY(rdv
+ m
+ 1, dvl
, svm
+ 1, svn
);
179 UADD(rdv
, sv
, svm
+ 1);
181 if (m
> KARATSUBA_CUTOFF
)
182 mpx_ksqr(sv
, ssv
, av
, avm
, ssv
, svl
);
184 mpx_usqr(sv
, ssv
, av
, avm
);
185 MPX_COPY(dv
, tdv
, sv
, svm
);
190 /*----- Test rig ----------------------------------------------------------*/
194 #include <mLib/alloc.h>
195 #include <mLib/testrig.h>
199 #define ALLOC(v, vl, sz) do { \
201 mpw *_vv = xmalloc(MPWS(_sz)); \
202 mpw *_vvl = _vv + _sz; \
207 #define LOAD(v, vl, d) do { \
208 const dstr *_d = (d); \
210 ALLOC(_v, _vl, MPW_RQ(_d->len)); \
211 mpx_loadb(_v, _vl, _d->buf, _d->len); \
216 #define MAX(x, y) ((x) > (y) ? (x) : (y))
218 static void dumpmp(const char *msg
, const mpw
*v
, const mpw
*vl
)
223 fprintf(stderr
, " %08lx", (unsigned long)*--vl
);
227 static int usqr(dstr
*v
)
240 ALLOC(s
, sl
, 2 * m
+ 32);
242 mpx_ksqr(d
, dl
, a
, al
, s
, sl
);
243 if (MPX_UCMP(d
, dl
, !=, c
, cl
)) {
244 fprintf(stderr
, "\n*** usqr failed\n");
246 dumpmp("expected", c
, cl
);
247 dumpmp(" result", d
, dl
);
251 free(a
); free(c
); free(d
); free(s
);
255 static test_chunk defs
[] = {
256 { "usqr", usqr
, { &type_hex
, &type_hex
, 0 } },
260 int main(int argc
, char *argv
[])
262 test_run(argc
, argv
, defs
, SRCDIR
"/tests/mpx");
268 /*----- That's all, folks -------------------------------------------------*/