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