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^n + p'$% where %$p' = \sum_{0\le i<n} p_i x^i$%,
53 * hopefully with only a few %$p_i \ne 0$%. We're going to compile %$p$%
54 * into a sequence of instructions which can be used to perform reduction
55 * modulo %$p$%. The important observation is that
56 * %$x^n \equiv p' \pmod p$%.
58 * Suppose we're working with %$w$%-bit words; let %$n = N w + n'$% with
59 * %$0 \le n' < w$%. Let %$u(x)$% be some arbitrary polynomial. Write
60 * %$u = z x^k + u'$% with %$\deg u' < k \ge n$%. Then a reduction step uses
61 * that %$u \equiv u' + z p' x^{k-n} \pmod p$%: the right hand side has
62 * degree %$\max \{ \deg u', k + \deg p' - n + \deg z \} < \deg u$%, so this
63 * makes progress towards a complete reduction.
65 * The compiled instruction sequence computes
66 * %$u' + z p' x^{k-n} = u' + \sum_{0\le i<n} z x^{k-n+i}$%.
69 /* --- @gfreduce_create@ --- *
71 * Arguments: @gfreduce *r@ = structure to fill in
72 * @mp *x@ = a (hopefully sparse) polynomial
76 * Use: Initializes a context structure for reduction.
80 unsigned f
; /* Flags */
81 #define f_lsr 1u /* Overflow from previous word */
82 #define f_load 2u /* Outstanding @LOAD@ */
83 #define f_fip 4u /* Final-pass offset is set */
84 instr_v iv
; /* Instruction vector */
85 size_t fip
; /* Offset for final-pass reduction */
86 size_t w
; /* Currently loaded target word */
87 size_t wi
; /* Left-shifts for current word */
88 gfreduce
*r
; /* Reduction context pointer */
91 #define INSTR(g_, op_, arg_) do { \
92 struct gen *_g = (g_); \
93 instr_v *_iv = &_g->iv; \
94 size_t _i = DA_LEN(_iv); \
97 DA(_iv)[_i].op = (op_); \
98 DA(_iv)[_i].arg = (arg_); \
102 static void emit_load(struct gen
*g
, size_t w
)
104 /* --- If this is not the low-order word then note final-pass start --- *
106 * Once we've eliminated the whole high-degree words, there will possibly
107 * remain a few high-degree bits. We can further reduce the subject
108 * polynomial by subtracting an appropriate multiple of %$p'$%, but if we
109 * do this naively we'll end up addressing `low-order' words beyond the
110 * bottom of our input. We solve this problem by storing an alternative
111 * start position for this final pass (which works because we scan bits
115 if (!(g
->f
& f_fip
) && w
< g
->r
->lim
) {
116 g
->fip
= DA_LEN(&g
->iv
);
120 /* --- Actually emit the instruction --- */
122 INSTR(g
, GFRI_LOAD
, w
);
127 static void emit_right_shifts(struct gen
*g
)
132 /* --- Close off the current word --- *
134 * If we shifted into this current word with a nonzero bit offset, then
135 * we'll also need to arrange to perform a sequence of right shifts into
136 * the following word, which we might as well do by scanning the
137 * instruction sequence (which starts at @wi@).
139 * Either way, we leave a @LOAD@ unmatched if there was one before, in the
140 * hope that callers have an easier time; @g->w@ is updated to reflect the
141 * currently open word.
148 INSTR(g
, GFRI_STORE
, g
->w
);
149 emit_load(g
, g
->w
- 1);
150 for (i
= g
->wi
; i
< wl
; i
++) {
152 assert(ip
->op
== GFRI_LSL
);
154 INSTR(g
, GFRI_LSR
, MPW_BITS
- ip
->arg
);
159 static void ensure_loaded(struct gen
*g
, size_t w
)
161 if (!(g
->f
& f_load
)) {
163 g
->wi
= DA_LEN(&g
->iv
);
164 } else if (w
!= g
->w
) {
165 emit_right_shifts(g
);
167 INSTR(g
, GFRI_STORE
, g
->w
);
170 g
->wi
= DA_LEN(&g
->iv
);
174 void gfreduce_create(gfreduce
*r
, mp
*p
)
176 struct gen g
= { 0, DA_INIT
};
183 /* --- Sort out the easy stuff --- */
186 d
= mp_bits(p
); assert(d
); d
--;
192 r
->mask
= MPW(((mpw
)-1) << dw
);
197 /* --- How this works --- *
199 * The instruction sequence is run with two ambient parameters: a pointer
200 * (usually) just past the most significant word of the polynomial to be
201 * reduced; and a word %$z$% which is the multiple of %$p'$% we are meant
204 * The sequence visits each word of the polynomial at most once. Suppose
205 * %$u = z x^{w N} + u'$%; our pointer points just past the end of %$u'$%.
206 * Word %$I$% of %$u'$% will be affected by modulus bits %$p_i$% where
207 * %$(N - I - 1) w + 1 \le i \le (N - I + 1) w - 1$%, so %$p_i$% affects
208 * word %$I = \lceil (n - i + 1)/w \rceil$% and (if %$i$% is not a multiple
209 * of %$w$%) also word %$I - 1$%.
211 * We have four instructions: @LOAD@ reads a specified word of %$u$% into an
212 * accumulator, and @STORE@ stores it back (we'll always store back to the
213 * same word we most recently read, but this isn't a requirement); and
214 * @LSL@ and @LSR@, which XOR in appropriately shifted copies of %$z$% into
215 * the accumulator. So a typical program will contain sequences of @LSR@
216 * and @LSL@ instructions sandwiched between @LOAD@/@STORE@ pairs.
218 * We do a single right-to-left pass across %$p$%.
223 for (i
= 0, mp_scan(&sc
, p
); mp_step(&sc
) && i
< d
; i
++) {
227 /* --- We've found a set bit, so work out which word it affects --- *
229 * In general, a bit affects two words: it needs to be shifted left into
230 * one, and shifted right into the next. We find the former here.
233 w
= (d
- i
+ MPW_BITS
- 1)/MPW_BITS
;
235 /* --- Concentrate on the appropriate word --- */
237 ensure_loaded(&g
, w
);
239 /* --- Accumulate a new @LSL@ instruction --- *
241 * If this was a nonzero shift, then we'll need to arrange to do right
242 * shifts into the following word.
245 INSTR(&g
, GFRI_LSL
, (bb
+ i
)%MPW_BITS
);
246 if ((bb
+ i
)%MPW_BITS
)
250 /* --- Wrapping up --- *
252 * We probably need a final @STORE@, and maybe a sequence of right shifts.
256 emit_right_shifts(&g
);
257 INSTR(&g
, GFRI_STORE
, g
.w
);
260 /* --- Copy the instruction vector.
262 * If we've not set a final-pass offset yet then now would be an excellent
263 * time. Obviously it should be right at the end, because there's nothing
264 * for a final pass to do.
267 r
->in
= DA_LEN(&g
.iv
);
268 r
->iv
= xmalloc(r
->in
* sizeof(gfreduce_instr
));
269 memcpy(r
->iv
, DA(&g
.iv
), r
->in
* sizeof(gfreduce_instr
));
271 if (!(g
.f
& f_fip
)) g
.fip
= DA_LEN(&g
.iv
);
272 r
->fiv
= r
->iv
+ g
.fip
;
283 /* --- @gfreduce_destroy@ --- *
285 * Arguments: @gfreduce *r@ = structure to free
289 * Use: Reclaims the resources from a reduction context.
292 void gfreduce_destroy(gfreduce
*r
)
298 /* --- @gfreduce_dump@ --- *
300 * Arguments: @gfreduce *r@ = structure to dump
301 * @FILE *fp@ = file to dump on
305 * Use: Dumps a reduction context.
308 void gfreduce_dump(gfreduce
*r
, FILE *fp
)
312 fprintf(fp
, "poly = "); mp_writefile(r
->p
, fp
, 16);
313 fprintf(fp
, "\n lim = %lu; mask = %lx\n",
314 (unsigned long)r
->lim
, (unsigned long)r
->mask
);
315 for (i
= 0; i
< r
->in
; i
++) {
316 static const char *opname
[] = { "load", "lsl", "lsr", "store" };
317 if (&r
->iv
[i
] == r
->fiv
)
318 fputs("final:\n", fp
);
319 assert(r
->iv
[i
].op
< N(opname
));
320 fprintf(fp
, " %s %lu\n",
322 (unsigned long)r
->iv
[i
].arg
);
324 if (&r
->iv
[i
] == r
->fiv
)
325 fputs("final:\n", fp
);
328 /* --- @gfreduce_do@ --- *
330 * Arguments: @gfreduce *r@ = reduction context
331 * @mp *d@ = destination
334 * Returns: Destination, @x@ reduced modulo the reduction poly.
337 static void run(const gfreduce_instr
*i
, const gfreduce_instr
*il
,
342 for (; i
< il
; i
++) {
344 case GFRI_LOAD
: w
= *(v
- i
->arg
); break;
345 case GFRI_LSL
: w
^= z
<< i
->arg
; break;
346 case GFRI_LSR
: w
^= z
>> i
->arg
; break;
347 case GFRI_STORE
: *(v
- i
->arg
) = MPW(w
); break;
353 mp
*gfreduce_do(gfreduce
*r
, mp
*d
, mp
*x
)
356 const gfreduce_instr
*il
;
359 /* --- Try to reuse the source's space --- */
363 MP_DEST(x
, MP_LEN(x
), x
->f
);
365 /* --- Do the reduction --- */
368 if (MP_LEN(x
) >= r
->lim
) {
375 run(r
->iv
, il
, vl
, z
);
379 while (*vl
& r
->mask
) {
382 run(r
->fiv
, il
, vl
, z
);
393 /* --- @gfreduce_sqrt@ --- *
395 * Arguments: @gfreduce *r@ = pointer to reduction context
396 * @mp *d@ = destination
397 * @mp *x@ = some polynomial
399 * Returns: The square root of @x@ modulo @r->p@, or null.
402 mp
*gfreduce_sqrt(gfreduce
*r
, mp
*d
, mp
*x
)
405 mp
*z
, *spare
= MP_NEW
;
406 unsigned long m
= mp_bits(r
->p
) - 1;
409 for (i
= 0; i
< m
- 1; i
++) {
410 mp
*t
= gf_sqr(spare
, y
);
412 y
= gfreduce_do(r
, t
, t
);
414 z
= gf_sqr(spare
, y
);
415 z
= gfreduce_do(r
, z
, z
);
425 /* --- @gfreduce_trace@ --- *
427 * Arguments: @gfreduce *r@ = pointer to reduction context
428 * @mp *x@ = some polynomial
430 * Returns: The trace of @x@. (%$\Tr(x)=x + x^2 + \cdots + x^{2^{m-1}}$%
431 * if %$x \in \gf{2^m}$%).
434 int gfreduce_trace(gfreduce
*r
, mp
*x
)
438 unsigned long m
= mp_bits(r
->p
) - 1;
442 for (i
= 0; i
< m
- 1; i
++) {
443 mp
*t
= gf_sqr(spare
, y
);
445 y
= gfreduce_do(r
, t
, t
);
454 /* --- @gfreduce_halftrace@ --- *
456 * Arguments: @gfreduce *r@ = pointer to reduction context
457 * @mp *d@ = destination
458 * @mp *x@ = some polynomial
460 * Returns: The half-trace of @x@.
461 * (%$\HfTr(x)= x + x^{2^2} + \cdots + x^{2^{m-1}}$%
462 * if %$x \in \gf{2^m}$% with %$m$% odd).
465 mp
*gfreduce_halftrace(gfreduce
*r
, mp
*d
, mp
*x
)
469 unsigned long m
= mp_bits(r
->p
) - 1;
473 for (i
= 0; i
< m
- 1; i
+= 2) {
474 mp
*t
= gf_sqr(spare
, y
);
476 y
= gfreduce_do(r
, t
, t
);
477 t
= gf_sqr(spare
, y
);
479 y
= gfreduce_do(r
, t
, t
);
486 /* --- @gfreduce_quadsolve@ --- *
488 * Arguments: @gfreduce *r@ = pointer to reduction context
489 * @mp *d@ = destination
490 * @mp *x@ = some polynomial
492 * Returns: A polynomial @y@ such that %$y^2 + y = x$%, or null.
495 mp
*gfreduce_quadsolve(gfreduce
*r
, mp
*d
, mp
*x
)
497 unsigned long m
= mp_bits(r
->p
) - 1;
502 d
= gfreduce_halftrace(r
, d
, x
);
504 mp
*z
, *w
, *rho
= MP_NEW
;
506 grand
*fr
= fibrand_create(0);
510 rho
= mprand(rho
, m
, fr
, 0);
513 for (i
= 0; i
< m
- 1; i
++) {
514 t
= gf_sqr(spare
, z
); spare
= z
; z
= gfreduce_do(r
, t
, t
);
515 t
= gf_sqr(spare
, w
); spare
= w
; w
= gfreduce_do(r
, t
, t
);
516 t
= gf_mul(spare
, w
, x
); t
= gfreduce_do(r
, t
, t
); spare
= t
;
518 w
= gf_add(w
, w
, rho
);
529 fr
->ops
->destroy(fr
);
533 t
= gf_sqr(MP_NEW
, d
); t
= gfreduce_do(r
, t
, t
); t
= gf_add(t
, t
, d
);
540 if (d
) d
->v
[0] &= ~(mpw
)1;
544 /* --- @gfreduce_exp@ --- *
546 * Arguments: @gfreduce *gr@ = pointer to reduction context
547 * @mp *d@ = fake destination
551 * Returns: Result, %$a^e \bmod m$%.
554 mp
*gfreduce_exp(gfreduce
*gr
, mp
*d
, mp
*a
, mp
*e
)
557 mp
*spare
= (e
->f
& MP_BURN
) ? MP_NEWSEC
: MP_NEW
;
565 a
= gf_modinv(a
, a
, gr
->p
);
566 if (MP_LEN(e
) < EXP_THRESH
)
577 /*----- Test rig ----------------------------------------------------------*/
581 static int vreduce(dstr
*v
)
583 mp
*d
= *(mp
**)v
[0].buf
;
584 mp
*n
= *(mp
**)v
[1].buf
;
585 mp
*r
= *(mp
**)v
[2].buf
;
590 gfreduce_create(&rr
, d
);
591 c
= gfreduce_do(&rr
, MP_NEW
, n
);
593 fprintf(stderr
, "\n*** reduction failed\n*** ");
594 gfreduce_dump(&rr
, stderr
);
595 fprintf(stderr
, "\n*** n = "); mp_writefile(n
, stderr
, 16);
596 fprintf(stderr
, "\n*** r = "); mp_writefile(r
, stderr
, 16);
597 fprintf(stderr
, "\n*** c = "); mp_writefile(c
, stderr
, 16);
598 fprintf(stderr
, "\n");
601 gfreduce_destroy(&rr
);
602 mp_drop(n
); mp_drop(d
); mp_drop(r
); mp_drop(c
);
603 assert(mparena_count(MPARENA_GLOBAL
) == 0);
607 static int vmodexp(dstr
*v
)
609 mp
*p
= *(mp
**)v
[0].buf
;
610 mp
*g
= *(mp
**)v
[1].buf
;
611 mp
*x
= *(mp
**)v
[2].buf
;
612 mp
*r
= *(mp
**)v
[3].buf
;
617 gfreduce_create(&rr
, p
);
618 c
= gfreduce_exp(&rr
, MP_NEW
, g
, x
);
620 fprintf(stderr
, "\n*** modexp failed\n*** ");
621 fprintf(stderr
, "\n*** p = "); mp_writefile(p
, stderr
, 16);
622 fprintf(stderr
, "\n*** g = "); mp_writefile(g
, stderr
, 16);
623 fprintf(stderr
, "\n*** x = "); mp_writefile(x
, stderr
, 16);
624 fprintf(stderr
, "\n*** c = "); mp_writefile(c
, stderr
, 16);
625 fprintf(stderr
, "\n*** r = "); mp_writefile(r
, stderr
, 16);
626 fprintf(stderr
, "\n");
629 gfreduce_destroy(&rr
);
630 mp_drop(p
); mp_drop(g
); mp_drop(r
); mp_drop(x
); mp_drop(c
);
631 assert(mparena_count(MPARENA_GLOBAL
) == 0);
635 static int vsqrt(dstr
*v
)
637 mp
*p
= *(mp
**)v
[0].buf
;
638 mp
*x
= *(mp
**)v
[1].buf
;
639 mp
*r
= *(mp
**)v
[2].buf
;
644 gfreduce_create(&rr
, p
);
645 c
= gfreduce_sqrt(&rr
, MP_NEW
, x
);
647 fprintf(stderr
, "\n*** sqrt failed\n*** ");
648 fprintf(stderr
, "\n*** p = "); mp_writefile(p
, stderr
, 16);
649 fprintf(stderr
, "\n*** x = "); mp_writefile(x
, stderr
, 16);
650 fprintf(stderr
, "\n*** c = "); mp_writefile(c
, stderr
, 16);
651 fprintf(stderr
, "\n*** r = "); mp_writefile(r
, stderr
, 16);
652 fprintf(stderr
, "\n");
655 gfreduce_destroy(&rr
);
656 mp_drop(p
); mp_drop(r
); mp_drop(x
); mp_drop(c
);
657 assert(mparena_count(MPARENA_GLOBAL
) == 0);
661 static int vtr(dstr
*v
)
663 mp
*p
= *(mp
**)v
[0].buf
;
664 mp
*x
= *(mp
**)v
[1].buf
;
665 int r
= *(int *)v
[2].buf
, c
;
669 gfreduce_create(&rr
, p
);
670 c
= gfreduce_trace(&rr
, x
);
672 fprintf(stderr
, "\n*** trace failed\n*** ");
673 fprintf(stderr
, "\n*** p = "); mp_writefile(p
, stderr
, 16);
674 fprintf(stderr
, "\n*** x = "); mp_writefile(x
, stderr
, 16);
675 fprintf(stderr
, "\n*** c = %d", c
);
676 fprintf(stderr
, "\n*** r = %d", r
);
677 fprintf(stderr
, "\n");
680 gfreduce_destroy(&rr
);
681 mp_drop(p
); mp_drop(x
);
682 assert(mparena_count(MPARENA_GLOBAL
) == 0);
686 static int vhftr(dstr
*v
)
688 mp
*p
= *(mp
**)v
[0].buf
;
689 mp
*x
= *(mp
**)v
[1].buf
;
690 mp
*r
= *(mp
**)v
[2].buf
;
695 gfreduce_create(&rr
, p
);
696 c
= gfreduce_halftrace(&rr
, MP_NEW
, x
);
698 fprintf(stderr
, "\n*** halftrace failed\n*** ");
699 fprintf(stderr
, "\n*** p = "); mp_writefile(p
, stderr
, 16);
700 fprintf(stderr
, "\n*** x = "); mp_writefile(x
, stderr
, 16);
701 fprintf(stderr
, "\n*** c = "); mp_writefile(c
, stderr
, 16);
702 fprintf(stderr
, "\n*** r = "); mp_writefile(r
, stderr
, 16);
703 fprintf(stderr
, "\n");
706 gfreduce_destroy(&rr
);
707 mp_drop(p
); mp_drop(r
); mp_drop(x
); mp_drop(c
);
708 assert(mparena_count(MPARENA_GLOBAL
) == 0);
712 static int vquad(dstr
*v
)
714 mp
*p
= *(mp
**)v
[0].buf
;
715 mp
*x
= *(mp
**)v
[1].buf
;
716 mp
*r
= *(mp
**)v
[2].buf
;
721 gfreduce_create(&rr
, p
);
722 c
= gfreduce_quadsolve(&rr
, MP_NEW
, x
);
724 fprintf(stderr
, "\n*** quadsolve failed\n*** ");
725 fprintf(stderr
, "\n*** p = "); mp_writefile(p
, stderr
, 16);
726 fprintf(stderr
, "\n*** x = "); mp_writefile(x
, stderr
, 16);
727 fprintf(stderr
, "\n*** c = "); mp_writefile(c
, stderr
, 16);
728 fprintf(stderr
, "\n*** r = "); mp_writefile(r
, stderr
, 16);
729 fprintf(stderr
, "\n");
732 gfreduce_destroy(&rr
);
733 mp_drop(p
); mp_drop(r
); mp_drop(x
); mp_drop(c
);
734 assert(mparena_count(MPARENA_GLOBAL
) == 0);
738 static test_chunk defs
[] = {
739 { "reduce", vreduce
, { &type_mp
, &type_mp
, &type_mp
, 0 } },
740 { "modexp", vmodexp
, { &type_mp
, &type_mp
, &type_mp
, &type_mp
, 0 } },
741 { "sqrt", vsqrt
, { &type_mp
, &type_mp
, &type_mp
, 0 } },
742 { "trace", vtr
, { &type_mp
, &type_mp
, &type_int
, 0 } },
743 { "halftrace", vhftr
, { &type_mp
, &type_mp
, &type_mp
, 0 } },
744 { "quadsolve", vquad
, { &type_mp
, &type_mp
, &type_mp
, 0 } },
748 int main(int argc
, char *argv
[])
750 test_run(argc
, argv
, defs
, SRCDIR
"/t/gfreduce");
756 /*----- That's all, folks -------------------------------------------------*/