3 * Generate `strong' prime numbers
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
30 #include <mLib/dstr.h>
40 /*----- Main code ---------------------------------------------------------*/
42 /* --- @strongprime_setup@ --- *
44 * Arguments: @const char *name@ = pointer to name root
45 * @mp *d@ = destination for search start point
46 * @pfilt *f@ = where to store filter jump context
47 * @unsigned nbits@ = number of bits wanted
48 * @grand *r@ = random number source
49 * @unsigned n@ = number of attempts to make
50 * @pgen_proc *event@ = event handler function
51 * @void *ectx@ = argument for the event handler
53 * Returns: A starting point for a `strong' prime search, or zero.
55 * Use: Sets up for a strong prime search, so that primes with
56 * particular properties can be found. It's probably important
57 * to note that the number left in the filter context @f@ is
58 * congruent to 2 (mod 4).
61 mp
*strongprime_setup(const char *name
, mp
*d
, pfilt
*f
, unsigned nbits
,
62 grand
*r
, unsigned n
, pgen_proc
*event
, void *ectx
)
72 /* --- The bitslop parameter --- *
74 * There's quite a lot of prime searching to be done. The constant
75 * @BITSLOP@ is a (low) approximation to the base-2 log of the expected
76 * number of steps to find a prime number. Experimentation shows that
77 * numbers around 10 seem to be good.
82 /* --- Choose two primes %$s$% and %$t$% of half the required size --- */
84 assert(((void)"nbits too small in strongprime_setup", nbits
/2 > BITSLOP
));
85 nbits
= nbits
/2 - BITSLOP
;
88 rr
= mprand(rr
, nbits
, r
, 1);
89 DRESET(&dn
); dstr_putf(&dn
, "%s [s]", name
);
90 if ((s
= pgen(dn
.buf
, MP_NEWSEC
, rr
, event
, ectx
, n
, pgen_filter
, &c
,
91 rabin_iters(nbits
), pgen_test
, &rb
)) == 0)
94 rr
= mprand(rr
, nbits
, r
, 1);
95 DRESET(&dn
); dstr_putf(&dn
, "%s [t]", name
);
96 if ((t
= pgen(dn
.buf
, MP_NEWSEC
, rr
, event
, ectx
, n
, pgen_filter
, &c
,
97 rabin_iters(nbits
), pgen_test
, &rb
)) == 0)
100 /* --- Choose a suitable value for %$r = 2it + 1$% for some %$i$% --- */
102 rr
= mp_lsl(rr
, t
, 1);
103 pfilt_create(&c
.f
, rr
);
104 rr
= mp_lsl(rr
, rr
, BITSLOP
- 1);
105 rr
= mp_add(rr
, rr
, MP_ONE
);
106 DRESET(&dn
); dstr_putf(&dn
, "%s [r]", name
);
109 q
= pgen(dn
.buf
, MP_NEW
, rr
, event
, ectx
, n
, pgen_jump
, &j
,
110 rabin_iters(nbits
), pgen_test
, &rb
);
115 /* --- Select a suitable starting-point for finding %$p$% --- *
117 * This computes %$p_0 = 2(s^{r - 2} \bmod r)s - 1$%.
123 mpmont_create(&mm
, q
);
124 rr
= mp_sub(rr
, q
, MP_TWO
);
125 rr
= mpmont_exp(&mm
, rr
, s
, rr
);
127 rr
= mp_mul(rr
, rr
, s
);
128 rr
= mp_lsl(rr
, rr
, 1);
129 rr
= mp_sub(rr
, rr
, MP_ONE
);
132 /* --- Now find %$p = p_0 + 2jrs$% for some %$j$% --- */
136 x
= mp_mul(MP_NEW
, q
, s
);
139 x
= mp_lsl(x
, x
, BITSLOP
- 1);
140 rr
= mp_add(rr
, rr
, x
);
144 /* --- Return the result --- */
152 /* --- Tidy up if something failed --- */
166 /* --- @strongprime@ --- *
168 * Arguments: @const char *name@ = pointer to name root
169 * @mp *d@ = destination integer
170 * @unsigned nbits@ = number of bits wanted
171 * @grand *r@ = random number source
172 * @unsigned n@ = number of attempts to make
173 * @pgen_proc *event@ = event handler function
174 * @void *ectx@ = argument for the event handler
176 * Returns: A `strong' prime, or zero.
178 * Use: Finds `strong' primes. A strong prime %$p$% is such that
180 * * %$p - 1$% has a large prime factor %$r$%,
181 * * %$p + 1$% has a large prime factor %$s$%, and
182 * * %$r - 1$% has a large prime factor %$t$%.
184 * The numbers produced may be slightly larger than requested,
188 mp
*strongprime(const char *name
, mp
*d
, unsigned nbits
, grand
*r
,
189 unsigned n
, pgen_proc
*event
, void *ectx
)
195 d
= strongprime_setup(name
, d
, &f
, nbits
, r
, n
, event
, ectx
);
197 d
= pgen(name
, d
, d
, event
, ectx
, n
, pgen_jump
, &j
,
198 rabin_iters(nbits
), pgen_test
, &rb
);
203 /*----- That's all, folks -------------------------------------------------*/