chiark / gitweb /
Expunge revision histories in files.
[catacomb] / pfilt.c
1 /* -*-c-*-
2  *
3  * $Id: pfilt.c,v 1.6 2004/04/08 01:36:15 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 /*----- Header files ------------------------------------------------------*/
31
32 #include "mp.h"
33 #include "mpint.h"
34 #include "pfilt.h"
35 #include "pgen.h"
36 #include "primetab.h"
37
38 /*----- Main code ---------------------------------------------------------*/
39
40 /* --- @smallenough@ --- *
41  *
42  * Arguments:   @mp *m@ = integer to test
43  *
44  * Returns:     One of the @PGEN@ result codes.
45  *
46  * Use:         Assuming that @m@ has been tested by trial division on every
47  *              prime in the small-primes array, this function will return
48  *              @PGEN_DONE@ if the number is less than the square of the
49  *              largest small prime.
50  */
51
52 static int smallenough(mp *m)
53 {
54   static mp *max = 0;
55   int rc = PGEN_TRY;
56
57   if (!max) {
58     max = mp_fromuint(MP_NEW, MAXPRIME);
59     max = mp_sqr(max, max);
60     max->a->n--; /* Permanent allocation */
61   }
62   if (MP_CMP(m, <, max))
63     rc = PGEN_DONE;
64   return (rc);
65 }
66
67 /* --- @pfilt_smallfactor@ --- *
68  *
69  * Arguments:   @mp *m@ = integer to test
70  *
71  * Returns:     One of the @PGEN@ result codes.
72  *
73  * Use:         Tests a number by dividing by a number of small primes.  This
74  *              is a useful first step if you're testing random primes; for
75  *              sequential searches, @pfilt_create@ works better.
76  */
77
78 int pfilt_smallfactor(mp *m)
79 {
80   int rc = PGEN_TRY;
81   int i;
82   size_t sz = MP_LEN(m);
83   mparena *a = m->a ? m->a : MPARENA_GLOBAL;
84   mpw *v = mpalloc(a, sz);
85
86   /* --- Fill in the residues --- */
87
88   for (i = 0; i < NPRIME; i++) {
89     if (!mpx_udivn(v, v + sz, m->v, m->vl, primetab[i])) {
90       if (MP_LEN(m) == 1 && m->v[0] == primetab[i])
91         rc = PGEN_DONE;
92       else
93         rc = PGEN_FAIL;
94     }
95   }
96
97   /* --- Check for small primes --- */
98
99   if (rc == PGEN_TRY)
100     rc = smallenough(m);
101
102   /* --- Done --- */
103
104   mpfree(a, v);
105   return (rc);
106 }
107
108 /* --- @pfilt_create@ --- *
109  *
110  * Arguments:   @pfilt *p@ = pointer to prime filtering context
111  *              @mp *m@ = pointer to initial number to test
112  *
113  * Returns:     One of the @PGEN@ result codes.
114  *
115  * Use:         Tests an initial number for primality by computing its
116  *              residue modulo various small prime numbers.  This is fairly
117  *              quick, but not particularly certain.  If a @PGEN_TRY@
118  *              result is returned, perform Rabin-Miller tests to confirm.
119  */
120
121 int pfilt_create(pfilt *p, mp *m)
122 {
123   int rc = PGEN_TRY;
124   int i;
125   size_t sz = MP_LEN(m);
126   mparena *a = m->a ? m->a : MPARENA_GLOBAL;
127   mpw *v = mpalloc(a, sz);
128
129   /* --- Take a copy of the number --- */
130
131   mp_shrink(m);
132   p->m = MP_COPY(m);
133
134   /* --- Fill in the residues --- */
135
136   for (i = 0; i < NPRIME; i++) {
137     p->r[i] = mpx_udivn(v, v + sz, m->v, m->vl, primetab[i]);
138     if (!p->r[i] && rc == PGEN_TRY) {
139       if (MP_LEN(m) == 1 && m->v[0] == primetab[i])
140         rc = PGEN_DONE;
141       else
142         rc = PGEN_FAIL;
143     }
144   }
145
146   /* --- Check for small primes --- */
147
148   if (rc == PGEN_TRY)
149     rc = smallenough(m);
150
151   /* --- Done --- */
152
153   mpfree(a, v);
154   return (rc);
155 }
156
157 /* --- @pfilt_destroy@ --- *
158  *
159  * Arguments:   @pfilt *p@ = pointer to prime filtering context
160  *
161  * Returns:     ---
162  *
163  * Use:         Discards a context and all the resources it holds.
164  */
165
166 void pfilt_destroy(pfilt *p)
167 {
168   mp_drop(p->m);
169 }
170
171 /* --- @pfilt_step@ --- *
172  *
173  * Arguments:   @pfilt *p@ = pointer to prime filtering context
174  *              @mpw step@ = how much to step the number
175  *
176  * Returns:     One of the @PGEN@ result codes.
177  *
178  * Use:         Steps a number by a small amount.  Stepping is much faster
179  *              than initializing with a new number.  The test performed is
180  *              the same simple one used by @primetab_create@, so @PGEN_TRY@
181  *              results should be followed up by a Rabin-Miller test.
182  */
183
184 int pfilt_step(pfilt *p, mpw step)
185 {
186   int rc = PGEN_TRY;
187   int i;
188
189   /* --- Add the step on to the number --- */
190
191   p->m = mp_split(p->m);
192   mp_ensure(p->m, MP_LEN(p->m) + 1);
193   mpx_uaddn(p->m->v, p->m->vl, step);
194   mp_shrink(p->m);
195
196   /* --- Update the residue table --- */
197
198   for (i = 0; i < NPRIME; i++) {
199     p->r[i] = (p->r[i] + step) % primetab[i];
200     if (!p->r[i] && rc == PGEN_TRY) {
201       if (MP_LEN(p->m) == 1 && p->m->v[0] == primetab[i])
202         rc = PGEN_DONE;
203       else
204         rc = PGEN_FAIL;
205     }
206   }
207
208   /* --- Check for small primes --- */
209
210   if (rc == PGEN_TRY)
211     rc = smallenough(p->m);
212
213   /* --- Done --- */
214
215   return (rc);
216 }
217
218 /* --- @pfilt_muladd@ --- *
219  *
220  * Arguments:   @pfilt *p@ = destination prime filtering context
221  *              @const pfilt *q@ = source prime filtering context
222  *              @mpw m@ = number to multiply by
223  *              @mpw a@ = number to add
224  *
225  * Returns:     One of the @PGEN@ result codes.
226  *
227  * Use:         Multiplies the number in a prime filtering context by a
228  *              small value and then adds a small value.  The destination
229  *              should either be uninitialized or the same as the source.
230  *
231  *              Common things to do include multiplying by 2 and adding 0 to
232  *              turn a prime into a jump for finding other primes with @q@ as
233  *              a factor of @p - 1@, or multiplying by 2 and adding 1.
234  */
235
236 int pfilt_muladd(pfilt *p, const pfilt *q, mpw m, mpw a)
237 {
238   int rc = PGEN_TRY;
239   int i;
240
241   /* --- Multiply the big number --- */
242
243   {
244     mp *d = mp_new(MP_LEN(q->m) + 2, q->m->f);
245     mpx_umuln(d->v, d->vl, q->m->v, q->m->vl, m);
246     mpx_uaddn(d->v, d->vl, a);
247     if (p == q)
248       mp_drop(p->m);
249     mp_shrink(d);
250     p->m = d;
251   }
252
253   /* --- Gallivant through the residue table --- */
254       
255   for (i = 0; i < NPRIME; i++) {
256     p->r[i] = (q->r[i] * m + a) % primetab[i];
257     if (!p->r[i] && rc == PGEN_TRY) {
258       if (MP_LEN(p->m) == 1 && p->m->v[0] == primetab[i])
259         rc = PGEN_DONE;
260       else
261         rc = PGEN_FAIL;
262     }
263   }
264
265   /* --- Check for small primes --- */
266
267   if (rc == PGEN_TRY)
268     rc = smallenough(p->m);
269
270   /* --- Finished --- */
271
272   return (rc);
273 }
274
275 /* --- @pfilt_jump@ --- *
276  *
277  * Arguments:   @pfilt *p@ = pointer to prime filtering context
278  *              @const pfilt *j@ = pointer to another filtering context
279  *
280  * Returns:     One of the @PGEN@ result codes.
281  *
282  * Use:         Steps a number by a large amount.  Even so, jumping is much
283  *              faster than initializing a new number.  The test peformed is
284  *              the same simple one used by @primetab_create@, so @PGEN_TRY@
285  *              results should be followed up by a Rabin-Miller test.
286  *
287  *              Note that the number stored in the @j@ context is probably
288  *              better off being even than prime.  The important thing is
289  *              that all of the residues for the number have already been
290  *              computed.
291  */
292
293 int pfilt_jump(pfilt *p, const pfilt *j)
294 {
295   int rc = PGEN_TRY;
296   int i;
297
298   /* --- Add the step on --- */
299
300   p->m = mp_add(p->m, p->m, j->m);
301
302   /* --- Update the residue table --- */
303
304   for (i = 0; i < NPRIME; i++) {
305     p->r[i] = p->r[i] + j->r[i];
306     if (p->r[i] > primetab[i])
307       p->r[i] -= primetab[i];
308     if (!p->r[i] && rc == PGEN_TRY) {
309       if (MP_LEN(p->m) == 1 && p->m->v[0] == primetab[i])
310         rc = PGEN_DONE;
311       else
312         rc = PGEN_FAIL;
313     }
314   }
315
316   /* --- Check for small primes --- */
317
318   if (rc == PGEN_TRY)
319     rc = smallenough(p->m);
320
321   /* --- Done --- */
322
323   return (rc);
324 }
325
326 /*----- That's all, folks -------------------------------------------------*/