3 * $Id: pgen.c,v 1.2 1999/11/20 22:23:05 mdw Exp $
5 * Finding and testing prime numbers
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.2 1999/11/20 22:23:05 mdw
34 * Add multiply-and-add function for Diffie-Hellman safe prime generation.
36 * Revision 1.1 1999/11/19 13:17:57 mdw
37 * Prime number generator and tester.
41 /*----- Header files ------------------------------------------------------*/
49 /*----- Main code ---------------------------------------------------------*/
51 /* --- @pgen_create@ --- *
53 * Arguments: @pgen *p@ = pointer to prime generation context
54 * @mp *m@ = pointer to initial number to test
56 * Returns: One of the @PGEN@ constants above.
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.
64 int pgen_create(pgen
*p
, mp
*m
)
72 /* --- Take a copy of the number --- */
77 /* --- Fill in the residues --- */
79 mp_build(&q
, &qw
, &qw
+ 1);
80 for (i
= 0; i
< NPRIME
; i
++) {
84 if (!p
->r
[i
] && rc
== PGEN_MAYBE
) {
85 if (MP_LEN(m
) == 1 && m
->v
[0] == ptab
[i
])
98 /* --- @pgen_destroy@ --- *
100 * Arguments: @pgen *p@ = pointer to prime generation context
104 * Use: Discards a context and all the resources it holds.
107 void pgen_destroy(pgen
*p
)
112 /* --- @pgen_step@ --- *
114 * Arguments: @pgen *p@ = pointer to prime generation context
115 * @mpw step@ = how much to step the number
117 * Returns: One of the @PGEN@ constants above.
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.
125 int pgen_step(pgen
*p
, mpw step
)
130 /* --- Add the step on to the number --- */
132 p
->m
= mp_modify(p
->m
, MP_LEN(p
->m
) + 1);
133 mpx_uaddn(p
->m
->v
, p
->m
->vl
, step
);
136 /* --- Update the residue table --- */
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
])
148 /* --- Small numbers must be prime --- */
150 if (rc
== PGEN_MAYBE
&& MP_LEN(p
->m
) == 1 &&
151 p
->m
->v
[0] < MAXPRIME
* MAXPRIME
)
159 /* --- @pgen_muladd@ --- *
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
166 * Returns: One of the @PGEN@ constants above.
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.
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.
177 int pgen_muladd(pgen
*p
, const pgen
*q
, mpw m
, mpw a
)
182 /* --- Multiply the big number --- */
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
);
195 /* --- Gallivant through the residue table --- */
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
])
207 /* --- Small numbers must be prime --- */
209 if (rc
== PGEN_MAYBE
&& MP_LEN(p
->m
) == 1 &&
210 p
->m
->v
[0] < MAXPRIME
* MAXPRIME
)
213 /* --- Finished --- */
219 /* --- @pgen_jump@ --- *
221 * Arguments: @pgen *p@ = pointer to prime generation context
222 * @pgen *j@ = pointer to another generation context
224 * Returns: One of the @PGEN@ constants above.
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.
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
237 int pgen_jump(pgen
*p
, pgen
*j
)
242 /* --- Add the step on --- */
244 p
->m
= mp_add(p
->m
, p
->m
, j
->m
);
246 /* --- Update the residue table --- */
248 for (i
= 0; i
< NPRIME
; i
++) {
249 p
->r
[i
] = p
->r
[i
] + j
->r
[i
];
250 if (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
])
260 /* --- Small numbers must be prime --- */
262 if (rc
== PGEN_MAYBE
&& MP_LEN(p
->m
) == 1 &&
263 p
->m
->v
[0] < MAXPRIME
* MAXPRIME
)
271 /*----- Test code ---------------------------------------------------------*/
275 #include <mLib/testrig.h>
277 static int verify(dstr
*v
)
279 mp
*m
= *(mp
**)v
[0].buf
;
280 mp
*r
= *(mp
**)v
[1].buf
;
285 static char baton
[] = "/-\\|";
289 mp_build(&g
, &gw
, &gw
+ 1);
290 rc
= pgen_create(&p
, m
);
292 if (rc
== PGEN_PRIME
)
294 if (rc
== PGEN_MAYBE
) {
297 rabin_create(&r
, p
.m
);
298 for (i
= 0; i
< 5; i
++) {
300 if (rabin_test(&r
, &g
) == PGEN_COMPOSITE
)
311 rc
= pgen_step(&p
, 2);
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);
329 static test_chunk tests
[] = {
330 { "pgen", verify
, { &type_mp
, &type_mp
, 0 } },
334 int main(int argc
, char *argv
[])
337 test_run(argc
, argv
, tests
, SRCDIR
"/tests/pgen");
343 /*----- That's all, folks -------------------------------------------------*/