| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * Miller-Rabin primality test |
| 4 | * |
| 5 | * (c) 1999 Straylight/Edgeware |
| 6 | */ |
| 7 | |
| 8 | /*----- Licensing notice --------------------------------------------------* |
| 9 | * |
| 10 | * This file is part of Catacomb. |
| 11 | * |
| 12 | * Catacomb is free software; you can redistribute it and/or modify |
| 13 | * it under the terms of the GNU Library General Public License as |
| 14 | * published by the Free Software Foundation; either version 2 of the |
| 15 | * License, or (at your option) any later version. |
| 16 | * |
| 17 | * Catacomb is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU Library General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU Library General Public |
| 23 | * License along with Catacomb; if not, write to the Free |
| 24 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
| 25 | * MA 02111-1307, USA. |
| 26 | */ |
| 27 | |
| 28 | #ifndef CATACOMB_RABIN_H |
| 29 | #define CATACOMB_RABIN_H |
| 30 | |
| 31 | #ifdef __cplusplus |
| 32 | extern "C" { |
| 33 | #endif |
| 34 | |
| 35 | /*----- Header files ------------------------------------------------------*/ |
| 36 | |
| 37 | #ifndef CATACOMB_MP_H |
| 38 | # include "mp.h" |
| 39 | #endif |
| 40 | |
| 41 | #ifndef CATACOMB_MPMONT_H |
| 42 | # include "mpmont.h" |
| 43 | #endif |
| 44 | |
| 45 | #ifndef CATACOMB_PFILT_H |
| 46 | # include "pfilt.h" |
| 47 | #endif |
| 48 | |
| 49 | /*----- Data structures ---------------------------------------------------*/ |
| 50 | |
| 51 | typedef struct rabin { |
| 52 | mpmont mm; /* Montgomery arithmetic context */ |
| 53 | size_t s; /* %$m = 2^s r + 1$% */ |
| 54 | mp *r; /* %$m = 2^s r + 1$% */ |
| 55 | mp *m1; /* %$(m - 1)R \bmod m$% */ |
| 56 | } rabin; |
| 57 | |
| 58 | /*----- Functions provided ------------------------------------------------*/ |
| 59 | |
| 60 | /* --- @rabin_create@ --- * |
| 61 | * |
| 62 | * Arguments: @rabin *r@ = pointer to Rabin-Miller context |
| 63 | * @mp *m@ = pointer to number to test |
| 64 | * |
| 65 | * Returns: Zero on success, nonzero for failure. |
| 66 | * |
| 67 | * Use: Precomputes some useful values for performing the |
| 68 | * Miller-Rabin probabilistic primality test. |
| 69 | */ |
| 70 | |
| 71 | extern int rabin_create(rabin */*r*/, mp */*m*/); |
| 72 | |
| 73 | /* --- @rabin_destroy@ --- * |
| 74 | * |
| 75 | * Arguments: @rabin *r@ = pointer to Rabin-Miller context |
| 76 | * |
| 77 | * Returns: --- |
| 78 | * |
| 79 | * Use: Disposes of a Rabin-Miller context when it's no longer |
| 80 | * needed. |
| 81 | */ |
| 82 | |
| 83 | extern void rabin_destroy(rabin */*r*/); |
| 84 | |
| 85 | /* --- @rabin_test@, @rabin_rtest@ --- * |
| 86 | * |
| 87 | * Arguments: @rabin *r@ = pointer to Rabin-Miller context |
| 88 | * @mp *g@ = base to test the number against |
| 89 | * |
| 90 | * Returns: Either @PGEN_FAIL@ if the test failed, or @PGEN_PASS@ |
| 91 | * if it succeeded. |
| 92 | * |
| 93 | * Use: Performs a single iteration of the Rabin-Miller primality |
| 94 | * test. The @rtest@ variant assumes that %$g$% is either |
| 95 | * already in Montgomery representation, or you don't care. |
| 96 | */ |
| 97 | |
| 98 | extern int rabin_rtest(rabin */*r*/, mp */*g*/); |
| 99 | extern int rabin_test(rabin */*r*/, mp */*g*/); |
| 100 | |
| 101 | /* --- @rabin_iters@ --- * |
| 102 | * |
| 103 | * Arguments: @unsigned len@ = number of bits in value |
| 104 | * |
| 105 | * Returns: Number of iterations recommended. |
| 106 | * |
| 107 | * Use: Returns the recommended number of iterations to ensure that a |
| 108 | * number with @len@ bits is really prime. |
| 109 | */ |
| 110 | |
| 111 | extern int rabin_iters(unsigned /*len*/); |
| 112 | |
| 113 | /*----- That's all, folks -------------------------------------------------*/ |
| 114 | |
| 115 | #ifdef __cplusplus |
| 116 | } |
| 117 | #endif |
| 118 | |
| 119 | #endif |