Skip to content

Commit

Permalink
libzecale: use null hash in most cases (avoid huge testing times unti…
Browse files Browse the repository at this point in the history
…l we have an optimized hash)
  • Loading branch information
dtebbs committed Sep 2, 2020
1 parent 2c1c259 commit 66d4bdf
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 37 deletions.
4 changes: 2 additions & 2 deletions aggregator_server/aggregator_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// the corresponding pairing parameters type.

#include "libzecale/circuits/aggregator_circuit_wrapper.hpp"
#include "libzecale/circuits/null_hash_gadget.hpp"
#include "libzecale/core/application_pool.hpp"
#include "libzecale/serialization/proto_utils.hpp"
#include "zecale_config.h"
Expand All @@ -19,7 +20,6 @@
#include <grpcpp/server_context.h>
#include <iostream>
#include <libsnark/common/data_structures/merkle_tree.hpp>
#include <libzeth/circuits/circuit_types.hpp>
#include <libzeth/core/utils.hpp>
#include <libzeth/serialization/proto_utils.hpp>
#include <libzeth/serialization/r1cs_serialization.hpp>
Expand Down Expand Up @@ -67,7 +67,7 @@ using napi_handler = libzeth::groth16_api_handler<npp>;
#endif

using nsnark = typename nverifier::snark;
using hash = libzeth::BLAKE2s_256<libff::Fr<wpp>>;
using hash = libzecale::null_hash_gadget<libff::Fr<wpp>>;

static const size_t batch_size = 2;
static const size_t num_inputs_per_nested_proof = 1;
Expand Down
4 changes: 2 additions & 2 deletions libzecale/circuits/aggregator_circuit_wrapper.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ aggregator_circuit_wrapper<wppT, wsnarkT, nverifierT, hashT, NumProofs>::
for (size_t i = 0; i < NumProofs; ++i) {
_nested_proofs[i]->generate_r1cs_constraints();
}
// _nested_vk_hash_gadget->generate_r1cs_constraints();
_nested_vk_hash_gadget->generate_r1cs_constraints();
_aggregator_gadget->generate_r1cs_constraints();
}

Expand Down Expand Up @@ -157,7 +157,7 @@ libzeth::extended_proof<wppT, wsnarkT> aggregator_circuit_wrapper<
_nested_vk->generate_r1cs_witness(nested_vk);

// Witness hash of verification keypair
// _nested_vk_hash_gadget->generate_r1cs_witness();
_nested_vk_hash_gadget->generate_r1cs_witness();

// Pass the input values (in npp) to the aggregator gadget.
_aggregator_gadget->generate_r1cs_witness(nested_inputs);
Expand Down
32 changes: 32 additions & 0 deletions libzecale/circuits/null_hash_gadget.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) 2015-2020 Clearmatics Technologies Ltd
//
// SPDX-License-Identifier: LGPL-3.0+

#ifndef __ZECALE_CIRCUIT_NULL_HASH_GADGET_HPP__
#define __ZECALE_CIRCUIT_NULL_HASH_GADGET_HPP__

namespace libzecale
{

/// A trivial hash gadget as a way to disable verification key hashing in
/// the aggregator_circuit_wrapper.
template<typename FieldT> class null_hash_gadget
{
public:
null_hash_gadget(
libsnark::protoboard<FieldT> &pb,
const libsnark::block_variable<FieldT> &input,
const libsnark::digest_variable<FieldT> &output,
const std::string &annotation_prefix);

void generate_r1cs_constraints(const bool ensure_output_bitness = true);
void generate_r1cs_witness();

static size_t get_digest_len();
};

} // namespace libzecale

#include "libzecale/circuits/null_hash_gadget.tcc"

#endif // __ZECALE_CIRCUIT_NULL_HASH_GADGET_HPP__
37 changes: 37 additions & 0 deletions libzecale/circuits/null_hash_gadget.tcc
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) 2015-2020 Clearmatics Technologies Ltd
//
// SPDX-License-Identifier: LGPL-3.0+

#ifndef __ZECALE_CIRCUIT_NULL_GADGET_HPP__
#define __ZECALE_CIRCUIT_NULL_GADGET_HPP__

namespace libzecale
{

template<typename FieldT>
null_hash_gadget<FieldT>::null_hash_gadget(
libsnark::protoboard<FieldT> & /* pb */,
const libsnark::block_variable<FieldT> & /* input */,
const libsnark::digest_variable<FieldT> & /* output */,
const std::string & /* annotation_prefix */)
{
}

template<typename FieldT>
void null_hash_gadget<FieldT>::generate_r1cs_constraints(
const bool /* ensure_output_bitness */)
{
}

template<typename FieldT> void null_hash_gadget<FieldT>::generate_r1cs_witness()
{
}

template<typename FieldT> size_t null_hash_gadget<FieldT>::get_digest_len()
{
return 0;
}

} // namespace libzecale

#endif // __ZECALE_CIRCUIT_NULL_GADGET_HPP__
55 changes: 24 additions & 31 deletions libzecale/tests/aggregator/aggregator_dummy_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "libzecale/circuits/aggregator_circuit_wrapper.hpp"
#include "libzecale/circuits/groth16_verifier/groth16_verifier_parameters.hpp"
#include "libzecale/circuits/null_hash_gadget.hpp"
#include "libzecale/circuits/pairing/bw6_761_pairing_params.hpp"
#include "libzecale/circuits/pairing/mnt_pairing_params.hpp"
#include "libzecale/circuits/pghr13_verifier/pghr13_verifier_parameters.hpp"
Expand All @@ -14,7 +15,9 @@

using namespace libzecale;

template<typename wppT> using hash = libzeth::BLAKE2s_256<libff::Fr<wppT>>;
template<typename wppT> using full_hash = libzeth::BLAKE2s_256<libff::Fr<wppT>>;
template<typename wppT>
using null_hash = libzecale::null_hash_gadget<libff::Fr<wppT>>;

namespace
{
Expand Down Expand Up @@ -46,6 +49,7 @@ template<
typename wppT,
typename wsnarkT,
typename nverifierT,
typename hashT,
size_t batch_size>
void test_aggregator_with_batch(
const size_t num_inputs_per_nested_proof,
Expand All @@ -55,12 +59,8 @@ void test_aggregator_with_batch(
typename nverifierT::snark,
batch_size> &batch,
const typename wsnarkT::keypair &wkeypair,
aggregator_circuit_wrapper<
wppT,
wsnarkT,
nverifierT,
hash<wppT>,
batch_size> &aggregator,
aggregator_circuit_wrapper<wppT, wsnarkT, nverifierT, hashT, batch_size>
&aggregator,
const std::array<libff::Fr<wppT>, batch_size> &expected_results)
{
using npp = libzecale::other_curve<wppT>;
Expand All @@ -79,10 +79,10 @@ void test_aggregator_with_batch(
size_t winput_idx = 0;

// Check the nested vk hash
// libff::Fr<wppT> expect_nested_vk_hash =
// verification_key_hash_gadget<wppT, nverifierT, hash<wppT>>::
// compute_hash(nkp.vk, num_inputs_per_nested_proof);
// ASSERT_EQ(expect_nested_vk_hash, winputs[winput_idx++]);
libff::Fr<wppT> expect_nested_vk_hash =
verification_key_hash_gadget<wppT, nverifierT, hashT>::compute_hash(
nkp.vk, num_inputs_per_nested_proof);
ASSERT_EQ(expect_nested_vk_hash, winputs[winput_idx]);
++winput_idx;

for (size_t proof_idx = 0; proof_idx < batch_size; ++proof_idx) {
Expand All @@ -100,7 +100,7 @@ void test_aggregator_with_batch(
}
}

template<typename wppT, typename wsnarkT, typename nverifierT>
template<typename wppT, typename wsnarkT, typename nverifierT, typename hashT>
void test_aggregate_dummy_application()
{
using npp = other_curve<wppT>;
Expand All @@ -126,12 +126,7 @@ void test_aggregate_dummy_application()
npf2.write_json(std::cout);

// Wrapper keypair
aggregator_circuit_wrapper<
wppT,
wsnarkT,
nverifierT,
hash<wppT>,
batch_size>
aggregator_circuit_wrapper<wppT, wsnarkT, nverifierT, hashT, batch_size>
aggregator(public_inputs_per_proof);
const typename wsnarkT::keypair wkeypair =
aggregator.generate_trusted_setup();
Expand All @@ -146,7 +141,7 @@ void test_aggregate_dummy_application()
{libff::Fr<wppT>::one(), libff::Fr<wppT>::one()});
}

template<typename wppT, typename wsnarkT, typename nverifierT>
template<typename wppT, typename wsnarkT, typename nverifierT, typename hashT>
void test_aggregate_dummy_application_with_invalid_proof()
{
using npp = other_curve<wppT>;
Expand Down Expand Up @@ -180,12 +175,7 @@ void test_aggregate_dummy_application_with_invalid_proof()
npf2_invalid.write_json(std::cout);

// Wrapper keypair
aggregator_circuit_wrapper<
wppT,
wsnarkT,
nverifierT,
hash<wppT>,
batch_size>
aggregator_circuit_wrapper<wppT, wsnarkT, nverifierT, hashT, batch_size>
aggregator(public_inputs_per_proof);
const typename wsnarkT::keypair wkeypair =
aggregator.generate_trusted_setup();
Expand All @@ -205,35 +195,38 @@ TEST(AggregatorTest, AggregateDummyApplicationMnt4Groth16Mnt6Groth16)
using wpp = libff::mnt6_pp;
using wsnark = libzeth::groth16_snark<wpp>;
using nverifier = groth16_verifier_parameters<wpp>;
test_aggregate_dummy_application<wpp, wsnark, nverifier>();
test_aggregate_dummy_application<wpp, wsnark, nverifier, null_hash<wpp>>();
test_aggregate_dummy_application_with_invalid_proof<
wpp,
wsnark,
nverifier>();
nverifier,
null_hash<wpp>>();
}

TEST(AggregatorTest, AggregateDummyApplicationBls12Groth16Bw6Groth16)
{
using wpp = libff::bw6_761_pp;
using wsnark = groth16_snark<wpp>;
using nverifier = groth16_verifier_parameters<wpp>;
test_aggregate_dummy_application<wpp, wsnark, nverifier>();
test_aggregate_dummy_application<wpp, wsnark, nverifier, null_hash<wpp>>();
test_aggregate_dummy_application_with_invalid_proof<
wpp,
wsnark,
nverifier>();
nverifier,
null_hash<wpp>>();
}

TEST(AggregatorTest, AggregateDummyApplicationBls12Groth16Bw6Pghr13)
{
using wpp = libff::bw6_761_pp;
using wsnark = libzeth::pghr13_snark<wpp>;
using nverifier = groth16_verifier_parameters<wpp>;
test_aggregate_dummy_application<wpp, wsnark, nverifier>();
test_aggregate_dummy_application<wpp, wsnark, nverifier, null_hash<wpp>>();
test_aggregate_dummy_application_with_invalid_proof<
wpp,
wsnark,
nverifier>();
nverifier,
null_hash<wpp>>();
}

} // namespace
Expand Down
7 changes: 5 additions & 2 deletions libzecale/tests/aggregator/aggregator_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "libzecale/circuits/aggregator_circuit_wrapper.hpp"
#include "libzecale/circuits/groth16_verifier/groth16_verifier_parameters.hpp"
#include "libzecale/circuits/null_hash_gadget.hpp"
#include "libzecale/circuits/pairing/bw6_761_pairing_params.hpp"
#include "libzecale/circuits/pairing/mnt_pairing_params.hpp"
#include "libzecale/circuits/pghr13_verifier/pghr13_verifier_parameters.hpp"
Expand Down Expand Up @@ -40,6 +41,8 @@ static const size_t batch_size = 2;
// [Root, NullifierS(2), CommitmentS(2), h_sig, h_iS(2), Residual Field,
// Element]
static const size_t num_zeth_inputs = 9;
template<typename wppT>
using nested_key_hash = libzecale::null_hash_gadget<libff::Fr<wppT>>;

using namespace libzecale;

Expand Down Expand Up @@ -200,7 +203,7 @@ bool test_valid_aggregation_batch_proofs(
wppT,
wsnarkT,
nverifierT,
hash<wppT>,
nested_key_hash<wppT>,
batch_size> &aggregator_prover,
typename wsnarkT::keypair &aggregator_keypair,
typename nverifierT::snark::keypair &zeth_keypair,
Expand Down Expand Up @@ -282,7 +285,7 @@ void aggregator_test()
wppT,
wsnarkT,
nverifierT,
hash<wppT>,
nested_key_hash<wppT>,
batch_size>
aggregator_prover(num_zeth_inputs);
std::cout << "[DEBUG] Before gen Aggregator setup" << std::endl;
Expand Down

0 comments on commit 66d4bdf

Please sign in to comment.