5 * Prime generation glue
7 * (c) 1999 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 ------------------------------------------------------*/
45 /*----- Standard prime filter ---------------------------------------------*/
47 /* --- @pgen_filter@ --- */
49 int pgen_filter(int rq, pgen_event *ev, void *p)
51 pgen_filterctx *f = p;
56 rc = pfilt_create(&f->f, ev->m);
70 if (rc == PGEN_FAIL && !((f->step | f->f.m->v[0]) & 1))
71 rc = pfilt_step(&f->f, 1);
72 while (rc == PGEN_FAIL)
73 rc = pfilt_step(&f->f, f->step);
74 ev->m = MP_COPY(f->f.m);
78 /* --- @pgen_jump@ --- *
80 * Similar to the standard @pgen_filter@, but jumps in large steps rather
84 int pgen_jump(int rq, pgen_event *ev, void *p)
92 mp_gcd(&g, 0, 0, ev->m, f->j->m);
93 if (MP_CMP(g, >, MP_ONE)) {
98 rc = pfilt_create(&f->f, ev->m);
103 rc = pfilt_jump(&f->f, f->j);
106 pfilt_destroy(&f->f);
110 while (rc == PGEN_FAIL)
111 rc = pfilt_jump(&f->f, f->j);
112 ev->m = MP_COPY(f->f.m);
116 /*----- Standard prime test -----------------------------------------------*/
118 /* --- @pgen_test@ --- */
120 int pgen_test(int rq, pgen_event *ev, void *p)
127 rabin_create(r, ev->m);
132 rc = rabin_rtest(r, MP_TWO);
134 mp *a = mprand_range(MP_NEW, ev->m, ev->r, 0);
135 rc = rabin_rtest(r, a);
148 /*----- The main driver ---------------------------------------------------*/
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
164 * Returns: Pointer to final result, or null.
166 * Use: A generalized prime-number search skeleton. Yes, that's a
167 * scary number of arguments.
170 mp *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)
180 enum { P_STEP, P_TEST };
182 /* --- Set up the initial event block --- */
191 ev.r = fibrand_create(0);
193 /* --- Tell the event handler we're under way --- */
195 if (event && event(PGEN_BEGIN, &ev, ectx) == PGEN_ABORT) {
196 ev.r->ops->destroy(ev.r);
200 /* --- Set up for the initial call --- */
202 proc = step; ctx = sctx; p = P_STEP; rq = PGEN_BEGIN;
204 /* --- Enter the great maelstrom of state transitions --- */
213 #define A_ENDSTEP 16u
216 /* --- Call the procedure and decide what to do next --- */
218 rc = proc(rq, &ev, ctx);
225 proc = test; ctx = tctx; p = P_TEST;
230 act |= A_TEST | A_EVENT;
234 proc = test; ctx = tctx; p = P_TEST;
241 act |= A_ENDTEST | A_EVENT;
242 proc = step; ctx = sctx; p = P_STEP;
247 act |= A_EVENT | A_DONE | A_ENDSTEP;
252 act |= A_EVENT | A_DONE;
253 if (p == P_TEST || rq == PGEN_TRY)
255 if (p == P_TEST && rq != PGEN_BEGIN)
259 assert(((void)"Invalid response from function", 0));
263 /* --- If decrementing counters is requested, do that --- */
265 if ((act & A_STEP) && steps) {
267 if (ev.steps == steps) {
268 act |= A_EVENT | A_ENDSTEP | A_DONE;
274 if ((act & A_TEST) && tests) {
276 if (ev.tests == tests) {
277 act |= A_ENDTEST | A_ENDSTEP | A_DONE;
282 /* --- Report an event if so directed --- */
284 if ((act & A_EVENT) && event && event(rc, &ev, ectx) == PGEN_ABORT) {
286 if (!(act & A_DONE)) {
287 act |= A_ENDSTEP | A_DONE;
293 /* --- Close down tester and stepper functions --- */
296 test(PGEN_DONE, &ev, tctx);
298 step(PGEN_DONE, &ev, sctx);
300 /* --- Stop the entire test if necessary --- */
306 /* --- Tidy up and return --- */
308 if (rc == PGEN_ABORT) {
312 ev.r->ops->destroy(ev.r);
318 /* --- @pgen_primep@ --- *
320 * Arguments: @mp *p@ = a number to check
321 * @grand *gr@ = a random number source
323 * Returns: Nonzero if @p@ is really prime.
326 int pgen_primep(mp *p, grand *gr)
332 if (MP_NEGP(p)) return (0);
333 switch (pfilt_smallfactor(p)) {
334 case PGEN_DONE: return (1);
335 case PGEN_FAIL: return (0);
338 for (i = 32; i; i--) {
339 x = mprand_range(x, p, gr, 0);
340 if (rabin_rtest(&r, x) == PGEN_FAIL)
348 /*----- Test rig ----------------------------------------------------------*/
352 #include <mLib/testrig.h>
354 static int verify(dstr *v)
356 mp *m = *(mp **)v[0].buf;
357 mp *q = *(mp **)v[1].buf;
365 p = pgen("p", MP_NEW, m, pgen_evspin, 0, 0, pgen_filter, &pf,
366 rabin_iters(mp_bits(m)), pgen_test, &r);
367 if (!p || !MP_EQ(p, q)) {
368 fputs("\n*** pgen failed", stderr);
369 fputs("\nm = ", stderr); mp_writefile(m, stderr, 10);
370 fputs("\np = ", stderr); mp_writefile(p, stderr, 10);
371 fputs("\nq = ", stderr); mp_writefile(q, stderr, 10);
379 assert(mparena_count(MPARENA_GLOBAL) == 0);
383 static test_chunk tests[] = {
384 { "pgen", verify, { &type_mp, &type_mp, 0 } },
388 int main(int argc, char *argv[])
391 test_run(argc, argv, tests, SRCDIR "/tests/pgen");
396 /*----- That's all, folks -------------------------------------------------*/