chiark / gitweb /
(pgen_gcd): Bug fix -- check the GCDs of the right things when deciding
[catacomb] / pgen-gcd.c
1 /* -*-c-*-
2  *
3  * $Id: pgen-gcd.c,v 1.2 2000/07/01 11:09:20 mdw Exp $
4  *
5  * Prime search stepper ensuring a low GCD for %$(p - 1)/2$%
6  *
7  * (c) 2000 Straylight/Edgeware
8  */
9
10 /*----- Licensing notice --------------------------------------------------* 
11  *
12  * This file is part of Catacomb.
13  *
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.
18  * 
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.
23  * 
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,
27  * MA 02111-1307, USA.
28  */
29
30 /*----- Revision history --------------------------------------------------* 
31  *
32  * $Log: pgen-gcd.c,v $
33  * Revision 1.2  2000/07/01 11:09:20  mdw
34  * (pgen_gcd): Bug fix -- check the GCDs of the right things when deciding
35  * whether to abort.
36  *
37  * Revision 1.1  2000/06/17 11:51:53  mdw
38  * Filter which imposes additional restrictions on GCD of %$(p - 1)/2$%
39  * with a given integer.
40  *
41  */
42
43 /*----- Header files ------------------------------------------------------*/
44
45 #include "mp.h"
46 #include "pgen.h"
47
48 /*----- Main code ---------------------------------------------------------*/
49
50 int pgen_gcdstep(int rq, pgen_event *ev, void *p)
51 {
52   pgen_gcdstepctx *g = p;
53   int rc = PGEN_ABORT;
54
55   switch (rq) {
56
57     /* --- Set everything up --- *
58      *
59      * Call things off if @p@ and @jp@ have common factors, or if @q@, @r@
60      * and @jq@ have common factors greater than @max@.
61      */
62
63     case PGEN_BEGIN: {
64       mp *p = ev->m;
65       mp_gcd(&g->g, 0, 0, p, g->jp.m);
66       if (MP_CMP(g->g, >, MP_ONE))
67         return (PGEN_ABORT);
68       g->q = mp_lsr(MP_NEW, p, 1);
69       g->jq = mp_lsr(MP_NEW, g->jp.m, 1);
70       mp_gcd(&g->g, 0, 0, g->q, g->jq);
71       mp_gcd(&g->g, 0, 0, g->g, g->r);
72       if (MP_CMP(g->g, >, g->max)) {
73         mp_drop(g->q);
74         mp_drop(g->jq);
75         return (PGEN_ABORT);
76       }
77       rc = pfilt_create(&g->p, p);
78       mp_drop(p);
79     } break;
80
81     /* --- Grind through another iteration --- */
82
83     case PGEN_TRY:
84       mp_drop(ev->m);
85       rc = pfilt_jump(&g->p, &g->jp);
86       g->q = mp_add(g->q, g->q, g->jq);
87       break;
88
89     /* --- Finished --- */
90
91     case PGEN_DONE:
92       pfilt_destroy(&g->p);
93       mp_drop(g->q);
94       mp_drop(g->jq);
95       return (PGEN_DONE);
96   }
97
98   /* --- Step on until everything is OK --- */
99
100   for (;;) {
101     if (rc != PGEN_FAIL) {
102       mp_gcd(&g->g, 0, 0, g->r, g->q);
103       if (MP_CMP(g->g, >, g->max))
104         rc = PGEN_FAIL;
105     }
106     if (rc != PGEN_FAIL)
107       break;
108     rc = pfilt_jump(&g->p, &g->jp);
109     g->q = mp_add(g->q, g->q, g->jq);
110   }
111
112   ev->m = MP_COPY(g->p.m);
113   return (rc);
114 }
115
116 /*----- That's all, folks -------------------------------------------------*/