-
Notifications
You must be signed in to change notification settings - Fork 268
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e6db535
commit 0959678
Showing
29 changed files
with
1,955 additions
and
1,167 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
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; | ||
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; | ||
|
||
// 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 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.