]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - factor/factor.c
6d7de720096991d78b4e41540b287d086ba2574b
1 /* $NetBSD: factor.c,v 1.30 2020/10/03 22:27:00 christos Exp $ */
3 * Copyright (c) 1989, 1993
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
37 __COPYRIGHT("@(#) Copyright (c) 1989, 1993\
38 The Regents of the University of California. All rights reserved.");
41 __SCCSID("@(#)factor.c 8.4 (Berkeley) 5/4/95");
44 __RCSID("$NetBSD: factor.c,v 1.30 2020/10/03 22:27:00 christos Exp $");
47 __FBSDID("$FreeBSD: head/usr.bin/factor/factor.c 356666 2020-01-12 20:25:11Z gad $");
52 * factor - factor a number into primes
54 * By: Landon Curt Noll chongo@toad.com, ...!{sun,tolsoft}!hoptoad!chongo
56 * chongo <for a good prime call: 391581 * 2^216193 - 1> /\oo/\
59 * factor [-hx] [number] ...
61 * The form of the output is:
63 * number: factor1 factor1 factor2 factor3 factor3 factor3 ...
65 * where factor1 <= factor2 <= factor3 <= ...
67 * If the -h flag is specified, the output is in "human-readable" format.
68 * Duplicate factors are printed in the form of x^n.
70 * If the -x flag is specified numbers are printed in hex.
72 * If no number args are given, the list of numbers are read from stdin.
73 * If no args are given, the list of numbers are read from stdin.
90 #include <openssl/bn.h>
92 #define PRIME_CHECKS 5
94 static void pollard_pminus1(BIGNUM
*, int); /* print factors for big numbers */
99 typedef u_long BN_ULONG
;
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))
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
*);
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
*, int, int); /* print factors of a value */
119 static void pr_print(BIGNUM
*, int); /* print a prime */
120 static void usage(void);
122 static BN_CTX
*ctx
; /* just use a global context */
125 main(int argc
, char *argv
[])
128 int ch
, hflag
, xflag
;
129 char *p
, buf
[LINE_MAX
]; /* > max number of digits. */
135 errx(1, "can't initialise bignum");
137 while ((ch
= getopt(argc
, argv
, "hx")) != -1)
152 /* No args supplied, read numbers from stdin. */
155 if (fgets(buf
, sizeof(buf
), stdin
) == NULL
) {
160 for (p
= buf
; isblank((unsigned char)*p
); ++p
);
161 if (*p
== '\n' || *p
== '\0')
163 convert_str2bn(&val
, p
);
164 pr_fact(val
, hflag
, xflag
);
166 /* Factor the arguments. */
168 for (p
= *argv
; p
!= NULL
; p
= *++argv
) {
169 convert_str2bn(&val
, p
);
170 pr_fact(val
, hflag
, xflag
);
176 * pr_fact - print the factors of a number
178 * Print the factors of the number, from the lowest to the highest.
179 * A factor will be printed multiple times if it divides the value
182 * If hflag is specified, duplicate factors are printed in "human-
183 * readable" form of x^n.
185 * If the xflag is specified, numbers will be printed in hex.
187 * Factors are printed with leading tabs.
190 pr_fact(BIGNUM
*val
, int hflag
, int xflag
)
193 const uint64_t *fact
; /* The factor found. */
195 /* Firewall - catch 0 and 1. */
196 if (BN_is_zero(val
)) /* Historical practice; 0 just exits. */
198 if (BN_is_one(val
)) {
207 BN_print_fp(stdout
, val
);
209 BN_print_dec_fp(stdout
, val
);
211 for (fact
= &prime
[0]; !BN_is_one(val
); ++fact
) {
212 /* Look for the smallest factor. */
214 if (BN_mod_word(val
, (BN_ULONG
)*fact
) == 0)
216 } while (++fact
<= pr_limit
);
218 /* Watch for primes larger than the table. */
219 if (fact
> pr_limit
) {
224 BN_set_word(bnfact
, *(fact
- 1));
225 if (!BN_sqr(bnfact
, bnfact
, ctx
))
226 errx(1, "error in BN_sqr()");
227 if (BN_cmp(bnfact
, val
) > 0 ||
228 BN_is_prime_ex(val
, PRIME_CHECKS
, NULL
, NULL
) == 1)
229 pr_print(val
, xflag
);
231 pollard_pminus1(val
, xflag
);
233 pr_print(val
, xflag
);
238 /* Divide factor out until none are left. */
243 printf(xflag
? " 0x%" PRIx64
: " %" PRIu64
,
245 BN_div_word(val
, (BN_ULONG
)*fact
);
246 } while (BN_mod_word(val
, (BN_ULONG
)*fact
) == 0);
249 printf(xflag
? " 0x%" PRIx64
: " %" PRIu64
, *fact
);
251 printf(xflag
? "^0x%x" : "^%d", i
);
254 /* Let the user know we're doing something. */
261 pr_print(BIGNUM
*val
, int xflag
)
264 fputs(" 0x", stdout
);
265 BN_print_fp(stdout
, val
);
268 BN_print_dec_fp(stdout
, val
);
275 fprintf(stderr
, "usage: factor [-h] [value ...]\n");
281 /* pollard p-1, algorithm from Jim Gillogly, May 2000 */
283 pollard_pminus1(BIGNUM
*val
, int xflag
)
285 BIGNUM
*base
, *rbase
, *num
, *i
, *x
;
293 BN_set_word(rbase
, 1);
295 if (!BN_add_word(rbase
, 1))
296 errx(1, "error in BN_add_word()");
298 BN_copy(base
, rbase
);
301 BN_mod_exp(base
, base
, i
, val
, ctx
);
307 if (!BN_gcd(x
, x
, val
, ctx
))
308 errx(1, "error in BN_gcd()");
311 if (BN_is_prime_ex(x
, PRIME_CHECKS
, NULL
, NULL
) == 1)
314 pollard_pminus1(x
, xflag
);
317 BN_div(num
, NULL
, val
, x
, ctx
);
320 if (BN_is_prime_ex(num
, PRIME_CHECKS
, NULL
,
322 pr_print(num
, xflag
);
328 if (!BN_add_word(i
, 1))
329 errx(1, "error in BN_add_word()");
334 * Sigh.. No _decimal_ output to file functions in BN.
337 BN_print_dec_fp(FILE *fp
, const BIGNUM
*num
)
341 buf
= BN_bn2dec(num
);
343 return; /* XXX do anything here? */
344 fprintf(fp
, "%s", buf
);
351 BN_print_fp(FILE *fp
, const BIGNUM
*num
)
353 fprintf(fp
, "%lx", (unsigned long)*num
);
357 BN_print_dec_fp(FILE *fp
, const BIGNUM
*num
)
359 fprintf(fp
, "%lu", (unsigned long)*num
);
363 BN_dec2bn(BIGNUM
**a
, const char *str
)
368 **a
= strtoul(str
, &p
, 10);
369 return (errno
== 0 ? 1 : 0); /* OpenSSL returns 0 on error! */
373 BN_hex2bn(BIGNUM
**a
, const char *str
)
378 **a
= strtoul(str
, &p
, 16);
379 return (errno
== 0 ? 1 : 0); /* OpenSSL returns 0 on error! */
383 BN_div_word(BIGNUM
*a
, BN_ULONG b
)
395 * Scan the string from left-to-right to see if the longest substring
396 * is a valid hexadecimal number.
399 is_hex_str(char *str
)
402 bool saw_hex
= false;
404 for (p
= str
; *p
; p
++) {
405 if (isdigit((unsigned char)*p
))
407 c
= tolower((unsigned char)*p
);
408 if (c
>= 'a' && c
<= 'f') {
412 break; /* Not a hexadecimal digit. */
417 /* Convert string pointed to by *str to a bignum. */
419 convert_str2bn(BIGNUM
**val
, char *p
)
425 errx(1, "negative numbers aren't permitted.");
428 if (*p
== 'x' || *p
== 'X')
429 n
= BN_hex2bn(val
, ++p
);
431 n
= is_hex_str(p
) ? BN_hex2bn(val
, p
) : BN_dec2bn(val
, p
);
434 errx(1, "%s: illegal numeric format.", p
);