chiark / gitweb /
progs/perftest.c: Use from Glibc syscall numbers.
[catacomb] / math / rabin.c
CommitLineData
0f5ec153 1/* -*-c-*-
0f5ec153 2 *
3 * Miller-Rabin primality test
4 *
5 * (c) 1999 Straylight/Edgeware
6 */
7
45c0fd36 8/*----- Licensing notice --------------------------------------------------*
0f5ec153 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.
45c0fd36 16 *
0f5ec153 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.
45c0fd36 21 *
0f5ec153 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
0f5ec153 28/*----- Header files ------------------------------------------------------*/
29
30#include "mp.h"
dfdacfdc 31#include "mpbarrett.h"
0f5ec153 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 *
4a548fc1 43 * Returns: Zero on success, nonzero on failure.
0f5ec153 44 *
45 * Use: Precomputes some useful values for performing the
46 * Miller-Rabin probabilistic primality test.
47 */
48
4a548fc1 49int rabin_create(rabin *r, mp *m)
0f5ec153 50{
51 mp *m1 = mp_sub(MP_NEW, m, MP_ONE);
4a548fc1 52 if (mpmont_create(&r->mm, m)) {
53 MP_DROP(m1);
54 return (-1);
55 }
31cb4e2e 56 r->r = mp_odd(MP_NEW, m1, &r->s);
dfdacfdc 57 r->m1 = mp_sub(MP_NEW, m, r->mm.r);
0f5ec153 58 mp_drop(m1);
4a548fc1 59 return (0);
0f5ec153 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
72void rabin_destroy(rabin *r)
73{
74 mp_drop(r->r);
75 mp_drop(r->m1);
76 mpmont_destroy(&r->mm);
77}
78
283b9af0 79/* --- @rabin_test@, @rabin_rtest@ --- *
0f5ec153 80 *
81 * Arguments: @rabin *r@ = pointer to Rabin-Miller context
82 * @mp *g@ = base to test the number against
83 *
dfdacfdc 84 * Returns: Either @PGEN_FAIL@ if the test failed, or @PGEN_PASS@
0f5ec153 85 * if it succeeded.
86 *
87 * Use: Performs a single iteration of the Rabin-Miller primality
283b9af0 88 * test. The @rtest@ variant assumes that %$g$% is either
89 * already in Montgomery representation, or you don't care.
0f5ec153 90 */
91
283b9af0 92int rabin_rtest(rabin *r, mp *g)
0f5ec153 93{
94 mp *y;
95 mp *dd, *spare = MP_NEW;
96 size_t j;
dfdacfdc 97 int rc = PGEN_FAIL;
0f5ec153 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
283b9af0 105 y = mpmont_expr(&r->mm, MP_NEW, g, r->r);
22bab86c 106 if (MP_EQ(y, r->mm.r) || MP_EQ(y, r->m1)) {
dfdacfdc 107 rc = PGEN_PASS;
0f5ec153 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++) {
dfdacfdc 118 dd = mp_sqr(spare, y);
119 dd = mpmont_reduce(&r->mm, dd, dd);
120 spare = y; y = dd;
22bab86c 121 if (MP_EQ(y, r->mm.r))
0f5ec153 122 break;
22bab86c 123 if (MP_EQ(y, r->m1)) {
dfdacfdc 124 rc = PGEN_PASS;
0f5ec153 125 break;
126 }
127 }
128
129 /* --- Done --- */
130
131done:
132 if (spare != MP_NEW)
133 MP_DROP(spare);
134 MP_DROP(y);
135 return (rc);
136}
137
283b9af0 138int 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
dfdacfdc 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
157int rabin_iters(unsigned len)
158{
4e66da02 159 static const struct {
dfdacfdc 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
0f5ec153 197/*----- That's all, folks -------------------------------------------------*/