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