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