3 * Finding and testing prime numbers
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
36 /*----- Main code ---------------------------------------------------------*/
38 /* --- @smallenough@ --- *
40 * Arguments: @mp *m@ = integer to test
42 * Returns: One of the @PGEN@ result codes.
44 * Use: Assuming that @m@ has been tested by trial division on every
45 * prime in the small-primes array, this function will return
46 * @PGEN_DONE@ if the number is less than the square of the
47 * largest small prime.
50 static int smallenough(mp
*m
)
56 max
= mp_fromuint(MP_NEW
, MAXPRIME
);
57 max
= mp_sqr(max
, max
);
58 max
->a
->n
--; /* Permanent allocation */
60 if (MP_CMP(m
, <=, MP_ONE
))
62 else if (MP_CMP(m
, <, max
))
67 /* --- @pfilt_smallfactor@ --- *
69 * Arguments: @mp *m@ = integer to test
71 * Returns: One of the @PGEN@ result codes.
73 * Use: Tests a number by dividing by a number of small primes. This
74 * is a useful first step if you're testing random primes; for
75 * sequential searches, @pfilt_create@ works better.
78 int pfilt_smallfactor(mp
*m
)
82 size_t sz
= MP_LEN(m
);
83 mparena
*a
= m
->a ? m
->a
: MPARENA_GLOBAL
;
84 mpw
*v
= mpalloc(a
, sz
);
86 /* --- Fill in the residues --- */
88 for (i
= 0; i
< NPRIME
; i
++) {
89 if (!mpx_udivn(v
, v
+ sz
, m
->v
, m
->vl
, primetab
[i
])) {
90 if (MP_LEN(m
) == 1 && m
->v
[0] == primetab
[i
])
98 /* --- Check for small primes --- */
109 /* --- @pfilt_create@ --- *
111 * Arguments: @pfilt *p@ = pointer to prime filtering context
112 * @mp *m@ = pointer to initial number to test
114 * Returns: One of the @PGEN@ result codes.
116 * Use: Tests an initial number for primality by computing its
117 * residue modulo various small prime numbers. This is fairly
118 * quick, but not particularly certain. If a @PGEN_TRY@
119 * result is returned, perform Rabin-Miller tests to confirm.
122 int pfilt_create(pfilt
*p
, mp
*m
)
126 size_t sz
= MP_LEN(m
);
127 mparena
*a
= m
->a ? m
->a
: MPARENA_GLOBAL
;
128 mpw
*v
= mpalloc(a
, sz
);
130 /* --- Take a copy of the number --- */
135 /* --- Fill in the residues --- */
137 for (i
= 0; i
< NPRIME
; i
++) {
138 p
->r
[i
] = mpx_udivn(v
, v
+ sz
, m
->v
, m
->vl
, primetab
[i
]);
139 if (!p
->r
[i
] && rc
== PGEN_TRY
) {
140 if (MP_LEN(m
) == 1 && m
->v
[0] == primetab
[i
])
147 /* --- Check for small primes --- */
158 /* --- @pfilt_destroy@ --- *
160 * Arguments: @pfilt *p@ = pointer to prime filtering context
164 * Use: Discards a context and all the resources it holds.
167 void pfilt_destroy(pfilt
*p
)
172 /* --- @pfilt_step@ --- *
174 * Arguments: @pfilt *p@ = pointer to prime filtering context
175 * @mpw step@ = how much to step the number
177 * Returns: One of the @PGEN@ result codes.
179 * Use: Steps a number by a small amount. Stepping is much faster
180 * than initializing with a new number. The test performed is
181 * the same simple one used by @primetab_create@, so @PGEN_TRY@
182 * results should be followed up by a Rabin-Miller test.
185 int pfilt_step(pfilt
*p
, mpw step
)
190 /* --- Add the step on to the number --- */
192 p
->m
= mp_split(p
->m
);
193 mp_ensure(p
->m
, MP_LEN(p
->m
) + 1);
194 mpx_uaddn(p
->m
->v
, p
->m
->vl
, step
);
197 /* --- Update the residue table --- */
199 for (i
= 0; i
< NPRIME
; i
++) {
200 p
->r
[i
] = (p
->r
[i
] + step
) % primetab
[i
];
201 if (!p
->r
[i
] && rc
== PGEN_TRY
) {
202 if (MP_LEN(p
->m
) == 1 && p
->m
->v
[0] == primetab
[i
])
209 /* --- Check for small primes --- */
212 rc
= smallenough(p
->m
);
219 /* --- @pfilt_muladd@ --- *
221 * Arguments: @pfilt *p@ = destination prime filtering context
222 * @const pfilt *q@ = source prime filtering context
223 * @mpw m@ = number to multiply by
224 * @mpw a@ = number to add
226 * Returns: One of the @PGEN@ result codes.
228 * Use: Multiplies the number in a prime filtering context by a
229 * small value and then adds a small value. The destination
230 * should either be uninitialized or the same as the source.
232 * Common things to do include multiplying by 2 and adding 0 to
233 * turn a prime into a jump for finding other primes with @q@ as
234 * a factor of @p - 1@, or multiplying by 2 and adding 1.
237 int pfilt_muladd(pfilt
*p
, const pfilt
*q
, mpw m
, mpw a
)
242 /* --- Multiply the big number --- */
245 mp
*d
= mp_new(MP_LEN(q
->m
) + 2, q
->m
->f
);
246 mpx_umuln(d
->v
, d
->vl
, q
->m
->v
, q
->m
->vl
, m
);
247 mpx_uaddn(d
->v
, d
->vl
, a
);
254 /* --- Gallivant through the residue table --- */
256 for (i
= 0; i
< NPRIME
; i
++) {
257 p
->r
[i
] = (q
->r
[i
] * m
+ a
) % primetab
[i
];
258 if (!p
->r
[i
] && rc
== PGEN_TRY
) {
259 if (MP_LEN(p
->m
) == 1 && p
->m
->v
[0] == primetab
[i
])
266 /* --- Check for small primes --- */
269 rc
= smallenough(p
->m
);
271 /* --- Finished --- */
276 /* --- @pfilt_jump@ --- *
278 * Arguments: @pfilt *p@ = pointer to prime filtering context
279 * @const pfilt *j@ = pointer to another filtering context
281 * Returns: One of the @PGEN@ result codes.
283 * Use: Steps a number by a large amount. Even so, jumping is much
284 * faster than initializing a new number. The test peformed is
285 * the same simple one used by @primetab_create@, so @PGEN_TRY@
286 * results should be followed up by a Rabin-Miller test.
288 * Note that the number stored in the @j@ context is probably
289 * better off being even than prime. The important thing is
290 * that all of the residues for the number have already been
294 int pfilt_jump(pfilt
*p
, const pfilt
*j
)
299 /* --- Add the step on --- */
301 p
->m
= mp_add(p
->m
, p
->m
, j
->m
);
303 /* --- Update the residue table --- */
305 for (i
= 0; i
< NPRIME
; i
++) {
306 p
->r
[i
] = p
->r
[i
] + j
->r
[i
];
307 if (p
->r
[i
] > primetab
[i
])
308 p
->r
[i
] -= primetab
[i
];
309 if (!p
->r
[i
] && rc
== PGEN_TRY
) {
310 if (MP_LEN(p
->m
) == 1 && p
->m
->v
[0] == primetab
[i
])
317 /* --- Check for small primes --- */
320 rc
= smallenough(p
->m
);
327 /*----- That's all, folks -------------------------------------------------*/