0f5ec153 |
1 | /* -*-c-*- |
2 | * |
3 | * $Id: rabin.c,v 1.1 1999/11/19 13:17:57 mdw Exp $ |
4 | * |
5 | * Miller-Rabin primality test |
6 | * |
7 | * (c) 1999 Straylight/Edgeware |
8 | */ |
9 | |
10 | /*----- Licensing notice --------------------------------------------------* |
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. |
18 | * |
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. |
23 | * |
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 | |
30 | /*----- Revision history --------------------------------------------------* |
31 | * |
32 | * $Log: rabin.c,v $ |
33 | * Revision 1.1 1999/11/19 13:17:57 mdw |
34 | * Prime number generator and tester. |
35 | * |
36 | */ |
37 | |
38 | /*----- Header files ------------------------------------------------------*/ |
39 | |
40 | #include "mp.h" |
41 | #include "mpmont.h" |
42 | #include "pgen.h" |
43 | #include "rabin.h" |
44 | |
45 | /*----- Main code ---------------------------------------------------------*/ |
46 | |
47 | /* --- @rabin_create@ --- * |
48 | * |
49 | * Arguments: @rabin *r@ = pointer to Rabin-Miller context |
50 | * @mp *m@ = pointer to number to test |
51 | * |
52 | * Returns: --- |
53 | * |
54 | * Use: Precomputes some useful values for performing the |
55 | * Miller-Rabin probabilistic primality test. |
56 | */ |
57 | |
58 | void rabin_create(rabin *r, mp *m) |
59 | { |
60 | mp *m1 = mp_sub(MP_NEW, m, MP_ONE); |
61 | mpscan sc; |
62 | size_t s; |
63 | |
64 | /* --- Find @r@ and @s@ --- */ |
65 | |
66 | mpmont_create(&r->mm, m); |
67 | mp_scan(&sc, m1); |
68 | s = 0; |
69 | while (mp_step(&sc)) { |
70 | if (mp_bit(&sc)) |
71 | break; |
72 | s++; |
73 | } |
74 | r->s = s; |
75 | r->r = mp_lsr(MP_NEW, m1, s); |
76 | |
77 | /* --- Compute %$(m - 1)R \bmod m$% --- */ |
78 | |
79 | r->m1 = mpmont_mul(&r->mm, MP_NEW, m1, r->mm.r2); |
80 | mp_drop(m1); |
81 | } |
82 | |
83 | /* --- @rabin_destroy@ --- * |
84 | * |
85 | * Arguments: @rabin *r@ = pointer to Rabin-Miller context |
86 | * |
87 | * Returns: --- |
88 | * |
89 | * Use: Disposes of a Rabin-Miller context when it's no longer |
90 | * needed. |
91 | */ |
92 | |
93 | void rabin_destroy(rabin *r) |
94 | { |
95 | mp_drop(r->r); |
96 | mp_drop(r->m1); |
97 | mpmont_destroy(&r->mm); |
98 | } |
99 | |
100 | /* --- @rabin_test@ --- * |
101 | * |
102 | * Arguments: @rabin *r@ = pointer to Rabin-Miller context |
103 | * @mp *g@ = base to test the number against |
104 | * |
105 | * Returns: Either @PGEN_COMPOSITE@ if the test failed, or @PGEN_MAYBE@ |
106 | * if it succeeded. |
107 | * |
108 | * Use: Performs a single iteration of the Rabin-Miller primality |
109 | * test. |
110 | */ |
111 | |
112 | int rabin_test(rabin *r, mp *g) |
113 | { |
114 | mp *y; |
115 | mp *dd, *spare = MP_NEW; |
116 | size_t j; |
117 | int rc = PGEN_COMPOSITE; |
118 | |
119 | /* --- Calculate %$y R = g^r R \bmod m$% --- * |
120 | * |
121 | * If %$y = 1$% or %$y = m - 1$% then %$m$% is prime. If course, note that |
122 | * @y@ here has an extra factor of %$R$%. |
123 | */ |
124 | |
125 | y = mpmont_expr(&r->mm, g, r->r); |
126 | if (MP_CMP(y, ==, r->mm.r) || MP_CMP(y, ==, r->m1)) { |
127 | rc = PGEN_MAYBE; |
128 | goto done; |
129 | } |
130 | |
131 | /* --- Now for the main loop --- * |
132 | * |
133 | * If %$y^{2^j} \ne m - 1$% for any %$0 \le j < s$% then %$m$% is |
134 | * composite. Of course, %$j = 0$% has already been tested. |
135 | */ |
136 | |
137 | for (j = 1; j < r->s; j++) { |
138 | dd = mpmont_mul(&r->mm, spare, y, y); spare = y; y = dd; |
139 | if (MP_CMP(y, ==, r->mm.r)) |
140 | break; |
141 | if (MP_CMP(y, ==, r->m1)) { |
142 | rc = PGEN_MAYBE; |
143 | break; |
144 | } |
145 | } |
146 | |
147 | /* --- Done --- */ |
148 | |
149 | done: |
150 | if (spare != MP_NEW) |
151 | MP_DROP(spare); |
152 | MP_DROP(y); |
153 | return (rc); |
154 | } |
155 | |
156 | /*----- That's all, folks -------------------------------------------------*/ |