3 * $Id: pgen.c,v 1.6 2000/10/08 12:11:22 mdw Exp $
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 /*----- Revision history --------------------------------------------------*
33 * Revision 1.6 2000/10/08 12:11:22 mdw
34 * Use @MP_EQ@ instead of @MP_CMP@.
36 * Revision 1.5 2000/06/17 11:52:36 mdw
37 * Signal a pgen abort if the jump and base share a common factor.
39 * Revision 1.4 1999/12/22 16:01:11 mdw
40 * Same file, completely different code. Main interface for new prime-
45 /*----- Header files ------------------------------------------------------*/
60 /*----- Standard prime filter ---------------------------------------------*/
62 /* --- @pgen_filter@ --- */
64 int pgen_filter(int rq
, pgen_event
*ev
, void *p
)
66 pgen_filterctx
*f
= p
;
71 rc
= pfilt_create(&f
->f
, ev
->m
);
76 if (!((f
->step
| f
->f
.m
->v
[0]) & 1))
77 rc
= pfilt_step(&f
->f
, 1);
79 rc
= pfilt_step(&f
->f
, f
->step
);
86 while (rc
== PGEN_FAIL
)
87 rc
= pfilt_step(&f
->f
, f
->step
);
88 ev
->m
= MP_COPY(f
->f
.m
);
92 /* --- @pgen_jump@ --- *
94 * Similar to the standard @pgen_filter@, but jumps in large steps rather
98 int pgen_jump(int rq
, pgen_event
*ev
, void *p
)
106 mp_gcd(&g
, 0, 0, ev
->m
, f
->j
->m
);
107 if (MP_CMP(g
, >, MP_ONE
)) {
112 rc
= pfilt_create(&f
->f
, ev
->m
);
117 rc
= pfilt_jump(&f
->f
, f
->j
);
120 pfilt_destroy(&f
->f
);
124 while (rc
== PGEN_FAIL
)
125 rc
= pfilt_jump(&f
->f
, f
->j
);
126 ev
->m
= MP_COPY(f
->f
.m
);
130 /*----- Standard prime test -----------------------------------------------*/
132 /* --- @pgen_test@ --- */
134 int pgen_test(int rq
, pgen_event
*ev
, void *p
)
141 rabin_create(r
, ev
->m
);
145 mp
*a
= mprand_range(MP_NEW
, ev
->m
, ev
->r
, 0);
146 rc
= rabin_test(r
, a
);
158 /*----- The main driver ---------------------------------------------------*/
162 * Arguments: @const char *name@ = name of the value being searched for
163 * @mp *d@ = destination for the result integer
164 * @mp *m@ = start value to pass to stepper
165 * @pgen_proc *event@ = event handler function
166 * @void *ectx@ = context argument for event andler
167 * @unsigned steps@ = number of steps to take in search
168 * @pgen_proc *step@ = stepper function to use
169 * @void *sctx@ = context argument for stepper
170 * @unsigned tests@ = number of tests to make
171 * @pgen_proc *test@ = tester function to use
172 * @void *tctx@ = context argument for tester
174 * Returns: Pointer to final result, or null.
176 * Use: A generalized prime-number search skeleton. Yes, that's a
177 * scary number of arguments.
180 mp
*pgen(const char *name
, mp
*d
, mp
*m
, pgen_proc
*event
, void *ectx
,
181 unsigned steps
, pgen_proc
*step
, void *sctx
,
182 unsigned tests
, pgen_proc
*test
, void *tctx
)
189 /* --- Set up the initial event block --- */
198 ev
.r
= fibrand_create(0);
200 /* --- Tell the event handler we're under way --- */
202 if (event
&& event(PGEN_BEGIN
, &ev
, ectx
) == PGEN_ABORT
)
205 /* --- Set up for the initial call --- */
207 proc
= step
; ctx
= sctx
; rq
= PGEN_BEGIN
;
209 /* --- Enter the great maelstrom of state transitions --- */
223 /* --- Call the procedure and decide what to do next --- */
225 rc
= proc(rq
, &ev
, ctx
);
232 proc
= test
; ctx
= tctx
;
237 act
|= A_TEST
| A_EVENT
;
241 proc
= test
; ctx
= tctx
;
248 act
|= A_ENDTEST
| A_EVENT
;
249 proc
= step
; ctx
= sctx
;
254 act
|= A_EVENT
| A_DONE
| A_ENDSTEP
;
259 act
|= A_EVENT
| A_DONE
;
260 if (proc
== test
|| rq
== PGEN_TRY
)
262 if (proc
== test
&& rq
== PGEN_BEGIN
)
266 assert(((void)"Invalid response from function", 0));
270 /* --- If decrementing counters is requested, do that --- */
272 if ((act
& A_STEP
) && steps
) {
275 act
|= A_EVENT
| A_ENDSTEP
| A_DONE
;
281 if ((act
& A_TEST
) && tests
) {
284 act
|= A_ENDTEST
| A_ENDSTEP
| A_DONE
;
289 /* --- Report an event if so directed --- */
291 if ((act
& A_EVENT
) && event
&& event(rc
, &ev
, ectx
) == PGEN_ABORT
) {
293 if (!(act
& A_DONE
)) {
294 act
|= A_ENDSTEP
| A_DONE
;
300 /* --- Close down tester and stepper functions --- */
303 test(PGEN_DONE
, &ev
, tctx
);
305 step(PGEN_DONE
, &ev
, sctx
);
307 /* --- Stop the entire test if necessary --- */
313 /* --- Tidy up and return --- */
315 if (rc
== PGEN_ABORT
) {
319 ev
.r
->ops
->destroy(ev
.r
);
326 /*----- Test rig ----------------------------------------------------------*/
330 #include <mLib/testrig.h>
332 static int verify(dstr
*v
)
334 mp
*m
= *(mp
**)v
[0].buf
;
335 mp
*q
= *(mp
**)v
[1].buf
;
343 p
= pgen("p", MP_NEW
, m
, pgen_evspin
, 0, 0, pgen_filter
, &pf
,
344 rabin_iters(mp_bits(m
)), pgen_test
, &r
);
345 if (!p
|| !MP_EQ(p
, q
)) {
346 fputs("\n*** pgen failed", stderr
);
347 fputs("\nm = ", stderr
); mp_writefile(m
, stderr
, 10);
348 fputs("\np = ", stderr
); mp_writefile(p
, stderr
, 10);
349 fputs("\nq = ", stderr
); mp_writefile(q
, stderr
, 10);
358 assert(mparena_count(MPARENA_GLOBAL
) == 0);
362 static test_chunk tests
[] = {
363 { "pgen", verify
, { &type_mp
, &type_mp
, 0 } },
367 int main(int argc
, char *argv
[])
370 test_run(argc
, argv
, tests
, SRCDIR
"/tests/pgen");
375 /*----- That's all, folks -------------------------------------------------*/