Same file, completely different code. Main interface for new prime-
[u/mdw/catacomb] / pgen.h
diff --git a/pgen.h b/pgen.h
index 6d180c3..9ce4e86 100644 (file)
--- a/pgen.h
+++ b/pgen.h
@@ -1,8 +1,8 @@
 /* -*-c-*-
  *
- * $Id: pgen.h,v 1.3 1999/12/10 23:29:48 mdw Exp $
+ * $Id: pgen.h,v 1.4 1999/12/22 16:01:11 mdw Exp $
  *
- * Finding and testing prime numbers
+ * Prime generation glue
  *
  * (c) 1999 Straylight/Edgeware
  */
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: pgen.h,v $
- * Revision 1.3  1999/12/10 23:29:48  mdw
- * Change header file guard names.
- *
- * Revision 1.2  1999/11/20 22:23:05  mdw
- * Add multiply-and-add function for Diffie-Hellman safe prime generation.
- *
- * Revision 1.1  1999/11/19 13:17:57  mdw
- * Prime number generator and tester.
+ * Revision 1.4  1999/12/22 16:01:11  mdw
+ * Same file, completely different code.  Main interface for new prime-
+ * search system.
  *
  */
 
 
 /*----- Header files ------------------------------------------------------*/
 
+#ifndef CATACOMB_GRAND_H
+#  include "grand.h"
+#endif
+
 #ifndef CATACOMB_MP_H
 #  include "mp.h"
 #endif
 
-#ifndef CATACOMB_PTAB_H
-#  include "ptab.h"
+#ifndef CATACOMB_PFILT_H
+#  include "pfilt.h"
 #endif
 
-/*----- Constants ---------------------------------------------------------*/
-
-#define PGEN_COMPOSITE (-1)            /* Number is definitely composite */
-#define PGEN_MAYBE (0)                 /* Number may be prime */
-#define PGEN_PRIME (1)                 /* Number is definitely prime */
-
-/*----- Data structures ---------------------------------------------------*/
-
-typedef struct pgen {
-  mp *m;
-  unsigned char r[NPRIME];
-} pgen;
-
-/*----- Functions provided ------------------------------------------------*/
+#ifndef CATACOMB_RABIN_H
+#  include "rabin.h"
+#endif
 
-/* --- @pgen_create@ --- *
+/*----- Event handling ----------------------------------------------------*
  *
- * Arguments:  @pgen *p@ = pointer to prime generation context
- *             @mp *m@ = pointer to initial number to test
+ * Different programs and architectures will want to show progress of prime
+ * searches and similar processes in different ways.  Of course, for simple
+ * searches, it's possible to use the @pfilt@ and @rabin@ functions and
+ * maintain control over the general control flow throughout the search.
  *
- * Returns:    One of the @PGEN@ constants above.
+ * For more complex cases, this sort of control is undesirable.  It's
+ * possible to specify an event handler which is informed in abstract about
+ * the search.  The event handler can also request the search be aborted.
+ */
+
+/* --- Event code constants --- *
  *
- * Use:                Tests an initial number for primality by computing its
- *             residue modulo various small prime numbers.  This is fairly
- *             quick, but not particularly certain.  If a @PGEN_MAYBE@
- *             result is returned, perform Rabin-Miller tests to confirm.
+ * You're allowed to rely on the values of @PGEN_DONE@ and @PGEN_ABORT@.
  */
 
-extern int pgen_create(pgen */*p*/, mp */*m*/);
+enum {
+  PGEN_BEGIN = 1,                      /* Search for value begins */
+  PGEN_TRY,                            /* A new candidate has appeared */
+  PGEN_FAIL,                           /* The candidate failed the test */
+  PGEN_PASS,                           /* The candidate passed a test */
+  PGEN_DONE = 0,                       /* A good value has been found */
+  PGEN_ABORT = -1                      /* The search has been aborted */
+};
 
-/* --- @pgen_destroy@ --- *
+/* --- Event information --- *
  *
- * Arguments:  @pgen *p@ = pointer to prime generation context
- *
- * Returns:    ---
- *
- * Use:                Discards a context and all the resources it holds.
+ * Note that the pseudorandom number generator supplied is not
+ * cryptographically strong.
  */
 
-extern void pgen_destroy(pgen */*p*/);
+typedef struct pgen_event {
+  const char *name;                    /* Which quantity is being found */
+  mp *m;                               /* Current value under test */
+  unsigned steps;                      /* Number of candidates left */
+  unsigned tests;                      /* Tests left before passing */
+  grand *r;                            /* Source of random numbers */
+} pgen_event;
+
+/*----- Prime search parameters -------------------------------------------*
+ *
+ * The prime search is parameterized in a large number of ways, although this
+ * isn't so much of a surprise given the different sorts of properties
+ * required from prime numbers in cryptographic applications.
+ *
+ * There are two main things which need to be configured: stepping, and
+ * testing.  (Filtering is done as part of stepping.)
+ *
+ * The functions here provide a toolkit for constructing stepping and testing
+ * routines.  In a lot of cases, the functions can be used directly; in
+ * others, simple bits of glue need be written.
+ *
+ * Two types of functions are defined: steppers and testers, but their
+ * interfaces are substantially similar.  Each is given a request code, a
+ * context block and an event block.  It is meant to update its context and
+ * the event block and return an event code.
+ *
+ * A call with a request of @PGEN_BEGIN@ asks the stepper or tester to
+ * initialize itself using the information in its event block and context.  A
+ * return of @PGEN_FAIL@ reports an immediate failure; @PGEN_ABORT@ reports a
+ * fatal problem; @PGEN_DONE@ reports immediate success.  @PGEN_TRY@ reports
+ * successful initialization and requests test iterations.
+ *
+ * A call to a stepper with a request of @PGEN_TRY@ asks it to step to the
+ * next possible candidate, replacing the value @m@ in the event block with
+ * the new candidate.  A call to a tester with a request of @PGEN_TRY@
+ * runs one pass of the test.  It should return @PGEN_FAIL@ to report a
+ * failure, @PGEN_PASS@ to report a success and request another iteration,
+ * @PGEN_DONE@ to report final acceptance and @PGEN_ABORT@ to terminate the
+ * search unsuccessfully.  Note that even if the search is aborted, a
+ * shutdown request is still made.
+ *
+ * A call with a request of @PGEN_DONE@ closes down the stepper or tester.
+ * After a successful initialization (i.e., a return of something other than
+ * @PGEN_ABORT@), a shutdown call is guaranteed.  The return code is ignored.
+ */
 
-/* --- @pgen_step@ --- *
- *
- * Arguments:  @pgen *p@ = pointer to prime generation context
- *             @mpw step@ = how much to step the number
+typedef int pgen_proc(int /*rq*/, pgen_event */*ev*/, void */*p*/);
+
+/*----- Simple handler functions ------------------------------------------*/
+
+/* --- @pgen_filter@ --- *
  *
- * Returns:    One of the @PGEN@ constants above.
+ * A prime generation context contains the information required for the
+ * simple prime filter and tester presented here.
+ */
+
+typedef struct pgen_filterctx {
+  unsigned step;                       /* Step size (set by client) */
+  pfilt f;                             /* The rapid prime filter */
+} pgen_filterctx;
+
+extern int pgen_filter(int /*rq*/, pgen_event */*ev*/, void */*p*/);
+
+/* --- @pgen_jump@ --- *
  *
- * Use:                Steps a number by a small amount.  Stepping is much faster
- *             than initializing with a new number.  The test performed is
- *             the same simple one used by @ptab_create@, so @PGEN_MAYBE@
- *             results should be followed up by a Rabin-Miller test.
+ * Similar to the standard @pgen_filter@, but jumps in large steps rather
+ * than small ones.
  */
 
-extern int pgen_step(pgen */*p*/, mpw /*step*/);
+typedef struct pgen_jumpctx {
+  const pfilt *j;
+  pfilt f;
+} pgen_jumpctx;
+
+extern int pgen_jump(int /*rq*/, pgen_event */*ev*/, void */*p*/);
 
-/* --- @pgen_muladd@ --- *
+/* --- @pgen_test@ --- *
  *
- * Arguments:  @pgen *p@ = destination prime generation context
- *             @const pgen *q@ = source prime generation context
- *             @mpw m@ = number to multiply by
- *             @mpw a@ = number to add
+ * Runs the Rabin-Miller primality test.  The context block is simply a
+ * @rabin@ context.
+ */
+
+extern int pgen_test(int /*rq*/, pgen_event */*ev*/, void */*p*/);
+
+/*----- Safe prime functions ----------------------------------------------*/
+
+/* --- @pgen_safestep@ --- *
  *
- * Returns:    One of the @PGEN@ constants above.
+ * Steps two numbers, %$q$% and %$p = 2q + 1$%, such that neither has any
+ * small factors.  %$p$% is put in the event block.
+ */
+
+typedef struct pgen_safestepctx {
+  pfilt q, p;
+} pgen_safestepctx;
+
+extern int pgen_safestep(int /*rq*/, pgen_event */*ev*/, void */*p*/);
+
+/* --- @pgen_safetest@ --- *
  *
- * Use:                Multiplies the number in a prime generation context by a
- *             small value and then adds a small value.  The destination
- *             should either be uninitialized or the same as the source.
+ * Applies Rabin-Miller tests to %$p$% and %$(p - 1)/2$%.
+ */
+
+typedef struct pgen_safetestctx {
+  pgen_safestepctx c;
+  rabin q, p;
+} pgen_safetestctx;
+
+extern int pgen_safetest(int /*rq*/, pgen_event */*ev*/, void */*p*/);
+
+/*----- Standard event handlers -------------------------------------------*/
+
+/* --- @pgen_evspin@ --- *
  *
- *             Common things to do include multiplying by 2 and adding 0 to
- *             turn a prime into a jump for finding other primes with @q@ as
- *             a factor of @p - 1@, or multiplying by 2 and adding 1.
+ * Displays a spinning baton to show progress.
  */
 
-extern int pgen_muladd(pgen */*p*/, const pgen */*q*/, mpw /*m*/, mpw /*a*/);
+extern int pgen_evspin(int /*rq*/, pgen_event */*ev*/, void */*p*/);
 
-/* --- @pgen_jump@ --- *
+/* --- @pgen_ev@ --- *
  *
- * Arguments:  @pgen *p@ = pointer to prime generation context
- *             @pgen *j@ = pointer to another generation context
+ * Traditional event handler, shows dots for each test.
+ */
+
+extern int pgen_ev(int /*rq*/, pgen_event */*ev*/, void */*p*/);
+
+/*----- The main driver ---------------------------------------------------*/
+
+/* --- @pgen@ --- *
  *
- * Returns:    One of the @PGEN@ constants above.
+ * Arguments:  @const char *name@ = name of the value being searched for
+ *             @mp *d@ = destination for resulting integer
+ *             @mp *m@ = start value to pass to stepper
+ *             @pgen_proc *event@ = event handler function
+ *             @void *ectx@ = context argument for event andler
+ *             @unsigned steps@ = number of steps to take in search
+ *             @pgen_proc *step@ = stepper function to use
+ *             @void *sctx@ = context argument for stepper
+ *             @unsigned tests@ = number of tests to make
+ *             @pgen_proc *test@ = tester function to use
+ *             @void *tctx@ = context argument for tester
  *
- * Use:                Steps a number by a large amount.  Even so, jumping is much
- *             faster than initializing a new number.  The test peformed is
- *             the same simple one used by @ptab_create@, so @PGEN_MAYBE@
- *             results should be followed up by a Rabin-Miller test.
+ * Returns:    If successful, @PGEN_DONE@; otherwise @PGEN_ABORT@.
  *
- *             Note that the number stored in the @j@ context is probably
- *             better off being even than prime.  The important thing is
- *             that all of the residues for the number have already been
- *             computed.
+ * Use:                A generalized prime-number search skeleton.  Yes, that's a
+ *             scary number of arguments.
  */
 
-extern int pgen_jump(pgen */*p*/, pgen */*j*/);
+extern mp *pgen(const char */*name*/, mp */*d*/, mp */*m*/,
+               pgen_proc */*event*/, void */*ectx*/,
+               unsigned /*steps*/, pgen_proc */*step*/, void */*sctx*/,
+               unsigned /*tests*/, pgen_proc */*test*/, void */*tctx*/);
 
 /*----- That's all, folks -------------------------------------------------*/