3 * $Id: pgen-safe.c,v 1.2 2000/02/12 18:21:03 mdw Exp $
5 * Safe prime generation
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 --------------------------------------------------*
32 * $Log: pgen-safe.c,v $
33 * Revision 1.2 2000/02/12 18:21:03 mdw
34 * Overhaul of key management (again).
36 * Revision 1.1 1999/12/22 16:01:34 mdw
37 * Find `safe' primes (i.e., %$p = 2q + 1$%).
41 /*----- Header files ------------------------------------------------------*/
47 /*----- Main code ---------------------------------------------------------*/
49 /* --- @pgen_safestep@ --- *
51 * Steps two numbers, %$q$% and %$p = 2q + 1$%, such that neither has any
52 * small factors. %$p$% is put in the event block.
55 int pgen_safestep(int rq
, pgen_event
*ev
, void *p
)
57 pgen_safestepctx
*c
= p
;
58 int rc
= PGEN_ABORT
, qrc
= 0;
62 /* --- Set up the contexts --- */
65 mp
*p
= mp_split(MP_COPY(ev
->m
));
68 q
= mp_lsr(MP_NEW
, p
, 1);
69 rc
= pfilt_create(&c
->p
, p
);
70 qrc
= pfilt_create(&c
->q
, q
);
71 mp_drop(p
); mp_drop(q
);
74 /* --- Step along --- */
78 rc
= pfilt_step(&c
->p
, 4);
79 qrc
= pfilt_step(&c
->q
, 2);
84 /* --- Tidy the toys away --- */
92 /* --- Continue stepping if necessary --- */
94 while (rc
== PGEN_FAIL
|| qrc
== PGEN_FAIL
) {
95 rc
= pfilt_step(&c
->p
, 4);
96 qrc
= pfilt_step(&c
->q
, 2);
99 ev
->m
= MP_COPY(c
->p
.m
);
105 /* --- @pgen_safejump@ --- *
107 * Jumps two numbers, %$q$% and %$p = 2q + 1$% such that neither has any
111 int pgen_safejump(int rq
, pgen_event
*ev
, void *p
)
113 pgen_safejumpctx
*j
= p
;
114 int rc
= PGEN_ABORT
, qrc
= 0;
118 /* --- Set up the jump contexts --- *
120 * The jump in @j.q@ is congruent to 2 (mod 4); see @strongprime_setup@.
121 * If @p@ is initially 1 (mod 4) then add @j.q@. Then double @j.q@ to
122 * ensure that the step is 0 (mod 4).
128 if ((p
->v
[0] & 3) != 3)
129 p
= mp_add(p
, p
, j
->jq
.m
);
130 rc
= pfilt_create(&j
->p
, p
);
131 pfilt_muladd(&j
->jp
, &j
->jq
, 2, 0);
132 q
= mp_lsr(MP_NEW
, p
, 1);
133 qrc
= pfilt_create(&j
->q
, q
);
138 /* --- Step on one place --- */
142 rc
= pfilt_jump(&j
->p
, &j
->jp
);
143 qrc
= pfilt_jump(&j
->q
, &j
->jq
);
146 /* --- Tidy everything up --- */
149 pfilt_destroy(&j
->jp
);
150 pfilt_destroy(&j
->p
);
151 pfilt_destroy(&j
->q
);
155 /* --- Step on while @p@ or @q@ have small factors --- */
157 while (rc
== PGEN_FAIL
|| qrc
== PGEN_FAIL
) {
158 rc
= pfilt_jump(&j
->p
, &j
->jp
);
159 qrc
= pfilt_jump(&j
->q
, &j
->jq
);
161 ev
->m
= MP_COPY(j
->p
.m
);
167 /* --- @pgen_safetest@ --- *
169 * Applies Rabin-Miller tests to %$p$% and %$q$%.
172 int pgen_safetest(int rq
, pgen_event
*ev
, void *p
)
174 pgen_safetestctx
*c
= p
;
179 rabin_create(&c
->q
, c
->c
.q
.m
);
180 rabin_create(&c
->p
, c
->c
.p
.m
);
184 mp
*m
= mprand_range(MP_NEW
, c
->c
.q
.m
, ev
->r
, 0);
185 rc
= rabin_test(&c
->p
, m
);
187 rc
= rabin_test(&c
->q
, m
);
191 rabin_destroy(&c
->q
);
192 rabin_destroy(&c
->p
);
198 /*----- That's all, folks -------------------------------------------------*/