chiark / gitweb /
tests: Fix tests for 222c8a43... (mp-modsqrt change).
[catacomb] / pfilt.c
CommitLineData
9a8b0c8d 1/* -*-c-*-
2 *
b817bfc6 3 * $Id: pfilt.c,v 1.6 2004/04/08 01:36:15 mdw Exp $
9a8b0c8d 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
9a8b0c8d 30/*----- Header files ------------------------------------------------------*/
31
32#include "mp.h"
b8e79eeb 33#include "mpint.h"
9a8b0c8d 34#include "pfilt.h"
35#include "pgen.h"
36#include "primetab.h"
37
38/*----- Main code ---------------------------------------------------------*/
39
b8e79eeb 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
52static 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
78int pfilt_smallfactor(mp *m)
79{
80 int rc = PGEN_TRY;
81 int i;
82 size_t sz = MP_LEN(m);
34e4f738 83 mparena *a = m->a ? m->a : MPARENA_GLOBAL;
84 mpw *v = mpalloc(a, sz);
b8e79eeb 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
34e4f738 104 mpfree(a, v);
b8e79eeb 105 return (rc);
106}
107
9a8b0c8d 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
121int pfilt_create(pfilt *p, mp *m)
122{
123 int rc = PGEN_TRY;
124 int i;
b8e79eeb 125 size_t sz = MP_LEN(m);
34e4f738 126 mparena *a = m->a ? m->a : MPARENA_GLOBAL;
127 mpw *v = mpalloc(a, sz);
9a8b0c8d 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
9a8b0c8d 136 for (i = 0; i < NPRIME; i++) {
b8e79eeb 137 p->r[i] = mpx_udivn(v, v + sz, m->v, m->vl, primetab[i]);
9a8b0c8d 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
b8e79eeb 146 /* --- Check for small primes --- */
147
148 if (rc == PGEN_TRY)
149 rc = smallenough(m);
150
9a8b0c8d 151 /* --- Done --- */
152
34e4f738 153 mpfree(a, v);
9a8b0c8d 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
166void 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
184int 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
b8e79eeb 208 /* --- Check for small primes --- */
9a8b0c8d 209
b8e79eeb 210 if (rc == PGEN_TRY)
211 rc = smallenough(p->m);
9a8b0c8d 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
236int 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 {
87b63d41 244 mp *d = mp_new(MP_LEN(q->m) + 2, q->m->f);
9a8b0c8d 245 mpx_umuln(d->v, d->vl, q->m->v, q->m->vl, m);
246 mpx_uaddn(d->v, d->vl, a);
9a8b0c8d 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
b8e79eeb 265 /* --- Check for small primes --- */
9a8b0c8d 266
b8e79eeb 267 if (rc == PGEN_TRY)
268 rc = smallenough(p->m);
9a8b0c8d 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
293int 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
b8e79eeb 316 /* --- Check for small primes --- */
9a8b0c8d 317
b8e79eeb 318 if (rc == PGEN_TRY)
319 rc = smallenough(p->m);
9a8b0c8d 320
321 /* --- Done --- */
322
323 return (rc);
324}
325
326/*----- That's all, folks -------------------------------------------------*/