Zeth - Zerocash on Ethereum  0.8
Reference implementation of the Zeth protocol by Clearmatics
prove_cmd.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 
7 #include "libtool/tool_util.hpp"
10 
11 namespace zethtool
12 {
13 
14 namespace commands
15 {
16 
17 class prove_cmd : public generic_subcommand<prove_cmd>
18 {
19 public:
21 
23  const std::string &subcommand_name, const std::string &description)
26  , profile(false)
27  {
28  }
29 
30  template<typename ppT, typename snarkT>
32  {
33  ppT::init_public_params();
34  if (!profile) {
35  libff::inhibit_profiling_info = true;
36  libff::inhibit_profiling_counters = true;
37  }
38 
39  typename snarkT::proving_key proving_key;
40  {
41  std::ifstream in_s = libtool::open_binary_input_file(pk_file);
42  snarkT::proving_key_read_bytes(proving_key, in_s);
43  }
44 
45  libsnark::r1cs_primary_input<libff::Fr<ppT>> primary;
46  libsnark::r1cs_auxiliary_input<libff::Fr<ppT>> auxiliary;
47  {
48  std::ifstream in_s =
51  primary, auxiliary, num_primary_inputs, in_s);
52  }
53 
54  typename snarkT::proof proof =
55  snarkT::generate_proof(proving_key, primary, auxiliary);
56 
57  // Write to output file
58  std::cout << "Writing proof to file: " << proof_file << "\n";
59  {
60  std::ofstream out_s = libtool::open_binary_output_file(proof_file);
61  snarkT::proof_write_bytes(proof, out_s);
62  }
63 
64  return 0;
65  }
66 
67 protected:
69  boost::program_options::options_description &options,
70  boost::program_options::options_description &all_options,
71  boost::program_options::positional_options_description &pos) override
72  {
73  base_class::initialize_suboptions(options, all_options, pos);
74 
75  options.add_options()(
76  "primary_inputs,p",
77  po::value<uint16_t>(),
78  "Number of primary inputs (default: 1)")(
79  "profile,r", "Enable profiling output");
80 
81  all_options.add(options).add_options()(
82  "pk_file", po::value<std::string>(), "Proving key file");
83  all_options.add_options()(
84  "assignment_file", po::value<std::string>(), "Assignment file");
85  all_options.add_options()(
86  "proof_file", po::value<std::string>(), "(Output) Proof file");
87 
88  pos.add("pk_file", 1);
89  pos.add("assignment_file", 1);
90  pos.add("proof_file", 1);
91  }
92 
94  const boost::program_options::variables_map &vm) override
95  {
97 
98  if (vm.count("pk_file") == 0) {
99  throw po::error("pk_file not specified");
100  }
101  if (vm.count("assignment_file") == 0) {
102  throw po::error("assignment_file not specified");
103  }
104  if (vm.count("proof_file") == 0) {
105  throw po::error("proof_file not specified");
106  }
107 
108  pk_file = vm["pk_file"].as<std::string>();
109  assignment_file = vm["assignment_file"].as<std::string>();
110  proof_file = vm["proof_file"].as<std::string>();
111  if (vm.count("primary_inputs")) {
112  num_primary_inputs = vm["primary_inputs"].as<uint16_t>();
113  }
114  profile = (bool)vm.count("profile");
115  }
116 
117  void subcommand_usage(const char *argv0) override
118  {
119  std::cout << "Usage:\n"
120  " "
121  << argv0
122  << " prove [pk_file] [assignment_file] [proof_file]\n";
123  }
124 
125  std::string pk_file;
126  std::string assignment_file;
127  std::string proof_file;
129  bool profile;
130 };
131 
132 } // namespace commands
133 
135  "prove", "Generate proof given proving key and assignment");
136 
137 } // namespace zethtool
zethtool::generic_subcommand< prove_cmd >::parse_suboptions
void parse_suboptions(const boost::program_options::variables_map &vm) override
Definition: tool_common.hpp:111
libtool::subcommand::description
const std::string & description() const
commands
Definition: __init__.py:1
zethtool::generic_subcommand< prove_cmd >::initialize_suboptions
void initialize_suboptions(boost::program_options::options_description &options, boost::program_options::options_description &, boost::program_options::positional_options_description &) override
Definition: tool_common.hpp:97
tool_util.hpp
zethtool::generic_subcommand
Definition: tool_common.hpp:87
global_options
Definition: mpc_subcommand.hpp:15
tool_common.hpp
zethtool::commands::prove_cmd::execute_generic
int execute_generic(const global_options &)
Definition: prove_cmd.cpp:31
libtool::open_binary_output_file
std::ofstream open_binary_output_file(const std::string &filename)
Utility function to open a binary file for writing, with appropriate flags.
Definition: tool_util.cpp:19
libtool::subcommand::subcommand_name
std::string subcommand_name
Definition: subcommand.hpp:54
zethtool::commands::prove_cmd::assignment_file
std::string assignment_file
Definition: prove_cmd.cpp:126
zethtool::commands::prove_cmd::subcommand_usage
void subcommand_usage(const char *argv0) override
Any command-specific output for usage.
Definition: prove_cmd.cpp:117
zethtool::commands::prove_cmd
Definition: prove_cmd.cpp:17
libtool::open_binary_input_file
std::ifstream open_binary_input_file(const std::string &filename)
Definition: tool_util.cpp:10
zethtool::prove_cmd
zeth_subcommand * prove_cmd
Definition: prove_cmd.cpp:134
zethtool::commands::prove_cmd::parse_suboptions
void parse_suboptions(const boost::program_options::variables_map &vm) override
Definition: prove_cmd.cpp:93
libzeth::r1cs_variable_assignment_read_bytes
void r1cs_variable_assignment_read_bytes(libsnark::r1cs_variable_assignment< FieldT > &assignment, std::istream &in_s)
zethtool::commands::prove_cmd::initialize_suboptions
void initialize_suboptions(boost::program_options::options_description &options, boost::program_options::options_description &all_options, boost::program_options::positional_options_description &pos) override
Instantiation can now set up the boost program_options structures.
Definition: prove_cmd.cpp:68
zethtool::commands::prove_cmd::prove_cmd
prove_cmd(const std::string &subcommand_name, const std::string &description)
Definition: prove_cmd.cpp:22
libtool::subcommand
Class representing a tool subcommand.
Definition: subcommand.hpp:18
zethtool::commands::prove_cmd::pk_file
std::string pk_file
Definition: prove_cmd.cpp:125
zethtool
Definition: dump_proof_cmd.cpp:11
zethtool::commands::prove_cmd::proof_file
std::string proof_file
Definition: prove_cmd.cpp:127
zethtool::commands::prove_cmd::num_primary_inputs
uint16_t num_primary_inputs
Definition: prove_cmd.cpp:128
r1cs_variable_assignment_serialization.hpp
prove_cmd.hpp
zethtool::commands::prove_cmd::profile
bool profile
Definition: prove_cmd.cpp:129