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