Use @MP_EQ@ instead of @MP_CMP@.
[u/mdw/catacomb] / dsa-gen.c
index d26bfe4..1e7fdc7 100644 (file)
--- a/dsa-gen.c
+++ b/dsa-gen.c
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: dsa-gen.c,v 1.4 1999/12/22 15:52:44 mdw Exp $
+ * $Id: dsa-gen.c,v 1.8 2000/10/08 12:12:47 mdw Exp $
  *
  * Generate DSA shared parameters
  *
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: dsa-gen.c,v $
+ * Revision 1.8  2000/10/08 12:12:47  mdw
+ * Use @MP_EQ@ instead of @MP_CMP@.  Remove vestages of @primorial@.
+ *
+ * Revision 1.7  2000/08/15 21:45:05  mdw
+ * Use the new trial division equipment in pfilt.  This gives a 10%
+ * performance improvement in dsa-gen.t.
+ *
+ * Revision 1.6  2000/07/29 10:00:14  mdw
+ * Rename `dsa_seed' to `dsa_gen' for consistency with other parameter-
+ * generation interfaces.
+ *
+ * Revision 1.5  2000/02/12 18:21:02  mdw
+ * Overhaul of key management (again).
+ *
  * Revision 1.4  1999/12/22 15:52:44  mdw
  * Reworking for new prime-search system.
  *
@@ -56,7 +70,6 @@
 #include "mprand.h"
 #include "pgen.h"
 #include "prim.h"
-#include "primorial.h"
 #include "sha.h"
 
 /*----- The DSA stepper ---------------------------------------------------*/
@@ -93,15 +106,7 @@ static int next(pgen_event *ev, dsa_stepctx *d)
 
   /* --- Do the trial division --- */
 
-  {
-    mp *g = MP_NEW;
-    mp_gcd(&g, 0, 0, m, primorial);
-    if (MP_CMP(g, ==, MP_ONE) || MP_CMP(g, ==, m))
-      rc = PGEN_TRY;
-    else
-      rc = PGEN_FAIL;
-    mp_drop(g);
-  }
+  rc = pfilt_smallfactor(m);
 
   /* --- Return the result --- */
 
@@ -116,7 +121,6 @@ int dsa_step(int rq, pgen_event *ev, void *p)
 
   switch (rq) {
     case PGEN_BEGIN:
-      primorial_setup();
     case PGEN_TRY:
       return (next(ev, d));
     case PGEN_DONE:
@@ -127,7 +131,7 @@ int dsa_step(int rq, pgen_event *ev, void *p)
 
 /*----- Glue code ---------------------------------------------------------*/
 
-/* --- @dsa_seed@ --- *
+/* --- @dsa_gen@ --- *
  *
  * Arguments:  @dsa_param *dp@ = where to store parameters
  *             @unsigned ql@ = length of @q@ in bits
@@ -141,7 +145,12 @@ int dsa_step(int rq, pgen_event *ev, void *p)
  * Returns:    @PGEN_DONE@ if everything worked ok; @PGEN_ABORT@ otherwise.
  *
  * Use:                Generates the DSA shared parameters from a given seed value.
- *             This can take quite a long time.
+ *
+ *             The parameters are a prime %$q$%, relatively small, and a
+ *             large prime %$p = kq + 1$% for some %$k$%, together with a
+ *             generator %$g$% of the cyclic subgroup of order %$q$%.  These
+ *             are actually the same as the Diffie-Hellman parameter set,
+ *             but the generation algorithm is different.
  *
  *             The algorithm used is a compatible extension of the method
  *             described in the DSA standard, FIPS 186.  The standard
@@ -150,8 +159,8 @@ int dsa_step(int rq, pgen_event *ev, void *p)
  *             %$l$%.  Neither limitation applies to this implementation.
  */
 
-int dsa_seed(dsa_param *dp, unsigned ql, unsigned pl, unsigned steps,
-            const void *k, size_t sz, pgen_proc *event, void *ectx)
+int dsa_gen(dsa_param *dp, unsigned ql, unsigned pl, unsigned steps,
+           const void *k, size_t sz, pgen_proc *event, void *ectx)
 {
   dsa_stepctx s;
   prim_ctx p;
@@ -191,7 +200,7 @@ int dsa_seed(dsa_param *dp, unsigned ql, unsigned pl, unsigned steps,
   mpmont_create(&p.mm, dp->p);
   qc = MP_NEW; mp_div(&qc, 0, dp->p, dp->q);
   i = 0;
-  p.f = qc;
+  p.exp = qc;
   p.n = 0;
   if ((dp->g = pgen("g", MP_NEW, MP_NEW, event, ectx, 0, prim_step, &i,
                    1, prim_test, &p)) == 0)
@@ -231,9 +240,9 @@ static int verify(dstr *v)
   int ok = 1;
   int rc;
 
-  rc = dsa_seed(&dp, 160, l, 1, v[0].buf, v[0].len, pgen_evspin, 0);
-  if (rc || MP_CMP(q, !=, dp.q) ||
-      MP_CMP(p, !=, dp.p) || MP_CMP(g, !=, dp.g)) {
+  rc = dsa_gen(&dp, 160, l, 1, v[0].buf, v[0].len, pgen_evspin, 0);
+  if (rc || !MP_EQ(q, dp.q) ||
+      !MP_EQ(p, dp.p) || !MP_EQ(g, dp.g)) {
     fputs("\n*** gen failed", stderr);
     fputs("\nseed = ", stderr); type_hex.dump(&v[0], stderr);
     fprintf(stderr, "\nl = %u", l);
@@ -253,7 +262,7 @@ static int verify(dstr *v)
   if (!rc) {
     mp_drop(dp.q); mp_drop(dp.p); mp_drop(dp.g);
   }
-  assert(mparena_count(MPARENA_GLOBAL) == 1); /* Primorial! */
+  assert(mparena_count(MPARENA_GLOBAL) == 0);
   return (ok);
 }