bbs-gen: Don't use the safe stepper.
[u/mdw/catacomb] / pgen.h
CommitLineData
0f5ec153 1/* -*-c-*-
2 *
b7500b7b 3 * $Id$
0f5ec153 4 *
581c854e 5 * Prime generation glue
0f5ec153 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
b3f05084 30#ifndef CATACOMB_PGEN_H
31#define CATACOMB_PGEN_H
0f5ec153 32
33#ifdef __cplusplus
34 extern "C" {
35#endif
36
37/*----- Header files ------------------------------------------------------*/
38
581c854e 39#ifndef CATACOMB_GRAND_H
40# include "grand.h"
41#endif
42
b3f05084 43#ifndef CATACOMB_MP_H
0f5ec153 44# include "mp.h"
45#endif
46
581c854e 47#ifndef CATACOMB_PFILT_H
48# include "pfilt.h"
0f5ec153 49#endif
50
581c854e 51#ifndef CATACOMB_RABIN_H
52# include "rabin.h"
53#endif
0f5ec153 54
581c854e 55/*----- Event handling ----------------------------------------------------*
0f5ec153 56 *
581c854e 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.
0f5ec153 61 *
581c854e 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 --- *
0f5ec153 68 *
581c854e 69 * You're allowed to rely on the values of @PGEN_DONE@ and @PGEN_ABORT@.
0f5ec153 70 */
71
581c854e 72enum {
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};
0f5ec153 80
581c854e 81/* --- Event information --- *
0f5ec153 82 *
581c854e 83 * Note that the pseudorandom number generator supplied is not
84 * cryptographically strong.
0f5ec153 85 */
86
581c854e 87typedef 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 */
0f5ec153 132
581c854e 133typedef int pgen_proc(int /*rq*/, pgen_event */*ev*/, void */*p*/);
134
135/*----- Simple handler functions ------------------------------------------*/
136
137/* --- @pgen_filter@ --- *
0f5ec153 138 *
581c854e 139 * A prime generation context contains the information required for the
140 * simple prime filter and tester presented here.
141 */
142
143typedef struct pgen_filterctx {
144 unsigned step; /* Step size (set by client) */
145 pfilt f; /* The rapid prime filter */
146} pgen_filterctx;
147
ab6ce636 148extern pgen_proc pgen_filter;
581c854e 149
150/* --- @pgen_jump@ --- *
0f5ec153 151 *
581c854e 152 * Similar to the standard @pgen_filter@, but jumps in large steps rather
153 * than small ones.
0f5ec153 154 */
155
581c854e 156typedef struct pgen_jumpctx {
157 const pfilt *j;
158 pfilt f;
159} pgen_jumpctx;
160
ab6ce636 161extern pgen_proc pgen_jump;
0f5ec153 162
581c854e 163/* --- @pgen_test@ --- *
bd98b2df 164 *
581c854e 165 * Runs the Rabin-Miller primality test. The context block is simply a
166 * @rabin@ context.
167 */
168
ab6ce636 169extern pgen_proc pgen_test;
581c854e 170
29e444ad
MW
171/*----- Simultaneous primality checking -----------------------------------*/
172
173typedef struct pgen_simulprime {
174 mp *mul, *add; /* Arguments from the client */
175 unsigned f; /* Flags, set by client, changed */
176#define PGENF_KEEP 1u /* Keep this prime's value */
177#define PGENF_JUMP 8u /* Use jump table, not stepping */
178 pfilt p; /* This prime's filter */
179 rabin r; /* Rabin testing context */
180 union {
181 mpw step; /* The simple step to use */
182 pfilt *jump; /* The jump to move by */
183 mp *x; /* The result, if wanted */
184 } u;
185} pgen_simulprime;
186
187typedef struct pgen_simulctx {
188 pgen_simulprime *v; /* Vector of related primes */
189 unsigned n; /* Size of the vector */
190 mp *step; /* Basic stepping value */
191} pgen_simulctx;
192
193/* --- @pgen_simulstep@ --- *
194 *
195 * Step a collection of numbers simultaneously.
196 */
197
198extern pgen_proc pgen_simulstep;
199
200/* --- @pgen_simultest@ --- *
201 *
202 * Test a collection of numbers simultaneously.
203 */
204
205extern pgen_proc pgen_simultest;
206
581c854e 207/*----- Safe prime functions ----------------------------------------------*/
208
209/* --- @pgen_safestep@ --- *
bd98b2df 210 *
581c854e 211 * Steps two numbers, %$q$% and %$p = 2q + 1$%, such that neither has any
212 * small factors. %$p$% is put in the event block.
213 */
214
215typedef struct pgen_safestepctx {
216 pfilt q, p;
217} pgen_safestepctx;
218
ab6ce636 219extern pgen_proc pgen_safestep;
581c854e 220
052b36d0 221/* --- @pgen_safejump@ --- *
222 *
223 * Jumps two numbers, %$q$% and %$p = 2q + 1$% such that neither has any
224 * small factors.
225 */
226
227typedef struct pgen_safejumpctx {
228 pfilt q, jq;
229 pfilt p, jp;
230} pgen_safejumpctx;
231
ab6ce636 232extern pgen_proc pgen_safejump;
052b36d0 233
581c854e 234/* --- @pgen_safetest@ --- *
bd98b2df 235 *
581c854e 236 * Applies Rabin-Miller tests to %$p$% and %$(p - 1)/2$%.
237 */
238
239typedef struct pgen_safetestctx {
240 pgen_safestepctx c;
241 rabin q, p;
242} pgen_safetestctx;
243
ab6ce636 244extern pgen_proc pgen_safetest;
581c854e 245
f39a39c9 246/*----- Miscellaneous steppers and testers --------------------------------*/
247
248typedef struct pgen_gcdstepctx {
249 pfilt p, jp; /* Prime filter and step filter */
250 mp *q, *jq; /* %$p - 1$%, and a step value*/
251 mp *r; /* Other argument for GCD */
252 mp *g; /* GCD output (must be inited) */
253 mp *max; /* Maximum permissible GCD */
254} pgen_gcdstepctx;
255
256/* --- @pgen_gcdstep@ --- *
257 *
258 * Steps @p@ and @q@, until @p@ has no small factors, and
259 * %$\gcd(p, r) \le max$%.
260 */
261
ab6ce636 262extern pgen_proc pgen_gcdstep;
f39a39c9 263
581c854e 264/*----- Standard event handlers -------------------------------------------*/
265
266/* --- @pgen_evspin@ --- *
bd98b2df 267 *
581c854e 268 * Displays a spinning baton to show progress.
bd98b2df 269 */
270
ab6ce636 271extern pgen_proc pgen_evspin;
bd98b2df 272
581c854e 273/* --- @pgen_ev@ --- *
0f5ec153 274 *
581c854e 275 * Traditional event handler, shows dots for each test.
276 */
277
ab6ce636 278extern pgen_proc pgen_ev;
581c854e 279
df0292de 280/* --- @pgen_subev@ --- *
281 *
282 * Subsidiary event handler, mainly for Lim-Lee searches and so on.
283 */
284
ab6ce636 285extern pgen_proc pgen_subev;
df0292de 286
581c854e 287/*----- The main driver ---------------------------------------------------*/
288
289/* --- @pgen@ --- *
0f5ec153 290 *
581c854e 291 * Arguments: @const char *name@ = name of the value being searched for
292 * @mp *d@ = destination for resulting integer
293 * @mp *m@ = start value to pass to stepper
294 * @pgen_proc *event@ = event handler function
295 * @void *ectx@ = context argument for event andler
296 * @unsigned steps@ = number of steps to take in search
297 * @pgen_proc *step@ = stepper function to use
298 * @void *sctx@ = context argument for stepper
299 * @unsigned tests@ = number of tests to make
300 * @pgen_proc *test@ = tester function to use
301 * @void *tctx@ = context argument for tester
0f5ec153 302 *
b7500b7b 303 * Returns: The resulting value, or null.
0f5ec153 304 *
581c854e 305 * Use: A generalized prime-number search skeleton. Yes, that's a
306 * scary number of arguments.
0f5ec153 307 */
308
581c854e 309extern mp *pgen(const char */*name*/, mp */*d*/, mp */*m*/,
310 pgen_proc */*event*/, void */*ectx*/,
311 unsigned /*steps*/, pgen_proc */*step*/, void */*sctx*/,
312 unsigned /*tests*/, pgen_proc */*test*/, void */*tctx*/);
0f5ec153 313
34e4f738 314/* --- @pgen_primep@ --- *
315 *
316 * Arguments: @mp *p@ = a number to check
317 * @grand *gr@ = a random number source
318 *
319 * Returns: Nonzero if @p@ is really prime.
320 */
321
322extern int pgen_primep(mp */*p*/, grand */*gr*/);
323
0f5ec153 324/*----- That's all, folks -------------------------------------------------*/
325
326#ifdef __cplusplus
327 }
328#endif
329
330#endif