Skip to content

Commit

Permalink
Clean compile with -DNDEBUG and -Werror=unused-but-set-variable.
Browse files Browse the repository at this point in the history
All our tests use C-style assert() to signal failure. We eventually
want to rework this in proper C++ exceptions (or, better, GTest as in
gadgetlib2). In the meantime, however, we squelch compiler warnings
about unused (i.e. used only in assert, which evaluate to nil in
DNDEBUG) but set variables by either wrapping debug assertions in
ifdef's or compiling a generic warning message instead.
  • Loading branch information
madars committed Mar 29, 2020
1 parent 8b5efc9 commit a1cff25
Show file tree
Hide file tree
Showing 16 changed files with 98 additions and 4 deletions.
2 changes: 2 additions & 0 deletions libsnark/common/data_structures/merkle_tree.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ typename HashT::hash_value_type two_to_one_CRH(const typename HashT::hash_value_
new_input.insert(new_input.end(), l.begin(), l.end());
new_input.insert(new_input.end(), r.begin(), r.end());

#ifdef DEBUG
const size_t digest_size = HashT::get_digest_len();
assert(l.size() == digest_size);
assert(r.size() == digest_size);
#endif

return HashT::get_hash(new_input);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -443,8 +443,10 @@ void as_waksman_route_inner(const size_t left,
const bool lhs_switch_setting = as_waksman_get_switch_setting_from_top_bottom_decision(lo, permutation_inv.get(to_route), use_top);

/* The value on the left-hand side is either the same or not set. */
#ifdef DEBUG
auto it = routing[left].find(lhs_switch);
assert(it == routing[left].end() || it->second == lhs_switch_setting);
#endif
routing[left][lhs_switch] = lhs_switch_setting;

const size_t t = as_waksman_switch_input(subnetwork_size, lo, rhs_switch, use_top);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ void knapsack_CRH_with_bit_out_gadget<FieldT>::sample_randomness(const size_t in
template<typename FieldT>
void test_knapsack_CRH_with_bit_out_gadget_internal(const size_t dimension, const libff::bit_vector &input_bits, const libff::bit_vector &digest_bits)
{
#ifndef NDEBUG
assert(knapsack_dimension<FieldT>::dimension == dimension);
knapsack_CRH_with_bit_out_gadget<FieldT>::sample_randomness(input_bits.size());
protoboard<FieldT> pb;
Expand All @@ -252,6 +253,12 @@ void test_knapsack_CRH_with_bit_out_gadget_internal(const size_t dimension, cons
const size_t num_constraints = pb.num_constraints();
const size_t expected_constraints = knapsack_CRH_with_bit_out_gadget<FieldT>::expected_constraints();
assert(num_constraints == expected_constraints);
#else // NDEBUG
printf("All tests here depend on assert() which is disabled by -DNDEBUG. Please recompile and run again.\n");
libff::UNUSED(dimension);
libff::UNUSED(input_bits);
libff::UNUSED(digest_bits);
#endif // NDEBUG
}

} // libsnark
Expand Down
4 changes: 4 additions & 0 deletions libsnark/gadgetlib1/gadgets/hashes/sha256/sha256_gadget.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,12 @@ sha256_two_to_one_hash_gadget<FieldT>::sha256_two_to_one_hash_gadget(protoboard<
const std::string &annotation_prefix) :
gadget<FieldT>(pb, annotation_prefix)
{
#ifndef NDEBUG
assert(block_length == SHA256_block_size);
assert(input_block.bits.size() == block_length);
#else
libff::UNUSED(block_length);
#endif
f.reset(new sha256_compression_function_gadget<FieldT>(pb, SHA256_default_IV<FieldT>(pb), input_block.bits, output, FMT(this->annotation_prefix, " f")));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ size_t merkle_tree_check_read_gadget<FieldT, HashT>::expected_constraints(const
template<typename FieldT, typename HashT>
void test_merkle_tree_check_read_gadget()
{
#ifndef NDEBUG
/* prepare test */
const size_t digest_len = HashT::get_digest_len();
const size_t tree_depth = 16;
Expand Down Expand Up @@ -189,6 +190,9 @@ void test_merkle_tree_check_read_gadget()
const size_t num_constraints = pb.num_constraints();
const size_t expected_constraints = merkle_tree_check_read_gadget<FieldT, HashT>::expected_constraints(tree_depth);
assert(num_constraints == expected_constraints);
#else // NDEBUG
printf("All tests here depend on assert() which is disabled by -DNDEBUG. Please recompile and run again.\n");
#endif // NDEBUG
}

} // libsnark
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ size_t merkle_tree_check_update_gadget<FieldT, HashT>::expected_constraints(cons
template<typename FieldT, typename HashT>
void test_merkle_tree_check_update_gadget()
{
#ifndef NDEBUG
/* prepare test */
const size_t digest_len = HashT::get_digest_len();

Expand Down Expand Up @@ -258,6 +259,9 @@ void test_merkle_tree_check_update_gadget()
const size_t num_constraints = pb.num_constraints();
const size_t expected_constraints = merkle_tree_check_update_gadget<FieldT, HashT>::expected_constraints(tree_depth);
assert(num_constraints == expected_constraints);
#else // NDEBUG
printf("All tests here depend on assert() which is disabled by -DNDEBUG. Please recompile and run again.\n");
#endif // NDEBUG
}

} // libsnark
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

using namespace libsnark;

#ifndef NDEBUG

template<typename FieldT>
void dump_constraints(const protoboard<FieldT> &pb)
{
Expand Down Expand Up @@ -374,7 +376,7 @@ void test_full_precomputed_pairing(const std::string &annotation)
printf("number of constraints for full precomputed pairing (Fr is %s) = %zu\n", annotation.c_str(), pb.num_constraints());
}

int main(void)
int main()
{
libff::start_profiling();
libff::mnt4_pp::init_public_params();
Expand Down Expand Up @@ -428,3 +430,11 @@ int main(void)
test_hardcoded_verifier<libff::mnt4_pp, libff::mnt6_pp>("mnt4", "mnt6");
test_hardcoded_verifier<libff::mnt6_pp, libff::mnt4_pp>("mnt6", "mnt4");
}

#else // NDEBUG

int main()
{
printf("All tests here depend on assert() which is disabled by -DNDEBUG. Please recompile and run again.\n");
}
#endif // NDEBUG
11 changes: 9 additions & 2 deletions libsnark/knowledge_commitment/kc_multiexp.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ knowledge_commitment<T1, T2> kc_multi_exp_with_mixed_addition(const knowledge_co
typename std::vector<FieldT>::const_iterator scalar_end,
const size_t chunks)
{
const size_t scalar_length = std::distance(scalar_start, scalar_end);
#ifndef NDEBUG
assert((size_t)(scalar_length) <= vec.domain_size_);
#else
libff::UNUSED(scalar_length);
#endif

libff::enter_block("Process scalar vector");
auto index_it = std::lower_bound(vec.indices.begin(), vec.indices.end(), min_idx);
const size_t offset = index_it - vec.indices.begin();
Expand All @@ -44,12 +51,12 @@ knowledge_commitment<T1, T2> kc_multi_exp_with_mixed_addition(const knowledge_co
size_t num_add = 0;
size_t num_other = 0;

const size_t scalar_length = std::distance(scalar_start, scalar_end);

while (index_it != vec.indices.end() && *index_it < max_idx)
{
const size_t scalar_position = (*index_it) - min_idx;
#ifdef DEBUG
assert(scalar_position < scalar_length);
#endif

const FieldT scalar = *(scalar_start + scalar_position);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

using namespace libsnark;

#ifndef NDEBUG
template<typename ppT>
void test_bacs_ppzksnark(const size_t primary_input_size,
const size_t auxiliary_input_size,
Expand All @@ -45,3 +46,9 @@ int main()

test_bacs_ppzksnark<default_bacs_ppzksnark_pp>(10, 10, 20, 5);
}
#else // NDEBUG
int main()
{
printf("All tests here depend on assert() which is disabled by -DNDEBUG. Please recompile and run again.\n");
}
#endif // NDEBUG
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

using namespace libsnark;

#ifndef NDEBUG
template<typename ppT>
void test_r1cs_gg_ppzksnark(size_t num_constraints,
size_t input_size)
Expand All @@ -41,3 +42,9 @@ int main()

test_r1cs_gg_ppzksnark<default_r1cs_gg_ppzksnark_pp>(1000, 100);
}
#else // NDEBUG
int main()
{
printf("All tests here depend on assert() which is disabled by -DNDEBUG. Please recompile and run again.\n");
}
#endif // NDEBUG
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ bool run_r1cs_ppzksnark(const r1cs_example<libff::Fr<ppT> > &example,

libff::leave_block("Call to run_r1cs_ppzksnark");

return ans;
return (ans && ans2);
}

} // libsnark
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

using namespace libsnark;

#ifndef NDEBUG
template<typename ppT>
void test_r1cs_ppzksnark(size_t num_constraints,
size_t input_size)
Expand All @@ -41,3 +42,9 @@ int main()

test_r1cs_ppzksnark<default_r1cs_ppzksnark_pp>(1000, 100);
}
#else // NDEBUG
int main()
{
printf("All tests here depend on assert() which is disabled by -DNDEBUG. Please recompile and run again.\n");
}
#endif // NDEBUG
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

using namespace libsnark;

#ifndef NDEBUG

template<typename ppT>
void test_r1cs_se_ppzksnark(size_t num_constraints,
size_t input_size)
Expand All @@ -41,3 +43,10 @@ int main()

test_r1cs_se_ppzksnark<default_r1cs_se_ppzksnark_pp>(1000, 100);
}

#else // NDEBUG
int main()
{
printf("All tests here depend on assert() which is disabled by -DNDEBUG. Please recompile and run again.\n");
}
#endif // NDEBUG
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

using namespace libsnark;

#ifndef NDEBUG

template<typename ppT>
void test_ram_ppzksnark(const size_t w,
const size_t k,
Expand Down Expand Up @@ -56,3 +58,11 @@ int main()
// 32-bit TinyRAM with 16 registers
test_ram_ppzksnark<default_ram_ppzksnark_pp>(32, 16, program_size, input_size, time_bound);
}

#else // NDEBUG

int main()
{
printf("All tests here depend on assert() which is disabled by -DNDEBUG. Please recompile and run again.\n");
}
#endif // NDEBUG
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

using namespace libsnark;

#ifndef NDEBUG
template<typename ppT>
void test_tbcs_ppzksnark(const size_t primary_input_size,
const size_t auxiliary_input_size,
Expand All @@ -45,3 +46,9 @@ int main()

test_tbcs_ppzksnark<default_tbcs_ppzksnark_pp>(10, 10, 20, 5);
}
#else // NDEBUG
int main()
{
printf("All tests here depend on assert() which is disabled by -DNDEBUG. Please recompile and run again.\n");
}
#endif // NDEBUG
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

using namespace libsnark;

#ifndef NDEBUG
template<typename ppT>
void test_uscs_ppzksnark(size_t num_constraints,
size_t input_size)
Expand All @@ -41,3 +42,9 @@ int main()

test_uscs_ppzksnark<default_uscs_ppzksnark_pp>(1000, 100);
}
#else // NDEBUG
int main()
{
printf("All tests here depend on assert() which is disabled by -DNDEBUG. Please recompile and run again.\n");
}
#endif // NDEBUG

0 comments on commit a1cff25

Please sign in to comment.