3 * $Id: strongprime.c,v 1.1 1999/12/22 15:51:22 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.1 1999/12/22 15:51:22 mdw
34 * Find `strong' RSA primes using Gordon's algorithm.
38 /*----- Header files ------------------------------------------------------*/
40 #include <mLib/dstr.h>
51 /*----- Main code ---------------------------------------------------------*/
53 /* --- @strongprime@ --- *
55 * Arguments: @const char *name@ = pointer to name root
56 * @mp *d@ = destination integer
57 * @unsigned nbits@ = number of bits wanted
58 * @grand *r@ = random number source
59 * @unsigned n@ = number of attempts to make
60 * @pgen_proc *event@ = event handler function
61 * @void *ectx@ = argument for the event handler
63 * Returns: A `strong' prime, or zero.
65 * Use: Finds `strong' primes. A strong prime %$p$% is such that
67 * * %$p - 1$% has a large prime factor %$r$%,
68 * * %$p + 1$% has a large prime factor %$s$%, and
69 * * %$r - 1$% has a large prime factor %$t$%.
71 * The numbers produced may be slightly larger than requested,
75 mp
*strongprime(const char *name
, mp
*d
, unsigned nbits
, grand
*r
,
76 unsigned n
, pgen_proc
*event
, void *ectx
)
78 mp
*s
, *t
, *q
, *p
= 0;
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 if ((q
= pgen(dn
.buf
, MP_NEW
, rr
, event
, ectx
, n
, pgen_jump
, &cj
,
125 rabin_iters(nbits
), pgen_test
, &rb
)) == 0)
129 /* --- Select a suitable starting-point for finding %$p$% --- *
131 * This computes %$p_0 = 2(s^{r - 2} \bmod r)s - 1$%.
137 mpmont_create(&mm
, q
);
138 rr
= mp_sub(rr
, q
, MP_TWO
);
139 rr
= mpmont_exp(&mm
, rr
, s
, rr
);
141 rr
= mp_mul(rr
, rr
, s
);
142 rr
= mp_lsl(rr
, rr
, 1);
143 rr
= mp_sub(rr
, rr
, MP_ONE
);
146 /* --- Now find %$p = p_0 + 2jrs$% for some %$j$% --- */
150 x
= mp_mul(MP_NEW
, q
, s
);
152 pfilt_create(&c
.f
, x
);
153 x
= mp_lsl(x
, x
, BITSLOP
- 1);
154 rr
= mp_add(rr
, rr
, x
);
158 if ((p
= pgen(name
, d
, rr
, event
, ectx
, n
, pgen_jump
, &cj
,
159 rabin_iters(nbits
* 2), pgen_test
, &rb
)) == 0)
162 /* --- Tidy up because we've finished --- */
180 /*----- That's all, folks -------------------------------------------------*/