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