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 instr_v iv
; /* Instruction vector */
83 size_t w
; /* Currently loaded target word */
84 size_t wi
; /* Left-shifts for current word */
87 #define INSTR(g_, op_, arg_) do { \
88 struct gen *_g = (g_); \
89 instr_v *_iv = &_g->iv; \
90 size_t _i = DA_LEN(_iv); \
93 DA(_iv)[_i].op = (op_); \
94 DA(_iv)[_i].arg = (arg_); \
98 static void emit_load(struct gen
*g
, size_t w
)
100 INSTR(g
, GFRI_LOAD
, w
);
105 static void emit_right_shifts(struct gen
*g
)
110 /* --- Close off the current word --- *
112 * If we shifted into this current word with a nonzero bit offset, then
113 * we'll also need to arrange to perform a sequence of right shifts into
114 * the following word, which we might as well do by scanning the
115 * instruction sequence (which starts at @wi@).
117 * Either way, we leave a @LOAD@ unmatched if there was one before, in the
118 * hope that callers have an easier time; @g->w@ is updated to reflect the
119 * currently open word.
126 INSTR(g
, GFRI_STORE
, g
->w
);
127 emit_load(g
, g
->w
- 1);
128 for (i
= g
->wi
; i
< wl
; i
++) {
130 assert(ip
->op
== GFRI_LSL
);
132 INSTR(g
, GFRI_LSR
, MPW_BITS
- ip
->arg
);
137 static void ensure_loaded(struct gen
*g
, size_t w
)
139 if (!(g
->f
& f_load
)) {
141 g
->wi
= DA_LEN(&g
->iv
);
142 } else if (w
!= g
->w
) {
143 emit_right_shifts(g
);
145 INSTR(g
, GFRI_STORE
, g
->w
);
148 g
->wi
= DA_LEN(&g
->iv
);
152 void gfreduce_create(gfreduce
*r
, mp
*p
)
154 struct gen g
= { 0, DA_INIT
};
161 /* --- Sort out the easy stuff --- */
163 d
= mp_bits(p
); assert(d
); d
--;
169 r
->mask
= MPW(((mpw
)-1) << dw
);
174 /* --- How this works --- *
176 * The instruction sequence is run with two ambient parameters: a pointer
177 * (usually) just past the most significant word of the polynomial to be
178 * reduced; and a word %$z$% which is the multiple of %$p'$% we are meant
181 * The sequence visits each word of the polynomial at most once. Suppose
182 * %$u = z x^{w N} + u'$%; our pointer points just past the end of %$u'$%.
183 * Word %$I$% of %$u'$% will be affected by modulus bits %$p_i$% where
184 * %$(N - I - 1) w + 1 \le i \le (N - I + 1) w - 1$%, so %$p_i$% affects
185 * word %$I = \lceil (n - i + 1)/w \rceil$% and (if %$i$% is not a multiple
186 * of %$w$%) also word %$I - 1$%.
188 * We have four instructions: @LOAD@ reads a specified word of %$u$% into an
189 * accumulator, and @STORE@ stores it back (we'll always store back to the
190 * same word we most recently read, but this isn't a requirement); and
191 * @LSL@ and @LSR@, which XOR in appropriately shifted copies of %$z$% into
192 * the accumulator. So a typical program will contain sequences of @LSR@
193 * and @LSL@ instructions sandwiched between @LOAD@/@STORE@ pairs.
195 * We do a single right-to-left pass across %$p$%.
200 for (i
= 0, mp_scan(&sc
, p
); mp_step(&sc
) && i
< d
; i
++) {
204 /* --- We've found a set bit, so work out which word it affects --- *
206 * In general, a bit affects two words: it needs to be shifted left into
207 * one, and shifted right into the next. We find the former here.
210 w
= (d
- i
+ MPW_BITS
- 1)/MPW_BITS
;
212 /* --- Concentrate on the appropriate word --- */
214 ensure_loaded(&g
, w
);
216 /* --- Accumulate a new @LSL@ instruction --- *
218 * If this was a nonzero shift, then we'll need to arrange to do right
219 * shifts into the following word.
222 INSTR(&g
, GFRI_LSL
, (bb
+ i
)%MPW_BITS
);
223 if ((bb
+ i
)%MPW_BITS
)
227 /* --- Wrapping up --- *
229 * We probably need a final @STORE@, and maybe a sequence of right shifts.
233 emit_right_shifts(&g
);
234 INSTR(&g
, GFRI_STORE
, g
.w
);
237 r
->in
= DA_LEN(&g
.iv
);
238 r
->iv
= xmalloc(r
->in
* sizeof(gfreduce_instr
));
239 memcpy(r
->iv
, DA(&g
.iv
), r
->in
* sizeof(gfreduce_instr
));
248 /* --- @gfreduce_destroy@ --- *
250 * Arguments: @gfreduce *r@ = structure to free
254 * Use: Reclaims the resources from a reduction context.
257 void gfreduce_destroy(gfreduce
*r
)
263 /* --- @gfreduce_dump@ --- *
265 * Arguments: @gfreduce *r@ = structure to dump
266 * @FILE *fp@ = file to dump on
270 * Use: Dumps a reduction context.
273 void gfreduce_dump(gfreduce
*r
, FILE *fp
)
277 fprintf(fp
, "poly = "); mp_writefile(r
->p
, fp
, 16);
278 fprintf(fp
, "\n lim = %lu; mask = %lx\n",
279 (unsigned long)r
->lim
, (unsigned long)r
->mask
);
280 for (i
= 0; i
< r
->in
; i
++) {
281 static const char *opname
[] = { "load", "lsl", "lsr", "store" };
282 assert(r
->iv
[i
].op
< N(opname
));
283 fprintf(fp
, " %s %lu\n",
285 (unsigned long)r
->iv
[i
].arg
);
289 /* --- @gfreduce_do@ --- *
291 * Arguments: @gfreduce *r@ = reduction context
292 * @mp *d@ = destination
295 * Returns: Destination, @x@ reduced modulo the reduction poly.
298 static void run(const gfreduce_instr
*i
, const gfreduce_instr
*il
,
303 for (; i
< il
; i
++) {
305 case GFRI_LOAD
: w
= *(v
- i
->arg
); break;
306 case GFRI_LSL
: w
^= z
<< i
->arg
; break;
307 case GFRI_LSR
: w
^= z
>> i
->arg
; break;
308 case GFRI_STORE
: *(v
- i
->arg
) = MPW(w
); break;
314 mp
*gfreduce_do(gfreduce
*r
, mp
*d
, mp
*x
)
317 const gfreduce_instr
*il
;
320 /* --- Try to reuse the source's space --- */
324 MP_DEST(x
, MP_LEN(x
), x
->f
);
326 /* --- Do the reduction --- */
329 if (MP_LEN(x
) >= r
->lim
) {
336 run(r
->iv
, il
, vl
, z
);
340 while (*vl
& r
->mask
) {
343 run(r
->iv
, il
, vl
, z
);
354 /* --- @gfreduce_sqrt@ --- *
356 * Arguments: @gfreduce *r@ = pointer to reduction context
357 * @mp *d@ = destination
358 * @mp *x@ = some polynomial
360 * Returns: The square root of @x@ modulo @r->p@, or null.
363 mp
*gfreduce_sqrt(gfreduce
*r
, mp
*d
, mp
*x
)
366 mp
*z
, *spare
= MP_NEW
;
367 unsigned long m
= mp_bits(r
->p
) - 1;
370 for (i
= 0; i
< m
- 1; i
++) {
371 mp
*t
= gf_sqr(spare
, y
);
373 y
= gfreduce_do(r
, t
, t
);
375 z
= gf_sqr(spare
, y
);
376 z
= gfreduce_do(r
, z
, z
);
386 /* --- @gfreduce_trace@ --- *
388 * Arguments: @gfreduce *r@ = pointer to reduction context
389 * @mp *x@ = some polynomial
391 * Returns: The trace of @x@. (%$\Tr(x)=x + x^2 + \cdots + x^{2^{m-1}}$%
392 * if %$x \in \gf{2^m}$%).
395 int gfreduce_trace(gfreduce
*r
, mp
*x
)
399 unsigned long m
= mp_bits(r
->p
) - 1;
403 for (i
= 0; i
< m
- 1; i
++) {
404 mp
*t
= gf_sqr(spare
, y
);
406 y
= gfreduce_do(r
, t
, t
);
415 /* --- @gfreduce_halftrace@ --- *
417 * Arguments: @gfreduce *r@ = pointer to reduction context
418 * @mp *d@ = destination
419 * @mp *x@ = some polynomial
421 * Returns: The half-trace of @x@.
422 * (%$\HfTr(x)= x + x^{2^2} + \cdots + x^{2^{m-1}}$%
423 * if %$x \in \gf{2^m}$% with %$m$% odd).
426 mp
*gfreduce_halftrace(gfreduce
*r
, mp
*d
, mp
*x
)
430 unsigned long m
= mp_bits(r
->p
) - 1;
434 for (i
= 0; i
< m
- 1; i
+= 2) {
435 mp
*t
= gf_sqr(spare
, y
);
437 y
= gfreduce_do(r
, t
, t
);
438 t
= gf_sqr(spare
, y
);
440 y
= gfreduce_do(r
, t
, t
);
447 /* --- @gfreduce_quadsolve@ --- *
449 * Arguments: @gfreduce *r@ = pointer to reduction context
450 * @mp *d@ = destination
451 * @mp *x@ = some polynomial
453 * Returns: A polynomial @y@ such that %$y^2 + y = x$%, or null.
456 mp
*gfreduce_quadsolve(gfreduce
*r
, mp
*d
, mp
*x
)
458 unsigned long m
= mp_bits(r
->p
) - 1;
463 d
= gfreduce_halftrace(r
, d
, x
);
465 mp
*z
, *w
, *rho
= MP_NEW
;
467 grand
*fr
= fibrand_create(0);
471 rho
= mprand(rho
, m
, fr
, 0);
474 for (i
= 0; i
< m
- 1; i
++) {
475 t
= gf_sqr(spare
, z
); spare
= z
; z
= gfreduce_do(r
, t
, t
);
476 t
= gf_sqr(spare
, w
); spare
= w
; w
= gfreduce_do(r
, t
, t
);
477 t
= gf_mul(spare
, w
, x
); t
= gfreduce_do(r
, t
, t
); spare
= t
;
479 w
= gf_add(w
, w
, rho
);
490 fr
->ops
->destroy(fr
);
494 t
= gf_sqr(MP_NEW
, d
); t
= gfreduce_do(r
, t
, t
); t
= gf_add(t
, t
, d
);
501 if (d
) d
->v
[0] &= ~(mpw
)1;
505 /* --- @gfreduce_exp@ --- *
507 * Arguments: @gfreduce *gr@ = pointer to reduction context
508 * @mp *d@ = fake destination
512 * Returns: Result, %$a^e \bmod m$%.
515 mp
*gfreduce_exp(gfreduce
*gr
, mp
*d
, mp
*a
, mp
*e
)
518 mp
*spare
= (e
->f
& MP_BURN
) ? MP_NEWSEC
: MP_NEW
;
526 a
= gf_modinv(a
, a
, gr
->p
);
527 if (MP_LEN(e
) < EXP_THRESH
)
538 /*----- Test rig ----------------------------------------------------------*/
542 #define MP(x) mp_readstring(MP_NEW, #x, 0, 0)
544 static int vreduce(dstr
*v
)
546 mp
*d
= *(mp
**)v
[0].buf
;
547 mp
*n
= *(mp
**)v
[1].buf
;
548 mp
*r
= *(mp
**)v
[2].buf
;
553 gfreduce_create(&rr
, d
);
554 c
= gfreduce_do(&rr
, MP_NEW
, n
);
556 fprintf(stderr
, "\n*** reduction failed\n*** ");
557 gfreduce_dump(&rr
, stderr
);
558 fprintf(stderr
, "\n*** n = "); mp_writefile(n
, stderr
, 16);
559 fprintf(stderr
, "\n*** r = "); mp_writefile(r
, stderr
, 16);
560 fprintf(stderr
, "\n*** c = "); mp_writefile(c
, stderr
, 16);
561 fprintf(stderr
, "\n");
564 gfreduce_destroy(&rr
);
565 mp_drop(n
); mp_drop(d
); mp_drop(r
); mp_drop(c
);
566 assert(mparena_count(MPARENA_GLOBAL
) == 0);
570 static int vmodexp(dstr
*v
)
572 mp
*p
= *(mp
**)v
[0].buf
;
573 mp
*g
= *(mp
**)v
[1].buf
;
574 mp
*x
= *(mp
**)v
[2].buf
;
575 mp
*r
= *(mp
**)v
[3].buf
;
580 gfreduce_create(&rr
, p
);
581 c
= gfreduce_exp(&rr
, MP_NEW
, g
, x
);
583 fprintf(stderr
, "\n*** modexp failed\n*** ");
584 fprintf(stderr
, "\n*** p = "); mp_writefile(p
, stderr
, 16);
585 fprintf(stderr
, "\n*** g = "); mp_writefile(g
, stderr
, 16);
586 fprintf(stderr
, "\n*** x = "); mp_writefile(x
, stderr
, 16);
587 fprintf(stderr
, "\n*** c = "); mp_writefile(c
, stderr
, 16);
588 fprintf(stderr
, "\n*** r = "); mp_writefile(r
, stderr
, 16);
589 fprintf(stderr
, "\n");
592 gfreduce_destroy(&rr
);
593 mp_drop(p
); mp_drop(g
); mp_drop(r
); mp_drop(x
); mp_drop(c
);
594 assert(mparena_count(MPARENA_GLOBAL
) == 0);
598 static int vsqrt(dstr
*v
)
600 mp
*p
= *(mp
**)v
[0].buf
;
601 mp
*x
= *(mp
**)v
[1].buf
;
602 mp
*r
= *(mp
**)v
[2].buf
;
607 gfreduce_create(&rr
, p
);
608 c
= gfreduce_sqrt(&rr
, MP_NEW
, x
);
610 fprintf(stderr
, "\n*** sqrt failed\n*** ");
611 fprintf(stderr
, "\n*** p = "); mp_writefile(p
, stderr
, 16);
612 fprintf(stderr
, "\n*** x = "); mp_writefile(x
, stderr
, 16);
613 fprintf(stderr
, "\n*** c = "); mp_writefile(c
, stderr
, 16);
614 fprintf(stderr
, "\n*** r = "); mp_writefile(r
, stderr
, 16);
615 fprintf(stderr
, "\n");
618 gfreduce_destroy(&rr
);
619 mp_drop(p
); mp_drop(r
); mp_drop(x
); mp_drop(c
);
620 assert(mparena_count(MPARENA_GLOBAL
) == 0);
624 static int vtr(dstr
*v
)
626 mp
*p
= *(mp
**)v
[0].buf
;
627 mp
*x
= *(mp
**)v
[1].buf
;
628 int r
= *(int *)v
[2].buf
, c
;
632 gfreduce_create(&rr
, p
);
633 c
= gfreduce_trace(&rr
, x
);
635 fprintf(stderr
, "\n*** trace failed\n*** ");
636 fprintf(stderr
, "\n*** p = "); mp_writefile(p
, stderr
, 16);
637 fprintf(stderr
, "\n*** x = "); mp_writefile(x
, stderr
, 16);
638 fprintf(stderr
, "\n*** c = %d", c
);
639 fprintf(stderr
, "\n*** r = %d", r
);
640 fprintf(stderr
, "\n");
643 gfreduce_destroy(&rr
);
644 mp_drop(p
); mp_drop(x
);
645 assert(mparena_count(MPARENA_GLOBAL
) == 0);
649 static int vhftr(dstr
*v
)
651 mp
*p
= *(mp
**)v
[0].buf
;
652 mp
*x
= *(mp
**)v
[1].buf
;
653 mp
*r
= *(mp
**)v
[2].buf
;
658 gfreduce_create(&rr
, p
);
659 c
= gfreduce_halftrace(&rr
, MP_NEW
, x
);
661 fprintf(stderr
, "\n*** halftrace failed\n*** ");
662 fprintf(stderr
, "\n*** p = "); mp_writefile(p
, stderr
, 16);
663 fprintf(stderr
, "\n*** x = "); mp_writefile(x
, stderr
, 16);
664 fprintf(stderr
, "\n*** c = "); mp_writefile(c
, stderr
, 16);
665 fprintf(stderr
, "\n*** r = "); mp_writefile(r
, stderr
, 16);
666 fprintf(stderr
, "\n");
669 gfreduce_destroy(&rr
);
670 mp_drop(p
); mp_drop(r
); mp_drop(x
); mp_drop(c
);
671 assert(mparena_count(MPARENA_GLOBAL
) == 0);
675 static int vquad(dstr
*v
)
677 mp
*p
= *(mp
**)v
[0].buf
;
678 mp
*x
= *(mp
**)v
[1].buf
;
679 mp
*r
= *(mp
**)v
[2].buf
;
684 gfreduce_create(&rr
, p
);
685 c
= gfreduce_quadsolve(&rr
, MP_NEW
, x
);
687 fprintf(stderr
, "\n*** quadsolve failed\n*** ");
688 fprintf(stderr
, "\n*** p = "); mp_writefile(p
, stderr
, 16);
689 fprintf(stderr
, "\n*** x = "); mp_writefile(x
, stderr
, 16);
690 fprintf(stderr
, "\n*** c = "); mp_writefile(c
, stderr
, 16);
691 fprintf(stderr
, "\n*** r = "); mp_writefile(r
, stderr
, 16);
692 fprintf(stderr
, "\n");
695 gfreduce_destroy(&rr
);
696 mp_drop(p
); mp_drop(r
); mp_drop(x
); mp_drop(c
);
697 assert(mparena_count(MPARENA_GLOBAL
) == 0);
701 static test_chunk defs
[] = {
702 { "reduce", vreduce
, { &type_mp
, &type_mp
, &type_mp
, 0 } },
703 { "modexp", vmodexp
, { &type_mp
, &type_mp
, &type_mp
, &type_mp
, 0 } },
704 { "sqrt", vsqrt
, { &type_mp
, &type_mp
, &type_mp
, 0 } },
705 { "trace", vtr
, { &type_mp
, &type_mp
, &type_int
, 0 } },
706 { "halftrace", vhftr
, { &type_mp
, &type_mp
, &type_mp
, 0 } },
707 { "quadsolve", vquad
, { &type_mp
, &type_mp
, &type_mp
, 0 } },
711 int main(int argc
, char *argv
[])
713 test_run(argc
, argv
, defs
, SRCDIR
"/t/gfreduce");
719 /*----- That's all, folks -------------------------------------------------*/