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