| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: pgen.h,v 1.3 1999/12/10 23:29:48 mdw Exp $ |
| 4 | * |
| 5 | * Finding and testing prime numbers |
| 6 | * |
| 7 | * (c) 1999 Straylight/Edgeware |
| 8 | */ |
| 9 | |
| 10 | /*----- Licensing notice --------------------------------------------------* |
| 11 | * |
| 12 | * This file is part of Catacomb. |
| 13 | * |
| 14 | * Catacomb is free software; you can redistribute it and/or modify |
| 15 | * it under the terms of the GNU Library General Public License as |
| 16 | * published by the Free Software Foundation; either version 2 of the |
| 17 | * License, or (at your option) any later version. |
| 18 | * |
| 19 | * Catacomb is distributed in the hope that it will be useful, |
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | * GNU Library General Public License for more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU Library General Public |
| 25 | * License along with Catacomb; if not, write to the Free |
| 26 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
| 27 | * MA 02111-1307, USA. |
| 28 | */ |
| 29 | |
| 30 | /*----- Revision history --------------------------------------------------* |
| 31 | * |
| 32 | * $Log: pgen.h,v $ |
| 33 | * Revision 1.3 1999/12/10 23:29:48 mdw |
| 34 | * Change header file guard names. |
| 35 | * |
| 36 | * Revision 1.2 1999/11/20 22:23:05 mdw |
| 37 | * Add multiply-and-add function for Diffie-Hellman safe prime generation. |
| 38 | * |
| 39 | * Revision 1.1 1999/11/19 13:17:57 mdw |
| 40 | * Prime number generator and tester. |
| 41 | * |
| 42 | */ |
| 43 | |
| 44 | #ifndef CATACOMB_PGEN_H |
| 45 | #define CATACOMB_PGEN_H |
| 46 | |
| 47 | #ifdef __cplusplus |
| 48 | extern "C" { |
| 49 | #endif |
| 50 | |
| 51 | /*----- Header files ------------------------------------------------------*/ |
| 52 | |
| 53 | #ifndef CATACOMB_MP_H |
| 54 | # include "mp.h" |
| 55 | #endif |
| 56 | |
| 57 | #ifndef CATACOMB_PTAB_H |
| 58 | # include "ptab.h" |
| 59 | #endif |
| 60 | |
| 61 | /*----- Constants ---------------------------------------------------------*/ |
| 62 | |
| 63 | #define PGEN_COMPOSITE (-1) /* Number is definitely composite */ |
| 64 | #define PGEN_MAYBE (0) /* Number may be prime */ |
| 65 | #define PGEN_PRIME (1) /* Number is definitely prime */ |
| 66 | |
| 67 | /*----- Data structures ---------------------------------------------------*/ |
| 68 | |
| 69 | typedef struct pgen { |
| 70 | mp *m; |
| 71 | unsigned char r[NPRIME]; |
| 72 | } pgen; |
| 73 | |
| 74 | /*----- Functions provided ------------------------------------------------*/ |
| 75 | |
| 76 | /* --- @pgen_create@ --- * |
| 77 | * |
| 78 | * Arguments: @pgen *p@ = pointer to prime generation context |
| 79 | * @mp *m@ = pointer to initial number to test |
| 80 | * |
| 81 | * Returns: One of the @PGEN@ constants above. |
| 82 | * |
| 83 | * Use: Tests an initial number for primality by computing its |
| 84 | * residue modulo various small prime numbers. This is fairly |
| 85 | * quick, but not particularly certain. If a @PGEN_MAYBE@ |
| 86 | * result is returned, perform Rabin-Miller tests to confirm. |
| 87 | */ |
| 88 | |
| 89 | extern int pgen_create(pgen */*p*/, mp */*m*/); |
| 90 | |
| 91 | /* --- @pgen_destroy@ --- * |
| 92 | * |
| 93 | * Arguments: @pgen *p@ = pointer to prime generation context |
| 94 | * |
| 95 | * Returns: --- |
| 96 | * |
| 97 | * Use: Discards a context and all the resources it holds. |
| 98 | */ |
| 99 | |
| 100 | extern void pgen_destroy(pgen */*p*/); |
| 101 | |
| 102 | /* --- @pgen_step@ --- * |
| 103 | * |
| 104 | * Arguments: @pgen *p@ = pointer to prime generation context |
| 105 | * @mpw step@ = how much to step the number |
| 106 | * |
| 107 | * Returns: One of the @PGEN@ constants above. |
| 108 | * |
| 109 | * Use: Steps a number by a small amount. Stepping is much faster |
| 110 | * than initializing with a new number. The test performed is |
| 111 | * the same simple one used by @ptab_create@, so @PGEN_MAYBE@ |
| 112 | * results should be followed up by a Rabin-Miller test. |
| 113 | */ |
| 114 | |
| 115 | extern int pgen_step(pgen */*p*/, mpw /*step*/); |
| 116 | |
| 117 | /* --- @pgen_muladd@ --- * |
| 118 | * |
| 119 | * Arguments: @pgen *p@ = destination prime generation context |
| 120 | * @const pgen *q@ = source prime generation context |
| 121 | * @mpw m@ = number to multiply by |
| 122 | * @mpw a@ = number to add |
| 123 | * |
| 124 | * Returns: One of the @PGEN@ constants above. |
| 125 | * |
| 126 | * Use: Multiplies the number in a prime generation context by a |
| 127 | * small value and then adds a small value. The destination |
| 128 | * should either be uninitialized or the same as the source. |
| 129 | * |
| 130 | * Common things to do include multiplying by 2 and adding 0 to |
| 131 | * turn a prime into a jump for finding other primes with @q@ as |
| 132 | * a factor of @p - 1@, or multiplying by 2 and adding 1. |
| 133 | */ |
| 134 | |
| 135 | extern int pgen_muladd(pgen */*p*/, const pgen */*q*/, mpw /*m*/, mpw /*a*/); |
| 136 | |
| 137 | /* --- @pgen_jump@ --- * |
| 138 | * |
| 139 | * Arguments: @pgen *p@ = pointer to prime generation context |
| 140 | * @pgen *j@ = pointer to another generation context |
| 141 | * |
| 142 | * Returns: One of the @PGEN@ constants above. |
| 143 | * |
| 144 | * Use: Steps a number by a large amount. Even so, jumping is much |
| 145 | * faster than initializing a new number. The test peformed is |
| 146 | * the same simple one used by @ptab_create@, so @PGEN_MAYBE@ |
| 147 | * results should be followed up by a Rabin-Miller test. |
| 148 | * |
| 149 | * Note that the number stored in the @j@ context is probably |
| 150 | * better off being even than prime. The important thing is |
| 151 | * that all of the residues for the number have already been |
| 152 | * computed. |
| 153 | */ |
| 154 | |
| 155 | extern int pgen_jump(pgen */*p*/, pgen */*j*/); |
| 156 | |
| 157 | /*----- That's all, folks -------------------------------------------------*/ |
| 158 | |
| 159 | #ifdef __cplusplus |
| 160 | } |
| 161 | #endif |
| 162 | |
| 163 | #endif |