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
, <, max
))
67 /* --- @pfilt_smallfactor@ --- *
69 * Arguments: @mp *m@ = integer to test
71 * Returns: One of the @PGEN@ result codes.
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.
78 int pfilt_smallfactor(mp
*m
)
82 size_t sz
= MP_LEN(m
);
83 mparena
*a
= m
->a ? m
->a
: MPARENA_GLOBAL
;
84 mpw
*v
= mpalloc(a
, sz
);
86 /* --- Fill in the residues --- */
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
])
97 /* --- Check for small primes --- */
108 /* --- @pfilt_create@ --- *
110 * Arguments: @pfilt *p@ = pointer to prime filtering context
111 * @mp *m@ = pointer to initial number to test
113 * Returns: One of the @PGEN@ result codes.
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.
121 int pfilt_create(pfilt
*p
, mp
*m
)
125 size_t sz
= MP_LEN(m
);
126 mparena
*a
= m
->a ? m
->a
: MPARENA_GLOBAL
;
127 mpw
*v
= mpalloc(a
, sz
);
129 /* --- Take a copy of the number --- */
134 /* --- Fill in the residues --- */
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
])
146 /* --- Check for small primes --- */
157 /* --- @pfilt_destroy@ --- *
159 * Arguments: @pfilt *p@ = pointer to prime filtering context
163 * Use: Discards a context and all the resources it holds.
166 void pfilt_destroy(pfilt
*p
)
171 /* --- @pfilt_step@ --- *
173 * Arguments: @pfilt *p@ = pointer to prime filtering context
174 * @mpw step@ = how much to step the number
176 * Returns: One of the @PGEN@ result codes.
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.
184 int pfilt_step(pfilt
*p
, mpw step
)
189 /* --- Add the step on to the number --- */
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
);
196 /* --- Update the residue table --- */
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
])
208 /* --- Check for small primes --- */
211 rc
= smallenough(p
->m
);
218 /* --- @pfilt_muladd@ --- *
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
225 * Returns: One of the @PGEN@ result codes.
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.
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.
236 int pfilt_muladd(pfilt
*p
, const pfilt
*q
, mpw m
, mpw a
)
241 /* --- Multiply the big number --- */
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
);
253 /* --- Gallivant through the residue table --- */
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
])
265 /* --- Check for small primes --- */
268 rc
= smallenough(p
->m
);
270 /* --- Finished --- */
275 /* --- @pfilt_jump@ --- *
277 * Arguments: @pfilt *p@ = pointer to prime filtering context
278 * @const pfilt *j@ = pointer to another filtering context
280 * Returns: One of the @PGEN@ result codes.
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.
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
293 int pfilt_jump(pfilt
*p
, const pfilt
*j
)
298 /* --- Add the step on --- */
300 p
->m
= mp_add(p
->m
, p
->m
, j
->m
);
302 /* --- Update the residue table --- */
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
])
316 /* --- Check for small primes --- */
319 rc
= smallenough(p
->m
);
326 /*----- That's all, folks -------------------------------------------------*/