-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathparamgen.cpp
129 lines (107 loc) · 2.98 KB
/
paramgen.cpp
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/**
* @file paramgen.cpp
*
* @brief Parameter generation utility for Zerocoin.
*
* @author Ian Miers, Christina Garman and Matthew Green
* @date June 2013
*
* @copyright Copyright 2013 Ian Miers, Christina Garman and Matthew Green
* @license This project is released under the MIT license.
**/
using namespace std;
#include <string>
#include <iostream>
#include <fstream>
#include <curses.h>
#include <exception>
#include "Zerocoin.h"
#define DEFAULT_MODULUS_SIZE 3072
#define MIN_MODULUS_SIZE 1026
using namespace libzerocoin;
void
PrintWarning()
{
cout << "Zerocoin parameter generation utility" << endl;
cout << "-------------------------------------" << endl << endl;
cout << "This utility generates an l-bit modulus N as the product of" << endl;
cout << "two safe primes p, q. The values p and q are not stored." << endl;
cout << "Call this program with no arguments to see usage options." << endl;
cout << endl;
cout << "SECURITY WARNING: ZEROCOIN PARAMETERS MUST BE GENERATED BY" << endl;
cout << "A TRUSTED PARTY WHO DOES NOT STORE THE FACTORS. WHILE WE MAKE" << endl;
cout << "A BEST EFFORT TO DESTROY THIS INFORMATION WE DO NOT TAKE" << endl;
cout << "SPECIAL PRECAUTIONS TO ENSURE THAT THEY ARE DESTROYED." << endl;
cout << endl;
cout << "USE THIS UTILITY AT YOUR OWN RISK" << endl << endl;
}
void usage()
{
printf("Usage:\n");
printf(" -b <numbits>\n");
printf(" -o <output file>\n");
exit (8);
}
int main(int argc, char **argv)
{
static Bignum resultModulus(0);
uint32_t numBits = DEFAULT_MODULUS_SIZE;
ofstream outfile;
char* outfileName;
bool writeToFile = false;
while ((argc > 1) && (argv[1][0] == '-'))
{
switch (argv[1][1])
{
case 'b':
numBits = atoi(argv[2]);
++argv;
--argc;
break;
case 'o':
outfileName = argv[2];
writeToFile = true;
break;
case 'h':
usage();
break;
default:
printf("Wrong Argument: %s\n", argv[1]);
usage();
break;
}
++argv;
--argc;
}
if (numBits < MIN_MODULUS_SIZE) {
cout << "Modulus is below minimum length (" << MIN_MODULUS_SIZE << ") bits" << endl;
return(0);
}
PrintWarning();
cout << "Modulus size set to " << numBits << " bits." << endl;
cout << "Generating parameters. This may take a few minutes..." << endl;
// Generate two safe primes "p" and "q"
Bignum *p, *q;
p = new Bignum(0);
q = new Bignum(0);
*p = Bignum::generatePrime(numBits / 2, true);
*q = Bignum::generatePrime(numBits / 2, true);
// Multiply to compute N
resultModulus = (*p) * (*q);
// Wipe out the factors
delete p;
delete q;
// Convert to a hexidecimal string
std::string resultHex = resultModulus.ToString(16);
cout << endl << "N = " << endl << resultHex << endl;
if (writeToFile) {
try {
outfile.open (outfileName);
outfile << resultHex;
outfile.close();
cout << endl << "Result has been written to file '" << outfileName << "'." << endl;
} catch (std::runtime_error &e) {
cout << "Unable to write to file:" << e.what() << endl;
}
}
}