Skip to content

Commit

Permalink
mpc: templatize implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
dtebbs committed Aug 29, 2019
1 parent 65def7d commit 9718bb9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 25 deletions.
12 changes: 7 additions & 5 deletions src/snarks/groth16/mpc_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@
namespace libzeth
{

using ppT = libff::default_ec_pp;

/// Output from linear combination $L_1$ - the linear combination of
/// elements in powersoftau, based on a specific circuit.
class srs_mpc_layer_L1
template<typename ppT> class srs_mpc_layer_L1
{
public:
/// { [ t(x) . x^i ]_1 } i = 0 .. n-1
Expand Down Expand Up @@ -57,20 +55,24 @@ class srs_mpc_layer_L1
/// Given a circuit and a powersoftau with pre-computed lagrange
/// polynomials, perform the correct linear combination for the CRS
/// MPC.
srs_mpc_layer_L1 mpc_compute_linearcombination(
template<typename ppT>
srs_mpc_layer_L1<ppT> mpc_compute_linearcombination(
const powersoftau &pot, const libsnark::qap_instance<libff::Fr<ppT>> &qap);

/// Given the output from the first layer of the MPC, perform the 2nd
/// layer computation using just local randomness. This is not a
/// substitute for the full MPC with an auditable log of
/// contributions, but is useful for testing.
template<typename ppT>
libsnark::r1cs_gg_ppzksnark_keypair<ppT> mpc_dummy_layer2(
powersoftau &&pot,
srs_mpc_layer_L1 &&layer1,
srs_mpc_layer_L1<ppT> &&layer1,
const libff::Fr<ppT> &delta,
libsnark::r1cs_constraint_system<libff::Fr<ppT>> &&cs,
const libsnark::qap_instance<libff::Fr<ppT>> &qap);

} // namespace libzeth

#include "snarks/groth16/mpc_utils.tcc"

#endif // __ZETH_SNARKS_GROTH16_MPC_UTILS_HPP__
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
#include "mpc_utils.hpp"
#ifndef __ZETH_SNARKS_GROTH16_MPC_UTILS_TCC__
#define __ZETH_SNARKS_GROTH16_MPC_UTILS_TCC__

#include "evaluator_from_lagrange.hpp"
#include "mpc_utils.hpp"
#include "multi_exp.hpp"

#include <libff/algebra/scalar_multiplication/multiexp.hpp>

namespace libzeth
{

using ppT = libff::default_ec_pp;
using Fr = libff::Fr<ppT>;
using G1 = libff::G1<ppT>;
using G2 = libff::G2<ppT>;

// -----------------------------------------------------------------------------
// srs_mpc_layer_L1
// -----------------------------------------------------------------------------

srs_mpc_layer_L1::srs_mpc_layer_L1(
template<typename ppT>
srs_mpc_layer_L1<ppT>::srs_mpc_layer_L1(
libff::G1_vector<ppT> &&T_tau_powers_g1,
libff::G1_vector<ppT> &&A_g1,
libff::G1_vector<ppT> &&B_g1,
Expand All @@ -31,9 +25,14 @@ srs_mpc_layer_L1::srs_mpc_layer_L1(
{
}

srs_mpc_layer_L1 mpc_compute_linearcombination(
const powersoftau &pot, const libsnark::qap_instance<Fr> &qap)
template<typename ppT>
srs_mpc_layer_L1<ppT> mpc_compute_linearcombination(
const powersoftau &pot, const libsnark::qap_instance<libff::Fr<ppT>> &qap)
{
using Fr = libff::Fr<ppT>;
using G1 = libff::G1<ppT>;
using G2 = libff::G2<ppT>;

libfqfft::evaluation_domain<Fr> &domain = *qap.domain;

// n = number of constraints in qap / degree of t().
Expand Down Expand Up @@ -116,21 +115,26 @@ srs_mpc_layer_L1 mpc_compute_linearcombination(
// by this circuit and using sparse vectors where it makes sense
// (as is done for B_i's in r1cs_gg_ppzksnark_proving_key).

return srs_mpc_layer_L1(
return srs_mpc_layer_L1<ppT>(
std::move(t_x_pow_i),
std::move(A_i_g1),
std::move(B_i_g1),
std::move(B_i_g2),
std::move(ABC_i_g1));
}

template<typename ppT>
libsnark::r1cs_gg_ppzksnark_keypair<ppT> mpc_dummy_layer2(
powersoftau &&pot,
srs_mpc_layer_L1 &&layer1,
const Fr &delta,
libsnark::r1cs_constraint_system<Fr> &&cs,
const libsnark::qap_instance<Fr> &qap)
srs_mpc_layer_L1<ppT> &&layer1,
const libff::Fr<ppT> &delta,
libsnark::r1cs_constraint_system<libff::Fr<ppT>> &&cs,
const libsnark::qap_instance<libff::Fr<ppT>> &qap)
{
using Fr = libff::Fr<ppT>;
using G1 = libff::G1<ppT>;
using G2 = libff::G2<ppT>;

const Fr delta_inverse = delta.inverse();

// { H_i } = { [ t(x) . x^i / delta ]_i } i = 0 .. m-1
Expand Down Expand Up @@ -200,3 +204,5 @@ libsnark::r1cs_gg_ppzksnark_keypair<ppT> mpc_dummy_layer2(
}

} // namespace libzeth

#endif // __ZETH_SNARKS_GROTH16_MPC_UTILS_TCC__
5 changes: 3 additions & 2 deletions src/test/mpc_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ TEST(MPCTests, LinearCombination)
dummy_powersoftau_from_secrets(tau, alpha, beta, qap.degree());

// linear combination
const srs_mpc_layer_L1 layer1 = mpc_compute_linearcombination(pot, qap);
const srs_mpc_layer_L1<ppT> layer1 =
mpc_compute_linearcombination<ppT>(pot, qap);

// Without knowlege of tau, not many checks can be performed
// beyond the ratio of terms in [ t(x) . x^i ]_1.
Expand Down Expand Up @@ -113,7 +114,7 @@ TEST(MPCTests, Layer2)
size_t num_variables = qap.num_variables();
size_t num_inputs = qap.num_inputs();

srs_mpc_layer_L1 layer1 = mpc_compute_linearcombination(pot, qap);
srs_mpc_layer_L1<ppT> layer1 = mpc_compute_linearcombination<ppT>(pot, qap);

// Final key pair
const r1cs_gg_ppzksnark_keypair<ppT> keypair = mpc_dummy_layer2(
Expand Down

0 comments on commit 9718bb9

Please sign in to comment.