0f5ec153 |
1 | /* -*-c-*- |
2 | * |
b0b682aa |
3 | * $Id: rabin.c,v 1.6 2001/06/16 12:56:38 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.c,v $ |
b0b682aa |
33 | * Revision 1.6 2001/06/16 12:56:38 mdw |
34 | * Fixes for interface change to @mpmont_expr@ and @mpmont_mexpr@. |
35 | * |
22bab86c |
36 | * Revision 1.5 2000/10/08 12:11:22 mdw |
37 | * Use @MP_EQ@ instead of @MP_CMP@. |
38 | * |
31cb4e2e |
39 | * Revision 1.4 2000/06/22 19:03:02 mdw |
40 | * Use the new @mp_odd@ function. |
41 | * |
dfdacfdc |
42 | * Revision 1.3 1999/12/22 15:50:29 mdw |
43 | * Reworking for new prime-search system. Add function for working out how |
44 | * many iterations to use for a particular number. |
45 | * |
b3f05084 |
46 | * Revision 1.2 1999/12/10 23:29:48 mdw |
47 | * Change header file guard names. |
48 | * |
0f5ec153 |
49 | * Revision 1.1 1999/11/19 13:17:57 mdw |
50 | * Prime number generator and tester. |
51 | * |
52 | */ |
53 | |
54 | /*----- Header files ------------------------------------------------------*/ |
55 | |
56 | #include "mp.h" |
dfdacfdc |
57 | #include "mpbarrett.h" |
0f5ec153 |
58 | #include "mpmont.h" |
59 | #include "pgen.h" |
60 | #include "rabin.h" |
61 | |
62 | /*----- Main code ---------------------------------------------------------*/ |
63 | |
64 | /* --- @rabin_create@ --- * |
65 | * |
66 | * Arguments: @rabin *r@ = pointer to Rabin-Miller context |
67 | * @mp *m@ = pointer to number to test |
68 | * |
69 | * Returns: --- |
70 | * |
71 | * Use: Precomputes some useful values for performing the |
72 | * Miller-Rabin probabilistic primality test. |
73 | */ |
74 | |
75 | void rabin_create(rabin *r, mp *m) |
76 | { |
77 | mp *m1 = mp_sub(MP_NEW, m, MP_ONE); |
0f5ec153 |
78 | mpmont_create(&r->mm, m); |
31cb4e2e |
79 | r->r = mp_odd(MP_NEW, m1, &r->s); |
dfdacfdc |
80 | r->m1 = mp_sub(MP_NEW, m, r->mm.r); |
0f5ec153 |
81 | mp_drop(m1); |
82 | } |
83 | |
84 | /* --- @rabin_destroy@ --- * |
85 | * |
86 | * Arguments: @rabin *r@ = pointer to Rabin-Miller context |
87 | * |
88 | * Returns: --- |
89 | * |
90 | * Use: Disposes of a Rabin-Miller context when it's no longer |
91 | * needed. |
92 | */ |
93 | |
94 | void rabin_destroy(rabin *r) |
95 | { |
96 | mp_drop(r->r); |
97 | mp_drop(r->m1); |
98 | mpmont_destroy(&r->mm); |
99 | } |
100 | |
101 | /* --- @rabin_test@ --- * |
102 | * |
103 | * Arguments: @rabin *r@ = pointer to Rabin-Miller context |
104 | * @mp *g@ = base to test the number against |
105 | * |
dfdacfdc |
106 | * Returns: Either @PGEN_FAIL@ if the test failed, or @PGEN_PASS@ |
0f5ec153 |
107 | * if it succeeded. |
108 | * |
109 | * Use: Performs a single iteration of the Rabin-Miller primality |
110 | * test. |
111 | */ |
112 | |
113 | int rabin_test(rabin *r, mp *g) |
114 | { |
115 | mp *y; |
116 | mp *dd, *spare = MP_NEW; |
117 | size_t j; |
dfdacfdc |
118 | int rc = PGEN_FAIL; |
0f5ec153 |
119 | |
120 | /* --- Calculate %$y R = g^r R \bmod m$% --- * |
121 | * |
122 | * If %$y = 1$% or %$y = m - 1$% then %$m$% is prime. If course, note that |
123 | * @y@ here has an extra factor of %$R$%. |
124 | */ |
125 | |
b0b682aa |
126 | y = mpmont_mul(&r->mm, MP_NEW, g, r->mm.r2); |
127 | y = mpmont_expr(&r->mm, y, y, r->r); |
22bab86c |
128 | if (MP_EQ(y, r->mm.r) || MP_EQ(y, r->m1)) { |
dfdacfdc |
129 | rc = PGEN_PASS; |
0f5ec153 |
130 | goto done; |
131 | } |
132 | |
133 | /* --- Now for the main loop --- * |
134 | * |
135 | * If %$y^{2^j} \ne m - 1$% for any %$0 \le j < s$% then %$m$% is |
136 | * composite. Of course, %$j = 0$% has already been tested. |
137 | */ |
138 | |
139 | for (j = 1; j < r->s; j++) { |
dfdacfdc |
140 | dd = mp_sqr(spare, y); |
141 | dd = mpmont_reduce(&r->mm, dd, dd); |
142 | spare = y; y = dd; |
22bab86c |
143 | if (MP_EQ(y, r->mm.r)) |
0f5ec153 |
144 | break; |
22bab86c |
145 | if (MP_EQ(y, r->m1)) { |
dfdacfdc |
146 | rc = PGEN_PASS; |
0f5ec153 |
147 | break; |
148 | } |
149 | } |
150 | |
151 | /* --- Done --- */ |
152 | |
153 | done: |
154 | if (spare != MP_NEW) |
155 | MP_DROP(spare); |
156 | MP_DROP(y); |
157 | return (rc); |
158 | } |
159 | |
dfdacfdc |
160 | /* --- @rabin_iters@ --- * |
161 | * |
162 | * Arguments: @unsigned len@ = number of bits in value |
163 | * |
164 | * Returns: Number of iterations recommended. |
165 | * |
166 | * Use: Returns the recommended number of iterations to ensure that a |
167 | * number with @len@ bits is really prime. |
168 | */ |
169 | |
170 | int rabin_iters(unsigned len) |
171 | { |
172 | static struct { |
173 | unsigned b; |
174 | int i; |
175 | } *p, *q, tab[] = { |
176 | { 100, 27 }, |
177 | { 150, 18 }, |
178 | { 200, 15 }, |
179 | { 250, 12 }, |
180 | { 300, 9 }, |
181 | { 350, 8 }, |
182 | { 400, 7 }, |
183 | { 450, 6 }, |
184 | { 550, 5 }, |
185 | { 650, 4 }, |
186 | { 850, 3 }, |
187 | { 1300, 2 } |
188 | }; |
189 | |
190 | unsigned i; |
191 | |
192 | /* --- Binary search through the table --- */ |
193 | |
194 | p = tab; |
195 | q = tab + (sizeof(tab)/sizeof(tab[0])); |
196 | for (;;) { |
197 | i = (q - p) / 2; |
198 | if (!i) |
199 | break; |
200 | if (len >= p[i].b && len < p[i + 1].b) |
201 | break; |
202 | if (len > p[i].b) |
203 | p = p + i; |
204 | else |
205 | q = p + i; |
206 | } |
207 | return (p[i].i); |
208 | } |
209 | |
0f5ec153 |
210 | /*----- That's all, folks -------------------------------------------------*/ |