Zeth - Zerocash on Ethereum  0.8
Reference implementation of the Zeth protocol by Clearmatics
mpc_create_keypair.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 
5 #include "libzeth/core/utils.hpp"
9 #include "mpc_common.hpp"
10 
11 #include <vector>
12 
13 using namespace libzeth;
14 using pp = defaults::pp;
15 namespace po = boost::program_options;
16 
17 namespace
18 {
19 
20 // Usage:
21 // mpc create-keypair [<option>]
22 // <powersoftau_file>
23 // <linear_combination_file>
24 // <phase2_challenge_file>
25 // <keypair_output_file>
26 //
27 // Options:
28 // -h,--help This message
29 // --pot-degree powersoftau degree (assumed to match linear comb)
30 class mpc_create_keypair : public mpc_subcommand
31 {
32 private:
33  std::string powersoftau_file;
34  std::string lin_comb_file;
35  std::string phase2_challenge_file;
36  std::string keypair_out_file;
37  size_t powersoftau_degree;
38 
39 public:
40  mpc_create_keypair()
42  "create-keypair", "Create a full keypair from MPC output")
43  , powersoftau_file()
44  , lin_comb_file()
45  , phase2_challenge_file()
46  , keypair_out_file()
47  , powersoftau_degree(0)
48  {
49  }
50 
51 private:
52  void initialize_suboptions(
53  po::options_description &options,
54  po::options_description &all_options,
55  po::positional_options_description &pos) override
56  {
57  options.add_options()(
58  "pot-degree",
59  po::value<size_t>(),
60  "powersoftau degree (assumed to match linear comb)");
61  all_options.add(options).add_options()(
62  "powersoftau_file", po::value<std::string>(), "powersoftau file")(
63  "linear_combination_file",
64  po::value<std::string>(),
65  "linear combination file")(
66  "phase2_challenge_file",
67  po::value<std::string>(),
68  "phase2 final challenge file")(
69  "keypair_out_file",
70  po::value<std::string>(),
71  "keypair output file");
72  pos.add("powersoftau_file", 1)
73  .add("linear_combination_file", 1)
74  .add("phase2_challenge_file", 1)
75  .add("keypair_out_file", 1);
76  }
77 
78  void parse_suboptions(const po::variables_map &vm) override
79  {
80  if (0 == vm.count("powersoftau_file")) {
81  throw po::error("powersoftau_file not specified");
82  }
83  if (0 == vm.count("linear_combination_file")) {
84  throw po::error("linear_combination_file not specified");
85  }
86  if (0 == vm.count("phase2_challenge_file")) {
87  throw po::error("phase2_challenge_file not specified");
88  }
89  if (0 == vm.count("keypair_out_file")) {
90  throw po::error("keypair_out_file not specified");
91  }
92 
93  powersoftau_file = vm["powersoftau_file"].as<std::string>();
94  lin_comb_file = vm["linear_combination_file"].as<std::string>();
95  phase2_challenge_file = vm["phase2_challenge_file"].as<std::string>();
96  keypair_out_file = vm["keypair_out_file"].as<std::string>();
97  powersoftau_degree =
98  vm.count("pot-degree") ? vm["pot-degree"].as<size_t>() : 0;
99  }
100 
101  void subcommand_usage(const char *argv0) override
102  {
103  std::cout << "Usage:\n"
104  << " " << argv0 << " " << subcommand_name
105  << " [<options>] \\\n"
106  << " <powersoftau_file> <linear_combination_file> \\\n"
107  << " <phase2_challenge_file> <keypair_out_file>\n\n";
108  }
109 
110  int execute_subcommand(const global_options &options) override
111  {
112  if (options.verbose) {
113  std::cout << "powersoftau_file: " << powersoftau_file << "\n"
114  << "lin_comb_file: " << lin_comb_file << "\n"
115  << "phase2_challenge_file: " << phase2_challenge_file
116  << "\n"
117  << "powersoftau_degree: " << powersoftau_degree << "\n"
118  << "out_file: " << keypair_out_file << std::endl;
119  }
120 
121  // Load all data
122  // TODO: Load just degree from lin_comb data, then load everything
123  // in parallel.
124  libff::enter_block("Load linear combination data");
125  libff::print_indent();
126  std::cout << lin_comb_file << std::endl;
127  srs_mpc_layer_L1<pp> lin_comb =
128  read_from_file<srs_mpc_layer_L1<pp>>(lin_comb_file);
129  libff::leave_block("Load linear combination data");
130 
131  libff::enter_block("Load powers of tau");
132  libff::print_indent();
133  std::cout << powersoftau_file << std::endl;
134  srs_powersoftau<pp> pot = [this, &lin_comb]() {
135  std::ifstream in(
136  powersoftau_file, std::ios_base::binary | std::ios_base::in);
137  const size_t pot_degree =
138  powersoftau_degree ? powersoftau_degree : lin_comb.degree();
139  return powersoftau_load<pp>(in, pot_degree);
140  }();
141  libff::leave_block("Load powers of tau");
142 
143  libff::enter_block("Load phase2 data");
144  libff::print_indent();
145  std::cout << phase2_challenge_file << std::endl;
147  read_from_file<srs_mpc_phase2_challenge<pp>>(phase2_challenge_file);
148  libff::leave_block("Load phase2 data");
149 
150  // Compute circuit
151  libff::enter_block("Generate QAP");
152  libsnark::protoboard<Field> pb;
153  options.protoboard_init(pb);
154  libsnark::r1cs_constraint_system<Field> cs = pb.get_constraint_system();
155  const libsnark::qap_instance<Field> qap =
156  libsnark::r1cs_to_qap_instance_map(cs, true);
157  libff::leave_block("Generate QAP");
158 
159  libsnark::r1cs_gg_ppzksnark_keypair<pp> keypair =
160  mpc_create_key_pair<pp>(
161  std::move(pot),
162  std::move(lin_comb),
163  std::move(phase2.accumulator),
164  std::move(cs),
165  qap);
166 
167  // Write keypair to a file
168  libff::enter_block("Writing keypair file");
169  if (!libff::inhibit_profiling_info) {
170  libff::print_indent();
171  std::cout << keypair_out_file << std::endl;
172  }
173  {
174  std::ofstream out(
175  keypair_out_file, std::ios_base::binary | std::ios_base::out);
177  }
178  libff::leave_block("Writing keypair file");
179 
180  return 0;
181  }
182 };
183 
184 } // namespace
185 
186 // Subcommand instance
187 mpc_subcommand *mpc_create_keypair_cmd = new mpc_create_keypair();
libzeth::srs_mpc_layer_L1::degree
size_t degree() const
mpc_common.hpp
mpc_utils.hpp
utils.hpp
libzeth::srs_mpc_phase2_challenge::accumulator
srs_mpc_phase2_accumulator< ppT > accumulator
Definition: phase2.hpp:114
libzeth::groth16_snark::keypair_write_bytes
static void keypair_write_bytes(const keypair &, std::ostream &)
Write a keypair as bytes.
libzeth
Definition: binary_operation.hpp:15
global_options::verbose
bool verbose
Definition: mpc_subcommand.hpp:19
libzeth::srs_mpc_phase2_challenge
Definition: phase2.hpp:110
global_options
Definition: mpc_subcommand.hpp:15
global_options::protoboard_init
ProtoboardInitFn protoboard_init
Definition: mpc_subcommand.hpp:18
libzeth::srs_mpc_layer_L1
Definition: mpc_utils.hpp:37
mpc_subcommand
libtool::subcommand< global_options > mpc_subcommand
Definition: mpc_subcommand.hpp:22
libzeth::srs_powersoftau
Definition: mpc_utils.hpp:31
pp
defaults::pp pp
Definition: mpc_create_keypair.cpp:14
powersoftau_utils.hpp
mpc_create_keypair_cmd
mpc_subcommand * mpc_create_keypair_cmd
Definition: mpc_create_keypair.cpp:187
phase2.hpp
libtool::subcommand
Class representing a tool subcommand.
Definition: subcommand.hpp:18