Commit | Line | Data |
---|---|---|
f46efa79 | 1 | /* -*-c-*- |
2 | * | |
a69a3efd | 3 | * $Id$ |
f46efa79 | 4 | * |
5 | * Efficient reduction modulo nice primes | |
6 | * | |
7 | * (c) 2004 Straylight/Edgeware | |
8 | */ | |
9 | ||
45c0fd36 | 10 | /*----- Licensing notice --------------------------------------------------* |
f46efa79 | 11 | * |
12 | * This file is part of Catacomb. | |
13 | * | |
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. | |
45c0fd36 | 18 | * |
f46efa79 | 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. | |
45c0fd36 | 23 | * |
f46efa79 | 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, | |
27 | * MA 02111-1307, USA. | |
28 | */ | |
29 | ||
f46efa79 | 30 | /*----- Header files ------------------------------------------------------*/ |
31 | ||
32 | #include <mLib/darray.h> | |
33 | #include <mLib/macros.h> | |
34 | ||
35 | #include "mp.h" | |
36 | #include "mpreduce.h" | |
37 | #include "mpreduce-exp.h" | |
38 | ||
39 | /*----- Data structures ---------------------------------------------------*/ | |
40 | ||
41 | DA_DECL(instr_v, mpreduce_instr); | |
42 | ||
43 | /*----- Main code ---------------------------------------------------------*/ | |
44 | ||
45 | /* --- @mpreduce_create@ --- * | |
46 | * | |
47 | * Arguments: @gfreduce *r@ = structure to fill in | |
48 | * @mp *x@ = an integer | |
49 | * | |
f4535c64 | 50 | * Returns: Zero if successful; nonzero on failure. |
f46efa79 | 51 | * |
52 | * Use: Initializes a context structure for reduction. | |
53 | */ | |
54 | ||
f4535c64 | 55 | int mpreduce_create(mpreduce *r, mp *p) |
f46efa79 | 56 | { |
57 | mpscan sc; | |
58 | enum { Z = 0, Z1 = 2, X = 4, X0 = 6 }; | |
59 | unsigned st = Z; | |
60 | instr_v iv = DA_INIT; | |
61 | unsigned long d, i; | |
62 | unsigned op; | |
c29970a7 | 63 | size_t w, b, bb; |
f46efa79 | 64 | |
65 | /* --- Fill in the easy stuff --- */ | |
66 | ||
f4535c64 | 67 | if (!MP_POSP(p)) |
68 | return (-1); | |
f46efa79 | 69 | d = mp_bits(p); |
70 | r->lim = d/MPW_BITS; | |
71 | r->s = d%MPW_BITS; | |
72 | if (r->s) | |
73 | r->lim++; | |
74 | r->p = mp_copy(p); | |
75 | ||
76 | /* --- Stash a new instruction --- */ | |
77 | ||
78 | #define INSTR(op_, argx_, argy_) do { \ | |
79 | DA_ENSURE(&iv, 1); \ | |
80 | DA(&iv)[DA_LEN(&iv)].op = (op_); \ | |
81 | DA(&iv)[DA_LEN(&iv)].argx = (argx_); \ | |
82 | DA(&iv)[DA_LEN(&iv)].argy = (argy_); \ | |
83 | DA_EXTEND(&iv, 1); \ | |
84 | } while (0) | |
85 | ||
86 | /* --- Main loop --- * | |
87 | * | |
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). | |
91 | */ | |
92 | ||
93 | #ifdef DEBUG | |
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; | |
45c0fd36 MW |
97 | case Z1 | 0: st = Z; printf("+ %lu\n", i - 1); break; |
98 | case Z1 | 1: st = X; printf("- %lu\n", i - 1); break; | |
f46efa79 | 99 | case X | 0: st = X0; break; |
45c0fd36 MW |
100 | case X0 | 1: st = X; printf("- %lu\n", i - 1); break; |
101 | case X0 | 0: st = Z; printf("+ %lu\n", i - 1); break; | |
f46efa79 | 102 | } |
103 | } | |
f9c435c0 MW |
104 | if (st >= X) printf("+ %lu\n", i - 1); |
105 | st = Z; | |
f46efa79 | 106 | #endif |
107 | ||
c29970a7 | 108 | bb = MPW_BITS - (d + 1)%MPW_BITS; |
bccb92dd | 109 | for (i = 0, mp_scan(&sc, p); i < d && mp_step(&sc); i++) { |
f46efa79 | 110 | switch (st | mp_bit(&sc)) { |
111 | case Z | 1: st = Z1; break; | |
45c0fd36 MW |
112 | case Z1 | 0: st = Z; op = MPRI_SUB; goto instr; |
113 | case Z1 | 1: st = X; op = MPRI_ADD; goto instr; | |
f46efa79 | 114 | case X | 0: st = X0; break; |
45c0fd36 MW |
115 | case X0 | 1: st = X; op = MPRI_ADD; goto instr; |
116 | case X0 | 0: st = Z; op = MPRI_SUB; goto instr; | |
f46efa79 | 117 | instr: |
118 | w = (d - i)/MPW_BITS + 1; | |
c29970a7 | 119 | b = (bb + i)%MPW_BITS; |
f46efa79 | 120 | INSTR(op | !!b, w, b); |
121 | } | |
122 | } | |
cd9aae84 | 123 | if (DA_LEN(&iv) && (DA(&iv)[DA_LEN(&iv) - 1].op & ~1u) == MPRI_SUB) { |
f4535c64 | 124 | mp_drop(r->p); |
125 | DA_DESTROY(&iv); | |
126 | return (-1); | |
f46efa79 | 127 | } |
128 | ||
129 | #undef INSTR | |
130 | ||
131 | /* --- Wrap up --- */ | |
132 | ||
133 | r->in = DA_LEN(&iv); | |
cd9aae84 MW |
134 | if (!r->in) |
135 | r->iv = 0; | |
136 | else if (!r->s) { | |
f46efa79 | 137 | r->iv = xmalloc(r->in * sizeof(mpreduce_instr)); |
138 | memcpy(r->iv, DA(&iv), r->in * sizeof(mpreduce_instr)); | |
139 | } else { | |
140 | r->iv = xmalloc(r->in * 2 * sizeof(mpreduce_instr)); | |
141 | for (i = 0; i < r->in; i++) { | |
142 | r->iv[i] = DA(&iv)[i]; | |
143 | op = r->iv[i].op & ~1u; | |
144 | w = r->iv[i].argx; | |
145 | b = r->iv[i].argy; | |
146 | b += r->s; | |
147 | if (b >= MPW_BITS) { | |
148 | b -= MPW_BITS; | |
149 | w--; | |
150 | } | |
151 | if (b) op |= 1; | |
152 | r->iv[i + r->in].op = op; | |
153 | r->iv[i + r->in].argx = w; | |
154 | r->iv[i + r->in].argy = b; | |
155 | } | |
156 | } | |
f4535c64 | 157 | DA_DESTROY(&iv); |
c29970a7 MW |
158 | |
159 | #ifdef DEBUG | |
160 | mpreduce_dump(r, stdout); | |
161 | #endif | |
f4535c64 | 162 | return (0); |
f46efa79 | 163 | } |
164 | ||
165 | /* --- @mpreduce_destroy@ --- * | |
166 | * | |
167 | * Arguments: @mpreduce *r@ = structure to free | |
168 | * | |
169 | * Returns: --- | |
170 | * | |
171 | * Use: Reclaims the resources from a reduction context. | |
172 | */ | |
173 | ||
174 | void mpreduce_destroy(mpreduce *r) | |
175 | { | |
176 | mp_drop(r->p); | |
cd9aae84 | 177 | if (r->iv) xfree(r->iv); |
f46efa79 | 178 | } |
179 | ||
180 | /* --- @mpreduce_dump@ --- * | |
181 | * | |
182 | * Arguments: @mpreduce *r@ = structure to dump | |
183 | * @FILE *fp@ = file to dump on | |
184 | * | |
185 | * Returns: --- | |
186 | * | |
187 | * Use: Dumps a reduction context. | |
188 | */ | |
189 | ||
190 | void mpreduce_dump(mpreduce *r, FILE *fp) | |
191 | { | |
192 | size_t i; | |
193 | static const char *opname[] = { "add", "addshift", "sub", "subshift" }; | |
194 | ||
195 | fprintf(fp, "mod = "); mp_writefile(r->p, fp, 16); | |
196 | fprintf(fp, "\n lim = %lu; s = %d\n", (unsigned long)r->lim, r->s); | |
197 | for (i = 0; i < r->in; i++) { | |
198 | assert(r->iv[i].op < N(opname)); | |
199 | fprintf(fp, " %s %lu %lu\n", | |
200 | opname[r->iv[i].op], | |
201 | (unsigned long)r->iv[i].argx, | |
202 | (unsigned long)r->iv[i].argy); | |
203 | } | |
204 | if (r->s) { | |
205 | fprintf(fp, "tail end charlie\n"); | |
206 | for (i = r->in; i < 2 * r->in; i++) { | |
207 | assert(r->iv[i].op < N(opname)); | |
208 | fprintf(fp, " %s %lu %lu\n", | |
209 | opname[r->iv[i].op], | |
210 | (unsigned long)r->iv[i].argx, | |
211 | (unsigned long)r->iv[i].argy); | |
212 | } | |
213 | } | |
214 | } | |
215 | ||
216 | /* --- @mpreduce_do@ --- * | |
217 | * | |
218 | * Arguments: @mpreduce *r@ = reduction context | |
219 | * @mp *d@ = destination | |
220 | * @mp *x@ = source | |
221 | * | |
222 | * Returns: Destination, @x@ reduced modulo the reduction poly. | |
223 | */ | |
224 | ||
225 | static void run(const mpreduce_instr *i, const mpreduce_instr *il, | |
226 | mpw *v, mpw z) | |
227 | { | |
228 | for (; i < il; i++) { | |
229 | #ifdef DEBUG | |
230 | mp vv; | |
231 | mp_build(&vv, v - i->argx, v + 1); | |
232 | printf(" 0x"); mp_writefile(&vv, stdout, 16); | |
233 | printf(" %c (0x%lx << %u) == 0x", | |
234 | (i->op & ~1u) == MPRI_ADD ? '+' : '-', | |
235 | (unsigned long)z, | |
236 | i->argy); | |
237 | #endif | |
238 | switch (i->op) { | |
239 | case MPRI_ADD: MPX_UADDN(v - i->argx, v + 1, z); break; | |
240 | case MPRI_ADDLSL: mpx_uaddnlsl(v - i->argx, v + 1, z, i->argy); break; | |
241 | case MPRI_SUB: MPX_USUBN(v - i->argx, v + 1, z); break; | |
242 | case MPRI_SUBLSL: mpx_usubnlsl(v - i->argx, v + 1, z, i->argy); break; | |
243 | default: | |
244 | abort(); | |
245 | } | |
246 | #ifdef DEBUG | |
247 | mp_build(&vv, v - i->argx, v + 1); | |
248 | mp_writefile(&vv, stdout, 16); | |
249 | printf("\n"); | |
250 | #endif | |
251 | } | |
252 | } | |
253 | ||
254 | mp *mpreduce_do(mpreduce *r, mp *d, mp *x) | |
255 | { | |
256 | mpw *v, *vl; | |
257 | const mpreduce_instr *il; | |
258 | mpw z; | |
259 | ||
260 | #ifdef DEBUG | |
261 | mp *_r = 0, *_rr = 0; | |
262 | #endif | |
263 | ||
264 | /* --- If source is negative, divide --- */ | |
265 | ||
a69a3efd | 266 | if (MP_NEGP(x)) { |
f46efa79 | 267 | mp_div(0, &d, x, r->p); |
268 | return (d); | |
269 | } | |
270 | ||
271 | /* --- Try to reuse the source's space --- */ | |
272 | ||
273 | MP_COPY(x); | |
274 | if (d) MP_DROP(d); | |
275 | MP_DEST(x, MP_LEN(x), x->f); | |
276 | ||
277 | /* --- Do the reduction --- */ | |
278 | ||
279 | #ifdef DEBUG | |
280 | _r = MP_NEW; | |
281 | mp_div(0, &_r, x, r->p); | |
282 | MP_PRINTX("x", x); | |
283 | _rr = 0; | |
284 | #endif | |
285 | ||
286 | il = r->iv + r->in; | |
287 | if (MP_LEN(x) >= r->lim) { | |
288 | v = x->v + r->lim; | |
289 | vl = x->vl; | |
290 | while (vl-- > v) { | |
291 | while (*vl) { | |
292 | z = *vl; | |
293 | *vl = 0; | |
294 | run(r->iv, il, vl, z); | |
295 | #ifdef DEBUG | |
106b481c MW |
296 | MP_PRINTX("x", x); |
297 | mp_div(0, &_rr, x, r->p); | |
298 | assert(MP_EQ(_r, _rr)); | |
f46efa79 | 299 | #endif |
300 | } | |
301 | } | |
302 | if (r->s) { | |
303 | while (*vl >> r->s) { | |
304 | z = *vl >> r->s; | |
305 | *vl &= ((1 << r->s) - 1); | |
306 | run(r->iv + r->in, il + r->in, vl, z); | |
307 | #ifdef DEBUG | |
106b481c MW |
308 | MP_PRINTX("x", x); |
309 | mp_div(0, &_rr, x, r->p); | |
310 | assert(MP_EQ(_r, _rr)); | |
f46efa79 | 311 | #endif |
312 | } | |
313 | } | |
314 | } | |
315 | ||
316 | /* --- Finishing touches --- */ | |
317 | ||
318 | MP_SHRINK(x); | |
319 | if (MP_CMP(x, >=, r->p)) | |
320 | x = mp_sub(x, x, r->p); | |
321 | ||
322 | /* --- Done --- */ | |
323 | ||
324 | #ifdef DEBUG | |
325 | assert(MP_EQ(_r, x)); | |
326 | mp_drop(_r); | |
327 | mp_drop(_rr); | |
328 | #endif | |
329 | return (x); | |
330 | } | |
331 | ||
332 | /* --- @mpreduce_exp@ --- * | |
333 | * | |
334 | * Arguments: @mpreduce *mr@ = pointer to reduction context | |
45c0fd36 MW |
335 | * @mp *d@ = fake destination |
336 | * @mp *a@ = base | |
337 | * @mp *e@ = exponent | |
f46efa79 | 338 | * |
45c0fd36 | 339 | * Returns: Result, %$a^e \bmod m$%. |
f46efa79 | 340 | */ |
341 | ||
342 | mp *mpreduce_exp(mpreduce *mr, mp *d, mp *a, mp *e) | |
343 | { | |
344 | mp *x = MP_ONE; | |
345 | mp *spare = (e->f & MP_BURN) ? MP_NEWSEC : MP_NEW; | |
346 | ||
347 | MP_SHRINK(e); | |
a69a3efd | 348 | MP_COPY(a); |
349 | if (MP_ZEROP(e)) | |
f46efa79 | 350 | ; |
a69a3efd | 351 | else { |
352 | if (MP_NEGP(e)) | |
353 | a = mp_modinv(a, a, mr->p); | |
354 | if (MP_LEN(e) < EXP_THRESH) | |
355 | EXP_SIMPLE(x, a, e); | |
356 | else | |
357 | EXP_WINDOW(x, a, e); | |
358 | } | |
359 | mp_drop(a); | |
f46efa79 | 360 | mp_drop(d); |
361 | mp_drop(spare); | |
362 | return (x); | |
363 | } | |
364 | ||
365 | /*----- Test rig ----------------------------------------------------------*/ | |
366 | ||
367 | ||
368 | #ifdef TEST_RIG | |
369 | ||
370 | #define MP(x) mp_readstring(MP_NEW, #x, 0, 0) | |
371 | ||
372 | static int vreduce(dstr *v) | |
373 | { | |
374 | mp *d = *(mp **)v[0].buf; | |
375 | mp *n = *(mp **)v[1].buf; | |
376 | mp *r = *(mp **)v[2].buf; | |
377 | mp *c; | |
378 | int ok = 1; | |
379 | mpreduce rr; | |
380 | ||
381 | mpreduce_create(&rr, d); | |
382 | c = mpreduce_do(&rr, MP_NEW, n); | |
383 | if (!MP_EQ(c, r)) { | |
384 | fprintf(stderr, "\n*** reduction failed\n*** "); | |
385 | mpreduce_dump(&rr, stderr); | |
386 | fprintf(stderr, "\n*** n = "); mp_writefile(n, stderr, 10); | |
387 | fprintf(stderr, "\n*** r = "); mp_writefile(r, stderr, 10); | |
388 | fprintf(stderr, "\n*** c = "); mp_writefile(c, stderr, 10); | |
389 | fprintf(stderr, "\n"); | |
390 | ok = 0; | |
391 | } | |
392 | mpreduce_destroy(&rr); | |
393 | mp_drop(n); mp_drop(d); mp_drop(r); mp_drop(c); | |
394 | assert(mparena_count(MPARENA_GLOBAL) == 0); | |
395 | return (ok); | |
396 | } | |
397 | ||
398 | static int vmodexp(dstr *v) | |
399 | { | |
400 | mp *p = *(mp **)v[0].buf; | |
401 | mp *g = *(mp **)v[1].buf; | |
402 | mp *x = *(mp **)v[2].buf; | |
403 | mp *r = *(mp **)v[3].buf; | |
404 | mp *c; | |
405 | int ok = 1; | |
406 | mpreduce rr; | |
407 | ||
408 | mpreduce_create(&rr, p); | |
409 | c = mpreduce_exp(&rr, MP_NEW, g, x); | |
410 | if (!MP_EQ(c, r)) { | |
411 | fprintf(stderr, "\n*** modexp failed\n*** "); | |
412 | fprintf(stderr, "\n*** p = "); mp_writefile(p, stderr, 10); | |
413 | fprintf(stderr, "\n*** g = "); mp_writefile(g, stderr, 10); | |
414 | fprintf(stderr, "\n*** x = "); mp_writefile(x, stderr, 10); | |
415 | fprintf(stderr, "\n*** c = "); mp_writefile(c, stderr, 10); | |
416 | fprintf(stderr, "\n*** r = "); mp_writefile(r, stderr, 10); | |
417 | fprintf(stderr, "\n"); | |
418 | ok = 0; | |
419 | } | |
420 | mpreduce_destroy(&rr); | |
421 | mp_drop(p); mp_drop(g); mp_drop(r); mp_drop(x); mp_drop(c); | |
422 | assert(mparena_count(MPARENA_GLOBAL) == 0); | |
423 | return (ok); | |
424 | } | |
425 | ||
426 | static test_chunk defs[] = { | |
427 | { "reduce", vreduce, { &type_mp, &type_mp, &type_mp, 0 } }, | |
428 | { "modexp", vmodexp, { &type_mp, &type_mp, &type_mp, &type_mp, 0 } }, | |
429 | { 0, 0, { 0 } } | |
430 | }; | |
431 | ||
432 | int main(int argc, char *argv[]) | |
433 | { | |
434 | test_run(argc, argv, defs, SRCDIR"/tests/mpreduce"); | |
435 | return (0); | |
436 | } | |
437 | ||
438 | #endif | |
439 | ||
440 | /*----- That's all, folks -------------------------------------------------*/ |