From 9b074e32a04977e1a9c7286b4791dfe7d1c0d44c Mon Sep 17 00:00:00 2001 From: lucasxia01 Date: Fri, 12 Jan 2024 21:10:39 +0000 Subject: [PATCH 1/3] updated test to include failing case --- .../barretenberg/crypto/poseidon2/poseidon2.test.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/barretenberg/cpp/src/barretenberg/crypto/poseidon2/poseidon2.test.cpp b/barretenberg/cpp/src/barretenberg/crypto/poseidon2/poseidon2.test.cpp index 9649f757728..895e07a11f3 100644 --- a/barretenberg/cpp/src/barretenberg/crypto/poseidon2/poseidon2.test.cpp +++ b/barretenberg/cpp/src/barretenberg/crypto/poseidon2/poseidon2.test.cpp @@ -53,13 +53,16 @@ TEST(Poseidon2, HashBufferConsistencyCheck) // element barretenberg::fr a(std::string("00000b615c4d3e2fa0b1c2d3e4f56789fedcba9876543210abcdef0123456789")); - auto input_vec = to_buffer(a); // takes field element and converts it to 32 bytes + // takes field element and converts it to 32 bytes + auto input_vec = to_buffer(a); + barretenberg::fr result1 = crypto::Poseidon2::hash_buffer(input_vec); input_vec.erase(input_vec.begin()); // erase first byte since we want 31 bytes + barretenberg::fr result2 = crypto::Poseidon2::hash_buffer(input_vec); + std::vector input{ a }; auto expected = crypto::Poseidon2::hash(input); - barretenberg::fr result = crypto::Poseidon2::hash_buffer(input_vec); - - EXPECT_EQ(result, expected); + EXPECT_NE(result1, expected); + EXPECT_EQ(result2, expected); } } // namespace poseidon2_tests \ No newline at end of file From 17b72369b9be74193058f3966e3c49e4b47f4f75 Mon Sep 17 00:00:00 2001 From: lucasxia01 Date: Fri, 12 Jan 2024 21:10:57 +0000 Subject: [PATCH 2/3] updated to use span instead of vector --- .../cpp/src/barretenberg/crypto/poseidon2/poseidon2.cpp | 5 ++--- .../cpp/src/barretenberg/crypto/poseidon2/poseidon2.hpp | 2 +- .../cpp/src/barretenberg/stdlib/hash/poseidon2/poseidon2.cpp | 5 ++--- .../cpp/src/barretenberg/stdlib/hash/poseidon2/poseidon2.hpp | 2 +- .../barretenberg/stdlib/hash/poseidon2/poseidon2.test.cpp | 3 ++- 5 files changed, 8 insertions(+), 9 deletions(-) diff --git a/barretenberg/cpp/src/barretenberg/crypto/poseidon2/poseidon2.cpp b/barretenberg/cpp/src/barretenberg/crypto/poseidon2/poseidon2.cpp index ad96dc31271..65c6dc0f168 100644 --- a/barretenberg/cpp/src/barretenberg/crypto/poseidon2/poseidon2.cpp +++ b/barretenberg/cpp/src/barretenberg/crypto/poseidon2/poseidon2.cpp @@ -5,10 +5,9 @@ namespace crypto { * @brief Hashes a vector of field elements */ template -typename Poseidon2::FF Poseidon2::hash(const std::vector::FF>& input) +typename Poseidon2::FF Poseidon2::hash(const std::span::FF>& input) { - auto input_span = input; - return Sponge::hash_fixed_length(input_span); + return Sponge::hash_fixed_length(input); } /** diff --git a/barretenberg/cpp/src/barretenberg/crypto/poseidon2/poseidon2.hpp b/barretenberg/cpp/src/barretenberg/crypto/poseidon2/poseidon2.hpp index 6969c680e17..6f86bb35e3f 100644 --- a/barretenberg/cpp/src/barretenberg/crypto/poseidon2/poseidon2.hpp +++ b/barretenberg/cpp/src/barretenberg/crypto/poseidon2/poseidon2.hpp @@ -16,7 +16,7 @@ template class Poseidon2 { /** * @brief Hashes a vector of field elements */ - static FF hash(const std::vector& input); + static FF hash(const std::span& input); /** * @brief Hashes vector of bytes by chunking it into 31 byte field elements and calling hash() * @details Slice function cuts out the required number of bytes from the byte vector diff --git a/barretenberg/cpp/src/barretenberg/stdlib/hash/poseidon2/poseidon2.cpp b/barretenberg/cpp/src/barretenberg/stdlib/hash/poseidon2/poseidon2.cpp index ff8e31dd56e..167c6779c77 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/hash/poseidon2/poseidon2.cpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/hash/poseidon2/poseidon2.cpp @@ -8,15 +8,14 @@ using namespace proof_system; /** * @brief Hash a vector of field_ct. */ -template field_t poseidon2::hash(C& builder, const std::vector& inputs) +template field_t poseidon2::hash(C& builder, const std::span& inputs) { /* Run the sponge by absorbing all the input and squeezing one output. * This should just call the sponge variable length hash function * */ - auto input{ inputs }; - return Sponge::hash_fixed_length(builder, input); + return Sponge::hash_fixed_length(builder, inputs); } /** diff --git a/barretenberg/cpp/src/barretenberg/stdlib/hash/poseidon2/poseidon2.hpp b/barretenberg/cpp/src/barretenberg/stdlib/hash/poseidon2/poseidon2.hpp index fcfb93f91d2..521a33413bc 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/hash/poseidon2/poseidon2.hpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/hash/poseidon2/poseidon2.hpp @@ -26,7 +26,7 @@ template class poseidon2 { using Sponge = FieldSponge; public: - static field_ct hash(Builder& builder, const std::vector& in); + static field_ct hash(Builder& builder, const std::span& in); static field_ct hash_buffer(Builder& builder, const stdlib::byte_array& input); }; diff --git a/barretenberg/cpp/src/barretenberg/stdlib/hash/poseidon2/poseidon2.test.cpp b/barretenberg/cpp/src/barretenberg/stdlib/hash/poseidon2/poseidon2.test.cpp index 797cec6669c..f4dae1642e4 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/hash/poseidon2/poseidon2.test.cpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/hash/poseidon2/poseidon2.test.cpp @@ -68,7 +68,8 @@ template class StdlibPoseidon2 : public testing::Test { // num_inputs - 1 iterations since the first hash hashes two elements for (size_t i = 0; i < num_inputs - 1; ++i) { - left = poseidon2::hash(builder, { left, right }); + std::vector inputs = { left, right }; + left = poseidon2::hash(builder, inputs); } builder.set_public_input(left.witness_index); From e58d3ac4072207dde940f99e99f93d5b31578479 Mon Sep 17 00:00:00 2001 From: lucasxia01 Date: Fri, 12 Jan 2024 22:01:21 +0000 Subject: [PATCH 3/3] updating consts --- .../cpp/src/barretenberg/crypto/poseidon2/poseidon2.cpp | 2 +- .../cpp/src/barretenberg/crypto/poseidon2/poseidon2.hpp | 2 +- .../src/barretenberg/crypto/poseidon2/sponge/sponge.hpp | 7 ++++--- .../src/barretenberg/stdlib/hash/poseidon2/poseidon2.cpp | 2 +- .../src/barretenberg/stdlib/hash/poseidon2/poseidon2.hpp | 2 +- .../barretenberg/stdlib/hash/poseidon2/poseidon2.test.cpp | 3 +-- .../barretenberg/stdlib/hash/poseidon2/sponge/sponge.hpp | 6 +++--- 7 files changed, 12 insertions(+), 12 deletions(-) diff --git a/barretenberg/cpp/src/barretenberg/crypto/poseidon2/poseidon2.cpp b/barretenberg/cpp/src/barretenberg/crypto/poseidon2/poseidon2.cpp index 65c6dc0f168..2f74f7fc250 100644 --- a/barretenberg/cpp/src/barretenberg/crypto/poseidon2/poseidon2.cpp +++ b/barretenberg/cpp/src/barretenberg/crypto/poseidon2/poseidon2.cpp @@ -5,7 +5,7 @@ namespace crypto { * @brief Hashes a vector of field elements */ template -typename Poseidon2::FF Poseidon2::hash(const std::span::FF>& input) +typename Poseidon2::FF Poseidon2::hash(const std::vector::FF>& input) { return Sponge::hash_fixed_length(input); } diff --git a/barretenberg/cpp/src/barretenberg/crypto/poseidon2/poseidon2.hpp b/barretenberg/cpp/src/barretenberg/crypto/poseidon2/poseidon2.hpp index 6f86bb35e3f..6969c680e17 100644 --- a/barretenberg/cpp/src/barretenberg/crypto/poseidon2/poseidon2.hpp +++ b/barretenberg/cpp/src/barretenberg/crypto/poseidon2/poseidon2.hpp @@ -16,7 +16,7 @@ template class Poseidon2 { /** * @brief Hashes a vector of field elements */ - static FF hash(const std::span& input); + static FF hash(const std::vector& input); /** * @brief Hashes vector of bytes by chunking it into 31 byte field elements and calling hash() * @details Slice function cuts out the required number of bytes from the byte vector diff --git a/barretenberg/cpp/src/barretenberg/crypto/poseidon2/sponge/sponge.hpp b/barretenberg/cpp/src/barretenberg/crypto/poseidon2/sponge/sponge.hpp index bc4a2c5c1dc..80aab6d4589 100644 --- a/barretenberg/cpp/src/barretenberg/crypto/poseidon2/sponge/sponge.hpp +++ b/barretenberg/cpp/src/barretenberg/crypto/poseidon2/sponge/sponge.hpp @@ -129,7 +129,8 @@ template */ - template static std::array hash_internal(std::span input) + template + static std::array hash_internal(std::span input) { size_t in_len = input.size(); const uint256_t iv = (static_cast(in_len) << 64) + out_len - 1; @@ -153,11 +154,11 @@ template static std::array hash_fixed_length(std::span input) + template static std::array hash_fixed_length(std::span input) { return hash_internal(input); } - static FF hash_fixed_length(std::span input) { return hash_fixed_length<1>(input)[0]; } + static FF hash_fixed_length(std::span input) { return hash_fixed_length<1>(input)[0]; } template static std::array hash_variable_length(std::span input) { diff --git a/barretenberg/cpp/src/barretenberg/stdlib/hash/poseidon2/poseidon2.cpp b/barretenberg/cpp/src/barretenberg/stdlib/hash/poseidon2/poseidon2.cpp index 167c6779c77..9004ff3002b 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/hash/poseidon2/poseidon2.cpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/hash/poseidon2/poseidon2.cpp @@ -8,7 +8,7 @@ using namespace proof_system; /** * @brief Hash a vector of field_ct. */ -template field_t poseidon2::hash(C& builder, const std::span& inputs) +template field_t poseidon2::hash(C& builder, const std::vector& inputs) { /* Run the sponge by absorbing all the input and squeezing one output. diff --git a/barretenberg/cpp/src/barretenberg/stdlib/hash/poseidon2/poseidon2.hpp b/barretenberg/cpp/src/barretenberg/stdlib/hash/poseidon2/poseidon2.hpp index 521a33413bc..fcfb93f91d2 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/hash/poseidon2/poseidon2.hpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/hash/poseidon2/poseidon2.hpp @@ -26,7 +26,7 @@ template class poseidon2 { using Sponge = FieldSponge; public: - static field_ct hash(Builder& builder, const std::span& in); + static field_ct hash(Builder& builder, const std::vector& in); static field_ct hash_buffer(Builder& builder, const stdlib::byte_array& input); }; diff --git a/barretenberg/cpp/src/barretenberg/stdlib/hash/poseidon2/poseidon2.test.cpp b/barretenberg/cpp/src/barretenberg/stdlib/hash/poseidon2/poseidon2.test.cpp index f4dae1642e4..797cec6669c 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/hash/poseidon2/poseidon2.test.cpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/hash/poseidon2/poseidon2.test.cpp @@ -68,8 +68,7 @@ template class StdlibPoseidon2 : public testing::Test { // num_inputs - 1 iterations since the first hash hashes two elements for (size_t i = 0; i < num_inputs - 1; ++i) { - std::vector inputs = { left, right }; - left = poseidon2::hash(builder, inputs); + left = poseidon2::hash(builder, { left, right }); } builder.set_public_input(left.witness_index); diff --git a/barretenberg/cpp/src/barretenberg/stdlib/hash/poseidon2/sponge/sponge.hpp b/barretenberg/cpp/src/barretenberg/stdlib/hash/poseidon2/sponge/sponge.hpp index 359faf50f94..667e0daa886 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib/hash/poseidon2/sponge/sponge.hpp +++ b/barretenberg/cpp/src/barretenberg/stdlib/hash/poseidon2/sponge/sponge.hpp @@ -135,7 +135,7 @@ template */ template - static std::array hash_internal(Builder& builder, std::span input) + static std::array hash_internal(Builder& builder, std::span input) { size_t in_len = input.size(); const uint256_t iv = (static_cast(in_len) << 64) + out_len - 1; @@ -160,11 +160,11 @@ template - static std::array hash_fixed_length(Builder& builder, std::span input) + static std::array hash_fixed_length(Builder& builder, std::span input) { return hash_internal(builder, input); } - static field_t hash_fixed_length(Builder& builder, std::span input) + static field_t hash_fixed_length(Builder& builder, std::span input) { return hash_fixed_length<1>(builder, input)[0]; }