3 * $Id: pfilt.c,v 1.3 2000/08/15 21:44:27 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.3 2000/08/15 21:44:27 mdw
34 * (pfilt_smallfactor): New function for doing trial division the hard
37 * (pfilt_create): Use @mpx_udivn@ for computing residues, for improved
40 * Pull the `small prime' test into a separate function, and do it
43 * Revision 1.2 2000/06/17 11:54:27 mdw
44 * Use new MP memory management functions.
46 * Revision 1.1 1999/12/22 15:49:39 mdw
47 * Renamed from `pgen'. Reworking for new prime-search system.
49 * Revision 1.3 1999/12/10 23:28:35 mdw
50 * Track suggested destination changes.
52 * Revision 1.2 1999/11/20 22:23:05 mdw
53 * Add multiply-and-add function for Diffie-Hellman safe prime generation.
55 * Revision 1.1 1999/11/19 13:17:57 mdw
56 * Prime number generator and tester.
60 /*----- Header files ------------------------------------------------------*/
67 #include "primorial.h"
69 /*----- Main code ---------------------------------------------------------*/
71 /* --- @smallenough@ --- *
73 * Arguments: @mp *m@ = integer to test
75 * Returns: One of the @PGEN@ result codes.
77 * Use: Assuming that @m@ has been tested by trial division on every
78 * prime in the small-primes array, this function will return
79 * @PGEN_DONE@ if the number is less than the square of the
80 * largest small prime.
83 static int smallenough(mp *m)
89 max = mp_fromuint(MP_NEW, MAXPRIME);
90 max = mp_sqr(max, max);
91 max->a->n--; /* Permanent allocation */
93 if (MP_CMP(m, <, max))
98 /* --- @pfilt_smallfactor@ --- *
100 * Arguments: @mp *m@ = integer to test
102 * Returns: One of the @PGEN@ result codes.
104 * Use: Tests a number by dividing by a number of small primes. This
105 * is a useful first step if you're testing random primes; for
106 * sequential searches, @pfilt_create@ works better.
109 int pfilt_smallfactor(mp *m)
113 size_t sz = MP_LEN(m);
114 mpw *v = mpalloc(m->a, sz);
116 /* --- Fill in the residues --- */
118 for (i = 0; i < NPRIME; i++) {
119 if (!mpx_udivn(v, v + sz, m->v, m->vl, primetab[i])) {
120 if (MP_LEN(m) == 1 && m->v[0] == primetab[i])
127 /* --- Check for small primes --- */
138 /* --- @pfilt_create@ --- *
140 * Arguments: @pfilt *p@ = pointer to prime filtering context
141 * @mp *m@ = pointer to initial number to test
143 * Returns: One of the @PGEN@ result codes.
145 * Use: Tests an initial number for primality by computing its
146 * residue modulo various small prime numbers. This is fairly
147 * quick, but not particularly certain. If a @PGEN_TRY@
148 * result is returned, perform Rabin-Miller tests to confirm.
151 int pfilt_create(pfilt *p, mp *m)
155 size_t sz = MP_LEN(m);
156 mpw *v = mpalloc(m->a, sz);
158 /* --- Take a copy of the number --- */
163 /* --- Fill in the residues --- */
165 for (i = 0; i < NPRIME; i++) {
166 p->r[i] = mpx_udivn(v, v + sz, m->v, m->vl, primetab[i]);
167 if (!p->r[i] && rc == PGEN_TRY) {
168 if (MP_LEN(m) == 1 && m->v[0] == primetab[i])
175 /* --- Check for small primes --- */
186 /* --- @pfilt_destroy@ --- *
188 * Arguments: @pfilt *p@ = pointer to prime filtering context
192 * Use: Discards a context and all the resources it holds.
195 void pfilt_destroy(pfilt *p)
200 /* --- @pfilt_step@ --- *
202 * Arguments: @pfilt *p@ = pointer to prime filtering context
203 * @mpw step@ = how much to step the number
205 * Returns: One of the @PGEN@ result codes.
207 * Use: Steps a number by a small amount. Stepping is much faster
208 * than initializing with a new number. The test performed is
209 * the same simple one used by @primetab_create@, so @PGEN_TRY@
210 * results should be followed up by a Rabin-Miller test.
213 int pfilt_step(pfilt *p, mpw step)
218 /* --- Add the step on to the number --- */
220 p->m = mp_split(p->m);
221 mp_ensure(p->m, MP_LEN(p->m) + 1);
222 mpx_uaddn(p->m->v, p->m->vl, step);
225 /* --- Update the residue table --- */
227 for (i = 0; i < NPRIME; i++) {
228 p->r[i] = (p->r[i] + step) % primetab[i];
229 if (!p->r[i] && rc == PGEN_TRY) {
230 if (MP_LEN(p->m) == 1 && p->m->v[0] == primetab[i])
237 /* --- Check for small primes --- */
240 rc = smallenough(p->m);
247 /* --- @pfilt_muladd@ --- *
249 * Arguments: @pfilt *p@ = destination prime filtering context
250 * @const pfilt *q@ = source prime filtering context
251 * @mpw m@ = number to multiply by
252 * @mpw a@ = number to add
254 * Returns: One of the @PGEN@ result codes.
256 * Use: Multiplies the number in a prime filtering context by a
257 * small value and then adds a small value. The destination
258 * should either be uninitialized or the same as the source.
260 * Common things to do include multiplying by 2 and adding 0 to
261 * turn a prime into a jump for finding other primes with @q@ as
262 * a factor of @p - 1@, or multiplying by 2 and adding 1.
265 int pfilt_muladd(pfilt *p, const pfilt *q, mpw m, mpw a)
270 /* --- Multiply the big number --- */
273 mp *d = mp_new(MP_LEN(q->m) + 2, q->m->f);
274 mpx_umuln(d->v, d->vl, q->m->v, q->m->vl, m);
275 mpx_uaddn(d->v, d->vl, a);
282 /* --- Gallivant through the residue table --- */
284 for (i = 0; i < NPRIME; i++) {
285 p->r[i] = (q->r[i] * m + a) % primetab[i];
286 if (!p->r[i] && rc == PGEN_TRY) {
287 if (MP_LEN(p->m) == 1 && p->m->v[0] == primetab[i])
294 /* --- Check for small primes --- */
297 rc = smallenough(p->m);
299 /* --- Finished --- */
304 /* --- @pfilt_jump@ --- *
306 * Arguments: @pfilt *p@ = pointer to prime filtering context
307 * @const pfilt *j@ = pointer to another filtering context
309 * Returns: One of the @PGEN@ result codes.
311 * Use: Steps a number by a large amount. Even so, jumping is much
312 * faster than initializing a new number. The test peformed is
313 * the same simple one used by @primetab_create@, so @PGEN_TRY@
314 * results should be followed up by a Rabin-Miller test.
316 * Note that the number stored in the @j@ context is probably
317 * better off being even than prime. The important thing is
318 * that all of the residues for the number have already been
322 int pfilt_jump(pfilt *p, const pfilt *j)
327 /* --- Add the step on --- */
329 p->m = mp_add(p->m, p->m, j->m);
331 /* --- Update the residue table --- */
333 for (i = 0; i < NPRIME; i++) {
334 p->r[i] = p->r[i] + j->r[i];
335 if (p->r[i] > primetab[i])
336 p->r[i] -= primetab[i];
337 if (!p->r[i] && rc == PGEN_TRY) {
338 if (MP_LEN(p->m) == 1 && p->m->v[0] == primetab[i])
345 /* --- Check for small primes --- */
348 rc = smallenough(p->m);
355 /*----- That's all, folks -------------------------------------------------*/