1*0a6a1f1dSLionel Sambuc /* $NetBSD: factor.c,v 1.27 2014/10/02 21:36:37 ast Exp $ */
2c1f592a2SThomas Cort
3c1f592a2SThomas Cort /*
4c1f592a2SThomas Cort * Copyright (c) 1989, 1993
5c1f592a2SThomas Cort * The Regents of the University of California. All rights reserved.
6c1f592a2SThomas Cort *
7c1f592a2SThomas Cort * This code is derived from software contributed to Berkeley by
8c1f592a2SThomas Cort * Landon Curt Noll.
9c1f592a2SThomas Cort *
10c1f592a2SThomas Cort * Redistribution and use in source and binary forms, with or without
11c1f592a2SThomas Cort * modification, are permitted provided that the following conditions
12c1f592a2SThomas Cort * are met:
13c1f592a2SThomas Cort * 1. Redistributions of source code must retain the above copyright
14c1f592a2SThomas Cort * notice, this list of conditions and the following disclaimer.
15c1f592a2SThomas Cort * 2. Redistributions in binary form must reproduce the above copyright
16c1f592a2SThomas Cort * notice, this list of conditions and the following disclaimer in the
17c1f592a2SThomas Cort * documentation and/or other materials provided with the distribution.
18c1f592a2SThomas Cort * 3. Neither the name of the University nor the names of its contributors
19c1f592a2SThomas Cort * may be used to endorse or promote products derived from this software
20c1f592a2SThomas Cort * without specific prior written permission.
21c1f592a2SThomas Cort *
22c1f592a2SThomas Cort * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23c1f592a2SThomas Cort * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24c1f592a2SThomas Cort * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25c1f592a2SThomas Cort * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26c1f592a2SThomas Cort * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27c1f592a2SThomas Cort * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28c1f592a2SThomas Cort * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29c1f592a2SThomas Cort * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30c1f592a2SThomas Cort * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31c1f592a2SThomas Cort * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32c1f592a2SThomas Cort * SUCH DAMAGE.
33c1f592a2SThomas Cort */
34c1f592a2SThomas Cort
35c1f592a2SThomas Cort #include <sys/cdefs.h>
36c1f592a2SThomas Cort #ifndef lint
37c1f592a2SThomas Cort __COPYRIGHT("@(#) Copyright (c) 1989, 1993\
38c1f592a2SThomas Cort The Regents of the University of California. All rights reserved.");
39c1f592a2SThomas Cort #endif /* not lint */
40c1f592a2SThomas Cort
41c1f592a2SThomas Cort #ifndef lint
42c1f592a2SThomas Cort #if 0
43c1f592a2SThomas Cort static char sccsid[] = "@(#)factor.c 8.4 (Berkeley) 5/4/95";
44c1f592a2SThomas Cort #else
45*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: factor.c,v 1.27 2014/10/02 21:36:37 ast Exp $");
46c1f592a2SThomas Cort #endif
47c1f592a2SThomas Cort #endif /* not lint */
48c1f592a2SThomas Cort
49c1f592a2SThomas Cort /*
50c1f592a2SThomas Cort * factor - factor a number into primes
51c1f592a2SThomas Cort *
52*0a6a1f1dSLionel Sambuc * By Landon Curt Noll, http://www.isthe.com/chongo/index.html /\oo/\
53c1f592a2SThomas Cort *
54c1f592a2SThomas Cort * usage:
55c1f592a2SThomas Cort * factor [number] ...
56c1f592a2SThomas Cort *
57c1f592a2SThomas Cort * The form of the output is:
58c1f592a2SThomas Cort *
59c1f592a2SThomas Cort * number: factor1 factor1 factor2 factor3 factor3 factor3 ...
60c1f592a2SThomas Cort *
61c1f592a2SThomas Cort * where factor1 <= factor2 <= factor3 <= ...
62c1f592a2SThomas Cort *
63c1f592a2SThomas Cort * If no args are given, the list of numbers are read from stdin.
64c1f592a2SThomas Cort */
65c1f592a2SThomas Cort
66c1f592a2SThomas Cort #include <ctype.h>
67c1f592a2SThomas Cort #include <err.h>
68c1f592a2SThomas Cort #include <errno.h>
69c1f592a2SThomas Cort #include <limits.h>
70c1f592a2SThomas Cort #include <stdio.h>
71c1f592a2SThomas Cort #include <stdlib.h>
72c1f592a2SThomas Cort #include <unistd.h>
73*0a6a1f1dSLionel Sambuc #include <inttypes.h>
74c1f592a2SThomas Cort
75c1f592a2SThomas Cort #ifdef HAVE_OPENSSL
76c1f592a2SThomas Cort #include <openssl/bn.h>
77c1f592a2SThomas Cort #else
78c1f592a2SThomas Cort typedef long BIGNUM;
79c1f592a2SThomas Cort typedef u_long BN_ULONG;
80c1f592a2SThomas Cort static int BN_dec2bn(BIGNUM **a, const char *str);
81c1f592a2SThomas Cort #define BN_new() ((BIGNUM *)calloc(sizeof(BIGNUM), 1))
82c1f592a2SThomas Cort #define BN_is_zero(v) (*(v) == 0)
83c1f592a2SThomas Cort #define BN_is_one(v) (*(v) == 1)
84c1f592a2SThomas Cort #define BN_mod_word(a, b) (*(a) % (b))
85c1f592a2SThomas Cort #endif
86c1f592a2SThomas Cort
87c1f592a2SThomas Cort #include "primes.h"
88c1f592a2SThomas Cort
89c1f592a2SThomas Cort /*
90c1f592a2SThomas Cort * prime[i] is the (i-1)th prime.
91c1f592a2SThomas Cort *
92c1f592a2SThomas Cort * We are able to sieve 2^32-1 because this byte table yields all primes
93c1f592a2SThomas Cort * up to 65537 and 65537^2 > 2^32-1.
94c1f592a2SThomas Cort */
95*0a6a1f1dSLionel Sambuc
96c1f592a2SThomas Cort #if 0 /* debugging: limit table use to stress the "pollard" code */
97c1f592a2SThomas Cort #define pr_limit &prime[0]
98c1f592a2SThomas Cort #endif
99c1f592a2SThomas Cort
100c1f592a2SThomas Cort #define PRIME_CHECKS 5
101c1f592a2SThomas Cort
102c1f592a2SThomas Cort #ifdef HAVE_OPENSSL
103c1f592a2SThomas Cort static BN_CTX *ctx; /* just use a global context */
104c1f592a2SThomas Cort #endif
105c1f592a2SThomas Cort
106c1f592a2SThomas Cort static void pr_fact(BIGNUM *); /* print factors of a value */
107c1f592a2SThomas Cort static void BN_print_dec_fp(FILE *, const BIGNUM *);
108c1f592a2SThomas Cort static void usage(void) __dead;
109c1f592a2SThomas Cort #ifdef HAVE_OPENSSL
110c1f592a2SThomas Cort static void pollard_rho(BIGNUM *); /* print factors for big numbers */
111c1f592a2SThomas Cort #else
112c1f592a2SThomas Cort static char *BN_bn2dec(const BIGNUM *);
113c1f592a2SThomas Cort static BN_ULONG BN_div_word(BIGNUM *, BN_ULONG);
114c1f592a2SThomas Cort #endif
115c1f592a2SThomas Cort
116c1f592a2SThomas Cort
117c1f592a2SThomas Cort #ifndef HAVE_OPENSSL
118c1f592a2SThomas Cort static int
BN_dec2bn(BIGNUM ** a,const char * str)119c1f592a2SThomas Cort BN_dec2bn(BIGNUM **a, const char *str)
120c1f592a2SThomas Cort {
121c1f592a2SThomas Cort char *p;
122c1f592a2SThomas Cort
123c1f592a2SThomas Cort errno = 0;
124c1f592a2SThomas Cort **a = strtoul(str, &p, 10);
125c1f592a2SThomas Cort if (errno)
126c1f592a2SThomas Cort err(1, "%s", str);
127c1f592a2SThomas Cort return (*p == '\n' || *p == '\0');
128c1f592a2SThomas Cort }
129c1f592a2SThomas Cort #endif
130c1f592a2SThomas Cort
131c1f592a2SThomas Cort int
main(int argc,char * argv[])132c1f592a2SThomas Cort main(int argc, char *argv[])
133c1f592a2SThomas Cort {
134c1f592a2SThomas Cort BIGNUM *val;
135c1f592a2SThomas Cort int ch;
136c1f592a2SThomas Cort char *p, buf[LINE_MAX]; /* > max number of digits. */
137c1f592a2SThomas Cort
138c1f592a2SThomas Cort #ifdef HAVE_OPENSSL
139c1f592a2SThomas Cort ctx = BN_CTX_new();
140c1f592a2SThomas Cort #endif
141c1f592a2SThomas Cort val = BN_new();
142c1f592a2SThomas Cort if (val == NULL)
143c1f592a2SThomas Cort errx(1, "can't initialise bignum");
144c1f592a2SThomas Cort
145c1f592a2SThomas Cort while ((ch = getopt(argc, argv, "")) != -1)
146c1f592a2SThomas Cort switch (ch) {
147c1f592a2SThomas Cort case '?':
148c1f592a2SThomas Cort default:
149c1f592a2SThomas Cort usage();
150c1f592a2SThomas Cort }
151c1f592a2SThomas Cort argc -= optind;
152c1f592a2SThomas Cort argv += optind;
153c1f592a2SThomas Cort
154c1f592a2SThomas Cort /* No args supplied, read numbers from stdin. */
155c1f592a2SThomas Cort if (argc == 0)
156c1f592a2SThomas Cort for (;;) {
157c1f592a2SThomas Cort if (fgets(buf, sizeof(buf), stdin) == NULL) {
158c1f592a2SThomas Cort if (ferror(stdin))
159c1f592a2SThomas Cort err(1, "stdin");
160c1f592a2SThomas Cort exit (0);
161c1f592a2SThomas Cort }
162c1f592a2SThomas Cort for (p = buf; isblank((unsigned char)*p); ++p);
163c1f592a2SThomas Cort if (*p == '\n' || *p == '\0')
164c1f592a2SThomas Cort continue;
165c1f592a2SThomas Cort if (*p == '-')
166c1f592a2SThomas Cort errx(1, "negative numbers aren't permitted.");
167c1f592a2SThomas Cort if (BN_dec2bn(&val, buf) == 0)
168c1f592a2SThomas Cort errx(1, "%s: illegal numeric format.", argv[0]);
169c1f592a2SThomas Cort pr_fact(val);
170c1f592a2SThomas Cort }
171c1f592a2SThomas Cort /* Factor the arguments. */
172c1f592a2SThomas Cort else
173c1f592a2SThomas Cort for (; *argv != NULL; ++argv) {
174c1f592a2SThomas Cort if (argv[0][0] == '-')
175c1f592a2SThomas Cort errx(1, "numbers <= 1 aren't permitted.");
176c1f592a2SThomas Cort if (BN_dec2bn(&val, argv[0]) == 0)
177c1f592a2SThomas Cort errx(1, "%s: illegal numeric format.", argv[0]);
178c1f592a2SThomas Cort pr_fact(val);
179c1f592a2SThomas Cort }
180c1f592a2SThomas Cort exit(0);
181c1f592a2SThomas Cort }
182c1f592a2SThomas Cort
183c1f592a2SThomas Cort /*
184c1f592a2SThomas Cort * pr_fact - print the factors of a number
185c1f592a2SThomas Cort *
186c1f592a2SThomas Cort * If the number is 0 or 1, then print the number and return.
187c1f592a2SThomas Cort * If the number is < 0, print -1, negate the number and continue
188c1f592a2SThomas Cort * processing.
189c1f592a2SThomas Cort *
190c1f592a2SThomas Cort * Print the factors of the number, from the lowest to the highest.
191c1f592a2SThomas Cort * A factor will be printed numtiple times if it divides the value
192c1f592a2SThomas Cort * multiple times.
193c1f592a2SThomas Cort *
194c1f592a2SThomas Cort * Factors are printed with leading tabs.
195c1f592a2SThomas Cort */
196c1f592a2SThomas Cort static void
pr_fact(BIGNUM * val)197c1f592a2SThomas Cort pr_fact(BIGNUM *val)
198c1f592a2SThomas Cort {
199*0a6a1f1dSLionel Sambuc const uint64_t *fact; /* The factor found. */
200c1f592a2SThomas Cort
201c1f592a2SThomas Cort /* Firewall - catch 0 and 1. */
202c1f592a2SThomas Cort if (BN_is_zero(val) || BN_is_one(val))
203c1f592a2SThomas Cort errx(1, "numbers <= 1 aren't permitted.");
204c1f592a2SThomas Cort
205c1f592a2SThomas Cort /* Factor value. */
206c1f592a2SThomas Cort
207c1f592a2SThomas Cort BN_print_dec_fp(stdout, val);
208c1f592a2SThomas Cort putchar(':');
209c1f592a2SThomas Cort for (fact = &prime[0]; !BN_is_one(val); ++fact) {
210c1f592a2SThomas Cort /* Look for the smallest factor. */
211c1f592a2SThomas Cort while (fact <= pr_limit) {
212c1f592a2SThomas Cort if (BN_mod_word(val, (BN_ULONG)*fact) == 0)
213c1f592a2SThomas Cort break;
214c1f592a2SThomas Cort fact++;
215c1f592a2SThomas Cort }
216c1f592a2SThomas Cort
217c1f592a2SThomas Cort /* Watch for primes larger than the table. */
218c1f592a2SThomas Cort if (fact > pr_limit) {
219c1f592a2SThomas Cort #ifdef HAVE_OPENSSL
220c1f592a2SThomas Cort BIGNUM *bnfact;
221c1f592a2SThomas Cort
222c1f592a2SThomas Cort bnfact = BN_new();
223c1f592a2SThomas Cort BN_set_word(bnfact, (BN_ULONG)*(fact - 1));
224c1f592a2SThomas Cort BN_sqr(bnfact, bnfact, ctx);
225c1f592a2SThomas Cort if (BN_cmp(bnfact, val) > 0
226c1f592a2SThomas Cort || BN_is_prime(val, PRIME_CHECKS, NULL, NULL,
227c1f592a2SThomas Cort NULL) == 1) {
228c1f592a2SThomas Cort putchar(' ');
229c1f592a2SThomas Cort BN_print_dec_fp(stdout, val);
230c1f592a2SThomas Cort } else
231c1f592a2SThomas Cort pollard_rho(val);
232c1f592a2SThomas Cort #else
233c1f592a2SThomas Cort printf(" %s", BN_bn2dec(val));
234c1f592a2SThomas Cort #endif
235c1f592a2SThomas Cort break;
236c1f592a2SThomas Cort }
237c1f592a2SThomas Cort
238c1f592a2SThomas Cort /* Divide factor out until none are left. */
239c1f592a2SThomas Cort do {
240*0a6a1f1dSLionel Sambuc printf(" %" PRIu64, *fact);
241c1f592a2SThomas Cort BN_div_word(val, (BN_ULONG)*fact);
242c1f592a2SThomas Cort } while (BN_mod_word(val, (BN_ULONG)*fact) == 0);
243c1f592a2SThomas Cort
244c1f592a2SThomas Cort /* Let the user know we're doing something. */
245c1f592a2SThomas Cort fflush(stdout);
246c1f592a2SThomas Cort }
247c1f592a2SThomas Cort putchar('\n');
248c1f592a2SThomas Cort }
249c1f592a2SThomas Cort
250c1f592a2SThomas Cort /*
251c1f592a2SThomas Cort * Sigh.. No _decimal_ output to file functions in BN.
252c1f592a2SThomas Cort */
253c1f592a2SThomas Cort static void
BN_print_dec_fp(FILE * fp,const BIGNUM * num)254c1f592a2SThomas Cort BN_print_dec_fp(FILE *fp, const BIGNUM *num)
255c1f592a2SThomas Cort {
256c1f592a2SThomas Cort char *buf;
257c1f592a2SThomas Cort
258c1f592a2SThomas Cort buf = BN_bn2dec(num);
259c1f592a2SThomas Cort if (buf == NULL)
260c1f592a2SThomas Cort return; /* XXX do anything here? */
261c1f592a2SThomas Cort fprintf(fp, "%s", buf);
262c1f592a2SThomas Cort free(buf);
263c1f592a2SThomas Cort }
264c1f592a2SThomas Cort
265c1f592a2SThomas Cort void
usage(void)266c1f592a2SThomas Cort usage(void)
267c1f592a2SThomas Cort {
268c1f592a2SThomas Cort fprintf(stderr, "usage: factor [value ...]\n");
269c1f592a2SThomas Cort exit (0);
270c1f592a2SThomas Cort }
271c1f592a2SThomas Cort
272c1f592a2SThomas Cort
273c1f592a2SThomas Cort
274c1f592a2SThomas Cort
275c1f592a2SThomas Cort #ifdef HAVE_OPENSSL
276c1f592a2SThomas Cort static void
pollard_rho(BIGNUM * val)277c1f592a2SThomas Cort pollard_rho(BIGNUM *val)
278c1f592a2SThomas Cort {
279c1f592a2SThomas Cort BIGNUM *x, *y, *tmp, *num;
280c1f592a2SThomas Cort BN_ULONG a;
281c1f592a2SThomas Cort unsigned int steps_taken, steps_limit;
282c1f592a2SThomas Cort
283c1f592a2SThomas Cort x = BN_new();
284c1f592a2SThomas Cort y = BN_new();
285c1f592a2SThomas Cort tmp = BN_new();
286c1f592a2SThomas Cort num = BN_new();
287c1f592a2SThomas Cort a = 1;
288c1f592a2SThomas Cort restart:
289c1f592a2SThomas Cort steps_taken = 0;
290c1f592a2SThomas Cort steps_limit = 2;
291c1f592a2SThomas Cort BN_set_word(x, 1);
292c1f592a2SThomas Cort BN_copy(y, x);
293c1f592a2SThomas Cort
294c1f592a2SThomas Cort for (;;) {
295c1f592a2SThomas Cort BN_sqr(tmp, x, ctx);
296c1f592a2SThomas Cort BN_add_word(tmp, a);
297c1f592a2SThomas Cort BN_mod(x, tmp, val, ctx);
298c1f592a2SThomas Cort BN_sub(tmp, x, y);
299c1f592a2SThomas Cort if (BN_is_zero(tmp)) {
300c1f592a2SThomas Cort #ifdef DEBUG
301c1f592a2SThomas Cort printf(" (loop)");
302c1f592a2SThomas Cort #endif
303c1f592a2SThomas Cort a++;
304c1f592a2SThomas Cort goto restart;
305c1f592a2SThomas Cort }
306c1f592a2SThomas Cort BN_gcd(tmp, tmp, val, ctx);
307c1f592a2SThomas Cort
308c1f592a2SThomas Cort if (!BN_is_one(tmp)) {
309c1f592a2SThomas Cort if (BN_is_prime(tmp, PRIME_CHECKS, NULL, NULL,
310c1f592a2SThomas Cort NULL) == 1) {
311c1f592a2SThomas Cort putchar(' ');
312c1f592a2SThomas Cort BN_print_dec_fp(stdout, tmp);
313c1f592a2SThomas Cort } else {
314c1f592a2SThomas Cort #ifdef DEBUG
315c1f592a2SThomas Cort printf(" (recurse for ");
316c1f592a2SThomas Cort BN_print_dec_fp(stdout, tmp);
317c1f592a2SThomas Cort putchar(')');
318c1f592a2SThomas Cort #endif
319c1f592a2SThomas Cort pollard_rho(BN_dup(tmp));
320c1f592a2SThomas Cort #ifdef DEBUG
321c1f592a2SThomas Cort printf(" (back)");
322c1f592a2SThomas Cort #endif
323c1f592a2SThomas Cort }
324c1f592a2SThomas Cort fflush(stdout);
325c1f592a2SThomas Cort
326c1f592a2SThomas Cort BN_div(num, NULL, val, tmp, ctx);
327c1f592a2SThomas Cort if (BN_is_one(num))
328c1f592a2SThomas Cort return;
329c1f592a2SThomas Cort if (BN_is_prime(num, PRIME_CHECKS, NULL, NULL,
330c1f592a2SThomas Cort NULL) == 1) {
331c1f592a2SThomas Cort putchar(' ');
332c1f592a2SThomas Cort BN_print_dec_fp(stdout, num);
333c1f592a2SThomas Cort fflush(stdout);
334c1f592a2SThomas Cort return;
335c1f592a2SThomas Cort }
336c1f592a2SThomas Cort BN_copy(val, num);
337c1f592a2SThomas Cort goto restart;
338c1f592a2SThomas Cort }
339c1f592a2SThomas Cort steps_taken++;
340c1f592a2SThomas Cort if (steps_taken == steps_limit) {
341c1f592a2SThomas Cort BN_copy(y, x); /* teleport the turtle */
342c1f592a2SThomas Cort steps_taken = 0;
343c1f592a2SThomas Cort steps_limit *= 2;
344c1f592a2SThomas Cort if (steps_limit == 0) {
345c1f592a2SThomas Cort #ifdef DEBUG
346c1f592a2SThomas Cort printf(" (overflow)");
347c1f592a2SThomas Cort #endif
348c1f592a2SThomas Cort a++;
349c1f592a2SThomas Cort goto restart;
350c1f592a2SThomas Cort }
351c1f592a2SThomas Cort }
352c1f592a2SThomas Cort }
353c1f592a2SThomas Cort }
354c1f592a2SThomas Cort #else
355c1f592a2SThomas Cort char *
BN_bn2dec(const BIGNUM * val)356c1f592a2SThomas Cort BN_bn2dec(const BIGNUM *val)
357c1f592a2SThomas Cort {
358c1f592a2SThomas Cort char *buf;
359c1f592a2SThomas Cort
360c1f592a2SThomas Cort buf = malloc(100);
361c1f592a2SThomas Cort if (!buf)
362c1f592a2SThomas Cort return buf;
363c1f592a2SThomas Cort snprintf(buf, 100, "%ld", (long)*val);
364c1f592a2SThomas Cort return buf;
365c1f592a2SThomas Cort }
366c1f592a2SThomas Cort
367c1f592a2SThomas Cort static BN_ULONG
BN_div_word(BIGNUM * a,BN_ULONG b)368c1f592a2SThomas Cort BN_div_word(BIGNUM *a, BN_ULONG b)
369c1f592a2SThomas Cort {
370c1f592a2SThomas Cort BN_ULONG mod;
371c1f592a2SThomas Cort
372c1f592a2SThomas Cort mod = *a % b;
373c1f592a2SThomas Cort *a /= b;
374c1f592a2SThomas Cort return mod;
375c1f592a2SThomas Cort }
376c1f592a2SThomas Cort #endif
377