3 * $Id: strongprime.c,v 1.2 2000/02/12 18:21:03 mdw Exp $
5 * Generate `strong' prime numbers
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: strongprime.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 15:51:22 mdw
37 * Find `strong' RSA primes using Gordon's algorithm.
41 /*----- Header files ------------------------------------------------------*/
43 #include <mLib/dstr.h>
54 /*----- Main code ---------------------------------------------------------*/
56 /* --- @strongprime_setup@ --- *
58 * Arguments: @const char *name@ = pointer to name root
59 * @mp *d@ = destination for search start point
60 * @pfilt *f@ = where to store filter jump context
61 * @unsigned nbits@ = number of bits wanted
62 * @grand *r@ = random number source
63 * @unsigned n@ = number of attempts to make
64 * @pgen_proc *event@ = event handler function
65 * @void *ectx@ = argument for the event handler
67 * Returns: A starting point for a `strong' prime search, or zero.
69 * Use: Sets up for a strong prime search, so that primes with
70 * particular properties can be found. It's probably important
71 * to note that the number left in the filter context @f@ is
72 * congruent to 2 (mod 4).
75 mp
*strongprime_setup(const char *name
, mp
*d
, pfilt
*f
, unsigned nbits
,
76 grand
*r
, unsigned n
, pgen_proc
*event
, void *ectx
)
86 /* --- The bitslop parameter --- *
88 * There's quite a lot of prime searching to be done. The constant
89 * @BITSLOP@ is a (low) approximation to the base-2 log of the expected
90 * number of steps to find a prime number. Experimentation shows that
91 * numbers around 10 seem to be good.
96 /* --- Choose two primes %$s$% and %$t$% of half the required size --- */
98 nbits
= nbits
/2 - BITSLOP
;
101 rr
= mprand(rr
, nbits
, r
, 1);
102 DRESET(&dn
); dstr_putf(&dn
, "%s [s]", name
);
103 if ((s
= pgen(dn
.buf
, MP_NEW
, rr
, event
, ectx
, n
, pgen_filter
, &c
,
104 rabin_iters(nbits
), pgen_test
, &rb
)) == 0)
108 rr
= mprand(rr
, nbits
, r
, 1);
109 DRESET(&dn
); dstr_putf(&dn
, "%s [t]", name
);
110 if ((t
= pgen(dn
.buf
, MP_NEW
, rr
, event
, ectx
, n
, pgen_filter
, &c
,
111 rabin_iters(nbits
), pgen_test
, &rb
)) == 0)
115 /* --- Choose a suitable value for %$r = 2it + 1$% for some %$i$% --- */
117 rr
= mp_lsl(rr
, t
, 1);
118 pfilt_create(&c
.f
, rr
);
119 rr
= mp_lsl(rr
, rr
, BITSLOP
- 1);
120 rr
= mp_add(rr
, rr
, MP_ONE
);
121 DRESET(&dn
); dstr_putf(&dn
, "%s [r]", name
);
124 q
= pgen(dn
.buf
, MP_NEW
, rr
, event
, ectx
, n
, pgen_jump
, &j
,
125 rabin_iters(nbits
), pgen_test
, &rb
);
130 /* --- Select a suitable starting-point for finding %$p$% --- *
132 * This computes %$p_0 = 2(s^{r - 2} \bmod r)s - 1$%.
138 mpmont_create(&mm
, q
);
139 rr
= mp_sub(rr
, q
, MP_TWO
);
140 rr
= mpmont_exp(&mm
, rr
, s
, rr
);
142 rr
= mp_mul(rr
, rr
, s
);
143 rr
= mp_lsl(rr
, rr
, 1);
144 rr
= mp_sub(rr
, rr
, MP_ONE
);
147 /* --- Now find %$p = p_0 + 2jrs$% for some %$j$% --- */
151 x
= mp_mul(MP_NEW
, q
, s
);
154 x
= mp_lsl(x
, x
, BITSLOP
- 1);
155 rr
= mp_add(rr
, rr
, x
);
159 /* --- Return the result --- */
162 fputs("r = ", stdout
); mp_writefile(q
, stdout
, 10); putchar('\n');
163 fputs("s = ", stdout
); mp_writefile(s
, stdout
, 10); putchar('\n');
164 fputs("t = ", stdout
); mp_writefile(t
, stdout
, 10); putchar('\n');
173 /* --- Tidy up if something failed --- */
187 /* --- @strongprime@ --- *
189 * Arguments: @const char *name@ = pointer to name root
190 * @mp *d@ = destination integer
191 * @unsigned nbits@ = number of bits wanted
192 * @grand *r@ = random number source
193 * @unsigned n@ = number of attempts to make
194 * @pgen_proc *event@ = event handler function
195 * @void *ectx@ = argument for the event handler
197 * Returns: A `strong' prime, or zero.
199 * Use: Finds `strong' primes. A strong prime %$p$% is such that
201 * * %$p - 1$% has a large prime factor %$r$%,
202 * * %$p + 1$% has a large prime factor %$s$%, and
203 * * %$r - 1$% has a large prime factor %$t$%.
205 * The numbers produced may be slightly larger than requested,
209 mp
*strongprime(const char *name
, mp
*d
, unsigned nbits
, grand
*r
,
210 unsigned n
, pgen_proc
*event
, void *ectx
)
216 d
= strongprime_setup(name
, d
, &f
, nbits
, r
, n
, event
, ectx
);
218 d
= pgen(name
, d
, d
, event
, ectx
, n
, pgen_jump
, &j
,
219 rabin_iters(nbits
), pgen_test
, &rb
);
224 /*----- That's all, folks -------------------------------------------------*/