3 * $Id: pgen-gcd.c,v 1.3 2004/04/08 01:36:15 mdw Exp $
5 * Prime search stepper ensuring a low GCD for %$(p - 1)/2$%
7 * (c) 2000 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 ------------------------------------------------------*/
35 /*----- Main code ---------------------------------------------------------*/
37 int pgen_gcdstep(int rq
, pgen_event
*ev
, void *p
)
39 pgen_gcdstepctx
*g
= p
;
44 /* --- Set everything up --- *
46 * Call things off if @p@ and @jp@ have common factors, or if @q@, @r@
47 * and @jq@ have common factors greater than @max@.
52 mp_gcd(&g
->g
, 0, 0, p
, g
->jp
.m
);
53 if (MP_CMP(g
->g
, >, MP_ONE
))
55 g
->q
= mp_lsr(MP_NEW
, p
, 1);
56 g
->jq
= mp_lsr(MP_NEW
, g
->jp
.m
, 1);
57 mp_gcd(&g
->g
, 0, 0, g
->q
, g
->jq
);
58 mp_gcd(&g
->g
, 0, 0, g
->g
, g
->r
);
59 if (MP_CMP(g
->g
, >, g
->max
)) {
64 rc
= pfilt_create(&g
->p
, p
);
68 /* --- Grind through another iteration --- */
72 rc
= pfilt_jump(&g
->p
, &g
->jp
);
73 g
->q
= mp_add(g
->q
, g
->q
, g
->jq
);
76 /* --- Finished --- */
85 /* --- Step on until everything is OK --- */
88 if (rc
!= PGEN_FAIL
) {
89 mp_gcd(&g
->g
, 0, 0, g
->r
, g
->q
);
90 if (MP_CMP(g
->g
, >, g
->max
))
95 rc
= pfilt_jump(&g
->p
, &g
->jp
);
96 g
->q
= mp_add(g
->q
, g
->q
, g
->jq
);
99 ev
->m
= MP_COPY(g
->p
.m
);
103 /*----- That's all, folks -------------------------------------------------*/