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. 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.
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
32 * @(#) Copyright (c) 1989, 1993 The Regents of the University of California. All rights reserved.
33 * @(#)factor.c 8.4 (Berkeley) 5/4/95
34 * $NetBSD: factor.c,v 1.13 2002/06/18 23:07:36 simonb Exp $
35 * $FreeBSD: src/games/factor/factor.c,v 1.9.2.2 2002/10/23 14:59:14 fanf Exp $
39 * factor - factor a number into primes
41 * By: Landon Curt Noll chongo@toad.com, ...!{sun,tolsoft}!hoptoad!chongo
43 * chongo <for a good prime call: 391581 * 2^216193 - 1> /\oo/\
46 * factor [-h] [number] ...
48 * The form of the output is:
50 * number: factor1 factor1 factor2 factor3 factor3 factor3 ...
52 * where factor1 < factor2 < factor3 < ...
54 * If no args are given, the list of numbers are read from stdin.
68 #include <openssl/bn.h>
69 #define PRIME_CHECKS 5
70 static void pollard_pminus1(BIGNUM
*); /* print factors for big numbers */
73 typedef u_long BN_ULONG
;
76 #define BN_CTX_new() NULL
77 #define BN_new() ((BIGNUM *)calloc(sizeof(BIGNUM), 1))
78 #define BN_is_zero(v) (*(v) == 0)
79 #define BN_is_one(v) (*(v) == 1)
80 #define BN_mod_word(a, b) (*(a) % (b))
82 static int BN_dec2bn(BIGNUM
**a
, const char *str
);
83 static int BN_hex2bn(BIGNUM
**a
, const char *str
);
84 static BN_ULONG
BN_div_word(BIGNUM
*, BN_ULONG
);
85 static void BN_print_fp(FILE *, const BIGNUM
*);
88 static void BN_print_dec_fp(FILE *, const BIGNUM
*);
90 static void pr_fact(BIGNUM
*); /* print factors of a value */
91 static void pr_print(BIGNUM
*); /* print a prime */
92 static void usage(void);
94 static BN_CTX
*ctx
; /* just use a global context */
98 main(int argc
, char *argv
[])
102 char *p
, buf
[LINE_MAX
]; /* > max number of digits. */
107 errx(1, "can't initialise bignum");
109 while ((ch
= getopt(argc
, argv
, "h")) != -1)
121 /* No args supplied, read numbers from stdin. */
124 if (fgets(buf
, sizeof(buf
), stdin
) == NULL
) {
129 for (p
= buf
; isblank(*p
); ++p
);
130 if (*p
== '\n' || *p
== '\0')
133 errx(1, "negative numbers aren't permitted.");
134 if (BN_dec2bn(&val
, buf
) == 0 &&
135 BN_hex2bn(&val
, buf
) == 0)
136 errx(1, "%s: illegal numeric format.", buf
);
139 /* Factor the arguments. */
141 for (; *argv
!= NULL
; ++argv
) {
142 if (argv
[0][0] == '-')
143 errx(1, "negative numbers aren't permitted.");
144 if (BN_dec2bn(&val
, argv
[0]) == 0 &&
145 BN_hex2bn(&val
, argv
[0]) == 0)
146 errx(1, "%s: illegal numeric format.", argv
[0]);
153 * pr_fact - print the factors of a number
155 * Print the factors of the number, from the lowest to the highest.
156 * A factor will be printed multiple times if it divides the value
159 * Factors are printed with leading tabs.
164 const ubig
*fact
; /* The factor found. */
166 /* Firewall - catch 0 and 1. */
167 if (BN_is_zero(val
)) /* Historical practice; 0 just exits. */
169 if (BN_is_one(val
)) {
178 BN_print_fp(stdout
, val
);
180 BN_print_dec_fp(stdout
, val
);
182 for (fact
= &prime
[0]; !BN_is_one(val
); ++fact
) {
183 /* Look for the smallest factor. */
185 if (BN_mod_word(val
, (BN_ULONG
)*fact
) == 0)
187 } while (++fact
<= pr_limit
);
189 /* Watch for primes larger than the table. */
190 if (fact
> pr_limit
) {
195 BN_set_word(bnfact
, *(fact
- 1));
196 BN_sqr(bnfact
, bnfact
, ctx
);
197 if (BN_cmp(bnfact
, val
) > 0)
200 pollard_pminus1(val
);
207 /* Divide factor out until none are left. */
209 printf(hflag
? " 0x%lx" : " %lu", *fact
);
210 BN_div_word(val
, (BN_ULONG
)*fact
);
211 } while (BN_mod_word(val
, (BN_ULONG
)*fact
) == 0);
213 /* Let the user know we're doing something. */
220 pr_print(BIGNUM
*val
)
223 fputs(" 0x", stdout
);
224 BN_print_fp(stdout
, val
);
227 BN_print_dec_fp(stdout
, val
);
234 fprintf(stderr
, "usage: factor [-h] [value ...]\n");
240 /* pollard p-1, algorithm from Jim Gillogly, May 2000 */
242 pollard_pminus1(BIGNUM
*val
)
244 BIGNUM
*base
, *num
, *i
, *x
;
252 BN_set_word(base
, 2);
255 BN_mod_exp(base
, base
, i
, val
, ctx
);
259 BN_gcd(x
, x
, val
, ctx
);
262 if (BN_is_prime(x
, PRIME_CHECKS
, NULL
, NULL
,
269 BN_div(num
, NULL
, val
, x
, ctx
);
272 if (BN_is_prime(num
, PRIME_CHECKS
, NULL
, NULL
,
285 * Sigh.. No _decimal_ output to file functions in BN.
288 BN_print_dec_fp(FILE *fp
, const BIGNUM
*num
)
292 buf
= BN_bn2dec(num
);
294 return; /* XXX do anything here? */
295 fprintf(fp
, "%s", buf
);
302 BN_print_fp(FILE *fp
, const BIGNUM
*num
)
304 fprintf(fp
, "%lx", (unsigned long)*num
);
308 BN_print_dec_fp(FILE *fp
, const BIGNUM
*num
)
310 fprintf(fp
, "%lu", (unsigned long)*num
);
314 BN_dec2bn(BIGNUM
**a
, const char *str
)
319 **a
= strtoul(str
, &p
, 10);
320 return (errno
== 0 && (*p
== '\n' || *p
== '\0'));
324 BN_hex2bn(BIGNUM
**a
, const char *str
)
329 **a
= strtoul(str
, &p
, 16);
330 return (errno
== 0 && (*p
== '\n' || *p
== '\0'));
334 BN_div_word(BIGNUM
*a
, BN_ULONG b
)