9a8b0c8d |
1 | /* -*-c-*- |
2 | * |
3 | * $Id: pfilt.c,v 1.1 1999/12/22 15:49:39 mdw Exp $ |
4 | * |
5 | * Finding and testing prime numbers |
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: pfilt.c,v $ |
33 | * Revision 1.1 1999/12/22 15:49:39 mdw |
34 | * Renamed from `pgen'. Reworking for new prime-search system. |
35 | * |
36 | * Revision 1.3 1999/12/10 23:28:35 mdw |
37 | * Track suggested destination changes. |
38 | * |
39 | * Revision 1.2 1999/11/20 22:23:05 mdw |
40 | * Add multiply-and-add function for Diffie-Hellman safe prime generation. |
41 | * |
42 | * Revision 1.1 1999/11/19 13:17:57 mdw |
43 | * Prime number generator and tester. |
44 | * |
45 | */ |
46 | |
47 | /*----- Header files ------------------------------------------------------*/ |
48 | |
49 | #include "mp.h" |
50 | #include "mpmont.h" |
51 | #include "pfilt.h" |
52 | #include "pgen.h" |
53 | #include "primetab.h" |
54 | |
55 | /*----- Main code ---------------------------------------------------------*/ |
56 | |
57 | /* --- @pfilt_create@ --- * |
58 | * |
59 | * Arguments: @pfilt *p@ = pointer to prime filtering context |
60 | * @mp *m@ = pointer to initial number to test |
61 | * |
62 | * Returns: One of the @PGEN@ result codes. |
63 | * |
64 | * Use: Tests an initial number for primality by computing its |
65 | * residue modulo various small prime numbers. This is fairly |
66 | * quick, but not particularly certain. If a @PGEN_TRY@ |
67 | * result is returned, perform Rabin-Miller tests to confirm. |
68 | */ |
69 | |
70 | int pfilt_create(pfilt *p, mp *m) |
71 | { |
72 | int rc = PGEN_TRY; |
73 | int i; |
74 | mp *r = MP_NEW; |
75 | mpw qw; |
76 | mp q; |
77 | |
78 | /* --- Take a copy of the number --- */ |
79 | |
80 | mp_shrink(m); |
81 | p->m = MP_COPY(m); |
82 | |
83 | /* --- Fill in the residues --- */ |
84 | |
85 | mp_build(&q, &qw, &qw + 1); |
86 | for (i = 0; i < NPRIME; i++) { |
87 | qw = primetab[i]; |
88 | mp_div(0, &r, m, &q); |
89 | p->r[i] = r->v[0]; |
90 | if (!p->r[i] && rc == PGEN_TRY) { |
91 | if (MP_LEN(m) == 1 && m->v[0] == primetab[i]) |
92 | rc = PGEN_DONE; |
93 | else |
94 | rc = PGEN_FAIL; |
95 | } |
96 | } |
97 | |
98 | /* --- Done --- */ |
99 | |
100 | mp_drop(r); |
101 | return (rc); |
102 | } |
103 | |
104 | /* --- @pfilt_destroy@ --- * |
105 | * |
106 | * Arguments: @pfilt *p@ = pointer to prime filtering context |
107 | * |
108 | * Returns: --- |
109 | * |
110 | * Use: Discards a context and all the resources it holds. |
111 | */ |
112 | |
113 | void pfilt_destroy(pfilt *p) |
114 | { |
115 | mp_drop(p->m); |
116 | } |
117 | |
118 | /* --- @pfilt_step@ --- * |
119 | * |
120 | * Arguments: @pfilt *p@ = pointer to prime filtering context |
121 | * @mpw step@ = how much to step the number |
122 | * |
123 | * Returns: One of the @PGEN@ result codes. |
124 | * |
125 | * Use: Steps a number by a small amount. Stepping is much faster |
126 | * than initializing with a new number. The test performed is |
127 | * the same simple one used by @primetab_create@, so @PGEN_TRY@ |
128 | * results should be followed up by a Rabin-Miller test. |
129 | */ |
130 | |
131 | int pfilt_step(pfilt *p, mpw step) |
132 | { |
133 | int rc = PGEN_TRY; |
134 | int i; |
135 | |
136 | /* --- Add the step on to the number --- */ |
137 | |
138 | p->m = mp_split(p->m); |
139 | mp_ensure(p->m, MP_LEN(p->m) + 1); |
140 | mpx_uaddn(p->m->v, p->m->vl, step); |
141 | mp_shrink(p->m); |
142 | |
143 | /* --- Update the residue table --- */ |
144 | |
145 | for (i = 0; i < NPRIME; i++) { |
146 | p->r[i] = (p->r[i] + step) % primetab[i]; |
147 | if (!p->r[i] && rc == PGEN_TRY) { |
148 | if (MP_LEN(p->m) == 1 && p->m->v[0] == primetab[i]) |
149 | rc = PGEN_DONE; |
150 | else |
151 | rc = PGEN_FAIL; |
152 | } |
153 | } |
154 | |
155 | /* --- Small numbers must be prime --- */ |
156 | |
157 | if (rc == PGEN_TRY && MP_LEN(p->m) == 1 && |
158 | p->m->v[0] < MAXPRIME * MAXPRIME) |
159 | rc = PGEN_DONE; |
160 | |
161 | /* --- Done --- */ |
162 | |
163 | return (rc); |
164 | } |
165 | |
166 | /* --- @pfilt_muladd@ --- * |
167 | * |
168 | * Arguments: @pfilt *p@ = destination prime filtering context |
169 | * @const pfilt *q@ = source prime filtering context |
170 | * @mpw m@ = number to multiply by |
171 | * @mpw a@ = number to add |
172 | * |
173 | * Returns: One of the @PGEN@ result codes. |
174 | * |
175 | * Use: Multiplies the number in a prime filtering context by a |
176 | * small value and then adds a small value. The destination |
177 | * should either be uninitialized or the same as the source. |
178 | * |
179 | * Common things to do include multiplying by 2 and adding 0 to |
180 | * turn a prime into a jump for finding other primes with @q@ as |
181 | * a factor of @p - 1@, or multiplying by 2 and adding 1. |
182 | */ |
183 | |
184 | int pfilt_muladd(pfilt *p, const pfilt *q, mpw m, mpw a) |
185 | { |
186 | int rc = PGEN_TRY; |
187 | int i; |
188 | |
189 | /* --- Multiply the big number --- */ |
190 | |
191 | { |
192 | mp *d = mp_create(MP_LEN(q->m) + 2); |
193 | mpx_umuln(d->v, d->vl, q->m->v, q->m->vl, m); |
194 | mpx_uaddn(d->v, d->vl, a); |
195 | d->f = q->m->f; |
196 | if (p == q) |
197 | mp_drop(p->m); |
198 | mp_shrink(d); |
199 | p->m = d; |
200 | } |
201 | |
202 | /* --- Gallivant through the residue table --- */ |
203 | |
204 | for (i = 0; i < NPRIME; i++) { |
205 | p->r[i] = (q->r[i] * m + a) % primetab[i]; |
206 | if (!p->r[i] && rc == PGEN_TRY) { |
207 | if (MP_LEN(p->m) == 1 && p->m->v[0] == primetab[i]) |
208 | rc = PGEN_DONE; |
209 | else |
210 | rc = PGEN_FAIL; |
211 | } |
212 | } |
213 | |
214 | /* --- Small numbers must be prime --- */ |
215 | |
216 | if (rc == PGEN_TRY && MP_LEN(p->m) == 1 && |
217 | p->m->v[0] < MAXPRIME * MAXPRIME) |
218 | rc = PGEN_DONE; |
219 | |
220 | /* --- Finished --- */ |
221 | |
222 | return (rc); |
223 | } |
224 | |
225 | /* --- @pfilt_jump@ --- * |
226 | * |
227 | * Arguments: @pfilt *p@ = pointer to prime filtering context |
228 | * @const pfilt *j@ = pointer to another filtering context |
229 | * |
230 | * Returns: One of the @PGEN@ result codes. |
231 | * |
232 | * Use: Steps a number by a large amount. Even so, jumping is much |
233 | * faster than initializing a new number. The test peformed is |
234 | * the same simple one used by @primetab_create@, so @PGEN_TRY@ |
235 | * results should be followed up by a Rabin-Miller test. |
236 | * |
237 | * Note that the number stored in the @j@ context is probably |
238 | * better off being even than prime. The important thing is |
239 | * that all of the residues for the number have already been |
240 | * computed. |
241 | */ |
242 | |
243 | int pfilt_jump(pfilt *p, const pfilt *j) |
244 | { |
245 | int rc = PGEN_TRY; |
246 | int i; |
247 | |
248 | /* --- Add the step on --- */ |
249 | |
250 | p->m = mp_add(p->m, p->m, j->m); |
251 | |
252 | /* --- Update the residue table --- */ |
253 | |
254 | for (i = 0; i < NPRIME; i++) { |
255 | p->r[i] = p->r[i] + j->r[i]; |
256 | if (p->r[i] > primetab[i]) |
257 | p->r[i] -= primetab[i]; |
258 | if (!p->r[i] && rc == PGEN_TRY) { |
259 | if (MP_LEN(p->m) == 1 && p->m->v[0] == primetab[i]) |
260 | rc = PGEN_DONE; |
261 | else |
262 | rc = PGEN_FAIL; |
263 | } |
264 | } |
265 | |
266 | /* --- Small numbers must be prime --- */ |
267 | |
268 | if (rc == PGEN_TRY && MP_LEN(p->m) == 1 && |
269 | p->m->v[0] < MAXPRIME * MAXPRIME) |
270 | rc = PGEN_DONE; |
271 | |
272 | /* --- Done --- */ |
273 | |
274 | return (rc); |
275 | } |
276 | |
277 | /*----- That's all, folks -------------------------------------------------*/ |