From d98286d6ad2d4e27352ed58c30b30fcb7f988711 Mon Sep 17 00:00:00 2001 From: Suyash Bagad Date: Thu, 23 Mar 2023 18:53:47 +0000 Subject: [PATCH] Get rid of magic `15` --- .../pedersen_lookup.test.cpp | 45 ++++++++++++++----- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/cpp/src/barretenberg/crypto/pedersen_commitment/pedersen_lookup.test.cpp b/cpp/src/barretenberg/crypto/pedersen_commitment/pedersen_lookup.test.cpp index fc8b47eef6..a2aa31ef1d 100644 --- a/cpp/src/barretenberg/crypto/pedersen_commitment/pedersen_lookup.test.cpp +++ b/cpp/src/barretenberg/crypto/pedersen_commitment/pedersen_lookup.test.cpp @@ -16,7 +16,7 @@ auto compute_expected(const grumpkin::fq exponent, size_t generator_offset) const auto lambda = grumpkin::fr::cube_root_of_unity(); const auto mask = crypto::pedersen_hash::lookup::PEDERSEN_TABLE_SIZE - 1; - for (size_t i = 0; i < 15; ++i) { + for (size_t i = 0; i < (crypto::pedersen_hash::lookup::NUM_PEDERSEN_TABLES / 2); ++i) { const auto slice_a = static_cast(bits.data[0] & mask) + 1; bits >>= crypto::pedersen_hash::lookup::BITS_PER_TABLE; const auto slice_b = static_cast(bits.data[0] & mask) + 1; @@ -81,7 +81,7 @@ TEST(pedersen_lookup, hash_single) std::array accumulators; - for (size_t i = 0; i < 15; ++i) { + for (size_t i = 0; i < (crypto::pedersen_hash::lookup::NUM_PEDERSEN_TABLES / 2); ++i) { const auto slice_a = static_cast(bits.data[0] & mask) + 1; bits >>= crypto::pedersen_hash::lookup::BITS_PER_TABLE; const auto slice_b = static_cast(bits.data[0] & mask) + 1; @@ -115,7 +115,8 @@ TEST(pedersen_lookup, hash_pair) const fq result(crypto::pedersen_hash::lookup::hash_pair(left, right)); - const affine_element expected(compute_expected(left, 0) + compute_expected(right, 15)); + const affine_element expected(compute_expected(left, 0) + + compute_expected(right, (crypto::pedersen_hash::lookup::NUM_PEDERSEN_TABLES / 2))); EXPECT_EQ(result, expected.x); } @@ -136,11 +137,16 @@ TEST(pedersen_lookup, merkle_damgard_compress) fq intermediate = (grumpkin::g1::affine_one * fr(iv + 1)).x; for (size_t i = 0; i < m; i++) { - intermediate = affine_element(compute_expected(intermediate, 0) + compute_expected(inputs[i], 15)).x; + intermediate = + affine_element(compute_expected(intermediate, 0) + + compute_expected(inputs[i], (crypto::pedersen_hash::lookup::NUM_PEDERSEN_TABLES / 2))) + .x; } EXPECT_EQ(affine_element(result).x, - affine_element(compute_expected(intermediate, 0) + compute_expected(fq(m), 15)).x); + affine_element(compute_expected(intermediate, 0) + + compute_expected(fq(m), (crypto::pedersen_hash::lookup::NUM_PEDERSEN_TABLES / 2))) + .x); } TEST(pedersen_lookup, merkle_damgard_compress_multiple_iv) @@ -164,14 +170,22 @@ TEST(pedersen_lookup, merkle_damgard_compress_multiple_iv) for (size_t i = 0; i < 2 * m; i++) { if ((i & 1) == 0) { const auto iv = (grumpkin::g1::affine_one * fr(ivs[i >> 1] + 1)).x; - intermediate = affine_element(compute_expected(intermediate, 0) + compute_expected(iv, 15)).x; + intermediate = + affine_element(compute_expected(intermediate, 0) + + compute_expected(iv, (crypto::pedersen_hash::lookup::NUM_PEDERSEN_TABLES / 2))) + .x; } else { - intermediate = affine_element(compute_expected(intermediate, 0) + compute_expected(inputs[i >> 1], 15)).x; + intermediate = affine_element(compute_expected(intermediate, 0) + + compute_expected(inputs[i >> 1], + (crypto::pedersen_hash::lookup::NUM_PEDERSEN_TABLES / 2))) + .x; } } EXPECT_EQ(affine_element(result).x, - affine_element(compute_expected(intermediate, 0) + compute_expected(fq(m), 15)).x); + affine_element(compute_expected(intermediate, 0) + + compute_expected(fq(m), (crypto::pedersen_hash::lookup::NUM_PEDERSEN_TABLES / 2))) + .x); } TEST(pedersen_lookup, merkle_damgard_tree_compress) @@ -193,16 +207,25 @@ TEST(pedersen_lookup, merkle_damgard_tree_compress) std::vector temp; for (size_t i = 0; i < m; i++) { const fq iv_term = (grumpkin::g1::affine_one * fr(ivs[i] + 1)).x; - temp.push_back(affine_element(compute_expected(iv_term, 0) + compute_expected(inputs[i], 15)).x); + temp.push_back( + affine_element(compute_expected(iv_term, 0) + + compute_expected(inputs[i], (crypto::pedersen_hash::lookup::NUM_PEDERSEN_TABLES / 2))) + .x); } const size_t logm = numeric::get_msb(m); for (size_t j = 1; j <= logm; j++) { const size_t nodes = (1UL << (logm - j)); for (size_t i = 0; i < nodes; i++) { - temp[i] = affine_element(compute_expected(temp[2 * i], 0) + compute_expected(temp[2 * i + 1], 15)).x; + temp[i] = affine_element( + compute_expected(temp[2 * i], 0) + + compute_expected(temp[2 * i + 1], (crypto::pedersen_hash::lookup::NUM_PEDERSEN_TABLES / 2))) + .x; } } - EXPECT_EQ(affine_element(result).x, affine_element(compute_expected(temp[0], 0) + compute_expected(fq(m), 15)).x); + EXPECT_EQ(affine_element(result).x, + affine_element(compute_expected(temp[0], 0) + + compute_expected(fq(m), (crypto::pedersen_hash::lookup::NUM_PEDERSEN_TABLES / 2))) + .x); }