Zecale - Reconciling Privacy and Scalability on Smart-Contract Chains  0.5
Reference implementation of the Zecale protocol by Clearmatics
Classes | Typedefs | Functions
aggregator_server.cpp File Reference
#include "libzecale/circuits/aggregator_circuit.hpp"
#include "libzecale/core/application_pool.hpp"
#include "libzecale/serialization/proto_utils.hpp"
#include "zecale_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 <iostream>
#include <libsnark/common/data_structures/merkle_tree.hpp>
#include <libtool/tool_util.hpp>
#include <libzeth/circuits/circuit_types.hpp>
#include <libzeth/core/utils.hpp>
#include <libzeth/serialization/proto_utils.hpp>
#include <libzeth/serialization/r1cs_serialization.hpp>
#include <libzeth/zeth_constants.hpp>
#include <map>
#include <memory>
#include <stdio.h>
#include <string>
#include <zecale/api/aggregator.grpc.pb.h>
Include dependency graph for aggregator_server.cpp:

Go to the source code of this file.

Classes

class  aggregator_server
 

Typedefs

using npp = libsnark::other_curve< wpp >
 
using nsnark = typename nverifier::snark
 
using aggregator_circuit = libzecale::aggregator_circuit< wpp, wsnark, nverifier, batch_size >
 

Functions

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

Typedef Documentation

◆ aggregator_circuit

using aggregator_circuit = libzecale::aggregator_circuit<wpp, wsnark, nverifier, batch_size>

Definition at line 76 of file aggregator_server.cpp.

◆ npp

using npp = libsnark::other_curve<wpp>

Definition at line 49 of file aggregator_server.cpp.

◆ nsnark

using nsnark = typename nverifier::snark

Definition at line 70 of file aggregator_server.cpp.

Function Documentation

◆ display_server_start_message()

void display_server_start_message ( )

Definition at line 343 of file aggregator_server.cpp.

344 {
345  std::string copyright =
346  "Copyright (c) 2015-2022 Clearmatics Technologies Ltd";
347  std::string license = "SPDX-License-Identifier: LGPL-3.0+";
348  std::string project = "R&D Department: PoC for a privacy preserving "
349  "scalability solution on Ethereum";
350  std::string version = get_server_version();
351  std::string warning = "**WARNING:** This code is a research-quality proof "
352  "of concept, DO NOT use in production!";
353 
354  std::cout << "\n=====================================================\n";
355  std::cout << copyright << "\n";
356  std::cout << license << "\n";
357  std::cout << project << "\n";
358  std::cout << version << "\n";
359  std::cout << warning << "\n";
360  std::cout << "=====================================================\n"
361  << std::endl;
362 }
Here is the call graph for this function:

◆ get_server_version()

std::string get_server_version ( )

Definition at line 325 of file aggregator_server.cpp.

326 {
327  char buffer[100];
328  int n;
329  // Defined in the zecale_config file
330  n = snprintf(
331  buffer,
332  100,
333  "Version %d.%d",
334  ZECALE_VERSION_MAJOR,
335  ZECALE_VERSION_MINOR);
336  if (n < 0) {
337  return "Version <Not specified>";
338  }
339  std::string version(buffer);
340  return version;
341 }
Here is the caller graph for this function:

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 392 of file aggregator_server.cpp.

393 {
394  // Options
395  po::options_description options("");
396  options.add_options()(
397  "keypair,k",
398  po::value<boost::filesystem::path>(),
399  "file to load keypair from");
400 #ifdef DEBUG
401  options.add_options()(
402  "r1cs,r",
403  po::value<boost::filesystem::path>(),
404  "file in which to export the r1cs in json format");
405 #endif
406 
407  auto usage = [&]() {
408  std::cout << "Usage:"
409  << "\n"
410  << " " << argv[0] << " [<options>]\n"
411  << "\n";
412  std::cout << options;
413  std::cout << std::endl;
414  };
415 
416  boost::filesystem::path keypair_file;
417  boost::filesystem::path r1cs_file;
418  try {
419  po::variables_map vm;
420  po::store(
421  po::command_line_parser(argc, argv).options(options).run(), vm);
422  if (vm.count("help")) {
423  usage();
424  return 0;
425  }
426  if (vm.count("keypair")) {
427  keypair_file = vm["keypair"].as<boost::filesystem::path>();
428  }
429  if (vm.count("r1cs")) {
430  r1cs_file = vm["r1cs"].as<boost::filesystem::path>();
431  }
432  } catch (po::error &error) {
433  std::cerr << " ERROR: " << error.what() << std::endl;
434  usage();
435  return 1;
436  }
437 
438  // Default keypair_file if none given
439  if (keypair_file.empty()) {
440  boost::filesystem::path setup_dir =
441  libzeth::get_path_to_setup_directory();
442  if (!setup_dir.empty()) {
443  boost::filesystem::create_directories(setup_dir);
444  }
445  keypair_file = setup_dir / "zecale_keypair.bin";
446  }
447 
448  // Inititalize the curve parameters
449  std::cout << "[INFO] Init params of both curves" << std::endl;
450  npp::init_public_params();
451  wpp::init_public_params();
452 
453  // Set up the aggregator circuit
454  aggregator_circuit aggregator(num_inputs_per_nested_proof);
455 
456  // Load or generate the keypair
457  wsnark::keypair keypair = [&keypair_file, &aggregator]() {
458  if (boost::filesystem::exists(keypair_file)) {
459  std::cout << "[INFO] Loading keypair: " << keypair_file << "\n";
460  wsnark::keypair keypair;
461  std::ifstream in_s =
462  libtool::open_binary_input_file(keypair_file.c_str());
463  wsnark::keypair_read_bytes(keypair, in_s);
464 
465  // Check the VK is for the correct number of inputs.
466  if (keypair.vk.ABC_g1.size() != aggregator.num_primary_inputs()) {
467  throw std::invalid_argument("invalid VK");
468  }
469 
470  return keypair;
471  }
472 
473  std::cout << "[INFO] No keypair file " << keypair_file
474  << ". Generating.\n";
475  const wsnark::keypair keypair = aggregator.generate_trusted_setup();
476 
477  // Check the VK is for the correct number of inputs.
478  if (keypair.vk.ABC_g1.size() != aggregator.num_primary_inputs()) {
479  throw std::invalid_argument("invalid VK");
480  }
481 
482  const size_t num_constraints =
483  aggregator.get_constraint_system().num_constraints();
484  std::cout << "[INFO] Circuit has " << std::to_string(num_constraints)
485  << " constraints\n";
486 
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  wsnark::keypair_write_bytes(keypair, out_s);
491  return keypair;
492  }();
493 
494  // If a file has been given for the JSON representation of the circuit,
495  // write it out.
496  if (!r1cs_file.empty()) {
497  std::cout << "[INFO] Writing R1CS to " << std::endl;
498  std::ofstream r1cs_stream(r1cs_file.c_str());
499  libzeth::r1cs_write_json(
500  aggregator.get_constraint_system(), r1cs_stream);
501  }
502 
503  // Launch the server
504  std::cout << "[INFO] Setup successful, starting the server..." << std::endl;
505  RunServer(aggregator, keypair);
506  return 0;
507 }
setup.version
version
Definition: setup.py:19
libzecale::aggregator_circuit
Definition: aggregator_circuit.hpp:33
get_server_version
std::string get_server_version()
Definition: aggregator_server.cpp:325