Zeth - Zerocash on Ethereum  0.8
Reference implementation of the Zeth protocol by Clearmatics
Classes | Typedefs | Functions
prover_server.cpp File Reference
#include "libtool/tool_util.hpp"
#include "libzeth/circuits/circuit_types.hpp"
#include "libzeth/core/extended_proof.hpp"
#include "libzeth/core/utils.hpp"
#include "libzeth/serialization/proto_utils.hpp"
#include "libzeth/serialization/r1cs_serialization.hpp"
#include "libzeth/serialization/r1cs_variable_assignment_serialization.hpp"
#include "libzeth/zeth_constants.hpp"
#include "zeth_config.h"
#include <boost/program_options.hpp>
#include <fstream>
#include <grpc/grpc.h>
#include <grpcpp/security/server_credentials.h>
#include <grpcpp/server.h>
#include <grpcpp/server_builder.h>
#include <grpcpp/server_context.h>
#include <libsnark/common/data_structures/merkle_tree.hpp>
#include <memory>
#include <stdio.h>
#include <string>
#include <zeth/api/prover.grpc.pb.h>
Include dependency graph for prover_server.cpp:

Go to the source code of this file.

Classes

class  prover_server
 

Typedefs

using pp = libzeth::defaults::pp
 
using Field = libzeth::defaults::Field
 
using snark = libzeth::defaults::snark
 
using api_handler = libzeth::defaults::api_handler
 
using circuit_wrapper = libzeth::JoinsplitCircuitT< pp, snark >
 

Functions

std::string get_server_version ()
 
void display_server_start_message ()
 
int main (int argc, char **argv)
 

Typedef Documentation

◆ api_handler

using api_handler = libzeth::defaults::api_handler

Definition at line 31 of file prover_server.cpp.

◆ circuit_wrapper

Definition at line 32 of file prover_server.cpp.

◆ Field

using Field = libzeth::defaults::Field

Definition at line 29 of file prover_server.cpp.

◆ pp

using pp = libzeth::defaults::pp

Definition at line 28 of file prover_server.cpp.

◆ snark

using snark = libzeth::defaults::snark

Definition at line 30 of file prover_server.cpp.

Function Documentation

◆ display_server_start_message()

void display_server_start_message ( )

Definition at line 275 of file prover_server.cpp.

276 {
277  std::string copyright =
278  "Copyright (c) 2015-2022 Clearmatics Technologies Ltd";
279  std::string license = "SPDX-License-Identifier: LGPL-3.0+";
280  std::string project =
281  "R&D Department: PoC for Zerocash on Ethereum/Autonity";
282  std::string version = get_server_version();
283  std::string warning = "**WARNING:** This code is a research-quality proof "
284  "of concept, DO NOT use in production!";
285 
286  std::cout << "\n=====================================================\n";
287  std::cout << copyright << "\n";
288  std::cout << license << "\n";
289  std::cout << project << "\n";
290  std::cout << version << "\n";
291  std::cout << warning << "\n";
292  std::cout << "=====================================================\n"
293  << std::endl;
294 }
Here is the call graph for this function:

◆ get_server_version()

std::string get_server_version ( )

Definition at line 261 of file prover_server.cpp.

262 {
263  char buffer[100];
264  int n;
265  // Defined in the zethConfig file
266  n = snprintf(
267  buffer, 100, "Version %d.%d", ZETH_VERSION_MAJOR, ZETH_VERSION_MINOR);
268  if (n < 0) {
269  return "Version <Not specified>";
270  }
271  std::string version(buffer);
272  return version;
273 }
Here is the caller graph for this function:

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 334 of file prover_server.cpp.

335 {
336  // Options
337  po::options_description options("Options");
338  options.add_options()("help,h", "This help");
339  options.add_options()(
340  "keypair,k",
341  po::value<boost::filesystem::path>(),
342  "file to load keypair from. If it doesn't exist, a new keypair will be "
343  "generated and written to this file. (default: "
344  "~/zeth_setup/keypair.bin)");
345  options.add_options()(
346  "r1cs,r",
347  po::value<boost::filesystem::path>(),
348  "file in which to export the r1cs (in json format)");
349  options.add_options()(
350  "proving-key-output",
351  po::value<boost::filesystem::path>(),
352  "write proving key to file (if generated)");
353  options.add_options()(
354  "verification-key-output",
355  po::value<boost::filesystem::path>(),
356  "write verification key to file (if generated)");
357  options.add_options()(
358  "extproof-json-output",
359  po::value<boost::filesystem::path>(),
360  "(DEBUG) write generated extended proofs (JSON) to file");
361  options.add_options()(
362  "proof-output",
363  po::value<boost::filesystem::path>(),
364  "(DEBUG) write generated proofs to file");
365  options.add_options()(
366  "primary-output",
367  po::value<boost::filesystem::path>(),
368  "(DEBUG) write primary input to file");
369  options.add_options()(
370  "assignment-output",
371  po::value<boost::filesystem::path>(),
372  "(DEBUG) write full assignment to file (INSECURE!)");
373 
374  auto usage = [&]() {
375  std::cout << "Usage:"
376  << "\n"
377  << " " << argv[0] << " [<options>]\n"
378  << "\n";
379  std::cout << options;
380  std::cout << std::endl;
381  };
382 
383  boost::filesystem::path keypair_file;
384  boost::filesystem::path r1cs_file;
385  boost::filesystem::path proving_key_output_file;
386  boost::filesystem::path verification_key_output_file;
387  boost::filesystem::path extproof_json_output_file;
388  boost::filesystem::path proof_output_file;
389  boost::filesystem::path primary_output_file;
390  boost::filesystem::path assignment_output_file;
391  try {
392  po::variables_map vm;
393  po::store(
394  po::command_line_parser(argc, argv).options(options).run(), vm);
395  if (vm.count("help")) {
396  usage();
397  return 0;
398  }
399  if (vm.count("keypair")) {
400  keypair_file = vm["keypair"].as<boost::filesystem::path>();
401  }
402  if (vm.count("r1cs")) {
403  r1cs_file = vm["r1cs"].as<boost::filesystem::path>();
404  }
405  if (vm.count("proving-key-output")) {
406  proving_key_output_file =
407  vm["proving-key-output"].as<boost::filesystem::path>();
408  }
409  if (vm.count("verification-key-output")) {
410  verification_key_output_file =
411  vm["verification-key-output"].as<boost::filesystem::path>();
412  }
413  if (vm.count("extproof-json-output")) {
414  extproof_json_output_file =
415  vm["extproof-json-output"].as<boost::filesystem::path>();
416  }
417  if (vm.count("proof-output")) {
418  proof_output_file =
419  vm["proof-output"].as<boost::filesystem::path>();
420  }
421  if (vm.count("primary-output")) {
422  primary_output_file =
423  vm["primary-output"].as<boost::filesystem::path>();
424  }
425  if (vm.count("assignment-output")) {
426  assignment_output_file =
427  vm["assignment-output"].as<boost::filesystem::path>();
428  }
429  } catch (po::error &error) {
430  std::cerr << " ERROR: " << error.what() << std::endl;
431  usage();
432  return 1;
433  }
434 
435  // Default keypair_file if none given
436  if (keypair_file.empty()) {
437  boost::filesystem::path setup_dir =
439  if (!setup_dir.empty()) {
440  boost::filesystem::create_directories(setup_dir);
441  }
442  keypair_file = setup_dir / "keypair.bin";
443  }
444 
445  // Inititalize the curve parameters
446  std::cout << "[INFO] Init params (" << libzeth::pp_name<pp>() << ")\n";
447  pp::init_public_params();
448 
449  // If the keypair file exists, load and use it, otherwise generate a new
450  // keypair and write it to the file.
451  circuit_wrapper prover;
452  snark::keypair keypair = [&keypair_file,
453  &proving_key_output_file,
454  &verification_key_output_file,
455  &prover]() {
456  if (boost::filesystem::exists(keypair_file)) {
457  std::cout << "[INFO] Loading keypair: " << keypair_file << "\n";
458 
459  snark::keypair keypair;
460  std::ifstream in_s =
461  libtool::open_binary_input_file(keypair_file.c_str());
462  snark::keypair_read_bytes(keypair, in_s);
463  return keypair;
464  }
465 
466  std::cout << "[INFO] No keypair file " << keypair_file
467  << ". Generating.\n";
468  const snark::keypair keypair = prover.generate_trusted_setup();
469 
470  if (!proving_key_output_file.empty()) {
471  std::cout << "[DEBUG] Writing separate proving key to "
472  << proving_key_output_file << "\n";
473  std::ofstream out_s = libtool::open_binary_output_file(
474  proving_key_output_file.c_str());
475  snark::proving_key_write_bytes(keypair.pk, out_s);
476  }
477  if (!verification_key_output_file.empty()) {
478  std::cout << "[DEBUG] Writing separate verification key to "
479  << verification_key_output_file << "\n";
480  std::ofstream out_s = libtool::open_binary_output_file(
481  verification_key_output_file.c_str());
482  snark::verification_key_write_bytes(keypair.vk, out_s);
483  }
484 
485  // Write the keypair last. If something above fails, this same
486  // code-path will be executed again on the next invocation.
487  std::cout << "[INFO] Writing new keypair to " << keypair_file << "\n";
488  std::ofstream out_s =
489  libtool::open_binary_output_file(keypair_file.c_str());
490  snark::keypair_write_bytes(keypair, out_s);
491 
492  return keypair;
493  }();
494 
495  // If a file is given, export the JSON representation of the constraint
496  // system.
497  if (!r1cs_file.empty()) {
498  std::cout << "[INFO] Writing R1CS to " << r1cs_file << "\n";
499  std::ofstream r1cs_stream(r1cs_file.c_str());
500  libzeth::r1cs_write_json(prover.get_constraint_system(), r1cs_stream);
501  }
502 
503  std::cout << "[INFO] Setup successful, starting the server..." << std::endl;
504  RunServer(
505  prover,
506  keypair,
507  extproof_json_output_file,
508  proof_output_file,
509  primary_output_file,
510  assignment_output_file);
511  return 0;
512 }
Here is the call graph for this function:
libzeth::circuit_wrapper
Definition: circuit_wrapper.hpp:27
libzeth::circuit_wrapper::get_constraint_system
const libsnark::r1cs_constraint_system< Field > & get_constraint_system() const
get_server_version
std::string get_server_version()
Definition: prover_server.cpp:261
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
setup.version
version
Definition: setup.py:21
libzeth::r1cs_write_json
std::ostream & r1cs_write_json(const libsnark::r1cs_constraint_system< FieldT > &r1cs, std::ostream &out_s)
libtool::open_binary_input_file
std::ifstream open_binary_input_file(const std::string &filename)
Definition: tool_util.cpp:10
libzeth::get_path_to_setup_directory
boost::filesystem::path get_path_to_setup_directory()
Definition: filesystem_util.cpp:10
libzeth::circuit_wrapper::generate_trusted_setup
snarkT::keypair generate_trusted_setup() const