Use auto-version machinery for building.
[u/mdw/catacomb] / pgen.c
CommitLineData
0f5ec153 1/* -*-c-*-
2 *
a69a3efd 3 * $Id$
0f5ec153 4 *
581c854e 5 * Prime generation glue
0f5ec153 6 *
7 * (c) 1999 Straylight/Edgeware
8 */
9
45c0fd36 10/*----- Licensing notice --------------------------------------------------*
0f5ec153 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 *
0f5ec153 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 *
0f5ec153 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
0f5ec153 30/*----- Header files ------------------------------------------------------*/
31
581c854e 32#include <assert.h>
33#include <stdio.h>
34#include <stdlib.h>
35#include <string.h>
36
37#include "fibrand.h"
38#include "grand.h"
0f5ec153 39#include "mp.h"
581c854e 40#include "mprand.h"
0f5ec153 41#include "pgen.h"
581c854e 42#include "pfilt.h"
0f5ec153 43#include "rabin.h"
44
581c854e 45/*----- Standard prime filter ---------------------------------------------*/
0f5ec153 46
581c854e 47/* --- @pgen_filter@ --- */
0f5ec153 48
581c854e 49int pgen_filter(int rq, pgen_event *ev, void *p)
0f5ec153 50{
581c854e 51 pgen_filterctx *f = p;
57541e97 52 int rc = PGEN_FAIL;
581c854e 53
54 switch (rq) {
55 case PGEN_BEGIN:
56 rc = pfilt_create(&f->f, ev->m);
57 mp_drop(ev->m);
58 break;
59 case PGEN_TRY:
60 mp_drop(ev->m);
581c854e 61 break;
62 case PGEN_DONE:
63 pfilt_destroy(&f->f);
64 return (PGEN_DONE);
57541e97 65 default:
66 rc = PGEN_ABORT;
67 break;
0f5ec153 68 }
45c0fd36 69
57541e97 70 if (rc == PGEN_FAIL && !((f->step | f->f.m->v[0]) & 1))
71 rc = pfilt_step(&f->f, 1);
581c854e 72 while (rc == PGEN_FAIL)
73 rc = pfilt_step(&f->f, f->step);
74 ev->m = MP_COPY(f->f.m);
0f5ec153 75 return (rc);
76}
77
581c854e 78/* --- @pgen_jump@ --- *
0f5ec153 79 *
581c854e 80 * Similar to the standard @pgen_filter@, but jumps in large steps rather
81 * than small ones.
0f5ec153 82 */
83
581c854e 84int pgen_jump(int rq, pgen_event *ev, void *p)
0f5ec153 85{
581c854e 86 pgen_jumpctx *f = p;
87 int rc = PGEN_ABORT;
0f5ec153 88
581c854e 89 switch (rq) {
8b021c3f 90 case PGEN_BEGIN: {
91 mp *g = MP_NEW;
92 mp_gcd(&g, 0, 0, ev->m, f->j->m);
93 if (MP_CMP(g, >, MP_ONE)) {
94 mp_drop(g);
95 return (PGEN_ABORT);
96 }
97 mp_drop(g);
581c854e 98 rc = pfilt_create(&f->f, ev->m);
99 mp_drop(ev->m);
8b021c3f 100 } break;
581c854e 101 case PGEN_TRY:
102 mp_drop(ev->m);
103 rc = pfilt_jump(&f->f, f->j);
104 break;
105 case PGEN_DONE:
106 pfilt_destroy(&f->f);
107 return (PGEN_DONE);
108 }
45c0fd36 109
581c854e 110 while (rc == PGEN_FAIL)
111 rc = pfilt_jump(&f->f, f->j);
112 ev->m = MP_COPY(f->f.m);
113 return (rc);
114}
0f5ec153 115
581c854e 116/*----- Standard prime test -----------------------------------------------*/
0f5ec153 117
581c854e 118/* --- @pgen_test@ --- */
0f5ec153 119
581c854e 120int pgen_test(int rq, pgen_event *ev, void *p)
121{
122 rabin *r = p;
123 int rc = PGEN_ABORT;
0f5ec153 124
581c854e 125 switch (rq) {
126 case PGEN_BEGIN:
127 rabin_create(r, ev->m);
128 rc = PGEN_TRY;
129 break;
283b9af0 130 case PGEN_TRY:
131 if (!ev->tests)
132 rc = rabin_rtest(r, MP_TWO);
133 else {
134 mp *a = mprand_range(MP_NEW, ev->m, ev->r, 0);
135 rc = rabin_rtest(r, a);
136 mp_drop(a);
137 }
138 break;
581c854e 139 case PGEN_DONE:
140 rabin_destroy(r);
141 rc = PGEN_DONE;
142 break;
0f5ec153 143 }
144
0f5ec153 145 return (rc);
146}
147
581c854e 148/*----- The main driver ---------------------------------------------------*/
149
150/* --- @pgen@ --- *
bd98b2df 151 *
581c854e 152 * Arguments: @const char *name@ = name of the value being searched for
153 * @mp *d@ = destination for the result integer
154 * @mp *m@ = start value to pass to stepper
155 * @pgen_proc *event@ = event handler function
156 * @void *ectx@ = context argument for event andler
157 * @unsigned steps@ = number of steps to take in search
158 * @pgen_proc *step@ = stepper function to use
159 * @void *sctx@ = context argument for stepper
160 * @unsigned tests@ = number of tests to make
161 * @pgen_proc *test@ = tester function to use
162 * @void *tctx@ = context argument for tester
bd98b2df 163 *
581c854e 164 * Returns: Pointer to final result, or null.
bd98b2df 165 *
581c854e 166 * Use: A generalized prime-number search skeleton. Yes, that's a
167 * scary number of arguments.
bd98b2df 168 */
169
581c854e 170mp *pgen(const char *name, mp *d, mp *m, pgen_proc *event, void *ectx,
171 unsigned steps, pgen_proc *step, void *sctx,
172 unsigned tests, pgen_proc *test, void *tctx)
bd98b2df 173{
581c854e 174 pgen_event ev;
175 int rq, rc;
176 pgen_proc *proc;
177 void *ctx;
4108c8d2 178 int p;
179
180 enum { P_STEP, P_TEST };
bd98b2df 181
581c854e 182 /* --- Set up the initial event block --- */
bd98b2df 183
581c854e 184 ev.name = name;
185 if (m)
186 ev.m = MP_COPY(m);
187 else
188 ev.m = 0;
283b9af0 189 ev.steps = 0;
190 ev.tests = 0;
581c854e 191 ev.r = fibrand_create(0);
bd98b2df 192
581c854e 193 /* --- Tell the event handler we're under way --- */
bd98b2df 194
4108c8d2 195 if (event && event(PGEN_BEGIN, &ev, ectx) == PGEN_ABORT) {
196 ev.r->ops->destroy(ev.r);
581c854e 197 return (0);
4108c8d2 198 }
bd98b2df 199
581c854e 200 /* --- Set up for the initial call --- */
bd98b2df 201
4108c8d2 202 proc = step; ctx = sctx; p = P_STEP; rq = PGEN_BEGIN;
0f5ec153 203
581c854e 204 /* --- Enter the great maelstrom of state transitions --- */
0f5ec153 205
581c854e 206 for (;;) {
207 unsigned act = 0;
208
4108c8d2 209#define A_STEP 1u
210#define A_TEST 2u
211#define A_EVENT 4u
212#define A_ENDTEST 8u
213#define A_ENDSTEP 16u
214#define A_DONE 32u
581c854e 215
216 /* --- Call the procedure and decide what to do next --- */
217
218 rc = proc(rq, &ev, ctx);
219 switch (rc) {
220 case PGEN_TRY:
4108c8d2 221 if (p == P_TEST)
581c854e 222 rq = PGEN_TRY;
223 else {
224 act |= A_EVENT;
4108c8d2 225 proc = test; ctx = tctx; p = P_TEST;
581c854e 226 rq = PGEN_BEGIN;
227 }
228 break;
229 case PGEN_PASS:
230 act |= A_TEST | A_EVENT;
4108c8d2 231 if (p == P_TEST)
581c854e 232 rq = PGEN_TRY;
233 else {
4108c8d2 234 proc = test; ctx = tctx; p = P_TEST;
581c854e 235 rq = PGEN_BEGIN;
236 }
237 break;
238 case PGEN_FAIL:
239 act |= A_STEP;
4108c8d2 240 if (p == P_TEST) {
581c854e 241 act |= A_ENDTEST | A_EVENT;
4108c8d2 242 proc = step; ctx = sctx; p = P_STEP;
581c854e 243 }
244 rq = PGEN_TRY;
245 break;
246 case PGEN_DONE:
247 act |= A_EVENT | A_DONE | A_ENDSTEP;
4108c8d2 248 if (p == P_TEST)
581c854e 249 act |= A_ENDTEST;
250 break;
251 case PGEN_ABORT:
252 act |= A_EVENT | A_DONE;
4108c8d2 253 if (p == P_TEST || rq == PGEN_TRY)
581c854e 254 act |= A_ENDSTEP;
4108c8d2 255 if (p == P_TEST && rq != PGEN_BEGIN)
581c854e 256 act |= A_ENDTEST;
257 break;
258 default:
259 assert(((void)"Invalid response from function", 0));
260 break;
261 }
0f5ec153 262
581c854e 263 /* --- If decrementing counters is requested, do that --- */
0f5ec153 264
581c854e 265 if ((act & A_STEP) && steps) {
283b9af0 266 ev.steps++;
267 if (ev.steps == steps) {
581c854e 268 act |= A_EVENT | A_ENDSTEP | A_DONE;
269 rc = PGEN_ABORT;
270 }
283b9af0 271 ev.tests = 0;
581c854e 272 }
0f5ec153 273
581c854e 274 if ((act & A_TEST) && tests) {
283b9af0 275 ev.tests++;
276 if (ev.tests == tests) {
581c854e 277 act |= A_ENDTEST | A_ENDSTEP | A_DONE;
278 rc = PGEN_DONE;
279 }
0f5ec153 280 }
0f5ec153 281
581c854e 282 /* --- Report an event if so directed --- */
bd98b2df 283
581c854e 284 if ((act & A_EVENT) && event && event(rc, &ev, ectx) == PGEN_ABORT) {
285 rc = PGEN_ABORT;
286 if (!(act & A_DONE)) {
287 act |= A_ENDSTEP | A_DONE;
4108c8d2 288 if (p == P_TEST)
581c854e 289 act |= A_ENDTEST;
290 }
45c0fd36 291 }
bd98b2df 292
581c854e 293 /* --- Close down tester and stepper functions --- */
0f5ec153 294
581c854e 295 if (act & A_ENDTEST)
296 test(PGEN_DONE, &ev, tctx);
297 if (act & A_ENDSTEP)
298 step(PGEN_DONE, &ev, sctx);
299
300 /* --- Stop the entire test if necessary --- */
301
302 if (act & A_DONE)
303 break;
304 }
305
306 /* --- Tidy up and return --- */
307
308 if (rc == PGEN_ABORT) {
309 mp_drop(ev.m);
310 ev.m = 0;
311 }
312 ev.r->ops->destroy(ev.r);
eab06f16 313 mp_drop(d);
581c854e 314
315 return (ev.m);
0f5ec153 316}
317
34e4f738 318/* --- @pgen_primep@ --- *
319 *
320 * Arguments: @mp *p@ = a number to check
321 * @grand *gr@ = a random number source
322 *
323 * Returns: Nonzero if @p@ is really prime.
324 */
325
326int pgen_primep(mp *p, grand *gr)
327{
4108c8d2 328 int i;
34e4f738 329 rabin r;
330 mp *x = MP_NEW;
331
a69a3efd 332 if (MP_NEGP(p)) return (0);
34e4f738 333 switch (pfilt_smallfactor(p)) {
334 case PGEN_DONE: return (1);
335 case PGEN_FAIL: return (0);
336 }
337 rabin_create(&r, p);
4108c8d2 338 for (i = 32; i; i--) {
34e4f738 339 x = mprand_range(x, p, gr, 0);
340 if (rabin_rtest(&r, x) == PGEN_FAIL)
341 break;
34e4f738 342 }
343 MP_DROP(x);
344 rabin_destroy(&r);
345 return (!i);
346}
347
581c854e 348/*----- Test rig ----------------------------------------------------------*/
0f5ec153 349
350#ifdef TEST_RIG
351
352#include <mLib/testrig.h>
353
d31f4a79
MW
354static int t_primep(dstr *v)
355{
356 mp *m = *(mp **)v[0].buf;
357 int e = *(int *)v[1].buf;
358 int r;
359 grand *rng;
360 int ok = 1;
361
362 rng = fibrand_create(0);
363 r = pgen_primep(m, rng);
364 GR_DESTROY(rng);
365 if (e != r) {
366 fputs("\n*** primep failed", stderr);
367 fputs("\nm = ", stderr); mp_writefile(m, stderr, 10);
368 fprintf(stderr, "\nexpected %d", e);
369 fprintf(stderr, "\nreported %d", r);
370 fputc('\n', stderr);
371 ok = 0;
372 }
373
374 mp_drop(m);
375 assert(mparena_count(MPARENA_GLOBAL) == 0);
45c0fd36 376 return (ok);
d31f4a79
MW
377}
378
0f5ec153 379static int verify(dstr *v)
380{
381 mp *m = *(mp **)v[0].buf;
581c854e 382 mp *q = *(mp **)v[1].buf;
383 mp *p;
0f5ec153 384 int ok = 1;
385
581c854e 386 pgen_filterctx pf;
387 rabin r;
0f5ec153 388
581c854e 389 pf.step = 2;
390 p = pgen("p", MP_NEW, m, pgen_evspin, 0, 0, pgen_filter, &pf,
391 rabin_iters(mp_bits(m)), pgen_test, &r);
22bab86c 392 if (!p || !MP_EQ(p, q)) {
581c854e 393 fputs("\n*** pgen failed", stderr);
0f5ec153 394 fputs("\nm = ", stderr); mp_writefile(m, stderr, 10);
581c854e 395 fputs("\np = ", stderr); mp_writefile(p, stderr, 10);
396 fputs("\nq = ", stderr); mp_writefile(q, stderr, 10);
0f5ec153 397 fputc('\n', stderr);
398 ok = 0;
399 }
400
401 mp_drop(m);
581c854e 402 mp_drop(q);
eab06f16 403 mp_drop(p);
d94f85ac 404 assert(mparena_count(MPARENA_GLOBAL) == 0);
0f5ec153 405 return (ok);
406}
407
408static test_chunk tests[] = {
409 { "pgen", verify, { &type_mp, &type_mp, 0 } },
d31f4a79 410 { "primep", t_primep, { &type_mp, &type_int, 0 } },
0f5ec153 411 { 0, 0, { 0 } }
412};
413
414int main(int argc, char *argv[])
415{
416 sub_init();
417 test_run(argc, argv, tests, SRCDIR "/tests/pgen");
418 return (0);
419}
0f5ec153 420#endif
421
422/*----- That's all, folks -------------------------------------------------*/