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