| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: pgen.h,v 1.2 1999/11/20 22:23:05 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.2 1999/11/20 22:23:05 mdw |
| 34 | * Add multiply-and-add function for Diffie-Hellman safe prime generation. |
| 35 | * |
| 36 | * Revision 1.1 1999/11/19 13:17:57 mdw |
| 37 | * Prime number generator and tester. |
| 38 | * |
| 39 | */ |
| 40 | |
| 41 | #ifndef PGEN_H |
| 42 | #define PGEN_H |
| 43 | |
| 44 | #ifdef __cplusplus |
| 45 | extern "C" { |
| 46 | #endif |
| 47 | |
| 48 | /*----- Header files ------------------------------------------------------*/ |
| 49 | |
| 50 | #ifndef MP_H |
| 51 | # include "mp.h" |
| 52 | #endif |
| 53 | |
| 54 | #ifndef PTAB_H |
| 55 | # include "ptab.h" |
| 56 | #endif |
| 57 | |
| 58 | /*----- Constants ---------------------------------------------------------*/ |
| 59 | |
| 60 | #define PGEN_COMPOSITE (-1) /* Number is definitely composite */ |
| 61 | #define PGEN_MAYBE (0) /* Number may be prime */ |
| 62 | #define PGEN_PRIME (1) /* Number is definitely prime */ |
| 63 | |
| 64 | /*----- Data structures ---------------------------------------------------*/ |
| 65 | |
| 66 | typedef struct pgen { |
| 67 | mp *m; |
| 68 | unsigned char r[NPRIME]; |
| 69 | } pgen; |
| 70 | |
| 71 | /*----- Functions provided ------------------------------------------------*/ |
| 72 | |
| 73 | /* --- @pgen_create@ --- * |
| 74 | * |
| 75 | * Arguments: @pgen *p@ = pointer to prime generation context |
| 76 | * @mp *m@ = pointer to initial number to test |
| 77 | * |
| 78 | * Returns: One of the @PGEN@ constants above. |
| 79 | * |
| 80 | * Use: Tests an initial number for primality by computing its |
| 81 | * residue modulo various small prime numbers. This is fairly |
| 82 | * quick, but not particularly certain. If a @PGEN_MAYBE@ |
| 83 | * result is returned, perform Rabin-Miller tests to confirm. |
| 84 | */ |
| 85 | |
| 86 | extern int pgen_create(pgen */*p*/, mp */*m*/); |
| 87 | |
| 88 | /* --- @pgen_destroy@ --- * |
| 89 | * |
| 90 | * Arguments: @pgen *p@ = pointer to prime generation context |
| 91 | * |
| 92 | * Returns: --- |
| 93 | * |
| 94 | * Use: Discards a context and all the resources it holds. |
| 95 | */ |
| 96 | |
| 97 | extern void pgen_destroy(pgen */*p*/); |
| 98 | |
| 99 | /* --- @pgen_step@ --- * |
| 100 | * |
| 101 | * Arguments: @pgen *p@ = pointer to prime generation context |
| 102 | * @mpw step@ = how much to step the number |
| 103 | * |
| 104 | * Returns: One of the @PGEN@ constants above. |
| 105 | * |
| 106 | * Use: Steps a number by a small amount. Stepping is much faster |
| 107 | * than initializing with a new number. The test performed is |
| 108 | * the same simple one used by @ptab_create@, so @PGEN_MAYBE@ |
| 109 | * results should be followed up by a Rabin-Miller test. |
| 110 | */ |
| 111 | |
| 112 | extern int pgen_step(pgen */*p*/, mpw /*step*/); |
| 113 | |
| 114 | /* --- @pgen_muladd@ --- * |
| 115 | * |
| 116 | * Arguments: @pgen *p@ = destination prime generation context |
| 117 | * @const pgen *q@ = source prime generation context |
| 118 | * @mpw m@ = number to multiply by |
| 119 | * @mpw a@ = number to add |
| 120 | * |
| 121 | * Returns: One of the @PGEN@ constants above. |
| 122 | * |
| 123 | * Use: Multiplies the number in a prime generation context by a |
| 124 | * small value and then adds a small value. The destination |
| 125 | * should either be uninitialized or the same as the source. |
| 126 | * |
| 127 | * Common things to do include multiplying by 2 and adding 0 to |
| 128 | * turn a prime into a jump for finding other primes with @q@ as |
| 129 | * a factor of @p - 1@, or multiplying by 2 and adding 1. |
| 130 | */ |
| 131 | |
| 132 | extern int pgen_muladd(pgen */*p*/, const pgen */*q*/, mpw /*m*/, mpw /*a*/); |
| 133 | |
| 134 | /* --- @pgen_jump@ --- * |
| 135 | * |
| 136 | * Arguments: @pgen *p@ = pointer to prime generation context |
| 137 | * @pgen *j@ = pointer to another generation context |
| 138 | * |
| 139 | * Returns: One of the @PGEN@ constants above. |
| 140 | * |
| 141 | * Use: Steps a number by a large amount. Even so, jumping is much |
| 142 | * faster than initializing a new number. The test peformed is |
| 143 | * the same simple one used by @ptab_create@, so @PGEN_MAYBE@ |
| 144 | * results should be followed up by a Rabin-Miller test. |
| 145 | * |
| 146 | * Note that the number stored in the @j@ context is probably |
| 147 | * better off being even than prime. The important thing is |
| 148 | * that all of the residues for the number have already been |
| 149 | * computed. |
| 150 | */ |
| 151 | |
| 152 | extern int pgen_jump(pgen */*p*/, pgen */*j*/); |
| 153 | |
| 154 | /*----- That's all, folks -------------------------------------------------*/ |
| 155 | |
| 156 | #ifdef __cplusplus |
| 157 | } |
| 158 | #endif |
| 159 | |
| 160 | #endif |