2020-10-11 20:18:34 +03:00
|
|
|
/* $NetBSD: factor.c,v 1.36 2020/10/11 17:18:34 christos Exp $ */
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
1995-03-23 11:28:00 +03:00
|
|
|
* Copyright (c) 1989, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-03-21 12:45:37 +03:00
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Landon Curt Noll.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2003-08-07 13:36:50 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1993-03-21 12:45:37 +03:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
2020-10-04 01:27:00 +03:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
#ifdef __COPYRIGHT
|
2008-07-20 04:52:39 +04:00
|
|
|
__COPYRIGHT("@(#) Copyright (c) 1989, 1993\
|
2020-10-04 01:27:00 +03:00
|
|
|
The Regents of the University of California. All rights reserved.");
|
|
|
|
#endif
|
|
|
|
#ifdef __SCCSID
|
|
|
|
__SCCSID("@(#)factor.c 8.4 (Berkeley) 5/4/95");
|
|
|
|
#endif
|
|
|
|
#ifdef __RCSID
|
2020-10-11 20:18:34 +03:00
|
|
|
__RCSID("$NetBSD: factor.c,v 1.36 2020/10/11 17:18:34 christos Exp $");
|
2020-10-04 01:27:00 +03:00
|
|
|
#endif
|
|
|
|
#ifdef __FBSDID
|
|
|
|
__FBSDID("$FreeBSD: head/usr.bin/factor/factor.c 356666 2020-01-12 20:25:11Z gad $");
|
1995-03-23 11:28:00 +03:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* factor - factor a number into primes
|
|
|
|
*
|
2020-10-04 01:27:00 +03:00
|
|
|
* By: Landon Curt Noll chongo@toad.com, ...!{sun,tolsoft}!hoptoad!chongo
|
|
|
|
*
|
|
|
|
* chongo <for a good prime call: 391581 * 2^216193 - 1> /\oo/\
|
1993-03-21 12:45:37 +03:00
|
|
|
*
|
|
|
|
* usage:
|
2020-10-04 01:27:00 +03:00
|
|
|
* factor [-hx] [number] ...
|
1993-03-21 12:45:37 +03:00
|
|
|
*
|
2020-10-04 01:27:00 +03:00
|
|
|
* The form of the output is:
|
1993-03-21 12:45:37 +03:00
|
|
|
*
|
|
|
|
* number: factor1 factor1 factor2 factor3 factor3 factor3 ...
|
|
|
|
*
|
2005-06-28 00:49:31 +04:00
|
|
|
* where factor1 <= factor2 <= factor3 <= ...
|
1993-03-21 12:45:37 +03:00
|
|
|
*
|
2017-11-12 02:48:44 +03:00
|
|
|
* If the -h flag is specified, the output is in "human-readable" format.
|
|
|
|
* Duplicate factors are printed in the form of x^n.
|
|
|
|
*
|
2020-10-04 01:27:00 +03:00
|
|
|
* If the -x flag is specified numbers are printed in hex.
|
|
|
|
*
|
2017-11-12 02:48:44 +03:00
|
|
|
* If no number args are given, the list of numbers are read from stdin.
|
2020-10-04 01:27:00 +03:00
|
|
|
* If no args are given, the list of numbers are read from stdin.
|
1993-03-21 12:45:37 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <ctype.h>
|
2002-06-15 06:12:23 +04:00
|
|
|
#include <err.h>
|
1995-03-23 11:28:00 +03:00
|
|
|
#include <errno.h>
|
2020-10-04 01:27:00 +03:00
|
|
|
#include <inttypes.h>
|
1995-03-23 11:28:00 +03:00
|
|
|
#include <limits.h>
|
2020-10-04 01:27:00 +03:00
|
|
|
#include <stdbool.h>
|
1995-03-23 11:28:00 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
1997-01-07 14:56:32 +03:00
|
|
|
#include <unistd.h>
|
2020-10-04 01:27:00 +03:00
|
|
|
|
|
|
|
#include "primes.h"
|
1995-03-23 11:28:00 +03:00
|
|
|
|
2002-06-17 02:24:00 +04:00
|
|
|
#ifdef HAVE_OPENSSL
|
2020-10-04 01:27:00 +03:00
|
|
|
|
2002-06-15 06:12:23 +04:00
|
|
|
#include <openssl/bn.h>
|
2020-10-04 01:27:00 +03:00
|
|
|
|
|
|
|
#define PRIME_CHECKS 5
|
|
|
|
|
2020-10-11 20:18:34 +03:00
|
|
|
/* print factors for big numbers */
|
|
|
|
static void pollard_pminus1(BIGNUM *, int, int);
|
2020-10-04 01:27:00 +03:00
|
|
|
|
2002-06-17 02:24:00 +04:00
|
|
|
#else
|
2020-10-04 01:27:00 +03:00
|
|
|
|
2002-06-17 02:24:00 +04:00
|
|
|
typedef long BIGNUM;
|
|
|
|
typedef u_long BN_ULONG;
|
2020-10-04 01:27:00 +03:00
|
|
|
|
|
|
|
#define BN_CTX int
|
|
|
|
#define BN_CTX_new() NULL
|
2020-10-06 00:11:47 +03:00
|
|
|
#define BN_new() calloc(sizeof(BIGNUM), 1)
|
|
|
|
#define BN_free(a) free(a)
|
|
|
|
|
|
|
|
#define BN_copy(a, b) *(a) = *(b)
|
|
|
|
#define BN_cmp(a, b) (*(a) - *(b))
|
2002-06-19 03:07:36 +04:00
|
|
|
#define BN_is_zero(v) (*(v) == 0)
|
|
|
|
#define BN_is_one(v) (*(v) == 1)
|
2002-06-17 02:24:00 +04:00
|
|
|
#define BN_mod_word(a, b) (*(a) % (b))
|
2002-06-15 06:12:23 +04:00
|
|
|
|
2020-10-06 00:11:47 +03:00
|
|
|
static BIGNUM *BN_dup(const BIGNUM *);
|
2020-10-04 01:27:00 +03:00
|
|
|
static int BN_dec2bn(BIGNUM **, const char *);
|
|
|
|
static int BN_hex2bn(BIGNUM **, const char *);
|
2009-08-12 09:48:04 +04:00
|
|
|
static BN_ULONG BN_div_word(BIGNUM *, BN_ULONG);
|
2020-10-04 01:27:00 +03:00
|
|
|
static void BN_print_fp(FILE *, const BIGNUM *);
|
|
|
|
|
2002-06-17 02:24:00 +04:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
|
2020-10-04 01:27:00 +03:00
|
|
|
static void BN_print_dec_fp(FILE *, const BIGNUM *);
|
|
|
|
static void convert_str2bn(BIGNUM **, char *);
|
|
|
|
static bool is_hex_str(char *);
|
|
|
|
static void pr_fact(BIGNUM *, int, int); /* print factors of a value */
|
2020-10-11 20:18:34 +03:00
|
|
|
static void pr_print(BIGNUM *, int, int); /* print a prime */
|
2020-10-05 15:22:49 +03:00
|
|
|
static void usage(void) __dead;
|
2002-06-19 03:07:36 +04:00
|
|
|
|
2020-10-04 01:27:00 +03:00
|
|
|
static BN_CTX *ctx; /* just use a global context */
|
2002-06-19 03:07:36 +04:00
|
|
|
|
1995-03-23 11:28:00 +03:00
|
|
|
int
|
2002-06-15 06:12:23 +04:00
|
|
|
main(int argc, char *argv[])
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2002-06-15 06:12:23 +04:00
|
|
|
BIGNUM *val;
|
2020-10-04 01:27:00 +03:00
|
|
|
int ch, hflag, xflag;
|
2002-06-15 06:12:23 +04:00
|
|
|
char *p, buf[LINE_MAX]; /* > max number of digits. */
|
|
|
|
|
2020-10-04 01:27:00 +03:00
|
|
|
hflag = xflag = 0;
|
2002-06-17 19:43:52 +04:00
|
|
|
ctx = BN_CTX_new();
|
2002-06-15 06:12:23 +04:00
|
|
|
val = BN_new();
|
|
|
|
if (val == NULL)
|
|
|
|
errx(1, "can't initialise bignum");
|
1995-03-23 11:28:00 +03:00
|
|
|
|
2020-10-04 01:27:00 +03:00
|
|
|
while ((ch = getopt(argc, argv, "hx")) != -1)
|
1995-03-23 11:28:00 +03:00
|
|
|
switch (ch) {
|
2017-11-12 02:48:44 +03:00
|
|
|
case 'h':
|
2020-10-04 01:27:00 +03:00
|
|
|
hflag++;
|
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
xflag++;
|
2017-11-12 02:48:44 +03:00
|
|
|
break;
|
1995-03-23 11:28:00 +03:00
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage();
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1995-03-23 11:28:00 +03:00
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
/* No args supplied, read numbers from stdin. */
|
|
|
|
if (argc == 0)
|
|
|
|
for (;;) {
|
|
|
|
if (fgets(buf, sizeof(buf), stdin) == NULL) {
|
|
|
|
if (ferror(stdin))
|
|
|
|
err(1, "stdin");
|
|
|
|
exit (0);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2010-05-13 21:52:11 +04:00
|
|
|
for (p = buf; isblank((unsigned char)*p); ++p);
|
1995-03-23 11:28:00 +03:00
|
|
|
if (*p == '\n' || *p == '\0')
|
|
|
|
continue;
|
2020-10-04 01:27:00 +03:00
|
|
|
convert_str2bn(&val, p);
|
|
|
|
pr_fact(val, hflag, xflag);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1995-03-23 11:28:00 +03:00
|
|
|
/* Factor the arguments. */
|
|
|
|
else
|
2020-10-04 01:27:00 +03:00
|
|
|
for (p = *argv; p != NULL; p = *++argv) {
|
|
|
|
convert_str2bn(&val, p);
|
|
|
|
pr_fact(val, hflag, xflag);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1995-03-23 11:28:00 +03:00
|
|
|
exit(0);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
2020-10-07 22:48:29 +03:00
|
|
|
static void
|
|
|
|
pr_exp(int i, int xflag)
|
|
|
|
{
|
|
|
|
printf(xflag && i > 9 ? "^0x%x" : "^%d", i);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
pr_uint64(uint64_t i, int xflag)
|
|
|
|
{
|
|
|
|
printf(xflag ? " 0x%" PRIx64 : " %" PRIu64, i);
|
|
|
|
}
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
|
|
|
* pr_fact - print the factors of a number
|
|
|
|
*
|
|
|
|
* Print the factors of the number, from the lowest to the highest.
|
2020-10-04 01:27:00 +03:00
|
|
|
* A factor will be printed multiple times if it divides the value
|
|
|
|
* multiple times.
|
2017-11-12 02:48:44 +03:00
|
|
|
*
|
|
|
|
* If hflag is specified, duplicate factors are printed in "human-
|
|
|
|
* readable" form of x^n.
|
1993-03-21 12:45:37 +03:00
|
|
|
*
|
2020-10-04 01:27:00 +03:00
|
|
|
* If the xflag is specified, numbers will be printed in hex.
|
|
|
|
*
|
1993-03-21 12:45:37 +03:00
|
|
|
* Factors are printed with leading tabs.
|
|
|
|
*/
|
2009-08-12 09:48:04 +04:00
|
|
|
static void
|
2020-10-04 01:27:00 +03:00
|
|
|
pr_fact(BIGNUM *val, int hflag, int xflag)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2017-11-12 02:48:44 +03:00
|
|
|
int i;
|
2020-10-04 01:27:00 +03:00
|
|
|
const uint64_t *fact; /* The factor found. */
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1995-03-23 11:28:00 +03:00
|
|
|
/* Firewall - catch 0 and 1. */
|
2020-10-04 01:27:00 +03:00
|
|
|
if (BN_is_zero(val)) /* Historical practice; 0 just exits. */
|
|
|
|
exit(0);
|
|
|
|
if (BN_is_one(val)) {
|
|
|
|
printf("1: 1\n");
|
|
|
|
return;
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1995-03-23 11:28:00 +03:00
|
|
|
/* Factor value. */
|
2002-06-15 06:12:23 +04:00
|
|
|
|
2020-10-04 01:27:00 +03:00
|
|
|
if (xflag) {
|
|
|
|
fputs("0x", stdout);
|
|
|
|
BN_print_fp(stdout, val);
|
|
|
|
} else
|
|
|
|
BN_print_dec_fp(stdout, val);
|
2002-06-15 06:12:23 +04:00
|
|
|
putchar(':');
|
|
|
|
for (fact = &prime[0]; !BN_is_one(val); ++fact) {
|
1995-03-23 11:28:00 +03:00
|
|
|
/* Look for the smallest factor. */
|
2020-10-04 01:27:00 +03:00
|
|
|
do {
|
2002-06-15 06:12:23 +04:00
|
|
|
if (BN_mod_word(val, (BN_ULONG)*fact) == 0)
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
2020-10-04 01:27:00 +03:00
|
|
|
} while (++fact <= pr_limit);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1995-03-23 11:28:00 +03:00
|
|
|
/* Watch for primes larger than the table. */
|
1993-03-21 12:45:37 +03:00
|
|
|
if (fact > pr_limit) {
|
2002-06-17 02:24:00 +04:00
|
|
|
#ifdef HAVE_OPENSSL
|
2002-06-17 19:43:52 +04:00
|
|
|
BIGNUM *bnfact;
|
|
|
|
|
|
|
|
bnfact = BN_new();
|
2020-10-04 01:27:00 +03:00
|
|
|
BN_set_word(bnfact, *(fact - 1));
|
|
|
|
if (!BN_sqr(bnfact, bnfact, ctx))
|
|
|
|
errx(1, "error in BN_sqr()");
|
|
|
|
if (BN_cmp(bnfact, val) > 0 ||
|
|
|
|
BN_is_prime_ex(val, PRIME_CHECKS, NULL, NULL) == 1)
|
2020-10-11 20:18:34 +03:00
|
|
|
pr_print(val, hflag, xflag);
|
2020-10-04 01:27:00 +03:00
|
|
|
else
|
2020-10-11 20:18:34 +03:00
|
|
|
pollard_pminus1(val, hflag, xflag);
|
2002-06-17 02:24:00 +04:00
|
|
|
#else
|
2020-10-11 20:18:34 +03:00
|
|
|
pr_print(val, hflag, xflag);
|
2002-06-17 02:24:00 +04:00
|
|
|
#endif
|
2020-10-11 20:18:34 +03:00
|
|
|
pr_print(NULL, hflag, xflag);
|
1995-03-23 11:28:00 +03:00
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
1995-03-23 11:28:00 +03:00
|
|
|
/* Divide factor out until none are left. */
|
2017-11-12 02:48:44 +03:00
|
|
|
i = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
do {
|
2017-11-12 02:48:44 +03:00
|
|
|
i++;
|
|
|
|
if (!hflag)
|
2020-10-07 22:48:29 +03:00
|
|
|
pr_uint64(*fact, xflag);
|
2002-06-15 06:12:23 +04:00
|
|
|
BN_div_word(val, (BN_ULONG)*fact);
|
|
|
|
} while (BN_mod_word(val, (BN_ULONG)*fact) == 0);
|
1995-03-23 11:28:00 +03:00
|
|
|
|
2017-11-12 02:48:44 +03:00
|
|
|
if (hflag) {
|
2020-10-07 22:48:29 +03:00
|
|
|
pr_uint64(*fact, xflag);
|
2017-11-12 02:48:44 +03:00
|
|
|
if (i > 1)
|
2020-10-07 22:48:29 +03:00
|
|
|
pr_exp(i, xflag);
|
2017-11-12 02:48:44 +03:00
|
|
|
}
|
|
|
|
|
1995-03-23 11:28:00 +03:00
|
|
|
/* Let the user know we're doing something. */
|
2002-06-15 06:12:23 +04:00
|
|
|
fflush(stdout);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2002-06-15 06:12:23 +04:00
|
|
|
putchar('\n');
|
|
|
|
}
|
|
|
|
|
2009-08-12 09:48:04 +04:00
|
|
|
static void
|
2020-10-11 20:18:34 +03:00
|
|
|
pr_print(BIGNUM *val, int hflag, int xflag)
|
2002-06-15 06:12:23 +04:00
|
|
|
{
|
2020-10-06 00:11:47 +03:00
|
|
|
static BIGNUM *sval;
|
|
|
|
static int ex = 1;
|
2020-10-11 20:18:34 +03:00
|
|
|
BIGNUM *pval;
|
|
|
|
|
|
|
|
if (hflag) {
|
|
|
|
if (sval == NULL) {
|
|
|
|
sval = BN_dup(val);
|
|
|
|
return;
|
|
|
|
}
|
2020-10-06 00:11:47 +03:00
|
|
|
|
2020-10-11 20:18:34 +03:00
|
|
|
if (val != NULL && BN_cmp(val, sval) == 0) {
|
|
|
|
ex++;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
pval = sval;
|
|
|
|
} else if (val == NULL) {
|
2020-10-06 00:11:47 +03:00
|
|
|
return;
|
2020-10-11 20:18:34 +03:00
|
|
|
} else {
|
|
|
|
pval = val;
|
2020-10-06 00:11:47 +03:00
|
|
|
}
|
|
|
|
|
2020-10-04 01:27:00 +03:00
|
|
|
if (xflag) {
|
|
|
|
fputs(" 0x", stdout);
|
2020-10-11 20:18:34 +03:00
|
|
|
BN_print_fp(stdout, pval);
|
2020-10-04 01:27:00 +03:00
|
|
|
} else {
|
|
|
|
putchar(' ');
|
2020-10-11 20:18:34 +03:00
|
|
|
BN_print_dec_fp(stdout, pval);
|
2020-10-04 01:27:00 +03:00
|
|
|
}
|
2020-10-06 00:11:47 +03:00
|
|
|
|
2020-10-11 20:18:34 +03:00
|
|
|
if (hflag) {
|
|
|
|
if (ex > 1)
|
|
|
|
pr_exp(ex, xflag);
|
|
|
|
|
|
|
|
if (val != NULL) {
|
|
|
|
BN_copy(sval, val);
|
|
|
|
} else {
|
|
|
|
BN_free(sval);
|
|
|
|
sval = NULL;
|
|
|
|
}
|
|
|
|
ex = 1;
|
2020-10-06 00:11:47 +03:00
|
|
|
}
|
1995-03-23 11:28:00 +03:00
|
|
|
}
|
|
|
|
|
2020-10-04 01:27:00 +03:00
|
|
|
static void
|
2002-06-15 06:12:23 +04:00
|
|
|
usage(void)
|
1995-03-23 11:28:00 +03:00
|
|
|
{
|
2020-10-07 22:48:29 +03:00
|
|
|
fprintf(stderr, "Usage: %s [-hx] [value ...]\n", getprogname());
|
2020-10-04 01:27:00 +03:00
|
|
|
exit(1);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2002-06-15 06:12:23 +04:00
|
|
|
|
2002-06-17 02:24:00 +04:00
|
|
|
#ifdef HAVE_OPENSSL
|
2020-10-04 01:27:00 +03:00
|
|
|
|
|
|
|
/* pollard p-1, algorithm from Jim Gillogly, May 2000 */
|
2009-08-12 09:48:04 +04:00
|
|
|
static void
|
2020-10-11 20:18:34 +03:00
|
|
|
pollard_pminus1(BIGNUM *val, int hflag, int xflag)
|
2002-06-15 06:12:23 +04:00
|
|
|
{
|
2020-10-04 01:27:00 +03:00
|
|
|
BIGNUM *base, *rbase, *num, *i, *x;
|
2002-06-15 06:12:23 +04:00
|
|
|
|
2020-10-04 01:27:00 +03:00
|
|
|
base = BN_new();
|
|
|
|
rbase = BN_new();
|
2010-04-27 22:11:19 +04:00
|
|
|
num = BN_new();
|
2020-10-04 01:27:00 +03:00
|
|
|
i = BN_new();
|
|
|
|
x = BN_new();
|
|
|
|
|
|
|
|
BN_set_word(rbase, 1);
|
|
|
|
newbase:
|
|
|
|
if (!BN_add_word(rbase, 1))
|
|
|
|
errx(1, "error in BN_add_word()");
|
|
|
|
BN_set_word(i, 2);
|
|
|
|
BN_copy(base, rbase);
|
2002-06-15 06:12:23 +04:00
|
|
|
|
|
|
|
for (;;) {
|
2020-10-04 01:27:00 +03:00
|
|
|
BN_mod_exp(base, base, i, val, ctx);
|
|
|
|
if (BN_is_one(base))
|
|
|
|
goto newbase;
|
|
|
|
|
|
|
|
BN_copy(x, base);
|
|
|
|
BN_sub_word(x, 1);
|
|
|
|
if (!BN_gcd(x, x, val, ctx))
|
|
|
|
errx(1, "error in BN_gcd()");
|
|
|
|
|
|
|
|
if (!BN_is_one(x)) {
|
|
|
|
if (BN_is_prime_ex(x, PRIME_CHECKS, NULL, NULL) == 1)
|
2020-10-11 20:18:34 +03:00
|
|
|
pr_print(x, hflag, xflag);
|
2020-10-04 01:27:00 +03:00
|
|
|
else
|
2020-10-11 20:18:34 +03:00
|
|
|
pollard_pminus1(x, hflag, xflag);
|
2002-06-15 06:12:23 +04:00
|
|
|
fflush(stdout);
|
|
|
|
|
2020-10-04 01:27:00 +03:00
|
|
|
BN_div(num, NULL, val, x, ctx);
|
2002-06-15 06:12:23 +04:00
|
|
|
if (BN_is_one(num))
|
|
|
|
return;
|
2020-10-04 01:27:00 +03:00
|
|
|
if (BN_is_prime_ex(num, PRIME_CHECKS, NULL,
|
|
|
|
NULL) == 1) {
|
2020-10-11 20:18:34 +03:00
|
|
|
pr_print(num, hflag, xflag);
|
2002-06-15 06:12:23 +04:00
|
|
|
fflush(stdout);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
BN_copy(val, num);
|
|
|
|
}
|
2020-10-04 01:27:00 +03:00
|
|
|
if (!BN_add_word(i, 1))
|
|
|
|
errx(1, "error in BN_add_word()");
|
2002-06-15 06:12:23 +04:00
|
|
|
}
|
|
|
|
}
|
2020-10-04 01:27:00 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Sigh.. No _decimal_ output to file functions in BN.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
BN_print_dec_fp(FILE *fp, const BIGNUM *num)
|
2002-06-17 02:24:00 +04:00
|
|
|
{
|
|
|
|
char *buf;
|
|
|
|
|
2020-10-04 01:27:00 +03:00
|
|
|
buf = BN_bn2dec(num);
|
|
|
|
if (buf == NULL)
|
|
|
|
return; /* XXX do anything here? */
|
|
|
|
fprintf(fp, "%s", buf);
|
|
|
|
free(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
static void
|
|
|
|
BN_print_fp(FILE *fp, const BIGNUM *num)
|
|
|
|
{
|
|
|
|
fprintf(fp, "%lx", (unsigned long)*num);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
BN_print_dec_fp(FILE *fp, const BIGNUM *num)
|
|
|
|
{
|
|
|
|
fprintf(fp, "%lu", (unsigned long)*num);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
BN_dec2bn(BIGNUM **a, const char *str)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
**a = strtoul(str, &p, 10);
|
|
|
|
return (errno == 0 ? 1 : 0); /* OpenSSL returns 0 on error! */
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
BN_hex2bn(BIGNUM **a, const char *str)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
**a = strtoul(str, &p, 16);
|
|
|
|
return (errno == 0 ? 1 : 0); /* OpenSSL returns 0 on error! */
|
2002-06-17 02:24:00 +04:00
|
|
|
}
|
|
|
|
|
2009-08-12 09:48:04 +04:00
|
|
|
static BN_ULONG
|
2002-06-17 02:24:00 +04:00
|
|
|
BN_div_word(BIGNUM *a, BN_ULONG b)
|
|
|
|
{
|
|
|
|
BN_ULONG mod;
|
|
|
|
|
|
|
|
mod = *a % b;
|
|
|
|
*a /= b;
|
|
|
|
return mod;
|
|
|
|
}
|
2020-10-04 01:27:00 +03:00
|
|
|
|
2020-10-06 00:11:47 +03:00
|
|
|
static BIGNUM *
|
|
|
|
BN_dup(const BIGNUM *a)
|
|
|
|
{
|
|
|
|
BIGNUM *b = BN_new();
|
|
|
|
BN_copy(b, a);
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
2002-06-17 02:24:00 +04:00
|
|
|
#endif
|
2020-10-04 01:27:00 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Scan the string from left-to-right to see if the longest substring
|
|
|
|
* is a valid hexadecimal number.
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
is_hex_str(char *str)
|
|
|
|
{
|
|
|
|
char c, *p;
|
|
|
|
bool saw_hex = false;
|
|
|
|
|
|
|
|
for (p = str; *p; p++) {
|
|
|
|
if (isdigit((unsigned char)*p))
|
|
|
|
continue;
|
|
|
|
c = tolower((unsigned char)*p);
|
|
|
|
if (c >= 'a' && c <= 'f') {
|
|
|
|
saw_hex = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break; /* Not a hexadecimal digit. */
|
|
|
|
}
|
|
|
|
return saw_hex;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert string pointed to by *str to a bignum. */
|
|
|
|
static void
|
|
|
|
convert_str2bn(BIGNUM **val, char *p)
|
|
|
|
{
|
|
|
|
int n = 0;
|
|
|
|
|
|
|
|
if (*p == '+') p++;
|
|
|
|
if (*p == '-')
|
|
|
|
errx(1, "negative numbers aren't permitted.");
|
2020-10-05 17:31:30 +03:00
|
|
|
if (*p == '0' && (p[1] == 'x' || p[1] == 'X')) {
|
|
|
|
n = BN_hex2bn(val, p + 2);
|
2020-10-04 01:27:00 +03:00
|
|
|
} else {
|
|
|
|
n = is_hex_str(p) ? BN_hex2bn(val, p) : BN_dec2bn(val, p);
|
|
|
|
}
|
|
|
|
if (n == 0)
|
|
|
|
errx(1, "%s: illegal numeric format.", p);
|
|
|
|
}
|