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