chiark / gitweb /
utils/mpreducetests.py: Tool to generate unpleasant mpreduce tests.
[catacomb] / mpreduce.c
CommitLineData
f46efa79 1/* -*-c-*-
2 *
a69a3efd 3 * $Id$
f46efa79 4 *
5 * Efficient reduction modulo nice primes
6 *
7 * (c) 2004 Straylight/Edgeware
8 */
9
10/*----- Licensing notice --------------------------------------------------*
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.
18 *
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.
23 *
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
41DA_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 55int 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;
63 size_t w, b;
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;
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;
102 }
103 }
f9c435c0
MW
104 if (st >= X) printf("+ %lu\n", i - 1);
105 st = Z;
f46efa79 106#endif
107
bccb92dd 108 for (i = 0, mp_scan(&sc, p); i < d && mp_step(&sc); i++) {
f46efa79 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;
116 instr:
117 w = (d - i)/MPW_BITS + 1;
118 b = (MPW_BITS + i - d - 1)%MPW_BITS;
119 INSTR(op | !!b, w, b);
120 }
121 }
cd9aae84 122 if (DA_LEN(&iv) && (DA(&iv)[DA_LEN(&iv) - 1].op & ~1u) == MPRI_SUB) {
f4535c64 123 mp_drop(r->p);
124 DA_DESTROY(&iv);
125 return (-1);
f46efa79 126 }
127
128#undef INSTR
129
130 /* --- Wrap up --- */
131
132 r->in = DA_LEN(&iv);
cd9aae84
MW
133 if (!r->in)
134 r->iv = 0;
135 else if (!r->s) {
f46efa79 136 r->iv = xmalloc(r->in * sizeof(mpreduce_instr));
137 memcpy(r->iv, DA(&iv), r->in * sizeof(mpreduce_instr));
138 } else {
139 r->iv = xmalloc(r->in * 2 * sizeof(mpreduce_instr));
140 for (i = 0; i < r->in; i++) {
141 r->iv[i] = DA(&iv)[i];
142 op = r->iv[i].op & ~1u;
143 w = r->iv[i].argx;
144 b = r->iv[i].argy;
145 b += r->s;
146 if (b >= MPW_BITS) {
147 b -= MPW_BITS;
148 w--;
149 }
150 if (b) op |= 1;
151 r->iv[i + r->in].op = op;
152 r->iv[i + r->in].argx = w;
153 r->iv[i + r->in].argy = b;
154 }
155 }
f4535c64 156 DA_DESTROY(&iv);
157 return (0);
f46efa79 158}
159
160/* --- @mpreduce_destroy@ --- *
161 *
162 * Arguments: @mpreduce *r@ = structure to free
163 *
164 * Returns: ---
165 *
166 * Use: Reclaims the resources from a reduction context.
167 */
168
169void mpreduce_destroy(mpreduce *r)
170{
171 mp_drop(r->p);
cd9aae84 172 if (r->iv) xfree(r->iv);
f46efa79 173}
174
175/* --- @mpreduce_dump@ --- *
176 *
177 * Arguments: @mpreduce *r@ = structure to dump
178 * @FILE *fp@ = file to dump on
179 *
180 * Returns: ---
181 *
182 * Use: Dumps a reduction context.
183 */
184
185void mpreduce_dump(mpreduce *r, FILE *fp)
186{
187 size_t i;
188 static const char *opname[] = { "add", "addshift", "sub", "subshift" };
189
190 fprintf(fp, "mod = "); mp_writefile(r->p, fp, 16);
191 fprintf(fp, "\n lim = %lu; s = %d\n", (unsigned long)r->lim, r->s);
192 for (i = 0; i < r->in; i++) {
193 assert(r->iv[i].op < N(opname));
194 fprintf(fp, " %s %lu %lu\n",
195 opname[r->iv[i].op],
196 (unsigned long)r->iv[i].argx,
197 (unsigned long)r->iv[i].argy);
198 }
199 if (r->s) {
200 fprintf(fp, "tail end charlie\n");
201 for (i = r->in; i < 2 * r->in; i++) {
202 assert(r->iv[i].op < N(opname));
203 fprintf(fp, " %s %lu %lu\n",
204 opname[r->iv[i].op],
205 (unsigned long)r->iv[i].argx,
206 (unsigned long)r->iv[i].argy);
207 }
208 }
209}
210
211/* --- @mpreduce_do@ --- *
212 *
213 * Arguments: @mpreduce *r@ = reduction context
214 * @mp *d@ = destination
215 * @mp *x@ = source
216 *
217 * Returns: Destination, @x@ reduced modulo the reduction poly.
218 */
219
220static void run(const mpreduce_instr *i, const mpreduce_instr *il,
221 mpw *v, mpw z)
222{
223 for (; i < il; i++) {
224#ifdef DEBUG
225 mp vv;
226 mp_build(&vv, v - i->argx, v + 1);
227 printf(" 0x"); mp_writefile(&vv, stdout, 16);
228 printf(" %c (0x%lx << %u) == 0x",
229 (i->op & ~1u) == MPRI_ADD ? '+' : '-',
230 (unsigned long)z,
231 i->argy);
232#endif
233 switch (i->op) {
234 case MPRI_ADD: MPX_UADDN(v - i->argx, v + 1, z); break;
235 case MPRI_ADDLSL: mpx_uaddnlsl(v - i->argx, v + 1, z, i->argy); break;
236 case MPRI_SUB: MPX_USUBN(v - i->argx, v + 1, z); break;
237 case MPRI_SUBLSL: mpx_usubnlsl(v - i->argx, v + 1, z, i->argy); break;
238 default:
239 abort();
240 }
241#ifdef DEBUG
242 mp_build(&vv, v - i->argx, v + 1);
243 mp_writefile(&vv, stdout, 16);
244 printf("\n");
245#endif
246 }
247}
248
249mp *mpreduce_do(mpreduce *r, mp *d, mp *x)
250{
251 mpw *v, *vl;
252 const mpreduce_instr *il;
253 mpw z;
254
255#ifdef DEBUG
256 mp *_r = 0, *_rr = 0;
257#endif
258
259 /* --- If source is negative, divide --- */
260
a69a3efd 261 if (MP_NEGP(x)) {
f46efa79 262 mp_div(0, &d, x, r->p);
263 return (d);
264 }
265
266 /* --- Try to reuse the source's space --- */
267
268 MP_COPY(x);
269 if (d) MP_DROP(d);
270 MP_DEST(x, MP_LEN(x), x->f);
271
272 /* --- Do the reduction --- */
273
274#ifdef DEBUG
275 _r = MP_NEW;
276 mp_div(0, &_r, x, r->p);
277 MP_PRINTX("x", x);
278 _rr = 0;
279#endif
280
281 il = r->iv + r->in;
282 if (MP_LEN(x) >= r->lim) {
283 v = x->v + r->lim;
284 vl = x->vl;
285 while (vl-- > v) {
286 while (*vl) {
287 z = *vl;
288 *vl = 0;
289 run(r->iv, il, vl, z);
290#ifdef DEBUG
106b481c
MW
291 MP_PRINTX("x", x);
292 mp_div(0, &_rr, x, r->p);
293 assert(MP_EQ(_r, _rr));
f46efa79 294#endif
295 }
296 }
297 if (r->s) {
298 while (*vl >> r->s) {
299 z = *vl >> r->s;
300 *vl &= ((1 << r->s) - 1);
301 run(r->iv + r->in, il + r->in, vl, z);
302#ifdef DEBUG
106b481c
MW
303 MP_PRINTX("x", x);
304 mp_div(0, &_rr, x, r->p);
305 assert(MP_EQ(_r, _rr));
f46efa79 306#endif
307 }
308 }
309 }
310
311 /* --- Finishing touches --- */
312
313 MP_SHRINK(x);
314 if (MP_CMP(x, >=, r->p))
315 x = mp_sub(x, x, r->p);
316
317 /* --- Done --- */
318
319#ifdef DEBUG
320 assert(MP_EQ(_r, x));
321 mp_drop(_r);
322 mp_drop(_rr);
323#endif
324 return (x);
325}
326
327/* --- @mpreduce_exp@ --- *
328 *
329 * Arguments: @mpreduce *mr@ = pointer to reduction context
330 * @mp *d@ = fake destination
331 * @mp *a@ = base
332 * @mp *e@ = exponent
333 *
334 * Returns: Result, %$a^e \bmod m$%.
335 */
336
337mp *mpreduce_exp(mpreduce *mr, mp *d, mp *a, mp *e)
338{
339 mp *x = MP_ONE;
340 mp *spare = (e->f & MP_BURN) ? MP_NEWSEC : MP_NEW;
341
342 MP_SHRINK(e);
a69a3efd 343 MP_COPY(a);
344 if (MP_ZEROP(e))
f46efa79 345 ;
a69a3efd 346 else {
347 if (MP_NEGP(e))
348 a = mp_modinv(a, a, mr->p);
349 if (MP_LEN(e) < EXP_THRESH)
350 EXP_SIMPLE(x, a, e);
351 else
352 EXP_WINDOW(x, a, e);
353 }
354 mp_drop(a);
f46efa79 355 mp_drop(d);
356 mp_drop(spare);
357 return (x);
358}
359
360/*----- Test rig ----------------------------------------------------------*/
361
362
363#ifdef TEST_RIG
364
365#define MP(x) mp_readstring(MP_NEW, #x, 0, 0)
366
367static int vreduce(dstr *v)
368{
369 mp *d = *(mp **)v[0].buf;
370 mp *n = *(mp **)v[1].buf;
371 mp *r = *(mp **)v[2].buf;
372 mp *c;
373 int ok = 1;
374 mpreduce rr;
375
376 mpreduce_create(&rr, d);
377 c = mpreduce_do(&rr, MP_NEW, n);
378 if (!MP_EQ(c, r)) {
379 fprintf(stderr, "\n*** reduction failed\n*** ");
380 mpreduce_dump(&rr, stderr);
381 fprintf(stderr, "\n*** n = "); mp_writefile(n, stderr, 10);
382 fprintf(stderr, "\n*** r = "); mp_writefile(r, stderr, 10);
383 fprintf(stderr, "\n*** c = "); mp_writefile(c, stderr, 10);
384 fprintf(stderr, "\n");
385 ok = 0;
386 }
387 mpreduce_destroy(&rr);
388 mp_drop(n); mp_drop(d); mp_drop(r); mp_drop(c);
389 assert(mparena_count(MPARENA_GLOBAL) == 0);
390 return (ok);
391}
392
393static int vmodexp(dstr *v)
394{
395 mp *p = *(mp **)v[0].buf;
396 mp *g = *(mp **)v[1].buf;
397 mp *x = *(mp **)v[2].buf;
398 mp *r = *(mp **)v[3].buf;
399 mp *c;
400 int ok = 1;
401 mpreduce rr;
402
403 mpreduce_create(&rr, p);
404 c = mpreduce_exp(&rr, MP_NEW, g, x);
405 if (!MP_EQ(c, r)) {
406 fprintf(stderr, "\n*** modexp failed\n*** ");
407 fprintf(stderr, "\n*** p = "); mp_writefile(p, stderr, 10);
408 fprintf(stderr, "\n*** g = "); mp_writefile(g, stderr, 10);
409 fprintf(stderr, "\n*** x = "); mp_writefile(x, stderr, 10);
410 fprintf(stderr, "\n*** c = "); mp_writefile(c, stderr, 10);
411 fprintf(stderr, "\n*** r = "); mp_writefile(r, stderr, 10);
412 fprintf(stderr, "\n");
413 ok = 0;
414 }
415 mpreduce_destroy(&rr);
416 mp_drop(p); mp_drop(g); mp_drop(r); mp_drop(x); mp_drop(c);
417 assert(mparena_count(MPARENA_GLOBAL) == 0);
418 return (ok);
419}
420
421static test_chunk defs[] = {
422 { "reduce", vreduce, { &type_mp, &type_mp, &type_mp, 0 } },
423 { "modexp", vmodexp, { &type_mp, &type_mp, &type_mp, &type_mp, 0 } },
424 { 0, 0, { 0 } }
425};
426
427int main(int argc, char *argv[])
428{
429 test_run(argc, argv, defs, SRCDIR"/tests/mpreduce");
430 return (0);
431}
432
433#endif
434
435/*----- That's all, folks -------------------------------------------------*/