3 * $Id: strongprime.c,v 1.4 2000/07/01 11:24:52 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.4 2000/07/01 11:24:52 mdw
34 * Remove old debugging code.
36 * Revision 1.3 2000/06/17 12:10:09 mdw
37 * Add some argument checking. Use MP secure memory interface.
39 * Revision 1.2 2000/02/12 18:21:03 mdw
40 * Overhaul of key management (again).
42 * Revision 1.1 1999/12/22 15:51:22 mdw
43 * Find `strong' RSA primes using Gordon's algorithm.
47 /*----- Header files ------------------------------------------------------*/
49 #include <mLib/dstr.h>
60 /*----- Main code ---------------------------------------------------------*/
62 /* --- @strongprime_setup@ --- *
64 * Arguments: @const char *name@ = pointer to name root
65 * @mp *d@ = destination for search start point
66 * @pfilt *f@ = where to store filter jump context
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
73 * Returns: A starting point for a `strong' prime search, or zero.
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).
81 mp
*strongprime_setup(const char *name
, mp
*d
, pfilt
*f
, unsigned nbits
,
82 grand
*r
, unsigned n
, pgen_proc
*event
, void *ectx
)
92 /* --- The bitslop parameter --- *
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.
102 /* --- Choose two primes %$s$% and %$t$% of half the required size --- */
104 assert(((void)"nbits too small in strongprime_setup", nbits
/2 > BITSLOP
));
105 nbits
= nbits
/2 - BITSLOP
;
108 rr
= mprand(rr
, nbits
, r
, 1);
109 DRESET(&dn
); dstr_putf(&dn
, "%s [s]", name
);
110 if ((s
= pgen(dn
.buf
, MP_NEWSEC
, rr
, event
, ectx
, n
, pgen_filter
, &c
,
111 rabin_iters(nbits
), pgen_test
, &rb
)) == 0)
114 rr
= mprand(rr
, nbits
, r
, 1);
115 DRESET(&dn
); dstr_putf(&dn
, "%s [t]", name
);
116 if ((t
= pgen(dn
.buf
, MP_NEWSEC
, rr
, event
, ectx
, n
, pgen_filter
, &c
,
117 rabin_iters(nbits
), pgen_test
, &rb
)) == 0)
120 /* --- Choose a suitable value for %$r = 2it + 1$% for some %$i$% --- */
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
);
129 q
= pgen(dn
.buf
, MP_NEW
, rr
, event
, ectx
, n
, pgen_jump
, &j
,
130 rabin_iters(nbits
), pgen_test
, &rb
);
135 /* --- Select a suitable starting-point for finding %$p$% --- *
137 * This computes %$p_0 = 2(s^{r - 2} \bmod r)s - 1$%.
143 mpmont_create(&mm
, q
);
144 rr
= mp_sub(rr
, q
, MP_TWO
);
145 rr
= mpmont_exp(&mm
, rr
, s
, rr
);
147 rr
= mp_mul(rr
, rr
, s
);
148 rr
= mp_lsl(rr
, rr
, 1);
149 rr
= mp_sub(rr
, rr
, MP_ONE
);
152 /* --- Now find %$p = p_0 + 2jrs$% for some %$j$% --- */
156 x
= mp_mul(MP_NEW
, q
, s
);
159 x
= mp_lsl(x
, x
, BITSLOP
- 1);
160 rr
= mp_add(rr
, rr
, x
);
164 /* --- Return the result --- */
172 /* --- Tidy up if something failed --- */
186 /* --- @strongprime@ --- *
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
196 * Returns: A `strong' prime, or zero.
198 * Use: Finds `strong' primes. A strong prime %$p$% is such that
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$%.
204 * The numbers produced may be slightly larger than requested,
208 mp
*strongprime(const char *name
, mp
*d
, unsigned nbits
, grand
*r
,
209 unsigned n
, pgen_proc
*event
, void *ectx
)
215 d
= strongprime_setup(name
, d
, &f
, nbits
, r
, n
, event
, ectx
);
217 d
= pgen(name
, d
, d
, event
, ectx
, n
, pgen_jump
, &j
,
218 rabin_iters(nbits
), pgen_test
, &rb
);
223 /*----- That's all, folks -------------------------------------------------*/