#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <openssl/bn.h>
#include "qfile.h"
#define TRIAL_MINIMUM (4)
__dead static void usage(void);
int
main(int argc, char *argv[])
{
BIGNUM *q, *p, *a;
BN_CTX *ctx;
char *cp;
char *lp;
uint32_t count_in = 0;
uint32_t count_out = 0;
uint32_t count_possible = 0;
uint32_t generator_known;
uint32_t generator_wanted = 0;
uint32_t in_tests;
uint32_t in_tries;
uint32_t in_type;
uint32_t in_size;
int trials;
time_t time_start;
time_t time_stop;
setprogname(argv[0]);
if (argc < 2) {
usage();
}
if ((trials = strtoul(argv[1], NULL, 10)) < TRIAL_MINIMUM) {
trials = TRIAL_MINIMUM;
}
if (argc > 2) {
generator_wanted = strtoul(argv[2], NULL, 16);
}
time(&time_start);
p = BN_new();
q = BN_new();
ctx = BN_CTX_new();
(void)fprintf(stderr,
"%.24s Final %d Miller-Rabin trials (%x generator)\n",
ctime(&time_start), trials, generator_wanted);
lp = (char *) malloc((unsigned long) QLINESIZE + 1);
while (fgets(lp, QLINESIZE, stdin) != NULL) {
size_t ll = strlen(lp);
count_in++;
if (ll < 14 || *lp == '!' || *lp == '#') {
#ifdef DEBUGPRINT
(void)fprintf(stderr, "%10lu: comment or short"
" line\n", count_in);
#endif
continue;
}
cp = &lp[14];
in_type = strtoul(cp, &cp, 10);
in_tests = strtoul(cp, &cp, 10);
if (in_tests & QTEST_COMPOSITE) {
#ifdef DEBUGPRINT
(void)fprintf(stderr, "%10lu: known composite\n",
count_in);
#endif
continue;
}
in_tries = (uint32_t) strtoul(cp, &cp, 10);
in_size = (uint32_t) strtoul(cp, &cp, 10);
generator_known = (uint32_t) strtoul(cp, &cp, 16);
cp += strspn(cp, " ");
switch (in_type) {
case QTYPE_SOPHIE_GERMAINE:
#ifdef DEBUGPRINT
(void)fprintf(stderr, "%10lu: (%lu) "
"Sophie-Germaine\n", count_in,
in_type);
#endif
a = q;
BN_hex2bn(&a, cp);
BN_lshift(p, q, 1);
BN_add_word(p, 1UL);
in_size += 1;
generator_known = 0;
break;
default:
#ifdef DEBUGPRINT
(void)fprintf(stderr, "%10lu: (%lu)\n",
count_in, in_type);
#endif
a = p;
BN_hex2bn(&a, cp);
BN_rshift(q, p, 1);
break;
}
if ((uint32_t)BN_num_bits(p) != (in_size + 1)) {
#ifdef DEBUGPRINT
(void)fprintf(stderr, "%10lu: bit size %ul "
"mismatch\n", count_in, in_size);
#endif
continue;
}
if (in_size < QSIZE_MINIMUM) {
#ifdef DEBUGPRINT
(void)fprintf(stderr, "%10lu: bit size %ul "
"too short\n", count_in, in_size);
#endif
continue;
}
if (in_tests & QTEST_MILLER_RABIN)
in_tries += trials;
else
in_tries = trials;
if (generator_known == 0) {
if (BN_mod_word(p, 24UL) == 11)
generator_known = 2;
else if (BN_mod_word(p, 12UL) == 5)
generator_known = 3;
else {
BN_ULONG r = BN_mod_word(p, 10UL);
if (r == 3 || r == 7) {
generator_known = 5;
}
}
}
if (generator_wanted > 0 &&
generator_wanted != generator_known) {
#ifdef DEBUGPRINT
(void)fprintf(stderr,
"%10lu: generator %ld != %ld\n",
count_in, generator_known, generator_wanted);
#endif
continue;
}
count_possible++;
if (BN_is_prime_ex(q, 1, ctx, NULL) <= 0) {
#ifdef DEBUGPRINT
(void)fprintf(stderr, "%10lu: q failed first "
"possible prime test\n", count_in);
#endif
continue;
}
if (!BN_is_prime_ex(p, trials, ctx, NULL)) {
#ifdef DEBUGPRINT
(void)fprintf(stderr, "%10lu: p is not prime\n",
count_in);
#endif
continue;
}
#ifdef DEBUGPRINT
(void)fprintf(stderr, "%10lu: p is almost certainly "
"prime\n", count_in);
#endif
if (!BN_is_prime_ex(q, trials - 1, ctx, NULL)) {
#ifdef DEBUGPRINT
(void)fprintf(stderr, "%10lu: q is not prime\n",
count_in);
#endif
continue;
}
#ifdef DEBUGPRINT
fprintf(stderr, "%10lu: q is almost certainly prime\n",
count_in);
#endif
if (0 > qfileout(stdout,
QTYPE_SAFE,
(in_tests | QTEST_MILLER_RABIN),
in_tries,
in_size,
generator_known,
p)) {
break;
}
count_out++;
#ifdef DEBUGPRINT
fflush(stderr);
fflush(stdout);
#endif
}
time(&time_stop);
free(lp);
BN_free(p);
BN_free(q);
BN_CTX_free(ctx);
fflush(stdout);
(void)fprintf(stderr,
"%.24s Found %u safe primes of %u candidates in %lu seconds\n",
ctime(&time_stop), count_out, count_possible,
(long) (time_stop - time_start));
return (0);
}
static void
usage(void)
{
(void)fprintf(stderr, "Usage: %s <trials> [generator]\n",
getprogname());
exit(1);
}