From 9a8b0c8d33c593353185871b581bc8f5be8600fb Mon Sep 17 00:00:00 2001 From: mdw Date: Wed, 22 Dec 1999 15:49:39 +0000 Subject: [PATCH] Renamed from `pgen'. Reworking for new prime-search system. --- pfilt.c | 277 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ pfilt.h | 161 +++++++++++++++++++++++++++++++++++++ 2 files changed, 438 insertions(+) create mode 100644 pfilt.c create mode 100644 pfilt.h diff --git a/pfilt.c b/pfilt.c new file mode 100644 index 0000000..0eb6054 --- /dev/null +++ b/pfilt.c @@ -0,0 +1,277 @@ +/* -*-c-*- + * + * $Id: pfilt.c,v 1.1 1999/12/22 15:49:39 mdw Exp $ + * + * Finding and testing prime numbers + * + * (c) 1999 Straylight/Edgeware + */ + +/*----- Licensing notice --------------------------------------------------* + * + * This file is part of Catacomb. + * + * Catacomb is free software; you can redistribute it and/or modify + * 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.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" +#include "mpmont.h" +#include "pfilt.h" +#include "pgen.h" +#include "primetab.h" + +/*----- Main code ---------------------------------------------------------*/ + +/* --- @pfilt_create@ --- * + * + * Arguments: @pfilt *p@ = pointer to prime filtering context + * @mp *m@ = pointer to initial number to test + * + * Returns: One of the @PGEN@ result codes. + * + * 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_TRY@ + * result is returned, perform Rabin-Miller tests to confirm. + */ + +int pfilt_create(pfilt *p, mp *m) +{ + int rc = PGEN_TRY; + int i; + mp *r = MP_NEW; + mpw qw; + mp q; + + /* --- Take a copy of the number --- */ + + mp_shrink(m); + p->m = MP_COPY(m); + + /* --- 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]; + if (!p->r[i] && rc == PGEN_TRY) { + if (MP_LEN(m) == 1 && m->v[0] == primetab[i]) + rc = PGEN_DONE; + else + rc = PGEN_FAIL; + } + } + + /* --- Done --- */ + + mp_drop(r); + return (rc); +} + +/* --- @pfilt_destroy@ --- * + * + * Arguments: @pfilt *p@ = pointer to prime filtering context + * + * Returns: --- + * + * Use: Discards a context and all the resources it holds. + */ + +void pfilt_destroy(pfilt *p) +{ + mp_drop(p->m); +} + +/* --- @pfilt_step@ --- * + * + * Arguments: @pfilt *p@ = pointer to prime filtering context + * @mpw step@ = how much to step the number + * + * Returns: One of the @PGEN@ result codes. + * + * 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 @primetab_create@, so @PGEN_TRY@ + * results should be followed up by a Rabin-Miller test. + */ + +int pfilt_step(pfilt *p, mpw step) +{ + int rc = PGEN_TRY; + int i; + + /* --- Add the step on to the number --- */ + + p->m = mp_split(p->m); + mp_ensure(p->m, MP_LEN(p->m) + 1); + mpx_uaddn(p->m->v, p->m->vl, step); + mp_shrink(p->m); + + /* --- Update the residue table --- */ + + for (i = 0; i < NPRIME; i++) { + p->r[i] = (p->r[i] + step) % primetab[i]; + if (!p->r[i] && rc == PGEN_TRY) { + if (MP_LEN(p->m) == 1 && p->m->v[0] == primetab[i]) + rc = PGEN_DONE; + else + rc = PGEN_FAIL; + } + } + + /* --- Small numbers must be prime --- */ + + if (rc == PGEN_TRY && MP_LEN(p->m) == 1 && + p->m->v[0] < MAXPRIME * MAXPRIME) + rc = PGEN_DONE; + + /* --- Done --- */ + + return (rc); +} + +/* --- @pfilt_muladd@ --- * + * + * Arguments: @pfilt *p@ = destination prime filtering context + * @const pfilt *q@ = source prime filtering context + * @mpw m@ = number to multiply by + * @mpw a@ = number to add + * + * Returns: One of the @PGEN@ result codes. + * + * Use: Multiplies the number in a prime filtering context by a + * small value and then adds a small value. The destination + * should either be uninitialized or the same as the source. + * + * 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. + */ + +int pfilt_muladd(pfilt *p, const pfilt *q, mpw m, mpw a) +{ + int rc = PGEN_TRY; + int i; + + /* --- Multiply the big number --- */ + + { + mp *d = mp_create(MP_LEN(q->m) + 2); + 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); + p->m = d; + } + + /* --- 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) { + if (MP_LEN(p->m) == 1 && p->m->v[0] == primetab[i]) + rc = PGEN_DONE; + else + rc = PGEN_FAIL; + } + } + + /* --- Small numbers must be prime --- */ + + if (rc == PGEN_TRY && MP_LEN(p->m) == 1 && + p->m->v[0] < MAXPRIME * MAXPRIME) + rc = PGEN_DONE; + + /* --- Finished --- */ + + return (rc); +} + +/* --- @pfilt_jump@ --- * + * + * Arguments: @pfilt *p@ = pointer to prime filtering context + * @const pfilt *j@ = pointer to another filtering context + * + * Returns: One of the @PGEN@ result codes. + * + * 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 @primetab_create@, so @PGEN_TRY@ + * results should be followed up by a Rabin-Miller test. + * + * 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. + */ + +int pfilt_jump(pfilt *p, const pfilt *j) +{ + int rc = PGEN_TRY; + int i; + + /* --- Add the step on --- */ + + p->m = mp_add(p->m, p->m, j->m); + + /* --- Update the residue table --- */ + + for (i = 0; i < NPRIME; i++) { + p->r[i] = p->r[i] + j->r[i]; + if (p->r[i] > primetab[i]) + p->r[i] -= primetab[i]; + if (!p->r[i] && rc == PGEN_TRY) { + if (MP_LEN(p->m) == 1 && p->m->v[0] == primetab[i]) + rc = PGEN_DONE; + else + rc = PGEN_FAIL; + } + } + + /* --- Small numbers must be prime --- */ + + if (rc == PGEN_TRY && MP_LEN(p->m) == 1 && + p->m->v[0] < MAXPRIME * MAXPRIME) + rc = PGEN_DONE; + + /* --- Done --- */ + + return (rc); +} + +/*----- That's all, folks -------------------------------------------------*/ diff --git a/pfilt.h b/pfilt.h new file mode 100644 index 0000000..e3461d2 --- /dev/null +++ b/pfilt.h @@ -0,0 +1,161 @@ +/* -*-c-*- + * + * $Id: pfilt.h,v 1.1 1999/12/22 15:49:39 mdw Exp $ + * + * Finding and testing prime numbers + * + * (c) 1999 Straylight/Edgeware + */ + +/*----- Licensing notice --------------------------------------------------* + * + * This file is part of Catacomb. + * + * Catacomb is free software; you can redistribute it and/or modify + * 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.h,v $ + * 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: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. + * + */ + +#ifndef CATACOMB_PFILT_H +#define CATACOMB_PFILT_H + +#ifdef __cplusplus + extern "C" { +#endif + +/*----- Header files ------------------------------------------------------*/ + +#ifndef CATACOMB_MP_H +# include "mp.h" +#endif + +#ifndef CATACOMB_PTAB_H +# include "primetab.h" +#endif + +/*----- Data structures ---------------------------------------------------*/ + +typedef struct pfilt { + mp *m; + unsigned char r[NPRIME]; +} pfilt; + +/*----- Functions provided ------------------------------------------------*/ + +/* --- @pfilt_create@ --- * + * + * Arguments: @pfilt *p@ = pointer to prime filtering context + * @mp *m@ = pointer to initial number to test + * + * Returns: A @PGEN@ result code. + * + * 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_TRY@ + * result is returned, perform Rabin-Miller tests to confirm. + */ + +extern int pfilt_create(pfilt */*p*/, mp */*m*/); + +/* --- @pfilt_destroy@ --- * + * + * Arguments: @pfilt *p@ = pointer to prime filtering context + * + * Returns: --- + * + * Use: Discards a context and all the resources it holds. + */ + +extern void pfilt_destroy(pfilt */*p*/); + +/* --- @pfilt_step@ --- * + * + * Arguments: @pfilt *p@ = pointer to prime filtering context + * @mpw step@ = how much to step the number + * + * Returns: One of the @PGEN@ result codes. + * + * 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 @primetab_create@, so @PGEN_TRY@ + * results should be followed up by a Rabin-Miller test. + */ + +extern int pfilt_step(pfilt */*p*/, mpw /*step*/); + +/* --- @pfilt_muladd@ --- * + * + * Arguments: @pfilt *p@ = destination prime filtering context + * @const pfilt *q@ = source prime filtering context + * @mpw m@ = number to multiply by + * @mpw a@ = number to add + * + * Returns: One of the @PGEN@ result codes. + * + * Use: Multiplies the number in a prime filtering context by a + * small value and then adds a small value. The destination + * should either be uninitialized or the same as the source. + * + * 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. + */ + +extern int pfilt_muladd(pfilt */*p*/, const pfilt */*q*/, + mpw /*m*/, mpw /*a*/); + +/* --- @pfilt_jump@ --- * + * + * Arguments: @pfilt *p@ = pointer to prime filtering context + * @const pfilt *j@ = pointer to another filtering context + * + * Returns: One of the @PGEN@ result codes. + * + * 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 @primetab_create@, so @PGEN_TRY@ + * results should be followed up by a Rabin-Miller test. + * + * 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. + */ + +extern int pfilt_jump(pfilt */*p*/, const pfilt */*j*/); + +/*----- That's all, folks -------------------------------------------------*/ + +#ifdef __cplusplus + } +#endif + +#endif -- 2.11.0