9a8b0c8d |
1 | /* -*-c-*- |
2 | * |
64d27fe0 |
3 | * $Id: pfilt.h,v 1.2 2000/08/15 21:42:56 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 | |
30 | /*----- Revision history --------------------------------------------------* |
31 | * |
32 | * $Log: pfilt.h,v $ |
64d27fe0 |
33 | * Revision 1.2 2000/08/15 21:42:56 mdw |
34 | * Use the small primes type from `genprimes' output. New function for |
35 | * doing trial division the hard way. |
36 | * |
9a8b0c8d |
37 | * Revision 1.1 1999/12/22 15:49:39 mdw |
38 | * Renamed from `pgen'. Reworking for new prime-search system. |
39 | * |
40 | * Revision 1.3 1999/12/10 23:29:48 mdw |
41 | * Change header file guard names. |
42 | * |
43 | * Revision 1.2 1999/11/20 22:23:05 mdw |
44 | * Add multiply-and-add function for Diffie-Hellman safe prime generation. |
45 | * |
46 | * Revision 1.1 1999/11/19 13:17:57 mdw |
47 | * Prime number generator and tester. |
48 | * |
49 | */ |
50 | |
51 | #ifndef CATACOMB_PFILT_H |
52 | #define CATACOMB_PFILT_H |
53 | |
54 | #ifdef __cplusplus |
55 | extern "C" { |
56 | #endif |
57 | |
58 | /*----- Header files ------------------------------------------------------*/ |
59 | |
60 | #ifndef CATACOMB_MP_H |
61 | # include "mp.h" |
62 | #endif |
63 | |
64 | #ifndef CATACOMB_PTAB_H |
65 | # include "primetab.h" |
66 | #endif |
67 | |
68 | /*----- Data structures ---------------------------------------------------*/ |
69 | |
70 | typedef struct pfilt { |
71 | mp *m; |
64d27fe0 |
72 | smallprime r[NPRIME]; |
9a8b0c8d |
73 | } pfilt; |
74 | |
75 | /*----- Functions provided ------------------------------------------------*/ |
76 | |
64d27fe0 |
77 | /* --- @pfilt_smallfactor@ --- * |
78 | * |
79 | * Arguments: @mp *m@ = integer to test |
80 | * |
81 | * Returns: One of the @PGEN@ result codes. |
82 | * |
83 | * Use: Tests a number by dividing by a number of small primes. This |
84 | * is a useful first step if you're testing random primes; for |
85 | * sequential searches, @pfilt_create@ works better. |
86 | */ |
87 | |
88 | extern int pfilt_smallfactor(mp */*m*/); |
89 | |
9a8b0c8d |
90 | /* --- @pfilt_create@ --- * |
91 | * |
92 | * Arguments: @pfilt *p@ = pointer to prime filtering context |
93 | * @mp *m@ = pointer to initial number to test |
94 | * |
95 | * Returns: A @PGEN@ result code. |
96 | * |
97 | * Use: Tests an initial number for primality by computing its |
98 | * residue modulo various small prime numbers. This is fairly |
99 | * quick, but not particularly certain. If a @PGEN_TRY@ |
100 | * result is returned, perform Rabin-Miller tests to confirm. |
101 | */ |
102 | |
103 | extern int pfilt_create(pfilt */*p*/, mp */*m*/); |
104 | |
105 | /* --- @pfilt_destroy@ --- * |
106 | * |
107 | * Arguments: @pfilt *p@ = pointer to prime filtering context |
108 | * |
109 | * Returns: --- |
110 | * |
111 | * Use: Discards a context and all the resources it holds. |
112 | */ |
113 | |
114 | extern void pfilt_destroy(pfilt */*p*/); |
115 | |
116 | /* --- @pfilt_step@ --- * |
117 | * |
118 | * Arguments: @pfilt *p@ = pointer to prime filtering context |
119 | * @mpw step@ = how much to step the number |
120 | * |
121 | * Returns: One of the @PGEN@ result codes. |
122 | * |
123 | * Use: Steps a number by a small amount. Stepping is much faster |
124 | * than initializing with a new number. The test performed is |
125 | * the same simple one used by @primetab_create@, so @PGEN_TRY@ |
126 | * results should be followed up by a Rabin-Miller test. |
127 | */ |
128 | |
129 | extern int pfilt_step(pfilt */*p*/, mpw /*step*/); |
130 | |
131 | /* --- @pfilt_muladd@ --- * |
132 | * |
133 | * Arguments: @pfilt *p@ = destination prime filtering context |
134 | * @const pfilt *q@ = source prime filtering context |
135 | * @mpw m@ = number to multiply by |
136 | * @mpw a@ = number to add |
137 | * |
138 | * Returns: One of the @PGEN@ result codes. |
139 | * |
140 | * Use: Multiplies the number in a prime filtering context by a |
141 | * small value and then adds a small value. The destination |
142 | * should either be uninitialized or the same as the source. |
143 | * |
144 | * Common things to do include multiplying by 2 and adding 0 to |
145 | * turn a prime into a jump for finding other primes with @q@ as |
146 | * a factor of @p - 1@, or multiplying by 2 and adding 1. |
147 | */ |
148 | |
149 | extern int pfilt_muladd(pfilt */*p*/, const pfilt */*q*/, |
150 | mpw /*m*/, mpw /*a*/); |
151 | |
152 | /* --- @pfilt_jump@ --- * |
153 | * |
154 | * Arguments: @pfilt *p@ = pointer to prime filtering context |
155 | * @const pfilt *j@ = pointer to another filtering context |
156 | * |
157 | * Returns: One of the @PGEN@ result codes. |
158 | * |
159 | * Use: Steps a number by a large amount. Even so, jumping is much |
160 | * faster than initializing a new number. The test peformed is |
161 | * the same simple one used by @primetab_create@, so @PGEN_TRY@ |
162 | * results should be followed up by a Rabin-Miller test. |
163 | * |
164 | * Note that the number stored in the @j@ context is probably |
165 | * better off being even than prime. The important thing is |
166 | * that all of the residues for the number have already been |
167 | * computed. |
168 | */ |
169 | |
170 | extern int pfilt_jump(pfilt */*p*/, const pfilt */*j*/); |
171 | |
172 | /*----- That's all, folks -------------------------------------------------*/ |
173 | |
174 | #ifdef __cplusplus |
175 | } |
176 | #endif |
177 | |
178 | #endif |