-/* $NetBSD: factor.c,v 1.13 2002/06/18 23:07:36 simonb Exp $ */
+/* $NetBSD: factor.c,v 1.17 2007/12/15 19:44:40 perry Exp $ */
/*
* Copyright (c) 1989, 1993
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- * must display the following acknowledgement:
- * This product includes software developed by the University of
- * California, Berkeley and its contributors.
- * 4. Neither the name of the University nor the names of its contributors
+ * 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
#if 0
static char sccsid[] = "@(#)factor.c 8.4 (Berkeley) 5/4/95";
#else
-__RCSID("$NetBSD: factor.c,v 1.13 2002/06/18 23:07:36 simonb Exp $");
+__RCSID("$NetBSD: factor.c,v 1.17 2007/12/15 19:44:40 perry Exp $");
#endif
#endif /* not lint */
*
* number: factor1 factor1 factor2 factor3 factor3 factor3 ...
*
- * where factor1 < factor2 < factor3 < ...
+ * where factor1 <= factor2 <= factor3 <= ...
*
* If no args are given, the list of numbers are read from stdin.
*/
int main(int, char *[]);
void pr_fact(BIGNUM *); /* print factors of a value */
void BN_print_dec_fp(FILE *, const BIGNUM *);
-void usage(void) __attribute__((__noreturn__));
+void usage(void) __dead;
#ifdef HAVE_OPENSSL
void pollard_pminus1(BIGNUM *); /* print factors for big numbers */
#else
bnfact = BN_new();
BN_set_word(bnfact, *(fact - 1));
BN_sqr(bnfact, bnfact, ctx);
- if (BN_cmp(bnfact, val) > 0) {
+ if (BN_cmp(bnfact, val) > 0
+ || BN_is_prime(val, PRIME_CHECKS, NULL, NULL,
+ NULL) == 1) {
putchar(' ');
BN_print_dec_fp(stdout, val);
} else
#ifdef HAVE_OPENSSL
-/* pollard rho, algorithm from Jim Gillogly, May 2000 */
+/* pollard p-1, algorithm from Jim Gillogly, May 2000 */
void
pollard_pminus1(BIGNUM *val)
{
- BIGNUM *base, *num, *i, *x;
+ BIGNUM *base, *rbase, *num, *i, *x;
base = BN_new();
+ rbase = BN_new();
num = BN_new();
i = BN_new();
x = BN_new();
+ BN_set_word(rbase, 1);
+ newbase:
+ BN_add_word(rbase, 1);
BN_set_word(i, 2);
- BN_set_word(base, 2);
+ BN_copy(base, rbase);
for (;;) {
BN_mod_exp(base, base, i, val, ctx);
+ if (BN_is_one(base))
+ goto newbase;
BN_copy(x, base);
BN_sub_word(x, 1);