3 * Efficient reduction modulo sparse binary polynomials
5 * (c) 2004 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
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.
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.
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,
28 /*----- Header files ------------------------------------------------------*/
30 #include <mLib/alloc.h>
31 #include <mLib/darray.h>
32 #include <mLib/macros.h>
36 #include "gfreduce-exp.h"
40 /*----- Data structures ---------------------------------------------------*/
42 DA_DECL(instr_v
, gfreduce_instr
);
44 /*----- Main code ---------------------------------------------------------*/
46 /* --- What's going on here? --- *
48 * Let's face it, @gfx_div@ sucks. It works (I hope), but it's not in any
49 * sense fast. Here, we do efficient reduction modulo sparse polynomials.
51 * Suppose we have a polynomial @X@ we're trying to reduce mod @P@. If we
52 * take the topmost nonzero word of @X@, call it @w@, then we can eliminate
53 * it by subtracting off @w P x^{k}@ for an appropriate value of @k@. The
54 * trick is in observing that if @P@ is sparse we can do this multiplication
55 * and subtraction efficiently, just by XORing appropriate shifts of @w@ into
58 * The first tricky bit is in working out when to stop. I'll use eight-bit
59 * words to demonstrate what I'm talking about.
61 * xxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
62 * 001ppppp pppppppp pppppppp pppppppp
64 * |<------------ bp ------------->|
65 * |<------------ nw --------------->|
67 * The trick of taking whole words off of @X@ stops working when there are
68 * only @nw@ words left. Then we have to mask off the bottom bits of @w@
72 /* --- @gfreduce_create@ --- *
74 * Arguments: @gfreduce *r@ = structure to fill in
75 * @mp *x@ = a (hopefully sparse) polynomial
79 * Use: Initializes a context structure for reduction.
82 void gfreduce_create(gfreduce
*r
, mp
*p
)
91 size_t w
, ww
, wi
, wl
, ll
, bb
;
93 /* --- Sort out the easy stuff --- */
95 d
= mp_bits(p
); assert(d
); d
--;
101 r
->mask
= MPW(((mpw
)-1) << dw
);
106 /* --- Stash a new instruction --- */
108 #define INSTR(op_, arg_) do { \
110 DA(&iv)[DA_LEN(&iv)].op = (op_); \
111 DA(&iv)[DA_LEN(&iv)].arg = (arg_); \
117 w
= (d
+ MPW_BITS
- 1)/MPW_BITS
;
123 for (i
= 0, mp_scan(&sc
, p
); mp_step(&sc
) && i
< d
; i
++) {
126 ww
= (d
- i
+ MPW_BITS
- 1)/MPW_BITS
;
129 INSTR(GFRI_STORE
, w
);
133 INSTR(GFRI_LOAD
, ww
);
135 INSTR(GFRI_LOAD
, w
- 1);
136 for (; wi
< wl
; wi
++) {
138 assert(ip
->op
== GFRI_LSL
);
140 INSTR(GFRI_LSR
, MPW_BITS
- ip
->arg
);
143 INSTR(GFRI_STORE
, w
- 1);
144 INSTR(GFRI_LOAD
, ww
);
151 INSTR(GFRI_LSL
, (bb
+ i
)%MPW_BITS
);
152 if ((bb
+ i
)%MPW_BITS
)
156 INSTR(GFRI_STORE
, w
);
160 INSTR(GFRI_LOAD
, w
- 1);
161 for (; wi
< wl
; wi
++) {
163 assert(ip
->op
== GFRI_LSL
);
165 INSTR(GFRI_LSR
, MPW_BITS
- ip
->arg
);
167 INSTR(GFRI_STORE
, w
- 1);
173 r
->iv
= xmalloc(r
->in
* sizeof(gfreduce_instr
));
175 memcpy(r
->iv
, DA(&iv
), r
->in
* sizeof(gfreduce_instr
));
179 /* --- @gfreduce_destroy@ --- *
181 * Arguments: @gfreduce *r@ = structure to free
185 * Use: Reclaims the resources from a reduction context.
188 void gfreduce_destroy(gfreduce
*r
)
194 /* --- @gfreduce_dump@ --- *
196 * Arguments: @gfreduce *r@ = structure to dump
197 * @FILE *fp@ = file to dump on
201 * Use: Dumps a reduction context.
204 void gfreduce_dump(gfreduce
*r
, FILE *fp
)
208 fprintf(fp
, "poly = "); mp_writefile(r
->p
, fp
, 16);
209 fprintf(fp
, "\n lim = %lu; mask = %lx\n",
210 (unsigned long)r
->lim
, (unsigned long)r
->mask
);
211 for (i
= 0; i
< r
->in
; i
++) {
212 static const char *opname
[] = { "load", "lsl", "lsr", "store" };
213 assert(r
->iv
[i
].op
< N(opname
));
214 fprintf(fp
, " %s %lu\n",
216 (unsigned long)r
->iv
[i
].arg
);
220 /* --- @gfreduce_do@ --- *
222 * Arguments: @gfreduce *r@ = reduction context
223 * @mp *d@ = destination
226 * Returns: Destination, @x@ reduced modulo the reduction poly.
229 static void run(const gfreduce_instr
*i
, const gfreduce_instr
*il
,
234 for (; i
< il
; i
++) {
236 case GFRI_LOAD
: w
= *(v
- i
->arg
); break;
237 case GFRI_LSL
: w
^= z
<< i
->arg
; break;
238 case GFRI_LSR
: w
^= z
>> i
->arg
; break;
239 case GFRI_STORE
: *(v
- i
->arg
) = MPW(w
); break;
245 mp
*gfreduce_do(gfreduce
*r
, mp
*d
, mp
*x
)
248 const gfreduce_instr
*il
;
251 /* --- Try to reuse the source's space --- */
255 MP_DEST(x
, MP_LEN(x
), x
->f
);
257 /* --- Do the reduction --- */
260 if (MP_LEN(x
) >= r
->lim
) {
267 run(r
->iv
, il
, vl
, z
);
271 while (*vl
& r
->mask
) {
274 run(r
->iv
, il
, vl
, z
);
285 /* --- @gfreduce_sqrt@ --- *
287 * Arguments: @gfreduce *r@ = pointer to reduction context
288 * @mp *d@ = destination
289 * @mp *x@ = some polynomial
291 * Returns: The square root of @x@ modulo @r->p@, or null.
294 mp
*gfreduce_sqrt(gfreduce
*r
, mp
*d
, mp
*x
)
297 mp
*z
, *spare
= MP_NEW
;
298 unsigned long m
= mp_bits(r
->p
) - 1;
301 for (i
= 0; i
< m
- 1; i
++) {
302 mp
*t
= gf_sqr(spare
, y
);
304 y
= gfreduce_do(r
, t
, t
);
306 z
= gf_sqr(spare
, y
);
307 z
= gfreduce_do(r
, z
, z
);
317 /* --- @gfreduce_trace@ --- *
319 * Arguments: @gfreduce *r@ = pointer to reduction context
320 * @mp *x@ = some polynomial
322 * Returns: The trace of @x@. (%$\Tr(x)=x + x^2 + \cdots + x^{2^{m-1}}$%
323 * if %$x \in \gf{2^m}$%).
326 int gfreduce_trace(gfreduce
*r
, mp
*x
)
330 unsigned long m
= mp_bits(r
->p
) - 1;
334 for (i
= 0; i
< m
- 1; i
++) {
335 mp
*t
= gf_sqr(spare
, y
);
337 y
= gfreduce_do(r
, t
, t
);
346 /* --- @gfreduce_halftrace@ --- *
348 * Arguments: @gfreduce *r@ = pointer to reduction context
349 * @mp *d@ = destination
350 * @mp *x@ = some polynomial
352 * Returns: The half-trace of @x@.
353 * (%$\HfTr(x)= x + x^{2^2} + \cdots + x^{2^{m-1}}$%
354 * if %$x \in \gf{2^m}$% with %$m$% odd).
357 mp
*gfreduce_halftrace(gfreduce
*r
, mp
*d
, mp
*x
)
361 unsigned long m
= mp_bits(r
->p
) - 1;
365 for (i
= 0; i
< m
- 1; i
+= 2) {
366 mp
*t
= gf_sqr(spare
, y
);
368 y
= gfreduce_do(r
, t
, t
);
369 t
= gf_sqr(spare
, y
);
371 y
= gfreduce_do(r
, t
, t
);
378 /* --- @gfreduce_quadsolve@ --- *
380 * Arguments: @gfreduce *r@ = pointer to reduction context
381 * @mp *d@ = destination
382 * @mp *x@ = some polynomial
384 * Returns: A polynomial @y@ such that %$y^2 + y = x$%, or null.
387 mp
*gfreduce_quadsolve(gfreduce
*r
, mp
*d
, mp
*x
)
389 unsigned long m
= mp_bits(r
->p
) - 1;
394 d
= gfreduce_halftrace(r
, d
, x
);
396 mp
*z
, *w
, *rho
= MP_NEW
;
398 grand
*fr
= fibrand_create(0);
402 rho
= mprand(rho
, m
, fr
, 0);
405 for (i
= 0; i
< m
- 1; i
++) {
406 t
= gf_sqr(spare
, z
); spare
= z
; z
= gfreduce_do(r
, t
, t
);
407 t
= gf_sqr(spare
, w
); spare
= w
; w
= gfreduce_do(r
, t
, t
);
408 t
= gf_mul(spare
, w
, x
); t
= gfreduce_do(r
, t
, t
); spare
= t
;
410 w
= gf_add(w
, w
, rho
);
421 fr
->ops
->destroy(fr
);
425 t
= gf_sqr(MP_NEW
, d
); t
= gfreduce_do(r
, t
, t
); t
= gf_add(t
, t
, d
);
432 if (d
) d
->v
[0] &= ~(mpw
)1;
436 /* --- @gfreduce_exp@ --- *
438 * Arguments: @gfreduce *gr@ = pointer to reduction context
439 * @mp *d@ = fake destination
443 * Returns: Result, %$a^e \bmod m$%.
446 mp
*gfreduce_exp(gfreduce
*gr
, mp
*d
, mp
*a
, mp
*e
)
449 mp
*spare
= (e
->f
& MP_BURN
) ? MP_NEWSEC
: MP_NEW
;
457 a
= gf_modinv(a
, a
, gr
->p
);
458 if (MP_LEN(e
) < EXP_THRESH
)
469 /*----- Test rig ----------------------------------------------------------*/
473 #define MP(x) mp_readstring(MP_NEW, #x, 0, 0)
475 static int vreduce(dstr
*v
)
477 mp
*d
= *(mp
**)v
[0].buf
;
478 mp
*n
= *(mp
**)v
[1].buf
;
479 mp
*r
= *(mp
**)v
[2].buf
;
484 gfreduce_create(&rr
, d
);
485 c
= gfreduce_do(&rr
, MP_NEW
, n
);
487 fprintf(stderr
, "\n*** reduction failed\n*** ");
488 gfreduce_dump(&rr
, stderr
);
489 fprintf(stderr
, "\n*** n = "); mp_writefile(n
, stderr
, 16);
490 fprintf(stderr
, "\n*** r = "); mp_writefile(r
, stderr
, 16);
491 fprintf(stderr
, "\n*** c = "); mp_writefile(c
, stderr
, 16);
492 fprintf(stderr
, "\n");
495 gfreduce_destroy(&rr
);
496 mp_drop(n
); mp_drop(d
); mp_drop(r
); mp_drop(c
);
497 assert(mparena_count(MPARENA_GLOBAL
) == 0);
501 static int vmodexp(dstr
*v
)
503 mp
*p
= *(mp
**)v
[0].buf
;
504 mp
*g
= *(mp
**)v
[1].buf
;
505 mp
*x
= *(mp
**)v
[2].buf
;
506 mp
*r
= *(mp
**)v
[3].buf
;
511 gfreduce_create(&rr
, p
);
512 c
= gfreduce_exp(&rr
, MP_NEW
, g
, x
);
514 fprintf(stderr
, "\n*** modexp failed\n*** ");
515 fprintf(stderr
, "\n*** p = "); mp_writefile(p
, stderr
, 16);
516 fprintf(stderr
, "\n*** g = "); mp_writefile(g
, stderr
, 16);
517 fprintf(stderr
, "\n*** x = "); mp_writefile(x
, stderr
, 16);
518 fprintf(stderr
, "\n*** c = "); mp_writefile(c
, stderr
, 16);
519 fprintf(stderr
, "\n*** r = "); mp_writefile(r
, stderr
, 16);
520 fprintf(stderr
, "\n");
523 gfreduce_destroy(&rr
);
524 mp_drop(p
); mp_drop(g
); mp_drop(r
); mp_drop(x
); mp_drop(c
);
525 assert(mparena_count(MPARENA_GLOBAL
) == 0);
529 static int vsqrt(dstr
*v
)
531 mp
*p
= *(mp
**)v
[0].buf
;
532 mp
*x
= *(mp
**)v
[1].buf
;
533 mp
*r
= *(mp
**)v
[2].buf
;
538 gfreduce_create(&rr
, p
);
539 c
= gfreduce_sqrt(&rr
, MP_NEW
, x
);
541 fprintf(stderr
, "\n*** sqrt failed\n*** ");
542 fprintf(stderr
, "\n*** p = "); mp_writefile(p
, stderr
, 16);
543 fprintf(stderr
, "\n*** x = "); mp_writefile(x
, stderr
, 16);
544 fprintf(stderr
, "\n*** c = "); mp_writefile(c
, stderr
, 16);
545 fprintf(stderr
, "\n*** r = "); mp_writefile(r
, stderr
, 16);
546 fprintf(stderr
, "\n");
549 gfreduce_destroy(&rr
);
550 mp_drop(p
); mp_drop(r
); mp_drop(x
); mp_drop(c
);
551 assert(mparena_count(MPARENA_GLOBAL
) == 0);
555 static int vtr(dstr
*v
)
557 mp
*p
= *(mp
**)v
[0].buf
;
558 mp
*x
= *(mp
**)v
[1].buf
;
559 int r
= *(int *)v
[2].buf
, c
;
563 gfreduce_create(&rr
, p
);
564 c
= gfreduce_trace(&rr
, x
);
566 fprintf(stderr
, "\n*** trace failed\n*** ");
567 fprintf(stderr
, "\n*** p = "); mp_writefile(p
, stderr
, 16);
568 fprintf(stderr
, "\n*** x = "); mp_writefile(x
, stderr
, 16);
569 fprintf(stderr
, "\n*** c = %d", c
);
570 fprintf(stderr
, "\n*** r = %d", r
);
571 fprintf(stderr
, "\n");
574 gfreduce_destroy(&rr
);
575 mp_drop(p
); mp_drop(x
);
576 assert(mparena_count(MPARENA_GLOBAL
) == 0);
580 static int vhftr(dstr
*v
)
582 mp
*p
= *(mp
**)v
[0].buf
;
583 mp
*x
= *(mp
**)v
[1].buf
;
584 mp
*r
= *(mp
**)v
[2].buf
;
589 gfreduce_create(&rr
, p
);
590 c
= gfreduce_halftrace(&rr
, MP_NEW
, x
);
592 fprintf(stderr
, "\n*** halftrace failed\n*** ");
593 fprintf(stderr
, "\n*** p = "); mp_writefile(p
, stderr
, 16);
594 fprintf(stderr
, "\n*** x = "); mp_writefile(x
, stderr
, 16);
595 fprintf(stderr
, "\n*** c = "); mp_writefile(c
, stderr
, 16);
596 fprintf(stderr
, "\n*** r = "); mp_writefile(r
, stderr
, 16);
597 fprintf(stderr
, "\n");
600 gfreduce_destroy(&rr
);
601 mp_drop(p
); mp_drop(r
); mp_drop(x
); mp_drop(c
);
602 assert(mparena_count(MPARENA_GLOBAL
) == 0);
606 static int vquad(dstr
*v
)
608 mp
*p
= *(mp
**)v
[0].buf
;
609 mp
*x
= *(mp
**)v
[1].buf
;
610 mp
*r
= *(mp
**)v
[2].buf
;
615 gfreduce_create(&rr
, p
);
616 c
= gfreduce_quadsolve(&rr
, MP_NEW
, x
);
618 fprintf(stderr
, "\n*** quadsolve failed\n*** ");
619 fprintf(stderr
, "\n*** p = "); mp_writefile(p
, stderr
, 16);
620 fprintf(stderr
, "\n*** x = "); mp_writefile(x
, stderr
, 16);
621 fprintf(stderr
, "\n*** c = "); mp_writefile(c
, stderr
, 16);
622 fprintf(stderr
, "\n*** r = "); mp_writefile(r
, stderr
, 16);
623 fprintf(stderr
, "\n");
626 gfreduce_destroy(&rr
);
627 mp_drop(p
); mp_drop(r
); mp_drop(x
); mp_drop(c
);
628 assert(mparena_count(MPARENA_GLOBAL
) == 0);
632 static test_chunk defs
[] = {
633 { "reduce", vreduce
, { &type_mp
, &type_mp
, &type_mp
, 0 } },
634 { "modexp", vmodexp
, { &type_mp
, &type_mp
, &type_mp
, &type_mp
, 0 } },
635 { "sqrt", vsqrt
, { &type_mp
, &type_mp
, &type_mp
, 0 } },
636 { "trace", vtr
, { &type_mp
, &type_mp
, &type_int
, 0 } },
637 { "halftrace", vhftr
, { &type_mp
, &type_mp
, &type_mp
, 0 } },
638 { "quadsolve", vquad
, { &type_mp
, &type_mp
, &type_mp
, 0 } },
642 int main(int argc
, char *argv
[])
644 test_run(argc
, argv
, defs
, SRCDIR
"/t/gfreduce");
650 /*----- That's all, folks -------------------------------------------------*/