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