pub struct Poseidon2();
Expand description
Implementation of the Poseidon2 hash function with 256-bit output.
The implementation follows the original specification and its accompanying reference implementation.
The parameters used to instantiate the function are:
- Field: 64-bit prime field with modulus 2^64 - 2^32 + 1.
- State width: 12 field elements.
- Capacity size: 4 field elements.
- S-Box degree: 7.
- Rounds: There are 2 different types of rounds, called internal and external, and are structured as follows:
- Initial External rounds (IE):
add_constants
→apply_sbox
→apply_matmul_external
. - Internal rounds:
add_constants
→apply_sbox
→apply_matmul_internal
, where the constant addition and sbox application apply only to the first entry of the state. - Terminal External rounds (TE):
add_constants
→apply_sbox
→apply_matmul_external
. - An additional
apply_matmul_external
is inserted at the beginning in order to protect against some recent attacks.
The above parameters target a 128-bit security level. The digest consists of four field elements and it can be serialized into 32 bytes (256 bits).
§Hash output consistency
Functions hash_elements(), merge(), and merge_with_int() are internally consistent. That is, computing a hash for the same set of elements using these functions will always produce the same result. For example, merging two digests using merge() will produce the same result as hashing 8 elements which make up these digests using hash_elements() function.
However, hash() function is not consistent with functions mentioned above. For example, if we take two field elements, serialize them to bytes and hash them using hash(), the result will differ from the result obtained by hashing these elements directly using hash_elements() function. The reason for this difference is that hash() function needs to be able to handle arbitrary binary strings, which may or may not encode valid field elements - and thus, deserialization procedure used by this function is different from the procedure used to deserialize valid field elements.
Thus, if the underlying data consists of valid field elements, it might make more sense to deserialize them into field elements and then hash them using hash_elements() function rather than hashing the serialized bytes using hash() function.
§Domain separation
merge_in_domain() hashes two digests into one digest with some domain identifier and the current implementation sets the second capacity element to the value of this domain identifier. Using a similar argument to the one formulated for domain separation in Appendix C of the specifications, one sees that doing so degrades only pre-image resistance, from its initial bound of c.log_2(p), by as much as the log_2 of the size of the domain identifier space. Since pre-image resistance becomes the bottleneck for the security bound of the sponge in overwrite-mode only when it is lower than 2^128, we see that the target 128-bit security level is maintained as long as the size of the domain identifier space, including for padding, is less than 2^128.
§Hashing of empty input
The current implementation hashes empty input to the zero digest [0, 0, 0, 0]. This has the benefit of requiring no calls to the Poseidon2 permutation when hashing empty input.
Implementations§
Source§impl Poseidon2
impl Poseidon2
Sourcepub const NUM_EXTERNAL_ROUNDS_HALF: usize = 4usize
pub const NUM_EXTERNAL_ROUNDS_HALF: usize = 4usize
Number of initial or terminal external rounds.
Sourcepub const NUM_INTERNAL_ROUNDS: usize = 22usize
pub const NUM_INTERNAL_ROUNDS: usize = 22usize
Number of internal rounds.
Sourcepub const STATE_WIDTH: usize = 12usize
pub const STATE_WIDTH: usize = 12usize
Sponge state is set to 12 field elements or 768 bytes; 8 elements are reserved for rate and the remaining 4 elements are reserved for capacity.
Sourcepub const RATE_RANGE: Range<usize> = RATE_RANGE
pub const RATE_RANGE: Range<usize> = RATE_RANGE
The rate portion of the state is located in elements 4 through 11 (inclusive).
Sourcepub const CAPACITY_RANGE: Range<usize> = CAPACITY_RANGE
pub const CAPACITY_RANGE: Range<usize> = CAPACITY_RANGE
The capacity portion of the state is located in elements 0, 1, 2, and 3.
Sourcepub const DIGEST_RANGE: Range<usize> = DIGEST_RANGE
pub const DIGEST_RANGE: Range<usize> = DIGEST_RANGE
The output of the hash function can be read from state elements 4, 5, 6, and 7.
Sourcepub const MAT_DIAG: [BaseElement; 12] = MAT_DIAG
pub const MAT_DIAG: [BaseElement; 12] = MAT_DIAG
Matrix used for computing the linear layers of internal rounds.
Sourcepub const ARK_EXT_INITIAL: [[BaseElement; 12]; 4] = ARK_EXT_INITIAL
pub const ARK_EXT_INITIAL: [[BaseElement; 12]; 4] = ARK_EXT_INITIAL
Round constants added to the hasher state.
pub const ARK_EXT_TERMINAL: [[BaseElement; 12]; 4] = ARK_EXT_TERMINAL
pub const ARK_INT: [BaseElement; 22] = ARK_INT
Sourcepub fn merge(values: &[Word; 2]) -> Word
pub fn merge(values: &[Word; 2]) -> Word
Returns a hash of two digests. This method is intended for use in construction of Merkle trees and verification of Merkle paths.
Sourcepub fn hash_elements<E>(elements: &[E]) -> Wordwhere
E: FieldElement<BaseField = BaseElement>,
pub fn hash_elements<E>(elements: &[E]) -> Wordwhere
E: FieldElement<BaseField = BaseElement>,
Returns a hash of the provided field elements.
Sourcepub fn merge_in_domain(values: &[Word; 2], domain: BaseElement) -> Word
pub fn merge_in_domain(values: &[Word; 2], domain: BaseElement) -> Word
Returns a hash of two digests and a domain identifier.
Trait Implementations§
Source§impl ElementHasher for Poseidon2
impl ElementHasher for Poseidon2
Source§impl Hasher for Poseidon2
impl Hasher for Poseidon2
Source§const COLLISION_RESISTANCE: u32 = 128u32
const COLLISION_RESISTANCE: u32 = 128u32
Source§fn hash(bytes: &[u8]) -> <Poseidon2 as Hasher>::Digest
fn hash(bytes: &[u8]) -> <Poseidon2 as Hasher>::Digest
Source§fn merge(
values: &[<Poseidon2 as Hasher>::Digest; 2],
) -> <Poseidon2 as Hasher>::Digest
fn merge( values: &[<Poseidon2 as Hasher>::Digest; 2], ) -> <Poseidon2 as Hasher>::Digest
impl Copy for Poseidon2
impl Eq for Poseidon2
impl StructuralPartialEq for Poseidon2
Auto Trait Implementations§
impl Freeze for Poseidon2
impl RefUnwindSafe for Poseidon2
impl Send for Poseidon2
impl Sync for Poseidon2
impl Unpin for Poseidon2
impl UnwindSafe for Poseidon2
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<D> OwoColorize for D
impl<D> OwoColorize for D
Source§fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
Source§fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
Source§fn black(&self) -> FgColorDisplay<'_, Black, Self>
fn black(&self) -> FgColorDisplay<'_, Black, Self>
Source§fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
Source§fn red(&self) -> FgColorDisplay<'_, Red, Self>
fn red(&self) -> FgColorDisplay<'_, Red, Self>
Source§fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
Source§fn green(&self) -> FgColorDisplay<'_, Green, Self>
fn green(&self) -> FgColorDisplay<'_, Green, Self>
Source§fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
Source§fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
Source§fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
Source§fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
Source§fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
Source§fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
Source§fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
Source§fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
Source§fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
Source§fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
Source§fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
Source§fn white(&self) -> FgColorDisplay<'_, White, Self>
fn white(&self) -> FgColorDisplay<'_, White, Self>
Source§fn on_white(&self) -> BgColorDisplay<'_, White, Self>
fn on_white(&self) -> BgColorDisplay<'_, White, Self>
Source§fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
Source§fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
Source§fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
Source§fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
Source§fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
Source§fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
Source§fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
Source§fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
Source§fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
Source§fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
Source§fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
Source§fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
Source§fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
Source§fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
Source§fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
Source§fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
Source§fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
Source§fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
Source§fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
Source§fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
Source§fn bold(&self) -> BoldDisplay<'_, Self>
fn bold(&self) -> BoldDisplay<'_, Self>
Source§fn dimmed(&self) -> DimDisplay<'_, Self>
fn dimmed(&self) -> DimDisplay<'_, Self>
Source§fn italic(&self) -> ItalicDisplay<'_, Self>
fn italic(&self) -> ItalicDisplay<'_, Self>
Source§fn underline(&self) -> UnderlineDisplay<'_, Self>
fn underline(&self) -> UnderlineDisplay<'_, Self>
Source§fn blink(&self) -> BlinkDisplay<'_, Self>
fn blink(&self) -> BlinkDisplay<'_, Self>
Source§fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
Source§fn reversed(&self) -> ReversedDisplay<'_, Self>
fn reversed(&self) -> ReversedDisplay<'_, Self>
Source§fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
Source§fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::fg
or
a color-specific method, such as OwoColorize::green
, Read moreSource§fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::bg
or
a color-specific method, such as OwoColorize::on_yellow
, Read more