-
Notifications
You must be signed in to change notification settings - Fork 598
feat(avm): full poseidon2 #9141
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
Merged
Merged
Changes from all commits
Commits
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 hidden or 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 hidden or 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,89 @@ | ||
| include "./poseidon2.pil"; | ||
|
|
||
| // Performs the poseidon2 full hash | ||
| // It is **mostly** well-constrained | ||
| namespace poseidon2_full(256); | ||
| pol commit clk; | ||
| // These are the inputs to be hashed this round, we hash chunks of 3 field elements | ||
| pol commit input_0; | ||
| pol commit input_1; | ||
| pol commit input_2; | ||
|
|
||
| // Output of the hash it is matched with the result of the last permutation round | ||
| pol commit output; | ||
|
|
||
| pol commit sel_poseidon; | ||
| sel_poseidon * (1 - sel_poseidon) = 0; | ||
| sel_poseidon = execute_poseidon_perm + end_poseidon; | ||
IlyasRidhuan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| pol TWOPOW64 = 18446744073709551616; | ||
|
|
||
| pol commit input_len; | ||
| // Only used at the start of a new poseidon2 hash | ||
| pol IV = TWOPOW64 * input_len; | ||
|
|
||
| // Start of a poseidon2 computation | ||
| pol commit start_poseidon; | ||
| start_poseidon * (1 - start_poseidon) = 0; | ||
| // When we end a poseidon, the next row must naturally have a start_poseidon | ||
| sel_poseidon' * (1 - main.sel_first) * (start_poseidon' - end_poseidon) = 0; | ||
IlyasRidhuan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // We track the num of rounds remaining, excluding the first round that has to be performed by the start_poseidon selector. | ||
| // We use the padded length to calculate the num of rounds to perform and the unpadded length is used in the IV. | ||
| pol commit num_perm_rounds_rem; | ||
| pol commit padding; | ||
| // Padding can either be 0, 1 or 2 | ||
| padding * (padding - 1) * (padding - 2) = 0; | ||
| pol PADDED_LEN = input_len + padding; | ||
| start_poseidon * ((num_perm_rounds_rem + 1) * 3 - PADDED_LEN) = 0; | ||
|
|
||
|
|
||
| // The row with the final result of the poseidon computation | ||
| pol commit end_poseidon; | ||
| end_poseidon * (1 - end_poseidon) = 0; | ||
| // The final result of the output of the hash should match the output from the last permutation (b_0) | ||
| end_poseidon * (output - b_0) = 0; | ||
| pol commit num_perm_rounds_rem_inv; | ||
| // end_poseidon == 1 when the num_perm_rounds_rem == 0 | ||
| sel_poseidon * (num_perm_rounds_rem * (end_poseidon * (1 - num_perm_rounds_rem_inv) + num_perm_rounds_rem_inv) - 1 + end_poseidon) = 0; | ||
|
|
||
| // We perform the "squeeze" / perm operation until end_poseidon | ||
| pol commit execute_poseidon_perm; | ||
| execute_poseidon_perm * (1 - execute_poseidon_perm) = 0; | ||
| // The squeeze and end_poseidon selector must be mutually exclusive | ||
| sel_poseidon * (1 - end_poseidon - execute_poseidon_perm) = 0; | ||
| // Need an additional helper that holds the inverse of the num_perm_rounds_rem; | ||
| // If we still have rounds to perform, the num_perm_rounds_rem is decremented | ||
| execute_poseidon_perm * (num_perm_rounds_rem' - num_perm_rounds_rem + 1) = 0; | ||
|
|
||
|
|
||
| // The input values are represented by a_0, a_1, a_2, a_3 | ||
| // This most definitely could be simplified to a lower degree check | ||
| // the next perm input is constrained to be the previous perm output + the new values to be hashed. | ||
| // This occurs when we execute_poseidon_perm = 1 and we are not the start or the end of the poseidon perm | ||
| pol NEXT_INPUT_IS_PREV_OUTPUT_SEL = execute_poseidon_perm' * (1 - start_poseidon) * execute_poseidon_perm; | ||
| pol commit a_0; | ||
| start_poseidon * (a_0 - input_0) = 0; | ||
| sel_poseidon * NEXT_INPUT_IS_PREV_OUTPUT_SEL * (a_0' - b_0 - input_0') = 0; | ||
| pol commit a_1; | ||
| start_poseidon * (a_1 - input_1) = 0; | ||
| sel_poseidon * NEXT_INPUT_IS_PREV_OUTPUT_SEL * (a_1' - b_1 - input_1') = 0; | ||
| pol commit a_2; | ||
| start_poseidon * (a_2 - input_2) = 0; | ||
| sel_poseidon * NEXT_INPUT_IS_PREV_OUTPUT_SEL * (a_2' - b_2 - input_2') = 0; | ||
| pol commit a_3; | ||
| start_poseidon * (a_3 - IV) = 0; // IV is placed in the last slot if this is the start | ||
| sel_poseidon * NEXT_INPUT_IS_PREV_OUTPUT_SEL * (a_3' - b_3) = 0; | ||
|
|
||
| // Output value represented by b_0 | ||
| pol commit b_0; | ||
| pol commit b_1; | ||
| pol commit b_2; | ||
| pol commit b_3; | ||
|
|
||
| #[PERM_POS2_FIXED_POS2_PERM] | ||
| sel_poseidon {clk, a_0, a_1, a_2, a_3, b_0, b_1, b_2, b_3} | ||
| is | ||
| poseidon2.sel_poseidon_perm_immediate | ||
| { poseidon2.clk, poseidon2.a_0, poseidon2.a_1, poseidon2.a_2, poseidon2.a_3, | ||
| poseidon2.b_0, poseidon2.b_1, poseidon2.b_2, poseidon2.b_3 }; | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.