2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#) Copyright (c) 1989, 1993 The Regents of the University of California. All rights reserved.
37 * @(#)factor.c 8.4 (Berkeley) 5/4/95
38 * $NetBSD: factor.c,v 1.13 2002/06/18 23:07:36 simonb Exp $
39 * $FreeBSD: src/games/factor/factor.c,v 1.9.2.2 2002/10/23 14:59:14 fanf Exp $
40 * $DragonFly: src/games/factor/factor.c,v 1.2 2003/06/17 04:25:23 dillon Exp $
44 * factor - factor a number into primes
46 * By: Landon Curt Noll chongo@toad.com, ...!{sun,tolsoft}!hoptoad!chongo
48 * chongo <for a good prime call: 391581 * 2^216193 - 1> /\oo/\
51 * factor [-h] [number] ...
53 * The form of the output is:
55 * number: factor1 factor1 factor2 factor3 factor3 factor3 ...
57 * where factor1 < factor2 < factor3 < ...
59 * If no args are given, the list of numbers are read from stdin.
74 #include <openssl/bn.h>
76 #define PRIME_CHECKS 5
78 static void pollard_pminus1(BIGNUM
*); /* print factors for big numbers */
83 typedef u_long BN_ULONG
;
86 #define BN_CTX_new() NULL
87 #define BN_new() ((BIGNUM *)calloc(sizeof(BIGNUM), 1))
88 #define BN_is_zero(v) (*(v) == 0)
89 #define BN_is_one(v) (*(v) == 1)
90 #define BN_mod_word(a, b) (*(a) % (b))
92 static int BN_dec2bn(BIGNUM
**a
, const char *str
);
93 static int BN_hex2bn(BIGNUM
**a
, const char *str
);
94 static BN_ULONG
BN_div_word(BIGNUM
*, BN_ULONG
);
95 static void BN_print_fp(FILE *, const BIGNUM
*);
99 static void BN_print_dec_fp(FILE *, const BIGNUM
*);
101 static void pr_fact(BIGNUM
*); /* print factors of a value */
102 static void pr_print(BIGNUM
*); /* print a prime */
103 static void usage(void);
105 static BN_CTX
*ctx
; /* just use a global context */
109 main(int argc
, char *argv
[])
113 char *p
, buf
[LINE_MAX
]; /* > max number of digits. */
118 errx(1, "can't initialise bignum");
120 while ((ch
= getopt(argc
, argv
, "h")) != -1)
132 /* No args supplied, read numbers from stdin. */
135 if (fgets(buf
, sizeof(buf
), stdin
) == NULL
) {
140 for (p
= buf
; isblank(*p
); ++p
);
141 if (*p
== '\n' || *p
== '\0')
144 errx(1, "negative numbers aren't permitted.");
145 if (BN_dec2bn(&val
, buf
) == 0 &&
146 BN_hex2bn(&val
, buf
) == 0)
147 errx(1, "%s: illegal numeric format.", buf
);
150 /* Factor the arguments. */
152 for (; *argv
!= NULL
; ++argv
) {
153 if (argv
[0][0] == '-')
154 errx(1, "negative numbers aren't permitted.");
155 if (BN_dec2bn(&val
, argv
[0]) == 0 &&
156 BN_hex2bn(&val
, argv
[0]) == 0)
157 errx(1, "%s: illegal numeric format.", argv
[0]);
164 * pr_fact - print the factors of a number
166 * Print the factors of the number, from the lowest to the highest.
167 * A factor will be printed multiple times if it divides the value
170 * Factors are printed with leading tabs.
175 const ubig
*fact
; /* The factor found. */
177 /* Firewall - catch 0 and 1. */
178 if (BN_is_zero(val
)) /* Historical practice; 0 just exits. */
180 if (BN_is_one(val
)) {
189 BN_print_fp(stdout
, val
);
191 BN_print_dec_fp(stdout
, val
);
193 for (fact
= &prime
[0]; !BN_is_one(val
); ++fact
) {
194 /* Look for the smallest factor. */
196 if (BN_mod_word(val
, (BN_ULONG
)*fact
) == 0)
198 } while (++fact
<= pr_limit
);
200 /* Watch for primes larger than the table. */
201 if (fact
> pr_limit
) {
206 BN_set_word(bnfact
, *(fact
- 1));
207 BN_sqr(bnfact
, bnfact
, ctx
);
208 if (BN_cmp(bnfact
, val
) > 0)
211 pollard_pminus1(val
);
218 /* Divide factor out until none are left. */
220 printf(hflag
? " 0x%lx" : " %lu", *fact
);
221 BN_div_word(val
, (BN_ULONG
)*fact
);
222 } while (BN_mod_word(val
, (BN_ULONG
)*fact
) == 0);
224 /* Let the user know we're doing something. */
231 pr_print(BIGNUM
*val
)
234 fputs(" 0x", stdout
);
235 BN_print_fp(stdout
, val
);
238 BN_print_dec_fp(stdout
, val
);
245 fprintf(stderr
, "usage: factor [-h] [value ...]\n");
251 /* pollard rho, algorithm from Jim Gillogly, May 2000 */
253 pollard_pminus1(BIGNUM
*val
)
255 BIGNUM
*base
, *num
, *i
, *x
;
263 BN_set_word(base
, 2);
266 BN_mod_exp(base
, base
, i
, val
, ctx
);
270 BN_gcd(x
, x
, val
, ctx
);
273 if (BN_is_prime(x
, PRIME_CHECKS
, NULL
, NULL
,
280 BN_div(num
, NULL
, val
, x
, ctx
);
283 if (BN_is_prime(num
, PRIME_CHECKS
, NULL
, NULL
,
296 * Sigh.. No _decimal_ output to file functions in BN.
299 BN_print_dec_fp(FILE *fp
, const BIGNUM
*num
)
303 buf
= BN_bn2dec(num
);
305 return; /* XXX do anything here? */
313 BN_print_fp(FILE *fp
, const BIGNUM
*num
)
315 fprintf(fp
, "%lx", (unsigned long)*num
);
319 BN_print_dec_fp(FILE *fp
, const BIGNUM
*num
)
321 fprintf(fp
, "%lu", (unsigned long)*num
);
325 BN_dec2bn(BIGNUM
**a
, const char *str
)
330 **a
= strtoul(str
, &p
, 10);
331 return (errno
== 0 && (*p
== '\n' || *p
== '\0'));
335 BN_hex2bn(BIGNUM
**a
, const char *str
)
340 **a
= strtoul(str
, &p
, 16);
341 return (errno
== 0 && (*p
== '\n' || *p
== '\0'));
345 BN_div_word(BIGNUM
*a
, BN_ULONG b
)