Zeth - Zerocash on Ethereum  0.8
Reference implementation of the Zeth protocol by Clearmatics
pot_process.cpp
Go to the documentation of this file.
1 // Copyright (c) 2015-2022 Clearmatics Technologies Ltd
2 //
3 // SPDX-License-Identifier: LGPL-3.0+
4 
7 
9 #include "zeth_config.h"
10 
11 #include <boost/program_options.hpp>
12 #include <fstream>
13 
14 using namespace libzeth;
15 using pp = defaults::pp;
16 namespace po = boost::program_options;
17 
18 // -----------------------------------------------------------------------------
19 // cli_options
20 // -----------------------------------------------------------------------------
21 
22 // Usage:
23 // pot-process [<options>] <powersoftau file> <degree>
24 //
25 // Options:
26 // -h,--help This message
27 // -v,--verbose Verbose
28 // --check Check pot well-formedness and exit
29 // --out <file> Write the lagrange polynomial values to this file
30 // ("lagrange-radix2-<n>")
31 // --lagrange-degree <l> Use degree l instead of n (l < n)
32 // --dummy Create dummy powersoftau data (for testing only!)
34 {
35 public:
36  po::options_description desc;
37  po::options_description all_desc;
38  po::positional_options_description pos;
39 
40  std::string command;
41  bool help;
42  std::string powersoftau_file;
43  size_t degree;
44  bool verbose;
45  bool check;
46  bool dummy;
47  std::string out;
49 
50  cli_options();
51  void parse(int argc, char **argv);
52  void usage() const;
53 };
54 
56  : desc("Options")
57  , all_desc("")
58  , pos()
59  , command("pot-process")
60  , help(false)
61  , powersoftau_file()
62  , degree(0)
63  , verbose(false)
64  , check(false)
65  , dummy(false)
66  , out()
67  , lagrange_degree(0)
68 {
69  desc.add_options()("help,h", "This help")("verbose,v", "Verbose output")(
70  "check", "Check pot well-formedness and exit")(
71  "out,o", po::value<std::string>(), "Output file")(
72  "lagrange-degree", po::value<size_t>(), "Use degree l")(
73  "dummy", "Create dummy powersoftau data (!for testing only)");
74  all_desc.add(desc).add_options()(
75  "powersoftau_file", po::value<std::string>(), "powersoftau file")(
76  "degree", po::value<size_t>(), "degree");
77  pos.add("powersoftau_file", 1).add("degree", 1);
78 }
79 
80 void cli_options::usage() const
81 {
82  std::cout << "Usage:" << std::endl
83  << " " << command
84  << " [<options>] <powersoftau file> <degree>\n\n"
85  << desc << std::endl;
86 }
87 
88 void cli_options::parse(int argc, char **argv)
89 {
90  po::variables_map vm;
91  po::parsed_options parsed = po::command_line_parser(argc, argv)
92  .options(all_desc)
93  .positional(pos)
94  .run();
95  po::store(parsed, vm);
96 
97  command = argv[0];
98 
99  if (vm.count("help")) {
100  help = true;
101  return;
102  }
103 
104  if (0 == vm.count("powersoftau_file")) {
105  throw po::error("powersoftau_file not specified");
106  }
107  if (0 == vm.count("degree")) {
108  throw po::error("degree not specified");
109  }
110 
111  powersoftau_file = vm["powersoftau_file"].as<std::string>();
112  verbose = vm.count("verbose");
113  check = vm.count("check");
114  degree = vm["degree"].as<size_t>();
115  lagrange_degree = vm.count("lagrange-degree")
116  ? vm["lagrange-degree"].as<size_t>()
117  : degree;
118  out = vm.count("out") ? vm["out"].as<std::string>()
119  : "lagrange-" + std::to_string(lagrange_degree);
120  dummy = vm.count("dummy");
121 
122  if (dummy && check) {
123  throw po::error("specify at most one of --dummy and --check");
124  }
125 }
126 
127 // -----------------------------------------------------------------------------
128 // main
129 // -----------------------------------------------------------------------------
130 
131 static int powersoftau_main(const cli_options &options)
132 {
133  // Initialize
134  if (options.help) {
135  options.usage();
136  return 0;
137  }
138 
139  if (options.verbose) {
140  std::cout << " command: " << options.command << "\n";
141  std::cout << " help: " << std::to_string(options.help) << "\n";
142  std::cout << " powersoftau_file: " << options.powersoftau_file << "\n";
143  std::cout << " degree: " << std::to_string(options.degree) << "\n";
144  std::cout << " verbose: " << std::to_string(options.verbose) << "\n";
145  std::cout << " check: " << std::to_string(options.check) << std::endl;
146  std::cout << " out: " << options.out << "\n";
147  std::cout << " lagrange_degree: "
148  << std::to_string(options.lagrange_degree) << std::endl;
149  }
150 
151  pp::init_public_params();
152  if (!options.verbose) {
153  libff::inhibit_profiling_counters = true;
154  libff::inhibit_profiling_info = true;
155  }
156 
157  // If --dummy options was given, create a powersoftau struct from
158  // local randomness, write it out and return.
159  if (options.dummy) {
160  const srs_powersoftau<pp> dummy = dummy_powersoftau<pp>(options.degree);
161  std::cout << "Writing locally constructed powersoftau to "
162  << options.powersoftau_file << " ... ";
163  std::ofstream out(
164  options.powersoftau_file,
165  std::ios_base::binary | std::ios_base::out);
166  powersoftau_write(out, dummy);
167 
168  std::cout << "DONE" << std::endl;
169  return 0;
170  }
171 
172  // Read in powersoftau
173  std::ifstream in(
174  options.powersoftau_file, std::ios_base::binary | std::ios_base::in);
175  const srs_powersoftau<pp> powersoftau =
176  powersoftau_load<pp>(in, options.degree);
177  in.close();
178 
179  // If --check was given, run the well-formedness check and stop.
180  if (options.check) {
181  if (!powersoftau_is_well_formed(powersoftau)) {
182  std::cerr << "Invalid powersoftau file" << std::endl;
183  return 1;
184  }
185 
186  std::cout << "powersoftau file is valid" << std::endl;
187  return 0;
188  }
189 
192  powersoftau, options.lagrange_degree);
193 
194  std::cout << "Writing Lagrange polynomial values to " << options.out
195  << " ... ";
196  std::ofstream out(options.out, std::ios_base::binary | std::ios_base::out);
197  lagrange.write(out);
198  out.close();
199 
200  std::cout << "DONE" << std::endl;
201  return 0;
202 }
203 
204 int main(int argc, char **argv)
205 {
206  cli_options options;
207 
208  // Parse options
209  try {
210  options.parse(argc, argv);
211  } catch (po::error &error) {
212  std::cerr << " ERROR: " << error.what() << std::endl;
213  std::cout << std::endl;
214  options.usage();
215  return 1;
216  }
217 
218  // Execute and handle errors
219  try {
220  return powersoftau_main(options);
221  } catch (std::invalid_argument &error) {
222  std::cerr << " ERROR: " << error.what() << std::endl;
223  std::cout << std::endl;
224  return 1;
225  }
226 }
cli_options::pos
po::positional_options_description pos
Definition: pot_process.cpp:38
cli_options
Definition: pot_process.cpp:33
cli_options::cli_options
cli_options()
Definition: pot_process.cpp:55
cli_options::powersoftau_file
std::string powersoftau_file
Definition: pot_process.cpp:42
cli_options::desc
po::options_description desc
Definition: pot_process.cpp:36
libzeth
Definition: binary_operation.hpp:15
cli_options::verbose
bool verbose
Definition: pot_process.cpp:44
cli_options::dummy
bool dummy
Definition: pot_process.cpp:46
cli_options::help
bool help
Definition: pot_process.cpp:41
libzeth::srs_lagrange_evaluations
Definition: mpc_utils.hpp:32
cli_options::check
bool check
Definition: pot_process.cpp:45
cli_options::usage
void usage() const
Definition: pot_process.cpp:80
test_commands.deploy_test_token.help
help
Definition: deploy_test_token.py:21
cli_options::lagrange_degree
size_t lagrange_degree
Definition: pot_process.cpp:48
cli_options::command
std::string command
Definition: pot_process.cpp:40
libzeth::srs_powersoftau
Definition: mpc_utils.hpp:31
pp
defaults::pp pp
Definition: mpc_create_keypair.cpp:14
cli_options::degree
size_t degree
Definition: pot_process.cpp:43
powersoftau_utils.hpp
cli_options::parse
void parse(int argc, char **argv)
Definition: pot_process.cpp:88
libzeth::powersoftau_compute_lagrange_evaluations
srs_lagrange_evaluations< ppT > powersoftau_compute_lagrange_evaluations(const srs_powersoftau< ppT > &pot, const size_t n)
libzeth::powersoftau_write
void powersoftau_write(std::ostream &out, const srs_powersoftau< ppT > &pot)
libzeth::powersoftau_is_well_formed
bool powersoftau_is_well_formed(const srs_powersoftau< ppT > &pot)
Verify that the pot data is well formed.
pp
defaults::pp pp
Definition: pot_process.cpp:15
libzeth::srs_lagrange_evaluations::write
void write(std::ostream &out) const
main
int main(int argc, char **argv)
Definition: pot_process.cpp:204
cli_options::all_desc
po::options_description all_desc
Definition: pot_process.cpp:37
cli_options::out
std::string out
Definition: pot_process.cpp:47