a30942cc |
1 | /* -*-c-*- |
2 | * |
3 | * $Id: strongprime.c,v 1.1 1999/12/22 15:51:22 mdw Exp $ |
4 | * |
5 | * Generate `strong' prime numbers |
6 | * |
7 | * (c) 1999 Straylight/Edgeware |
8 | */ |
9 | |
10 | /*----- Licensing notice --------------------------------------------------* |
11 | * |
12 | * This file is part of Catacomb. |
13 | * |
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. |
18 | * |
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. |
23 | * |
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, |
27 | * MA 02111-1307, USA. |
28 | */ |
29 | |
30 | /*----- Revision history --------------------------------------------------* |
31 | * |
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. |
35 | * |
36 | */ |
37 | |
38 | /*----- Header files ------------------------------------------------------*/ |
39 | |
40 | #include <mLib/dstr.h> |
41 | |
42 | #include "grand.h" |
43 | #include "rand.h" |
44 | #include "mp.h" |
45 | #include "mpmont.h" |
46 | #include "mprand.h" |
47 | #include "pgen.h" |
48 | #include "pfilt.h" |
49 | #include "rabin.h" |
50 | |
51 | /*----- Main code ---------------------------------------------------------*/ |
52 | |
53 | /* --- @strongprime@ --- * |
54 | * |
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 |
62 | * |
63 | * Returns: A `strong' prime, or zero. |
64 | * |
65 | * Use: Finds `strong' primes. A strong prime %$p$% is such that |
66 | * |
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$%. |
70 | * |
71 | * The numbers produced may be slightly larger than requested, |
72 | * by a few bits. |
73 | */ |
74 | |
75 | mp *strongprime(const char *name, mp *d, unsigned nbits, grand *r, |
76 | unsigned n, pgen_proc *event, void *ectx) |
77 | { |
78 | mp *s, *t, *q, *p = 0; |
79 | dstr dn = DSTR_INIT; |
80 | |
81 | mp *rr = MP_NEW; |
82 | pgen_filterctx c; |
83 | pgen_jumpctx cj; |
84 | rabin rb; |
85 | |
86 | /* --- The bitslop parameter --- * |
87 | * |
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. |
92 | */ |
93 | |
94 | #define BITSLOP 10 |
95 | |
96 | /* --- Choose two primes %$s$% and %$t$% of half the required size --- */ |
97 | |
98 | nbits = nbits/2 - BITSLOP; |
99 | c.step = 1; |
100 | |
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) |
105 | goto fail_s; |
106 | mp_burn(s); |
107 | |
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) |
112 | goto fail_t; |
113 | mp_burn(t); |
114 | |
115 | /* --- Choose a suitable value for %$r = 2it + 1$% for some %$i$% --- */ |
116 | |
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); |
122 | cj.j = &c.f; |
123 | nbits += BITSLOP; |
124 | if ((q = pgen(dn.buf, MP_NEW, rr, event, ectx, n, pgen_jump, &cj, |
125 | rabin_iters(nbits), pgen_test, &rb)) == 0) |
126 | goto fail_r; |
127 | pfilt_destroy(&c.f); |
128 | |
129 | /* --- Select a suitable starting-point for finding %$p$% --- * |
130 | * |
131 | * This computes %$p_0 = 2(s^{r - 2} \bmod r)s - 1$%. |
132 | */ |
133 | |
134 | { |
135 | mpmont mm; |
136 | |
137 | mpmont_create(&mm, q); |
138 | rr = mp_sub(rr, q, MP_TWO); |
139 | rr = mpmont_exp(&mm, rr, s, rr); |
140 | mpmont_destroy(&mm); |
141 | rr = mp_mul(rr, rr, s); |
142 | rr = mp_lsl(rr, rr, 1); |
143 | rr = mp_sub(rr, rr, MP_ONE); |
144 | } |
145 | |
146 | /* --- Now find %$p = p_0 + 2jrs$% for some %$j$% --- */ |
147 | |
148 | { |
149 | mp *x; |
150 | x = mp_mul(MP_NEW, q, s); |
151 | x = mp_lsl(x, x, 1); |
152 | pfilt_create(&c.f, x); |
153 | x = mp_lsl(x, x, BITSLOP - 1); |
154 | rr = mp_add(rr, rr, x); |
155 | mp_drop(x); |
156 | } |
157 | |
158 | if ((p = pgen(name, d, rr, event, ectx, n, pgen_jump, &cj, |
159 | rabin_iters(nbits * 2), pgen_test, &rb)) == 0) |
160 | goto fail_p; |
161 | |
162 | /* --- Tidy up because we've finished --- */ |
163 | |
164 | fail_p: |
165 | mp_drop(q); |
166 | fail_r: |
167 | pfilt_destroy(&c.f); |
168 | mp_drop(t); |
169 | fail_t: |
170 | mp_drop(s); |
171 | fail_s: |
172 | mp_drop(rr); |
173 | dstr_destroy(&dn); |
174 | |
175 | return (p); |
176 | |
177 | #undef BITSLOP |
178 | } |
179 | |
180 | /*----- That's all, folks -------------------------------------------------*/ |