Pollard's rho algorithm for computing discrete logs.
[u/mdw/catacomb] / strongprime.c
CommitLineData
a30942cc 1/* -*-c-*-
2 *
8a1d1981 3 * $Id: strongprime.c,v 1.4 2000/07/01 11:24:52 mdw Exp $
a30942cc 4 *
5 * Generate `strong' prime numbers
6 *
7 * (c) 1999 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: strongprime.c,v $
8a1d1981 33 * Revision 1.4 2000/07/01 11:24:52 mdw
34 * Remove old debugging code.
35 *
47566c4d 36 * Revision 1.3 2000/06/17 12:10:09 mdw
37 * Add some argument checking. Use MP secure memory interface.
38 *
052b36d0 39 * Revision 1.2 2000/02/12 18:21:03 mdw
40 * Overhaul of key management (again).
41 *
a30942cc 42 * Revision 1.1 1999/12/22 15:51:22 mdw
43 * Find `strong' RSA primes using Gordon's algorithm.
44 *
45 */
46
47/*----- Header files ------------------------------------------------------*/
48
49#include <mLib/dstr.h>
50
51#include "grand.h"
52#include "rand.h"
53#include "mp.h"
54#include "mpmont.h"
55#include "mprand.h"
56#include "pgen.h"
57#include "pfilt.h"
58#include "rabin.h"
59
60/*----- Main code ---------------------------------------------------------*/
61
052b36d0 62/* --- @strongprime_setup@ --- *
a30942cc 63 *
64 * Arguments: @const char *name@ = pointer to name root
052b36d0 65 * @mp *d@ = destination for search start point
66 * @pfilt *f@ = where to store filter jump context
a30942cc 67 * @unsigned nbits@ = number of bits wanted
68 * @grand *r@ = random number source
69 * @unsigned n@ = number of attempts to make
70 * @pgen_proc *event@ = event handler function
71 * @void *ectx@ = argument for the event handler
72 *
052b36d0 73 * Returns: A starting point for a `strong' prime search, or zero.
a30942cc 74 *
052b36d0 75 * Use: Sets up for a strong prime search, so that primes with
76 * particular properties can be found. It's probably important
77 * to note that the number left in the filter context @f@ is
78 * congruent to 2 (mod 4).
a30942cc 79 */
80
052b36d0 81mp *strongprime_setup(const char *name, mp *d, pfilt *f, unsigned nbits,
82 grand *r, unsigned n, pgen_proc *event, void *ectx)
a30942cc 83{
052b36d0 84 mp *s, *t, *q;
a30942cc 85 dstr dn = DSTR_INIT;
86
052b36d0 87 mp *rr = d;
a30942cc 88 pgen_filterctx c;
052b36d0 89 pgen_jumpctx j;
a30942cc 90 rabin rb;
91
92 /* --- The bitslop parameter --- *
93 *
94 * There's quite a lot of prime searching to be done. The constant
95 * @BITSLOP@ is a (low) approximation to the base-2 log of the expected
96 * number of steps to find a prime number. Experimentation shows that
97 * numbers around 10 seem to be good.
98 */
99
052b36d0 100#define BITSLOP 12
a30942cc 101
102 /* --- Choose two primes %$s$% and %$t$% of half the required size --- */
103
47566c4d 104 assert(((void)"nbits too small in strongprime_setup", nbits/2 > BITSLOP));
a30942cc 105 nbits = nbits/2 - BITSLOP;
106 c.step = 1;
107
108 rr = mprand(rr, nbits, r, 1);
109 DRESET(&dn); dstr_putf(&dn, "%s [s]", name);
47566c4d 110 if ((s = pgen(dn.buf, MP_NEWSEC, rr, event, ectx, n, pgen_filter, &c,
052b36d0 111 rabin_iters(nbits), pgen_test, &rb)) == 0)
a30942cc 112 goto fail_s;
a30942cc 113
114 rr = mprand(rr, nbits, r, 1);
115 DRESET(&dn); dstr_putf(&dn, "%s [t]", name);
47566c4d 116 if ((t = pgen(dn.buf, MP_NEWSEC, rr, event, ectx, n, pgen_filter, &c,
a30942cc 117 rabin_iters(nbits), pgen_test, &rb)) == 0)
118 goto fail_t;
a30942cc 119
120 /* --- Choose a suitable value for %$r = 2it + 1$% for some %$i$% --- */
121
122 rr = mp_lsl(rr, t, 1);
123 pfilt_create(&c.f, rr);
124 rr = mp_lsl(rr, rr, BITSLOP - 1);
125 rr = mp_add(rr, rr, MP_ONE);
126 DRESET(&dn); dstr_putf(&dn, "%s [r]", name);
052b36d0 127 j.j = &c.f;
a30942cc 128 nbits += BITSLOP;
052b36d0 129 q = pgen(dn.buf, MP_NEW, rr, event, ectx, n, pgen_jump, &j,
130 rabin_iters(nbits), pgen_test, &rb);
a30942cc 131 pfilt_destroy(&c.f);
052b36d0 132 if (!q)
133 goto fail_r;
a30942cc 134
135 /* --- Select a suitable starting-point for finding %$p$% --- *
136 *
137 * This computes %$p_0 = 2(s^{r - 2} \bmod r)s - 1$%.
138 */
139
140 {
141 mpmont mm;
142
143 mpmont_create(&mm, q);
144 rr = mp_sub(rr, q, MP_TWO);
145 rr = mpmont_exp(&mm, rr, s, rr);
146 mpmont_destroy(&mm);
147 rr = mp_mul(rr, rr, s);
148 rr = mp_lsl(rr, rr, 1);
149 rr = mp_sub(rr, rr, MP_ONE);
150 }
151
152 /* --- Now find %$p = p_0 + 2jrs$% for some %$j$% --- */
153
154 {
155 mp *x;
156 x = mp_mul(MP_NEW, q, s);
157 x = mp_lsl(x, x, 1);
052b36d0 158 pfilt_create(f, x);
a30942cc 159 x = mp_lsl(x, x, BITSLOP - 1);
160 rr = mp_add(rr, rr, x);
161 mp_drop(x);
162 }
163
052b36d0 164 /* --- Return the result --- */
a30942cc 165
a30942cc 166 mp_drop(q);
052b36d0 167 mp_drop(t);
168 mp_drop(s);
169 dstr_destroy(&dn);
170 return (rr);
171
172 /* --- Tidy up if something failed --- */
173
a30942cc 174fail_r:
a30942cc 175 mp_drop(t);
176fail_t:
177 mp_drop(s);
178fail_s:
179 mp_drop(rr);
180 dstr_destroy(&dn);
052b36d0 181 return (0);
a30942cc 182
183#undef BITSLOP
184}
185
052b36d0 186/* --- @strongprime@ --- *
187 *
188 * Arguments: @const char *name@ = pointer to name root
189 * @mp *d@ = destination integer
190 * @unsigned nbits@ = number of bits wanted
191 * @grand *r@ = random number source
192 * @unsigned n@ = number of attempts to make
193 * @pgen_proc *event@ = event handler function
194 * @void *ectx@ = argument for the event handler
195 *
196 * Returns: A `strong' prime, or zero.
197 *
198 * Use: Finds `strong' primes. A strong prime %$p$% is such that
199 *
200 * * %$p - 1$% has a large prime factor %$r$%,
201 * * %$p + 1$% has a large prime factor %$s$%, and
202 * * %$r - 1$% has a large prime factor %$t$%.
203 *
204 * The numbers produced may be slightly larger than requested,
205 * by a few bits.
206 */
207
208mp *strongprime(const char *name, mp *d, unsigned nbits, grand *r,
209 unsigned n, pgen_proc *event, void *ectx)
210{
211 pfilt f;
212 pgen_jumpctx j;
213 rabin rb;
214
215 d = strongprime_setup(name, d, &f, nbits, r, n, event, ectx);
216 j.j = &f;
217 d = pgen(name, d, d, event, ectx, n, pgen_jump, &j,
218 rabin_iters(nbits), pgen_test, &rb);
219 pfilt_destroy(&f);
220 return (d);
221}
222
a30942cc 223/*----- That's all, folks -------------------------------------------------*/