/* -*-c-*-
*
- * $Id: pfilt.c,v 1.1 1999/12/22 15:49:39 mdw Exp $
+ * $Id: pfilt.c,v 1.6 2004/04/08 01:36:15 mdw Exp $
*
* Finding and testing prime numbers
*
* MA 02111-1307, USA.
*/
-/*----- Revision history --------------------------------------------------*
+/*----- Header files ------------------------------------------------------*/
+
+#include "mp.h"
+#include "mpint.h"
+#include "pfilt.h"
+#include "pgen.h"
+#include "primetab.h"
+
+/*----- Main code ---------------------------------------------------------*/
+
+/* --- @smallenough@ --- *
+ *
+ * Arguments: @mp *m@ = integer to test
*
- * $Log: pfilt.c,v $
- * Revision 1.1 1999/12/22 15:49:39 mdw
- * Renamed from `pgen'. Reworking for new prime-search system.
+ * Returns: One of the @PGEN@ result codes.
*
- * Revision 1.3 1999/12/10 23:28:35 mdw
- * Track suggested destination changes.
+ * Use: Assuming that @m@ has been tested by trial division on every
+ * prime in the small-primes array, this function will return
+ * @PGEN_DONE@ if the number is less than the square of the
+ * largest small prime.
+ */
+
+static int smallenough(mp *m)
+{
+ static mp *max = 0;
+ int rc = PGEN_TRY;
+
+ if (!max) {
+ max = mp_fromuint(MP_NEW, MAXPRIME);
+ max = mp_sqr(max, max);
+ max->a->n--; /* Permanent allocation */
+ }
+ if (MP_CMP(m, <, max))
+ rc = PGEN_DONE;
+ return (rc);
+}
+
+/* --- @pfilt_smallfactor@ --- *
*
- * Revision 1.2 1999/11/20 22:23:05 mdw
- * Add multiply-and-add function for Diffie-Hellman safe prime generation.
+ * Arguments: @mp *m@ = integer to test
*
- * Revision 1.1 1999/11/19 13:17:57 mdw
- * Prime number generator and tester.
+ * Returns: One of the @PGEN@ result codes.
*
+ * Use: Tests a number by dividing by a number of small primes. This
+ * is a useful first step if you're testing random primes; for
+ * sequential searches, @pfilt_create@ works better.
*/
-/*----- Header files ------------------------------------------------------*/
+int pfilt_smallfactor(mp *m)
+{
+ int rc = PGEN_TRY;
+ int i;
+ size_t sz = MP_LEN(m);
+ mparena *a = m->a ? m->a : MPARENA_GLOBAL;
+ mpw *v = mpalloc(a, sz);
-#include "mp.h"
-#include "mpmont.h"
-#include "pfilt.h"
-#include "pgen.h"
-#include "primetab.h"
+ /* --- Fill in the residues --- */
-/*----- Main code ---------------------------------------------------------*/
+ for (i = 0; i < NPRIME; i++) {
+ if (!mpx_udivn(v, v + sz, m->v, m->vl, primetab[i])) {
+ if (MP_LEN(m) == 1 && m->v[0] == primetab[i])
+ rc = PGEN_DONE;
+ else
+ rc = PGEN_FAIL;
+ }
+ }
+
+ /* --- Check for small primes --- */
+
+ if (rc == PGEN_TRY)
+ rc = smallenough(m);
+
+ /* --- Done --- */
+
+ mpfree(a, v);
+ return (rc);
+}
/* --- @pfilt_create@ --- *
*
{
int rc = PGEN_TRY;
int i;
- mp *r = MP_NEW;
- mpw qw;
- mp q;
+ size_t sz = MP_LEN(m);
+ mparena *a = m->a ? m->a : MPARENA_GLOBAL;
+ mpw *v = mpalloc(a, sz);
/* --- Take a copy of the number --- */
/* --- Fill in the residues --- */
- mp_build(&q, &qw, &qw + 1);
for (i = 0; i < NPRIME; i++) {
- qw = primetab[i];
- mp_div(0, &r, m, &q);
- p->r[i] = r->v[0];
+ p->r[i] = mpx_udivn(v, v + sz, m->v, m->vl, primetab[i]);
if (!p->r[i] && rc == PGEN_TRY) {
if (MP_LEN(m) == 1 && m->v[0] == primetab[i])
rc = PGEN_DONE;
}
}
+ /* --- Check for small primes --- */
+
+ if (rc == PGEN_TRY)
+ rc = smallenough(m);
+
/* --- Done --- */
- mp_drop(r);
+ mpfree(a, v);
return (rc);
}
}
}
- /* --- Small numbers must be prime --- */
+ /* --- Check for small primes --- */
- if (rc == PGEN_TRY && MP_LEN(p->m) == 1 &&
- p->m->v[0] < MAXPRIME * MAXPRIME)
- rc = PGEN_DONE;
+ if (rc == PGEN_TRY)
+ rc = smallenough(p->m);
/* --- Done --- */
/* --- Multiply the big number --- */
{
- mp *d = mp_create(MP_LEN(q->m) + 2);
+ mp *d = mp_new(MP_LEN(q->m) + 2, q->m->f);
mpx_umuln(d->v, d->vl, q->m->v, q->m->vl, m);
mpx_uaddn(d->v, d->vl, a);
- d->f = q->m->f;
if (p == q)
mp_drop(p->m);
mp_shrink(d);
}
}
- /* --- Small numbers must be prime --- */
+ /* --- Check for small primes --- */
- if (rc == PGEN_TRY && MP_LEN(p->m) == 1 &&
- p->m->v[0] < MAXPRIME * MAXPRIME)
- rc = PGEN_DONE;
+ if (rc == PGEN_TRY)
+ rc = smallenough(p->m);
/* --- Finished --- */
}
}
- /* --- Small numbers must be prime --- */
+ /* --- Check for small primes --- */
- if (rc == PGEN_TRY && MP_LEN(p->m) == 1 &&
- p->m->v[0] < MAXPRIME * MAXPRIME)
- rc = PGEN_DONE;
+ if (rc == PGEN_TRY)
+ rc = smallenough(p->m);
/* --- Done --- */