Zeth - Zerocash on Ethereum  0.8
Reference implementation of the Zeth protocol by Clearmatics
mpc_phase2_begin.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 #include "mpc_common.hpp"
8 
9 #include <boost/program_options.hpp>
10 #include <fstream>
11 
12 using namespace libzeth;
13 using pp = defaults::pp;
14 namespace po = boost::program_options;
15 
16 namespace
17 {
18 
19 // Usage:
20 // $0 phase2-begin [<options>] <linear_combination_file> <challenge_out_file>
21 //
22 class mpc_phase2_begin : public mpc_subcommand
23 {
24 private:
25  std::string lin_comb_file;
26  std::string out_file;
27 
28 public:
29  mpc_phase2_begin()
30  : mpc_subcommand("phase2-begin", "Create the initial MPC challenge")
31  , lin_comb_file()
32  , out_file()
33  {
34  }
35 
36 private:
37  void initialize_suboptions(
38  po::options_description &options,
39  po::options_description &all_options,
40  po::positional_options_description &pos) override
41  {
42  all_options.add(options).add_options()(
43  "linear_combination_file",
44  po::value<std::string>(),
45  "linear combination file")(
46  "challenge_out_file",
47  po::value<std::string>(),
48  "initial challenge output file");
49  pos.add("linear_combination_file", 1).add("challenge_out_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("challenge_out_file")) {
58  throw po::error("challenge_out_file not specified");
59  }
60  lin_comb_file = vm["linear_combination_file"].as<std::string>();
61  out_file = vm["challenge_out_file"].as<std::string>();
62  }
63 
64  void subcommand_usage(const char *argv0) override
65  {
66  std::cout << "Usage:\n " << argv0 << " " << subcommand_name
67  << " [<options>] <linear_combination_file> "
68  "<challenge_out_file>\n\n";
69  }
70 
71  int execute_subcommand(const global_options &options) override
72  {
73  if (options.verbose) {
74  std::cout << "lin_comb_file: " << lin_comb_file << "\n";
75  std::cout << "out: " << out_file << std::endl;
76  }
77 
78  libff::enter_block("Load linear combination file");
79  mpc_hash_t cs_hash;
80  srs_mpc_layer_L1<pp> lin_comb =
81  read_from_file_and_hash<srs_mpc_layer_L1<pp>>(
82  lin_comb_file, cs_hash);
83  libff::leave_block("Load linear combination file");
84 
85  // Compute circuit
86  libff::enter_block("Computing num inputs");
87  const size_t num_inputs = [&options]() {
88  libsnark::protoboard<Field> pb;
89  options.protoboard_init(pb);
90  return pb.num_inputs();
91  }();
92  libff::print_indent();
93  std::cout << std::to_string(num_inputs) << std::endl;
94  libff::leave_block("Computing num inputs");
95 
96  // Initial challenge
97  libff::enter_block("Computing initial challenge");
98  const srs_mpc_phase2_challenge<pp> initial_challenge =
99  srs_mpc_phase2_initial_challenge<pp>(
100  srs_mpc_phase2_begin<pp>(cs_hash, lin_comb, num_inputs));
101  libff::leave_block("Computing initial challenge");
102 
103  libff::enter_block("Writing initial challenge");
104  libff::print_indent();
105  std::cout << out_file << std::endl;
106  {
107  std::ofstream out(out_file);
108  initial_challenge.write(out);
109  }
110  libff::leave_block("Writing initial challenge");
111 
112  return 0;
113  }
114 };
115 
116 } // namespace
117 
118 // Subcommand instance
119 mpc_subcommand *mpc_phase2_begin_cmd = new mpc_phase2_begin();
mpc_common.hpp
mpc_utils.hpp
libzeth
Definition: binary_operation.hpp:15
global_options::verbose
bool verbose
Definition: mpc_subcommand.hpp:19
mpc_phase2_begin_cmd
mpc_subcommand * mpc_phase2_begin_cmd
Definition: mpc_phase2_begin.cpp:119
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
pp
defaults::pp pp
Definition: mpc_phase2_begin.cpp:13
libzeth::mpc_hash_t
size_t[MPC_HASH_ARRAY_LENGTH] mpc_hash_t
Definition: mpc_hash.hpp:21
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