Zeth - Zerocash on Ethereum  0.8
Reference implementation of the Zeth protocol by Clearmatics
mpc_dummy_phase2.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"
8 #include "mpc_common.hpp"
9 
10 using namespace libzeth;
11 using pp = defaults::pp;
12 namespace po = boost::program_options;
13 
14 namespace
15 {
16 
17 // Usage:
18 // mpc dummy_phase2 [<option>]
19 // <linear_combination_file>
20 // <final_challenge_file>
21 class mpc_dummy_phase2 : public mpc_subcommand
22 {
23  std::string linear_combination_file;
24  std::string out_file;
25 
26 public:
27  mpc_dummy_phase2()
29  "dummy-phase2", "Run a dummy MPC to generate test data")
30  , linear_combination_file()
31  , out_file()
32  {
33  }
34 
35 private:
36  void initialize_suboptions(
37  po::options_description &options,
38  po::options_description &all_options,
39  po::positional_options_description &pos) override
40  {
41  all_options.add(options).add_options()(
42  "linear_combination_file",
43  po::value<std::string>(),
44  "Linear combination file")(
45  "final_challenge_file",
46  po::value<std::string>(),
47  "Final challenge file");
48  pos.add("linear_combination_file", 1);
49  pos.add("final_challenge_file", 1);
50  }
51 
52  void parse_suboptions(const po::variables_map &vm) override
53  {
54  if (0 == vm.count("linear_combination_file")) {
55  throw po::error("linear_combination file not specified");
56  }
57  if (0 == vm.count("final_challenge_file")) {
58  throw po::error("final_challenge_file not specified");
59  }
60  linear_combination_file =
61  vm["linear_combination_file"].as<std::string>();
62  out_file = vm["final_challenge_file"].as<std::string>();
63  }
64 
65  void subcommand_usage(const char *argv0) override
66  {
67  std::cout << "Usage:\n " << argv0 << " " << subcommand_name
68  << " [<options>] <linear_combination_file> "
69  "<final_challenge_file>\n";
70  }
71 
72  int execute_subcommand(const global_options &options) override
73  {
74  if (options.verbose) {
75  std::cout << "linear_combination_file: " << linear_combination_file
76  << "\n"
77  << "out_file: " << out_file << std::endl;
78  }
79 
80  // Load the linear_combination output
81  libff::enter_block("reading linear combination data");
82  srs_mpc_layer_L1<pp> lin_comb =
83  read_from_file<srs_mpc_layer_L1<pp>>(linear_combination_file);
84  libff::leave_block("reading linear combination data");
85 
86  // Generate the zeth circuit (to determine the number of inputs)
87  libff::enter_block("computing num_inputs");
88  const size_t num_inputs = [&options]() {
89  libsnark::protoboard<Field> pb;
90  options.protoboard_init(pb);
91  return pb.num_inputs();
92  }();
93  libff::print_indent();
94  std::cout << std::to_string(num_inputs) << std::endl;
95  libff::leave_block("computing num_inputs");
96 
97  // Generate a single delta for dummy phase2
98  const Field delta = Field::random_element();
99 
100  // Generate and save the dummy phase2 challenge
101  const srs_mpc_phase2_challenge<pp> phase2 =
102  srs_mpc_dummy_phase2<pp>(lin_comb, delta, num_inputs);
103  libff::enter_block("writing phase2 data");
104  {
105  std::ofstream out(out_file);
106  phase2.write(out);
107  }
108  libff::leave_block("writing phase2 data");
109 
110  return 0;
111  }
112 };
113 
114 } // namespace
115 
116 mpc_subcommand *mpc_dummy_phase2_cmd = new mpc_dummy_phase2();
mpc_common.hpp
mpc_utils.hpp
mpc_dummy_phase2_cmd
mpc_subcommand * mpc_dummy_phase2_cmd
Definition: mpc_dummy_phase2.cpp:116
utils.hpp
Field
libzeth::defaults::Field Field
Definition: mpc_subcommand.hpp:11
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
pp
defaults::pp pp
Definition: mpc_dummy_phase2.cpp:11
mpc_subcommand
libtool::subcommand< global_options > mpc_subcommand
Definition: mpc_subcommand.hpp:22
pp
defaults::pp pp
Definition: mpc_create_keypair.cpp:14
phase2.hpp
libtool::subcommand
Class representing a tool subcommand.
Definition: subcommand.hpp:18
libzeth::srs_mpc_phase2_challenge::write
void write(std::ostream &out) const