Zeth - Zerocash on Ethereum  0.8
Reference implementation of the Zeth protocol by Clearmatics
mpc_phase2_verify_contribution.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 
6 #include "mpc_common.hpp"
7 
8 using namespace libzeth;
9 using pp = defaults::pp;
10 namespace po = boost::program_options;
11 
12 namespace
13 {
14 
15 // Usage:
16 // $0 phase2-verify-contribution [<options>] <challenge_file> <response_file>
17 //
18 // Options:
19 // --transcript <file> Append contribution, if it is valid
20 // --new-challenge <file> Write new challenge, if contribution is valid
21 class mpc_phase2_verify_contribution : public mpc_subcommand
22 {
23 private:
24  std::string challenge_file;
25  std::string response_file;
26  std::string transcript_file;
27  std::string new_challenge_file;
28 
29 public:
30  mpc_phase2_verify_contribution()
32  "mpc_phase2_verify_contribution",
33  "Verify contribution and optionally output next challenge")
34  , challenge_file()
35  , response_file()
36  , transcript_file()
37  , new_challenge_file()
38  {
39  }
40 
41 private:
42  void initialize_suboptions(
43  po::options_description &options,
44  po::options_description &all_options,
45  po::positional_options_description &pos) override
46  {
47  options.add_options()(
48  "transcript",
49  po::value<std::string>(),
50  "Append contribution, if it is valid")(
51  "new-challenge",
52  po::value<std::string>(),
53  "Write new challenge, if contribution is valid");
54  all_options.add(options).add_options()(
55  "challenge_file", po::value<std::string>(), "challenge file")(
56  "response_file", po::value<std::string>(), "response file");
57  pos.add("challenge_file", 1).add("response_file", 1);
58  }
59 
60  void parse_suboptions(const po::variables_map &vm) override
61  {
62  if (!vm.count("challenge_file")) {
63  throw po::error("challenge_file not specified");
64  }
65  if (!vm.count("response_file")) {
66  throw po::error("response_file not specified");
67  }
68  challenge_file = vm["challenge_file"].as<std::string>();
69  response_file = vm["response_file"].as<std::string>();
70  transcript_file =
71  vm.count("transcript") ? vm["transcript"].as<std::string>() : "";
72  new_challenge_file = vm.count("new-challenge")
73  ? vm["new-challenge"].as<std::string>()
74  : "";
75  }
76 
77  void subcommand_usage(const char *argv0) override
78  {
79  std::cout << "Usage:\n " << argv0 << " " << subcommand_name
80  << " [<options>] <challenge_file> <response_file>\n\n";
81  }
82 
83  int execute_subcommand(const global_options &options) override
84  {
85  if (options.verbose) {
86  std::cout << "challenge: " << challenge_file << "\n"
87  << "response: " << response_file << "\n"
88  << "transcript: " << transcript_file << "\n"
89  << "new_challenge: " << new_challenge_file << std::endl;
90  }
91 
92  libff::enter_block("Load challenge file");
94  read_from_file<srs_mpc_phase2_challenge<pp>>(challenge_file);
95  libff::leave_block("Load challenge file");
96 
97  libff::enter_block("Load response file");
99  read_from_file<srs_mpc_phase2_response<pp>>(response_file);
100  libff::leave_block("Load response file");
101 
102  libff::enter_block("Verifying response");
103  const bool response_is_valid =
104  srs_mpc_phase2_verify_response(challenge, response);
105  libff::leave_block("Verifying response");
106  if (!response_is_valid) {
107  std::cerr << "Response is invalid" << std::endl;
108  return 1;
109  }
110 
111  // TODO: Backup the transcript file before writing a new version?
112 
113  // If a transcript file has been specified, append this contribution
114  if (!transcript_file.empty()) {
115  libff::enter_block("appending contribution to transcript");
116  std::ofstream out(
117  transcript_file,
118  std::ios_base::binary | std::ios_base::out |
119  std::ios_base::app);
120  response.publickey.write(out);
121  libff::leave_block("appending contribution to transcript");
122  }
123 
124  // If a new-challenge file has been specified, create and write a new
125  // challenge.
126  if (!new_challenge_file.empty()) {
127  libff::enter_block("computing and writing new challenge");
128  srs_mpc_phase2_challenge<pp> new_challenge =
129  srs_mpc_phase2_compute_challenge(std::move(response));
130  std::ofstream out(
131  new_challenge_file, std::ios_base::binary | std::ios_base::out);
132  new_challenge.write(out);
133  libff::leave_block("computing and writing new challenge");
134  }
135 
136  return 0;
137  }
138 };
139 
140 } // namespace
141 
143  new mpc_phase2_verify_contribution();
mpc_common.hpp
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
mpc_subcommand
libtool::subcommand< global_options > mpc_subcommand
Definition: mpc_subcommand.hpp:22
libzeth::srs_mpc_phase2_verify_response
bool srs_mpc_phase2_verify_response(const srs_mpc_phase2_challenge< ppT > &challenge, const srs_mpc_phase2_response< ppT > &response)
pp
defaults::pp pp
Definition: mpc_create_keypair.cpp:14
libzeth::srs_mpc_phase2_response
Definition: phase2.hpp:128
mpc_phase2_verify_contribution_cmd
mpc_subcommand * mpc_phase2_verify_contribution_cmd
Definition: mpc_phase2_verify_contribution.cpp:142
libzeth::srs_mpc_phase2_compute_challenge
srs_mpc_phase2_challenge< ppT > srs_mpc_phase2_compute_challenge(srs_mpc_phase2_response< ppT > &&response)
phase2.hpp
libzeth::srs_mpc_phase2_response::publickey
srs_mpc_phase2_publickey< ppT > publickey
Definition: phase2.hpp:132
libtool::subcommand
Class representing a tool subcommand.
Definition: subcommand.hpp:18
pp
defaults::pp pp
Definition: mpc_phase2_verify_contribution.cpp:9
libzeth::srs_mpc_phase2_challenge::write
void write(std::ostream &out) const