math/mpreduce.h: Missing include files.
[u/mdw/catacomb] / math / mpreduce.c
1 /* -*-c-*-
2 *
3 * Efficient reduction modulo nice primes
4 *
5 * (c) 2004 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of Catacomb.
11 *
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
16 *
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
21 *
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25 * MA 02111-1307, USA.
26 */
27
28 /*----- Header files ------------------------------------------------------*/
29
30 #include <mLib/darray.h>
31 #include <mLib/macros.h>
32
33 #include "mp.h"
34 #include "mpreduce.h"
35 #include "mpreduce-exp.h"
36
37 /*----- Data structures ---------------------------------------------------*/
38
39 DA_DECL(instr_v, mpreduce_instr);
40
41 /*----- Main code ---------------------------------------------------------*/
42
43 /* --- @mpreduce_create@ --- *
44 *
45 * Arguments: @gfreduce *r@ = structure to fill in
46 * @mp *x@ = an integer
47 *
48 * Returns: Zero if successful; nonzero on failure.
49 *
50 * Use: Initializes a context structure for reduction.
51 */
52
53 int mpreduce_create(mpreduce *r, mp *p)
54 {
55 mpscan sc;
56 enum { Z = 0, Z1 = 2, X = 4, X0 = 6 };
57 unsigned st = Z;
58 instr_v iv = DA_INIT;
59 unsigned long d, i;
60 unsigned op;
61 size_t w, b, bb;
62
63 /* --- Fill in the easy stuff --- */
64
65 if (!MP_POSP(p))
66 return (-1);
67 d = mp_bits(p);
68 r->lim = d/MPW_BITS;
69 r->s = d%MPW_BITS;
70 if (r->s)
71 r->lim++;
72 r->p = mp_copy(p);
73
74 /* --- Stash a new instruction --- */
75
76 #define INSTR(op_, argx_, argy_) do { \
77 DA_ENSURE(&iv, 1); \
78 DA(&iv)[DA_LEN(&iv)].op = (op_); \
79 DA(&iv)[DA_LEN(&iv)].argx = (argx_); \
80 DA(&iv)[DA_LEN(&iv)].argy = (argy_); \
81 DA_EXTEND(&iv, 1); \
82 } while (0)
83
84 /* --- Main loop --- *
85 *
86 * A simple state machine decomposes @p@ conveniently into positive and
87 * negative powers of 2. The pure form of the state machine is left below
88 * for reference (and in case I need inspiration for a NAF exponentiator).
89 */
90
91 #ifdef DEBUG
92 for (i = 0, mp_scan(&sc, p); mp_step(&sc); i++) {
93 switch (st | mp_bit(&sc)) {
94 case Z | 1: st = Z1; break;
95 case Z1 | 0: st = Z; printf("+ %lu\n", i - 1); break;
96 case Z1 | 1: st = X; printf("- %lu\n", i - 1); break;
97 case X | 0: st = X0; break;
98 case X0 | 1: st = X; printf("- %lu\n", i - 1); break;
99 case X0 | 0: st = Z; printf("+ %lu\n", i - 1); break;
100 }
101 }
102 if (st >= X) printf("+ %lu\n", i - 1);
103 st = Z;
104 #endif
105
106 bb = MPW_BITS - (d + 1)%MPW_BITS;
107 for (i = 0, mp_scan(&sc, p); i < d && mp_step(&sc); i++) {
108 switch (st | mp_bit(&sc)) {
109 case Z | 1: st = Z1; break;
110 case Z1 | 0: st = Z; op = MPRI_SUB; goto instr;
111 case Z1 | 1: st = X; op = MPRI_ADD; goto instr;
112 case X | 0: st = X0; break;
113 case X0 | 1: st = X; op = MPRI_ADD; goto instr;
114 case X0 | 0: st = Z; op = MPRI_SUB; goto instr;
115 instr:
116 w = (d - i)/MPW_BITS + 1;
117 b = (bb + i)%MPW_BITS;
118 INSTR(op | !!b, w, b);
119 }
120 }
121 if (DA_LEN(&iv) && (DA(&iv)[DA_LEN(&iv) - 1].op & ~1u) == MPRI_SUB) {
122 mp_drop(r->p);
123 DA_DESTROY(&iv);
124 return (-1);
125 }
126
127 #undef INSTR
128
129 /* --- Wrap up --- */
130
131 r->in = DA_LEN(&iv);
132 if (!r->in)
133 r->iv = 0;
134 else if (!r->s) {
135 r->iv = xmalloc(r->in * sizeof(mpreduce_instr));
136 memcpy(r->iv, DA(&iv), r->in * sizeof(mpreduce_instr));
137 } else {
138 r->iv = xmalloc(r->in * 2 * sizeof(mpreduce_instr));
139 for (i = 0; i < r->in; i++) {
140 r->iv[i] = DA(&iv)[i];
141 op = r->iv[i].op & ~1u;
142 w = r->iv[i].argx;
143 b = r->iv[i].argy;
144 b += r->s;
145 if (b >= MPW_BITS) {
146 b -= MPW_BITS;
147 w--;
148 }
149 if (b) op |= 1;
150 r->iv[i + r->in].op = op;
151 r->iv[i + r->in].argx = w;
152 r->iv[i + r->in].argy = b;
153 }
154 }
155 DA_DESTROY(&iv);
156
157 #ifdef DEBUG
158 mpreduce_dump(r, stdout);
159 #endif
160 return (0);
161 }
162
163 /* --- @mpreduce_destroy@ --- *
164 *
165 * Arguments: @mpreduce *r@ = structure to free
166 *
167 * Returns: ---
168 *
169 * Use: Reclaims the resources from a reduction context.
170 */
171
172 void mpreduce_destroy(mpreduce *r)
173 {
174 mp_drop(r->p);
175 if (r->iv) xfree(r->iv);
176 }
177
178 /* --- @mpreduce_dump@ --- *
179 *
180 * Arguments: @mpreduce *r@ = structure to dump
181 * @FILE *fp@ = file to dump on
182 *
183 * Returns: ---
184 *
185 * Use: Dumps a reduction context.
186 */
187
188 void mpreduce_dump(mpreduce *r, FILE *fp)
189 {
190 size_t i;
191 static const char *opname[] = { "add", "addshift", "sub", "subshift" };
192
193 fprintf(fp, "mod = "); mp_writefile(r->p, fp, 16);
194 fprintf(fp, "\n lim = %lu; s = %d\n", (unsigned long)r->lim, r->s);
195 for (i = 0; i < r->in; i++) {
196 assert(r->iv[i].op < N(opname));
197 fprintf(fp, " %s %lu %lu\n",
198 opname[r->iv[i].op],
199 (unsigned long)r->iv[i].argx,
200 (unsigned long)r->iv[i].argy);
201 }
202 if (r->s) {
203 fprintf(fp, "tail end charlie\n");
204 for (i = r->in; i < 2 * r->in; i++) {
205 assert(r->iv[i].op < N(opname));
206 fprintf(fp, " %s %lu %lu\n",
207 opname[r->iv[i].op],
208 (unsigned long)r->iv[i].argx,
209 (unsigned long)r->iv[i].argy);
210 }
211 }
212 }
213
214 /* --- @mpreduce_do@ --- *
215 *
216 * Arguments: @mpreduce *r@ = reduction context
217 * @mp *d@ = destination
218 * @mp *x@ = source
219 *
220 * Returns: Destination, @x@ reduced modulo the reduction poly.
221 */
222
223 static void run(const mpreduce_instr *i, const mpreduce_instr *il,
224 mpw *v, mpw z)
225 {
226 for (; i < il; i++) {
227 #ifdef DEBUG
228 mp vv;
229 mp_build(&vv, v - i->argx, v + 1);
230 printf(" 0x"); mp_writefile(&vv, stdout, 16);
231 printf(" %c (0x%lx << %u) == 0x",
232 (i->op & ~1u) == MPRI_ADD ? '+' : '-',
233 (unsigned long)z,
234 i->argy);
235 #endif
236 switch (i->op) {
237 case MPRI_ADD: MPX_UADDN(v - i->argx, v + 1, z); break;
238 case MPRI_ADDLSL: mpx_uaddnlsl(v - i->argx, v + 1, z, i->argy); break;
239 case MPRI_SUB: MPX_USUBN(v - i->argx, v + 1, z); break;
240 case MPRI_SUBLSL: mpx_usubnlsl(v - i->argx, v + 1, z, i->argy); break;
241 default:
242 abort();
243 }
244 #ifdef DEBUG
245 mp_build(&vv, v - i->argx, v + 1);
246 mp_writefile(&vv, stdout, 16);
247 printf("\n");
248 #endif
249 }
250 }
251
252 mp *mpreduce_do(mpreduce *r, mp *d, mp *x)
253 {
254 mpw *v, *vl;
255 const mpreduce_instr *il;
256 mpw z;
257
258 #ifdef DEBUG
259 mp *_r = 0, *_rr = 0;
260 #endif
261
262 /* --- If source is negative, divide --- */
263
264 if (MP_NEGP(x)) {
265 mp_div(0, &d, x, r->p);
266 return (d);
267 }
268
269 /* --- Try to reuse the source's space --- */
270
271 MP_COPY(x);
272 if (d) MP_DROP(d);
273 MP_DEST(x, MP_LEN(x), x->f);
274
275 /* --- Do the reduction --- */
276
277 #ifdef DEBUG
278 _r = MP_NEW;
279 mp_div(0, &_r, x, r->p);
280 MP_PRINTX("x", x);
281 _rr = 0;
282 #endif
283
284 il = r->iv + r->in;
285 if (MP_LEN(x) >= r->lim) {
286 v = x->v + r->lim;
287 vl = x->vl;
288 while (vl-- > v) {
289 while (*vl) {
290 z = *vl;
291 *vl = 0;
292 run(r->iv, il, vl, z);
293 #ifdef DEBUG
294 MP_PRINTX("x", x);
295 mp_div(0, &_rr, x, r->p);
296 assert(MP_EQ(_r, _rr));
297 #endif
298 }
299 }
300 if (r->s) {
301 while (*vl >> r->s) {
302 z = *vl >> r->s;
303 *vl &= ((1 << r->s) - 1);
304 run(r->iv + r->in, il + r->in, vl, z);
305 #ifdef DEBUG
306 MP_PRINTX("x", x);
307 mp_div(0, &_rr, x, r->p);
308 assert(MP_EQ(_r, _rr));
309 #endif
310 }
311 }
312 }
313
314 /* --- Finishing touches --- */
315
316 MP_SHRINK(x);
317 if (MP_CMP(x, >=, r->p))
318 x = mp_sub(x, x, r->p);
319
320 /* --- Done --- */
321
322 #ifdef DEBUG
323 assert(MP_EQ(_r, x));
324 mp_drop(_r);
325 mp_drop(_rr);
326 #endif
327 return (x);
328 }
329
330 /* --- @mpreduce_exp@ --- *
331 *
332 * Arguments: @mpreduce *mr@ = pointer to reduction context
333 * @mp *d@ = fake destination
334 * @mp *a@ = base
335 * @mp *e@ = exponent
336 *
337 * Returns: Result, %$a^e \bmod m$%.
338 */
339
340 mp *mpreduce_exp(mpreduce *mr, mp *d, mp *a, mp *e)
341 {
342 mp *x = MP_ONE;
343 mp *spare = (e->f & MP_BURN) ? MP_NEWSEC : MP_NEW;
344
345 MP_SHRINK(e);
346 MP_COPY(a);
347 if (MP_ZEROP(e))
348 ;
349 else {
350 if (MP_NEGP(e))
351 a = mp_modinv(a, a, mr->p);
352 if (MP_LEN(e) < EXP_THRESH)
353 EXP_SIMPLE(x, a, e);
354 else
355 EXP_WINDOW(x, a, e);
356 }
357 mp_drop(a);
358 mp_drop(d);
359 mp_drop(spare);
360 return (x);
361 }
362
363 /*----- Test rig ----------------------------------------------------------*/
364
365
366 #ifdef TEST_RIG
367
368 #define MP(x) mp_readstring(MP_NEW, #x, 0, 0)
369
370 static int vreduce(dstr *v)
371 {
372 mp *d = *(mp **)v[0].buf;
373 mp *n = *(mp **)v[1].buf;
374 mp *r = *(mp **)v[2].buf;
375 mp *c;
376 int ok = 1;
377 mpreduce rr;
378
379 mpreduce_create(&rr, d);
380 c = mpreduce_do(&rr, MP_NEW, n);
381 if (!MP_EQ(c, r)) {
382 fprintf(stderr, "\n*** reduction failed\n*** ");
383 mpreduce_dump(&rr, stderr);
384 fprintf(stderr, "\n*** n = "); mp_writefile(n, stderr, 10);
385 fprintf(stderr, "\n*** r = "); mp_writefile(r, stderr, 10);
386 fprintf(stderr, "\n*** c = "); mp_writefile(c, stderr, 10);
387 fprintf(stderr, "\n");
388 ok = 0;
389 }
390 mpreduce_destroy(&rr);
391 mp_drop(n); mp_drop(d); mp_drop(r); mp_drop(c);
392 assert(mparena_count(MPARENA_GLOBAL) == 0);
393 return (ok);
394 }
395
396 static int vmodexp(dstr *v)
397 {
398 mp *p = *(mp **)v[0].buf;
399 mp *g = *(mp **)v[1].buf;
400 mp *x = *(mp **)v[2].buf;
401 mp *r = *(mp **)v[3].buf;
402 mp *c;
403 int ok = 1;
404 mpreduce rr;
405
406 mpreduce_create(&rr, p);
407 c = mpreduce_exp(&rr, MP_NEW, g, x);
408 if (!MP_EQ(c, r)) {
409 fprintf(stderr, "\n*** modexp failed\n*** ");
410 fprintf(stderr, "\n*** p = "); mp_writefile(p, stderr, 10);
411 fprintf(stderr, "\n*** g = "); mp_writefile(g, stderr, 10);
412 fprintf(stderr, "\n*** x = "); mp_writefile(x, stderr, 10);
413 fprintf(stderr, "\n*** c = "); mp_writefile(c, stderr, 10);
414 fprintf(stderr, "\n*** r = "); mp_writefile(r, stderr, 10);
415 fprintf(stderr, "\n");
416 ok = 0;
417 }
418 mpreduce_destroy(&rr);
419 mp_drop(p); mp_drop(g); mp_drop(r); mp_drop(x); mp_drop(c);
420 assert(mparena_count(MPARENA_GLOBAL) == 0);
421 return (ok);
422 }
423
424 static test_chunk defs[] = {
425 { "reduce", vreduce, { &type_mp, &type_mp, &type_mp, 0 } },
426 { "modexp", vmodexp, { &type_mp, &type_mp, &type_mp, &type_mp, 0 } },
427 { 0, 0, { 0 } }
428 };
429
430 int main(int argc, char *argv[])
431 {
432 test_run(argc, argv, defs, SRCDIR"/t/mpreduce");
433 return (0);
434 }
435
436 #endif
437
438 /*----- That's all, folks -------------------------------------------------*/