Skip to content

Commit

Permalink
mpc: separate mpc executables for production and test scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
dtebbs committed Sep 16, 2019
1 parent 92a1fbb commit b01d4db
Show file tree
Hide file tree
Showing 11 changed files with 162 additions and 138 deletions.
10 changes: 4 additions & 6 deletions scripts/test_mpc
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
#!/usr/bin/env bash

# Use the --simple-circuit option to test the SRS mpc pipeline

set -x
set -e

POT="build/src/pot-process"
MPC="build/src/mpc"
MPC="build/src/mpc-test"
QAP_DEGREE=8

pot_file=_test_pot-${QAP_DEGREE}.bin
Expand All @@ -23,16 +21,16 @@ ${POT} --dummy ${pot_file} ${QAP_DEGREE}
${POT} --out ${lagrange_file} ${pot_file} ${QAP_DEGREE}

# Generate the linear combination
${MPC} --simple-circuit \
${MPC} \
linear-combination --out ${linear_combination_file} \
${pot_file} ${lagrange_file}

# Create a dummy phase2 file
${MPC} --simple-circuit \
${MPC} \
dummy-phase2 --out ${phase2_file} ${linear_combination_file}

# Create the keypair
${MPC} --simple-circuit \
${MPC} \
create-keypair --out ${keypair_file} \
${pot_file} ${linear_combination_file} ${phase2_file}

Expand Down
18 changes: 13 additions & 5 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -117,17 +117,25 @@ target_link_libraries(
${Boost_PROGRAM_OPTIONS_LIBRARY}
)

# mpc utility executable
file(GLOB MPC_SOURCE mpc/*.?pp mpc/*.tcc)
add_executable(mpc ${MPC_SOURCE})
target_include_directories(mpc PRIVATE mpc)
# mpc library
file(GLOB MPC_LIB_SOURCE mpc/mpc_*.?pp mpc/mpc_*.tcc)
add_library(libmpc ${MPC_LIB_SOURCE})
target_include_directories(libmpc PUBLIC mpc)
target_link_libraries(
mpc
libmpc
zeth
${Boost_SYSTEM_LIBRARY}
${Boost_PROGRAM_OPTIONS_LIBRARY}
)

# mpc utility executable
add_executable(mpc mpc/mpc.cpp)
target_link_libraries(mpc libmpc)

# mpc test utility
add_executable(mpc-test test/mpc_test_cli.cpp)
target_link_libraries(mpc-test libmpc)

## Tests
include(CTest)

Expand Down
106 changes: 16 additions & 90 deletions src/mpc/mpc.cpp
Original file line number Diff line number Diff line change
@@ -1,93 +1,19 @@
#include "mpc_common.hpp"
#include "circuit-wrapper.hpp"
#include "mpc_main.hpp"

#include <boost/program_options.hpp>

namespace po = boost::program_options;

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

int main(int argc, char **argv)
void zeth_protoboard(libsnark::protoboard<FieldT> &pb)
{
ppT::init_public_params();
po::options_description global("");
global.add_options()("help,h", "This help")("verbose,v", "Verbose output")(
"simple-circuit", "Use simple circuit (for testing)");

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;
}

const bool simple_circuit = (bool)vm.count("simple-circuit");

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");
}

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

return 1;
using HashTreeT = MiMC_mp_gadget<FieldT>;
using HashT = sha256_ethereum<FieldT>;

joinsplit_gadget<
FieldT,
HashT,
HashTreeT,
ZETH_NUM_JS_INPUTS,
ZETH_NUM_JS_OUTPUTS>
js(pb);
js.generate_r1cs_constraints();
}

int main(int argc, char **argv) { mpc_main(argc, argv, zeth_protoboard); }
32 changes: 7 additions & 25 deletions src/mpc/mpc_common.cpp
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
#include "mpc_common.hpp"

#include "circuit-wrapper.hpp"
#include "test/simple_test.hpp"

#include <iostream>

namespace po = boost::program_options;

using HashTreeT = MiMC_mp_gadget<FieldT>;
using HashT = sha256_ethereum<FieldT>;

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

void subcommand::set_global_options(bool verbose, bool simple_circuit)
void subcommand::set_global_options(bool verbose, ProtoboardInitFn pb_init)
{
this->verbose = verbose;
this->simple_circuit = simple_circuit;
this->protoboard_init = pb_init;
}

int subcommand::execute(const std::vector<std::string> &args)
Expand Down Expand Up @@ -62,25 +56,13 @@ int subcommand::execute(const std::vector<std::string> &args)
}
}

void subcommand::usage(const po::options_description &options)
void subcommand::init_protoboard(libsnark::protoboard<FieldT> &pb) const
{
subcommand_usage();
std::cout << options << std::endl;
protoboard_init(pb);
}

void populate_protoboard(libsnark::protoboard<FieldT> &pb, bool simple_circuit)
void subcommand::usage(const po::options_description &options)
{
if (simple_circuit) {
libzeth::test::simple_circuit<FieldT>(pb);
return;
}

joinsplit_gadget<
FieldT,
HashT,
HashTreeT,
ZETH_NUM_JS_INPUTS,
ZETH_NUM_JS_OUTPUTS>
js(pb);
js.generate_r1cs_constraints();
subcommand_usage();
std::cout << options << std::endl;
}
15 changes: 6 additions & 9 deletions src/mpc/mpc_common.hpp
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
#ifndef __ZETH_MPC_MPC_COMMON_HPP__
#define __ZETH_MPC_MPC_COMMON_HPP__

#include "include_libsnark.hpp"
#include "mpc_main.hpp"

#include <boost/program_options.hpp>
#include <fstream>
#include <string>
#include <vector>

using ppT = libff::default_ec_pp;
using FieldT = libff::Fr<ppT>;

class subcommand
{
protected:
std::string subcommand_name;
bool verbose;
bool simple_circuit;
ProtoboardInitFn protoboard_init;

private:
bool help;

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

protected:
void init_protoboard(libsnark::protoboard<FieldT> &pb) const;

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

Expand All @@ -49,7 +49,4 @@ template<typename T> inline T read_from_file(const std::string &file_name)
return T::read(in);
}

void populate_protoboard(
libsnark::protoboard<libff::Fr<ppT>> &pb, bool simple_circuit);

#endif // __ZETH_MPC_MPC_COMMON_HPP__
2 changes: 1 addition & 1 deletion src/mpc/mpc_create_keypair.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class mpc_create_keypair : public subcommand
// Compute circuit
libff::enter_block("Generate QAP");
libsnark::protoboard<FieldT> pb;
populate_protoboard(pb, simple_circuit);
init_protoboard(pb);
libsnark::r1cs_constraint_system<FieldT> cs =
pb.get_constraint_system();
const libsnark::qap_instance<FieldT> qap =
Expand Down
2 changes: 1 addition & 1 deletion src/mpc/mpc_dummy_phase2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class mpc_dummy_phase2 : public subcommand
// Generate the zeth circuit (to determine the number of inputs)
const size_t num_inputs = [this]() {
libsnark::protoboard<FieldT> pb;
populate_protoboard(pb, simple_circuit);
init_protoboard(pb);
const libsnark::r1cs_constraint_system<FieldT> cs =
pb.get_constraint_system();
return cs.num_inputs();
Expand Down
2 changes: 1 addition & 1 deletion src/mpc/mpc_linear_combination.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class mpc_linear_combination : public subcommand
// Compute circuit
libff::enter_block("Generate QAP");
libsnark::protoboard<FieldT> pb;
populate_protoboard(pb, simple_circuit);
init_protoboard(pb);
const libsnark::r1cs_constraint_system<FieldT> cs =
pb.get_constraint_system();
const libsnark::qap_instance<FieldT> qap =
Expand Down
90 changes: 90 additions & 0 deletions src/mpc/mpc_main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#include "mpc_common.hpp"

#include <boost/program_options.hpp>

namespace po = boost::program_options;

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

int mpc_main(int argc, char **argv, ProtoboardInitFn pb_init)
{
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");
}

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

return 1;
}
Loading

0 comments on commit b01d4db

Please sign in to comment.