5 * Miller-Rabin primality test
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
30 /*----- Header files ------------------------------------------------------*/
33 #include "mpbarrett.h"
38 /*----- Main code ---------------------------------------------------------*/
40 /* --- @rabin_create@ --- *
42 * Arguments: @rabin *r@ = pointer to Rabin-Miller context
43 * @mp *m@ = pointer to number to test
45 * Returns: Zero on success, nonzero on failure.
47 * Use: Precomputes some useful values for performing the
48 * Miller-Rabin probabilistic primality test.
51 int rabin_create(rabin *r, mp *m)
53 mp *m1 = mp_sub(MP_NEW, m, MP_ONE);
54 if (mpmont_create(&r->mm, m)) {
58 r->r = mp_odd(MP_NEW, m1, &r->s);
59 r->m1 = mp_sub(MP_NEW, m, r->mm.r);
64 /* --- @rabin_destroy@ --- *
66 * Arguments: @rabin *r@ = pointer to Rabin-Miller context
70 * Use: Disposes of a Rabin-Miller context when it's no longer
74 void rabin_destroy(rabin *r)
78 mpmont_destroy(&r->mm);
81 /* --- @rabin_test@, @rabin_rtest@ --- *
83 * Arguments: @rabin *r@ = pointer to Rabin-Miller context
84 * @mp *g@ = base to test the number against
86 * Returns: Either @PGEN_FAIL@ if the test failed, or @PGEN_PASS@
89 * Use: Performs a single iteration of the Rabin-Miller primality
90 * test. The @rtest@ variant assumes that %$g$% is either
91 * already in Montgomery representation, or you don't care.
94 int rabin_rtest(rabin *r, mp *g)
97 mp *dd, *spare = MP_NEW;
101 /* --- Calculate %$y R = g^r R \bmod m$% --- *
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$%.
107 y = mpmont_expr(&r->mm, MP_NEW, g, r->r);
108 if (MP_EQ(y, r->mm.r) || MP_EQ(y, r->m1)) {
113 /* --- Now for the main loop --- *
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.
119 for (j = 1; j < r->s; j++) {
120 dd = mp_sqr(spare, y);
121 dd = mpmont_reduce(&r->mm, dd, dd);
123 if (MP_EQ(y, r->mm.r))
125 if (MP_EQ(y, r->m1)) {
140 int rabin_test(rabin *r, mp *g)
143 g = mpmont_mul(&r->mm, MP_NEW, g, r->mm.r2);
144 rc = rabin_rtest(r, g);
149 /* --- @rabin_iters@ --- *
151 * Arguments: @unsigned len@ = number of bits in value
153 * Returns: Number of iterations recommended.
155 * Use: Returns the recommended number of iterations to ensure that a
156 * number with @len@ bits is really prime.
159 int rabin_iters(unsigned len)
161 static const struct {
181 /* --- Binary search through the table --- */
184 q = tab + (sizeof(tab)/sizeof(tab[0]));
189 if (len >= p[i].b && len < p[i + 1].b)
199 /*----- That's all, folks -------------------------------------------------*/