Elliptic curves on binary fields work.
[u/mdw/catacomb] / gfreduce.c
1 /* -*-c-*-
2 *
3 * $Id: gfreduce.c,v 1.1.2.1 2004/03/21 22:39:46 mdw Exp $
4 *
5 * Efficient reduction modulo sparse binary polynomials
6 *
7 * (c) 2004 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: gfreduce.c,v $
33 * Revision 1.1.2.1 2004/03/21 22:39:46 mdw
34 * Elliptic curves on binary fields work.
35 *
36 */
37
38 /*----- Header files ------------------------------------------------------*/
39
40 #include <mLib/alloc.h>
41 #include <mLib/darray.h>
42 #include <mLib/macros.h>
43
44 #include "gf.h"
45 #include "gfreduce.h"
46 #include "gfreduce-exp.h"
47 #include "fibrand.h"
48 #include "mprand.h"
49
50 /*----- Data structures ---------------------------------------------------*/
51
52 DA_DECL(instr_v, gfreduce_instr);
53
54 /*----- Main code ---------------------------------------------------------*/
55
56 /* --- What's going on here? --- *
57 *
58 * Let's face it, @gfx_div@ sucks. It works (I hope), but it's not in any
59 * sense fast. Here, we do efficient reduction modulo sparse polynomials.
60 *
61 * Suppose we have a polynomial @X@ we're trying to reduce mod @P@. If we
62 * take the topmost nonzero word of @X@, call it @w@, then we can eliminate
63 * it by subtracting off @w P x^{k}@ for an appropriate value of @k@. The
64 * trick is in observing that if @P@ is sparse we can do this multiplication
65 * and subtraction efficiently, just by XORing appropriate shifts of @w@ into
66 * @X@.
67 *
68 * The first tricky bit is in working out when to stop. I'll use eight-bit
69 * words to demonstrate what I'm talking about.
70 *
71 * xxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
72 * 001ppppp pppppppp pppppppp pppppppp
73 * |<rp>|
74 * |<------------ bp ------------->|
75 * |<------------ nw --------------->|
76 *
77 * The trick of taking whole words off of @X@ stops working when there are
78 * only @nw@ words left. Then we have to mask off the bottom bits of @w@
79 * before continuing.
80 */
81
82 /* --- @gfreduce_create@ --- *
83 *
84 * Arguments: @gfreduce *r@ = structure to fill in
85 * @mp *x@ = a (hopefully sparse) polynomial
86 *
87 * Returns: ---
88 *
89 * Use: Initializes a context structure for reduction.
90 */
91
92 void gfreduce_create(gfreduce *r, mp *p)
93 {
94 instr_v iv = DA_INIT;
95 unsigned long d, dw;
96 mpscan sc;
97 unsigned long i;
98 gfreduce_instr *ip;
99 unsigned f = 0;
100 size_t w, ww, wi, wl, ll;
101
102 /* --- Sort out the easy stuff --- */
103
104 d = mp_bits(p); assert(d); d--;
105 r->lim = d/MPW_BITS;
106 dw = d%MPW_BITS;
107 if (!dw)
108 r->mask = 0;
109 else {
110 r->mask = MPW(((mpw)-1) << dw);
111 r->lim++;
112 }
113 r->p = mp_copy(p);
114
115 /* --- Stash a new instruction --- */
116
117 #define INSTR(op_, arg_) do { \
118 DA_ENSURE(&iv, 1); \
119 DA(&iv)[DA_LEN(&iv)].op = (op_); \
120 DA(&iv)[DA_LEN(&iv)].arg = (arg_); \
121 DA_EXTEND(&iv, 1); \
122 } while (0)
123
124 #define f_lsr 1u
125
126 w = (d + MPW_BITS - 1)/MPW_BITS;
127 INSTR(GFRI_LOAD, w);
128 wi = DA_LEN(&iv);
129 f = 0;
130 ll = 0;
131 for (i = 0, mp_scan(&sc, p); mp_step(&sc) && i < d; i++) {
132 if (!mp_bit(&sc))
133 continue;
134 ww = (d - i + MPW_BITS - 1)/MPW_BITS;
135 if (ww != w) {
136 wl = DA_LEN(&iv);
137 INSTR(GFRI_STORE, w);
138 if (!ll)
139 ll = DA_LEN(&iv);
140 if (!(f & f_lsr))
141 INSTR(GFRI_LOAD, ww);
142 else {
143 INSTR(GFRI_LOAD, w - 1);
144 for (; wi < wl; wi++) {
145 ip = &DA(&iv)[wi];
146 assert(ip->op == GFRI_LSL);
147 if (ip->arg)
148 INSTR(GFRI_LSR, MPW_BITS - ip->arg);
149 }
150 if (w - 1 != ww) {
151 INSTR(GFRI_STORE, w - 1);
152 INSTR(GFRI_LOAD, ww);
153 }
154 f &= ~f_lsr;
155 }
156 w = ww;
157 wi = DA_LEN(&iv);
158 }
159 INSTR(GFRI_LSL, (i - d)%MPW_BITS);
160 if ((i - d)%MPW_BITS)
161 f |= f_lsr;
162 }
163 wl = DA_LEN(&iv);
164 INSTR(GFRI_STORE, w);
165 if (!ll)
166 ll = DA_LEN(&iv);
167 if (f & f_lsr) {
168 INSTR(GFRI_LOAD, w - 1);
169 for (; wi < wl; wi++) {
170 ip = &DA(&iv)[wi];
171 assert(ip->op == GFRI_LSL);
172 if (ip->arg)
173 INSTR(GFRI_LSR, MPW_BITS - ip->arg);
174 }
175 INSTR(GFRI_STORE, w - 1);
176 }
177
178 #undef INSTR
179
180 r->in = DA_LEN(&iv);
181 r->iv = xmalloc(r->in * sizeof(gfreduce_instr));
182 r->liv = r->iv + ll;
183 memcpy(r->iv, DA(&iv), r->in * sizeof(gfreduce_instr));
184 DA_DESTROY(&iv);
185 }
186
187 /* --- @gfreduce_destroy@ --- *
188 *
189 * Arguments: @gfreduce *r@ = structure to free
190 *
191 * Returns: ---
192 *
193 * Use: Reclaims the resources from a reduction context.
194 */
195
196 void gfreduce_destroy(gfreduce *r)
197 {
198 mp_drop(r->p);
199 xfree(r->iv);
200 }
201
202 /* --- @gfreduce_dump@ --- *
203 *
204 * Arguments: @gfreduce *r@ = structure to dump
205 * @FILE *fp@ = file to dump on
206 *
207 * Returns: ---
208 *
209 * Use: Dumps a reduction context.
210 */
211
212 void gfreduce_dump(gfreduce *r, FILE *fp)
213 {
214 size_t i;
215
216 fprintf(fp, "poly = "); mp_writefile(r->p, fp, 16);
217 fprintf(fp, "\n lim = %lu; mask = %lx\n",
218 (unsigned long)r->lim, (unsigned long)r->mask);
219 for (i = 0; i < r->in; i++) {
220 static const char *opname[] = { "load", "lsl", "lsr", "store" };
221 assert(r->iv[i].op < N(opname));
222 fprintf(fp, " %s %lu\n",
223 opname[r->iv[i].op],
224 (unsigned long)r->iv[i].arg);
225 }
226 }
227
228 /* --- @gfreduce_do@ --- *
229 *
230 * Arguments: @gfreduce *r@ = reduction context
231 * @mp *d@ = destination
232 * @mp *x@ = source
233 *
234 * Returns: Destination, @x@ reduced modulo the reduction poly.
235 */
236
237 static void run(const gfreduce_instr *i, const gfreduce_instr *il,
238 mpw *v, mpw z)
239 {
240 mpw w = 0;
241
242 for (; i < il; i++) {
243 switch (i->op) {
244 case GFRI_LOAD: w = *(v - i->arg); break;
245 case GFRI_LSL: w ^= z << i->arg; break;
246 case GFRI_LSR: w ^= z >> i->arg; break;
247 case GFRI_STORE: *(v - i->arg) = MPW(w); break;
248 default: abort();
249 }
250 }
251 }
252
253 mp *gfreduce_do(gfreduce *r, mp *d, mp *x)
254 {
255 mpw *v, *vl;
256 const gfreduce_instr *il;
257 mpw z;
258
259 /* --- Try to reuse the source's space --- */
260
261 MP_COPY(x);
262 if (d) MP_DROP(d);
263 MP_DEST(x, MP_LEN(x), x->f);
264
265 /* --- Do the reduction --- */
266
267 il = r->iv + r->in;
268 if (MP_LEN(x) >= r->lim) {
269 v = x->v + r->lim;
270 vl = x->vl;
271 while (vl-- > v) {
272 while (*vl) {
273 z = *vl;
274 *vl = 0;
275 run(r->iv, il, vl, z);
276 }
277 }
278 if (r->mask) {
279 while (*vl & r->mask) {
280 z = *vl & r->mask;
281 *vl &= ~r->mask;
282 run(r->iv, il, vl, z);
283 }
284 }
285 }
286
287 /* --- Done --- */
288
289 MP_SHRINK(x);
290 return (x);
291 }
292
293 /* --- @gfreduce_sqrt@ --- *
294 *
295 * Arguments: @gfreduce *r@ = pointer to reduction context
296 * @mp *d@ = destination
297 * @mp *x@ = some polynomial
298 *
299 * Returns: The square root of @x@ modulo @r->p@, or null.
300 */
301
302 mp *gfreduce_sqrt(gfreduce *r, mp *d, mp *x)
303 {
304 mp *y = MP_COPY(x);
305 mp *z, *spare = MP_NEW;
306 unsigned long m = mp_bits(r->p) - 1;
307 unsigned long i;
308
309 for (i = 0; i < m - 1; i++) {
310 mp *t = gf_sqr(spare, y);
311 spare = y;
312 y = gfreduce_do(r, t, t);
313 }
314 z = gf_sqr(spare, y);
315 z = gfreduce_do(r, z, z);
316 if (!MP_EQ(x, z)) {
317 mp_drop(y);
318 y = 0;
319 }
320 mp_drop(z);
321 mp_drop(d);
322 return (y);
323 }
324
325 /* --- @gfreduce_trace@ --- *
326 *
327 * Arguments: @gfreduce *r@ = pointer to reduction context
328 * @mp *x@ = some polynomial
329 *
330 * Returns: The trace of @x@. (%$\Tr(x)=x + x^2 + \cdots + x^{2^{m-1}}$%
331 * if %$x \in \gf{2^m}$%).
332 */
333
334 int gfreduce_trace(gfreduce *r, mp *x)
335 {
336 mp *y = MP_COPY(x);
337 mp *spare = MP_NEW;
338 unsigned long m = mp_bits(r->p) - 1;
339 unsigned long i;
340 int rc;
341
342 for (i = 0; i < m - 1; i++) {
343 mp *t = gf_sqr(spare, y);
344 spare = y;
345 y = gfreduce_do(r, t, t);
346 y = gf_add(y, y, x);
347 }
348 rc = !MP_ISZERO(y);
349 mp_drop(spare);
350 mp_drop(y);
351 return (rc);
352 }
353
354 /* --- @gfreduce_halftrace@ --- *
355 *
356 * Arguments: @gfreduce *r@ = pointer to reduction context
357 * @mp *d@ = destination
358 * @mp *x@ = some polynomial
359 *
360 * Returns: The half-trace of @x@.
361 * (%$\HfTr(x)= x + x^{2^2} + \cdots + x^{2^{m-1}}$%
362 * if %$x \in \gf{2^m}$% with %$m$% odd).
363 */
364
365 mp *gfreduce_halftrace(gfreduce *r, mp *d, mp *x)
366 {
367 mp *y = MP_COPY(x);
368 mp *spare = MP_NEW;
369 unsigned long m = mp_bits(r->p) - 1;
370 unsigned long i;
371
372 mp_drop(d);
373 for (i = 0; i < m - 1; i += 2) {
374 mp *t = gf_sqr(spare, y);
375 spare = y;
376 y = gfreduce_do(r, t, t);
377 t = gf_sqr(spare, y);
378 spare = y;
379 y = gfreduce_do(r, t, t);
380 y = gf_add(y, y, x);
381 }
382 mp_drop(spare);
383 return (y);
384 }
385
386 /* --- @gfreduce_quadsolve@ --- *
387 *
388 * Arguments: @gfreduce *r@ = pointer to reduction context
389 * @mp *d@ = destination
390 * @mp *x@ = some polynomial
391 *
392 * Returns: A polynomial @y@ such that %$y^2 + y = x$%, or null.
393 */
394
395 mp *gfreduce_quadsolve(gfreduce *r, mp *d, mp *x)
396 {
397 unsigned long m = mp_bits(r->p) - 1;
398 mp *t;
399
400 MP_COPY(x);
401 if (m & 1)
402 d = gfreduce_halftrace(r, d, x);
403 else {
404 mp *z, *w, *rho = MP_NEW;
405 mp *spare = MP_NEW;
406 grand *fr = fibrand_create(0);
407 unsigned long i;
408
409 for (;;) {
410 rho = mprand(rho, m, fr, 0);
411 z = MP_ZERO;
412 w = MP_COPY(rho);
413 for (i = 0; i < m - 1; i++) {
414 t = gf_sqr(spare, z); spare = z; z = gfreduce_do(r, t, t);
415 t = gf_sqr(spare, w); spare = w; w = gfreduce_do(r, t, t);
416 t = gf_mul(spare, w, x); t = gfreduce_do(r, t, t); spare = t;
417 z = gf_add(z, z, t);
418 w = gf_add(w, w, rho);
419 }
420 if (!MP_ISZERO(w))
421 break;
422 MP_DROP(z);
423 MP_DROP(w);
424 }
425 if (d) MP_DROP(d);
426 MP_DROP(w);
427 MP_DROP(spare);
428 MP_DROP(rho);
429 fr->ops->destroy(fr);
430 d = z;
431 }
432
433 t = gf_sqr(MP_NEW, d); t = gfreduce_do(r, t, t); t = gf_add(t, t, d);
434 if (!MP_EQ(t, x)) {
435 MP_DROP(d);
436 d = 0;
437 }
438 MP_DROP(t);
439 MP_DROP(x);
440 d->v[0] &= ~(mpw)1;
441 return (d);
442 }
443
444 /* --- @gfreduce_exp@ --- *
445 *
446 * Arguments: @gfreduce *gr@ = pointer to reduction context
447 * @mp *d@ = fake destination
448 * @mp *a@ = base
449 * @mp *e@ = exponent
450 *
451 * Returns: Result, %$a^e \bmod m$%.
452 */
453
454 mp *gfreduce_exp(gfreduce *gr, mp *d, mp *a, mp *e)
455 {
456 mp *x = MP_ONE;
457 mp *spare = (e->f & MP_BURN) ? MP_NEWSEC : MP_NEW;
458
459 MP_SHRINK(e);
460 if (!MP_LEN(e))
461 ;
462 else if (MP_LEN(e) < EXP_THRESH)
463 EXP_SIMPLE(x, a, e);
464 else
465 EXP_WINDOW(x, a, e);
466 mp_drop(d);
467 mp_drop(spare);
468 return (x);
469 }
470
471 /*----- Test rig ----------------------------------------------------------*/
472
473 #ifdef TEST_RIG
474
475 #define MP(x) mp_readstring(MP_NEW, #x, 0, 0)
476
477 static int vreduce(dstr *v)
478 {
479 mp *d = *(mp **)v[0].buf;
480 mp *n = *(mp **)v[1].buf;
481 mp *r = *(mp **)v[2].buf;
482 mp *c;
483 int ok = 1;
484 gfreduce rr;
485
486 gfreduce_create(&rr, d);
487 c = gfreduce_do(&rr, MP_NEW, n);
488 if (!MP_EQ(c, r)) {
489 fprintf(stderr, "\n*** reduction failed\n*** ");
490 gfreduce_dump(&rr, stderr);
491 fprintf(stderr, "\n*** n = "); mp_writefile(n, stderr, 16);
492 fprintf(stderr, "\n*** r = "); mp_writefile(r, stderr, 16);
493 fprintf(stderr, "\n*** c = "); mp_writefile(c, stderr, 16);
494 fprintf(stderr, "\n");
495 ok = 0;
496 }
497 gfreduce_destroy(&rr);
498 mp_drop(n); mp_drop(d); mp_drop(r); mp_drop(c);
499 assert(mparena_count(MPARENA_GLOBAL) == 0);
500 return (ok);
501 }
502
503 static int vmodexp(dstr *v)
504 {
505 mp *p = *(mp **)v[0].buf;
506 mp *g = *(mp **)v[1].buf;
507 mp *x = *(mp **)v[2].buf;
508 mp *r = *(mp **)v[3].buf;
509 mp *c;
510 int ok = 1;
511 gfreduce rr;
512
513 gfreduce_create(&rr, p);
514 c = gfreduce_exp(&rr, MP_NEW, g, x);
515 if (!MP_EQ(c, r)) {
516 fprintf(stderr, "\n*** modexp failed\n*** ");
517 fprintf(stderr, "\n*** p = "); mp_writefile(p, stderr, 16);
518 fprintf(stderr, "\n*** g = "); mp_writefile(g, stderr, 16);
519 fprintf(stderr, "\n*** x = "); mp_writefile(x, stderr, 16);
520 fprintf(stderr, "\n*** c = "); mp_writefile(c, stderr, 16);
521 fprintf(stderr, "\n*** r = "); mp_writefile(r, stderr, 16);
522 fprintf(stderr, "\n");
523 ok = 0;
524 }
525 gfreduce_destroy(&rr);
526 mp_drop(p); mp_drop(g); mp_drop(r); mp_drop(x); mp_drop(c);
527 assert(mparena_count(MPARENA_GLOBAL) == 0);
528 return (ok);
529 }
530
531 static int vsqrt(dstr *v)
532 {
533 mp *p = *(mp **)v[0].buf;
534 mp *x = *(mp **)v[1].buf;
535 mp *r = *(mp **)v[2].buf;
536 mp *c;
537 int ok = 1;
538 gfreduce rr;
539
540 gfreduce_create(&rr, p);
541 c = gfreduce_sqrt(&rr, MP_NEW, x);
542 if (!MP_EQ(c, r)) {
543 fprintf(stderr, "\n*** sqrt failed\n*** ");
544 fprintf(stderr, "\n*** p = "); mp_writefile(p, stderr, 16);
545 fprintf(stderr, "\n*** x = "); mp_writefile(x, stderr, 16);
546 fprintf(stderr, "\n*** c = "); mp_writefile(c, stderr, 16);
547 fprintf(stderr, "\n*** r = "); mp_writefile(r, stderr, 16);
548 fprintf(stderr, "\n");
549 ok = 0;
550 }
551 gfreduce_destroy(&rr);
552 mp_drop(p); mp_drop(r); mp_drop(x); mp_drop(c);
553 assert(mparena_count(MPARENA_GLOBAL) == 0);
554 return (ok);
555 }
556
557 static int vtr(dstr *v)
558 {
559 mp *p = *(mp **)v[0].buf;
560 mp *x = *(mp **)v[1].buf;
561 int r = *(int *)v[2].buf, c;
562 int ok = 1;
563 gfreduce rr;
564
565 gfreduce_create(&rr, p);
566 c = gfreduce_trace(&rr, x);
567 if (c != r) {
568 fprintf(stderr, "\n*** trace failed\n*** ");
569 fprintf(stderr, "\n*** p = "); mp_writefile(p, stderr, 16);
570 fprintf(stderr, "\n*** x = "); mp_writefile(x, stderr, 16);
571 fprintf(stderr, "\n*** c = %d", c);
572 fprintf(stderr, "\n*** r = %d", r);
573 fprintf(stderr, "\n");
574 ok = 0;
575 }
576 gfreduce_destroy(&rr);
577 mp_drop(p); mp_drop(x);
578 assert(mparena_count(MPARENA_GLOBAL) == 0);
579 return (ok);
580 }
581
582 static int vhftr(dstr *v)
583 {
584 mp *p = *(mp **)v[0].buf;
585 mp *x = *(mp **)v[1].buf;
586 mp *r = *(mp **)v[2].buf;
587 mp *c;
588 int ok = 1;
589 gfreduce rr;
590
591 gfreduce_create(&rr, p);
592 c = gfreduce_halftrace(&rr, MP_NEW, x);
593 if (!MP_EQ(c, r)) {
594 fprintf(stderr, "\n*** halftrace failed\n*** ");
595 fprintf(stderr, "\n*** p = "); mp_writefile(p, stderr, 16);
596 fprintf(stderr, "\n*** x = "); mp_writefile(x, stderr, 16);
597 fprintf(stderr, "\n*** c = "); mp_writefile(c, stderr, 16);
598 fprintf(stderr, "\n*** r = "); mp_writefile(r, stderr, 16);
599 fprintf(stderr, "\n");
600 ok = 0;
601 }
602 gfreduce_destroy(&rr);
603 mp_drop(p); mp_drop(r); mp_drop(x); mp_drop(c);
604 assert(mparena_count(MPARENA_GLOBAL) == 0);
605 return (ok);
606 }
607
608 static int vquad(dstr *v)
609 {
610 mp *p = *(mp **)v[0].buf;
611 mp *x = *(mp **)v[1].buf;
612 mp *r = *(mp **)v[2].buf;
613 mp *c;
614 int ok = 1;
615 gfreduce rr;
616
617 gfreduce_create(&rr, p);
618 c = gfreduce_quadsolve(&rr, MP_NEW, x);
619 if (!MP_EQ(c, r)) {
620 fprintf(stderr, "\n*** quadsolve failed\n*** ");
621 fprintf(stderr, "\n*** p = "); mp_writefile(p, stderr, 16);
622 fprintf(stderr, "\n*** x = "); mp_writefile(x, stderr, 16);
623 fprintf(stderr, "\n*** c = "); mp_writefile(c, stderr, 16);
624 fprintf(stderr, "\n*** r = "); mp_writefile(r, stderr, 16);
625 fprintf(stderr, "\n");
626 ok = 0;
627 }
628 gfreduce_destroy(&rr);
629 mp_drop(p); mp_drop(r); mp_drop(x); mp_drop(c);
630 assert(mparena_count(MPARENA_GLOBAL) == 0);
631 return (ok);
632 }
633
634 static test_chunk defs[] = {
635 { "reduce", vreduce, { &type_mp, &type_mp, &type_mp, 0 } },
636 { "modexp", vmodexp, { &type_mp, &type_mp, &type_mp, &type_mp, 0 } },
637 { "sqrt", vsqrt, { &type_mp, &type_mp, &type_mp, 0 } },
638 { "trace", vtr, { &type_mp, &type_mp, &type_int, 0 } },
639 { "halftrace", vhftr, { &type_mp, &type_mp, &type_mp, 0 } },
640 { "quadsolve", vquad, { &type_mp, &type_mp, &type_mp, 0 } },
641 { 0, 0, { 0 } }
642 };
643
644 int main(int argc, char *argv[])
645 {
646 test_run(argc, argv, defs, SRCDIR"/tests/gfreduce");
647 return (0);
648 }
649
650 #endif
651
652 /*----- That's all, folks -------------------------------------------------*/