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