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