Add multiply-and-add function for Diffie-Hellman safe prime generation.
[u/mdw/catacomb] / pgen.c
1 /* -*-c-*-
2 *
3 * $Id: pgen.c,v 1.2 1999/11/20 22:23:05 mdw Exp $
4 *
5 * Finding and testing 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: pgen.c,v $
33 * Revision 1.2 1999/11/20 22:23:05 mdw
34 * Add multiply-and-add function for Diffie-Hellman safe prime generation.
35 *
36 * Revision 1.1 1999/11/19 13:17:57 mdw
37 * Prime number generator and tester.
38 *
39 */
40
41 /*----- Header files ------------------------------------------------------*/
42
43 #include "mp.h"
44 #include "mpmont.h"
45 #include "pgen.h"
46 #include "ptab.h"
47 #include "rabin.h"
48
49 /*----- Main code ---------------------------------------------------------*/
50
51 /* --- @pgen_create@ --- *
52 *
53 * Arguments: @pgen *p@ = pointer to prime generation context
54 * @mp *m@ = pointer to initial number to test
55 *
56 * Returns: One of the @PGEN@ constants above.
57 *
58 * Use: Tests an initial number for primality by computing its
59 * residue modulo various small prime numbers. This is fairly
60 * quick, but not particularly certain. If a @PGEN_MAYBE@
61 * result is returned, perform Rabin-Miller tests to confirm.
62 */
63
64 int pgen_create(pgen *p, mp *m)
65 {
66 int rc = PGEN_MAYBE;
67 int i;
68 mp *r = MP_NEW;
69 mpw qw;
70 mp q;
71
72 /* --- Take a copy of the number --- */
73
74 mp_shrink(m);
75 p->m = MP_COPY(m);
76
77 /* --- Fill in the residues --- */
78
79 mp_build(&q, &qw, &qw + 1);
80 for (i = 0; i < NPRIME; i++) {
81 qw = ptab[i];
82 mp_div(0, &r, m, &q);
83 p->r[i] = r->v[0];
84 if (!p->r[i] && rc == PGEN_MAYBE) {
85 if (MP_LEN(m) == 1 && m->v[0] == ptab[i])
86 rc = PGEN_PRIME;
87 else
88 rc = PGEN_COMPOSITE;
89 }
90 }
91
92 /* --- Done --- */
93
94 mp_drop(r);
95 return (rc);
96 }
97
98 /* --- @pgen_destroy@ --- *
99 *
100 * Arguments: @pgen *p@ = pointer to prime generation context
101 *
102 * Returns: ---
103 *
104 * Use: Discards a context and all the resources it holds.
105 */
106
107 void pgen_destroy(pgen *p)
108 {
109 mp_drop(p->m);
110 }
111
112 /* --- @pgen_step@ --- *
113 *
114 * Arguments: @pgen *p@ = pointer to prime generation context
115 * @mpw step@ = how much to step the number
116 *
117 * Returns: One of the @PGEN@ constants above.
118 *
119 * Use: Steps a number by a small amount. Stepping is much faster
120 * than initializing with a new number. The test performed is
121 * the same simple one used by @ptab_create@, so @PGEN_MAYBE@
122 * results should be followed up by a Rabin-Miller test.
123 */
124
125 int pgen_step(pgen *p, mpw step)
126 {
127 int rc = PGEN_MAYBE;
128 int i;
129
130 /* --- Add the step on to the number --- */
131
132 p->m = mp_modify(p->m, MP_LEN(p->m) + 1);
133 mpx_uaddn(p->m->v, p->m->vl, step);
134 mp_shrink(p->m);
135
136 /* --- Update the residue table --- */
137
138 for (i = 0; i < NPRIME; i++) {
139 p->r[i] = (p->r[i] + step) % ptab[i];
140 if (!p->r[i] && rc == PGEN_MAYBE) {
141 if (MP_LEN(p->m) == 1 && p->m->v[0] == ptab[i])
142 rc = PGEN_PRIME;
143 else
144 rc = PGEN_COMPOSITE;
145 }
146 }
147
148 /* --- Small numbers must be prime --- */
149
150 if (rc == PGEN_MAYBE && MP_LEN(p->m) == 1 &&
151 p->m->v[0] < MAXPRIME * MAXPRIME)
152 rc = PGEN_PRIME;
153
154 /* --- Done --- */
155
156 return (rc);
157 }
158
159 /* --- @pgen_muladd@ --- *
160 *
161 * Arguments: @pgen *p@ = destination prime generation context
162 * @const pgen *q@ = source prime generation context
163 * @mpw m@ = number to multiply by
164 * @mpw a@ = number to add
165 *
166 * Returns: One of the @PGEN@ constants above.
167 *
168 * Use: Multiplies the number in a prime generation context by a
169 * small value and then adds a small value. The destination
170 * should either be uninitialized or the same as the source.
171 *
172 * Common things to do include multiplying by 2 and adding 0 to
173 * turn a prime into a jump for finding other primes with @q@ as
174 * a factor of @p - 1@, or multiplying by 2 and adding 1.
175 */
176
177 int pgen_muladd(pgen *p, const pgen *q, mpw m, mpw a)
178 {
179 int rc = PGEN_MAYBE;
180 int i;
181
182 /* --- Multiply the big number --- */
183
184 {
185 mp *d = mp_create(MP_LEN(q->m) + 2);
186 mpx_umuln(d->v, d->vl, q->m->v, q->m->vl, m);
187 mpx_uaddn(d->v, d->vl, a);
188 d->f = q->m->f;
189 if (p == q)
190 mp_drop(p->m);
191 mp_shrink(d);
192 p->m = d;
193 }
194
195 /* --- Gallivant through the residue table --- */
196
197 for (i = 0; i < NPRIME; i++) {
198 p->r[i] = (q->r[i] * m + a) % ptab[i];
199 if (!p->r[i] && rc == PGEN_MAYBE) {
200 if (MP_LEN(p->m) == 1 && p->m->v[0] == ptab[i])
201 rc = PGEN_PRIME;
202 else
203 rc = PGEN_COMPOSITE;
204 }
205 }
206
207 /* --- Small numbers must be prime --- */
208
209 if (rc == PGEN_MAYBE && MP_LEN(p->m) == 1 &&
210 p->m->v[0] < MAXPRIME * MAXPRIME)
211 rc = PGEN_PRIME;
212
213 /* --- Finished --- */
214
215 return (rc);
216 }
217
218
219 /* --- @pgen_jump@ --- *
220 *
221 * Arguments: @pgen *p@ = pointer to prime generation context
222 * @pgen *j@ = pointer to another generation context
223 *
224 * Returns: One of the @PGEN@ constants above.
225 *
226 * Use: Steps a number by a large amount. Even so, jumping is much
227 * faster than initializing a new number. The test peformed is
228 * the same simple one used by @ptab_create@, so @PGEN_MAYBE@
229 * results should be followed up by a Rabin-Miller test.
230 *
231 * Note that the number stored in the @j@ context is probably
232 * better off being even than prime. The important thing is
233 * that all of the residues for the number have already been
234 * computed.
235 */
236
237 int pgen_jump(pgen *p, pgen *j)
238 {
239 int rc = PGEN_MAYBE;
240 int i;
241
242 /* --- Add the step on --- */
243
244 p->m = mp_add(p->m, p->m, j->m);
245
246 /* --- Update the residue table --- */
247
248 for (i = 0; i < NPRIME; i++) {
249 p->r[i] = p->r[i] + j->r[i];
250 if (p->r[i] > ptab[i])
251 p->r[i] -= ptab[i];
252 if (!p->r[i] && rc == PGEN_MAYBE) {
253 if (MP_LEN(p->m) == 1 && p->m->v[0] == ptab[i])
254 rc = PGEN_PRIME;
255 else
256 rc = PGEN_COMPOSITE;
257 }
258 }
259
260 /* --- Small numbers must be prime --- */
261
262 if (rc == PGEN_MAYBE && MP_LEN(p->m) == 1 &&
263 p->m->v[0] < MAXPRIME * MAXPRIME)
264 rc = PGEN_PRIME;
265
266 /* --- Done --- */
267
268 return (rc);
269 }
270
271 /*----- Test code ---------------------------------------------------------*/
272
273 #ifdef TEST_RIG
274
275 #include <mLib/testrig.h>
276
277 static int verify(dstr *v)
278 {
279 mp *m = *(mp **)v[0].buf;
280 mp *r = *(mp **)v[1].buf;
281 pgen p;
282 mpw gw;
283 mp g;
284 int rc;
285 static char baton[] = "/-\\|";
286 char *b = baton;
287 int ok = 1;
288
289 mp_build(&g, &gw, &gw + 1);
290 rc = pgen_create(&p, m);
291 for (;;) {
292 if (rc == PGEN_PRIME)
293 break;
294 if (rc == PGEN_MAYBE) {
295 int i;
296 rabin r;
297 rabin_create(&r, p.m);
298 for (i = 0; i < 5; i++) {
299 gw = rand();
300 if (rabin_test(&r, &g) == PGEN_COMPOSITE)
301 break;
302 }
303 rabin_destroy(&r);
304 if (i == 5)
305 break;
306 putc(*b++, stderr);
307 putc('\b', stderr);
308 if (!*b)
309 b = baton;
310 }
311 rc = pgen_step(&p, 2);
312 }
313
314 if (MP_CMP(p.m, !=, r)) {
315 fputs("\n*** failed.", stderr);
316 fputs("\nm = ", stderr); mp_writefile(m, stderr, 10);
317 fputs("\np = ", stderr); mp_writefile(p.m, stderr, 10);
318 fputs("\nr = ", stderr); mp_writefile(r, stderr, 10);
319 fputc('\n', stderr);
320 ok = 0;
321 }
322
323 mp_drop(m);
324 mp_drop(r);
325 pgen_destroy(&p);
326 return (ok);
327 }
328
329 static test_chunk tests[] = {
330 { "pgen", verify, { &type_mp, &type_mp, 0 } },
331 { 0, 0, { 0 } }
332 };
333
334 int main(int argc, char *argv[])
335 {
336 sub_init();
337 test_run(argc, argv, tests, SRCDIR "/tests/pgen");
338 return (0);
339 }
340
341 #endif
342
343 /*----- That's all, folks -------------------------------------------------*/