.links: Drop obsolete `lib-config.in' file.
[u/mdw/catacomb] / pfilt.c
diff --git a/pfilt.c b/pfilt.c
index 4b58304..ae99a9b 100644 (file)
--- a/pfilt.c
+++ b/pfilt.c
@@ -1,13 +1,13 @@
 /* -*-c-*-
  *
- * $Id: pfilt.c,v 1.3 2000/08/15 21:44:27 mdw Exp $
+ * $Id: pfilt.c,v 1.6 2004/04/08 01:36:15 mdw Exp $
  *
  * Finding and testing prime numbers
  *
  * (c) 1999 Straylight/Edgeware
  */
 
-/*----- Licensing notice --------------------------------------------------* 
+/*----- Licensing notice --------------------------------------------------*
  *
  * This file is part of Catacomb.
  *
  * it under the terms of the GNU Library General Public License as
  * published by the Free Software Foundation; either version 2 of the
  * License, or (at your option) any later version.
- * 
+ *
  * Catacomb is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU Library General Public License for more details.
- * 
+ *
  * You should have received a copy of the GNU Library General Public
  * License along with Catacomb; if not, write to the Free
  * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  * MA 02111-1307, USA.
  */
 
-/*----- Revision history --------------------------------------------------* 
- *
- * $Log: pfilt.c,v $
- * Revision 1.3  2000/08/15 21:44:27  mdw
- * (pfilt_smallfactor): New function for doing trial division the hard
- * way.
- *
- * (pfilt_create): Use @mpx_udivn@ for computing residues, for improved
- * performance.
- *
- * Pull the `small prime' test into a separate function, and do it
- * properly.
- *
- * Revision 1.2  2000/06/17 11:54:27  mdw
- * Use new MP memory management functions.
- *
- * Revision 1.1  1999/12/22 15:49:39  mdw
- * Renamed from `pgen'.  Reworking for new prime-search system.
- *
- * Revision 1.3  1999/12/10 23:28:35  mdw
- * Track suggested destination changes.
- *
- * 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.
- *
- */
-
 /*----- Header files ------------------------------------------------------*/
 
 #include "mp.h"
@@ -64,7 +34,6 @@
 #include "pfilt.h"
 #include "pgen.h"
 #include "primetab.h"
-#include "primorial.h"
 
 /*----- Main code ---------------------------------------------------------*/
 
@@ -90,7 +59,9 @@ static int smallenough(mp *m)
     max = mp_sqr(max, max);
     max->a->n--; /* Permanent allocation */
   }
-  if (MP_CMP(m, <, max))
+  if (MP_CMP(m, <=, MP_ONE))
+    rc = PGEN_FAIL;
+  else if (MP_CMP(m, <, max))
     rc = PGEN_DONE;
   return (rc);
 }
@@ -111,7 +82,8 @@ int pfilt_smallfactor(mp *m)
   int rc = PGEN_TRY;
   int i;
   size_t sz = MP_LEN(m);
-  mpw *v = mpalloc(m->a, sz);
+  mparena *a = m->a ? m->a : MPARENA_GLOBAL;
+  mpw *v = mpalloc(a, sz);
 
   /* --- Fill in the residues --- */
 
@@ -121,6 +93,7 @@ int pfilt_smallfactor(mp *m)
        rc = PGEN_DONE;
       else
        rc = PGEN_FAIL;
+      break;
     }
   }
 
@@ -131,7 +104,7 @@ int pfilt_smallfactor(mp *m)
 
   /* --- Done --- */
 
-  mpfree(m->a, v);
+  mpfree(a, v);
   return (rc);
 }
 
@@ -153,7 +126,8 @@ int pfilt_create(pfilt *p, mp *m)
   int rc = PGEN_TRY;
   int i;
   size_t sz = MP_LEN(m);
-  mpw *v = mpalloc(m->a, sz);
+  mparena *a = m->a ? m->a : MPARENA_GLOBAL;
+  mpw *v = mpalloc(a, sz);
 
   /* --- Take a copy of the number --- */
 
@@ -179,7 +153,7 @@ int pfilt_create(pfilt *p, mp *m)
 
   /* --- Done --- */
 
-  mpfree(m->a, v);
+  mpfree(a, v);
   return (rc);
 }
 
@@ -280,7 +254,7 @@ int pfilt_muladd(pfilt *p, const pfilt *q, mpw m, mpw a)
   }
 
   /* --- Gallivant through the residue table --- */
-      
+
   for (i = 0; i < NPRIME; i++) {
     p->r[i] = (q->r[i] * m + a) % primetab[i];
     if (!p->r[i] && rc == PGEN_TRY) {