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 /*----- Header files ------------------------------------------------------*/
32 #include <mLib/darray.h>
33 #include <mLib/macros.h>
37 #include "mpreduce-exp.h"
39 /*----- Data structures ---------------------------------------------------*/
41 DA_DECL(instr_v
, mpreduce_instr
);
43 /*----- Main code ---------------------------------------------------------*/
45 /* --- @mpreduce_create@ --- *
47 * Arguments: @gfreduce *r@ = structure to fill in
48 * @mp *x@ = an integer
50 * Returns: Zero if successful; nonzero on failure.
52 * Use: Initializes a context structure for reduction.
55 int mpreduce_create(mpreduce
*r
, mp
*p
)
58 enum { Z
= 0, Z1
= 2, X
= 4, X0
= 6 };
65 /* --- Fill in the easy stuff --- */
76 /* --- Stash a new instruction --- */
78 #define INSTR(op_, argx_, argy_) do { \
80 DA(&iv)[DA_LEN(&iv)].op = (op_); \
81 DA(&iv)[DA_LEN(&iv)].argx = (argx_); \
82 DA(&iv)[DA_LEN(&iv)].argy = (argy_); \
86 /* --- Main loop --- *
88 * A simple state machine decomposes @p@ conveniently into positive and
89 * negative powers of 2. The pure form of the state machine is left below
90 * for reference (and in case I need inspiration for a NAF exponentiator).
94 for (i
= 0, mp_scan(&sc
, p
); mp_step(&sc
); i
++) {
95 switch (st
| mp_bit(&sc
)) {
96 case Z
| 1: st
= Z1
; break;
97 case Z1
| 0: st
= Z
; printf("+ %lu\n", i
- 1); break;
98 case Z1
| 1: st
= X
; printf("- %lu\n", i
- 1); break;
99 case X
| 0: st
= X0
; break;
100 case X0
| 1: st
= X
; printf("- %lu\n", i
- 1); break;
101 case X0
| 0: st
= Z
; printf("+ %lu\n", i
- 1); break;
104 if (st
>= X
) printf("+ %lu\n", i
- 1);
108 for (i
= 0, mp_scan(&sc
, p
); i
< d
- 1 && mp_step(&sc
); i
++) {
109 switch (st
| mp_bit(&sc
)) {
110 case Z
| 1: st
= Z1
; break;
111 case Z1
| 0: st
= Z
; op
= MPRI_SUB
; goto instr
;
112 case Z1
| 1: st
= X
; op
= MPRI_ADD
; goto instr
;
113 case X
| 0: st
= X0
; break;
114 case X0
| 1: st
= X
; op
= MPRI_ADD
; goto instr
;
115 case X0
| 0: st
= Z
; op
= MPRI_SUB
; goto instr
;
117 w
= (d
- i
)/MPW_BITS
+ 1;
118 b
= (MPW_BITS
+ i
- d
- 1)%MPW_BITS
;
119 INSTR(op
| !!b
, w
, b
);
122 if ((DA(&iv
)[DA_LEN(&iv
) - 1].op
& ~1u) == MPRI_SUB
) {
130 /* --- Wrap up --- */
134 r
->iv
= xmalloc(r
->in
* sizeof(mpreduce_instr
));
135 memcpy(r
->iv
, DA(&iv
), r
->in
* sizeof(mpreduce_instr
));
137 r
->iv
= xmalloc(r
->in
* 2 * sizeof(mpreduce_instr
));
138 for (i
= 0; i
< r
->in
; i
++) {
139 r
->iv
[i
] = DA(&iv
)[i
];
140 op
= r
->iv
[i
].op
& ~1u;
149 r
->iv
[i
+ r
->in
].op
= op
;
150 r
->iv
[i
+ r
->in
].argx
= w
;
151 r
->iv
[i
+ r
->in
].argy
= b
;
158 /* --- @mpreduce_destroy@ --- *
160 * Arguments: @mpreduce *r@ = structure to free
164 * Use: Reclaims the resources from a reduction context.
167 void mpreduce_destroy(mpreduce
*r
)
173 /* --- @mpreduce_dump@ --- *
175 * Arguments: @mpreduce *r@ = structure to dump
176 * @FILE *fp@ = file to dump on
180 * Use: Dumps a reduction context.
183 void mpreduce_dump(mpreduce
*r
, FILE *fp
)
186 static const char *opname
[] = { "add", "addshift", "sub", "subshift" };
188 fprintf(fp
, "mod = "); mp_writefile(r
->p
, fp
, 16);
189 fprintf(fp
, "\n lim = %lu; s = %d\n", (unsigned long)r
->lim
, r
->s
);
190 for (i
= 0; i
< r
->in
; i
++) {
191 assert(r
->iv
[i
].op
< N(opname
));
192 fprintf(fp
, " %s %lu %lu\n",
194 (unsigned long)r
->iv
[i
].argx
,
195 (unsigned long)r
->iv
[i
].argy
);
198 fprintf(fp
, "tail end charlie\n");
199 for (i
= r
->in
; i
< 2 * r
->in
; i
++) {
200 assert(r
->iv
[i
].op
< N(opname
));
201 fprintf(fp
, " %s %lu %lu\n",
203 (unsigned long)r
->iv
[i
].argx
,
204 (unsigned long)r
->iv
[i
].argy
);
209 /* --- @mpreduce_do@ --- *
211 * Arguments: @mpreduce *r@ = reduction context
212 * @mp *d@ = destination
215 * Returns: Destination, @x@ reduced modulo the reduction poly.
218 static void run(const mpreduce_instr
*i
, const mpreduce_instr
*il
,
221 for (; i
< il
; i
++) {
224 mp_build(&vv
, v
- i
->argx
, v
+ 1);
225 printf(" 0x"); mp_writefile(&vv
, stdout
, 16);
226 printf(" %c (0x%lx << %u) == 0x",
227 (i
->op
& ~1u) == MPRI_ADD ?
'+' : '-',
232 case MPRI_ADD
: MPX_UADDN(v
- i
->argx
, v
+ 1, z
); break;
233 case MPRI_ADDLSL
: mpx_uaddnlsl(v
- i
->argx
, v
+ 1, z
, i
->argy
); break;
234 case MPRI_SUB
: MPX_USUBN(v
- i
->argx
, v
+ 1, z
); break;
235 case MPRI_SUBLSL
: mpx_usubnlsl(v
- i
->argx
, v
+ 1, z
, i
->argy
); break;
240 mp_build(&vv
, v
- i
->argx
, v
+ 1);
241 mp_writefile(&vv
, stdout
, 16);
247 mp
*mpreduce_do(mpreduce
*r
, mp
*d
, mp
*x
)
250 const mpreduce_instr
*il
;
254 mp
*_r
= 0, *_rr
= 0;
257 /* --- If source is negative, divide --- */
260 mp_div(0, &d
, x
, r
->p
);
264 /* --- Try to reuse the source's space --- */
268 MP_DEST(x
, MP_LEN(x
), x
->f
);
270 /* --- Do the reduction --- */
274 mp_div(0, &_r
, x
, r
->p
);
280 if (MP_LEN(x
) >= r
->lim
) {
287 run(r
->iv
, il
, vl
, z
);
290 mp_div(0, &_rr
, x
, r
->p
);
291 assert(MP_EQ(_r
, _rr
));
296 while (*vl
>> r
->s
) {
298 *vl
&= ((1 << r
->s
) - 1);
299 run(r
->iv
+ r
->in
, il
+ r
->in
, vl
, z
);
302 mp_div(0, &_rr
, x
, r
->p
);
303 assert(MP_EQ(_r
, _rr
));
309 /* --- Finishing touches --- */
312 if (MP_CMP(x
, >=, r
->p
))
313 x
= mp_sub(x
, x
, r
->p
);
318 assert(MP_EQ(_r
, x
));
325 /* --- @mpreduce_exp@ --- *
327 * Arguments: @mpreduce *mr@ = pointer to reduction context
328 * @mp *d@ = fake destination
332 * Returns: Result, %$a^e \bmod m$%.
335 mp
*mpreduce_exp(mpreduce
*mr
, mp
*d
, mp
*a
, mp
*e
)
338 mp
*spare
= (e
->f
& MP_BURN
) ? MP_NEWSEC
: MP_NEW
;
346 a
= mp_modinv(a
, a
, mr
->p
);
347 if (MP_LEN(e
) < EXP_THRESH
)
358 /*----- Test rig ----------------------------------------------------------*/
363 #define MP(x) mp_readstring(MP_NEW, #x, 0, 0)
365 static int vreduce(dstr
*v
)
367 mp
*d
= *(mp
**)v
[0].buf
;
368 mp
*n
= *(mp
**)v
[1].buf
;
369 mp
*r
= *(mp
**)v
[2].buf
;
374 mpreduce_create(&rr
, d
);
375 c
= mpreduce_do(&rr
, MP_NEW
, n
);
377 fprintf(stderr
, "\n*** reduction failed\n*** ");
378 mpreduce_dump(&rr
, stderr
);
379 fprintf(stderr
, "\n*** n = "); mp_writefile(n
, stderr
, 10);
380 fprintf(stderr
, "\n*** r = "); mp_writefile(r
, stderr
, 10);
381 fprintf(stderr
, "\n*** c = "); mp_writefile(c
, stderr
, 10);
382 fprintf(stderr
, "\n");
385 mpreduce_destroy(&rr
);
386 mp_drop(n
); mp_drop(d
); mp_drop(r
); mp_drop(c
);
387 assert(mparena_count(MPARENA_GLOBAL
) == 0);
391 static int vmodexp(dstr
*v
)
393 mp
*p
= *(mp
**)v
[0].buf
;
394 mp
*g
= *(mp
**)v
[1].buf
;
395 mp
*x
= *(mp
**)v
[2].buf
;
396 mp
*r
= *(mp
**)v
[3].buf
;
401 mpreduce_create(&rr
, p
);
402 c
= mpreduce_exp(&rr
, MP_NEW
, g
, x
);
404 fprintf(stderr
, "\n*** modexp failed\n*** ");
405 fprintf(stderr
, "\n*** p = "); mp_writefile(p
, stderr
, 10);
406 fprintf(stderr
, "\n*** g = "); mp_writefile(g
, stderr
, 10);
407 fprintf(stderr
, "\n*** x = "); mp_writefile(x
, stderr
, 10);
408 fprintf(stderr
, "\n*** c = "); mp_writefile(c
, stderr
, 10);
409 fprintf(stderr
, "\n*** r = "); mp_writefile(r
, stderr
, 10);
410 fprintf(stderr
, "\n");
413 mpreduce_destroy(&rr
);
414 mp_drop(p
); mp_drop(g
); mp_drop(r
); mp_drop(x
); mp_drop(c
);
415 assert(mparena_count(MPARENA_GLOBAL
) == 0);
419 static test_chunk defs
[] = {
420 { "reduce", vreduce
, { &type_mp
, &type_mp
, &type_mp
, 0 } },
421 { "modexp", vmodexp
, { &type_mp
, &type_mp
, &type_mp
, &type_mp
, 0 } },
425 int main(int argc
, char *argv
[])
427 test_run(argc
, argv
, defs
, SRCDIR
"/tests/mpreduce");
433 /*----- That's all, folks -------------------------------------------------*/