5bf74dea |
1 | /* -*-c-*- |
2 | * |
3 | * $Id: mpx-ksqr.c,v 1.1 1999/12/11 10:57:43 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.1 1999/12/11 10:57:43 mdw |
34 | * Karatsuba squaring algorithm. |
35 | * |
36 | */ |
37 | |
38 | /*----- Header files ------------------------------------------------------*/ |
39 | |
40 | #include <stdio.h> |
41 | |
42 | #include "mpx.h" |
43 | |
44 | /*----- Tweakables --------------------------------------------------------*/ |
45 | |
46 | #ifdef TEST_RIG |
47 | # undef KARATSUBA_CUTOFF |
48 | # define KARATSUBA_CUTOFF 2 |
49 | #endif |
50 | |
51 | /*----- Addition macros ---------------------------------------------------*/ |
52 | |
53 | #define ULSL1(dv, av, avl) do { \ |
54 | mpw *_dv = (dv); \ |
55 | const mpw *_av = (av), *_avl = (avl); \ |
56 | mpw _c = 0; \ |
57 | \ |
58 | while (_av < _avl) { \ |
59 | mpw _x = *_av++; \ |
60 | *_dv++ = MPW(_c | (_x << 1)); \ |
61 | _c = MPW(_x >> (MPW_BITS - 1)); \ |
62 | } \ |
63 | *_dv++ = _c; \ |
64 | } while (0) |
65 | |
66 | #define UADD(dv, av, avl) do { \ |
67 | mpw *_dv = (dv); \ |
68 | const mpw *_av = (av), *_avl = (avl); \ |
69 | mpw _c = 0; \ |
70 | \ |
71 | while (_av < _avl) { \ |
72 | mpw _a, _b; \ |
73 | mpd _x; \ |
74 | _a = *_av++; \ |
75 | _b = *_dv; \ |
76 | _x = (mpd)_a + (mpd)_b + _c; \ |
77 | *_dv++ = MPW(_x); \ |
78 | _c = _x >> MPW_BITS; \ |
79 | } \ |
80 | while (_c) { \ |
81 | mpd _x = (mpd)*_dv + (mpd)_c; \ |
82 | *_dv++ = MPW(_x); \ |
83 | _c = _x >> MPW_BITS; \ |
84 | } \ |
85 | } while (0) |
86 | |
87 | /*----- Main code ---------------------------------------------------------*/ |
88 | |
89 | /* --- @mpx_ksqr@ --- * |
90 | * |
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 |
94 | * |
95 | * Returns: --- |
96 | * |
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. |
102 | * |
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@. |
106 | */ |
107 | |
108 | void mpx_ksqr(mpw *dv, mpw *dvl, |
109 | const mpw *av, const mpw *avl, |
110 | mpw *sv, mpw *svl) |
111 | { |
112 | const mpw *avm; |
113 | size_t m; |
114 | |
115 | /* --- Dispose of easy cases to @mpx_usqr@ --- * |
116 | * |
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... |
121 | */ |
122 | |
123 | MPX_SHRINK(av, avl); |
124 | |
125 | if (avl - av <= KARATSUBA_CUTOFF) { |
126 | mpx_usqr(dv, dvl, av, avl); |
127 | return; |
128 | } |
129 | |
130 | /* --- How the algorithm works --- * |
131 | * |
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. |
136 | */ |
137 | |
138 | /* --- First things --- * |
139 | * |
140 | * Sort out where to break the factor in half. |
141 | */ |
142 | |
143 | m = (avl - av + 1) >> 1; |
144 | avm = av + m; |
145 | |
146 | /* --- Sort out everything --- */ |
147 | |
148 | { |
149 | mpw *ssv = sv + 2 * m; |
150 | mpw *tdv = dv + m; |
151 | mpw *rdv = tdv + m; |
152 | |
153 | /* --- The cross term in the middle needs a multiply --- * |
154 | * |
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. |
157 | */ |
158 | |
159 | if (m > KARATSUBA_CUTOFF) |
160 | mpx_kmul(sv, ssv, av, avm, avm, avl, ssv, svl); |
161 | else |
162 | mpx_umul(sv, ssv, av, avm, avm, avl); |
163 | ULSL1(tdv, sv, ssv); |
164 | MPX_ZERO(dv, tdv); |
165 | MPX_ZERO(rdv + m + 1, dvl); |
166 | |
167 | if (m > KARATSUBA_CUTOFF) |
168 | mpx_ksqr(sv, ssv, avm, avl, ssv, svl); |
169 | else |
170 | mpx_usqr(sv, ssv, avm, avl); |
171 | UADD(rdv, sv, ssv); |
172 | |
173 | if (m > KARATSUBA_CUTOFF) |
174 | mpx_ksqr(sv, ssv, av, avm, ssv, svl); |
175 | else |
176 | mpx_usqr(sv, ssv, av, avm); |
177 | UADD(dv, sv, ssv); |
178 | } |
179 | } |
180 | |
181 | /*----- Test rig ----------------------------------------------------------*/ |
182 | |
183 | #ifdef TEST_RIG |
184 | |
185 | #include <mLib/alloc.h> |
186 | #include <mLib/testrig.h> |
187 | |
188 | #include "mpscan.h" |
189 | |
190 | #define ALLOC(v, vl, sz) do { \ |
191 | size_t _sz = (sz); \ |
192 | mpw *_vv = xmalloc(MPWS(_sz)); \ |
193 | mpw *_vvl = _vv + _sz; \ |
194 | (v) = _vv; \ |
195 | (vl) = _vvl; \ |
196 | } while (0) |
197 | |
198 | #define LOAD(v, vl, d) do { \ |
199 | const dstr *_d = (d); \ |
200 | mpw *_v, *_vl; \ |
201 | ALLOC(_v, _vl, MPW_RQ(_d->len)); \ |
202 | mpx_loadb(_v, _vl, _d->buf, _d->len); \ |
203 | (v) = _v; \ |
204 | (vl) = _vl; \ |
205 | } while (0) |
206 | |
207 | #define MAX(x, y) ((x) > (y) ? (x) : (y)) |
208 | |
209 | static void dumpmp(const char *msg, const mpw *v, const mpw *vl) |
210 | { |
211 | fputs(msg, stderr); |
212 | MPX_SHRINK(v, vl); |
213 | while (v < vl) |
214 | fprintf(stderr, " %08lx", (unsigned long)*--vl); |
215 | fputc('\n', stderr); |
216 | } |
217 | |
218 | static int usqr(dstr *v) |
219 | { |
220 | mpw *a, *al; |
221 | mpw *c, *cl; |
222 | mpw *d, *dl; |
223 | mpw *s, *sl; |
224 | size_t m; |
225 | int ok = 1; |
226 | |
227 | LOAD(a, al, &v[0]); |
228 | LOAD(c, cl, &v[1]); |
229 | m = al - a + 1; |
230 | ALLOC(d, dl, 2 * m); |
231 | ALLOC(s, sl, 2 * m + 32); |
232 | |
233 | mpx_ksqr(d, dl, a, al, s, sl); |
234 | if (MPX_UCMP(d, dl, !=, c, cl)) { |
235 | fprintf(stderr, "\n*** usqr failed\n"); |
236 | dumpmp(" a", a, al); |
237 | dumpmp("expected", c, cl); |
238 | dumpmp(" result", d, dl); |
239 | ok = 0; |
240 | } |
241 | |
242 | free(a); free(c); free(d); free(s); |
243 | return (ok); |
244 | } |
245 | |
246 | static test_chunk defs[] = { |
247 | { "usqr", usqr, { &type_hex, &type_hex, 0 } }, |
248 | { 0, 0, { 0 } } |
249 | }; |
250 | |
251 | int main(int argc, char *argv[]) |
252 | { |
253 | test_run(argc, argv, defs, SRCDIR"/tests/mpx"); |
254 | return (0); |
255 | } |
256 | |
257 | #endif |
258 | |
259 | /*----- That's all, folks -------------------------------------------------*/ |