| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * Finding and testing prime numbers |
| 4 | * |
| 5 | * (c) 1999 Straylight/Edgeware |
| 6 | */ |
| 7 | |
| 8 | /*----- Licensing notice --------------------------------------------------* |
| 9 | * |
| 10 | * This file is part of Catacomb. |
| 11 | * |
| 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. |
| 16 | * |
| 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. |
| 21 | * |
| 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, |
| 25 | * MA 02111-1307, USA. |
| 26 | */ |
| 27 | |
| 28 | #ifndef CATACOMB_PFILT_H |
| 29 | #define CATACOMB_PFILT_H |
| 30 | |
| 31 | #ifdef __cplusplus |
| 32 | extern "C" { |
| 33 | #endif |
| 34 | |
| 35 | /*----- Header files ------------------------------------------------------*/ |
| 36 | |
| 37 | #ifndef CATACOMB_MP_H |
| 38 | # include "mp.h" |
| 39 | #endif |
| 40 | |
| 41 | #ifndef CATACOMB_PRIMETAB_H |
| 42 | # include "primetab.h" |
| 43 | #endif |
| 44 | |
| 45 | /*----- Data structures ---------------------------------------------------*/ |
| 46 | |
| 47 | typedef struct pfilt { |
| 48 | mp *m; |
| 49 | smallprime r[NPRIME]; |
| 50 | } pfilt; |
| 51 | |
| 52 | /*----- Functions provided ------------------------------------------------*/ |
| 53 | |
| 54 | /* --- @pfilt_smallfactor@ --- * |
| 55 | * |
| 56 | * Arguments: @mp *m@ = integer to test |
| 57 | * |
| 58 | * Returns: One of the @PGEN@ result codes. |
| 59 | * |
| 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. |
| 63 | */ |
| 64 | |
| 65 | extern int pfilt_smallfactor(mp */*m*/); |
| 66 | |
| 67 | /* --- @pfilt_create@ --- * |
| 68 | * |
| 69 | * Arguments: @pfilt *p@ = pointer to prime filtering context |
| 70 | * @mp *m@ = pointer to initial number to test |
| 71 | * |
| 72 | * Returns: A @PGEN@ result code. |
| 73 | * |
| 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. |
| 78 | */ |
| 79 | |
| 80 | extern int pfilt_create(pfilt */*p*/, mp */*m*/); |
| 81 | |
| 82 | /* --- @pfilt_destroy@ --- * |
| 83 | * |
| 84 | * Arguments: @pfilt *p@ = pointer to prime filtering context |
| 85 | * |
| 86 | * Returns: --- |
| 87 | * |
| 88 | * Use: Discards a context and all the resources it holds. |
| 89 | */ |
| 90 | |
| 91 | extern void pfilt_destroy(pfilt */*p*/); |
| 92 | |
| 93 | /* --- @pfilt_step@ --- * |
| 94 | * |
| 95 | * Arguments: @pfilt *p@ = pointer to prime filtering context |
| 96 | * @mpw step@ = how much to step the number |
| 97 | * |
| 98 | * Returns: One of the @PGEN@ result codes. |
| 99 | * |
| 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. |
| 104 | */ |
| 105 | |
| 106 | extern int pfilt_step(pfilt */*p*/, mpw /*step*/); |
| 107 | |
| 108 | /* --- @pfilt_muladd@ --- * |
| 109 | * |
| 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 |
| 114 | * |
| 115 | * Returns: One of the @PGEN@ result codes. |
| 116 | * |
| 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. |
| 120 | * |
| 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. |
| 124 | */ |
| 125 | |
| 126 | extern int pfilt_muladd(pfilt */*p*/, const pfilt */*q*/, |
| 127 | mpw /*m*/, mpw /*a*/); |
| 128 | |
| 129 | /* --- @pfilt_jump@ --- * |
| 130 | * |
| 131 | * Arguments: @pfilt *p@ = pointer to prime filtering context |
| 132 | * @const pfilt *j@ = pointer to another filtering context |
| 133 | * |
| 134 | * Returns: One of the @PGEN@ result codes. |
| 135 | * |
| 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. |
| 140 | * |
| 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 |
| 144 | * computed. |
| 145 | */ |
| 146 | |
| 147 | extern int pfilt_jump(pfilt */*p*/, const pfilt */*j*/); |
| 148 | |
| 149 | /*----- That's all, folks -------------------------------------------------*/ |
| 150 | |
| 151 | #ifdef __cplusplus |
| 152 | } |
| 153 | #endif |
| 154 | |
| 155 | #endif |