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