Signal a pgen abort if the jump and base share a common factor.
[u/mdw/catacomb] / pgen-safe.c
index a8c5ac7..b680ae4 100644 (file)
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: pgen-safe.c,v 1.2 2000/02/12 18:21:03 mdw Exp $
+ * $Id: pgen-safe.c,v 1.3 2000/06/17 11:52:36 mdw Exp $
  *
  * Safe prime generation
  *
@@ -30,6 +30,9 @@
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: pgen-safe.c,v $
+ * Revision 1.3  2000/06/17 11:52:36  mdw
+ * Signal a pgen abort if the jump and base share a common factor.
+ *
  * Revision 1.2  2000/02/12 18:21:03  mdw
  * Overhaul of key management (again).
  *
@@ -119,17 +122,27 @@ int pgen_safejump(int rq, pgen_event *ev, void *p)
      *
      * The jump in @j.q@ is congruent to 2 (mod 4); see @strongprime_setup@.
      * If @p@ is initially 1 (mod 4) then add @j.q@.  Then double @j.q@ to
-     * ensure that the step is 0 (mod 4).
+     * ensure that the step is 0 (mod 4).  Ensure that @jq@ and @q@ don't
+     * have any common factors.
      */
 
     case PGEN_BEGIN: {
       mp *p = ev->m;
       mp *q;
+      mp *g = MP_NEW;
       if ((p->v[0] & 3) != 3)
        p = mp_add(p, p, j->jq.m);
+      q = mp_lsr(MP_NEW, p, 1);
+      mp_gcd(&g, 0, 0, q, j->jq.m);
+      if (MP_CMP(g, >, MP_ONE)) {
+       ev->m = p;
+       mp_drop(q);
+       mp_drop(g);
+       return (PGEN_ABORT);
+      }
+      mp_drop(g);
       rc = pfilt_create(&j->p, p);
       pfilt_muladd(&j->jp, &j->jq, 2, 0);
-      q = mp_lsr(MP_NEW, p, 1);
       qrc = pfilt_create(&j->q, q);
       mp_drop(p);
       mp_drop(q);