Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: poseidon2 hash uses span instead of vector #4003

Merged
merged 5 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ namespace crypto {
* @brief Hashes a vector of field elements
*/
template <typename Params>
typename Poseidon2<Params>::FF Poseidon2<Params>::hash(const std::vector<typename Poseidon2<Params>::FF>& input)
typename Poseidon2<Params>::FF Poseidon2<Params>::hash(const std::span<typename Poseidon2<Params>::FF>& input)
{
auto input_span = input;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this input_span is still a vector, but not completely sure how this inference works.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's just the type of the right hand side, typeof(input) is std::vector so auto becomes std::vector and like a typical std::vector a = b it copies

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, ok so the function call on the next line doesn't have an effect on this inference.

return Sponge::hash_fixed_length(input_span);
return Sponge::hash_fixed_length(input);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ template <typename Params> class Poseidon2 {
/**
* @brief Hashes a vector of field elements
*/
static FF hash(const std::vector<FF>& input);
static FF hash(const std::span<FF>& 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<crypto::Poseidon2Bn254ScalarFieldParams>::hash_buffer(input_vec);
input_vec.erase(input_vec.begin()); // erase first byte since we want 31 bytes
barretenberg::fr result2 = crypto::Poseidon2<crypto::Poseidon2Bn254ScalarFieldParams>::hash_buffer(input_vec);

std::vector<barretenberg::fr> input{ a };
auto expected = crypto::Poseidon2<crypto::Poseidon2Bn254ScalarFieldParams>::hash(input);

barretenberg::fr result = crypto::Poseidon2<crypto::Poseidon2Bn254ScalarFieldParams>::hash_buffer(input_vec);

EXPECT_EQ(result, expected);
EXPECT_NE(result1, expected);
EXPECT_EQ(result2, expected);
}
} // namespace poseidon2_tests
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@ using namespace proof_system;
/**
* @brief Hash a vector of field_ct.
*/
template <typename C> field_t<C> poseidon2<C>::hash(C& builder, const std::vector<field_ct>& inputs)
template <typename C> field_t<C> poseidon2<C>::hash(C& builder, const std::span<field_ct>& 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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ template <typename Builder> class poseidon2 {
using Sponge = FieldSponge<Params::t - 1, 1, Params::t, Permutation, Builder>;

public:
static field_ct hash(Builder& builder, const std::vector<field_ct>& in);
static field_ct hash(Builder& builder, const std::span<field_ct>& in);
static field_ct hash_buffer(Builder& builder, const stdlib::byte_array<Builder>& input);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ template <typename Builder> 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<fr_ct> inputs = { left, right };
left = poseidon2::hash(builder, inputs);
}

builder.set_public_input(left.witness_index);
Expand Down