3 * Miller-Rabin primality test
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
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.
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.
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,
28 /*----- Header files ------------------------------------------------------*/
31 #include "mpbarrett.h"
36 /*----- Main code ---------------------------------------------------------*/
38 /* --- @rabin_create@ --- *
40 * Arguments: @rabin *r@ = pointer to Rabin-Miller context
41 * @mp *m@ = pointer to number to test
43 * Returns: Zero on success, nonzero on failure.
45 * Use: Precomputes some useful values for performing the
46 * Miller-Rabin probabilistic primality test.
49 int rabin_create(rabin *r, mp *m)
51 mp *m1 = mp_sub(MP_NEW, m, MP_ONE);
52 if (mpmont_create(&r->mm, m)) {
56 r->r = mp_odd(MP_NEW, m1, &r->s);
57 r->m1 = mp_sub(MP_NEW, m, r->mm.r);
62 /* --- @rabin_destroy@ --- *
64 * Arguments: @rabin *r@ = pointer to Rabin-Miller context
68 * Use: Disposes of a Rabin-Miller context when it's no longer
72 void rabin_destroy(rabin *r)
76 mpmont_destroy(&r->mm);
79 /* --- @rabin_test@, @rabin_rtest@ --- *
81 * Arguments: @rabin *r@ = pointer to Rabin-Miller context
82 * @mp *g@ = base to test the number against
84 * Returns: Either @PGEN_FAIL@ if the test failed, or @PGEN_PASS@
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.
92 int rabin_rtest(rabin *r, mp *g)
95 mp *dd, *spare = MP_NEW;
99 /* --- Calculate %$y R = g^r R \bmod m$% --- *
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$%.
105 y = mpmont_expr(&r->mm, MP_NEW, g, r->r);
106 if (MP_EQ(y, r->mm.r) || MP_EQ(y, r->m1)) {
111 /* --- Now for the main loop --- *
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.
117 for (j = 1; j < r->s; j++) {
118 dd = mp_sqr(spare, y);
119 dd = mpmont_reduce(&r->mm, dd, dd);
121 if (MP_EQ(y, r->mm.r))
123 if (MP_EQ(y, r->m1)) {
138 int rabin_test(rabin *r, mp *g)
141 g = mpmont_mul(&r->mm, MP_NEW, g, r->mm.r2);
142 rc = rabin_rtest(r, g);
147 /* --- @rabin_iters@ --- *
149 * Arguments: @unsigned len@ = number of bits in value
151 * Returns: Number of iterations recommended.
153 * Use: Returns the recommended number of iterations to ensure that a
154 * number with @len@ bits is really prime.
157 int rabin_iters(unsigned len)
159 static const struct {
179 /* --- Binary search through the table --- */
182 q = tab + (sizeof(tab)/sizeof(tab[0]));
187 if (len >= p[i].b && len < p[i + 1].b)
197 /*----- That's all, folks -------------------------------------------------*/