5bf74dea |
1 | /* -*-c-*- |
2 | * |
c9060100 |
3 | * $Id: mpx-ksqr.c,v 1.5 2000/10/08 12:11:01 mdw Exp $ |
5bf74dea |
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 $ |
c9060100 |
33 | * Revision 1.5 2000/10/08 12:11:01 mdw |
34 | * Use @mpx_ueq@ instead of @MPX_UCMP@. |
35 | * |
07dc33b2 |
36 | * Revision 1.4 2000/07/29 17:04:02 mdw |
37 | * Remove useless header `mpscan.h'. |
38 | * |
d2d86297 |
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 | * |
4468424e |
44 | * Revision 1.2 1999/12/13 15:35:01 mdw |
45 | * Simplify and improve. |
46 | * |
5bf74dea |
47 | * Revision 1.1 1999/12/11 10:57:43 mdw |
48 | * Karatsuba squaring algorithm. |
49 | * |
50 | */ |
51 | |
52 | /*----- Header files ------------------------------------------------------*/ |
53 | |
4468424e |
54 | #include <assert.h> |
5bf74dea |
55 | #include <stdio.h> |
56 | |
57 | #include "mpx.h" |
d2d86297 |
58 | #include "mpx-kmac.h" |
5bf74dea |
59 | |
60 | /*----- Tweakables --------------------------------------------------------*/ |
61 | |
62 | #ifdef TEST_RIG |
63 | # undef KARATSUBA_CUTOFF |
64 | # define KARATSUBA_CUTOFF 2 |
65 | #endif |
66 | |
5bf74dea |
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 | * |
d2d86297 |
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. |
5bf74dea |
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 | |
4468424e |
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 | |
5bf74dea |
131 | /* --- Sort out everything --- */ |
132 | |
133 | { |
4468424e |
134 | mpw *svm = sv + m, *svn = svm + m, *ssv = svn + 4; |
5bf74dea |
135 | mpw *tdv = dv + m; |
136 | mpw *rdv = tdv + m; |
137 | |
d2d86297 |
138 | UADD2(sv, svm, av, avm, avm, avl); |
5bf74dea |
139 | if (m > KARATSUBA_CUTOFF) |
d2d86297 |
140 | mpx_ksqr(tdv, rdv + m + 4, sv, svm + 1, ssv, svl); |
5bf74dea |
141 | else |
d2d86297 |
142 | mpx_usqr(tdv, rdv + m + 4, sv, svm + 1); |
5bf74dea |
143 | |
144 | if (m > KARATSUBA_CUTOFF) |
145 | mpx_ksqr(sv, ssv, avm, avl, ssv, svl); |
146 | else |
147 | mpx_usqr(sv, ssv, avm, avl); |
4468424e |
148 | MPX_COPY(rdv + m + 1, dvl, svm + 1, svn); |
149 | UADD(rdv, sv, svm + 1); |
d2d86297 |
150 | USUB(tdv, sv, svn); |
5bf74dea |
151 | |
152 | if (m > KARATSUBA_CUTOFF) |
153 | mpx_ksqr(sv, ssv, av, avm, ssv, svl); |
154 | else |
155 | mpx_usqr(sv, ssv, av, avm); |
4468424e |
156 | MPX_COPY(dv, tdv, sv, svm); |
157 | UADD(tdv, svm, svn); |
d2d86297 |
158 | USUB(tdv, sv, svn); |
5bf74dea |
159 | } |
160 | } |
161 | |
162 | /*----- Test rig ----------------------------------------------------------*/ |
163 | |
164 | #ifdef TEST_RIG |
165 | |
166 | #include <mLib/alloc.h> |
167 | #include <mLib/testrig.h> |
168 | |
5bf74dea |
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); |
c9060100 |
213 | if (!mpx_ueq(d, dl, c, cl)) { |
5bf74dea |
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 -------------------------------------------------*/ |