Skip to content

Commit

Permalink
chore: do not differentiate variable vs fixed length for Poseidon2 (#…
Browse files Browse the repository at this point in the history
…11740)

Remove the terminator for poseidon2 that was used to distinguish between
fixed and variable size hashes.
  • Loading branch information
guipublic authored Feb 7, 2025
1 parent 67aec61 commit ee5fc45
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace bb::crypto {
template <typename Params>
typename Poseidon2<Params>::FF Poseidon2<Params>::hash(const std::vector<typename Poseidon2<Params>::FF>& input)
{
return Sponge::hash_fixed_length(input);
return Sponge::hash_internal(input);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,10 @@ template <typename FF, size_t rate, size_t capacity, size_t t, typename Permutat
* @brief Use the sponge to hash an input string
*
* @tparam out_len
* @tparam is_variable_length. Distinguishes between hashes where the preimage length is constant/not constant
* @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<const FF> input)
template <size_t out_len> 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 @@ -140,30 +138,13 @@ template <typename FF, size_t rate, size_t capacity, size_t t, typename Permutat
sponge.absorb(input[i]);
}

// In the case where the hash preimage is variable-length, we append `1` to the end of the input, to distinguish
// from fixed-length hashes. (the combination of this additional field element + the hash IV ensures
// fixed-length and variable-length hashes do not collide)
if constexpr (is_variable_length) {
sponge.absorb(1);
}

std::array<FF, out_len> output;
for (size_t i = 0; i < out_len; ++i) {
output[i] = sponge.squeeze();
}
return output;
}

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<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)
{
return hash_internal<out_len, true>(input);
}
static FF hash_variable_length(std::span<FF> input) { return hash_variable_length<1>(input)[0]; }
static FF hash_internal(std::span<const FF> input) { return hash_internal<1>(input)[0]; }
};
} // namespace bb::crypto
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +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
*
*/
return Sponge::hash_fixed_length(builder, inputs);
return Sponge::hash_internal(builder, inputs);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ template <size_t rate, size_t capacity, size_t t, typename Permutation, typename
* @param input
* @return std::array<field_t, out_len>
*/
template <size_t out_len, bool is_variable_length>
template <size_t out_len>
static std::array<field_t, out_len> hash_internal(Builder& builder, std::span<const field_t> input)
{
size_t in_len = input.size();
Expand All @@ -145,38 +145,16 @@ template <size_t rate, size_t capacity, size_t t, typename Permutation, typename
sponge.absorb(input[i]);
}

// In the case where the hash preimage is variable-length, we append `1` to the end of the input, to distinguish
// from fixed-length hashes. (the combination of this additional field element + the hash IV ensures
// fixed-length and variable-length hashes do not collide)
if constexpr (is_variable_length) {
sponge.absorb(1);
}

std::array<field_t, out_len> output;
for (size_t i = 0; i < out_len; ++i) {
output[i] = sponge.squeeze();
}
return output;
}

template <size_t out_len>
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<const field_t> input)
{
return hash_fixed_length<1>(builder, input)[0];
}

template <size_t out_len>
static std::array<field_t, out_len> hash_variable_length(Builder& builder, std::span<field_t> input)
{
return hash_internal<out_len, true>(builder, input);
}
static field_t hash_variable_length(Builder& builder, std::span<field_t> input)
static field_t hash_internal(Builder& builder, std::span<const field_t> input)
{
return hash_variable_length<1>(builder, input)[0];
return hash_internal<1>(builder, input)[0];
}
};
} // namespace bb::stdlib

1 comment on commit ee5fc45

@AztecBot
Copy link
Collaborator

Choose a reason for hiding this comment

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

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'C++ Benchmark'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.05.

Benchmark suite Current: ee5fc45 Previous: 67aec61 Ratio
commit(t) 2803749417 ns/iter 2632830496 ns/iter 1.06
Goblin::merge(t) 141306638 ns/iter 133888551 ns/iter 1.06

This comment was automatically generated by workflow using github-action-benchmark.

CC: @ludamad @codygunton

Please sign in to comment.