-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandomfac.cc
95 lines (78 loc) · 2.22 KB
/
randomfac.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
* Program to test the methods of CFactoredInteger which generate
* random factored integers.
*
* Copyright (C) 2008 by Bryce Allen
*
* Distributed under the MIT license: see COPYING.MIT
*/
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <gmp.h>
#include "include/types.h"
#include "include/randomhelpers.h"
void usage () {
printf ("randomfac [-t targetBits] [-e exactBits] [-l smoothBitLimit]\n");
}
int main(int argc, char **argv) {
int targetBits = 256;
int exactBits = 0;
int smoothBitLimit = 0;
//printf ("argc = %d\n", argc);
char *endptr;
int opt;
while ((opt = getopt (argc, argv, "s:t:e:")) != -1) {
switch (opt) {
case 't':
targetBits = strtol (optarg, &endptr, 10);
if (*endptr != '\0') {
usage ();
exit (1);
}
break;
case 'e':
exactBits = strtol (optarg, &endptr, 10);
if (*endptr != '\0') {
usage ();
exit (1);
}
break;
case 's':
smoothBitLimit = strtol (optarg, &endptr, 10);
if (*endptr != '\0') {
usage ();
exit (1);
}
break;
case ':':
case '?':
usage ();
exit (1);
}
}
gmp_randstate_t rstate;
gmp_randinit_default (rstate);
seedRandState (rstate);
CFactoredInteger fi;
if (smoothBitLimit > 0) {
if (exactBits > 0) {
fi.randomSmoothExactBits (exactBits, rstate, smoothBitLimit);
} else {
fi.randomSmooth (targetBits, rstate, smoothBitLimit);
}
} else {
fi.random (targetBits, rstate);
}
if (fi.hasMallocError ()) {
fputs ("Failed to allocate random factored integer, exiting\n", stderr);
exit (EXIT_FAILURE);
}
gmp_printf ("value = %Zd\n", fi.value);
gmp_printf ("actual bits = %d\n", mpz_sizeinbase (fi.value, 2));
printf ("\tnFactors = %d\n", fi.nFactors);
for (unsigned int i = 0; i < fi.nFactors; i++) {
gmp_printf ("\t%Zd, %d\n", fi.factors[i].prime, fi.factors[i].power);
}
gmp_randclear (rstate);
}