3 * Prime generation glue
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
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.
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.
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,
28 #ifndef CATACOMB_PGEN_H
29 #define CATACOMB_PGEN_H
35 /*----- Header files ------------------------------------------------------*/
37 #ifndef CATACOMB_GRAND_H
45 #ifndef CATACOMB_PFILT_H
49 #ifndef CATACOMB_RABIN_H
53 /*----- Event handling ----------------------------------------------------*
55 * Different programs and architectures will want to show progress of prime
56 * searches and similar processes in different ways. Of course, for simple
57 * searches, it's possible to use the @pfilt@ and @rabin@ functions and
58 * maintain control over the general control flow throughout the search.
60 * For more complex cases, this sort of control is undesirable. It's
61 * possible to specify an event handler which is informed in abstract about
62 * the search. The event handler can also request the search be aborted.
65 /* --- Event code constants --- *
67 * You're allowed to rely on the values of @PGEN_DONE@ and @PGEN_ABORT@.
71 PGEN_BEGIN = 1, /* Search for value begins */
72 PGEN_TRY, /* A new candidate has appeared */
73 PGEN_FAIL, /* The candidate failed the test */
74 PGEN_PASS, /* The candidate passed a test */
75 PGEN_DONE = 0, /* A good value has been found */
76 PGEN_ABORT = -1 /* The search has been aborted */
79 /* --- Event information --- *
81 * Note that the pseudorandom number generator supplied is not
82 * cryptographically strong.
85 typedef struct pgen_event {
86 const char *name; /* Which quantity is being found */
87 mp *m; /* Current value under test */
88 unsigned steps; /* Number of candidates left */
89 unsigned tests; /* Tests left before passing */
90 grand *r; /* Source of random numbers */
93 /*----- Prime search parameters -------------------------------------------*
95 * The prime search is parameterized in a large number of ways, although this
96 * isn't so much of a surprise given the different sorts of properties
97 * required from prime numbers in cryptographic applications.
99 * There are two main things which need to be configured: stepping, and
100 * testing. (Filtering is done as part of stepping.)
102 * The functions here provide a toolkit for constructing stepping and testing
103 * routines. In a lot of cases, the functions can be used directly; in
104 * others, simple bits of glue need be written.
106 * Two types of functions are defined: steppers and testers, but their
107 * interfaces are substantially similar. Each is given a request code, a
108 * context block and an event block. It is meant to update its context and
109 * the event block and return an event code.
111 * A call with a request of @PGEN_BEGIN@ asks the stepper or tester to
112 * initialize itself using the information in its event block and context. A
113 * return of @PGEN_FAIL@ reports an immediate failure; @PGEN_ABORT@ reports a
114 * fatal problem; @PGEN_DONE@ reports immediate success. @PGEN_TRY@ reports
115 * successful initialization and requests test iterations.
117 * A call to a stepper with a request of @PGEN_TRY@ asks it to step to the
118 * next possible candidate, replacing the value @m@ in the event block with
119 * the new candidate. A call to a tester with a request of @PGEN_TRY@
120 * runs one pass of the test. It should return @PGEN_FAIL@ to report a
121 * failure, @PGEN_PASS@ to report a success and request another iteration,
122 * @PGEN_DONE@ to report final acceptance and @PGEN_ABORT@ to terminate the
123 * search unsuccessfully. Note that even if the search is aborted, a
124 * shutdown request is still made.
126 * A call with a request of @PGEN_DONE@ closes down the stepper or tester.
127 * After a successful initialization (i.e., a return of something other than
128 * @PGEN_ABORT@), a shutdown call is guaranteed. The return code is ignored.
131 typedef int pgen_proc(int /*rq*/, pgen_event */*ev*/, void */*p*/);
133 /*----- Simple handler functions ------------------------------------------*/
135 /* --- @pgen_filter@ --- *
137 * A prime generation context contains the information required for the
138 * simple prime filter and tester presented here.
141 typedef struct pgen_filterctx {
142 unsigned step; /* Step size (set by client) */
143 pfilt f; /* The rapid prime filter */
146 extern pgen_proc pgen_filter;
148 /* --- @pgen_jump@ --- *
150 * Similar to the standard @pgen_filter@, but jumps in large steps rather
154 typedef struct pgen_jumpctx {
159 extern pgen_proc pgen_jump;
161 /* --- @pgen_test@ --- *
163 * Runs the Rabin-Miller primality test. The context block is simply a
167 extern pgen_proc pgen_test;
169 /* --- @pgen_bailliepswtest@ --- *
171 * Runs the Baillie--PSW primality test. The context block is ignored. The
172 * number of tests must be 2 (= @PGEN_BAILLIEPSWNTESTS@).
175 extern pgen_proc pgen_bailliepswtest;
176 #define PGEN_BAILLIEPSWNTESTS 2
178 /*----- Simultaneous primality checking -----------------------------------*/
180 typedef struct pgen_simulprime {
181 mp *mul, *add; /* Arguments from the client */
182 unsigned f; /* Flags, set by client, changed */
183 #define PGENF_KEEP 1u /* Keep this prime's value */
184 #define PGENF_JUMP 8u /* Use jump table, not stepping */
185 pfilt p; /* This prime's filter */
186 rabin r; /* Rabin testing context */
188 mpw step; /* The simple step to use */
189 pfilt *jump; /* The jump to move by */
190 mp *x; /* The result, if wanted */
194 typedef struct pgen_simulctx {
195 pgen_simulprime *v; /* Vector of related primes */
196 unsigned n; /* Size of the vector */
197 mp *step; /* Basic stepping value */
200 /* --- @pgen_simulstep@ --- *
202 * Step a collection of numbers simultaneously.
205 extern pgen_proc pgen_simulstep;
207 /* --- @pgen_simultest@ --- *
209 * Test a collection of numbers simultaneously.
212 extern pgen_proc pgen_simultest;
214 /* --- @pgen_simulbailliepswtest@ --- *
216 * Test a collection of numbers simultaneously using Baillie--PSW.
219 extern pgen_proc pgen_simulbailliepswtest;
221 /*----- Miscellaneous steppers and testers --------------------------------*/
223 typedef struct pgen_gcdstepctx {
224 pfilt p, jp; /* Prime filter and step filter */
225 mp *q, *jq; /* %$p - 1$%, and a step value*/
226 mp *r; /* Other argument for GCD */
227 mp *g; /* GCD output (must be inited) */
228 mp *max; /* Maximum permissible GCD */
231 /* --- @pgen_gcdstep@ --- *
233 * Steps @p@ and @q@, until @p@ has no small factors, and
234 * %$\gcd(p, r) \le max$%.
237 extern pgen_proc pgen_gcdstep;
239 /*----- Standard event handlers -------------------------------------------*/
241 /* --- @pgen_evspin@ --- *
243 * Displays a spinning baton to show progress.
246 extern pgen_proc pgen_evspin;
248 /* --- @pgen_ev@ --- *
250 * Traditional event handler, shows dots for each test.
253 extern pgen_proc pgen_ev;
255 /* --- @pgen_subev@ --- *
257 * Subsidiary event handler, mainly for Lim-Lee searches and so on.
260 extern pgen_proc pgen_subev;
262 /*----- The main driver ---------------------------------------------------*/
266 * Arguments: @const char *name@ = name of the value being searched for
267 * @mp *d@ = destination for resulting integer
268 * @mp *m@ = start value to pass to stepper
269 * @pgen_proc *event@ = event handler function
270 * @void *ectx@ = context argument for event andler
271 * @unsigned steps@ = number of steps to take in search
272 * @pgen_proc *step@ = stepper function to use
273 * @void *sctx@ = context argument for stepper
274 * @unsigned tests@ = number of tests to make
275 * @pgen_proc *test@ = tester function to use
276 * @void *tctx@ = context argument for tester
278 * Returns: The resulting value, or null.
280 * Use: A generalized prime-number search skeleton. Yes, that's a
281 * scary number of arguments.
284 extern mp *pgen(const char */*name*/, mp */*d*/, mp */*m*/,
285 pgen_proc */*event*/, void */*ectx*/,
286 unsigned /*steps*/, pgen_proc */*step*/, void */*sctx*/,
287 unsigned /*tests*/, pgen_proc */*test*/, void */*tctx*/);
289 /* --- @pgen_granfrob@ --- *
291 * Arguments: @mp *n@ = an integer to test
292 * @int a, b@ = coefficients; if @a@ is zero then choose
295 * Returns: One of the @PGEN_...@ codes.
297 * Use: Performs a quadratic version of Grantham's Frobenius
298 * primality test, which is a simple extension of the standard
301 * If %$a^2 - 4 b$% is a perfect square then the test can't
302 * work; this function returns @PGEN_ABORT@ under these
305 * If @a@ is zero on entry, then the function will choose
306 * suitable parameters deterministically -- i.e., it always
307 * chooses the same parameters for a given %$n$%.
310 extern int pgen_granfrob(mp */*n*/, int /*a*/, int /*b*/);
312 /* --- @pgen_primep@ --- *
314 * Arguments: @mp *p@ = a number to check
315 * @grand *gr@ = a random number source
317 * Returns: Nonzero if @p@ is really prime.
320 extern int pgen_primep(mp */*p*/, grand */*gr*/);
322 /*----- That's all, folks -------------------------------------------------*/