0f5ec153 |
1 | /* -*-c-*- |
0f5ec153 |
2 | * |
3 | * Miller-Rabin primality test |
4 | * |
5 | * (c) 1999 Straylight/Edgeware |
6 | */ |
7 | |
45c0fd36 |
8 | /*----- Licensing notice --------------------------------------------------* |
0f5ec153 |
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. |
45c0fd36 |
16 | * |
0f5ec153 |
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. |
45c0fd36 |
21 | * |
0f5ec153 |
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 | |
b3f05084 |
28 | #ifndef CATACOMB_RABIN_H |
29 | #define CATACOMB_RABIN_H |
0f5ec153 |
30 | |
31 | #ifdef __cplusplus |
32 | extern "C" { |
33 | #endif |
34 | |
35 | /*----- Header files ------------------------------------------------------*/ |
36 | |
b3f05084 |
37 | #ifndef CATACOMB_MP_H |
0f5ec153 |
38 | # include "mp.h" |
39 | #endif |
40 | |
b3f05084 |
41 | #ifndef CATACOMB_MPMONT_H |
0f5ec153 |
42 | # include "mpmont.h" |
43 | #endif |
44 | |
dfdacfdc |
45 | #ifndef CATACOMB_PFILT_H |
46 | # include "pfilt.h" |
0f5ec153 |
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$% */ |
7fafb636 |
55 | mp *m1; /* %$(m - 1)R \bmod m$% */ |
0f5ec153 |
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 | * |
4a548fc1 |
65 | * Returns: Zero on success, nonzero for failure. |
0f5ec153 |
66 | * |
67 | * Use: Precomputes some useful values for performing the |
68 | * Miller-Rabin probabilistic primality test. |
69 | */ |
70 | |
4a548fc1 |
71 | extern int rabin_create(rabin */*r*/, mp */*m*/); |
0f5ec153 |
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 | |
283b9af0 |
85 | /* --- @rabin_test@, @rabin_rtest@ --- * |
0f5ec153 |
86 | * |
87 | * Arguments: @rabin *r@ = pointer to Rabin-Miller context |
88 | * @mp *g@ = base to test the number against |
89 | * |
866fa315 |
90 | * Returns: Either @PGEN_FAIL@ if the test failed, or @PGEN_PASS@ |
0f5ec153 |
91 | * if it succeeded. |
92 | * |
93 | * Use: Performs a single iteration of the Rabin-Miller primality |
283b9af0 |
94 | * test. The @rtest@ variant assumes that %$g$% is either |
95 | * already in Montgomery representation, or you don't care. |
0f5ec153 |
96 | */ |
97 | |
283b9af0 |
98 | extern int rabin_rtest(rabin */*r*/, mp */*g*/); |
0f5ec153 |
99 | extern int rabin_test(rabin */*r*/, mp */*g*/); |
100 | |
dfdacfdc |
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 | |
0f5ec153 |
113 | /*----- That's all, folks -------------------------------------------------*/ |
114 | |
115 | #ifdef __cplusplus |
116 | } |
117 | #endif |
118 | |
119 | #endif |