Skip to content

Commit

Permalink
Merge pull request #67 from clearmatics/mpc-separate-utilities
Browse files Browse the repository at this point in the history
[MPC] Utilities for SRS generation via MPC (depends on #48)
  • Loading branch information
AntoineRondelet committed Sep 10, 2019
2 parents 2f9fbce + ef71a30 commit 61df562
Show file tree
Hide file tree
Showing 22 changed files with 1,769 additions and 105 deletions.
2 changes: 1 addition & 1 deletion depends/libsnark
34 changes: 34 additions & 0 deletions scripts/test_pot_process
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env bash

POT=$1
[ "" == "${POT}" ] && POT="build/src/pot-process"

POT_DATA="testdata/powersoftau_response.4.bin"
POT_DATA_DEGREE=16

set -e
set -x

# Check real data
${POT} --check ${POT_DATA} ${POT_DATA_DEGREE}

# Write dummy data
${POT} --dummy /tmp/test_pot-6.bin 64

# Check consistency of dummy data
${POT} --check /tmp/test_pot-6.bin 64

# Generate encoded Lagrange evaluation from real data
${POT} --out /tmp/lagrange-4.bin ${POT_DATA} ${POT_DATA_DEGREE}

# Generate encoded evaluation of Lagrange polynomials of smaller
# degree, from real data
${POT} \
--out /tmp/lagrange-3.bin \
--lagrange-degree $((${POT_DATA_DEGREE} / 2)) \
${POT_DATA} ${POT_DATA_DEGREE}

set +x
echo "=================================================================="
echo "== PASSED =="
echo "=================================================================="
26 changes: 25 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ include_directories(


# Enable Boost for program_options
find_package( Boost REQUIRED COMPONENTS system filesystem )
find_package( Boost REQUIRED COMPONENTS system filesystem program_options )
include_directories( ${Boost_INCLUDE_DIR} )

# Add the binary tree to the search path for include files
Expand Down Expand Up @@ -113,6 +113,25 @@ target_link_libraries(
protobuf::libprotobuf
)

# pot-process utility
add_executable(pot-process pot_process/pot_process.cpp)
target_link_libraries(
pot-process
zeth
${Boost_SYSTEM_LIBRARY}
${Boost_PROGRAM_OPTIONS_LIBRARY}
)

# mpc utility executable
file(GLOB MPC_SOURCE mpc/*.?pp mpc/*.tcc)
add_executable(mpc ${MPC_SOURCE})
target_link_libraries(
mpc
zeth
${Boost_SYSTEM_LIBRARY}
${Boost_PROGRAM_OPTIONS_LIBRARY}
)

## Tests
include(CTest)

Expand Down Expand Up @@ -154,3 +173,8 @@ target_link_libraries(
${Boost_SYSTEM_LIBRARY}
${Boost_FILESYSTEM_LIBRARY}
)
target_link_libraries(
test_mpc
${Boost_SYSTEM_LIBRARY}
${Boost_FILESYSTEM_LIBRARY}
)
11 changes: 3 additions & 8 deletions src/libsnark_helpers/debug_helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include "libsnark_helpers/debug_helpers.hpp"

#include "util.hpp"

namespace libzeth
{

Expand Down Expand Up @@ -62,14 +64,7 @@ std::string outputPointG2AffineAsHex(libff::alt_bn128_G2 _p)

boost::filesystem::path getPathToSetupDir()
{
const char *pathToSetupFolder = std::getenv("ZETH_TRUSTED_SETUP_DIR");
if (pathToSetupFolder == NULL) {
// Fallback destination if the ZETH_TRUSTED_SETUP_DIR env var is not set
pathToSetupFolder = "../trusted_setup";
}

boost::filesystem::path setup_dir(pathToSetupFolder);
return setup_dir;
return boost::filesystem::path(trusted_setup_path("../trusted_setup"));
}

boost::filesystem::path getPathToDebugDir()
Expand Down
13 changes: 13 additions & 0 deletions src/mpc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# mpc command

Performs administrative operations related to the MPC for SRS generation.
Operations performed by this command are specific to the constraint system used
by zeth, relying on the circuit-agnostic `powersoftau` data and pre-computed
Lagrange polynomials evaluations, as computed by the `powersoftau` command.

This command can be used to generate the linear-combination data, which forms
the initial "challenge" of the Phase 2 MPC. Participants compute a "resonse"
for their challenge, which is then processed to create a final keypair and an
auditable transcript of contributions.

For the full list of options, see output of `mpc --help`.
92 changes: 92 additions & 0 deletions src/mpc/mpc.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#include "include_libsnark.hpp"
#include "mpc_common.hpp"

#include <boost/program_options.hpp>

namespace po = boost::program_options;

using ppT = libff::default_ec_pp;

extern subcommand *mpc_linear_combination_cmd;
extern subcommand *mpc_dummy_phase2_cmd;
extern subcommand *mpc_create_keypair_cmd;

int main(int argc, char **argv)
{
ppT::init_public_params();
po::options_description global("");
global.add_options()("help,h", "This help")("verbose,v", "Verbose output");

po::options_description all("");
all.add(global).add_options()(
"command", po::value<std::string>(), "Command to execute")(
"subargs",
po::value<std::vector<std::string>>(),
"Arguments to command");

po::positional_options_description pos;
pos.add("command", 1).add("subargs", -1);

const std::map<std::string, subcommand *> commands{
{"linear-combination", mpc_linear_combination_cmd},
{"dummy-phase2", mpc_dummy_phase2_cmd},
{"create-keypair", mpc_create_keypair_cmd},
};

auto usage = [&argv, &global, &commands]() {
std::cout << "Usage:\n"
<< " " << argv[0]
<< " [<options>] <command> <command-arguments> ...\n\n"
<< global;

std::cout << "\nCommands:\n";
for (const auto &cmd : commands) {
std::cout << " " << cmd.first;
}
std::cout << std::endl;
};

try {
po::variables_map vm;
po::parsed_options parsed = po::command_line_parser(argc, argv)
.options(all)
.positional(pos)
.allow_unregistered()
.run();
po::store(parsed, vm);

if (vm.count("help")) {
usage();
return 0;
}

const bool verbose = (bool)vm.count("verbose");
if (!verbose) {
libff::inhibit_profiling_info = true;
libff::inhibit_profiling_counters = true;
}

if (0 == vm.count("command")) {
std::cerr << "error: no command specified\n";
usage();
return 1;
}

const std::string command(vm["command"].as<std::string>());
std::vector<std::string> subargs =
po::collect_unrecognized(parsed.options, po::include_positional);
subargs[0] = std::string(argv[0]) + " " + subargs[0];

subcommand *sub = commands.find(command)->second;
if (sub == nullptr) {
throw po::error("invalid command");
}

return sub->execute(verbose, subargs);
} catch (po::error &error) {
std::cerr << " ERROR: " << error.what() << std::endl;
usage();
}

return 1;
}
59 changes: 59 additions & 0 deletions src/mpc/mpc_common.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include "mpc_common.hpp"

#include <iostream>

namespace po = boost::program_options;

subcommand::subcommand(const std::string &subcommand_name)
: subcommand_name(subcommand_name), verbose(false), help(false)
{
}

int subcommand::execute(bool verbose, const std::vector<std::string> &args)
{
this->verbose = verbose;

po::options_description options_desc("Options");
po::options_description all_options_desc("");
po::positional_options_description positional_options_desc;

try {
options_desc.add_options()("help,h", "This help"),
initialize_suboptions(
options_desc, all_options_desc, positional_options_desc);

po::variables_map vm;
po::parsed_options parsed =
po::command_line_parser(
std::vector<std::string>(args.begin() + 1, args.end()))
.options(all_options_desc)
.positional(positional_options_desc)
.run();
po::store(parsed, vm);
parse_suboptions(vm);

if (vm.count("help")) {
help = true;
usage(options_desc);
return 0;
}
} catch (po::error &error) {
std::cerr << " ERROR: " << error.what() << std::endl;
usage(options_desc);
return 1;
}

// Execute and handle errors
try {
return execute_subcommand();
} catch (std::invalid_argument &error) {
std::cerr << " ERROR: " << error.what() << std::endl;
return 1;
}
}

void subcommand::usage(const po::options_description &options)
{
subcommand_usage();
std::cout << options << std::endl;
}
40 changes: 40 additions & 0 deletions src/mpc/mpc_common.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <boost/program_options.hpp>
#include <fstream>
#include <string>
#include <vector>

class subcommand
{
protected:
std::string subcommand_name;
bool verbose;

private:
bool help;

public:
subcommand(const std::string &subcommand_name);
int execute(bool verbose, const std::vector<std::string> &args);

private:
void usage(const boost::program_options::options_description &all_options);

virtual void initialize_suboptions(
boost::program_options::options_description &options,
boost::program_options::options_description &all_options,
boost::program_options::positional_options_description &pos) = 0;
virtual void parse_suboptions(
const boost::program_options::variables_map &vm) = 0;
virtual void subcommand_usage() = 0;
virtual int execute_subcommand() = 0;
};

// Utility function to load data objects from a file, using a static read
// method.
template<typename T> inline T read_from_file(const std::string &file_name)
{
std::ifstream in(file_name, std::ios_base::binary | std::ios_base::in);
in.exceptions(
std::ios_base::eofbit | std::ios_base::badbit | std::ios_base::failbit);
return T::read(in);
}
Loading

0 comments on commit 61df562

Please sign in to comment.