3 * $Id: rabin.c,v 1.7 2002/01/13 13:42:53 mdw Exp $
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 /*----- Revision history --------------------------------------------------*
33 * Revision 1.7 2002/01/13 13:42:53 mdw
34 * More efficient Rabin-Miller test: with random witnesses, skip redundant
35 * Montgomerization. (Being bijective, it can't affect the distribution.)
37 * Revision 1.6 2001/06/16 12:56:38 mdw
38 * Fixes for interface change to @mpmont_expr@ and @mpmont_mexpr@.
40 * Revision 1.5 2000/10/08 12:11:22 mdw
41 * Use @MP_EQ@ instead of @MP_CMP@.
43 * Revision 1.4 2000/06/22 19:03:02 mdw
44 * Use the new @mp_odd@ function.
46 * Revision 1.3 1999/12/22 15:50:29 mdw
47 * Reworking for new prime-search system. Add function for working out how
48 * many iterations to use for a particular number.
50 * Revision 1.2 1999/12/10 23:29:48 mdw
51 * Change header file guard names.
53 * Revision 1.1 1999/11/19 13:17:57 mdw
54 * Prime number generator and tester.
58 /*----- Header files ------------------------------------------------------*/
61 #include "mpbarrett.h"
66 /*----- Main code ---------------------------------------------------------*/
68 /* --- @rabin_create@ --- *
70 * Arguments: @rabin *r@ = pointer to Rabin-Miller context
71 * @mp *m@ = pointer to number to test
75 * Use: Precomputes some useful values for performing the
76 * Miller-Rabin probabilistic primality test.
79 void rabin_create(rabin
*r
, mp
*m
)
81 mp
*m1
= mp_sub(MP_NEW
, m
, MP_ONE
);
82 mpmont_create(&r
->mm
, m
);
83 r
->r
= mp_odd(MP_NEW
, m1
, &r
->s
);
84 r
->m1
= mp_sub(MP_NEW
, m
, r
->mm
.r
);
88 /* --- @rabin_destroy@ --- *
90 * Arguments: @rabin *r@ = pointer to Rabin-Miller context
94 * Use: Disposes of a Rabin-Miller context when it's no longer
98 void rabin_destroy(rabin
*r
)
102 mpmont_destroy(&r
->mm
);
105 /* --- @rabin_test@, @rabin_rtest@ --- *
107 * Arguments: @rabin *r@ = pointer to Rabin-Miller context
108 * @mp *g@ = base to test the number against
110 * Returns: Either @PGEN_FAIL@ if the test failed, or @PGEN_PASS@
113 * Use: Performs a single iteration of the Rabin-Miller primality
114 * test. The @rtest@ variant assumes that %$g$% is either
115 * already in Montgomery representation, or you don't care.
118 int rabin_rtest(rabin
*r
, mp
*g
)
121 mp
*dd
, *spare
= MP_NEW
;
125 /* --- Calculate %$y R = g^r R \bmod m$% --- *
127 * If %$y = 1$% or %$y = m - 1$% then %$m$% is prime. If course, note that
128 * @y@ here has an extra factor of %$R$%.
131 y
= mpmont_expr(&r
->mm
, MP_NEW
, g
, r
->r
);
132 if (MP_EQ(y
, r
->mm
.r
) || MP_EQ(y
, r
->m1
)) {
137 /* --- Now for the main loop --- *
139 * If %$y^{2^j} \ne m - 1$% for any %$0 \le j < s$% then %$m$% is
140 * composite. Of course, %$j = 0$% has already been tested.
143 for (j
= 1; j
< r
->s
; j
++) {
144 dd
= mp_sqr(spare
, y
);
145 dd
= mpmont_reduce(&r
->mm
, dd
, dd
);
147 if (MP_EQ(y
, r
->mm
.r
))
149 if (MP_EQ(y
, r
->m1
)) {
164 int rabin_test(rabin
*r
, mp
*g
)
167 g
= mpmont_mul(&r
->mm
, MP_NEW
, g
, r
->mm
.r2
);
168 rc
= rabin_rtest(r
, g
);
173 /* --- @rabin_iters@ --- *
175 * Arguments: @unsigned len@ = number of bits in value
177 * Returns: Number of iterations recommended.
179 * Use: Returns the recommended number of iterations to ensure that a
180 * number with @len@ bits is really prime.
183 int rabin_iters(unsigned len
)
205 /* --- Binary search through the table --- */
208 q
= tab
+ (sizeof(tab
)/sizeof(tab
[0]));
213 if (len
>= p
[i
].b
&& len
< p
[i
+ 1].b
)
223 /*----- That's all, folks -------------------------------------------------*/