3 * Finding and testing prime numbers
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 #ifndef CATACOMB_PFILT_H
29 #define CATACOMB_PFILT_H
35 /*----- Header files ------------------------------------------------------*/
41 #ifndef CATACOMB_PRIMETAB_H
42 # include "primetab.h"
45 /*----- Data structures ---------------------------------------------------*/
47 typedef struct pfilt {
52 /*----- Functions provided ------------------------------------------------*/
54 /* --- @pfilt_smallfactor@ --- *
56 * Arguments: @mp *m@ = integer to test
58 * Returns: One of the @PGEN@ result codes.
60 * Use: Tests a number by dividing by a number of small primes. This
61 * is a useful first step if you're testing random primes; for
62 * sequential searches, @pfilt_create@ works better.
65 extern int pfilt_smallfactor(mp */*m*/);
67 /* --- @pfilt_create@ --- *
69 * Arguments: @pfilt *p@ = pointer to prime filtering context
70 * @mp *m@ = pointer to initial number to test
72 * Returns: A @PGEN@ result code.
74 * Use: Tests an initial number for primality by computing its
75 * residue modulo various small prime numbers. This is fairly
76 * quick, but not particularly certain. If a @PGEN_TRY@
77 * result is returned, perform Rabin-Miller tests to confirm.
80 extern int pfilt_create(pfilt */*p*/, mp */*m*/);
82 /* --- @pfilt_destroy@ --- *
84 * Arguments: @pfilt *p@ = pointer to prime filtering context
88 * Use: Discards a context and all the resources it holds.
91 extern void pfilt_destroy(pfilt */*p*/);
93 /* --- @pfilt_step@ --- *
95 * Arguments: @pfilt *p@ = pointer to prime filtering context
96 * @mpw step@ = how much to step the number
98 * Returns: One of the @PGEN@ result codes.
100 * Use: Steps a number by a small amount. Stepping is much faster
101 * than initializing with a new number. The test performed is
102 * the same simple one used by @primetab_create@, so @PGEN_TRY@
103 * results should be followed up by a Rabin-Miller test.
106 extern int pfilt_step(pfilt */*p*/, mpw /*step*/);
108 /* --- @pfilt_muladd@ --- *
110 * Arguments: @pfilt *p@ = destination prime filtering context
111 * @const pfilt *q@ = source prime filtering context
112 * @mpw m@ = number to multiply by
113 * @mpw a@ = number to add
115 * Returns: One of the @PGEN@ result codes.
117 * Use: Multiplies the number in a prime filtering context by a
118 * small value and then adds a small value. The destination
119 * should either be uninitialized or the same as the source.
121 * Common things to do include multiplying by 2 and adding 0 to
122 * turn a prime into a jump for finding other primes with @q@ as
123 * a factor of @p - 1@, or multiplying by 2 and adding 1.
126 extern int pfilt_muladd(pfilt */*p*/, const pfilt */*q*/,
127 mpw /*m*/, mpw /*a*/);
129 /* --- @pfilt_jump@ --- *
131 * Arguments: @pfilt *p@ = pointer to prime filtering context
132 * @const pfilt *j@ = pointer to another filtering context
134 * Returns: One of the @PGEN@ result codes.
136 * Use: Steps a number by a large amount. Even so, jumping is much
137 * faster than initializing a new number. The test peformed is
138 * the same simple one used by @primetab_create@, so @PGEN_TRY@
139 * results should be followed up by a Rabin-Miller test.
141 * Note that the number stored in the @j@ context is probably
142 * better off being even than prime. The important thing is
143 * that all of the residues for the number have already been
147 extern int pfilt_jump(pfilt */*p*/, const pfilt */*j*/);
149 /*----- That's all, folks -------------------------------------------------*/