3 * $Id: pgen.c,v 1.8 2002/01/13 13:42:53 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.8 2002/01/13 13:42:53 mdw
34 * More efficient Rabin-Miller test: with random witnesses, skip redundant
35 * Montgomerization. (Being bijective, it can't affect the distribution.)
37 * Revision 1.7 2001/02/03 16:05:32 mdw
38 * Now @mp_drop@ checks its argument is non-NULL before attempting to free
39 * it. Note that the macro version @MP_DROP@ doesn't do this.
41 * Revision 1.6 2000/10/08 12:11:22 mdw
42 * Use @MP_EQ@ instead of @MP_CMP@.
44 * Revision 1.5 2000/06/17 11:52:36 mdw
45 * Signal a pgen abort if the jump and base share a common factor.
47 * Revision 1.4 1999/12/22 16:01:11 mdw
48 * Same file, completely different code. Main interface for new prime-
53 /*----- Header files ------------------------------------------------------*/
68 /*----- Standard prime filter ---------------------------------------------*/
70 /* --- @pgen_filter@ --- */
72 int pgen_filter(int rq
, pgen_event
*ev
, void *p
)
74 pgen_filterctx
*f
= p
;
79 rc
= pfilt_create(&f
->f
, ev
->m
);
84 if (!((f
->step
| f
->f
.m
->v
[0]) & 1))
85 rc
= pfilt_step(&f
->f
, 1);
87 rc
= pfilt_step(&f
->f
, f
->step
);
94 while (rc
== PGEN_FAIL
)
95 rc
= pfilt_step(&f
->f
, f
->step
);
96 ev
->m
= MP_COPY(f
->f
.m
);
100 /* --- @pgen_jump@ --- *
102 * Similar to the standard @pgen_filter@, but jumps in large steps rather
106 int pgen_jump(int rq
, pgen_event
*ev
, void *p
)
114 mp_gcd(&g
, 0, 0, ev
->m
, f
->j
->m
);
115 if (MP_CMP(g
, >, MP_ONE
)) {
120 rc
= pfilt_create(&f
->f
, ev
->m
);
125 rc
= pfilt_jump(&f
->f
, f
->j
);
128 pfilt_destroy(&f
->f
);
132 while (rc
== PGEN_FAIL
)
133 rc
= pfilt_jump(&f
->f
, f
->j
);
134 ev
->m
= MP_COPY(f
->f
.m
);
138 /*----- Standard prime test -----------------------------------------------*/
140 /* --- @pgen_test@ --- */
142 int pgen_test(int rq
, pgen_event
*ev
, void *p
)
149 rabin_create(r
, ev
->m
);
154 rc
= rabin_rtest(r
, MP_TWO
);
156 mp
*a
= mprand_range(MP_NEW
, ev
->m
, ev
->r
, 0);
157 rc
= rabin_rtest(r
, a
);
170 /*----- The main driver ---------------------------------------------------*/
174 * Arguments: @const char *name@ = name of the value being searched for
175 * @mp *d@ = destination for the result integer
176 * @mp *m@ = start value to pass to stepper
177 * @pgen_proc *event@ = event handler function
178 * @void *ectx@ = context argument for event andler
179 * @unsigned steps@ = number of steps to take in search
180 * @pgen_proc *step@ = stepper function to use
181 * @void *sctx@ = context argument for stepper
182 * @unsigned tests@ = number of tests to make
183 * @pgen_proc *test@ = tester function to use
184 * @void *tctx@ = context argument for tester
186 * Returns: Pointer to final result, or null.
188 * Use: A generalized prime-number search skeleton. Yes, that's a
189 * scary number of arguments.
192 mp
*pgen(const char *name
, mp
*d
, mp
*m
, pgen_proc
*event
, void *ectx
,
193 unsigned steps
, pgen_proc
*step
, void *sctx
,
194 unsigned tests
, pgen_proc
*test
, void *tctx
)
201 /* --- Set up the initial event block --- */
210 ev
.r
= fibrand_create(0);
212 /* --- Tell the event handler we're under way --- */
214 if (event
&& event(PGEN_BEGIN
, &ev
, ectx
) == PGEN_ABORT
)
217 /* --- Set up for the initial call --- */
219 proc
= step
; ctx
= sctx
; rq
= PGEN_BEGIN
;
221 /* --- Enter the great maelstrom of state transitions --- */
235 /* --- Call the procedure and decide what to do next --- */
237 rc
= proc(rq
, &ev
, ctx
);
244 proc
= test
; ctx
= tctx
;
249 act
|= A_TEST
| A_EVENT
;
253 proc
= test
; ctx
= tctx
;
260 act
|= A_ENDTEST
| A_EVENT
;
261 proc
= step
; ctx
= sctx
;
266 act
|= A_EVENT
| A_DONE
| A_ENDSTEP
;
271 act
|= A_EVENT
| A_DONE
;
272 if (proc
== test
|| rq
== PGEN_TRY
)
274 if (proc
== test
&& rq
== PGEN_BEGIN
)
278 assert(((void)"Invalid response from function", 0));
282 /* --- If decrementing counters is requested, do that --- */
284 if ((act
& A_STEP
) && steps
) {
286 if (ev
.steps
== steps
) {
287 act
|= A_EVENT
| A_ENDSTEP
| A_DONE
;
293 if ((act
& A_TEST
) && tests
) {
295 if (ev
.tests
== tests
) {
296 act
|= A_ENDTEST
| A_ENDSTEP
| A_DONE
;
301 /* --- Report an event if so directed --- */
303 if ((act
& A_EVENT
) && event
&& event(rc
, &ev
, ectx
) == PGEN_ABORT
) {
305 if (!(act
& A_DONE
)) {
306 act
|= A_ENDSTEP
| A_DONE
;
312 /* --- Close down tester and stepper functions --- */
315 test(PGEN_DONE
, &ev
, tctx
);
317 step(PGEN_DONE
, &ev
, sctx
);
319 /* --- Stop the entire test if necessary --- */
325 /* --- Tidy up and return --- */
327 if (rc
== PGEN_ABORT
) {
331 ev
.r
->ops
->destroy(ev
.r
);
337 /*----- Test rig ----------------------------------------------------------*/
341 #include <mLib/testrig.h>
343 static int verify(dstr
*v
)
345 mp
*m
= *(mp
**)v
[0].buf
;
346 mp
*q
= *(mp
**)v
[1].buf
;
354 p
= pgen("p", MP_NEW
, m
, pgen_evspin
, 0, 0, pgen_filter
, &pf
,
355 rabin_iters(mp_bits(m
)), pgen_test
, &r
);
356 if (!p
|| !MP_EQ(p
, q
)) {
357 fputs("\n*** pgen failed", stderr
);
358 fputs("\nm = ", stderr
); mp_writefile(m
, stderr
, 10);
359 fputs("\np = ", stderr
); mp_writefile(p
, stderr
, 10);
360 fputs("\nq = ", stderr
); mp_writefile(q
, stderr
, 10);
368 assert(mparena_count(MPARENA_GLOBAL
) == 0);
372 static test_chunk tests
[] = {
373 { "pgen", verify
, { &type_mp
, &type_mp
, 0 } },
377 int main(int argc
, char *argv
[])
380 test_run(argc
, argv
, tests
, SRCDIR
"/tests/pgen");
385 /*----- That's all, folks -------------------------------------------------*/