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 all 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 @@ -7,8 +7,7 @@ namespace bb::crypto {
template <typename Params>
typename Poseidon2<Params>::FF Poseidon2<Params>::hash(const std::vector<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 @@ -52,12 +52,15 @@ TEST(Poseidon2, HashBufferConsistencyCheck)
// element
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);
bb::fr result1 = crypto::Poseidon2<crypto::Poseidon2Bn254ScalarFieldParams>::hash_buffer(input_vec);
input_vec.erase(input_vec.begin()); // erase first byte since we want 31 bytes
fr result2 = crypto::Poseidon2<crypto::Poseidon2Bn254ScalarFieldParams>::hash_buffer(input_vec);

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

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

EXPECT_EQ(result, expected);
EXPECT_NE(result1, expected);
EXPECT_EQ(result2, expected);
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ template <typename FF, size_t rate, size_t capacity, size_t t, typename Permutat
* @param input
* @return std::array<FF, out_len>
*/
template <size_t out_len, bool is_variable_length> static std::array<FF, out_len> hash_internal(std::span<FF> input)
template <size_t out_len, bool is_variable_length>
static std::array<FF, out_len> hash_internal(std::span<const FF> input)
{
size_t in_len = input.size();
const uint256_t iv = (static_cast<uint256_t>(in_len) << 64) + out_len - 1;
Expand All @@ -153,11 +154,11 @@ template <typename FF, size_t rate, size_t capacity, size_t t, typename Permutat
return output;
}

template <size_t out_len> static std::array<FF, out_len> hash_fixed_length(std::span<FF> input)
template <size_t out_len> static std::array<FF, out_len> hash_fixed_length(std::span<const FF> input)
{
return hash_internal<out_len, false>(input);
}
static FF hash_fixed_length(std::span<FF> input) { return hash_fixed_length<1>(input)[0]; }
static FF hash_fixed_length(std::span<const FF> input) { return hash_fixed_length<1>(input)[0]; }

template <size_t out_len> static std::array<FF, out_len> hash_variable_length(std::span<FF> input)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ template <typename C> field_t<C> poseidon2<C>::hash(C& builder, const std::vecto
* 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 @@ -135,7 +135,7 @@ template <size_t rate, size_t capacity, size_t t, typename Permutation, typename
* @return std::array<field_t, out_len>
*/
template <size_t out_len, bool is_variable_length>
static std::array<field_t, out_len> hash_internal(Builder& builder, std::span<field_t> input)
static std::array<field_t, out_len> hash_internal(Builder& builder, std::span<const field_t> 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.

allows for conversion from const std::vector to std::span. Otherwise, it throws an error.

{
size_t in_len = input.size();
const uint256_t iv = (static_cast<uint256_t>(in_len) << 64) + out_len - 1;
Expand All @@ -160,11 +160,11 @@ template <size_t rate, size_t capacity, size_t t, typename Permutation, typename
}

template <size_t out_len>
static std::array<field_t, out_len> hash_fixed_length(Builder& builder, std::span<field_t> input)
static std::array<field_t, out_len> hash_fixed_length(Builder& builder, std::span<const field_t> input)
{
return hash_internal<out_len, false>(builder, input);
}
static field_t hash_fixed_length(Builder& builder, std::span<field_t> input)
static field_t hash_fixed_length(Builder& builder, std::span<const field_t> input)
{
return hash_fixed_length<1>(builder, input)[0];
}
Expand Down
Loading