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