From 66d4bdf9b6a2a7e325366bc1eca337952ed53e03 Mon Sep 17 00:00:00 2001 From: Duncan Tebbs Date: Wed, 2 Sep 2020 14:50:19 +0100 Subject: [PATCH] libzecale: use null hash in most cases (avoid huge testing times until we have an optimized hash) --- aggregator_server/aggregator_server.cpp | 4 +- .../circuits/aggregator_circuit_wrapper.tcc | 4 +- libzecale/circuits/null_hash_gadget.hpp | 32 +++++++++++ libzecale/circuits/null_hash_gadget.tcc | 37 +++++++++++++ .../aggregator/aggregator_dummy_test.cpp | 55 ++++++++----------- .../tests/aggregator/aggregator_test.cpp | 7 ++- 6 files changed, 102 insertions(+), 37 deletions(-) create mode 100644 libzecale/circuits/null_hash_gadget.hpp create mode 100644 libzecale/circuits/null_hash_gadget.tcc diff --git a/aggregator_server/aggregator_server.cpp b/aggregator_server/aggregator_server.cpp index 9a38075e..a4e6f5c4 100644 --- a/aggregator_server/aggregator_server.cpp +++ b/aggregator_server/aggregator_server.cpp @@ -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" @@ -19,7 +20,6 @@ #include #include #include -#include #include #include #include @@ -67,7 +67,7 @@ using napi_handler = libzeth::groth16_api_handler; #endif using nsnark = typename nverifier::snark; -using hash = libzeth::BLAKE2s_256>; +using hash = libzecale::null_hash_gadget>; static const size_t batch_size = 2; static const size_t num_inputs_per_nested_proof = 1; diff --git a/libzecale/circuits/aggregator_circuit_wrapper.tcc b/libzecale/circuits/aggregator_circuit_wrapper.tcc index 4c120dd1..07d355ec 100644 --- a/libzecale/circuits/aggregator_circuit_wrapper.tcc +++ b/libzecale/circuits/aggregator_circuit_wrapper.tcc @@ -85,7 +85,7 @@ aggregator_circuit_wrapper:: 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(); } @@ -157,7 +157,7 @@ libzeth::extended_proof 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); diff --git a/libzecale/circuits/null_hash_gadget.hpp b/libzecale/circuits/null_hash_gadget.hpp new file mode 100644 index 00000000..25fdff64 --- /dev/null +++ b/libzecale/circuits/null_hash_gadget.hpp @@ -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 class null_hash_gadget +{ +public: + null_hash_gadget( + libsnark::protoboard &pb, + const libsnark::block_variable &input, + const libsnark::digest_variable &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__ diff --git a/libzecale/circuits/null_hash_gadget.tcc b/libzecale/circuits/null_hash_gadget.tcc new file mode 100644 index 00000000..fa002ebc --- /dev/null +++ b/libzecale/circuits/null_hash_gadget.tcc @@ -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 +null_hash_gadget::null_hash_gadget( + libsnark::protoboard & /* pb */, + const libsnark::block_variable & /* input */, + const libsnark::digest_variable & /* output */, + const std::string & /* annotation_prefix */) +{ +} + +template +void null_hash_gadget::generate_r1cs_constraints( + const bool /* ensure_output_bitness */) +{ +} + +template void null_hash_gadget::generate_r1cs_witness() +{ +} + +template size_t null_hash_gadget::get_digest_len() +{ + return 0; +} + +} // namespace libzecale + +#endif // __ZECALE_CIRCUIT_NULL_GADGET_HPP__ diff --git a/libzecale/tests/aggregator/aggregator_dummy_test.cpp b/libzecale/tests/aggregator/aggregator_dummy_test.cpp index 8d5cee1b..528436f9 100644 --- a/libzecale/tests/aggregator/aggregator_dummy_test.cpp +++ b/libzecale/tests/aggregator/aggregator_dummy_test.cpp @@ -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" @@ -14,7 +15,9 @@ using namespace libzecale; -template using hash = libzeth::BLAKE2s_256>; +template using full_hash = libzeth::BLAKE2s_256>; +template +using null_hash = libzecale::null_hash_gadget>; namespace { @@ -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, @@ -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, - batch_size> &aggregator, + aggregator_circuit_wrapper + &aggregator, const std::array, batch_size> &expected_results) { using npp = libzecale::other_curve; @@ -79,10 +79,10 @@ void test_aggregator_with_batch( size_t winput_idx = 0; // Check the nested vk hash - // libff::Fr expect_nested_vk_hash = - // verification_key_hash_gadget>:: - // compute_hash(nkp.vk, num_inputs_per_nested_proof); - // ASSERT_EQ(expect_nested_vk_hash, winputs[winput_idx++]); + libff::Fr expect_nested_vk_hash = + verification_key_hash_gadget::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) { @@ -100,7 +100,7 @@ void test_aggregator_with_batch( } } -template +template void test_aggregate_dummy_application() { using npp = other_curve; @@ -126,12 +126,7 @@ void test_aggregate_dummy_application() npf2.write_json(std::cout); // Wrapper keypair - aggregator_circuit_wrapper< - wppT, - wsnarkT, - nverifierT, - hash, - batch_size> + aggregator_circuit_wrapper aggregator(public_inputs_per_proof); const typename wsnarkT::keypair wkeypair = aggregator.generate_trusted_setup(); @@ -146,7 +141,7 @@ void test_aggregate_dummy_application() {libff::Fr::one(), libff::Fr::one()}); } -template +template void test_aggregate_dummy_application_with_invalid_proof() { using npp = other_curve; @@ -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, - batch_size> + aggregator_circuit_wrapper aggregator(public_inputs_per_proof); const typename wsnarkT::keypair wkeypair = aggregator.generate_trusted_setup(); @@ -205,11 +195,12 @@ TEST(AggregatorTest, AggregateDummyApplicationMnt4Groth16Mnt6Groth16) using wpp = libff::mnt6_pp; using wsnark = libzeth::groth16_snark; using nverifier = groth16_verifier_parameters; - test_aggregate_dummy_application(); + test_aggregate_dummy_application>(); test_aggregate_dummy_application_with_invalid_proof< wpp, wsnark, - nverifier>(); + nverifier, + null_hash>(); } TEST(AggregatorTest, AggregateDummyApplicationBls12Groth16Bw6Groth16) @@ -217,11 +208,12 @@ TEST(AggregatorTest, AggregateDummyApplicationBls12Groth16Bw6Groth16) using wpp = libff::bw6_761_pp; using wsnark = groth16_snark; using nverifier = groth16_verifier_parameters; - test_aggregate_dummy_application(); + test_aggregate_dummy_application>(); test_aggregate_dummy_application_with_invalid_proof< wpp, wsnark, - nverifier>(); + nverifier, + null_hash>(); } TEST(AggregatorTest, AggregateDummyApplicationBls12Groth16Bw6Pghr13) @@ -229,11 +221,12 @@ TEST(AggregatorTest, AggregateDummyApplicationBls12Groth16Bw6Pghr13) using wpp = libff::bw6_761_pp; using wsnark = libzeth::pghr13_snark; using nverifier = groth16_verifier_parameters; - test_aggregate_dummy_application(); + test_aggregate_dummy_application>(); test_aggregate_dummy_application_with_invalid_proof< wpp, wsnark, - nverifier>(); + nverifier, + null_hash>(); } } // namespace diff --git a/libzecale/tests/aggregator/aggregator_test.cpp b/libzecale/tests/aggregator/aggregator_test.cpp index 6e24ee77..54deaf7a 100644 --- a/libzecale/tests/aggregator/aggregator_test.cpp +++ b/libzecale/tests/aggregator/aggregator_test.cpp @@ -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" @@ -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 +using nested_key_hash = libzecale::null_hash_gadget>; using namespace libzecale; @@ -200,7 +203,7 @@ bool test_valid_aggregation_batch_proofs( wppT, wsnarkT, nverifierT, - hash, + nested_key_hash, batch_size> &aggregator_prover, typename wsnarkT::keypair &aggregator_keypair, typename nverifierT::snark::keypair &zeth_keypair, @@ -282,7 +285,7 @@ void aggregator_test() wppT, wsnarkT, nverifierT, - hash, + nested_key_hash, batch_size> aggregator_prover(num_zeth_inputs); std::cout << "[DEBUG] Before gen Aggregator setup" << std::endl;