3 * $Id: pfilt.c,v 1.1 1999/12/22 15:49:39 mdw Exp $
5 * Finding and testing prime numbers
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.1 1999/12/22 15:49:39 mdw
34 * Renamed from `pgen'. Reworking for new prime-search system.
36 * Revision 1.3 1999/12/10 23:28:35 mdw
37 * Track suggested destination changes.
39 * Revision 1.2 1999/11/20 22:23:05 mdw
40 * Add multiply-and-add function for Diffie-Hellman safe prime generation.
42 * Revision 1.1 1999/11/19 13:17:57 mdw
43 * Prime number generator and tester.
47 /*----- Header files ------------------------------------------------------*/
55 /*----- Main code ---------------------------------------------------------*/
57 /* --- @pfilt_create@ --- *
59 * Arguments: @pfilt *p@ = pointer to prime filtering context
60 * @mp *m@ = pointer to initial number to test
62 * Returns: One of the @PGEN@ result codes.
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.
70 int pfilt_create(pfilt
*p
, mp
*m
)
78 /* --- Take a copy of the number --- */
83 /* --- Fill in the residues --- */
85 mp_build(&q
, &qw
, &qw
+ 1);
86 for (i
= 0; i
< NPRIME
; i
++) {
90 if (!p
->r
[i
] && rc
== PGEN_TRY
) {
91 if (MP_LEN(m
) == 1 && m
->v
[0] == primetab
[i
])
104 /* --- @pfilt_destroy@ --- *
106 * Arguments: @pfilt *p@ = pointer to prime filtering context
110 * Use: Discards a context and all the resources it holds.
113 void pfilt_destroy(pfilt
*p
)
118 /* --- @pfilt_step@ --- *
120 * Arguments: @pfilt *p@ = pointer to prime filtering context
121 * @mpw step@ = how much to step the number
123 * Returns: One of the @PGEN@ result codes.
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.
131 int pfilt_step(pfilt
*p
, mpw step
)
136 /* --- Add the step on to the number --- */
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
);
143 /* --- Update the residue table --- */
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
])
155 /* --- Small numbers must be prime --- */
157 if (rc
== PGEN_TRY
&& MP_LEN(p
->m
) == 1 &&
158 p
->m
->v
[0] < MAXPRIME
* MAXPRIME
)
166 /* --- @pfilt_muladd@ --- *
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
173 * Returns: One of the @PGEN@ result codes.
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.
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.
184 int pfilt_muladd(pfilt
*p
, const pfilt
*q
, mpw m
, mpw a
)
189 /* --- Multiply the big number --- */
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
);
202 /* --- Gallivant through the residue table --- */
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
])
214 /* --- Small numbers must be prime --- */
216 if (rc
== PGEN_TRY
&& MP_LEN(p
->m
) == 1 &&
217 p
->m
->v
[0] < MAXPRIME
* MAXPRIME
)
220 /* --- Finished --- */
225 /* --- @pfilt_jump@ --- *
227 * Arguments: @pfilt *p@ = pointer to prime filtering context
228 * @const pfilt *j@ = pointer to another filtering context
230 * Returns: One of the @PGEN@ result codes.
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.
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
243 int pfilt_jump(pfilt
*p
, const pfilt
*j
)
248 /* --- Add the step on --- */
250 p
->m
= mp_add(p
->m
, p
->m
, j
->m
);
252 /* --- Update the residue table --- */
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
])
266 /* --- Small numbers must be prime --- */
268 if (rc
== PGEN_TRY
&& MP_LEN(p
->m
) == 1 &&
269 p
->m
->v
[0] < MAXPRIME
* MAXPRIME
)
277 /*----- That's all, folks -------------------------------------------------*/