chiark / gitweb /
rand/rand-x86ish.S: Hoist argument register allocation outside.
[catacomb] / math / rabin.c
1 /* -*-c-*-
2  *
3  * Miller-Rabin primality test
4  *
5  * (c) 1999 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
9  *
10  * This file is part of Catacomb.
11  *
12  * Catacomb is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU Library General Public License as
14  * published by the Free Software Foundation; either version 2 of the
15  * License, or (at your option) any later version.
16  *
17  * Catacomb is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU Library General Public License for more details.
21  *
22  * You should have received a copy of the GNU Library General Public
23  * License along with Catacomb; if not, write to the Free
24  * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25  * MA 02111-1307, USA.
26  */
27
28 /*----- Header files ------------------------------------------------------*/
29
30 #include "mp.h"
31 #include "mpbarrett.h"
32 #include "mpmont.h"
33 #include "pgen.h"
34 #include "rabin.h"
35
36 /*----- Main code ---------------------------------------------------------*/
37
38 /* --- @rabin_create@ --- *
39  *
40  * Arguments:   @rabin *r@ = pointer to Rabin-Miller context
41  *              @mp *m@ = pointer to number to test
42  *
43  * Returns:     Zero on success, nonzero on failure.
44  *
45  * Use:         Precomputes some useful values for performing the
46  *              Miller-Rabin probabilistic primality test.
47  */
48
49 int rabin_create(rabin *r, mp *m)
50 {
51   mp *m1 = mp_sub(MP_NEW, m, MP_ONE);
52   if (mpmont_create(&r->mm, m)) {
53     MP_DROP(m1);
54     return (-1);
55   }
56   r->r = mp_odd(MP_NEW, m1, &r->s);
57   r->m1 = mp_sub(MP_NEW, m, r->mm.r);
58   mp_drop(m1);
59   return (0);
60 }
61
62 /* --- @rabin_destroy@ --- *
63  *
64  * Arguments:   @rabin *r@ = pointer to Rabin-Miller context
65  *
66  * Returns:     ---
67  *
68  * Use:         Disposes of a Rabin-Miller context when it's no longer
69  *              needed.
70  */
71
72 void rabin_destroy(rabin *r)
73 {
74   mp_drop(r->r);
75   mp_drop(r->m1);
76   mpmont_destroy(&r->mm);
77 }
78
79 /* --- @rabin_test@, @rabin_rtest@ --- *
80  *
81  * Arguments:   @rabin *r@ = pointer to Rabin-Miller context
82  *              @mp *g@ = base to test the number against
83  *
84  * Returns:     Either @PGEN_FAIL@ if the test failed, or @PGEN_PASS@
85  *              if it succeeded.
86  *
87  * Use:         Performs a single iteration of the Rabin-Miller primality
88  *              test.  The @rtest@ variant assumes that %$g$% is either
89  *              already in Montgomery representation, or you don't care.
90  */
91
92 int rabin_rtest(rabin *r, mp *g)
93 {
94   mp *y;
95   mp *dd, *spare = MP_NEW;
96   size_t j;
97   int rc = PGEN_FAIL;
98
99   /* --- Calculate %$y R = g^r R \bmod m$% --- *
100    *
101    * If %$y = 1$% or %$y = m - 1$% then %$m$% is prime.  If course, note that
102    * @y@ here has an extra factor of %$R$%.
103    */
104
105   y = mpmont_expr(&r->mm, MP_NEW, g, r->r);
106   if (MP_EQ(y, r->mm.r) || MP_EQ(y, r->m1)) {
107     rc = PGEN_PASS;
108     goto done;
109   }
110
111   /* --- Now for the main loop --- *
112    *
113    * If %$y^{2^j} \ne m - 1$% for any %$0 \le j < s$% then %$m$% is
114    * composite.  Of course, %$j = 0$% has already been tested.
115    */
116
117   for (j = 1; j < r->s; j++) {
118     dd = mp_sqr(spare, y);
119     dd = mpmont_reduce(&r->mm, dd, dd);
120     spare = y; y = dd;
121     if (MP_EQ(y, r->mm.r))
122       break;
123     if (MP_EQ(y, r->m1)) {
124       rc = PGEN_PASS;
125       break;
126     }
127   }
128
129   /* --- Done --- */
130
131 done:
132   if (spare != MP_NEW)
133     MP_DROP(spare);
134   MP_DROP(y);
135   return (rc);
136 }
137
138 int rabin_test(rabin *r, mp *g)
139 {
140   int rc;
141   g = mpmont_mul(&r->mm, MP_NEW, g, r->mm.r2);
142   rc = rabin_rtest(r, g);
143   mp_drop(g);
144   return (rc);
145 }
146
147 /* --- @rabin_iters@ --- *
148  *
149  * Arguments:   @unsigned len@ = number of bits in value
150  *
151  * Returns:     Number of iterations recommended.
152  *
153  * Use:         Returns the recommended number of iterations to ensure that a
154  *              number with @len@ bits is really prime.
155  */
156
157 int rabin_iters(unsigned len)
158 {
159   static const struct {
160     unsigned b;
161     int i;
162   } *p, *q, tab[] = {
163     { 100, 27 },
164     { 150, 18 },
165     { 200, 15 },
166     { 250, 12 },
167     { 300, 9 },
168     { 350, 8 },
169     { 400, 7 },
170     { 450, 6 },
171     { 550, 5 },
172     { 650, 4 },
173     { 850, 3 },
174     { 1300, 2 }
175   };
176
177   unsigned i;
178
179   /* --- Binary search through the table --- */
180
181   p = tab;
182   q = tab + (sizeof(tab)/sizeof(tab[0]));
183   for (;;) {
184     i = (q - p) / 2;
185     if (!i)
186       break;
187     if (len >= p[i].b && len < p[i + 1].b)
188       break;
189     if (len > p[i].b)
190       p = p + i;
191     else
192       q = p + i;
193   }
194   return (p[i].i);
195 }
196
197 /*----- That's all, folks -------------------------------------------------*/