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