3 * $Id: strongprime.c,v 1.3 2000/06/17 12:10:09 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.3 2000/06/17 12:10:09 mdw
34 * Add some argument checking. Use MP secure memory interface.
36 * Revision 1.2 2000/02/12 18:21:03 mdw
37 * Overhaul of key management (again).
39 * Revision 1.1 1999/12/22 15:51:22 mdw
40 * Find `strong' RSA primes using Gordon's algorithm.
44 /*----- Header files ------------------------------------------------------*/
46 #include <mLib/dstr.h>
57 /*----- Main code ---------------------------------------------------------*/
59 /* --- @strongprime_setup@ --- *
61 * Arguments: @const char *name@ = pointer to name root
62 * @mp *d@ = destination for search start point
63 * @pfilt *f@ = where to store filter jump context
64 * @unsigned nbits@ = number of bits wanted
65 * @grand *r@ = random number source
66 * @unsigned n@ = number of attempts to make
67 * @pgen_proc *event@ = event handler function
68 * @void *ectx@ = argument for the event handler
70 * Returns: A starting point for a `strong' prime search, or zero.
72 * Use: Sets up for a strong prime search, so that primes with
73 * particular properties can be found. It's probably important
74 * to note that the number left in the filter context @f@ is
75 * congruent to 2 (mod 4).
78 mp
*strongprime_setup(const char *name
, mp
*d
, pfilt
*f
, unsigned nbits
,
79 grand
*r
, unsigned n
, pgen_proc
*event
, void *ectx
)
89 /* --- The bitslop parameter --- *
91 * There's quite a lot of prime searching to be done. The constant
92 * @BITSLOP@ is a (low) approximation to the base-2 log of the expected
93 * number of steps to find a prime number. Experimentation shows that
94 * numbers around 10 seem to be good.
99 /* --- Choose two primes %$s$% and %$t$% of half the required size --- */
101 assert(((void)"nbits too small in strongprime_setup", nbits
/2 > BITSLOP
));
102 nbits
= nbits
/2 - BITSLOP
;
105 rr
= mprand(rr
, nbits
, r
, 1);
106 DRESET(&dn
); dstr_putf(&dn
, "%s [s]", name
);
107 if ((s
= pgen(dn
.buf
, MP_NEWSEC
, rr
, event
, ectx
, n
, pgen_filter
, &c
,
108 rabin_iters(nbits
), pgen_test
, &rb
)) == 0)
111 rr
= mprand(rr
, nbits
, r
, 1);
112 DRESET(&dn
); dstr_putf(&dn
, "%s [t]", name
);
113 if ((t
= pgen(dn
.buf
, MP_NEWSEC
, rr
, event
, ectx
, n
, pgen_filter
, &c
,
114 rabin_iters(nbits
), pgen_test
, &rb
)) == 0)
117 /* --- Choose a suitable value for %$r = 2it + 1$% for some %$i$% --- */
119 rr
= mp_lsl(rr
, t
, 1);
120 pfilt_create(&c
.f
, rr
);
121 rr
= mp_lsl(rr
, rr
, BITSLOP
- 1);
122 rr
= mp_add(rr
, rr
, MP_ONE
);
123 DRESET(&dn
); dstr_putf(&dn
, "%s [r]", name
);
126 q
= pgen(dn
.buf
, MP_NEW
, rr
, event
, ectx
, n
, pgen_jump
, &j
,
127 rabin_iters(nbits
), pgen_test
, &rb
);
132 /* --- Select a suitable starting-point for finding %$p$% --- *
134 * This computes %$p_0 = 2(s^{r - 2} \bmod r)s - 1$%.
140 mpmont_create(&mm
, q
);
141 rr
= mp_sub(rr
, q
, MP_TWO
);
142 rr
= mpmont_exp(&mm
, rr
, s
, rr
);
144 rr
= mp_mul(rr
, rr
, s
);
145 rr
= mp_lsl(rr
, rr
, 1);
146 rr
= mp_sub(rr
, rr
, MP_ONE
);
149 /* --- Now find %$p = p_0 + 2jrs$% for some %$j$% --- */
153 x
= mp_mul(MP_NEW
, q
, s
);
156 x
= mp_lsl(x
, x
, BITSLOP
- 1);
157 rr
= mp_add(rr
, rr
, x
);
161 /* --- Return the result --- */
164 fputs("r = ", stdout
); mp_writefile(q
, stdout
, 10); putchar('\n');
165 fputs("s = ", stdout
); mp_writefile(s
, stdout
, 10); putchar('\n');
166 fputs("t = ", stdout
); mp_writefile(t
, stdout
, 10); putchar('\n');
175 /* --- Tidy up if something failed --- */
189 /* --- @strongprime@ --- *
191 * Arguments: @const char *name@ = pointer to name root
192 * @mp *d@ = destination integer
193 * @unsigned nbits@ = number of bits wanted
194 * @grand *r@ = random number source
195 * @unsigned n@ = number of attempts to make
196 * @pgen_proc *event@ = event handler function
197 * @void *ectx@ = argument for the event handler
199 * Returns: A `strong' prime, or zero.
201 * Use: Finds `strong' primes. A strong prime %$p$% is such that
203 * * %$p - 1$% has a large prime factor %$r$%,
204 * * %$p + 1$% has a large prime factor %$s$%, and
205 * * %$r - 1$% has a large prime factor %$t$%.
207 * The numbers produced may be slightly larger than requested,
211 mp
*strongprime(const char *name
, mp
*d
, unsigned nbits
, grand
*r
,
212 unsigned n
, pgen_proc
*event
, void *ectx
)
218 d
= strongprime_setup(name
, d
, &f
, nbits
, r
, n
, event
, ectx
);
220 d
= pgen(name
, d
, d
, event
, ectx
, n
, pgen_jump
, &j
,
221 rabin_iters(nbits
), pgen_test
, &rb
);
226 /*----- That's all, folks -------------------------------------------------*/