Gather up another utility.
[u/mdw/catacomb] / pgen.h
1 /* -*-c-*-
2 *
3 * $Id: pgen.h,v 1.9 2004/04/08 01:36:15 mdw Exp $
4 *
5 * Prime generation glue
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 #ifndef CATACOMB_PGEN_H
31 #define CATACOMB_PGEN_H
32
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36
37 /*----- Header files ------------------------------------------------------*/
38
39 #ifndef CATACOMB_GRAND_H
40 # include "grand.h"
41 #endif
42
43 #ifndef CATACOMB_MP_H
44 # include "mp.h"
45 #endif
46
47 #ifndef CATACOMB_PFILT_H
48 # include "pfilt.h"
49 #endif
50
51 #ifndef CATACOMB_RABIN_H
52 # include "rabin.h"
53 #endif
54
55 /*----- Event handling ----------------------------------------------------*
56 *
57 * Different programs and architectures will want to show progress of prime
58 * searches and similar processes in different ways. Of course, for simple
59 * searches, it's possible to use the @pfilt@ and @rabin@ functions and
60 * maintain control over the general control flow throughout the search.
61 *
62 * For more complex cases, this sort of control is undesirable. It's
63 * possible to specify an event handler which is informed in abstract about
64 * the search. The event handler can also request the search be aborted.
65 */
66
67 /* --- Event code constants --- *
68 *
69 * You're allowed to rely on the values of @PGEN_DONE@ and @PGEN_ABORT@.
70 */
71
72 enum {
73 PGEN_BEGIN = 1, /* Search for value begins */
74 PGEN_TRY, /* A new candidate has appeared */
75 PGEN_FAIL, /* The candidate failed the test */
76 PGEN_PASS, /* The candidate passed a test */
77 PGEN_DONE = 0, /* A good value has been found */
78 PGEN_ABORT = -1 /* The search has been aborted */
79 };
80
81 /* --- Event information --- *
82 *
83 * Note that the pseudorandom number generator supplied is not
84 * cryptographically strong.
85 */
86
87 typedef struct pgen_event {
88 const char *name; /* Which quantity is being found */
89 mp *m; /* Current value under test */
90 unsigned steps; /* Number of candidates left */
91 unsigned tests; /* Tests left before passing */
92 grand *r; /* Source of random numbers */
93 } pgen_event;
94
95 /*----- Prime search parameters -------------------------------------------*
96 *
97 * The prime search is parameterized in a large number of ways, although this
98 * isn't so much of a surprise given the different sorts of properties
99 * required from prime numbers in cryptographic applications.
100 *
101 * There are two main things which need to be configured: stepping, and
102 * testing. (Filtering is done as part of stepping.)
103 *
104 * The functions here provide a toolkit for constructing stepping and testing
105 * routines. In a lot of cases, the functions can be used directly; in
106 * others, simple bits of glue need be written.
107 *
108 * Two types of functions are defined: steppers and testers, but their
109 * interfaces are substantially similar. Each is given a request code, a
110 * context block and an event block. It is meant to update its context and
111 * the event block and return an event code.
112 *
113 * A call with a request of @PGEN_BEGIN@ asks the stepper or tester to
114 * initialize itself using the information in its event block and context. A
115 * return of @PGEN_FAIL@ reports an immediate failure; @PGEN_ABORT@ reports a
116 * fatal problem; @PGEN_DONE@ reports immediate success. @PGEN_TRY@ reports
117 * successful initialization and requests test iterations.
118 *
119 * A call to a stepper with a request of @PGEN_TRY@ asks it to step to the
120 * next possible candidate, replacing the value @m@ in the event block with
121 * the new candidate. A call to a tester with a request of @PGEN_TRY@
122 * runs one pass of the test. It should return @PGEN_FAIL@ to report a
123 * failure, @PGEN_PASS@ to report a success and request another iteration,
124 * @PGEN_DONE@ to report final acceptance and @PGEN_ABORT@ to terminate the
125 * search unsuccessfully. Note that even if the search is aborted, a
126 * shutdown request is still made.
127 *
128 * A call with a request of @PGEN_DONE@ closes down the stepper or tester.
129 * After a successful initialization (i.e., a return of something other than
130 * @PGEN_ABORT@), a shutdown call is guaranteed. The return code is ignored.
131 */
132
133 typedef int pgen_proc(int /*rq*/, pgen_event */*ev*/, void */*p*/);
134
135 /*----- Simple handler functions ------------------------------------------*/
136
137 /* --- @pgen_filter@ --- *
138 *
139 * A prime generation context contains the information required for the
140 * simple prime filter and tester presented here.
141 */
142
143 typedef struct pgen_filterctx {
144 unsigned step; /* Step size (set by client) */
145 pfilt f; /* The rapid prime filter */
146 } pgen_filterctx;
147
148 extern int pgen_filter(int /*rq*/, pgen_event */*ev*/, void */*p*/);
149
150 /* --- @pgen_jump@ --- *
151 *
152 * Similar to the standard @pgen_filter@, but jumps in large steps rather
153 * than small ones.
154 */
155
156 typedef struct pgen_jumpctx {
157 const pfilt *j;
158 pfilt f;
159 } pgen_jumpctx;
160
161 extern int pgen_jump(int /*rq*/, pgen_event */*ev*/, void */*p*/);
162
163 /* --- @pgen_test@ --- *
164 *
165 * Runs the Rabin-Miller primality test. The context block is simply a
166 * @rabin@ context.
167 */
168
169 extern int pgen_test(int /*rq*/, pgen_event */*ev*/, void */*p*/);
170
171 /*----- Safe prime functions ----------------------------------------------*/
172
173 /* --- @pgen_safestep@ --- *
174 *
175 * Steps two numbers, %$q$% and %$p = 2q + 1$%, such that neither has any
176 * small factors. %$p$% is put in the event block.
177 */
178
179 typedef struct pgen_safestepctx {
180 pfilt q, p;
181 } pgen_safestepctx;
182
183 extern int pgen_safestep(int /*rq*/, pgen_event */*ev*/, void */*p*/);
184
185 /* --- @pgen_safejump@ --- *
186 *
187 * Jumps two numbers, %$q$% and %$p = 2q + 1$% such that neither has any
188 * small factors.
189 */
190
191 typedef struct pgen_safejumpctx {
192 pfilt q, jq;
193 pfilt p, jp;
194 } pgen_safejumpctx;
195
196 extern int pgen_safejump(int /*rq*/, pgen_event */*ev*/, void */*p*/);
197
198 /* --- @pgen_safetest@ --- *
199 *
200 * Applies Rabin-Miller tests to %$p$% and %$(p - 1)/2$%.
201 */
202
203 typedef struct pgen_safetestctx {
204 pgen_safestepctx c;
205 rabin q, p;
206 } pgen_safetestctx;
207
208 extern int pgen_safetest(int /*rq*/, pgen_event */*ev*/, void */*p*/);
209
210 /*----- Miscellaneous steppers and testers --------------------------------*/
211
212 typedef struct pgen_gcdstepctx {
213 pfilt p, jp; /* Prime filter and step filter */
214 mp *q, *jq; /* %$p - 1$%, and a step value*/
215 mp *r; /* Other argument for GCD */
216 mp *g; /* GCD output (must be inited) */
217 mp *max; /* Maximum permissible GCD */
218 } pgen_gcdstepctx;
219
220 /* --- @pgen_gcdstep@ --- *
221 *
222 * Steps @p@ and @q@, until @p@ has no small factors, and
223 * %$\gcd(p, r) \le max$%.
224 */
225
226 extern int pgen_gcdstep(int /*rq*/, pgen_event */*ev*/, void */*p*/);
227
228 /*----- Standard event handlers -------------------------------------------*/
229
230 /* --- @pgen_evspin@ --- *
231 *
232 * Displays a spinning baton to show progress.
233 */
234
235 extern int pgen_evspin(int /*rq*/, pgen_event */*ev*/, void */*p*/);
236
237 /* --- @pgen_ev@ --- *
238 *
239 * Traditional event handler, shows dots for each test.
240 */
241
242 extern int pgen_ev(int /*rq*/, pgen_event */*ev*/, void */*p*/);
243
244 /* --- @pgen_subev@ --- *
245 *
246 * Subsidiary event handler, mainly for Lim-Lee searches and so on.
247 */
248
249 extern int pgen_subev(int /*rq*/, pgen_event */*ev*/, void */*p*/);
250
251 /*----- The main driver ---------------------------------------------------*/
252
253 /* --- @pgen@ --- *
254 *
255 * Arguments: @const char *name@ = name of the value being searched for
256 * @mp *d@ = destination for resulting integer
257 * @mp *m@ = start value to pass to stepper
258 * @pgen_proc *event@ = event handler function
259 * @void *ectx@ = context argument for event andler
260 * @unsigned steps@ = number of steps to take in search
261 * @pgen_proc *step@ = stepper function to use
262 * @void *sctx@ = context argument for stepper
263 * @unsigned tests@ = number of tests to make
264 * @pgen_proc *test@ = tester function to use
265 * @void *tctx@ = context argument for tester
266 *
267 * Returns: If successful, @PGEN_DONE@; otherwise @PGEN_ABORT@.
268 *
269 * Use: A generalized prime-number search skeleton. Yes, that's a
270 * scary number of arguments.
271 */
272
273 extern mp *pgen(const char */*name*/, mp */*d*/, mp */*m*/,
274 pgen_proc */*event*/, void */*ectx*/,
275 unsigned /*steps*/, pgen_proc */*step*/, void */*sctx*/,
276 unsigned /*tests*/, pgen_proc */*test*/, void */*tctx*/);
277
278 /* --- @pgen_primep@ --- *
279 *
280 * Arguments: @mp *p@ = a number to check
281 * @grand *gr@ = a random number source
282 *
283 * Returns: Nonzero if @p@ is really prime.
284 */
285
286 extern int pgen_primep(mp */*p*/, grand */*gr*/);
287
288 /*----- That's all, folks -------------------------------------------------*/
289
290 #ifdef __cplusplus
291 }
292 #endif
293
294 #endif