3 * $Id: pfilt.c,v 1.2 2000/06/17 11:54:27 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.2 2000/06/17 11:54:27 mdw
34 * Use new MP memory management functions.
36 * Revision 1.1 1999/12/22 15:49:39 mdw
37 * Renamed from `pgen'. Reworking for new prime-search system.
39 * Revision 1.3 1999/12/10 23:28:35 mdw
40 * Track suggested destination changes.
42 * Revision 1.2 1999/11/20 22:23:05 mdw
43 * Add multiply-and-add function for Diffie-Hellman safe prime generation.
45 * Revision 1.1 1999/11/19 13:17:57 mdw
46 * Prime number generator and tester.
50 /*----- Header files ------------------------------------------------------*/
58 /*----- Main code ---------------------------------------------------------*/
60 /* --- @pfilt_create@ --- *
62 * Arguments: @pfilt *p@ = pointer to prime filtering context
63 * @mp *m@ = pointer to initial number to test
65 * Returns: One of the @PGEN@ result codes.
67 * Use: Tests an initial number for primality by computing its
68 * residue modulo various small prime numbers. This is fairly
69 * quick, but not particularly certain. If a @PGEN_TRY@
70 * result is returned, perform Rabin-Miller tests to confirm.
73 int pfilt_create(pfilt
*p
, mp
*m
)
81 /* --- Take a copy of the number --- */
86 /* --- Fill in the residues --- */
88 mp_build(&q
, &qw
, &qw
+ 1);
89 for (i
= 0; i
< NPRIME
; i
++) {
93 if (!p
->r
[i
] && rc
== PGEN_TRY
) {
94 if (MP_LEN(m
) == 1 && m
->v
[0] == primetab
[i
])
107 /* --- @pfilt_destroy@ --- *
109 * Arguments: @pfilt *p@ = pointer to prime filtering context
113 * Use: Discards a context and all the resources it holds.
116 void pfilt_destroy(pfilt
*p
)
121 /* --- @pfilt_step@ --- *
123 * Arguments: @pfilt *p@ = pointer to prime filtering context
124 * @mpw step@ = how much to step the number
126 * Returns: One of the @PGEN@ result codes.
128 * Use: Steps a number by a small amount. Stepping is much faster
129 * than initializing with a new number. The test performed is
130 * the same simple one used by @primetab_create@, so @PGEN_TRY@
131 * results should be followed up by a Rabin-Miller test.
134 int pfilt_step(pfilt
*p
, mpw step
)
139 /* --- Add the step on to the number --- */
141 p
->m
= mp_split(p
->m
);
142 mp_ensure(p
->m
, MP_LEN(p
->m
) + 1);
143 mpx_uaddn(p
->m
->v
, p
->m
->vl
, step
);
146 /* --- Update the residue table --- */
148 for (i
= 0; i
< NPRIME
; i
++) {
149 p
->r
[i
] = (p
->r
[i
] + step
) % primetab
[i
];
150 if (!p
->r
[i
] && rc
== PGEN_TRY
) {
151 if (MP_LEN(p
->m
) == 1 && p
->m
->v
[0] == primetab
[i
])
158 /* --- Small numbers must be prime --- */
160 if (rc
== PGEN_TRY
&& MP_LEN(p
->m
) == 1 &&
161 p
->m
->v
[0] < MAXPRIME
* MAXPRIME
)
169 /* --- @pfilt_muladd@ --- *
171 * Arguments: @pfilt *p@ = destination prime filtering context
172 * @const pfilt *q@ = source prime filtering context
173 * @mpw m@ = number to multiply by
174 * @mpw a@ = number to add
176 * Returns: One of the @PGEN@ result codes.
178 * Use: Multiplies the number in a prime filtering context by a
179 * small value and then adds a small value. The destination
180 * should either be uninitialized or the same as the source.
182 * Common things to do include multiplying by 2 and adding 0 to
183 * turn a prime into a jump for finding other primes with @q@ as
184 * a factor of @p - 1@, or multiplying by 2 and adding 1.
187 int pfilt_muladd(pfilt
*p
, const pfilt
*q
, mpw m
, mpw a
)
192 /* --- Multiply the big number --- */
195 mp
*d
= mp_new(MP_LEN(q
->m
) + 2, q
->m
->f
);
196 mpx_umuln(d
->v
, d
->vl
, q
->m
->v
, q
->m
->vl
, m
);
197 mpx_uaddn(d
->v
, d
->vl
, a
);
204 /* --- Gallivant through the residue table --- */
206 for (i
= 0; i
< NPRIME
; i
++) {
207 p
->r
[i
] = (q
->r
[i
] * m
+ a
) % primetab
[i
];
208 if (!p
->r
[i
] && rc
== PGEN_TRY
) {
209 if (MP_LEN(p
->m
) == 1 && p
->m
->v
[0] == primetab
[i
])
216 /* --- Small numbers must be prime --- */
218 if (rc
== PGEN_TRY
&& MP_LEN(p
->m
) == 1 &&
219 p
->m
->v
[0] < MAXPRIME
* MAXPRIME
)
222 /* --- Finished --- */
227 /* --- @pfilt_jump@ --- *
229 * Arguments: @pfilt *p@ = pointer to prime filtering context
230 * @const pfilt *j@ = pointer to another filtering context
232 * Returns: One of the @PGEN@ result codes.
234 * Use: Steps a number by a large amount. Even so, jumping is much
235 * faster than initializing a new number. The test peformed is
236 * the same simple one used by @primetab_create@, so @PGEN_TRY@
237 * results should be followed up by a Rabin-Miller test.
239 * Note that the number stored in the @j@ context is probably
240 * better off being even than prime. The important thing is
241 * that all of the residues for the number have already been
245 int pfilt_jump(pfilt
*p
, const pfilt
*j
)
250 /* --- Add the step on --- */
252 p
->m
= mp_add(p
->m
, p
->m
, j
->m
);
254 /* --- Update the residue table --- */
256 for (i
= 0; i
< NPRIME
; i
++) {
257 p
->r
[i
] = p
->r
[i
] + j
->r
[i
];
258 if (p
->r
[i
] > primetab
[i
])
259 p
->r
[i
] -= primetab
[i
];
260 if (!p
->r
[i
] && rc
== PGEN_TRY
) {
261 if (MP_LEN(p
->m
) == 1 && p
->m
->v
[0] == primetab
[i
])
268 /* --- Small numbers must be prime --- */
270 if (rc
== PGEN_TRY
&& MP_LEN(p
->m
) == 1 &&
271 p
->m
->v
[0] < MAXPRIME
* MAXPRIME
)
279 /*----- That's all, folks -------------------------------------------------*/