3 * $Id: pgen.c,v 1.3 1999/12/10 23:28:35 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.3 1999/12/10 23:28:35 mdw
34 * Track suggested destination changes.
36 * Revision 1.2 1999/11/20 22:23:05 mdw
37 * Add multiply-and-add function for Diffie-Hellman safe prime generation.
39 * Revision 1.1 1999/11/19 13:17:57 mdw
40 * Prime number generator and tester.
44 /*----- Header files ------------------------------------------------------*/
52 /*----- Main code ---------------------------------------------------------*/
54 /* --- @pgen_create@ --- *
56 * Arguments: @pgen *p@ = pointer to prime generation context
57 * @mp *m@ = pointer to initial number to test
59 * Returns: One of the @PGEN@ constants above.
61 * Use: Tests an initial number for primality by computing its
62 * residue modulo various small prime numbers. This is fairly
63 * quick, but not particularly certain. If a @PGEN_MAYBE@
64 * result is returned, perform Rabin-Miller tests to confirm.
67 int pgen_create(pgen
*p
, mp
*m
)
75 /* --- Take a copy of the number --- */
80 /* --- Fill in the residues --- */
82 mp_build(&q
, &qw
, &qw
+ 1);
83 for (i
= 0; i
< NPRIME
; i
++) {
87 if (!p
->r
[i
] && rc
== PGEN_MAYBE
) {
88 if (MP_LEN(m
) == 1 && m
->v
[0] == ptab
[i
])
101 /* --- @pgen_destroy@ --- *
103 * Arguments: @pgen *p@ = pointer to prime generation context
107 * Use: Discards a context and all the resources it holds.
110 void pgen_destroy(pgen
*p
)
115 /* --- @pgen_step@ --- *
117 * Arguments: @pgen *p@ = pointer to prime generation context
118 * @mpw step@ = how much to step the number
120 * Returns: One of the @PGEN@ constants above.
122 * Use: Steps a number by a small amount. Stepping is much faster
123 * than initializing with a new number. The test performed is
124 * the same simple one used by @ptab_create@, so @PGEN_MAYBE@
125 * results should be followed up by a Rabin-Miller test.
128 int pgen_step(pgen
*p
, mpw step
)
133 /* --- Add the step on to the number --- */
135 p
->m
= mp_split(p
->m
);
136 mp_ensure(p
->m
, MP_LEN(p
->m
) + 1);
137 mpx_uaddn(p
->m
->v
, p
->m
->vl
, step
);
140 /* --- Update the residue table --- */
142 for (i
= 0; i
< NPRIME
; i
++) {
143 p
->r
[i
] = (p
->r
[i
] + step
) % ptab
[i
];
144 if (!p
->r
[i
] && rc
== PGEN_MAYBE
) {
145 if (MP_LEN(p
->m
) == 1 && p
->m
->v
[0] == ptab
[i
])
152 /* --- Small numbers must be prime --- */
154 if (rc
== PGEN_MAYBE
&& MP_LEN(p
->m
) == 1 &&
155 p
->m
->v
[0] < MAXPRIME
* MAXPRIME
)
163 /* --- @pgen_muladd@ --- *
165 * Arguments: @pgen *p@ = destination prime generation context
166 * @const pgen *q@ = source prime generation context
167 * @mpw m@ = number to multiply by
168 * @mpw a@ = number to add
170 * Returns: One of the @PGEN@ constants above.
172 * Use: Multiplies the number in a prime generation context by a
173 * small value and then adds a small value. The destination
174 * should either be uninitialized or the same as the source.
176 * Common things to do include multiplying by 2 and adding 0 to
177 * turn a prime into a jump for finding other primes with @q@ as
178 * a factor of @p - 1@, or multiplying by 2 and adding 1.
181 int pgen_muladd(pgen
*p
, const pgen
*q
, mpw m
, mpw a
)
186 /* --- Multiply the big number --- */
189 mp
*d
= mp_create(MP_LEN(q
->m
) + 2);
190 mpx_umuln(d
->v
, d
->vl
, q
->m
->v
, q
->m
->vl
, m
);
191 mpx_uaddn(d
->v
, d
->vl
, a
);
199 /* --- Gallivant through the residue table --- */
201 for (i
= 0; i
< NPRIME
; i
++) {
202 p
->r
[i
] = (q
->r
[i
] * m
+ a
) % ptab
[i
];
203 if (!p
->r
[i
] && rc
== PGEN_MAYBE
) {
204 if (MP_LEN(p
->m
) == 1 && p
->m
->v
[0] == ptab
[i
])
211 /* --- Small numbers must be prime --- */
213 if (rc
== PGEN_MAYBE
&& MP_LEN(p
->m
) == 1 &&
214 p
->m
->v
[0] < MAXPRIME
* MAXPRIME
)
217 /* --- Finished --- */
223 /* --- @pgen_jump@ --- *
225 * Arguments: @pgen *p@ = pointer to prime generation context
226 * @pgen *j@ = pointer to another generation context
228 * Returns: One of the @PGEN@ constants above.
230 * Use: Steps a number by a large amount. Even so, jumping is much
231 * faster than initializing a new number. The test peformed is
232 * the same simple one used by @ptab_create@, so @PGEN_MAYBE@
233 * results should be followed up by a Rabin-Miller test.
235 * Note that the number stored in the @j@ context is probably
236 * better off being even than prime. The important thing is
237 * that all of the residues for the number have already been
241 int pgen_jump(pgen
*p
, pgen
*j
)
246 /* --- Add the step on --- */
248 p
->m
= mp_add(p
->m
, p
->m
, j
->m
);
250 /* --- Update the residue table --- */
252 for (i
= 0; i
< NPRIME
; i
++) {
253 p
->r
[i
] = p
->r
[i
] + j
->r
[i
];
254 if (p
->r
[i
] > ptab
[i
])
256 if (!p
->r
[i
] && rc
== PGEN_MAYBE
) {
257 if (MP_LEN(p
->m
) == 1 && p
->m
->v
[0] == ptab
[i
])
264 /* --- Small numbers must be prime --- */
266 if (rc
== PGEN_MAYBE
&& MP_LEN(p
->m
) == 1 &&
267 p
->m
->v
[0] < MAXPRIME
* MAXPRIME
)
275 /*----- Test code ---------------------------------------------------------*/
279 #include <mLib/testrig.h>
281 static int verify(dstr
*v
)
283 mp
*m
= *(mp
**)v
[0].buf
;
284 mp
*r
= *(mp
**)v
[1].buf
;
289 static char baton
[] = "/-\\|";
293 mp_build(&g
, &gw
, &gw
+ 1);
294 rc
= pgen_create(&p
, m
);
296 if (rc
== PGEN_PRIME
)
298 if (rc
== PGEN_MAYBE
) {
301 rabin_create(&r
, p
.m
);
302 for (i
= 0; i
< 5; i
++) {
304 if (rabin_test(&r
, &g
) == PGEN_COMPOSITE
)
315 rc
= pgen_step(&p
, 2);
318 if (MP_CMP(p
.m
, !=, r
)) {
319 fputs("\n*** failed.", stderr
);
320 fputs("\nm = ", stderr
); mp_writefile(m
, stderr
, 10);
321 fputs("\np = ", stderr
); mp_writefile(p
.m
, stderr
, 10);
322 fputs("\nr = ", stderr
); mp_writefile(r
, stderr
, 10);
330 assert(mparena_count(MPARENA_GLOBAL
) == 0);
334 static test_chunk tests
[] = {
335 { "pgen", verify
, { &type_mp
, &type_mp
, 0 } },
339 int main(int argc
, char *argv
[])
342 test_run(argc
, argv
, tests
, SRCDIR
"/tests/pgen");
348 /*----- That's all, folks -------------------------------------------------*/