3 * $Id: gfreduce.c,v 1.2 2004/03/21 22:52:06 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 /*----- Revision history --------------------------------------------------*
32 * $Log: gfreduce.c,v $
33 * Revision 1.2 2004/03/21 22:52:06 mdw
34 * Merge and close elliptic curve branch.
36 * Revision 1.1.2.1 2004/03/21 22:39:46 mdw
37 * Elliptic curves on binary fields work.
41 /*----- Header files ------------------------------------------------------*/
43 #include <mLib/alloc.h>
44 #include <mLib/darray.h>
45 #include <mLib/macros.h>
49 #include "gfreduce-exp.h"
53 /*----- Data structures ---------------------------------------------------*/
55 DA_DECL(instr_v
, gfreduce_instr
);
57 /*----- Main code ---------------------------------------------------------*/
59 /* --- What's going on here? --- *
61 * Let's face it, @gfx_div@ sucks. It works (I hope), but it's not in any
62 * sense fast. Here, we do efficient reduction modulo sparse polynomials.
64 * Suppose we have a polynomial @X@ we're trying to reduce mod @P@. If we
65 * take the topmost nonzero word of @X@, call it @w@, then we can eliminate
66 * it by subtracting off @w P x^{k}@ for an appropriate value of @k@. The
67 * trick is in observing that if @P@ is sparse we can do this multiplication
68 * and subtraction efficiently, just by XORing appropriate shifts of @w@ into
71 * The first tricky bit is in working out when to stop. I'll use eight-bit
72 * words to demonstrate what I'm talking about.
74 * xxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
75 * 001ppppp pppppppp pppppppp pppppppp
77 * |<------------ bp ------------->|
78 * |<------------ nw --------------->|
80 * The trick of taking whole words off of @X@ stops working when there are
81 * only @nw@ words left. Then we have to mask off the bottom bits of @w@
85 /* --- @gfreduce_create@ --- *
87 * Arguments: @gfreduce *r@ = structure to fill in
88 * @mp *x@ = a (hopefully sparse) polynomial
92 * Use: Initializes a context structure for reduction.
95 void gfreduce_create(gfreduce
*r
, mp
*p
)
103 size_t w
, ww
, wi
, wl
, ll
;
105 /* --- Sort out the easy stuff --- */
107 d
= mp_bits(p
); assert(d
); d
--;
113 r
->mask
= MPW(((mpw
)-1) << dw
);
118 /* --- Stash a new instruction --- */
120 #define INSTR(op_, arg_) do { \
122 DA(&iv)[DA_LEN(&iv)].op = (op_); \
123 DA(&iv)[DA_LEN(&iv)].arg = (arg_); \
129 w
= (d
+ MPW_BITS
- 1)/MPW_BITS
;
134 for (i
= 0, mp_scan(&sc
, p
); mp_step(&sc
) && i
< d
; i
++) {
137 ww
= (d
- i
+ MPW_BITS
- 1)/MPW_BITS
;
140 INSTR(GFRI_STORE
, w
);
144 INSTR(GFRI_LOAD
, ww
);
146 INSTR(GFRI_LOAD
, w
- 1);
147 for (; wi
< wl
; wi
++) {
149 assert(ip
->op
== GFRI_LSL
);
151 INSTR(GFRI_LSR
, MPW_BITS
- ip
->arg
);
154 INSTR(GFRI_STORE
, w
- 1);
155 INSTR(GFRI_LOAD
, ww
);
162 INSTR(GFRI_LSL
, (i
- d
)%MPW_BITS
);
163 if ((i
- d
)%MPW_BITS
)
167 INSTR(GFRI_STORE
, w
);
171 INSTR(GFRI_LOAD
, w
- 1);
172 for (; wi
< wl
; wi
++) {
174 assert(ip
->op
== GFRI_LSL
);
176 INSTR(GFRI_LSR
, MPW_BITS
- ip
->arg
);
178 INSTR(GFRI_STORE
, w
- 1);
184 r
->iv
= xmalloc(r
->in
* sizeof(gfreduce_instr
));
186 memcpy(r
->iv
, DA(&iv
), r
->in
* sizeof(gfreduce_instr
));
190 /* --- @gfreduce_destroy@ --- *
192 * Arguments: @gfreduce *r@ = structure to free
196 * Use: Reclaims the resources from a reduction context.
199 void gfreduce_destroy(gfreduce
*r
)
205 /* --- @gfreduce_dump@ --- *
207 * Arguments: @gfreduce *r@ = structure to dump
208 * @FILE *fp@ = file to dump on
212 * Use: Dumps a reduction context.
215 void gfreduce_dump(gfreduce
*r
, FILE *fp
)
219 fprintf(fp
, "poly = "); mp_writefile(r
->p
, fp
, 16);
220 fprintf(fp
, "\n lim = %lu; mask = %lx\n",
221 (unsigned long)r
->lim
, (unsigned long)r
->mask
);
222 for (i
= 0; i
< r
->in
; i
++) {
223 static const char *opname
[] = { "load", "lsl", "lsr", "store" };
224 assert(r
->iv
[i
].op
< N(opname
));
225 fprintf(fp
, " %s %lu\n",
227 (unsigned long)r
->iv
[i
].arg
);
231 /* --- @gfreduce_do@ --- *
233 * Arguments: @gfreduce *r@ = reduction context
234 * @mp *d@ = destination
237 * Returns: Destination, @x@ reduced modulo the reduction poly.
240 static void run(const gfreduce_instr
*i
, const gfreduce_instr
*il
,
245 for (; i
< il
; i
++) {
247 case GFRI_LOAD
: w
= *(v
- i
->arg
); break;
248 case GFRI_LSL
: w
^= z
<< i
->arg
; break;
249 case GFRI_LSR
: w
^= z
>> i
->arg
; break;
250 case GFRI_STORE
: *(v
- i
->arg
) = MPW(w
); break;
256 mp
*gfreduce_do(gfreduce
*r
, mp
*d
, mp
*x
)
259 const gfreduce_instr
*il
;
262 /* --- Try to reuse the source's space --- */
266 MP_DEST(x
, MP_LEN(x
), x
->f
);
268 /* --- Do the reduction --- */
271 if (MP_LEN(x
) >= r
->lim
) {
278 run(r
->iv
, il
, vl
, z
);
282 while (*vl
& r
->mask
) {
285 run(r
->iv
, il
, vl
, z
);
296 /* --- @gfreduce_sqrt@ --- *
298 * Arguments: @gfreduce *r@ = pointer to reduction context
299 * @mp *d@ = destination
300 * @mp *x@ = some polynomial
302 * Returns: The square root of @x@ modulo @r->p@, or null.
305 mp
*gfreduce_sqrt(gfreduce
*r
, mp
*d
, mp
*x
)
308 mp
*z
, *spare
= MP_NEW
;
309 unsigned long m
= mp_bits(r
->p
) - 1;
312 for (i
= 0; i
< m
- 1; i
++) {
313 mp
*t
= gf_sqr(spare
, y
);
315 y
= gfreduce_do(r
, t
, t
);
317 z
= gf_sqr(spare
, y
);
318 z
= gfreduce_do(r
, z
, z
);
328 /* --- @gfreduce_trace@ --- *
330 * Arguments: @gfreduce *r@ = pointer to reduction context
331 * @mp *x@ = some polynomial
333 * Returns: The trace of @x@. (%$\Tr(x)=x + x^2 + \cdots + x^{2^{m-1}}$%
334 * if %$x \in \gf{2^m}$%).
337 int gfreduce_trace(gfreduce
*r
, mp
*x
)
341 unsigned long m
= mp_bits(r
->p
) - 1;
345 for (i
= 0; i
< m
- 1; i
++) {
346 mp
*t
= gf_sqr(spare
, y
);
348 y
= gfreduce_do(r
, t
, t
);
357 /* --- @gfreduce_halftrace@ --- *
359 * Arguments: @gfreduce *r@ = pointer to reduction context
360 * @mp *d@ = destination
361 * @mp *x@ = some polynomial
363 * Returns: The half-trace of @x@.
364 * (%$\HfTr(x)= x + x^{2^2} + \cdots + x^{2^{m-1}}$%
365 * if %$x \in \gf{2^m}$% with %$m$% odd).
368 mp
*gfreduce_halftrace(gfreduce
*r
, mp
*d
, mp
*x
)
372 unsigned long m
= mp_bits(r
->p
) - 1;
376 for (i
= 0; i
< m
- 1; i
+= 2) {
377 mp
*t
= gf_sqr(spare
, y
);
379 y
= gfreduce_do(r
, t
, t
);
380 t
= gf_sqr(spare
, y
);
382 y
= gfreduce_do(r
, t
, t
);
389 /* --- @gfreduce_quadsolve@ --- *
391 * Arguments: @gfreduce *r@ = pointer to reduction context
392 * @mp *d@ = destination
393 * @mp *x@ = some polynomial
395 * Returns: A polynomial @y@ such that %$y^2 + y = x$%, or null.
398 mp
*gfreduce_quadsolve(gfreduce
*r
, mp
*d
, mp
*x
)
400 unsigned long m
= mp_bits(r
->p
) - 1;
405 d
= gfreduce_halftrace(r
, d
, x
);
407 mp
*z
, *w
, *rho
= MP_NEW
;
409 grand
*fr
= fibrand_create(0);
413 rho
= mprand(rho
, m
, fr
, 0);
416 for (i
= 0; i
< m
- 1; i
++) {
417 t
= gf_sqr(spare
, z
); spare
= z
; z
= gfreduce_do(r
, t
, t
);
418 t
= gf_sqr(spare
, w
); spare
= w
; w
= gfreduce_do(r
, t
, t
);
419 t
= gf_mul(spare
, w
, x
); t
= gfreduce_do(r
, t
, t
); spare
= t
;
421 w
= gf_add(w
, w
, rho
);
432 fr
->ops
->destroy(fr
);
436 t
= gf_sqr(MP_NEW
, d
); t
= gfreduce_do(r
, t
, t
); t
= gf_add(t
, t
, d
);
447 /* --- @gfreduce_exp@ --- *
449 * Arguments: @gfreduce *gr@ = pointer to reduction context
450 * @mp *d@ = fake destination
454 * Returns: Result, %$a^e \bmod m$%.
457 mp
*gfreduce_exp(gfreduce
*gr
, mp
*d
, mp
*a
, mp
*e
)
460 mp
*spare
= (e
->f
& MP_BURN
) ? MP_NEWSEC
: MP_NEW
;
465 else if (MP_LEN(e
) < EXP_THRESH
)
474 /*----- Test rig ----------------------------------------------------------*/
478 #define MP(x) mp_readstring(MP_NEW, #x, 0, 0)
480 static int vreduce(dstr
*v
)
482 mp
*d
= *(mp
**)v
[0].buf
;
483 mp
*n
= *(mp
**)v
[1].buf
;
484 mp
*r
= *(mp
**)v
[2].buf
;
489 gfreduce_create(&rr
, d
);
490 c
= gfreduce_do(&rr
, MP_NEW
, n
);
492 fprintf(stderr
, "\n*** reduction failed\n*** ");
493 gfreduce_dump(&rr
, stderr
);
494 fprintf(stderr
, "\n*** n = "); mp_writefile(n
, stderr
, 16);
495 fprintf(stderr
, "\n*** r = "); mp_writefile(r
, stderr
, 16);
496 fprintf(stderr
, "\n*** c = "); mp_writefile(c
, stderr
, 16);
497 fprintf(stderr
, "\n");
500 gfreduce_destroy(&rr
);
501 mp_drop(n
); mp_drop(d
); mp_drop(r
); mp_drop(c
);
502 assert(mparena_count(MPARENA_GLOBAL
) == 0);
506 static int vmodexp(dstr
*v
)
508 mp
*p
= *(mp
**)v
[0].buf
;
509 mp
*g
= *(mp
**)v
[1].buf
;
510 mp
*x
= *(mp
**)v
[2].buf
;
511 mp
*r
= *(mp
**)v
[3].buf
;
516 gfreduce_create(&rr
, p
);
517 c
= gfreduce_exp(&rr
, MP_NEW
, g
, x
);
519 fprintf(stderr
, "\n*** modexp failed\n*** ");
520 fprintf(stderr
, "\n*** p = "); mp_writefile(p
, stderr
, 16);
521 fprintf(stderr
, "\n*** g = "); mp_writefile(g
, stderr
, 16);
522 fprintf(stderr
, "\n*** x = "); mp_writefile(x
, stderr
, 16);
523 fprintf(stderr
, "\n*** c = "); mp_writefile(c
, stderr
, 16);
524 fprintf(stderr
, "\n*** r = "); mp_writefile(r
, stderr
, 16);
525 fprintf(stderr
, "\n");
528 gfreduce_destroy(&rr
);
529 mp_drop(p
); mp_drop(g
); mp_drop(r
); mp_drop(x
); mp_drop(c
);
530 assert(mparena_count(MPARENA_GLOBAL
) == 0);
534 static int vsqrt(dstr
*v
)
536 mp
*p
= *(mp
**)v
[0].buf
;
537 mp
*x
= *(mp
**)v
[1].buf
;
538 mp
*r
= *(mp
**)v
[2].buf
;
543 gfreduce_create(&rr
, p
);
544 c
= gfreduce_sqrt(&rr
, MP_NEW
, x
);
546 fprintf(stderr
, "\n*** sqrt failed\n*** ");
547 fprintf(stderr
, "\n*** p = "); mp_writefile(p
, stderr
, 16);
548 fprintf(stderr
, "\n*** x = "); mp_writefile(x
, stderr
, 16);
549 fprintf(stderr
, "\n*** c = "); mp_writefile(c
, stderr
, 16);
550 fprintf(stderr
, "\n*** r = "); mp_writefile(r
, stderr
, 16);
551 fprintf(stderr
, "\n");
554 gfreduce_destroy(&rr
);
555 mp_drop(p
); mp_drop(r
); mp_drop(x
); mp_drop(c
);
556 assert(mparena_count(MPARENA_GLOBAL
) == 0);
560 static int vtr(dstr
*v
)
562 mp
*p
= *(mp
**)v
[0].buf
;
563 mp
*x
= *(mp
**)v
[1].buf
;
564 int r
= *(int *)v
[2].buf
, c
;
568 gfreduce_create(&rr
, p
);
569 c
= gfreduce_trace(&rr
, x
);
571 fprintf(stderr
, "\n*** trace failed\n*** ");
572 fprintf(stderr
, "\n*** p = "); mp_writefile(p
, stderr
, 16);
573 fprintf(stderr
, "\n*** x = "); mp_writefile(x
, stderr
, 16);
574 fprintf(stderr
, "\n*** c = %d", c
);
575 fprintf(stderr
, "\n*** r = %d", r
);
576 fprintf(stderr
, "\n");
579 gfreduce_destroy(&rr
);
580 mp_drop(p
); mp_drop(x
);
581 assert(mparena_count(MPARENA_GLOBAL
) == 0);
585 static int vhftr(dstr
*v
)
587 mp
*p
= *(mp
**)v
[0].buf
;
588 mp
*x
= *(mp
**)v
[1].buf
;
589 mp
*r
= *(mp
**)v
[2].buf
;
594 gfreduce_create(&rr
, p
);
595 c
= gfreduce_halftrace(&rr
, MP_NEW
, x
);
597 fprintf(stderr
, "\n*** halftrace failed\n*** ");
598 fprintf(stderr
, "\n*** p = "); mp_writefile(p
, stderr
, 16);
599 fprintf(stderr
, "\n*** x = "); mp_writefile(x
, stderr
, 16);
600 fprintf(stderr
, "\n*** c = "); mp_writefile(c
, stderr
, 16);
601 fprintf(stderr
, "\n*** r = "); mp_writefile(r
, stderr
, 16);
602 fprintf(stderr
, "\n");
605 gfreduce_destroy(&rr
);
606 mp_drop(p
); mp_drop(r
); mp_drop(x
); mp_drop(c
);
607 assert(mparena_count(MPARENA_GLOBAL
) == 0);
611 static int vquad(dstr
*v
)
613 mp
*p
= *(mp
**)v
[0].buf
;
614 mp
*x
= *(mp
**)v
[1].buf
;
615 mp
*r
= *(mp
**)v
[2].buf
;
620 gfreduce_create(&rr
, p
);
621 c
= gfreduce_quadsolve(&rr
, MP_NEW
, x
);
623 fprintf(stderr
, "\n*** quadsolve failed\n*** ");
624 fprintf(stderr
, "\n*** p = "); mp_writefile(p
, stderr
, 16);
625 fprintf(stderr
, "\n*** x = "); mp_writefile(x
, stderr
, 16);
626 fprintf(stderr
, "\n*** c = "); mp_writefile(c
, stderr
, 16);
627 fprintf(stderr
, "\n*** r = "); mp_writefile(r
, stderr
, 16);
628 fprintf(stderr
, "\n");
631 gfreduce_destroy(&rr
);
632 mp_drop(p
); mp_drop(r
); mp_drop(x
); mp_drop(c
);
633 assert(mparena_count(MPARENA_GLOBAL
) == 0);
637 static test_chunk defs
[] = {
638 { "reduce", vreduce
, { &type_mp
, &type_mp
, &type_mp
, 0 } },
639 { "modexp", vmodexp
, { &type_mp
, &type_mp
, &type_mp
, &type_mp
, 0 } },
640 { "sqrt", vsqrt
, { &type_mp
, &type_mp
, &type_mp
, 0 } },
641 { "trace", vtr
, { &type_mp
, &type_mp
, &type_int
, 0 } },
642 { "halftrace", vhftr
, { &type_mp
, &type_mp
, &type_mp
, 0 } },
643 { "quadsolve", vquad
, { &type_mp
, &type_mp
, &type_mp
, 0 } },
647 int main(int argc
, char *argv
[])
649 test_run(argc
, argv
, defs
, SRCDIR
"/tests/gfreduce");
655 /*----- That's all, folks -------------------------------------------------*/