Use @mpx_ueq@ instead of @MPX_UCMP@.
[u/mdw/catacomb] / mpx-ksqr.c
1 /* -*-c-*-
2 *
3 * $Id: mpx-ksqr.c,v 1.5 2000/10/08 12:11:01 mdw Exp $
4 *
5 * Karatsuba-based squaring 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-ksqr.c,v $
33 * Revision 1.5 2000/10/08 12:11:01 mdw
34 * Use @mpx_ueq@ instead of @MPX_UCMP@.
35 *
36 * Revision 1.4 2000/07/29 17:04:02 mdw
37 * Remove useless header `mpscan.h'.
38 *
39 * Revision 1.3 2000/06/17 11:42:54 mdw
40 * Moved the Karatsuba macros into a separate file for better sharing.
41 * Fixed some comments. Use an improved technique so that all the
42 * operations are squarings.
43 *
44 * Revision 1.2 1999/12/13 15:35:01 mdw
45 * Simplify and improve.
46 *
47 * Revision 1.1 1999/12/11 10:57:43 mdw
48 * Karatsuba squaring algorithm.
49 *
50 */
51
52 /*----- Header files ------------------------------------------------------*/
53
54 #include <assert.h>
55 #include <stdio.h>
56
57 #include "mpx.h"
58 #include "mpx-kmac.h"
59
60 /*----- Tweakables --------------------------------------------------------*/
61
62 #ifdef TEST_RIG
63 # undef KARATSUBA_CUTOFF
64 # define KARATSUBA_CUTOFF 2
65 #endif
66
67 /*----- Main code ---------------------------------------------------------*/
68
69 /* --- @mpx_ksqr@ --- *
70 *
71 * Arguments: @mpw *dv, *dvl@ = pointer to destination buffer
72 * @const mpw *av, *avl@ = pointer to first argument
73 * @mpw *sv, *svl@ = pointer to scratch workspace
74 *
75 * Returns: ---
76 *
77 * Use: Squares a multiprecision integers using something similar to
78 * Karatsuba's multiplication algorithm. This is rather faster
79 * than traditional long multiplication (e.g., @mpx_umul@) on
80 * large numbers, although more expensive on small ones, and
81 * rather simpler than full-blown Karatsuba multiplication.
82 *
83 * The destination must be twice as large as the argument. The
84 * scratch space must be twice as large as the argument, plus
85 * the magic number @KARATSUBA_SLOP@.
86 */
87
88 void mpx_ksqr(mpw *dv, mpw *dvl,
89 const mpw *av, const mpw *avl,
90 mpw *sv, mpw *svl)
91 {
92 const mpw *avm;
93 size_t m;
94
95 /* --- Dispose of easy cases to @mpx_usqr@ --- *
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_usqr@ 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
105 if (avl - av <= KARATSUBA_CUTOFF) {
106 mpx_usqr(dv, dvl, av, avl);
107 return;
108 }
109
110 /* --- How the algorithm works --- *
111 *
112 * The identity for squaring is known to all schoolchildren.
113 * Let %$A = xb + y$%. Then %$A^2 = x^2 b^2 + 2 x y b + y^2$%. Now,
114 * %$(x + y)^2 - x^2 - y^2 = 2 x y$%, which means I only need to do three
115 * squarings.
116 */
117
118 /* --- First things --- *
119 *
120 * Sort out where to break the factor in half.
121 */
122
123 m = (avl - av + 1) >> 1;
124 avm = av + m;
125
126 assert(((void)"Destination too small for Karatsuba square",
127 dvl - dv >= 4 * m));
128 assert(((void)"Not enough workspace for Karatsuba square",
129 svl - sv >= 4 * m));
130
131 /* --- Sort out everything --- */
132
133 {
134 mpw *svm = sv + m, *svn = svm + m, *ssv = svn + 4;
135 mpw *tdv = dv + m;
136 mpw *rdv = tdv + m;
137
138 UADD2(sv, svm, av, avm, avm, avl);
139 if (m > KARATSUBA_CUTOFF)
140 mpx_ksqr(tdv, rdv + m + 4, sv, svm + 1, ssv, svl);
141 else
142 mpx_usqr(tdv, rdv + m + 4, sv, svm + 1);
143
144 if (m > KARATSUBA_CUTOFF)
145 mpx_ksqr(sv, ssv, avm, avl, ssv, svl);
146 else
147 mpx_usqr(sv, ssv, avm, avl);
148 MPX_COPY(rdv + m + 1, dvl, svm + 1, svn);
149 UADD(rdv, sv, svm + 1);
150 USUB(tdv, sv, svn);
151
152 if (m > KARATSUBA_CUTOFF)
153 mpx_ksqr(sv, ssv, av, avm, ssv, svl);
154 else
155 mpx_usqr(sv, ssv, av, avm);
156 MPX_COPY(dv, tdv, sv, svm);
157 UADD(tdv, svm, svn);
158 USUB(tdv, sv, svn);
159 }
160 }
161
162 /*----- Test rig ----------------------------------------------------------*/
163
164 #ifdef TEST_RIG
165
166 #include <mLib/alloc.h>
167 #include <mLib/testrig.h>
168
169 #define ALLOC(v, vl, sz) do { \
170 size_t _sz = (sz); \
171 mpw *_vv = xmalloc(MPWS(_sz)); \
172 mpw *_vvl = _vv + _sz; \
173 (v) = _vv; \
174 (vl) = _vvl; \
175 } while (0)
176
177 #define LOAD(v, vl, d) do { \
178 const dstr *_d = (d); \
179 mpw *_v, *_vl; \
180 ALLOC(_v, _vl, MPW_RQ(_d->len)); \
181 mpx_loadb(_v, _vl, _d->buf, _d->len); \
182 (v) = _v; \
183 (vl) = _vl; \
184 } while (0)
185
186 #define MAX(x, y) ((x) > (y) ? (x) : (y))
187
188 static void dumpmp(const char *msg, const mpw *v, const mpw *vl)
189 {
190 fputs(msg, stderr);
191 MPX_SHRINK(v, vl);
192 while (v < vl)
193 fprintf(stderr, " %08lx", (unsigned long)*--vl);
194 fputc('\n', stderr);
195 }
196
197 static int usqr(dstr *v)
198 {
199 mpw *a, *al;
200 mpw *c, *cl;
201 mpw *d, *dl;
202 mpw *s, *sl;
203 size_t m;
204 int ok = 1;
205
206 LOAD(a, al, &v[0]);
207 LOAD(c, cl, &v[1]);
208 m = al - a + 1;
209 ALLOC(d, dl, 2 * m);
210 ALLOC(s, sl, 2 * m + 32);
211
212 mpx_ksqr(d, dl, a, al, s, sl);
213 if (!mpx_ueq(d, dl, c, cl)) {
214 fprintf(stderr, "\n*** usqr failed\n");
215 dumpmp(" a", a, al);
216 dumpmp("expected", c, cl);
217 dumpmp(" result", d, dl);
218 ok = 0;
219 }
220
221 free(a); free(c); free(d); free(s);
222 return (ok);
223 }
224
225 static test_chunk defs[] = {
226 { "usqr", usqr, { &type_hex, &type_hex, 0 } },
227 { 0, 0, { 0 } }
228 };
229
230 int main(int argc, char *argv[])
231 {
232 test_run(argc, argv, defs, SRCDIR"/tests/mpx");
233 return (0);
234 }
235
236 #endif
237
238 /*----- That's all, folks -------------------------------------------------*/