Make tables of standard encryption schemes etc.
[u/mdw/catacomb] / gfx-kmul.c
1 /* -*-c-*-
2 *
3 * $Id: gfx-kmul.c,v 1.3 2004/03/27 17:54:11 mdw Exp $
4 *
5 * Karatsuba's multiplication algorithm on binary polynomials
6 *
7 * (c) 2000 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: gfx-kmul.c,v $
33 * Revision 1.3 2004/03/27 17:54:11 mdw
34 * Standard curves and curve checking.
35 *
36 * Revision 1.2 2002/10/09 00:36:03 mdw
37 * Fix bounds on workspace for Karatsuba operations.
38 *
39 * Revision 1.1 2000/10/08 15:49:37 mdw
40 * First glimmerings of binary polynomial arithmetic.
41 *
42 */
43
44 /*----- Header files ------------------------------------------------------*/
45
46 #include <assert.h>
47 #include <stdio.h>
48
49 #include "gfx.h"
50 #include "karatsuba.h"
51
52 /*----- Tweakables --------------------------------------------------------*/
53
54 #ifdef TEST_RIG
55 # undef GFK_THRESH
56 # define GFK_THRESH 1
57 #endif
58
59 /*----- Main code ---------------------------------------------------------*/
60
61 /* --- @gfx_kmul@ --- *
62 *
63 * Arguments: @mpw *dv, *dvl@ = pointer to destination buffer
64 * @const mpw *av, *avl@ = pointer to first argument
65 * @const mpw *bv, *bvl@ = pointer to second argument
66 * @mpw *sv, *svl@ = pointer to scratch workspace
67 *
68 * Returns: ---
69 *
70 * Use: Multiplies two binary polynomials using Karatsuba's
71 * algorithm. This is rather faster than traditional long
72 * multiplication (e.g., @gfx_umul@) on polynomials with large
73 * degree, although more expensive on small ones.
74 *
75 * The destination must be twice as large as the larger
76 * argument. The scratch space must be twice as large as the
77 * larger argument.
78 */
79
80 void gfx_kmul(mpw *dv, mpw *dvl,
81 const mpw *av, const mpw *avl,
82 const mpw *bv, const mpw *bvl,
83 mpw *sv, mpw *svl)
84 {
85 const mpw *avm, *bvm;
86 size_t m;
87
88 /* --- Dispose of easy cases to @mpx_umul@ --- *
89 *
90 * Karatsuba is only a win on large numbers, because of all the
91 * recursiveness and bookkeeping. The recursive calls make a quick check
92 * to see whether to bottom out to @gfx_umul@ which should help quite a
93 * lot, but sometimes the only way to know is to make sure...
94 */
95
96 MPX_SHRINK(av, avl);
97 MPX_SHRINK(bv, bvl);
98
99 if (avl - av <= GFK_THRESH || bvl - bv <= GFK_THRESH) {
100 gfx_mul(dv, dvl, av, avl, bv, bvl);
101 return;
102 }
103
104 /* --- How the algorithm works --- *
105 *
106 * Let %$A = xb + y$% and %$B = ub + v$%. Then, simply by expanding,
107 * %$AB = x u b^2 + b(x v + y u) + y v$%. That's not helped any, because
108 * I've got four multiplications, each four times easier than the one I
109 * started with. However, note that I can rewrite the coefficient of %$b$%
110 * as %$xv + yu = (x + y)(u + v) - xu - yv$%. The terms %$xu$% and %$yv$%
111 * I've already calculated, and that leaves only one more multiplication to
112 * do. So now I have three multiplications, each four times easier, and
113 * that's a win.
114 */
115
116 /* --- First things --- *
117 *
118 * Sort out where to break the factors in half. I'll choose the midpoint
119 * of the larger one, since this minimizes the amount of work I have to do
120 * most effectively.
121 */
122
123 if (avl - av > bvl - bv) {
124 m = (avl - av + 1) >> 1;
125 avm = av + m;
126 if (bvl - bv > m)
127 bvm = bv + m;
128 else
129 bvm = bvl;
130 } else {
131 m = (bvl - bv + 1) >> 1;
132 bvm = bv + m;
133 if (avl - av > m)
134 avm = av + m;
135 else
136 avm = avl;
137 }
138
139 /* --- Sort out the middle term --- */
140
141 {
142 mpw *bsv = sv + m, *ssv = bsv + m;
143 mpw *rdv = dv + m, *rdvl = rdv + 2 * m;
144
145 assert(rdvl <= dvl);
146 assert(ssv <= svl);
147 UXOR2(sv, bsv, av, avm, avm, avl);
148 UXOR2(bsv, ssv, bv, bvm, bvm, bvl);
149 if (m > GFK_THRESH)
150 gfx_kmul(rdv, rdvl, sv, bsv, bsv, ssv, ssv, svl);
151 else
152 gfx_mul(rdv, rdvl, sv, bsv, bsv, ssv);
153 }
154
155 /* --- Sort out the other two terms --- */
156
157 {
158 mpw *svm = sv + m, *ssv = svm + m;
159 mpw *tdv = dv + m;
160 mpw *rdv = tdv + m;
161
162 if (avl == avm || bvl == bvm)
163 MPX_ZERO(rdv + m, dvl);
164 else {
165 if (m > GFK_THRESH)
166 gfx_kmul(sv, ssv, avm, avl, bvm, bvl, ssv, svl);
167 else
168 gfx_mul(sv, ssv, avm, avl, bvm, bvl);
169 MPX_COPY(rdv + m, dvl, svm, ssv);
170 UXOR(rdv, sv, svm);
171 UXOR(tdv, sv, ssv);
172 }
173
174 if (m > GFK_THRESH)
175 gfx_kmul(sv, ssv, av, avm, bv, bvm, ssv, svl);
176 else
177 gfx_mul(sv, ssv, av, avm, bv, bvm);
178 MPX_COPY(dv, tdv, sv, svm);
179 UXOR(tdv, sv, ssv);
180 UXOR(tdv, svm, ssv);
181 }
182 }
183
184 /*----- Test rig ----------------------------------------------------------*/
185
186 #ifdef TEST_RIG
187
188 #include <mLib/alloc.h>
189 #include <mLib/testrig.h>
190
191 #define ALLOC(v, vl, sz) do { \
192 size_t _sz = (sz); \
193 mpw *_vv = xmalloc(MPWS(_sz)); \
194 mpw *_vvl = _vv + _sz; \
195 (v) = _vv; \
196 (vl) = _vvl; \
197 } while (0)
198
199 #define LOAD(v, vl, d) do { \
200 const dstr *_d = (d); \
201 mpw *_v, *_vl; \
202 ALLOC(_v, _vl, MPW_RQ(_d->len)); \
203 mpx_loadb(_v, _vl, _d->buf, _d->len); \
204 (v) = _v; \
205 (vl) = _vl; \
206 } while (0)
207
208 #define MAX(x, y) ((x) > (y) ? (x) : (y))
209
210 static void dumpmp(const char *msg, const mpw *v, const mpw *vl)
211 {
212 fputs(msg, stderr);
213 MPX_SHRINK(v, vl);
214 while (v < vl)
215 fprintf(stderr, " %08lx", (unsigned long)*--vl);
216 fputc('\n', stderr);
217 }
218
219 static int mul(dstr *v)
220 {
221 mpw *a, *al;
222 mpw *b, *bl;
223 mpw *c, *cl;
224 mpw *d, *dl;
225 mpw *s, *sl;
226 size_t m;
227 int ok = 1;
228
229 LOAD(a, al, &v[0]);
230 LOAD(b, bl, &v[1]);
231 LOAD(c, cl, &v[2]);
232 m = MAX(al - a, bl - b) + 1;
233 ALLOC(d, dl, 2 * m);
234 ALLOC(s, sl, 2 * m);
235
236 gfx_kmul(d, dl, a, al, b, bl, s, sl);
237 if (!mpx_ueq(d, dl, c, cl)) {
238 fprintf(stderr, "\n*** mul failed\n");
239 dumpmp(" a", a, al);
240 dumpmp(" b", b, bl);
241 dumpmp("expected", c, cl);
242 dumpmp(" result", d, dl);
243 ok = 0;
244 }
245
246 free(a); free(b); free(c); free(d); free(s);
247 return (ok);
248 }
249
250 static test_chunk defs[] = {
251 { "mul", mul, { &type_hex, &type_hex, &type_hex, 0 } },
252 { 0, 0, { 0 } }
253 };
254
255 int main(int argc, char *argv[])
256 {
257 test_run(argc, argv, defs, SRCDIR"/tests/gfx");
258 return (0);
259 }
260
261 #endif
262
263 /*----- That's all, folks -------------------------------------------------*/