Merge branch '2.3.x'
[catacomb] / math / strongprime.c
1 /* -*-c-*-
2 *
3 * Generate `strong' prime numbers
4 *
5 * (c) 1999 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of Catacomb.
11 *
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.
16 *
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.
21 *
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,
25 * MA 02111-1307, USA.
26 */
27
28 /*----- Header files ------------------------------------------------------*/
29
30 #include <mLib/dstr.h>
31
32 #include "grand.h"
33 #include "mp.h"
34 #include "mpmont.h"
35 #include "mprand.h"
36 #include "pgen.h"
37 #include "pfilt.h"
38 #include "rabin.h"
39
40 /*----- Main code ---------------------------------------------------------*/
41
42 /* --- @strongprime_setup@ --- *
43 *
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
52 *
53 * Returns: A starting point for a `strong' prime search, or zero.
54 *
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); that the jump value is twice the
59 * product of two large primes; and that the starting point is
60 * at least %$3 \cdot 2^{N-2}$%. (Hence, if you multiply two
61 * such numbers, the product is at least
62 *
63 * %$9 \cdot 2^{2N-4} > 2^{2N-1}$%
64 *
65 * i.e., it will be (at least) a %$2 N$%-bit value.
66 */
67
68 mp *strongprime_setup(const char *name, mp *d, pfilt *f, unsigned nbits,
69 grand *r, unsigned n, pgen_proc *event, void *ectx)
70 {
71 mp *s, *t, *q;
72 dstr dn = DSTR_INIT;
73 unsigned slop, nb, u, i;
74
75 mp *rr = d;
76 pgen_filterctx c;
77 pgen_jumpctx j;
78 rabin rb;
79
80 /* --- Figure out how large the smaller primes should be --- *
81 *
82 * We want them to be `as large as possible', subject to the constraint
83 * that we produce a number of the requested size at the end. This is
84 * tricky, because the final prime search is going to involve quite large
85 * jumps from its starting point; the size of the jumps are basically
86 * determined by our choice here, and if they're too big then we won't find
87 * a prime in time.
88 *
89 * Let's suppose we're trying to make an %$N$%-bit prime. The expected
90 * number of steps tends to increase linearly with size, i.e., we need to
91 * take about %2^k N$% steps for some %$k$%. If we're jumping by a
92 * %$J$%-bit quantity each time, from an %$N$%-bit starting point, then we
93 * will only be able to find a match if %$2^k N 2^{J-1} \le 2^{N-1}$%,
94 * i.e., if %$J \le N - (k + \log_2 N)$%.
95 *
96 * Experimentation shows that taking %$k + \log_2 N = 12$% works well for
97 * %$N = 1024$%, so %$k = 2$%. Add a few extra bits for luck.
98 */
99
100 for (i = 1; i && nbits >> i; i <<= 1); assert(i);
101 for (slop = 6, nb = nbits; nb > 1; i >>= 1) {
102 u = nb >> i;
103 if (u) { slop += i; nb = u; }
104 }
105 if (nbits/2 <= slop) return (0);
106
107 /* --- Choose two primes %$s$% and %$t$% of half the required size --- */
108
109 nb = nbits/2 - slop;
110 c.step = 1;
111
112 rr = mprand(rr, nb, r, 1);
113 DRESET(&dn); dstr_putf(&dn, "%s [s]", name);
114 if ((s = pgen(dn.buf, MP_NEWSEC, rr, event, ectx, n, pgen_filter, &c,
115 rabin_iters(nb), pgen_test, &rb)) == 0)
116 goto fail_s;
117
118 rr = mprand(rr, nb, r, 1);
119 DRESET(&dn); dstr_putf(&dn, "%s [t]", name);
120 if ((t = pgen(dn.buf, MP_NEWSEC, rr, event, ectx, n, pgen_filter, &c,
121 rabin_iters(nb), pgen_test, &rb)) == 0)
122 goto fail_t;
123
124 /* --- Choose a suitable value for %$r = 2it + 1$% for some %$i$% --- *
125 *
126 * Then %$r \equiv 1 \pmod{t}$%, i.e., %$r - 1$% is a multiple of %$t$%.
127 */
128
129 rr = mp_lsl(rr, t, 1);
130 pfilt_create(&c.f, rr);
131 rr = mp_lsl(rr, rr, slop - 1);
132 rr = mp_add(rr, rr, MP_ONE);
133 DRESET(&dn); dstr_putf(&dn, "%s [r]", name);
134 j.j = &c.f;
135 q = pgen(dn.buf, MP_NEW, rr, event, ectx, n, pgen_jump, &j,
136 rabin_iters(nb + slop), pgen_test, &rb);
137 pfilt_destroy(&c.f);
138 if (!q)
139 goto fail_r;
140
141 /* --- Select a suitable congruence class for %$p$% --- *
142 *
143 * This computes %$p_0 = 2 s (s^{-1} \bmod r) - 1$%. Then %$p_0 + 1$% is
144 * clearly a multiple of %$s$%, and
145 *
146 * %$p_0 - 1 \equiv 2 s s^{-1} - 2 \equiv 0 \pmod{r}$%
147 *
148 * is a multiple of %$r$%.
149 */
150
151 rr = mp_modinv(rr, s, q);
152 rr = mp_mul(rr, rr, s);
153 rr = mp_lsl(rr, rr, 1);
154 rr = mp_sub(rr, rr, MP_ONE);
155
156 /* --- Pick a starting point for the search --- *
157 *
158 * Select %$3 \cdot 2^{N-2} < p_1 < 2^N$% at random, only with
159 * %$p_1 \equiv p_0 \pmod{2 r s}$.
160 */
161
162 {
163 mp *x, *y;
164 x = mp_mul(MP_NEW, q, s);
165 x = mp_lsl(x, x, 1);
166 pfilt_create(f, x); /* %$2 r s$% */
167 y = mprand(MP_NEW, nbits, r, 0);
168 y = mp_setbit(y, y, nbits - 2);
169 rr = mp_leastcongruent(rr, y, rr, x);
170 mp_drop(x); mp_drop(y);
171 }
172
173 /* --- Return the result --- */
174
175 mp_drop(q);
176 mp_drop(t);
177 mp_drop(s);
178 dstr_destroy(&dn);
179 return (rr);
180
181 /* --- Tidy up if something failed --- */
182
183 fail_r:
184 mp_drop(t);
185 fail_t:
186 mp_drop(s);
187 fail_s:
188 mp_drop(rr);
189 dstr_destroy(&dn);
190 return (0);
191 }
192
193 /* --- @strongprime@ --- *
194 *
195 * Arguments: @const char *name@ = pointer to name root
196 * @mp *d@ = destination integer
197 * @unsigned nbits@ = number of bits wanted
198 * @grand *r@ = random number source
199 * @unsigned n@ = number of attempts to make
200 * @pgen_proc *event@ = event handler function
201 * @void *ectx@ = argument for the event handler
202 *
203 * Returns: A `strong' prime, or zero.
204 *
205 * Use: Finds `strong' primes. A strong prime %$p$% is such that
206 *
207 * * %$p - 1$% has a large prime factor %$r$%,
208 * * %$p + 1$% has a large prime factor %$s$%, and
209 * * %$r - 1$% has a large prime factor %$t$%.
210 */
211
212 mp *strongprime(const char *name, mp *d, unsigned nbits, grand *r,
213 unsigned n, pgen_proc *event, void *ectx)
214 {
215 mp *p;
216 pfilt f;
217 pgen_jumpctx j;
218 rabin rb;
219
220 if (d) mp_copy(d);
221 p = strongprime_setup(name, d, &f, nbits, r, n, event, ectx);
222 if (!p) { mp_drop(d); return (0); }
223 j.j = &f;
224 p = pgen(name, p, p, event, ectx, n, pgen_jump, &j,
225 rabin_iters(nbits), pgen_test, &rb);
226 if (mp_bits(p) != nbits) { mp_drop(p); return (0); }
227 pfilt_destroy(&f);
228 mp_drop(d);
229 return (p);
230 }
231
232 /*----- That's all, folks -------------------------------------------------*/