5bf74dea |
1 | /* -*-c-*- |
2 | * |
b817bfc6 |
3 | * $Id: mpx-ksqr.c,v 1.8 2004/04/08 01:36:15 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 | |
5bf74dea |
30 | /*----- Header files ------------------------------------------------------*/ |
31 | |
4468424e |
32 | #include <assert.h> |
5bf74dea |
33 | #include <stdio.h> |
34 | |
35 | #include "mpx.h" |
52cdaca9 |
36 | #include "karatsuba.h" |
5bf74dea |
37 | |
38 | /*----- Tweakables --------------------------------------------------------*/ |
39 | |
40 | #ifdef TEST_RIG |
52cdaca9 |
41 | # undef MPK_THRESH |
dd22938e |
42 | # define MPK_THRESH 4 |
5bf74dea |
43 | #endif |
44 | |
5bf74dea |
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 | * |
dd22938e |
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. |
5bf74dea |
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 | |
52cdaca9 |
83 | if (avl - av <= MPK_THRESH) { |
5bf74dea |
84 | mpx_usqr(dv, dvl, av, avl); |
85 | return; |
86 | } |
87 | |
88 | /* --- How the algorithm works --- * |
89 | * |
d2d86297 |
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. |
5bf74dea |
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 | { |
4468424e |
107 | mpw *svm = sv + m, *svn = svm + m, *ssv = svn + 4; |
5bf74dea |
108 | mpw *tdv = dv + m; |
109 | mpw *rdv = tdv + m; |
110 | |
dd22938e |
111 | assert(rdv + m + 4 < dvl); |
112 | assert(ssv < svl); |
d2d86297 |
113 | UADD2(sv, svm, av, avm, avm, avl); |
52cdaca9 |
114 | if (m > MPK_THRESH) |
d2d86297 |
115 | mpx_ksqr(tdv, rdv + m + 4, sv, svm + 1, ssv, svl); |
5bf74dea |
116 | else |
d2d86297 |
117 | mpx_usqr(tdv, rdv + m + 4, sv, svm + 1); |
5bf74dea |
118 | |
52cdaca9 |
119 | if (m > MPK_THRESH) |
5bf74dea |
120 | mpx_ksqr(sv, ssv, avm, avl, ssv, svl); |
121 | else |
122 | mpx_usqr(sv, ssv, avm, avl); |
4468424e |
123 | MPX_COPY(rdv + m + 1, dvl, svm + 1, svn); |
124 | UADD(rdv, sv, svm + 1); |
d2d86297 |
125 | USUB(tdv, sv, svn); |
5bf74dea |
126 | |
52cdaca9 |
127 | if (m > MPK_THRESH) |
5bf74dea |
128 | mpx_ksqr(sv, ssv, av, avm, ssv, svl); |
129 | else |
130 | mpx_usqr(sv, ssv, av, avm); |
4468424e |
131 | MPX_COPY(dv, tdv, sv, svm); |
132 | UADD(tdv, svm, svn); |
d2d86297 |
133 | USUB(tdv, sv, svn); |
5bf74dea |
134 | } |
135 | } |
136 | |
137 | /*----- Test rig ----------------------------------------------------------*/ |
138 | |
139 | #ifdef TEST_RIG |
140 | |
141 | #include <mLib/alloc.h> |
142 | #include <mLib/testrig.h> |
143 | |
5bf74dea |
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; |
dd22938e |
184 | ALLOC(d, dl, 3 * m); |
185 | ALLOC(s, sl, 5 * m); |
5bf74dea |
186 | |
187 | mpx_ksqr(d, dl, a, al, s, sl); |
c9060100 |
188 | if (!mpx_ueq(d, dl, c, cl)) { |
5bf74dea |
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 | free(a); free(c); free(d); free(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 -------------------------------------------------*/ |