0f5ec153 |
1 | /* -*-c-*- |
2 | * |
dfdacfdc |
3 | * $Id: rabin.h,v 1.3 1999/12/22 15:50:29 mdw Exp $ |
0f5ec153 |
4 | * |
5 | * Miller-Rabin primality test |
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: rabin.h,v $ |
dfdacfdc |
33 | * Revision 1.3 1999/12/22 15:50:29 mdw |
34 | * Reworking for new prime-search system. Add function for working out how |
35 | * many iterations to use for a particular number. |
36 | * |
b3f05084 |
37 | * Revision 1.2 1999/12/10 23:29:48 mdw |
38 | * Change header file guard names. |
39 | * |
0f5ec153 |
40 | * Revision 1.1 1999/11/19 13:17:57 mdw |
41 | * Prime number generator and tester. |
42 | * |
43 | */ |
44 | |
b3f05084 |
45 | #ifndef CATACOMB_RABIN_H |
46 | #define CATACOMB_RABIN_H |
0f5ec153 |
47 | |
48 | #ifdef __cplusplus |
49 | extern "C" { |
50 | #endif |
51 | |
52 | /*----- Header files ------------------------------------------------------*/ |
53 | |
b3f05084 |
54 | #ifndef CATACOMB_MP_H |
0f5ec153 |
55 | # include "mp.h" |
56 | #endif |
57 | |
b3f05084 |
58 | #ifndef CATACOMB_MPMONT_H |
0f5ec153 |
59 | # include "mpmont.h" |
60 | #endif |
61 | |
dfdacfdc |
62 | #ifndef CATACOMB_PFILT_H |
63 | # include "pfilt.h" |
0f5ec153 |
64 | #endif |
65 | |
66 | /*----- Data structures ---------------------------------------------------*/ |
67 | |
68 | typedef struct rabin { |
69 | mpmont mm; /* Montgomery arithmetic context */ |
70 | size_t s; /* %$m = 2^s r + 1$% */ |
71 | mp *r; /* %$m = 2^s r + 1$% */ |
72 | mp *m1; /* %$(m - 1)R \bmod m */ |
73 | } rabin; |
74 | |
75 | /*----- Functions provided ------------------------------------------------*/ |
76 | |
77 | /* --- @rabin_create@ --- * |
78 | * |
79 | * Arguments: @rabin *r@ = pointer to Rabin-Miller context |
80 | * @mp *m@ = pointer to number to test |
81 | * |
82 | * Returns: --- |
83 | * |
84 | * Use: Precomputes some useful values for performing the |
85 | * Miller-Rabin probabilistic primality test. |
86 | */ |
87 | |
88 | extern void rabin_create(rabin */*r*/, mp */*m*/); |
89 | |
90 | /* --- @rabin_destroy@ --- * |
91 | * |
92 | * Arguments: @rabin *r@ = pointer to Rabin-Miller context |
93 | * |
94 | * Returns: --- |
95 | * |
96 | * Use: Disposes of a Rabin-Miller context when it's no longer |
97 | * needed. |
98 | */ |
99 | |
100 | extern void rabin_destroy(rabin */*r*/); |
101 | |
102 | /* --- @rabin_test@ --- * |
103 | * |
104 | * Arguments: @rabin *r@ = pointer to Rabin-Miller context |
105 | * @mp *g@ = base to test the number against |
106 | * |
dfdacfdc |
107 | * Returns: Either @PGEN_FAIL@ if the test failed, or @PGEN_TRY@ |
0f5ec153 |
108 | * if it succeeded. |
109 | * |
110 | * Use: Performs a single iteration of the Rabin-Miller primality |
111 | * test. |
112 | */ |
113 | |
114 | extern int rabin_test(rabin */*r*/, mp */*g*/); |
115 | |
dfdacfdc |
116 | /* --- @rabin_iters@ --- * |
117 | * |
118 | * Arguments: @unsigned len@ = number of bits in value |
119 | * |
120 | * Returns: Number of iterations recommended. |
121 | * |
122 | * Use: Returns the recommended number of iterations to ensure that a |
123 | * number with @len@ bits is really prime. |
124 | */ |
125 | |
126 | extern int rabin_iters(unsigned /*len*/); |
127 | |
0f5ec153 |
128 | /*----- That's all, folks -------------------------------------------------*/ |
129 | |
130 | #ifdef __cplusplus |
131 | } |
132 | #endif |
133 | |
134 | #endif |