configure.ac: Replace with a new version.
[u/mdw/catacomb] / mpx-ksqr.c
1 /* -*-c-*-
2 *
3 * $Id$
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 /*----- Header files ------------------------------------------------------*/
31
32 #include <assert.h>
33 #include <stdio.h>
34
35 #include "mpx.h"
36 #include "karatsuba.h"
37
38 /*----- Tweakables --------------------------------------------------------*/
39
40 #ifdef TEST_RIG
41 # undef MPK_THRESH
42 # define MPK_THRESH 4
43 #endif
44
45 /*----- Main code ---------------------------------------------------------*/
46
47 /* --- @mpx_ksqr@ --- *
48 *
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
52 *
53 * Returns: ---
54 *
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.
60 *
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.
64 */
65
66 void mpx_ksqr(mpw *dv, mpw *dvl,
67 const mpw *av, const mpw *avl,
68 mpw *sv, mpw *svl)
69 {
70 const mpw *avm;
71 size_t m;
72
73 /* --- Dispose of easy cases to @mpx_usqr@ --- *
74 *
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...
79 */
80
81 MPX_SHRINK(av, avl);
82
83 if (avl - av <= MPK_THRESH) {
84 mpx_usqr(dv, dvl, av, avl);
85 return;
86 }
87
88 /* --- How the algorithm works --- *
89 *
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
93 * squarings.
94 */
95
96 /* --- First things --- *
97 *
98 * Sort out where to break the factor in half.
99 */
100
101 m = (avl - av + 1) >> 1;
102 avm = av + m;
103
104 /* --- Sort out everything --- */
105
106 {
107 mpw *svm = sv + m, *svn = svm + m, *ssv = svn + 4;
108 mpw *tdv = dv + m;
109 mpw *rdv = tdv + m;
110
111 assert(rdv + m + 4 < dvl);
112 assert(ssv < svl);
113 UADD2(sv, svm, av, avm, avm, avl);
114 if (m > MPK_THRESH)
115 mpx_ksqr(tdv, rdv + m + 4, sv, svm + 1, ssv, svl);
116 else
117 mpx_usqr(tdv, rdv + m + 4, sv, svm + 1);
118
119 if (m > MPK_THRESH)
120 mpx_ksqr(sv, ssv, avm, avl, ssv, svl);
121 else
122 mpx_usqr(sv, ssv, avm, avl);
123 MPX_COPY(rdv + m + 1, dvl, svm + 1, svn);
124 UADD(rdv, sv, svm + 1);
125 USUB(tdv, sv, svn);
126
127 if (m > MPK_THRESH)
128 mpx_ksqr(sv, ssv, av, avm, ssv, svl);
129 else
130 mpx_usqr(sv, ssv, av, avm);
131 MPX_COPY(dv, tdv, sv, svm);
132 UADD(tdv, svm, svn);
133 USUB(tdv, sv, svn);
134 }
135 }
136
137 /*----- Test rig ----------------------------------------------------------*/
138
139 #ifdef TEST_RIG
140
141 #include <mLib/alloc.h>
142 #include <mLib/testrig.h>
143
144 #define ALLOC(v, vl, sz) do { \
145 size_t _sz = (sz); \
146 mpw *_vv = xmalloc(MPWS(_sz)); \
147 mpw *_vvl = _vv + _sz; \
148 (v) = _vv; \
149 (vl) = _vvl; \
150 } while (0)
151
152 #define LOAD(v, vl, d) do { \
153 const dstr *_d = (d); \
154 mpw *_v, *_vl; \
155 ALLOC(_v, _vl, MPW_RQ(_d->len)); \
156 mpx_loadb(_v, _vl, _d->buf, _d->len); \
157 (v) = _v; \
158 (vl) = _vl; \
159 } while (0)
160
161 #define MAX(x, y) ((x) > (y) ? (x) : (y))
162
163 static void dumpmp(const char *msg, const mpw *v, const mpw *vl)
164 {
165 fputs(msg, stderr);
166 MPX_SHRINK(v, vl);
167 while (v < vl)
168 fprintf(stderr, " %08lx", (unsigned long)*--vl);
169 fputc('\n', stderr);
170 }
171
172 static int usqr(dstr *v)
173 {
174 mpw *a, *al;
175 mpw *c, *cl;
176 mpw *d, *dl;
177 mpw *s, *sl;
178 size_t m;
179 int ok = 1;
180
181 LOAD(a, al, &v[0]);
182 LOAD(c, cl, &v[1]);
183 m = al - a + 1;
184 ALLOC(d, dl, 3 * m);
185 ALLOC(s, sl, 5 * m);
186
187 mpx_ksqr(d, dl, a, al, s, sl);
188 if (!mpx_ueq(d, dl, c, cl)) {
189 fprintf(stderr, "\n*** usqr failed\n");
190 dumpmp(" a", a, al);
191 dumpmp("expected", c, cl);
192 dumpmp(" result", d, dl);
193 ok = 0;
194 }
195
196 xfree(a); xfree(c); xfree(d); xfree(s);
197 return (ok);
198 }
199
200 static test_chunk defs[] = {
201 { "usqr", usqr, { &type_hex, &type_hex, 0 } },
202 { 0, 0, { 0 } }
203 };
204
205 int main(int argc, char *argv[])
206 {
207 test_run(argc, argv, defs, SRCDIR"/tests/mpx");
208 return (0);
209 }
210
211 #endif
212
213 /*----- That's all, folks -------------------------------------------------*/