3 * $Id: mpreduce.c,v 1.1 2004/03/27 00:04:46 mdw Exp $
5 * Efficient reduction modulo nice primes
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: mpreduce.c,v $
33 * Revision 1.1 2004/03/27 00:04:46 mdw
34 * Implement efficient reduction for pleasant-looking primes.
38 /*----- Header files ------------------------------------------------------*/
40 #include <mLib/darray.h>
41 #include <mLib/macros.h>
45 #include "mpreduce-exp.h"
47 /*----- Data structures ---------------------------------------------------*/
49 DA_DECL(instr_v
, mpreduce_instr
);
51 /*----- Main code ---------------------------------------------------------*/
53 /* --- @mpreduce_create@ --- *
55 * Arguments: @gfreduce *r@ = structure to fill in
56 * @mp *x@ = an integer
60 * Use: Initializes a context structure for reduction.
63 void mpreduce_create(mpreduce
*r
, mp
*p
)
66 enum { Z
= 0, Z1
= 2, X
= 4, X0
= 6 };
73 /* --- Fill in the easy stuff --- */
83 /* --- Stash a new instruction --- */
85 #define INSTR(op_, argx_, argy_) do { \
87 DA(&iv)[DA_LEN(&iv)].op = (op_); \
88 DA(&iv)[DA_LEN(&iv)].argx = (argx_); \
89 DA(&iv)[DA_LEN(&iv)].argy = (argy_); \
93 /* --- Main loop --- *
95 * A simple state machine decomposes @p@ conveniently into positive and
96 * negative powers of 2. The pure form of the state machine is left below
97 * for reference (and in case I need inspiration for a NAF exponentiator).
101 for (i
= 0, mp_scan(&sc
, p
); mp_step(&sc
); i
++) {
102 switch (st
| mp_bit(&sc
)) {
103 case Z
| 1: st
= Z1
; break;
104 case Z1
| 0: st
= Z
; printf("+ %lu\n", i
- 1); break;
105 case Z1
| 1: st
= X
; printf("- %lu\n", i
- 1); break;
106 case X
| 0: st
= X0
; break;
107 case X0
| 1: st
= X
; printf("- %lu\n", i
- 1); break;
108 case X0
| 0: st
= Z
; printf("+ %lu\n", i
- 1); break;
111 if (st
>= X
) printf("+ %lu\n", i
);
114 for (i
= 0, mp_scan(&sc
, p
); i
< d
- 1 && mp_step(&sc
); i
++) {
115 switch (st
| mp_bit(&sc
)) {
116 case Z
| 1: st
= Z1
; break;
117 case Z1
| 0: st
= Z
; op
= MPRI_SUB
; goto instr
;
118 case Z1
| 1: st
= X
; op
= MPRI_ADD
; goto instr
;
119 case X
| 0: st
= X0
; break;
120 case X0
| 1: st
= X
; op
= MPRI_ADD
; goto instr
;
121 case X0
| 0: st
= Z
; op
= MPRI_SUB
; goto instr
;
123 w
= (d
- i
)/MPW_BITS
+ 1;
124 b
= (MPW_BITS
+ i
- d
- 1)%MPW_BITS
;
125 INSTR(op
| !!b
, w
, b
);
128 if ((DA(&iv
)[DA_LEN(&iv
) - 1].op
& ~1u) == MPRI_SUB
) {
130 "mpreduce can't handle moduli of the form 2^m + x\n");
136 /* --- Wrap up --- */
140 r
->iv
= xmalloc(r
->in
* sizeof(mpreduce_instr
));
141 memcpy(r
->iv
, DA(&iv
), r
->in
* sizeof(mpreduce_instr
));
143 r
->iv
= xmalloc(r
->in
* 2 * sizeof(mpreduce_instr
));
144 for (i
= 0; i
< r
->in
; i
++) {
145 r
->iv
[i
] = DA(&iv
)[i
];
146 op
= r
->iv
[i
].op
& ~1u;
155 r
->iv
[i
+ r
->in
].op
= op
;
156 r
->iv
[i
+ r
->in
].argx
= w
;
157 r
->iv
[i
+ r
->in
].argy
= b
;
163 /* --- @mpreduce_destroy@ --- *
165 * Arguments: @mpreduce *r@ = structure to free
169 * Use: Reclaims the resources from a reduction context.
172 void mpreduce_destroy(mpreduce
*r
)
178 /* --- @mpreduce_dump@ --- *
180 * Arguments: @mpreduce *r@ = structure to dump
181 * @FILE *fp@ = file to dump on
185 * Use: Dumps a reduction context.
188 void mpreduce_dump(mpreduce
*r
, FILE *fp
)
191 static const char *opname
[] = { "add", "addshift", "sub", "subshift" };
193 fprintf(fp
, "mod = "); mp_writefile(r
->p
, fp
, 16);
194 fprintf(fp
, "\n lim = %lu; s = %d\n", (unsigned long)r
->lim
, r
->s
);
195 for (i
= 0; i
< r
->in
; i
++) {
196 assert(r
->iv
[i
].op
< N(opname
));
197 fprintf(fp
, " %s %lu %lu\n",
199 (unsigned long)r
->iv
[i
].argx
,
200 (unsigned long)r
->iv
[i
].argy
);
203 fprintf(fp
, "tail end charlie\n");
204 for (i
= r
->in
; i
< 2 * r
->in
; i
++) {
205 assert(r
->iv
[i
].op
< N(opname
));
206 fprintf(fp
, " %s %lu %lu\n",
208 (unsigned long)r
->iv
[i
].argx
,
209 (unsigned long)r
->iv
[i
].argy
);
214 /* --- @mpreduce_do@ --- *
216 * Arguments: @mpreduce *r@ = reduction context
217 * @mp *d@ = destination
220 * Returns: Destination, @x@ reduced modulo the reduction poly.
223 static void run(const mpreduce_instr
*i
, const mpreduce_instr
*il
,
226 for (; i
< il
; i
++) {
229 mp_build(&vv
, v
- i
->argx
, v
+ 1);
230 printf(" 0x"); mp_writefile(&vv
, stdout
, 16);
231 printf(" %c (0x%lx << %u) == 0x",
232 (i
->op
& ~1u) == MPRI_ADD ?
'+' : '-',
237 case MPRI_ADD
: MPX_UADDN(v
- i
->argx
, v
+ 1, z
); break;
238 case MPRI_ADDLSL
: mpx_uaddnlsl(v
- i
->argx
, v
+ 1, z
, i
->argy
); break;
239 case MPRI_SUB
: MPX_USUBN(v
- i
->argx
, v
+ 1, z
); break;
240 case MPRI_SUBLSL
: mpx_usubnlsl(v
- i
->argx
, v
+ 1, z
, i
->argy
); break;
245 mp_build(&vv
, v
- i
->argx
, v
+ 1);
246 mp_writefile(&vv
, stdout
, 16);
252 mp
*mpreduce_do(mpreduce
*r
, mp
*d
, mp
*x
)
255 const mpreduce_instr
*il
;
259 mp
*_r
= 0, *_rr
= 0;
262 /* --- If source is negative, divide --- */
265 mp_div(0, &d
, x
, r
->p
);
269 /* --- Try to reuse the source's space --- */
273 MP_DEST(x
, MP_LEN(x
), x
->f
);
275 /* --- Do the reduction --- */
279 mp_div(0, &_r
, x
, r
->p
);
285 if (MP_LEN(x
) >= r
->lim
) {
292 run(r
->iv
, il
, vl
, z
);
295 mp_div(0, &_rr
, x
, r
->p
);
296 assert(MP_EQ(_r
, _rr
));
301 while (*vl
>> r
->s
) {
303 *vl
&= ((1 << r
->s
) - 1);
304 run(r
->iv
+ r
->in
, il
+ r
->in
, vl
, z
);
307 mp_div(0, &_rr
, x
, r
->p
);
308 assert(MP_EQ(_r
, _rr
));
314 /* --- Finishing touches --- */
317 if (MP_CMP(x
, >=, r
->p
))
318 x
= mp_sub(x
, x
, r
->p
);
323 assert(MP_EQ(_r
, x
));
330 /* --- @mpreduce_exp@ --- *
332 * Arguments: @mpreduce *mr@ = pointer to reduction context
333 * @mp *d@ = fake destination
337 * Returns: Result, %$a^e \bmod m$%.
340 mp
*mpreduce_exp(mpreduce
*mr
, mp
*d
, mp
*a
, mp
*e
)
343 mp
*spare
= (e
->f
& MP_BURN
) ? MP_NEWSEC
: MP_NEW
;
348 else if (MP_LEN(e
) < EXP_THRESH
)
357 /*----- Test rig ----------------------------------------------------------*/
362 #define MP(x) mp_readstring(MP_NEW, #x, 0, 0)
364 static int vreduce(dstr
*v
)
366 mp
*d
= *(mp
**)v
[0].buf
;
367 mp
*n
= *(mp
**)v
[1].buf
;
368 mp
*r
= *(mp
**)v
[2].buf
;
373 mpreduce_create(&rr
, d
);
374 c
= mpreduce_do(&rr
, MP_NEW
, n
);
376 fprintf(stderr
, "\n*** reduction failed\n*** ");
377 mpreduce_dump(&rr
, stderr
);
378 fprintf(stderr
, "\n*** n = "); mp_writefile(n
, stderr
, 10);
379 fprintf(stderr
, "\n*** r = "); mp_writefile(r
, stderr
, 10);
380 fprintf(stderr
, "\n*** c = "); mp_writefile(c
, stderr
, 10);
381 fprintf(stderr
, "\n");
384 mpreduce_destroy(&rr
);
385 mp_drop(n
); mp_drop(d
); mp_drop(r
); mp_drop(c
);
386 assert(mparena_count(MPARENA_GLOBAL
) == 0);
390 static int vmodexp(dstr
*v
)
392 mp
*p
= *(mp
**)v
[0].buf
;
393 mp
*g
= *(mp
**)v
[1].buf
;
394 mp
*x
= *(mp
**)v
[2].buf
;
395 mp
*r
= *(mp
**)v
[3].buf
;
400 mpreduce_create(&rr
, p
);
401 c
= mpreduce_exp(&rr
, MP_NEW
, g
, x
);
403 fprintf(stderr
, "\n*** modexp failed\n*** ");
404 fprintf(stderr
, "\n*** p = "); mp_writefile(p
, stderr
, 10);
405 fprintf(stderr
, "\n*** g = "); mp_writefile(g
, stderr
, 10);
406 fprintf(stderr
, "\n*** x = "); mp_writefile(x
, stderr
, 10);
407 fprintf(stderr
, "\n*** c = "); mp_writefile(c
, stderr
, 10);
408 fprintf(stderr
, "\n*** r = "); mp_writefile(r
, stderr
, 10);
409 fprintf(stderr
, "\n");
412 mpreduce_destroy(&rr
);
413 mp_drop(p
); mp_drop(g
); mp_drop(r
); mp_drop(x
); mp_drop(c
);
414 assert(mparena_count(MPARENA_GLOBAL
) == 0);
418 static test_chunk defs
[] = {
419 { "reduce", vreduce
, { &type_mp
, &type_mp
, &type_mp
, 0 } },
420 { "modexp", vmodexp
, { &type_mp
, &type_mp
, &type_mp
, &type_mp
, 0 } },
424 int main(int argc
, char *argv
[])
426 test_run(argc
, argv
, defs
, SRCDIR
"/tests/mpreduce");
432 /*----- That's all, folks -------------------------------------------------*/