xref: /freebsd/usr.bin/factor/factor.c (revision 734e82fe33aa764367791a7d603b383996c6b40b)
1 /*
2  * Copyright (c) 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Landon Curt Noll.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #ifndef lint
34 #include <sys/cdefs.h>
35 #ifdef __COPYRIGHT
36 __COPYRIGHT("@(#) Copyright (c) 1989, 1993\
37 	The Regents of the University of California.  All rights reserved.");
38 #endif
39 #ifdef __SCCSID
40 __SCCSID("@(#)factor.c	8.4 (Berkeley) 5/4/95");
41 #endif
42 #ifdef __RCSID
43 __RCSID("$NetBSD: factor.c,v 1.19 2009/08/12 05:54:31 dholland Exp $");
44 #endif
45 #ifdef __FBSDID
46 #endif
47 #endif /* not lint */
48 
49 /*
50  * factor - factor a number into primes
51  *
52  * By: Landon Curt Noll   chongo@toad.com,   ...!{sun,tolsoft}!hoptoad!chongo
53  *
54  *   chongo <for a good prime call: 391581 * 2^216193 - 1> /\oo/\
55  *
56  * usage:
57  *	factor [-h] [number] ...
58  *
59  * The form of the output is:
60  *
61  *	number: factor1 factor1 factor2 factor3 factor3 factor3 ...
62  *
63  * where factor1 <= factor2 <= factor3 <= ...
64  *
65  * If no args are given, the list of numbers are read from stdin.
66  */
67 
68 #include <ctype.h>
69 #include <err.h>
70 #include <errno.h>
71 #include <inttypes.h>
72 #include <limits.h>
73 #include <stdbool.h>
74 #include <stdio.h>
75 #include <stdlib.h>
76 #include <unistd.h>
77 
78 #include "primes.h"
79 
80 #ifdef HAVE_OPENSSL
81 
82 #include <openssl/bn.h>
83 
84 #if OPENSSL_VERSION_NUMBER < 0x30000000L
85 static inline int
86 BN_check_prime(BIGNUM *p, BN_CTX *ctx, BN_GENCB *cb)
87 {
88 	const int nchecks = 5;
89 
90 	return BN_is_prime_ex(p, nchecks, ctx, cb);
91 }
92 #endif
93 
94 static void	pollard_pminus1(BIGNUM *); /* print factors for big numbers */
95 
96 #else
97 
98 typedef ubig	BIGNUM;
99 typedef u_long	BN_ULONG;
100 
101 #define BN_CTX			int
102 #define BN_CTX_new()		NULL
103 #define BN_new()		((BIGNUM *)calloc(sizeof(BIGNUM), 1))
104 #define BN_is_zero(v)		(*(v) == 0)
105 #define BN_is_one(v)		(*(v) == 1)
106 #define BN_mod_word(a, b)	(*(a) % (b))
107 
108 static int	BN_dec2bn(BIGNUM **, const char *);
109 static int	BN_hex2bn(BIGNUM **, const char *);
110 static BN_ULONG BN_div_word(BIGNUM *, BN_ULONG);
111 static void	BN_print_fp(FILE *, const BIGNUM *);
112 
113 #endif
114 
115 static void	BN_print_dec_fp(FILE *, const BIGNUM *);
116 static void	convert_str2bn(BIGNUM **, char *);
117 static bool	is_hex_str(char *);
118 static void	pr_fact(BIGNUM *);	/* print factors of a value */
119 static void	pr_print(BIGNUM *);	/* print a prime */
120 static void	usage(void);
121 
122 static BN_CTX	*ctx;			/* just use a global context */
123 static int	hflag;
124 
125 int
126 main(int argc, char *argv[])
127 {
128 	BIGNUM *val;
129 	int ch;
130 	char *p, buf[LINE_MAX];		/* > max number of digits. */
131 
132 	ctx = BN_CTX_new();
133 	val = BN_new();
134 	if (val == NULL)
135 		errx(1, "can't initialise bignum");
136 
137 	while ((ch = getopt(argc, argv, "h")) != -1)
138 		switch (ch) {
139 		case 'h':
140 			hflag++;
141 			break;
142 		case '?':
143 		default:
144 			usage();
145 		}
146 	argc -= optind;
147 	argv += optind;
148 
149 	/* No args supplied, read numbers from stdin. */
150 	if (argc == 0)
151 		for (;;) {
152 			if (fgets(buf, sizeof(buf), stdin) == NULL) {
153 				if (ferror(stdin))
154 					err(1, "stdin");
155 				exit (0);
156 			}
157 			for (p = buf; isblank(*p); ++p);
158 			if (*p == '\n' || *p == '\0')
159 				continue;
160 			convert_str2bn(&val, p);
161 			pr_fact(val);
162 		}
163 	/* Factor the arguments. */
164 	else
165 		for (p = *argv; p != NULL; p = *++argv) {
166 			convert_str2bn(&val, p);
167 			pr_fact(val);
168 		}
169 	exit(0);
170 }
171 
172 /*
173  * pr_fact - print the factors of a number
174  *
175  * Print the factors of the number, from the lowest to the highest.
176  * A factor will be printed multiple times if it divides the value
177  * multiple times.
178  *
179  * Factors are printed with leading tabs.
180  */
181 static void
182 pr_fact(BIGNUM *val)
183 {
184 	const ubig *fact;	/* The factor found. */
185 
186 	/* Firewall - catch 0 and 1. */
187 	if (BN_is_zero(val))	/* Historical practice; 0 just exits. */
188 		exit(0);
189 	if (BN_is_one(val)) {
190 		printf("1: 1\n");
191 		return;
192 	}
193 
194 	/* Factor value. */
195 
196 	if (hflag) {
197 		fputs("0x", stdout);
198 		BN_print_fp(stdout, val);
199 	} else
200 		BN_print_dec_fp(stdout, val);
201 	putchar(':');
202 	for (fact = &prime[0]; !BN_is_one(val); ++fact) {
203 		/* Look for the smallest factor. */
204 		do {
205 			if (BN_mod_word(val, (BN_ULONG)*fact) == 0)
206 				break;
207 		} while (++fact <= pr_limit);
208 
209 		/* Watch for primes larger than the table. */
210 		if (fact > pr_limit) {
211 #ifdef HAVE_OPENSSL
212 			BIGNUM *bnfact;
213 
214 			bnfact = BN_new();
215 			BN_set_word(bnfact, *(fact - 1));
216 			if (!BN_sqr(bnfact, bnfact, ctx))
217 				errx(1, "error in BN_sqr()");
218 			if (BN_cmp(bnfact, val) > 0 ||
219 			    BN_check_prime(val, NULL, NULL) == 1)
220 				pr_print(val);
221 			else
222 				pollard_pminus1(val);
223 #else
224 			pr_print(val);
225 #endif
226 			break;
227 		}
228 
229 		/* Divide factor out until none are left. */
230 		do {
231 			printf(hflag ? " 0x%" PRIx64 "" : " %" PRIu64 "", *fact);
232 			BN_div_word(val, (BN_ULONG)*fact);
233 		} while (BN_mod_word(val, (BN_ULONG)*fact) == 0);
234 
235 		/* Let the user know we're doing something. */
236 		fflush(stdout);
237 	}
238 	putchar('\n');
239 }
240 
241 static void
242 pr_print(BIGNUM *val)
243 {
244 	if (hflag) {
245 		fputs(" 0x", stdout);
246 		BN_print_fp(stdout, val);
247 	} else {
248 		putchar(' ');
249 		BN_print_dec_fp(stdout, val);
250 	}
251 }
252 
253 static void
254 usage(void)
255 {
256 	fprintf(stderr, "usage: factor [-h] [value ...]\n");
257 	exit(1);
258 }
259 
260 #ifdef HAVE_OPENSSL
261 
262 /* pollard p-1, algorithm from Jim Gillogly, May 2000 */
263 static void
264 pollard_pminus1(BIGNUM *val)
265 {
266 	BIGNUM *base, *rbase, *num, *i, *x;
267 
268 	base = BN_new();
269 	rbase = BN_new();
270 	num = BN_new();
271 	i = BN_new();
272 	x = BN_new();
273 
274 	BN_set_word(rbase, 1);
275 newbase:
276 	if (!BN_add_word(rbase, 1))
277 		errx(1, "error in BN_add_word()");
278 	BN_set_word(i, 2);
279 	BN_copy(base, rbase);
280 
281 	for (;;) {
282 		BN_mod_exp(base, base, i, val, ctx);
283 		if (BN_is_one(base))
284 			goto newbase;
285 
286 		BN_copy(x, base);
287 		BN_sub_word(x, 1);
288 		if (!BN_gcd(x, x, val, ctx))
289 			errx(1, "error in BN_gcd()");
290 
291 		if (!BN_is_one(x)) {
292 			if (BN_check_prime(x, NULL, NULL) == 1)
293 				pr_print(x);
294 			else
295 				pollard_pminus1(x);
296 			fflush(stdout);
297 
298 			BN_div(num, NULL, val, x, ctx);
299 			if (BN_is_one(num))
300 				return;
301 			if (BN_check_prime(num, NULL, NULL) == 1) {
302 				pr_print(num);
303 				fflush(stdout);
304 				return;
305 			}
306 			BN_copy(val, num);
307 		}
308 		if (!BN_add_word(i, 1))
309 			errx(1, "error in BN_add_word()");
310 	}
311 }
312 
313 /*
314  * Sigh..  No _decimal_ output to file functions in BN.
315  */
316 static void
317 BN_print_dec_fp(FILE *fp, const BIGNUM *num)
318 {
319 	char *buf;
320 
321 	buf = BN_bn2dec(num);
322 	if (buf == NULL)
323 		return;	/* XXX do anything here? */
324 	fprintf(fp, "%s", buf);
325 	free(buf);
326 }
327 
328 #else
329 
330 static void
331 BN_print_fp(FILE *fp, const BIGNUM *num)
332 {
333 	fprintf(fp, "%lx", (unsigned long)*num);
334 }
335 
336 static void
337 BN_print_dec_fp(FILE *fp, const BIGNUM *num)
338 {
339 	fprintf(fp, "%lu", (unsigned long)*num);
340 }
341 
342 static int
343 BN_dec2bn(BIGNUM **a, const char *str)
344 {
345 	char *p;
346 
347 	errno = 0;
348 	**a = strtoul(str, &p, 10);
349 	return (errno == 0 ? 1 : 0);	/* OpenSSL returns 0 on error! */
350 }
351 
352 static int
353 BN_hex2bn(BIGNUM **a, const char *str)
354 {
355 	char *p;
356 
357 	errno = 0;
358 	**a = strtoul(str, &p, 16);
359 	return (errno == 0 ? 1 : 0);	/* OpenSSL returns 0 on error! */
360 }
361 
362 static BN_ULONG
363 BN_div_word(BIGNUM *a, BN_ULONG b)
364 {
365 	BN_ULONG mod;
366 
367 	mod = *a % b;
368 	*a /= b;
369 	return mod;
370 }
371 
372 #endif
373 
374 /*
375  * Scan the string from left-to-right to see if the longest substring
376  * is a valid hexadecimal number.
377  */
378 static bool
379 is_hex_str(char *str)
380 {
381 	char c, *p;
382 	bool saw_hex = false;
383 
384 	for (p = str; *p; p++) {
385 		if (isdigit(*p))
386 			continue;
387 		c = tolower(*p);
388 		if (c >= 'a' && c <= 'f') {
389 			saw_hex = true;
390 			continue;
391 		}
392 		break;	/* Not a hexadecimal digit. */
393 	}
394 	return saw_hex;
395 }
396 
397 /* Convert string pointed to by *str to a bignum.  */
398 static void
399 convert_str2bn(BIGNUM **val, char *p)
400 {
401 	int n = 0;
402 
403 	if (*p == '+') p++;
404 	if (*p == '-')
405 		errx(1, "negative numbers aren't permitted.");
406 	if (*p == '0') {
407 		p++;
408 		if (*p == 'x' || *p == 'X')
409 			n = BN_hex2bn(val, ++p);
410 	} else {
411 		n = is_hex_str(p) ? BN_hex2bn(val, p) : BN_dec2bn(val, p);
412 	}
413 	if (n == 0)
414 		errx(1, "%s: illegal numeric format.", p);
415 }
416