3 * Efficient reduction modulo nice primes
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/darray.h>
31 #include <mLib/macros.h>
35 #include "mpreduce-exp.h"
37 /*----- Data structures ---------------------------------------------------*/
39 DA_DECL(instr_v, mpreduce_instr);
41 /*----- Main code ---------------------------------------------------------*/
43 /* --- @mpreduce_create@ --- *
45 * Arguments: @gfreduce *r@ = structure to fill in
46 * @mp *x@ = an integer
48 * Returns: Zero if successful; nonzero on failure.
50 * Use: Initializes a context structure for reduction.
53 int mpreduce_create(mpreduce *r, mp *p)
56 enum { Z = 0, Z1 = 2, X = 4, X0 = 6 };
63 /* --- Fill in the easy stuff --- */
74 /* --- Stash a new instruction --- */
76 #define INSTR(op_, argx_, argy_) do { \
78 DA(&iv)[DA_LEN(&iv)].op = (op_); \
79 DA(&iv)[DA_LEN(&iv)].argx = (argx_); \
80 DA(&iv)[DA_LEN(&iv)].argy = (argy_); \
84 /* --- Main loop --- *
86 * A simple state machine decomposes @p@ conveniently into positive and
87 * negative powers of 2. The pure form of the state machine is left below
88 * for reference (and in case I need inspiration for a NAF exponentiator).
92 for (i = 0, mp_scan(&sc, p); mp_step(&sc); i++) {
93 switch (st | mp_bit(&sc)) {
94 case Z | 1: st = Z1; break;
95 case Z1 | 0: st = Z; printf("+ %lu\n", i - 1); break;
96 case Z1 | 1: st = X; printf("- %lu\n", i - 1); break;
97 case X | 0: st = X0; break;
98 case X0 | 1: st = X; printf("- %lu\n", i - 1); break;
99 case X0 | 0: st = Z; printf("+ %lu\n", i - 1); break;
102 if (st >= X) printf("+ %lu\n", i - 1);
106 bb = MPW_BITS - (d + 1)%MPW_BITS;
107 for (i = 0, mp_scan(&sc, p); i < d && mp_step(&sc); i++) {
108 switch (st | mp_bit(&sc)) {
109 case Z | 1: st = Z1; break;
110 case Z1 | 0: st = Z; op = MPRI_SUB; goto instr;
111 case Z1 | 1: st = X; op = MPRI_ADD; goto instr;
112 case X | 0: st = X0; break;
113 case X0 | 1: st = X; op = MPRI_ADD; goto instr;
114 case X0 | 0: st = Z; op = MPRI_SUB; goto instr;
116 w = (d - i)/MPW_BITS + 1;
117 b = (bb + i)%MPW_BITS;
118 INSTR(op | !!b, w, b);
121 if (DA_LEN(&iv) && (DA(&iv)[DA_LEN(&iv) - 1].op & ~1u) == MPRI_SUB) {
129 /* --- Wrap up --- */
135 r->iv = xmalloc(r->in * sizeof(mpreduce_instr));
136 memcpy(r->iv, DA(&iv), r->in * sizeof(mpreduce_instr));
138 r->iv = xmalloc(r->in * 2 * sizeof(mpreduce_instr));
139 for (i = 0; i < r->in; i++) {
140 r->iv[i] = DA(&iv)[i];
141 op = r->iv[i].op & ~1u;
150 r->iv[i + r->in].op = op;
151 r->iv[i + r->in].argx = w;
152 r->iv[i + r->in].argy = b;
158 mpreduce_dump(r, stdout);
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)
175 if (r->iv) xfree(r->iv);
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;
351 a = mp_modinv(a, a, mr->p);
352 if (MP_LEN(e) < EXP_THRESH)
363 /*----- Test rig ----------------------------------------------------------*/
368 #define MP(x) mp_readstring(MP_NEW, #x, 0, 0)
370 static int vreduce(dstr *v)
372 mp *d = *(mp **)v[0].buf;
373 mp *n = *(mp **)v[1].buf;
374 mp *r = *(mp **)v[2].buf;
379 mpreduce_create(&rr, d);
380 c = mpreduce_do(&rr, MP_NEW, n);
382 fprintf(stderr, "\n*** reduction failed\n*** ");
383 mpreduce_dump(&rr, stderr);
384 fprintf(stderr, "\n*** n = "); mp_writefile(n, stderr, 10);
385 fprintf(stderr, "\n*** r = "); mp_writefile(r, stderr, 10);
386 fprintf(stderr, "\n*** c = "); mp_writefile(c, stderr, 10);
387 fprintf(stderr, "\n");
390 mpreduce_destroy(&rr);
391 mp_drop(n); mp_drop(d); mp_drop(r); mp_drop(c);
392 assert(mparena_count(MPARENA_GLOBAL) == 0);
396 static int vmodexp(dstr *v)
398 mp *p = *(mp **)v[0].buf;
399 mp *g = *(mp **)v[1].buf;
400 mp *x = *(mp **)v[2].buf;
401 mp *r = *(mp **)v[3].buf;
406 mpreduce_create(&rr, p);
407 c = mpreduce_exp(&rr, MP_NEW, g, x);
409 fprintf(stderr, "\n*** modexp failed\n*** ");
410 fprintf(stderr, "\n*** p = "); mp_writefile(p, stderr, 10);
411 fprintf(stderr, "\n*** g = "); mp_writefile(g, stderr, 10);
412 fprintf(stderr, "\n*** x = "); mp_writefile(x, stderr, 10);
413 fprintf(stderr, "\n*** c = "); mp_writefile(c, stderr, 10);
414 fprintf(stderr, "\n*** r = "); mp_writefile(r, stderr, 10);
415 fprintf(stderr, "\n");
418 mpreduce_destroy(&rr);
419 mp_drop(p); mp_drop(g); mp_drop(r); mp_drop(x); mp_drop(c);
420 assert(mparena_count(MPARENA_GLOBAL) == 0);
424 static test_chunk defs[] = {
425 { "reduce", vreduce, { &type_mp, &type_mp, &type_mp, 0 } },
426 { "modexp", vmodexp, { &type_mp, &type_mp, &type_mp, &type_mp, 0 } },
430 int main(int argc, char *argv[])
432 test_run(argc, argv, defs, SRCDIR"/t/mpreduce");
438 /*----- That's all, folks -------------------------------------------------*/