-
Notifications
You must be signed in to change notification settings - Fork 317
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
feat: Poseidon2 stdlib impl #3551
Merged
Merged
Changes from all commits
Commits
Show all changes
43 commits
Select commit
Hold shift + click to select a range
aa536b5
initial files and impl
lucasxia01 89f4bc4
getting it to compile
lucasxia01 e5298b4
sponge stdlib
lucasxia01 e80739d
added unconstrained permutation
lucasxia01 751d74b
added poseidon2 end gate
lucasxia01 4b3ca57
using the gates in poseidon2 permutation (with bugs)
lucasxia01 4b68594
compile bug with templating fixed (thanks adam)
lucasxia01 aaec935
added initial external mul gates
lucasxia01 45dc5d4
renaming field_t<Builder> to FF
lucasxia01 d933eef
fixed compile problems
lucasxia01 171b0e8
naming: poseidon2_hash->poseidon2
lucasxia01 dc5f3f9
hash_buffers function to native poseidon2
lucasxia01 26b1930
new stdlib test file copied from pedersen
lucasxia01 0e312e9
passing builder into everything
lucasxia01 4126d04
turned values into witnesses
lucasxia01 41aa7c1
Merge branch 'master' into lx/poseidon2-stdlib
lucasxia01 8f8b799
busread fix
lucasxia01 7266f35
other witness creations
lucasxia01 f766e2b
fixing stdlib poseidon2 hash_buffers
lucasxia01 db908d7
compile fix
lucasxia01 9dc8392
added comments
lucasxia01 cc9edd9
Merge branch 'master' into lx/poseidon2-stdlib
lucasxia01 6ddfcae
trying to split into source file (wip)
lucasxia01 e15be66
fixed linker error
lucasxia01 222e532
updated comments, removed debug printing
lucasxia01 eedcefb
updated type names, comments
lucasxia01 c24436f
circleci gcc fix
lucasxia01 13807f1
circleci fix
lucasxia01 7dbb45c
adding comments, refactoring to source file
lucasxia01 8b25996
stupid ci fix
lucasxia01 bf75776
Merge branch 'master' into lx/poseidon2-stdlib
lucasxia01 6f04eb0
compile fix
lucasxia01 ff22a5f
minor update, comments
lucasxia01 2419db5
small comment update
lucasxia01 aeaf1a1
revamped poseidon2 stdlib tests
lucasxia01 f134a94
Merge branch 'master' into lx/poseidon2-stdlib
lucasxia01 6b67f11
fixed hash consistency test
lucasxia01 e9fd7da
updated tests to include hash_buffer
lucasxia01 2c66d1f
added poseidon2 tests to bb-tests
lucasxia01 4b7d151
added poseidon2 to stdlib tests
lucasxia01 8bd4df0
Merge branch 'master' into lx/poseidon2-stdlib
lucasxia01 af5e017
added missing stdlib pedersen hash tests
lucasxia01 8aa065f
undo weird merge? update to circuit/ files
lucasxia01 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe double check this change
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks fine both here and in poseidon but I don't see any unit tests for
hash_buffer
can you add some please?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, good point, will add those