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.
50 * (It works for arbitrary polynomials, but isn't efficient for dense ones.)
52 * Suppose that %$p(x) = x^n + p'(x) = \sum_{0\le i<n} p_i x^i$%, hopefully
53 * with only a few other %$p_i \ne 0$%. We're going to compile %$p$% into a
54 * sequence of instructions which can be used to perform reduction modulo
55 * %$p$%. The important observation is that %$x^n \equiv p' \pmod p$%.
57 * Suppose we're working with %$w$%-bit words; let %$n = N w + n'$% with
58 * %$0 \le n' < w$%. Let %$u(x)$% be some arbitrary polynomial. Write
59 * %$u = z x^k + u'$% with %$\deg u' < k \ge n$%; then a reduction step uses
60 * that %$u \equiv u' + z p' x^{k-n} \pmod p$%: the right hand side has
61 * degree %$\max \{ \deg u', k + \deg p' - n + \deg z \} < \deg u$%, so this
62 * makes progress towards a complete reduction.
64 * The compiled instruction sequence computes
65 * %$u' + z p' x^{k-n} = u' + \sum_{0\le i<n} z x^{k-n+i}$%.
68 /* --- @gfreduce_create@ --- *
70 * Arguments: @gfreduce *r@ = structure to fill in
71 * @mp *x@ = a (hopefully sparse) polynomial
75 * Use: Initializes a context structure for reduction.
79 unsigned f
; /* Flags */
80 #define f_lsr 1u /* Overflow from previous word */
81 #define f_load 2u /* Outstanding @LOAD@ */
82 #define f_fip 4u /* Final-pass offset is set */
83 instr_v iv
; /* Instruction vector */
84 size_t fip
; /* Offset for final-pass reduction */
85 size_t w
; /* Currently loaded target word */
86 size_t wi
; /* Left-shifts for current word */
87 gfreduce
*r
; /* Reduction context pointer */
90 #define INSTR(g_, op_, arg_) do { \
91 struct gen *_g = (g_); \
92 instr_v *_iv = &_g->iv; \
93 size_t _i = DA_LEN(_iv); \
96 DA(_iv)[_i].op = (op_); \
97 DA(_iv)[_i].arg = (arg_); \
101 static void emit_load(struct gen
*g
, size_t w
)
103 /* --- If this is not the low-order word then note final-pass start --- *
105 * Once we've eliminated the whole high-degree words, there will possibly
106 * remain a few high-degree bits. We can further reduce the subject
107 * polynomial by subtracting an appropriate multiple of %$p'$%, but if we
108 * do this naively we'll end up addressing `low-order' words beyond the
109 * bottom of our input. We solve this problem by storing an alternative
110 * start position for this final pass (which works because we scan bits
114 if (!(g
->f
& f_fip
) && w
< g
->r
->lim
) {
115 g
->fip
= DA_LEN(&g
->iv
);
119 /* --- Actually emit the instruction --- */
121 INSTR(g
, GFRI_LOAD
, w
);
126 static void emit_right_shifts(struct gen
*g
)
131 /* --- Close off the current word --- *
133 * If we shifted into this current word with a nonzero bit offset, then
134 * we'll also need to arrange to perform a sequence of right shifts into
135 * the following word, which we might as well do by scanning the
136 * instruction sequence (which starts at @wi@).
138 * Either way, we leave a @LOAD@ unmatched if there was one before, in the
139 * hope that callers have an easier time; @g->w@ is updated to reflect the
140 * currently open word.
147 INSTR(g
, GFRI_STORE
, g
->w
);
148 emit_load(g
, g
->w
- 1);
149 for (i
= g
->wi
; i
< wl
; i
++) {
151 assert(ip
->op
== GFRI_LSL
);
153 INSTR(g
, GFRI_LSR
, MPW_BITS
- ip
->arg
);
158 static void ensure_loaded(struct gen
*g
, size_t w
)
160 if (!(g
->f
& f_load
)) {
162 g
->wi
= DA_LEN(&g
->iv
);
163 } else if (w
!= g
->w
) {
164 emit_right_shifts(g
);
166 INSTR(g
, GFRI_STORE
, g
->w
);
169 g
->wi
= DA_LEN(&g
->iv
);
173 void gfreduce_create(gfreduce
*r
, mp
*p
)
175 struct gen g
= { 0, DA_INIT
};
182 /* --- Sort out the easy stuff --- */
185 d
= mp_bits(p
); assert(d
); d
--;
191 r
->mask
= MPW(((mpw
)-1) << dw
);
196 /* --- How this works --- *
198 * The instruction sequence is run with two ambient parameters: a pointer
199 * (usually) just past the most significant word of the polynomial to be
200 * reduced; and a word %$z$% which is the multiple of %$p'$% we are meant
203 * The sequence visits each word of the polynomial at most once. Suppose
204 * %$u = z x^{w N} + u'$%; our pointer points just past the end of %$u'$%.
205 * Word %$I$% of %$u'$% will be affected by modulus bits %$p_i$% where
206 * %$(N - I - 1) w + 1 \le i \le (N - I + 1) w - 1$%, so %$p_i$% affects
207 * word %$I = \lceil (n - i + 1)/w \rceil$% and (if %$i$% is not a multiple
208 * of %$w$%) also word %$I - 1$%.
210 * We have four instructions: @LOAD@ reads a specified word of %$u$% into an
211 * accumulator, and @STORE@ stores it back (we'll always store back to the
212 * same word we most recently read, but this isn't a requirement); and
213 * @LSL@ and @LSR@, which XOR in appropriately shifted copies of %$z$% into
214 * the accumulator. So a typical program will contain sequences of @LSR@
215 * and @LSL@ instructions sandwiched between @LOAD@/@STORE@ pairs.
217 * We do a single right-to-left pass across %$p$%.
222 for (i
= 0, mp_scan(&sc
, p
); mp_step(&sc
) && i
< d
; i
++) {
226 /* --- We've found a set bit, so work out which word it affects --- *
228 * In general, a bit affects two words: it needs to be shifted left into
229 * one, and shifted right into the next. We find the former here.
232 w
= (d
- i
+ MPW_BITS
- 1)/MPW_BITS
;
234 /* --- Concentrate on the appropriate word --- */
236 ensure_loaded(&g
, w
);
238 /* --- Accumulate a new @LSL@ instruction --- *
240 * If this was a nonzero shift, then we'll need to arrange to do right
241 * shifts into the following word.
244 INSTR(&g
, GFRI_LSL
, (bb
+ i
)%MPW_BITS
);
245 if ((bb
+ i
)%MPW_BITS
)
249 /* --- Wrapping up --- *
251 * We probably need a final @STORE@, and maybe a sequence of right shifts.
255 emit_right_shifts(&g
);
256 INSTR(&g
, GFRI_STORE
, g
.w
);
259 /* --- Copy the instruction vector.
261 * If we've not set a final-pass offset yet then now would be an excellent
262 * time. Obviously it should be right at the end, because there's nothing
263 * for a final pass to do.
266 r
->in
= DA_LEN(&g
.iv
);
267 r
->iv
= xmalloc(r
->in
* sizeof(gfreduce_instr
));
268 memcpy(r
->iv
, DA(&g
.iv
), r
->in
* sizeof(gfreduce_instr
));
270 if (!(g
.f
& f_fip
)) g
.fip
= DA_LEN(&g
.iv
);
271 r
->fiv
= r
->iv
+ g
.fip
;
282 /* --- @gfreduce_destroy@ --- *
284 * Arguments: @gfreduce *r@ = structure to free
288 * Use: Reclaims the resources from a reduction context.
291 void gfreduce_destroy(gfreduce
*r
)
297 /* --- @gfreduce_dump@ --- *
299 * Arguments: @gfreduce *r@ = structure to dump
300 * @FILE *fp@ = file to dump on
304 * Use: Dumps a reduction context.
307 void gfreduce_dump(gfreduce
*r
, FILE *fp
)
311 fprintf(fp
, "poly = "); mp_writefile(r
->p
, fp
, 16);
312 fprintf(fp
, "\n lim = %lu; mask = %lx\n",
313 (unsigned long)r
->lim
, (unsigned long)r
->mask
);
314 for (i
= 0; i
< r
->in
; i
++) {
315 static const char *opname
[] = { "load", "lsl", "lsr", "store" };
316 if (&r
->iv
[i
] == r
->fiv
)
317 fputs("final:\n", fp
);
318 assert(r
->iv
[i
].op
< N(opname
));
319 fprintf(fp
, " %s %lu\n",
321 (unsigned long)r
->iv
[i
].arg
);
323 if (&r
->iv
[i
] == r
->fiv
)
324 fputs("final:\n", fp
);
327 /* --- @gfreduce_do@ --- *
329 * Arguments: @gfreduce *r@ = reduction context
330 * @mp *d@ = destination
333 * Returns: Destination, @x@ reduced modulo the reduction poly.
336 static void run(const gfreduce_instr
*i
, const gfreduce_instr
*il
,
341 for (; i
< il
; i
++) {
343 case GFRI_LOAD
: w
= *(v
- i
->arg
); break;
344 case GFRI_LSL
: w
^= z
<< i
->arg
; break;
345 case GFRI_LSR
: w
^= z
>> i
->arg
; break;
346 case GFRI_STORE
: *(v
- i
->arg
) = MPW(w
); break;
352 mp
*gfreduce_do(gfreduce
*r
, mp
*d
, mp
*x
)
355 const gfreduce_instr
*il
;
358 /* --- Try to reuse the source's space --- */
362 MP_DEST(x
, MP_LEN(x
), x
->f
);
364 /* --- Do the reduction --- */
367 if (MP_LEN(x
) >= r
->lim
) {
374 run(r
->iv
, il
, vl
, z
);
378 while (*vl
& r
->mask
) {
381 run(r
->fiv
, il
, vl
, z
);
392 /* --- @gfreduce_sqrt@ --- *
394 * Arguments: @gfreduce *r@ = pointer to reduction context
395 * @mp *d@ = destination
396 * @mp *x@ = some polynomial
398 * Returns: The square root of @x@ modulo @r->p@, or null.
401 mp
*gfreduce_sqrt(gfreduce
*r
, mp
*d
, mp
*x
)
404 mp
*z
, *spare
= MP_NEW
;
405 unsigned long m
= mp_bits(r
->p
) - 1;
408 for (i
= 0; i
< m
- 1; i
++) {
409 mp
*t
= gf_sqr(spare
, y
);
411 y
= gfreduce_do(r
, t
, t
);
413 z
= gf_sqr(spare
, y
);
414 z
= gfreduce_do(r
, z
, z
);
424 /* --- @gfreduce_trace@ --- *
426 * Arguments: @gfreduce *r@ = pointer to reduction context
427 * @mp *x@ = some polynomial
429 * Returns: The trace of @x@. (%$\Tr(x)=x + x^2 + \cdots + x^{2^{m-1}}$%
430 * if %$x \in \gf{2^m}$%).
433 int gfreduce_trace(gfreduce
*r
, mp
*x
)
437 unsigned long m
= mp_bits(r
->p
) - 1;
441 for (i
= 0; i
< m
- 1; i
++) {
442 mp
*t
= gf_sqr(spare
, y
);
444 y
= gfreduce_do(r
, t
, t
);
453 /* --- @gfreduce_halftrace@ --- *
455 * Arguments: @gfreduce *r@ = pointer to reduction context
456 * @mp *d@ = destination
457 * @mp *x@ = some polynomial
459 * Returns: The half-trace of @x@.
460 * (%$\HfTr(x)= x + x^{2^2} + \cdots + x^{2^{m-1}}$%
461 * if %$x \in \gf{2^m}$% with %$m$% odd).
464 mp
*gfreduce_halftrace(gfreduce
*r
, mp
*d
, mp
*x
)
468 unsigned long m
= mp_bits(r
->p
) - 1;
472 for (i
= 0; i
< m
- 1; i
+= 2) {
473 mp
*t
= gf_sqr(spare
, y
);
475 y
= gfreduce_do(r
, t
, t
);
476 t
= gf_sqr(spare
, y
);
478 y
= gfreduce_do(r
, t
, t
);
485 /* --- @gfreduce_quadsolve@ --- *
487 * Arguments: @gfreduce *r@ = pointer to reduction context
488 * @mp *d@ = destination
489 * @mp *x@ = some polynomial
491 * Returns: A polynomial @y@ such that %$y^2 + y = x$%, or null.
494 mp
*gfreduce_quadsolve(gfreduce
*r
, mp
*d
, mp
*x
)
496 unsigned long m
= mp_bits(r
->p
) - 1;
501 d
= gfreduce_halftrace(r
, d
, x
);
503 mp
*z
, *w
, *rho
= MP_NEW
;
505 grand
*fr
= fibrand_create(0);
509 rho
= mprand(rho
, m
, fr
, 0);
512 for (i
= 0; i
< m
- 1; i
++) {
513 t
= gf_sqr(spare
, z
); spare
= z
; z
= gfreduce_do(r
, t
, t
);
514 t
= gf_sqr(spare
, w
); spare
= w
; w
= gfreduce_do(r
, t
, t
);
515 t
= gf_mul(spare
, w
, x
); t
= gfreduce_do(r
, t
, t
); spare
= t
;
517 w
= gf_add(w
, w
, rho
);
528 fr
->ops
->destroy(fr
);
532 t
= gf_sqr(MP_NEW
, d
); t
= gfreduce_do(r
, t
, t
); t
= gf_add(t
, t
, d
);
539 if (d
) d
->v
[0] &= ~(mpw
)1;
543 /* --- @gfreduce_exp@ --- *
545 * Arguments: @gfreduce *gr@ = pointer to reduction context
546 * @mp *d@ = fake destination
550 * Returns: Result, %$a^e \bmod m$%.
553 mp
*gfreduce_exp(gfreduce
*gr
, mp
*d
, mp
*a
, mp
*e
)
556 mp
*spare
= (e
->f
& MP_BURN
) ? MP_NEWSEC
: MP_NEW
;
564 a
= gf_modinv(a
, a
, gr
->p
);
565 if (MP_LEN(e
) < EXP_THRESH
)
576 /*----- Test rig ----------------------------------------------------------*/
580 static int vreduce(dstr
*v
)
582 mp
*d
= *(mp
**)v
[0].buf
;
583 mp
*n
= *(mp
**)v
[1].buf
;
584 mp
*r
= *(mp
**)v
[2].buf
;
589 gfreduce_create(&rr
, d
);
590 c
= gfreduce_do(&rr
, MP_NEW
, n
);
592 fprintf(stderr
, "\n*** reduction failed\n*** ");
593 gfreduce_dump(&rr
, stderr
);
594 fprintf(stderr
, "\n*** n = "); mp_writefile(n
, stderr
, 16);
595 fprintf(stderr
, "\n*** r = "); mp_writefile(r
, stderr
, 16);
596 fprintf(stderr
, "\n*** c = "); mp_writefile(c
, stderr
, 16);
597 fprintf(stderr
, "\n");
600 gfreduce_destroy(&rr
);
601 mp_drop(n
); mp_drop(d
); mp_drop(r
); mp_drop(c
);
602 assert(mparena_count(MPARENA_GLOBAL
) == 0);
606 static int vmodexp(dstr
*v
)
608 mp
*p
= *(mp
**)v
[0].buf
;
609 mp
*g
= *(mp
**)v
[1].buf
;
610 mp
*x
= *(mp
**)v
[2].buf
;
611 mp
*r
= *(mp
**)v
[3].buf
;
616 gfreduce_create(&rr
, p
);
617 c
= gfreduce_exp(&rr
, MP_NEW
, g
, x
);
619 fprintf(stderr
, "\n*** modexp failed\n*** ");
620 fprintf(stderr
, "\n*** p = "); mp_writefile(p
, stderr
, 16);
621 fprintf(stderr
, "\n*** g = "); mp_writefile(g
, 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");
628 gfreduce_destroy(&rr
);
629 mp_drop(p
); mp_drop(g
); mp_drop(r
); mp_drop(x
); mp_drop(c
);
630 assert(mparena_count(MPARENA_GLOBAL
) == 0);
634 static int vsqrt(dstr
*v
)
636 mp
*p
= *(mp
**)v
[0].buf
;
637 mp
*x
= *(mp
**)v
[1].buf
;
638 mp
*r
= *(mp
**)v
[2].buf
;
643 gfreduce_create(&rr
, p
);
644 c
= gfreduce_sqrt(&rr
, MP_NEW
, x
);
646 fprintf(stderr
, "\n*** sqrt failed\n*** ");
647 fprintf(stderr
, "\n*** p = "); mp_writefile(p
, stderr
, 16);
648 fprintf(stderr
, "\n*** x = "); mp_writefile(x
, stderr
, 16);
649 fprintf(stderr
, "\n*** c = "); mp_writefile(c
, stderr
, 16);
650 fprintf(stderr
, "\n*** r = "); mp_writefile(r
, stderr
, 16);
651 fprintf(stderr
, "\n");
654 gfreduce_destroy(&rr
);
655 mp_drop(p
); mp_drop(r
); mp_drop(x
); mp_drop(c
);
656 assert(mparena_count(MPARENA_GLOBAL
) == 0);
660 static int vtr(dstr
*v
)
662 mp
*p
= *(mp
**)v
[0].buf
;
663 mp
*x
= *(mp
**)v
[1].buf
;
664 int r
= *(int *)v
[2].buf
, c
;
668 gfreduce_create(&rr
, p
);
669 c
= gfreduce_trace(&rr
, x
);
671 fprintf(stderr
, "\n*** trace failed\n*** ");
672 fprintf(stderr
, "\n*** p = "); mp_writefile(p
, stderr
, 16);
673 fprintf(stderr
, "\n*** x = "); mp_writefile(x
, stderr
, 16);
674 fprintf(stderr
, "\n*** c = %d", c
);
675 fprintf(stderr
, "\n*** r = %d", r
);
676 fprintf(stderr
, "\n");
679 gfreduce_destroy(&rr
);
680 mp_drop(p
); mp_drop(x
);
681 assert(mparena_count(MPARENA_GLOBAL
) == 0);
685 static int vhftr(dstr
*v
)
687 mp
*p
= *(mp
**)v
[0].buf
;
688 mp
*x
= *(mp
**)v
[1].buf
;
689 mp
*r
= *(mp
**)v
[2].buf
;
694 gfreduce_create(&rr
, p
);
695 c
= gfreduce_halftrace(&rr
, MP_NEW
, x
);
697 fprintf(stderr
, "\n*** halftrace failed\n*** ");
698 fprintf(stderr
, "\n*** p = "); mp_writefile(p
, stderr
, 16);
699 fprintf(stderr
, "\n*** x = "); mp_writefile(x
, stderr
, 16);
700 fprintf(stderr
, "\n*** c = "); mp_writefile(c
, stderr
, 16);
701 fprintf(stderr
, "\n*** r = "); mp_writefile(r
, stderr
, 16);
702 fprintf(stderr
, "\n");
705 gfreduce_destroy(&rr
);
706 mp_drop(p
); mp_drop(r
); mp_drop(x
); mp_drop(c
);
707 assert(mparena_count(MPARENA_GLOBAL
) == 0);
711 static int vquad(dstr
*v
)
713 mp
*p
= *(mp
**)v
[0].buf
;
714 mp
*x
= *(mp
**)v
[1].buf
;
715 mp
*r
= *(mp
**)v
[2].buf
;
720 gfreduce_create(&rr
, p
);
721 c
= gfreduce_quadsolve(&rr
, MP_NEW
, x
);
723 fprintf(stderr
, "\n*** quadsolve failed\n*** ");
724 fprintf(stderr
, "\n*** p = "); mp_writefile(p
, stderr
, 16);
725 fprintf(stderr
, "\n*** x = "); mp_writefile(x
, stderr
, 16);
726 fprintf(stderr
, "\n*** c = "); mp_writefile(c
, stderr
, 16);
727 fprintf(stderr
, "\n*** r = "); mp_writefile(r
, stderr
, 16);
728 fprintf(stderr
, "\n");
731 gfreduce_destroy(&rr
);
732 mp_drop(p
); mp_drop(r
); mp_drop(x
); mp_drop(c
);
733 assert(mparena_count(MPARENA_GLOBAL
) == 0);
737 static test_chunk defs
[] = {
738 { "reduce", vreduce
, { &type_mp
, &type_mp
, &type_mp
, 0 } },
739 { "modexp", vmodexp
, { &type_mp
, &type_mp
, &type_mp
, &type_mp
, 0 } },
740 { "sqrt", vsqrt
, { &type_mp
, &type_mp
, &type_mp
, 0 } },
741 { "trace", vtr
, { &type_mp
, &type_mp
, &type_int
, 0 } },
742 { "halftrace", vhftr
, { &type_mp
, &type_mp
, &type_mp
, 0 } },
743 { "quadsolve", vquad
, { &type_mp
, &type_mp
, &type_mp
, 0 } },
747 int main(int argc
, char *argv
[])
749 test_run(argc
, argv
, defs
, SRCDIR
"/t/gfreduce");
755 /*----- That's all, folks -------------------------------------------------*/