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