423c89a3 |
1 | /* -*-c-*- |
2 | * |
b817bfc6 |
3 | * $Id: pgen-gcd.c,v 1.3 2004/04/08 01:36:15 mdw Exp $ |
423c89a3 |
4 | * |
5 | * Prime search stepper ensuring a low GCD for %$(p - 1)/2$% |
6 | * |
7 | * (c) 2000 Straylight/Edgeware |
8 | */ |
9 | |
45c0fd36 |
10 | /*----- Licensing notice --------------------------------------------------* |
423c89a3 |
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. |
45c0fd36 |
18 | * |
423c89a3 |
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. |
45c0fd36 |
23 | * |
423c89a3 |
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 | |
423c89a3 |
30 | /*----- Header files ------------------------------------------------------*/ |
31 | |
32 | #include "mp.h" |
33 | #include "pgen.h" |
34 | |
35 | /*----- Main code ---------------------------------------------------------*/ |
36 | |
37 | int pgen_gcdstep(int rq, pgen_event *ev, void *p) |
38 | { |
39 | pgen_gcdstepctx *g = p; |
40 | int rc = PGEN_ABORT; |
41 | |
42 | switch (rq) { |
43 | |
44 | /* --- Set everything up --- * |
45 | * |
8615bf7e |
46 | * Call things off if @p@ and @jp@ have common factors, or if @q@, @r@ |
47 | * and @jq@ have common factors greater than @max@. |
423c89a3 |
48 | */ |
49 | |
50 | case PGEN_BEGIN: { |
51 | mp *p = ev->m; |
8615bf7e |
52 | mp_gcd(&g->g, 0, 0, p, g->jp.m); |
53 | if (MP_CMP(g->g, >, MP_ONE)) |
54 | return (PGEN_ABORT); |
423c89a3 |
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); |
8615bf7e |
58 | mp_gcd(&g->g, 0, 0, g->g, g->r); |
59 | if (MP_CMP(g->g, >, g->max)) { |
60 | mp_drop(g->q); |
61 | mp_drop(g->jq); |
423c89a3 |
62 | return (PGEN_ABORT); |
8615bf7e |
63 | } |
423c89a3 |
64 | rc = pfilt_create(&g->p, p); |
65 | mp_drop(p); |
66 | } break; |
67 | |
68 | /* --- Grind through another iteration --- */ |
69 | |
70 | case PGEN_TRY: |
71 | mp_drop(ev->m); |
72 | rc = pfilt_jump(&g->p, &g->jp); |
73 | g->q = mp_add(g->q, g->q, g->jq); |
74 | break; |
75 | |
76 | /* --- Finished --- */ |
77 | |
78 | case PGEN_DONE: |
79 | pfilt_destroy(&g->p); |
80 | mp_drop(g->q); |
81 | mp_drop(g->jq); |
82 | return (PGEN_DONE); |
83 | } |
84 | |
85 | /* --- Step on until everything is OK --- */ |
86 | |
87 | for (;;) { |
88 | if (rc != PGEN_FAIL) { |
89 | mp_gcd(&g->g, 0, 0, g->r, g->q); |
90 | if (MP_CMP(g->g, >, g->max)) |
91 | rc = PGEN_FAIL; |
92 | } |
93 | if (rc != PGEN_FAIL) |
94 | break; |
95 | rc = pfilt_jump(&g->p, &g->jp); |
96 | g->q = mp_add(g->q, g->q, g->jq); |
97 | } |
98 | |
99 | ev->m = MP_COPY(g->p.m); |
100 | return (rc); |
101 | } |
102 | |
103 | /*----- That's all, folks -------------------------------------------------*/ |