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
);
61 if (!((f
->step
| f
->f
.m
->v
[0]) & 1))
62 rc
= pfilt_step(&f
->f
, 1);
64 rc
= pfilt_step(&f
->f
, f
->step
);
71 while (rc
== PGEN_FAIL
)
72 rc
= pfilt_step(&f
->f
, f
->step
);
73 ev
->m
= MP_COPY(f
->f
.m
);
77 /* --- @pgen_jump@ --- *
79 * Similar to the standard @pgen_filter@, but jumps in large steps rather
83 int pgen_jump(int rq
, pgen_event
*ev
, void *p
)
91 mp_gcd(&g
, 0, 0, ev
->m
, f
->j
->m
);
92 if (MP_CMP(g
, >, MP_ONE
)) {
97 rc
= pfilt_create(&f
->f
, ev
->m
);
102 rc
= pfilt_jump(&f
->f
, f
->j
);
105 pfilt_destroy(&f
->f
);
109 while (rc
== PGEN_FAIL
)
110 rc
= pfilt_jump(&f
->f
, f
->j
);
111 ev
->m
= MP_COPY(f
->f
.m
);
115 /*----- Standard prime test -----------------------------------------------*/
117 /* --- @pgen_test@ --- */
119 int pgen_test(int rq
, pgen_event
*ev
, void *p
)
126 rabin_create(r
, ev
->m
);
131 rc
= rabin_rtest(r
, MP_TWO
);
133 mp
*a
= mprand_range(MP_NEW
, ev
->m
, ev
->r
, 0);
134 rc
= rabin_rtest(r
, a
);
147 /*----- The main driver ---------------------------------------------------*/
151 * Arguments: @const char *name@ = name of the value being searched for
152 * @mp *d@ = destination for the result integer
153 * @mp *m@ = start value to pass to stepper
154 * @pgen_proc *event@ = event handler function
155 * @void *ectx@ = context argument for event andler
156 * @unsigned steps@ = number of steps to take in search
157 * @pgen_proc *step@ = stepper function to use
158 * @void *sctx@ = context argument for stepper
159 * @unsigned tests@ = number of tests to make
160 * @pgen_proc *test@ = tester function to use
161 * @void *tctx@ = context argument for tester
163 * Returns: Pointer to final result, or null.
165 * Use: A generalized prime-number search skeleton. Yes, that's a
166 * scary number of arguments.
169 mp
*pgen(const char *name
, mp
*d
, mp
*m
, pgen_proc
*event
, void *ectx
,
170 unsigned steps
, pgen_proc
*step
, void *sctx
,
171 unsigned tests
, pgen_proc
*test
, void *tctx
)
179 enum { P_STEP
, P_TEST
};
181 /* --- Set up the initial event block --- */
190 ev
.r
= fibrand_create(0);
192 /* --- Tell the event handler we're under way --- */
194 if (event
&& event(PGEN_BEGIN
, &ev
, ectx
) == PGEN_ABORT
) {
195 ev
.r
->ops
->destroy(ev
.r
);
199 /* --- Set up for the initial call --- */
201 proc
= step
; ctx
= sctx
; p
= P_STEP
; rq
= PGEN_BEGIN
;
203 /* --- Enter the great maelstrom of state transitions --- */
212 #define A_ENDSTEP 16u
215 /* --- Call the procedure and decide what to do next --- */
217 rc
= proc(rq
, &ev
, ctx
);
224 proc
= test
; ctx
= tctx
; p
= P_TEST
;
229 act
|= A_TEST
| A_EVENT
;
233 proc
= test
; ctx
= tctx
; p
= P_TEST
;
240 act
|= A_ENDTEST
| A_EVENT
;
241 proc
= step
; ctx
= sctx
; p
= P_STEP
;
246 act
|= A_EVENT
| A_DONE
| A_ENDSTEP
;
251 act
|= A_EVENT
| A_DONE
;
252 if (p
== P_TEST
|| rq
== PGEN_TRY
)
254 if (p
== P_TEST
&& rq
!= PGEN_BEGIN
)
258 assert(((void)"Invalid response from function", 0));
262 /* --- If decrementing counters is requested, do that --- */
264 if ((act
& A_STEP
) && steps
) {
266 if (ev
.steps
== steps
) {
267 act
|= A_EVENT
| A_ENDSTEP
| A_DONE
;
273 if ((act
& A_TEST
) && tests
) {
275 if (ev
.tests
== tests
) {
276 act
|= A_ENDTEST
| A_ENDSTEP
| A_DONE
;
281 /* --- Report an event if so directed --- */
283 if ((act
& A_EVENT
) && event
&& event(rc
, &ev
, ectx
) == PGEN_ABORT
) {
285 if (!(act
& A_DONE
)) {
286 act
|= A_ENDSTEP
| A_DONE
;
292 /* --- Close down tester and stepper functions --- */
295 test(PGEN_DONE
, &ev
, tctx
);
297 step(PGEN_DONE
, &ev
, sctx
);
299 /* --- Stop the entire test if necessary --- */
305 /* --- Tidy up and return --- */
307 if (rc
== PGEN_ABORT
) {
311 ev
.r
->ops
->destroy(ev
.r
);
317 /* --- @pgen_primep@ --- *
319 * Arguments: @mp *p@ = a number to check
320 * @grand *gr@ = a random number source
322 * Returns: Nonzero if @p@ is really prime.
325 int pgen_primep(mp
*p
, grand
*gr
)
331 if (MP_NEGP(p
)) return (0);
332 switch (pfilt_smallfactor(p
)) {
333 case PGEN_DONE
: return (1);
334 case PGEN_FAIL
: return (0);
337 for (i
= 32; i
; i
--) {
338 x
= mprand_range(x
, p
, gr
, 0);
339 if (rabin_rtest(&r
, x
) == PGEN_FAIL
)
347 /*----- Test rig ----------------------------------------------------------*/
351 #include <mLib/testrig.h>
353 static int verify(dstr
*v
)
355 mp
*m
= *(mp
**)v
[0].buf
;
356 mp
*q
= *(mp
**)v
[1].buf
;
364 p
= pgen("p", MP_NEW
, m
, pgen_evspin
, 0, 0, pgen_filter
, &pf
,
365 rabin_iters(mp_bits(m
)), pgen_test
, &r
);
366 if (!p
|| !MP_EQ(p
, q
)) {
367 fputs("\n*** pgen failed", stderr
);
368 fputs("\nm = ", stderr
); mp_writefile(m
, stderr
, 10);
369 fputs("\np = ", stderr
); mp_writefile(p
, stderr
, 10);
370 fputs("\nq = ", stderr
); mp_writefile(q
, stderr
, 10);
378 assert(mparena_count(MPARENA_GLOBAL
) == 0);
382 static test_chunk tests
[] = {
383 { "pgen", verify
, { &type_mp
, &type_mp
, 0 } },
387 int main(int argc
, char *argv
[])
390 test_run(argc
, argv
, tests
, SRCDIR
"/tests/pgen");
395 /*----- That's all, folks -------------------------------------------------*/