3 * $Id: pfilt.c,v 1.6 2004/04/08 01:36:15 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 /*----- Header files ------------------------------------------------------*/
38 /*----- Main code ---------------------------------------------------------*/
40 /* --- @smallenough@ --- *
42 * Arguments: @mp *m@ = integer to test
44 * Returns: One of the @PGEN@ result codes.
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.
52 static int smallenough(mp
*m
)
58 max
= mp_fromuint(MP_NEW
, MAXPRIME
);
59 max
= mp_sqr(max
, max
);
60 max
->a
->n
--; /* Permanent allocation */
62 if (MP_CMP(m
, <=, MP_ONE
))
64 else if (MP_CMP(m
, <, max
))
69 /* --- @pfilt_smallfactor@ --- *
71 * Arguments: @mp *m@ = integer to test
73 * Returns: One of the @PGEN@ result codes.
75 * Use: Tests a number by dividing by a number of small primes. This
76 * is a useful first step if you're testing random primes; for
77 * sequential searches, @pfilt_create@ works better.
80 int pfilt_smallfactor(mp
*m
)
84 size_t sz
= MP_LEN(m
);
85 mparena
*a
= m
->a ? m
->a
: MPARENA_GLOBAL
;
86 mpw
*v
= mpalloc(a
, sz
);
88 /* --- Fill in the residues --- */
90 for (i
= 0; i
< NPRIME
; i
++) {
91 if (!mpx_udivn(v
, v
+ sz
, m
->v
, m
->vl
, primetab
[i
])) {
92 if (MP_LEN(m
) == 1 && m
->v
[0] == primetab
[i
])
100 /* --- Check for small primes --- */
111 /* --- @pfilt_create@ --- *
113 * Arguments: @pfilt *p@ = pointer to prime filtering context
114 * @mp *m@ = pointer to initial number to test
116 * Returns: One of the @PGEN@ result codes.
118 * Use: Tests an initial number for primality by computing its
119 * residue modulo various small prime numbers. This is fairly
120 * quick, but not particularly certain. If a @PGEN_TRY@
121 * result is returned, perform Rabin-Miller tests to confirm.
124 int pfilt_create(pfilt
*p
, mp
*m
)
128 size_t sz
= MP_LEN(m
);
129 mparena
*a
= m
->a ? m
->a
: MPARENA_GLOBAL
;
130 mpw
*v
= mpalloc(a
, sz
);
132 /* --- Take a copy of the number --- */
137 /* --- Fill in the residues --- */
139 for (i
= 0; i
< NPRIME
; i
++) {
140 p
->r
[i
] = mpx_udivn(v
, v
+ sz
, m
->v
, m
->vl
, primetab
[i
]);
141 if (!p
->r
[i
] && rc
== PGEN_TRY
) {
142 if (MP_LEN(m
) == 1 && m
->v
[0] == primetab
[i
])
149 /* --- Check for small primes --- */
160 /* --- @pfilt_destroy@ --- *
162 * Arguments: @pfilt *p@ = pointer to prime filtering context
166 * Use: Discards a context and all the resources it holds.
169 void pfilt_destroy(pfilt
*p
)
174 /* --- @pfilt_step@ --- *
176 * Arguments: @pfilt *p@ = pointer to prime filtering context
177 * @mpw step@ = how much to step the number
179 * Returns: One of the @PGEN@ result codes.
181 * Use: Steps a number by a small amount. Stepping is much faster
182 * than initializing with a new number. The test performed is
183 * the same simple one used by @primetab_create@, so @PGEN_TRY@
184 * results should be followed up by a Rabin-Miller test.
187 int pfilt_step(pfilt
*p
, mpw step
)
192 /* --- Add the step on to the number --- */
194 p
->m
= mp_split(p
->m
);
195 mp_ensure(p
->m
, MP_LEN(p
->m
) + 1);
196 mpx_uaddn(p
->m
->v
, p
->m
->vl
, step
);
199 /* --- Update the residue table --- */
201 for (i
= 0; i
< NPRIME
; i
++) {
202 p
->r
[i
] = (p
->r
[i
] + step
) % primetab
[i
];
203 if (!p
->r
[i
] && rc
== PGEN_TRY
) {
204 if (MP_LEN(p
->m
) == 1 && p
->m
->v
[0] == primetab
[i
])
211 /* --- Check for small primes --- */
214 rc
= smallenough(p
->m
);
221 /* --- @pfilt_muladd@ --- *
223 * Arguments: @pfilt *p@ = destination prime filtering context
224 * @const pfilt *q@ = source prime filtering context
225 * @mpw m@ = number to multiply by
226 * @mpw a@ = number to add
228 * Returns: One of the @PGEN@ result codes.
230 * Use: Multiplies the number in a prime filtering context by a
231 * small value and then adds a small value. The destination
232 * should either be uninitialized or the same as the source.
234 * Common things to do include multiplying by 2 and adding 0 to
235 * turn a prime into a jump for finding other primes with @q@ as
236 * a factor of @p - 1@, or multiplying by 2 and adding 1.
239 int pfilt_muladd(pfilt
*p
, const pfilt
*q
, mpw m
, mpw a
)
244 /* --- Multiply the big number --- */
247 mp
*d
= mp_new(MP_LEN(q
->m
) + 2, q
->m
->f
);
248 mpx_umuln(d
->v
, d
->vl
, q
->m
->v
, q
->m
->vl
, m
);
249 mpx_uaddn(d
->v
, d
->vl
, a
);
256 /* --- Gallivant through the residue table --- */
258 for (i
= 0; i
< NPRIME
; i
++) {
259 p
->r
[i
] = (q
->r
[i
] * m
+ a
) % primetab
[i
];
260 if (!p
->r
[i
] && rc
== PGEN_TRY
) {
261 if (MP_LEN(p
->m
) == 1 && p
->m
->v
[0] == primetab
[i
])
268 /* --- Check for small primes --- */
271 rc
= smallenough(p
->m
);
273 /* --- Finished --- */
278 /* --- @pfilt_jump@ --- *
280 * Arguments: @pfilt *p@ = pointer to prime filtering context
281 * @const pfilt *j@ = pointer to another filtering context
283 * Returns: One of the @PGEN@ result codes.
285 * Use: Steps a number by a large amount. Even so, jumping is much
286 * faster than initializing a new number. The test peformed is
287 * the same simple one used by @primetab_create@, so @PGEN_TRY@
288 * results should be followed up by a Rabin-Miller test.
290 * Note that the number stored in the @j@ context is probably
291 * better off being even than prime. The important thing is
292 * that all of the residues for the number have already been
296 int pfilt_jump(pfilt
*p
, const pfilt
*j
)
301 /* --- Add the step on --- */
303 p
->m
= mp_add(p
->m
, p
->m
, j
->m
);
305 /* --- Update the residue table --- */
307 for (i
= 0; i
< NPRIME
; i
++) {
308 p
->r
[i
] = p
->r
[i
] + j
->r
[i
];
309 if (p
->r
[i
] > primetab
[i
])
310 p
->r
[i
] -= primetab
[i
];
311 if (!p
->r
[i
] && rc
== PGEN_TRY
) {
312 if (MP_LEN(p
->m
) == 1 && p
->m
->v
[0] == primetab
[i
])
319 /* --- Check for small primes --- */
322 rc
= smallenough(p
->m
);
329 /*----- That's all, folks -------------------------------------------------*/