Anoncoin  0.9.4
P2P Digital Currency
paramgen.cpp
Go to the documentation of this file.
1 
13 using namespace std;
14 
15 #include <string>
16 #include <iostream>
17 #include <fstream>
18 #include <curses.h>
19 #include <exception>
20 #include "Zerocoin.h"
21 
22 #define DEFAULT_MODULUS_SIZE 3072
23 #define MIN_MODULUS_SIZE 1026
24 
25 using namespace libzerocoin;
26 
27 void
29 {
30  cout << "Zerocoin parameter generation utility" << endl;
31  cout << "-------------------------------------" << endl << endl;
32  cout << "This utility generates an l-bit modulus N as the product of" << endl;
33  cout << "two safe primes p, q. The values p and q are not stored." << endl;
34  cout << "Call this program with no arguments to see usage options." << endl;
35  cout << endl;
36  cout << "SECURITY WARNING: ZEROCOIN PARAMETERS MUST BE GENERATED BY" << endl;
37  cout << "A TRUSTED PARTY WHO DOES NOT STORE THE FACTORS. WHILE WE MAKE" << endl;
38  cout << "A BEST EFFORT TO DESTROY THIS INFORMATION WE DO NOT TAKE" << endl;
39  cout << "SPECIAL PRECAUTIONS TO ENSURE THAT THEY ARE DESTROYED." << endl;
40  cout << endl;
41  cout << "USE THIS UTILITY AT YOUR OWN RISK" << endl << endl;
42 }
43 
44 void usage()
45 {
46  printf("Usage:\n");
47  printf(" -b <numbits>\n");
48  printf(" -o <output file>\n");
49 
50  exit (8);
51 }
52 
53 int main(int argc, char **argv)
54 {
55  static Bignum resultModulus(0);
56  uint32_t numBits = DEFAULT_MODULUS_SIZE;
57  ofstream outfile;
58  char* outfileName;
59  bool writeToFile = false;
60 
61  while ((argc > 1) && (argv[1][0] == '-'))
62  {
63  switch (argv[1][1])
64  {
65  case 'b':
66  numBits = atoi(argv[2]);
67  ++argv;
68  --argc;
69  break;
70 
71  case 'o':
72  outfileName = argv[2];
73  writeToFile = true;
74  break;
75 
76  case 'h':
77  usage();
78  break;
79 
80  default:
81  printf("Wrong Argument: %s\n", argv[1]);
82  usage();
83  break;
84  }
85 
86  ++argv;
87  --argc;
88  }
89 
90  if (numBits < MIN_MODULUS_SIZE) {
91  cout << "Modulus is below minimum length (" << MIN_MODULUS_SIZE << ") bits" << endl;
92  return(0);
93  }
94 
95  PrintWarning();
96 
97  cout << "Modulus size set to " << numBits << " bits." << endl;
98  cout << "Generating parameters. This may take a few minutes..." << endl;
99 
100  // Generate two safe primes "p" and "q"
101  Bignum *p, *q;
102  p = new Bignum(0);
103  q = new Bignum(0);
104  *p = Bignum::generatePrime(numBits / 2, true);
105  *q = Bignum::generatePrime(numBits / 2, true);
106 
107  // Multiply to compute N
108  resultModulus = (*p) * (*q);
109 
110  // Wipe out the factors
111  delete p;
112  delete q;
113 
114  // Convert to a hexidecimal string
115  std::string resultHex = resultModulus.ToString(16);
116 
117  cout << endl << "N = " << endl << resultHex << endl;
118 
119  if (writeToFile) {
120  try {
121  outfile.open (outfileName);
122  outfile << resultHex;
123  outfile.close();
124  cout << endl << "Result has been written to file '" << outfileName << "'." << endl;
125  } catch (std::runtime_error &e) {
126  cout << "Unable to write to file:" << e.what() << endl;
127  }
128  }
129 }
void usage()
Definition: paramgen.cpp:44
#define MIN_MODULUS_SIZE
Definition: paramgen.cpp:23
int main(int argc, char **argv)
Definition: paramgen.cpp:53
void PrintWarning()
Definition: paramgen.cpp:28
Exceptions and constants for Zerocoin.
#define DEFAULT_MODULUS_SIZE
Definition: paramgen.cpp:22
int atoi(const std::string &str)
Definition: util.h:235