3 * $Id: gfreduce.c,v 1.4 2004/03/27 00:04:46 mdw Exp $
5 * Efficient reduction modulo sparse binary polynomials
7 * (c) 2004 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
30 /*----- Revision history --------------------------------------------------*
32 * $Log: gfreduce.c,v $
33 * Revision 1.4 2004/03/27 00:04:46 mdw
34 * Implement efficient reduction for pleasant-looking primes.
36 * Revision 1.3 2004/03/23 15:19:32 mdw
37 * Test elliptic curves more thoroughly.
39 * Revision 1.2 2004/03/21 22:52:06 mdw
40 * Merge and close elliptic curve branch.
42 * Revision 1.1.2.1 2004/03/21 22:39:46 mdw
43 * Elliptic curves on binary fields work.
47 /*----- Header files ------------------------------------------------------*/
49 #include <mLib/alloc.h>
50 #include <mLib/darray.h>
51 #include <mLib/macros.h>
55 #include "gfreduce-exp.h"
59 /*----- Data structures ---------------------------------------------------*/
61 DA_DECL(instr_v, gfreduce_instr);
63 /*----- Main code ---------------------------------------------------------*/
65 /* --- What's going on here? --- *
67 * Let's face it, @gfx_div@ sucks. It works (I hope), but it's not in any
68 * sense fast. Here, we do efficient reduction modulo sparse polynomials.
70 * Suppose we have a polynomial @X@ we're trying to reduce mod @P@. If we
71 * take the topmost nonzero word of @X@, call it @w@, then we can eliminate
72 * it by subtracting off @w P x^{k}@ for an appropriate value of @k@. The
73 * trick is in observing that if @P@ is sparse we can do this multiplication
74 * and subtraction efficiently, just by XORing appropriate shifts of @w@ into
77 * The first tricky bit is in working out when to stop. I'll use eight-bit
78 * words to demonstrate what I'm talking about.
80 * xxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
81 * 001ppppp pppppppp pppppppp pppppppp
83 * |<------------ bp ------------->|
84 * |<------------ nw --------------->|
86 * The trick of taking whole words off of @X@ stops working when there are
87 * only @nw@ words left. Then we have to mask off the bottom bits of @w@
91 /* --- @gfreduce_create@ --- *
93 * Arguments: @gfreduce *r@ = structure to fill in
94 * @mp *x@ = a (hopefully sparse) polynomial
98 * Use: Initializes a context structure for reduction.
101 void gfreduce_create(gfreduce *r, mp *p)
103 instr_v iv = DA_INIT;
110 size_t w, ww, wi, wl, ll;
112 /* --- Sort out the easy stuff --- */
114 d = mp_bits(p); assert(d); d--;
120 r->mask = MPW(((mpw)-1) << dw);
125 /* --- Stash a new instruction --- */
127 #define INSTR(op_, arg_) do { \
129 DA(&iv)[DA_LEN(&iv)].op = (op_); \
130 DA(&iv)[DA_LEN(&iv)].arg = (arg_); \
136 w = (d + MPW_BITS - 1)/MPW_BITS;
141 for (i = 0, mp_scan(&sc, p); mp_step(&sc) && i < d; i++) {
144 ww = (d - i + MPW_BITS - 1)/MPW_BITS;
147 INSTR(GFRI_STORE, w);
151 INSTR(GFRI_LOAD, ww);
153 INSTR(GFRI_LOAD, w - 1);
154 for (; wi < wl; wi++) {
156 assert(ip->op == GFRI_LSL);
158 INSTR(GFRI_LSR, MPW_BITS - ip->arg);
161 INSTR(GFRI_STORE, w - 1);
162 INSTR(GFRI_LOAD, ww);
169 INSTR(GFRI_LSL, (MPW_BITS + i - d)%MPW_BITS);
170 if ((MPW_BITS + i - d)%MPW_BITS)
174 INSTR(GFRI_STORE, w);
178 INSTR(GFRI_LOAD, w - 1);
179 for (; wi < wl; wi++) {
181 assert(ip->op == GFRI_LSL);
183 INSTR(GFRI_LSR, MPW_BITS - ip->arg);
185 INSTR(GFRI_STORE, w - 1);
191 r->iv = xmalloc(r->in * sizeof(gfreduce_instr));
193 memcpy(r->iv, DA(&iv), r->in * sizeof(gfreduce_instr));
197 /* --- @gfreduce_destroy@ --- *
199 * Arguments: @gfreduce *r@ = structure to free
203 * Use: Reclaims the resources from a reduction context.
206 void gfreduce_destroy(gfreduce *r)
212 /* --- @gfreduce_dump@ --- *
214 * Arguments: @gfreduce *r@ = structure to dump
215 * @FILE *fp@ = file to dump on
219 * Use: Dumps a reduction context.
222 void gfreduce_dump(gfreduce *r, FILE *fp)
226 fprintf(fp, "poly = "); mp_writefile(r->p, fp, 16);
227 fprintf(fp, "\n lim = %lu; mask = %lx\n",
228 (unsigned long)r->lim, (unsigned long)r->mask);
229 for (i = 0; i < r->in; i++) {
230 static const char *opname[] = { "load", "lsl", "lsr", "store" };
231 assert(r->iv[i].op < N(opname));
232 fprintf(fp, " %s %lu\n",
234 (unsigned long)r->iv[i].arg);
238 /* --- @gfreduce_do@ --- *
240 * Arguments: @gfreduce *r@ = reduction context
241 * @mp *d@ = destination
244 * Returns: Destination, @x@ reduced modulo the reduction poly.
247 static void run(const gfreduce_instr *i, const gfreduce_instr *il,
252 for (; i < il; i++) {
254 case GFRI_LOAD: w = *(v - i->arg); break;
255 case GFRI_LSL: w ^= z << i->arg; break;
256 case GFRI_LSR: w ^= z >> i->arg; break;
257 case GFRI_STORE: *(v - i->arg) = MPW(w); break;
263 mp *gfreduce_do(gfreduce *r, mp *d, mp *x)
266 const gfreduce_instr *il;
269 /* --- Try to reuse the source's space --- */
273 MP_DEST(x, MP_LEN(x), x->f);
275 /* --- Do the reduction --- */
278 if (MP_LEN(x) >= r->lim) {
285 run(r->iv, il, vl, z);
289 while (*vl & r->mask) {
292 run(r->iv, il, vl, z);
303 /* --- @gfreduce_sqrt@ --- *
305 * Arguments: @gfreduce *r@ = pointer to reduction context
306 * @mp *d@ = destination
307 * @mp *x@ = some polynomial
309 * Returns: The square root of @x@ modulo @r->p@, or null.
312 mp *gfreduce_sqrt(gfreduce *r, mp *d, mp *x)
315 mp *z, *spare = MP_NEW;
316 unsigned long m = mp_bits(r->p) - 1;
319 for (i = 0; i < m - 1; i++) {
320 mp *t = gf_sqr(spare, y);
322 y = gfreduce_do(r, t, t);
324 z = gf_sqr(spare, y);
325 z = gfreduce_do(r, z, z);
335 /* --- @gfreduce_trace@ --- *
337 * Arguments: @gfreduce *r@ = pointer to reduction context
338 * @mp *x@ = some polynomial
340 * Returns: The trace of @x@. (%$\Tr(x)=x + x^2 + \cdots + x^{2^{m-1}}$%
341 * if %$x \in \gf{2^m}$%).
344 int gfreduce_trace(gfreduce *r, mp *x)
348 unsigned long m = mp_bits(r->p) - 1;
352 for (i = 0; i < m - 1; i++) {
353 mp *t = gf_sqr(spare, y);
355 y = gfreduce_do(r, t, t);
364 /* --- @gfreduce_halftrace@ --- *
366 * Arguments: @gfreduce *r@ = pointer to reduction context
367 * @mp *d@ = destination
368 * @mp *x@ = some polynomial
370 * Returns: The half-trace of @x@.
371 * (%$\HfTr(x)= x + x^{2^2} + \cdots + x^{2^{m-1}}$%
372 * if %$x \in \gf{2^m}$% with %$m$% odd).
375 mp *gfreduce_halftrace(gfreduce *r, mp *d, mp *x)
379 unsigned long m = mp_bits(r->p) - 1;
383 for (i = 0; i < m - 1; i += 2) {
384 mp *t = gf_sqr(spare, y);
386 y = gfreduce_do(r, t, t);
387 t = gf_sqr(spare, y);
389 y = gfreduce_do(r, t, t);
396 /* --- @gfreduce_quadsolve@ --- *
398 * Arguments: @gfreduce *r@ = pointer to reduction context
399 * @mp *d@ = destination
400 * @mp *x@ = some polynomial
402 * Returns: A polynomial @y@ such that %$y^2 + y = x$%, or null.
405 mp *gfreduce_quadsolve(gfreduce *r, mp *d, mp *x)
407 unsigned long m = mp_bits(r->p) - 1;
412 d = gfreduce_halftrace(r, d, x);
414 mp *z, *w, *rho = MP_NEW;
416 grand *fr = fibrand_create(0);
420 rho = mprand(rho, m, fr, 0);
423 for (i = 0; i < m - 1; i++) {
424 t = gf_sqr(spare, z); spare = z; z = gfreduce_do(r, t, t);
425 t = gf_sqr(spare, w); spare = w; w = gfreduce_do(r, t, t);
426 t = gf_mul(spare, w, x); t = gfreduce_do(r, t, t); spare = t;
428 w = gf_add(w, w, rho);
439 fr->ops->destroy(fr);
443 t = gf_sqr(MP_NEW, d); t = gfreduce_do(r, t, t); t = gf_add(t, t, d);
450 if (d) d->v[0] &= ~(mpw)1;
454 /* --- @gfreduce_exp@ --- *
456 * Arguments: @gfreduce *gr@ = pointer to reduction context
457 * @mp *d@ = fake destination
461 * Returns: Result, %$a^e \bmod m$%.
464 mp *gfreduce_exp(gfreduce *gr, mp *d, mp *a, mp *e)
467 mp *spare = (e->f & MP_BURN) ? MP_NEWSEC : MP_NEW;
472 else if (MP_LEN(e) < EXP_THRESH)
481 /*----- Test rig ----------------------------------------------------------*/
485 #define MP(x) mp_readstring(MP_NEW, #x, 0, 0)
487 static int vreduce(dstr *v)
489 mp *d = *(mp **)v[0].buf;
490 mp *n = *(mp **)v[1].buf;
491 mp *r = *(mp **)v[2].buf;
496 gfreduce_create(&rr, d);
497 c = gfreduce_do(&rr, MP_NEW, n);
499 fprintf(stderr, "\n*** reduction failed\n*** ");
500 gfreduce_dump(&rr, stderr);
501 fprintf(stderr, "\n*** n = "); mp_writefile(n, stderr, 16);
502 fprintf(stderr, "\n*** r = "); mp_writefile(r, stderr, 16);
503 fprintf(stderr, "\n*** c = "); mp_writefile(c, stderr, 16);
504 fprintf(stderr, "\n");
507 gfreduce_destroy(&rr);
508 mp_drop(n); mp_drop(d); mp_drop(r); mp_drop(c);
509 assert(mparena_count(MPARENA_GLOBAL) == 0);
513 static int vmodexp(dstr *v)
515 mp *p = *(mp **)v[0].buf;
516 mp *g = *(mp **)v[1].buf;
517 mp *x = *(mp **)v[2].buf;
518 mp *r = *(mp **)v[3].buf;
523 gfreduce_create(&rr, p);
524 c = gfreduce_exp(&rr, MP_NEW, g, x);
526 fprintf(stderr, "\n*** modexp failed\n*** ");
527 fprintf(stderr, "\n*** p = "); mp_writefile(p, stderr, 16);
528 fprintf(stderr, "\n*** g = "); mp_writefile(g, stderr, 16);
529 fprintf(stderr, "\n*** x = "); mp_writefile(x, stderr, 16);
530 fprintf(stderr, "\n*** c = "); mp_writefile(c, stderr, 16);
531 fprintf(stderr, "\n*** r = "); mp_writefile(r, stderr, 16);
532 fprintf(stderr, "\n");
535 gfreduce_destroy(&rr);
536 mp_drop(p); mp_drop(g); mp_drop(r); mp_drop(x); mp_drop(c);
537 assert(mparena_count(MPARENA_GLOBAL) == 0);
541 static int vsqrt(dstr *v)
543 mp *p = *(mp **)v[0].buf;
544 mp *x = *(mp **)v[1].buf;
545 mp *r = *(mp **)v[2].buf;
550 gfreduce_create(&rr, p);
551 c = gfreduce_sqrt(&rr, MP_NEW, x);
553 fprintf(stderr, "\n*** sqrt failed\n*** ");
554 fprintf(stderr, "\n*** p = "); mp_writefile(p, stderr, 16);
555 fprintf(stderr, "\n*** x = "); mp_writefile(x, stderr, 16);
556 fprintf(stderr, "\n*** c = "); mp_writefile(c, stderr, 16);
557 fprintf(stderr, "\n*** r = "); mp_writefile(r, stderr, 16);
558 fprintf(stderr, "\n");
561 gfreduce_destroy(&rr);
562 mp_drop(p); mp_drop(r); mp_drop(x); mp_drop(c);
563 assert(mparena_count(MPARENA_GLOBAL) == 0);
567 static int vtr(dstr *v)
569 mp *p = *(mp **)v[0].buf;
570 mp *x = *(mp **)v[1].buf;
571 int r = *(int *)v[2].buf, c;
575 gfreduce_create(&rr, p);
576 c = gfreduce_trace(&rr, x);
578 fprintf(stderr, "\n*** trace failed\n*** ");
579 fprintf(stderr, "\n*** p = "); mp_writefile(p, stderr, 16);
580 fprintf(stderr, "\n*** x = "); mp_writefile(x, stderr, 16);
581 fprintf(stderr, "\n*** c = %d", c);
582 fprintf(stderr, "\n*** r = %d", r);
583 fprintf(stderr, "\n");
586 gfreduce_destroy(&rr);
587 mp_drop(p); mp_drop(x);
588 assert(mparena_count(MPARENA_GLOBAL) == 0);
592 static int vhftr(dstr *v)
594 mp *p = *(mp **)v[0].buf;
595 mp *x = *(mp **)v[1].buf;
596 mp *r = *(mp **)v[2].buf;
601 gfreduce_create(&rr, p);
602 c = gfreduce_halftrace(&rr, MP_NEW, x);
604 fprintf(stderr, "\n*** halftrace failed\n*** ");
605 fprintf(stderr, "\n*** p = "); mp_writefile(p, stderr, 16);
606 fprintf(stderr, "\n*** x = "); mp_writefile(x, stderr, 16);
607 fprintf(stderr, "\n*** c = "); mp_writefile(c, stderr, 16);
608 fprintf(stderr, "\n*** r = "); mp_writefile(r, stderr, 16);
609 fprintf(stderr, "\n");
612 gfreduce_destroy(&rr);
613 mp_drop(p); mp_drop(r); mp_drop(x); mp_drop(c);
614 assert(mparena_count(MPARENA_GLOBAL) == 0);
618 static int vquad(dstr *v)
620 mp *p = *(mp **)v[0].buf;
621 mp *x = *(mp **)v[1].buf;
622 mp *r = *(mp **)v[2].buf;
627 gfreduce_create(&rr, p);
628 c = gfreduce_quadsolve(&rr, MP_NEW, x);
630 fprintf(stderr, "\n*** quadsolve failed\n*** ");
631 fprintf(stderr, "\n*** p = "); mp_writefile(p, stderr, 16);
632 fprintf(stderr, "\n*** x = "); mp_writefile(x, stderr, 16);
633 fprintf(stderr, "\n*** c = "); mp_writefile(c, stderr, 16);
634 fprintf(stderr, "\n*** r = "); mp_writefile(r, stderr, 16);
635 fprintf(stderr, "\n");
638 gfreduce_destroy(&rr);
639 mp_drop(p); mp_drop(r); mp_drop(x); mp_drop(c);
640 assert(mparena_count(MPARENA_GLOBAL) == 0);
644 static test_chunk defs[] = {
645 { "reduce", vreduce, { &type_mp, &type_mp, &type_mp, 0 } },
646 { "modexp", vmodexp, { &type_mp, &type_mp, &type_mp, &type_mp, 0 } },
647 { "sqrt", vsqrt, { &type_mp, &type_mp, &type_mp, 0 } },
648 { "trace", vtr, { &type_mp, &type_mp, &type_int, 0 } },
649 { "halftrace", vhftr, { &type_mp, &type_mp, &type_mp, 0 } },
650 { "quadsolve", vquad, { &type_mp, &type_mp, &type_mp, 0 } },
654 int main(int argc, char *argv[])
656 test_run(argc, argv, defs, SRCDIR"/tests/gfreduce");
662 /*----- That's all, folks -------------------------------------------------*/