forked from visoftsolutions/noir_rs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Poseidon2 stdlib impl (AztecProtocol#3551)
Poseidon2 permutation and sponge function stdlib implementation that follows native crypto/ implementation. Adds hash_buffer function to native and stdlib poseidon2 implementations. Updates CI tests with poseidon2 tests, stdlib_pedersen_hash tests. Adds poseidon2 end gate. Resolves AztecProtocol/barretenberg#776
- Loading branch information
1 parent
8327427
commit 50b4a72
Showing
21 changed files
with
892 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
barretenberg/cpp/src/barretenberg/crypto/poseidon2/poseidon2.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#include "poseidon2.hpp" | ||
|
||
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) | ||
{ | ||
auto input_span = input; | ||
return Sponge::hash_fixed_length(input_span); | ||
} | ||
|
||
/** | ||
* @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 | ||
*/ | ||
template <typename Params> | ||
typename Poseidon2<Params>::FF Poseidon2<Params>::hash_buffer(const std::vector<uint8_t>& input) | ||
{ | ||
const size_t num_bytes = input.size(); | ||
const size_t bytes_per_element = 31; | ||
size_t num_elements = static_cast<size_t>(num_bytes % bytes_per_element != 0) + (num_bytes / bytes_per_element); | ||
|
||
const auto slice = [](const std::vector<uint8_t>& data, const size_t start, const size_t slice_size) { | ||
uint256_t result(0); | ||
for (size_t i = 0; i < slice_size; ++i) { | ||
result = (result << uint256_t(8)); | ||
result += uint256_t(data[i + start]); | ||
} | ||
return FF(result); | ||
}; | ||
|
||
std::vector<FF> converted; | ||
for (size_t i = 0; i < num_elements - 1; ++i) { | ||
size_t bytes_to_slice = bytes_per_element; | ||
FF element = slice(input, i * bytes_per_element, bytes_to_slice); | ||
converted.emplace_back(element); | ||
} | ||
size_t bytes_to_slice = num_bytes - ((num_elements - 1) * bytes_per_element); | ||
FF element = slice(input, (num_elements - 1) * bytes_per_element, bytes_to_slice); | ||
converted.emplace_back(element); | ||
|
||
return hash(converted); | ||
} | ||
|
||
template class Poseidon2<Poseidon2Bn254ScalarFieldParams>; | ||
} // namespace crypto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.