3 * $Id: gfreduce.c,v 1.5 2004/04/08 01:36:15 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 /*----- Header files ------------------------------------------------------*/
32 #include <mLib/alloc.h>
33 #include <mLib/darray.h>
34 #include <mLib/macros.h>
38 #include "gfreduce-exp.h"
42 /*----- Data structures ---------------------------------------------------*/
44 DA_DECL(instr_v
, gfreduce_instr
);
46 /*----- Main code ---------------------------------------------------------*/
48 /* --- What's going on here? --- *
50 * Let's face it, @gfx_div@ sucks. It works (I hope), but it's not in any
51 * sense fast. Here, we do efficient reduction modulo sparse polynomials.
53 * Suppose we have a polynomial @X@ we're trying to reduce mod @P@. If we
54 * take the topmost nonzero word of @X@, call it @w@, then we can eliminate
55 * it by subtracting off @w P x^{k}@ for an appropriate value of @k@. The
56 * trick is in observing that if @P@ is sparse we can do this multiplication
57 * and subtraction efficiently, just by XORing appropriate shifts of @w@ into
60 * The first tricky bit is in working out when to stop. I'll use eight-bit
61 * words to demonstrate what I'm talking about.
63 * xxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
64 * 001ppppp pppppppp pppppppp pppppppp
66 * |<------------ bp ------------->|
67 * |<------------ nw --------------->|
69 * The trick of taking whole words off of @X@ stops working when there are
70 * only @nw@ words left. Then we have to mask off the bottom bits of @w@
74 /* --- @gfreduce_create@ --- *
76 * Arguments: @gfreduce *r@ = structure to fill in
77 * @mp *x@ = a (hopefully sparse) polynomial
81 * Use: Initializes a context structure for reduction.
84 void gfreduce_create(gfreduce
*r
, mp
*p
)
93 size_t w
, ww
, wi
, wl
, ll
;
95 /* --- Sort out the easy stuff --- */
97 d
= mp_bits(p
); assert(d
); d
--;
103 r
->mask
= MPW(((mpw
)-1) << dw
);
108 /* --- Stash a new instruction --- */
110 #define INSTR(op_, arg_) do { \
112 DA(&iv)[DA_LEN(&iv)].op = (op_); \
113 DA(&iv)[DA_LEN(&iv)].arg = (arg_); \
119 w
= (d
+ MPW_BITS
- 1)/MPW_BITS
;
124 for (i
= 0, mp_scan(&sc
, p
); mp_step(&sc
) && i
< d
; i
++) {
127 ww
= (d
- i
+ MPW_BITS
- 1)/MPW_BITS
;
130 INSTR(GFRI_STORE
, w
);
134 INSTR(GFRI_LOAD
, ww
);
136 INSTR(GFRI_LOAD
, w
- 1);
137 for (; wi
< wl
; wi
++) {
139 assert(ip
->op
== GFRI_LSL
);
141 INSTR(GFRI_LSR
, MPW_BITS
- ip
->arg
);
144 INSTR(GFRI_STORE
, w
- 1);
145 INSTR(GFRI_LOAD
, ww
);
152 INSTR(GFRI_LSL
, (MPW_BITS
+ i
- d
)%MPW_BITS
);
153 if ((MPW_BITS
+ i
- d
)%MPW_BITS
)
157 INSTR(GFRI_STORE
, w
);
161 INSTR(GFRI_LOAD
, w
- 1);
162 for (; wi
< wl
; wi
++) {
164 assert(ip
->op
== GFRI_LSL
);
166 INSTR(GFRI_LSR
, MPW_BITS
- ip
->arg
);
168 INSTR(GFRI_STORE
, w
- 1);
174 r
->iv
= xmalloc(r
->in
* sizeof(gfreduce_instr
));
176 memcpy(r
->iv
, DA(&iv
), r
->in
* sizeof(gfreduce_instr
));
180 /* --- @gfreduce_destroy@ --- *
182 * Arguments: @gfreduce *r@ = structure to free
186 * Use: Reclaims the resources from a reduction context.
189 void gfreduce_destroy(gfreduce
*r
)
195 /* --- @gfreduce_dump@ --- *
197 * Arguments: @gfreduce *r@ = structure to dump
198 * @FILE *fp@ = file to dump on
202 * Use: Dumps a reduction context.
205 void gfreduce_dump(gfreduce
*r
, FILE *fp
)
209 fprintf(fp
, "poly = "); mp_writefile(r
->p
, fp
, 16);
210 fprintf(fp
, "\n lim = %lu; mask = %lx\n",
211 (unsigned long)r
->lim
, (unsigned long)r
->mask
);
212 for (i
= 0; i
< r
->in
; i
++) {
213 static const char *opname
[] = { "load", "lsl", "lsr", "store" };
214 assert(r
->iv
[i
].op
< N(opname
));
215 fprintf(fp
, " %s %lu\n",
217 (unsigned long)r
->iv
[i
].arg
);
221 /* --- @gfreduce_do@ --- *
223 * Arguments: @gfreduce *r@ = reduction context
224 * @mp *d@ = destination
227 * Returns: Destination, @x@ reduced modulo the reduction poly.
230 static void run(const gfreduce_instr
*i
, const gfreduce_instr
*il
,
235 for (; i
< il
; i
++) {
237 case GFRI_LOAD
: w
= *(v
- i
->arg
); break;
238 case GFRI_LSL
: w
^= z
<< i
->arg
; break;
239 case GFRI_LSR
: w
^= z
>> i
->arg
; break;
240 case GFRI_STORE
: *(v
- i
->arg
) = MPW(w
); break;
246 mp
*gfreduce_do(gfreduce
*r
, mp
*d
, mp
*x
)
249 const gfreduce_instr
*il
;
252 /* --- Try to reuse the source's space --- */
256 MP_DEST(x
, MP_LEN(x
), x
->f
);
258 /* --- Do the reduction --- */
261 if (MP_LEN(x
) >= r
->lim
) {
268 run(r
->iv
, il
, vl
, z
);
272 while (*vl
& r
->mask
) {
275 run(r
->iv
, il
, vl
, z
);
286 /* --- @gfreduce_sqrt@ --- *
288 * Arguments: @gfreduce *r@ = pointer to reduction context
289 * @mp *d@ = destination
290 * @mp *x@ = some polynomial
292 * Returns: The square root of @x@ modulo @r->p@, or null.
295 mp
*gfreduce_sqrt(gfreduce
*r
, mp
*d
, mp
*x
)
298 mp
*z
, *spare
= MP_NEW
;
299 unsigned long m
= mp_bits(r
->p
) - 1;
302 for (i
= 0; i
< m
- 1; i
++) {
303 mp
*t
= gf_sqr(spare
, y
);
305 y
= gfreduce_do(r
, t
, t
);
307 z
= gf_sqr(spare
, y
);
308 z
= gfreduce_do(r
, z
, z
);
318 /* --- @gfreduce_trace@ --- *
320 * Arguments: @gfreduce *r@ = pointer to reduction context
321 * @mp *x@ = some polynomial
323 * Returns: The trace of @x@. (%$\Tr(x)=x + x^2 + \cdots + x^{2^{m-1}}$%
324 * if %$x \in \gf{2^m}$%).
327 int gfreduce_trace(gfreduce
*r
, mp
*x
)
331 unsigned long m
= mp_bits(r
->p
) - 1;
335 for (i
= 0; i
< m
- 1; i
++) {
336 mp
*t
= gf_sqr(spare
, y
);
338 y
= gfreduce_do(r
, t
, t
);
347 /* --- @gfreduce_halftrace@ --- *
349 * Arguments: @gfreduce *r@ = pointer to reduction context
350 * @mp *d@ = destination
351 * @mp *x@ = some polynomial
353 * Returns: The half-trace of @x@.
354 * (%$\HfTr(x)= x + x^{2^2} + \cdots + x^{2^{m-1}}$%
355 * if %$x \in \gf{2^m}$% with %$m$% odd).
358 mp
*gfreduce_halftrace(gfreduce
*r
, mp
*d
, mp
*x
)
362 unsigned long m
= mp_bits(r
->p
) - 1;
366 for (i
= 0; i
< m
- 1; i
+= 2) {
367 mp
*t
= gf_sqr(spare
, y
);
369 y
= gfreduce_do(r
, t
, t
);
370 t
= gf_sqr(spare
, y
);
372 y
= gfreduce_do(r
, t
, t
);
379 /* --- @gfreduce_quadsolve@ --- *
381 * Arguments: @gfreduce *r@ = pointer to reduction context
382 * @mp *d@ = destination
383 * @mp *x@ = some polynomial
385 * Returns: A polynomial @y@ such that %$y^2 + y = x$%, or null.
388 mp
*gfreduce_quadsolve(gfreduce
*r
, mp
*d
, mp
*x
)
390 unsigned long m
= mp_bits(r
->p
) - 1;
395 d
= gfreduce_halftrace(r
, d
, x
);
397 mp
*z
, *w
, *rho
= MP_NEW
;
399 grand
*fr
= fibrand_create(0);
403 rho
= mprand(rho
, m
, fr
, 0);
406 for (i
= 0; i
< m
- 1; i
++) {
407 t
= gf_sqr(spare
, z
); spare
= z
; z
= gfreduce_do(r
, t
, t
);
408 t
= gf_sqr(spare
, w
); spare
= w
; w
= gfreduce_do(r
, t
, t
);
409 t
= gf_mul(spare
, w
, x
); t
= gfreduce_do(r
, t
, t
); spare
= t
;
411 w
= gf_add(w
, w
, rho
);
422 fr
->ops
->destroy(fr
);
426 t
= gf_sqr(MP_NEW
, d
); t
= gfreduce_do(r
, t
, t
); t
= gf_add(t
, t
, d
);
433 if (d
) d
->v
[0] &= ~(mpw
)1;
437 /* --- @gfreduce_exp@ --- *
439 * Arguments: @gfreduce *gr@ = pointer to reduction context
440 * @mp *d@ = fake destination
444 * Returns: Result, %$a^e \bmod m$%.
447 mp
*gfreduce_exp(gfreduce
*gr
, mp
*d
, mp
*a
, mp
*e
)
450 mp
*spare
= (e
->f
& MP_BURN
) ? MP_NEWSEC
: MP_NEW
;
455 else if (MP_LEN(e
) < EXP_THRESH
)
464 /*----- Test rig ----------------------------------------------------------*/
468 #define MP(x) mp_readstring(MP_NEW, #x, 0, 0)
470 static int vreduce(dstr
*v
)
472 mp
*d
= *(mp
**)v
[0].buf
;
473 mp
*n
= *(mp
**)v
[1].buf
;
474 mp
*r
= *(mp
**)v
[2].buf
;
479 gfreduce_create(&rr
, d
);
480 c
= gfreduce_do(&rr
, MP_NEW
, n
);
482 fprintf(stderr
, "\n*** reduction failed\n*** ");
483 gfreduce_dump(&rr
, stderr
);
484 fprintf(stderr
, "\n*** n = "); mp_writefile(n
, stderr
, 16);
485 fprintf(stderr
, "\n*** r = "); mp_writefile(r
, stderr
, 16);
486 fprintf(stderr
, "\n*** c = "); mp_writefile(c
, stderr
, 16);
487 fprintf(stderr
, "\n");
490 gfreduce_destroy(&rr
);
491 mp_drop(n
); mp_drop(d
); mp_drop(r
); mp_drop(c
);
492 assert(mparena_count(MPARENA_GLOBAL
) == 0);
496 static int vmodexp(dstr
*v
)
498 mp
*p
= *(mp
**)v
[0].buf
;
499 mp
*g
= *(mp
**)v
[1].buf
;
500 mp
*x
= *(mp
**)v
[2].buf
;
501 mp
*r
= *(mp
**)v
[3].buf
;
506 gfreduce_create(&rr
, p
);
507 c
= gfreduce_exp(&rr
, MP_NEW
, g
, x
);
509 fprintf(stderr
, "\n*** modexp failed\n*** ");
510 fprintf(stderr
, "\n*** p = "); mp_writefile(p
, stderr
, 16);
511 fprintf(stderr
, "\n*** g = "); mp_writefile(g
, stderr
, 16);
512 fprintf(stderr
, "\n*** x = "); mp_writefile(x
, stderr
, 16);
513 fprintf(stderr
, "\n*** c = "); mp_writefile(c
, stderr
, 16);
514 fprintf(stderr
, "\n*** r = "); mp_writefile(r
, stderr
, 16);
515 fprintf(stderr
, "\n");
518 gfreduce_destroy(&rr
);
519 mp_drop(p
); mp_drop(g
); mp_drop(r
); mp_drop(x
); mp_drop(c
);
520 assert(mparena_count(MPARENA_GLOBAL
) == 0);
524 static int vsqrt(dstr
*v
)
526 mp
*p
= *(mp
**)v
[0].buf
;
527 mp
*x
= *(mp
**)v
[1].buf
;
528 mp
*r
= *(mp
**)v
[2].buf
;
533 gfreduce_create(&rr
, p
);
534 c
= gfreduce_sqrt(&rr
, MP_NEW
, x
);
536 fprintf(stderr
, "\n*** sqrt failed\n*** ");
537 fprintf(stderr
, "\n*** p = "); mp_writefile(p
, stderr
, 16);
538 fprintf(stderr
, "\n*** x = "); mp_writefile(x
, stderr
, 16);
539 fprintf(stderr
, "\n*** c = "); mp_writefile(c
, stderr
, 16);
540 fprintf(stderr
, "\n*** r = "); mp_writefile(r
, stderr
, 16);
541 fprintf(stderr
, "\n");
544 gfreduce_destroy(&rr
);
545 mp_drop(p
); mp_drop(r
); mp_drop(x
); mp_drop(c
);
546 assert(mparena_count(MPARENA_GLOBAL
) == 0);
550 static int vtr(dstr
*v
)
552 mp
*p
= *(mp
**)v
[0].buf
;
553 mp
*x
= *(mp
**)v
[1].buf
;
554 int r
= *(int *)v
[2].buf
, c
;
558 gfreduce_create(&rr
, p
);
559 c
= gfreduce_trace(&rr
, x
);
561 fprintf(stderr
, "\n*** trace failed\n*** ");
562 fprintf(stderr
, "\n*** p = "); mp_writefile(p
, stderr
, 16);
563 fprintf(stderr
, "\n*** x = "); mp_writefile(x
, stderr
, 16);
564 fprintf(stderr
, "\n*** c = %d", c
);
565 fprintf(stderr
, "\n*** r = %d", r
);
566 fprintf(stderr
, "\n");
569 gfreduce_destroy(&rr
);
570 mp_drop(p
); mp_drop(x
);
571 assert(mparena_count(MPARENA_GLOBAL
) == 0);
575 static int vhftr(dstr
*v
)
577 mp
*p
= *(mp
**)v
[0].buf
;
578 mp
*x
= *(mp
**)v
[1].buf
;
579 mp
*r
= *(mp
**)v
[2].buf
;
584 gfreduce_create(&rr
, p
);
585 c
= gfreduce_halftrace(&rr
, MP_NEW
, x
);
587 fprintf(stderr
, "\n*** halftrace failed\n*** ");
588 fprintf(stderr
, "\n*** p = "); mp_writefile(p
, stderr
, 16);
589 fprintf(stderr
, "\n*** x = "); mp_writefile(x
, stderr
, 16);
590 fprintf(stderr
, "\n*** c = "); mp_writefile(c
, stderr
, 16);
591 fprintf(stderr
, "\n*** r = "); mp_writefile(r
, stderr
, 16);
592 fprintf(stderr
, "\n");
595 gfreduce_destroy(&rr
);
596 mp_drop(p
); mp_drop(r
); mp_drop(x
); mp_drop(c
);
597 assert(mparena_count(MPARENA_GLOBAL
) == 0);
601 static int vquad(dstr
*v
)
603 mp
*p
= *(mp
**)v
[0].buf
;
604 mp
*x
= *(mp
**)v
[1].buf
;
605 mp
*r
= *(mp
**)v
[2].buf
;
610 gfreduce_create(&rr
, p
);
611 c
= gfreduce_quadsolve(&rr
, MP_NEW
, x
);
613 fprintf(stderr
, "\n*** quadsolve failed\n*** ");
614 fprintf(stderr
, "\n*** p = "); mp_writefile(p
, stderr
, 16);
615 fprintf(stderr
, "\n*** x = "); mp_writefile(x
, stderr
, 16);
616 fprintf(stderr
, "\n*** c = "); mp_writefile(c
, stderr
, 16);
617 fprintf(stderr
, "\n*** r = "); mp_writefile(r
, stderr
, 16);
618 fprintf(stderr
, "\n");
621 gfreduce_destroy(&rr
);
622 mp_drop(p
); mp_drop(r
); mp_drop(x
); mp_drop(c
);
623 assert(mparena_count(MPARENA_GLOBAL
) == 0);
627 static test_chunk defs
[] = {
628 { "reduce", vreduce
, { &type_mp
, &type_mp
, &type_mp
, 0 } },
629 { "modexp", vmodexp
, { &type_mp
, &type_mp
, &type_mp
, &type_mp
, 0 } },
630 { "sqrt", vsqrt
, { &type_mp
, &type_mp
, &type_mp
, 0 } },
631 { "trace", vtr
, { &type_mp
, &type_mp
, &type_int
, 0 } },
632 { "halftrace", vhftr
, { &type_mp
, &type_mp
, &type_mp
, 0 } },
633 { "quadsolve", vquad
, { &type_mp
, &type_mp
, &type_mp
, 0 } },
637 int main(int argc
, char *argv
[])
639 test_run(argc
, argv
, defs
, SRCDIR
"/tests/gfreduce");
645 /*----- That's all, folks -------------------------------------------------*/