3 * $Id: gfreduce.c,v 1.3 2004/03/23 15:19:32 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.3 2004/03/23 15:19:32 mdw
34 * Test elliptic curves more thoroughly.
36 * Revision 1.2 2004/03/21 22:52:06 mdw
37 * Merge and close elliptic curve branch.
39 * Revision 1.1.2.1 2004/03/21 22:39:46 mdw
40 * Elliptic curves on binary fields work.
44 /*----- Header files ------------------------------------------------------*/
46 #include <mLib/alloc.h>
47 #include <mLib/darray.h>
48 #include <mLib/macros.h>
52 #include "gfreduce-exp.h"
56 /*----- Data structures ---------------------------------------------------*/
58 DA_DECL(instr_v
, gfreduce_instr
);
60 /*----- Main code ---------------------------------------------------------*/
62 /* --- What's going on here? --- *
64 * Let's face it, @gfx_div@ sucks. It works (I hope), but it's not in any
65 * sense fast. Here, we do efficient reduction modulo sparse polynomials.
67 * Suppose we have a polynomial @X@ we're trying to reduce mod @P@. If we
68 * take the topmost nonzero word of @X@, call it @w@, then we can eliminate
69 * it by subtracting off @w P x^{k}@ for an appropriate value of @k@. The
70 * trick is in observing that if @P@ is sparse we can do this multiplication
71 * and subtraction efficiently, just by XORing appropriate shifts of @w@ into
74 * The first tricky bit is in working out when to stop. I'll use eight-bit
75 * words to demonstrate what I'm talking about.
77 * xxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
78 * 001ppppp pppppppp pppppppp pppppppp
80 * |<------------ bp ------------->|
81 * |<------------ nw --------------->|
83 * The trick of taking whole words off of @X@ stops working when there are
84 * only @nw@ words left. Then we have to mask off the bottom bits of @w@
88 /* --- @gfreduce_create@ --- *
90 * Arguments: @gfreduce *r@ = structure to fill in
91 * @mp *x@ = a (hopefully sparse) polynomial
95 * Use: Initializes a context structure for reduction.
98 void gfreduce_create(gfreduce
*r
, mp
*p
)
100 instr_v iv
= DA_INIT
;
106 size_t w
, ww
, wi
, wl
, ll
;
108 /* --- Sort out the easy stuff --- */
110 d
= mp_bits(p
); assert(d
); d
--;
116 r
->mask
= MPW(((mpw
)-1) << dw
);
121 /* --- Stash a new instruction --- */
123 #define INSTR(op_, arg_) do { \
125 DA(&iv)[DA_LEN(&iv)].op = (op_); \
126 DA(&iv)[DA_LEN(&iv)].arg = (arg_); \
132 w
= (d
+ MPW_BITS
- 1)/MPW_BITS
;
137 for (i
= 0, mp_scan(&sc
, p
); mp_step(&sc
) && i
< d
; i
++) {
140 ww
= (d
- i
+ MPW_BITS
- 1)/MPW_BITS
;
143 INSTR(GFRI_STORE
, w
);
147 INSTR(GFRI_LOAD
, ww
);
149 INSTR(GFRI_LOAD
, w
- 1);
150 for (; wi
< wl
; wi
++) {
152 assert(ip
->op
== GFRI_LSL
);
154 INSTR(GFRI_LSR
, MPW_BITS
- ip
->arg
);
157 INSTR(GFRI_STORE
, w
- 1);
158 INSTR(GFRI_LOAD
, ww
);
165 INSTR(GFRI_LSL
, (i
- d
)%MPW_BITS
);
166 if ((i
- d
)%MPW_BITS
)
170 INSTR(GFRI_STORE
, w
);
174 INSTR(GFRI_LOAD
, w
- 1);
175 for (; wi
< wl
; wi
++) {
177 assert(ip
->op
== GFRI_LSL
);
179 INSTR(GFRI_LSR
, MPW_BITS
- ip
->arg
);
181 INSTR(GFRI_STORE
, w
- 1);
187 r
->iv
= xmalloc(r
->in
* sizeof(gfreduce_instr
));
189 memcpy(r
->iv
, DA(&iv
), r
->in
* sizeof(gfreduce_instr
));
193 /* --- @gfreduce_destroy@ --- *
195 * Arguments: @gfreduce *r@ = structure to free
199 * Use: Reclaims the resources from a reduction context.
202 void gfreduce_destroy(gfreduce
*r
)
208 /* --- @gfreduce_dump@ --- *
210 * Arguments: @gfreduce *r@ = structure to dump
211 * @FILE *fp@ = file to dump on
215 * Use: Dumps a reduction context.
218 void gfreduce_dump(gfreduce
*r
, FILE *fp
)
222 fprintf(fp
, "poly = "); mp_writefile(r
->p
, fp
, 16);
223 fprintf(fp
, "\n lim = %lu; mask = %lx\n",
224 (unsigned long)r
->lim
, (unsigned long)r
->mask
);
225 for (i
= 0; i
< r
->in
; i
++) {
226 static const char *opname
[] = { "load", "lsl", "lsr", "store" };
227 assert(r
->iv
[i
].op
< N(opname
));
228 fprintf(fp
, " %s %lu\n",
230 (unsigned long)r
->iv
[i
].arg
);
234 /* --- @gfreduce_do@ --- *
236 * Arguments: @gfreduce *r@ = reduction context
237 * @mp *d@ = destination
240 * Returns: Destination, @x@ reduced modulo the reduction poly.
243 static void run(const gfreduce_instr
*i
, const gfreduce_instr
*il
,
248 for (; i
< il
; i
++) {
250 case GFRI_LOAD
: w
= *(v
- i
->arg
); break;
251 case GFRI_LSL
: w
^= z
<< i
->arg
; break;
252 case GFRI_LSR
: w
^= z
>> i
->arg
; break;
253 case GFRI_STORE
: *(v
- i
->arg
) = MPW(w
); break;
259 mp
*gfreduce_do(gfreduce
*r
, mp
*d
, mp
*x
)
262 const gfreduce_instr
*il
;
265 /* --- Try to reuse the source's space --- */
269 MP_DEST(x
, MP_LEN(x
), x
->f
);
271 /* --- Do the reduction --- */
274 if (MP_LEN(x
) >= r
->lim
) {
281 run(r
->iv
, il
, vl
, z
);
285 while (*vl
& r
->mask
) {
288 run(r
->iv
, il
, vl
, z
);
299 /* --- @gfreduce_sqrt@ --- *
301 * Arguments: @gfreduce *r@ = pointer to reduction context
302 * @mp *d@ = destination
303 * @mp *x@ = some polynomial
305 * Returns: The square root of @x@ modulo @r->p@, or null.
308 mp
*gfreduce_sqrt(gfreduce
*r
, mp
*d
, mp
*x
)
311 mp
*z
, *spare
= MP_NEW
;
312 unsigned long m
= mp_bits(r
->p
) - 1;
315 for (i
= 0; i
< m
- 1; i
++) {
316 mp
*t
= gf_sqr(spare
, y
);
318 y
= gfreduce_do(r
, t
, t
);
320 z
= gf_sqr(spare
, y
);
321 z
= gfreduce_do(r
, z
, z
);
331 /* --- @gfreduce_trace@ --- *
333 * Arguments: @gfreduce *r@ = pointer to reduction context
334 * @mp *x@ = some polynomial
336 * Returns: The trace of @x@. (%$\Tr(x)=x + x^2 + \cdots + x^{2^{m-1}}$%
337 * if %$x \in \gf{2^m}$%).
340 int gfreduce_trace(gfreduce
*r
, mp
*x
)
344 unsigned long m
= mp_bits(r
->p
) - 1;
348 for (i
= 0; i
< m
- 1; i
++) {
349 mp
*t
= gf_sqr(spare
, y
);
351 y
= gfreduce_do(r
, t
, t
);
360 /* --- @gfreduce_halftrace@ --- *
362 * Arguments: @gfreduce *r@ = pointer to reduction context
363 * @mp *d@ = destination
364 * @mp *x@ = some polynomial
366 * Returns: The half-trace of @x@.
367 * (%$\HfTr(x)= x + x^{2^2} + \cdots + x^{2^{m-1}}$%
368 * if %$x \in \gf{2^m}$% with %$m$% odd).
371 mp
*gfreduce_halftrace(gfreduce
*r
, mp
*d
, mp
*x
)
375 unsigned long m
= mp_bits(r
->p
) - 1;
379 for (i
= 0; i
< m
- 1; i
+= 2) {
380 mp
*t
= gf_sqr(spare
, y
);
382 y
= gfreduce_do(r
, t
, t
);
383 t
= gf_sqr(spare
, y
);
385 y
= gfreduce_do(r
, t
, t
);
392 /* --- @gfreduce_quadsolve@ --- *
394 * Arguments: @gfreduce *r@ = pointer to reduction context
395 * @mp *d@ = destination
396 * @mp *x@ = some polynomial
398 * Returns: A polynomial @y@ such that %$y^2 + y = x$%, or null.
401 mp
*gfreduce_quadsolve(gfreduce
*r
, mp
*d
, mp
*x
)
403 unsigned long m
= mp_bits(r
->p
) - 1;
408 d
= gfreduce_halftrace(r
, d
, x
);
410 mp
*z
, *w
, *rho
= MP_NEW
;
412 grand
*fr
= fibrand_create(0);
416 rho
= mprand(rho
, m
, fr
, 0);
419 for (i
= 0; i
< m
- 1; i
++) {
420 t
= gf_sqr(spare
, z
); spare
= z
; z
= gfreduce_do(r
, t
, t
);
421 t
= gf_sqr(spare
, w
); spare
= w
; w
= gfreduce_do(r
, t
, t
);
422 t
= gf_mul(spare
, w
, x
); t
= gfreduce_do(r
, t
, t
); spare
= t
;
424 w
= gf_add(w
, w
, rho
);
435 fr
->ops
->destroy(fr
);
439 t
= gf_sqr(MP_NEW
, d
); t
= gfreduce_do(r
, t
, t
); t
= gf_add(t
, t
, d
);
446 if (d
) d
->v
[0] &= ~(mpw
)1;
450 /* --- @gfreduce_exp@ --- *
452 * Arguments: @gfreduce *gr@ = pointer to reduction context
453 * @mp *d@ = fake destination
457 * Returns: Result, %$a^e \bmod m$%.
460 mp
*gfreduce_exp(gfreduce
*gr
, mp
*d
, mp
*a
, mp
*e
)
463 mp
*spare
= (e
->f
& MP_BURN
) ? MP_NEWSEC
: MP_NEW
;
468 else if (MP_LEN(e
) < EXP_THRESH
)
477 /*----- Test rig ----------------------------------------------------------*/
481 #define MP(x) mp_readstring(MP_NEW, #x, 0, 0)
483 static int vreduce(dstr
*v
)
485 mp
*d
= *(mp
**)v
[0].buf
;
486 mp
*n
= *(mp
**)v
[1].buf
;
487 mp
*r
= *(mp
**)v
[2].buf
;
492 gfreduce_create(&rr
, d
);
493 c
= gfreduce_do(&rr
, MP_NEW
, n
);
495 fprintf(stderr
, "\n*** reduction failed\n*** ");
496 gfreduce_dump(&rr
, stderr
);
497 fprintf(stderr
, "\n*** n = "); mp_writefile(n
, stderr
, 16);
498 fprintf(stderr
, "\n*** r = "); mp_writefile(r
, stderr
, 16);
499 fprintf(stderr
, "\n*** c = "); mp_writefile(c
, stderr
, 16);
500 fprintf(stderr
, "\n");
503 gfreduce_destroy(&rr
);
504 mp_drop(n
); mp_drop(d
); mp_drop(r
); mp_drop(c
);
505 assert(mparena_count(MPARENA_GLOBAL
) == 0);
509 static int vmodexp(dstr
*v
)
511 mp
*p
= *(mp
**)v
[0].buf
;
512 mp
*g
= *(mp
**)v
[1].buf
;
513 mp
*x
= *(mp
**)v
[2].buf
;
514 mp
*r
= *(mp
**)v
[3].buf
;
519 gfreduce_create(&rr
, p
);
520 c
= gfreduce_exp(&rr
, MP_NEW
, g
, x
);
522 fprintf(stderr
, "\n*** modexp failed\n*** ");
523 fprintf(stderr
, "\n*** p = "); mp_writefile(p
, stderr
, 16);
524 fprintf(stderr
, "\n*** g = "); mp_writefile(g
, stderr
, 16);
525 fprintf(stderr
, "\n*** x = "); mp_writefile(x
, stderr
, 16);
526 fprintf(stderr
, "\n*** c = "); mp_writefile(c
, stderr
, 16);
527 fprintf(stderr
, "\n*** r = "); mp_writefile(r
, stderr
, 16);
528 fprintf(stderr
, "\n");
531 gfreduce_destroy(&rr
);
532 mp_drop(p
); mp_drop(g
); mp_drop(r
); mp_drop(x
); mp_drop(c
);
533 assert(mparena_count(MPARENA_GLOBAL
) == 0);
537 static int vsqrt(dstr
*v
)
539 mp
*p
= *(mp
**)v
[0].buf
;
540 mp
*x
= *(mp
**)v
[1].buf
;
541 mp
*r
= *(mp
**)v
[2].buf
;
546 gfreduce_create(&rr
, p
);
547 c
= gfreduce_sqrt(&rr
, MP_NEW
, x
);
549 fprintf(stderr
, "\n*** sqrt failed\n*** ");
550 fprintf(stderr
, "\n*** p = "); mp_writefile(p
, stderr
, 16);
551 fprintf(stderr
, "\n*** x = "); mp_writefile(x
, stderr
, 16);
552 fprintf(stderr
, "\n*** c = "); mp_writefile(c
, stderr
, 16);
553 fprintf(stderr
, "\n*** r = "); mp_writefile(r
, stderr
, 16);
554 fprintf(stderr
, "\n");
557 gfreduce_destroy(&rr
);
558 mp_drop(p
); mp_drop(r
); mp_drop(x
); mp_drop(c
);
559 assert(mparena_count(MPARENA_GLOBAL
) == 0);
563 static int vtr(dstr
*v
)
565 mp
*p
= *(mp
**)v
[0].buf
;
566 mp
*x
= *(mp
**)v
[1].buf
;
567 int r
= *(int *)v
[2].buf
, c
;
571 gfreduce_create(&rr
, p
);
572 c
= gfreduce_trace(&rr
, x
);
574 fprintf(stderr
, "\n*** trace failed\n*** ");
575 fprintf(stderr
, "\n*** p = "); mp_writefile(p
, stderr
, 16);
576 fprintf(stderr
, "\n*** x = "); mp_writefile(x
, stderr
, 16);
577 fprintf(stderr
, "\n*** c = %d", c
);
578 fprintf(stderr
, "\n*** r = %d", r
);
579 fprintf(stderr
, "\n");
582 gfreduce_destroy(&rr
);
583 mp_drop(p
); mp_drop(x
);
584 assert(mparena_count(MPARENA_GLOBAL
) == 0);
588 static int vhftr(dstr
*v
)
590 mp
*p
= *(mp
**)v
[0].buf
;
591 mp
*x
= *(mp
**)v
[1].buf
;
592 mp
*r
= *(mp
**)v
[2].buf
;
597 gfreduce_create(&rr
, p
);
598 c
= gfreduce_halftrace(&rr
, MP_NEW
, x
);
600 fprintf(stderr
, "\n*** halftrace failed\n*** ");
601 fprintf(stderr
, "\n*** p = "); mp_writefile(p
, stderr
, 16);
602 fprintf(stderr
, "\n*** x = "); mp_writefile(x
, stderr
, 16);
603 fprintf(stderr
, "\n*** c = "); mp_writefile(c
, stderr
, 16);
604 fprintf(stderr
, "\n*** r = "); mp_writefile(r
, stderr
, 16);
605 fprintf(stderr
, "\n");
608 gfreduce_destroy(&rr
);
609 mp_drop(p
); mp_drop(r
); mp_drop(x
); mp_drop(c
);
610 assert(mparena_count(MPARENA_GLOBAL
) == 0);
614 static int vquad(dstr
*v
)
616 mp
*p
= *(mp
**)v
[0].buf
;
617 mp
*x
= *(mp
**)v
[1].buf
;
618 mp
*r
= *(mp
**)v
[2].buf
;
623 gfreduce_create(&rr
, p
);
624 c
= gfreduce_quadsolve(&rr
, MP_NEW
, x
);
626 fprintf(stderr
, "\n*** quadsolve failed\n*** ");
627 fprintf(stderr
, "\n*** p = "); mp_writefile(p
, stderr
, 16);
628 fprintf(stderr
, "\n*** x = "); mp_writefile(x
, stderr
, 16);
629 fprintf(stderr
, "\n*** c = "); mp_writefile(c
, stderr
, 16);
630 fprintf(stderr
, "\n*** r = "); mp_writefile(r
, stderr
, 16);
631 fprintf(stderr
, "\n");
634 gfreduce_destroy(&rr
);
635 mp_drop(p
); mp_drop(r
); mp_drop(x
); mp_drop(c
);
636 assert(mparena_count(MPARENA_GLOBAL
) == 0);
640 static test_chunk defs
[] = {
641 { "reduce", vreduce
, { &type_mp
, &type_mp
, &type_mp
, 0 } },
642 { "modexp", vmodexp
, { &type_mp
, &type_mp
, &type_mp
, &type_mp
, 0 } },
643 { "sqrt", vsqrt
, { &type_mp
, &type_mp
, &type_mp
, 0 } },
644 { "trace", vtr
, { &type_mp
, &type_mp
, &type_int
, 0 } },
645 { "halftrace", vhftr
, { &type_mp
, &type_mp
, &type_mp
, 0 } },
646 { "quadsolve", vquad
, { &type_mp
, &type_mp
, &type_mp
, 0 } },
650 int main(int argc
, char *argv
[])
652 test_run(argc
, argv
, defs
, SRCDIR
"/tests/gfreduce");
658 /*----- That's all, folks -------------------------------------------------*/