Struct Poseidon2
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_externalis 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§
§impl Poseidon2
impl Poseidon2
pub const COLLISION_RESISTANCE: u32 = 128
pub const COLLISION_RESISTANCE: u32 = 128
Target collision resistance level in bits.
pub const NUM_EXTERNAL_ROUNDS_HALF: usize = NUM_EXTERNAL_ROUNDS_HALF
pub const NUM_EXTERNAL_ROUNDS_HALF: usize = NUM_EXTERNAL_ROUNDS_HALF
Number of initial or terminal external rounds.
pub const NUM_INTERNAL_ROUNDS: usize = NUM_INTERNAL_ROUNDS
pub const NUM_INTERNAL_ROUNDS: usize = NUM_INTERNAL_ROUNDS
Number of internal rounds.
pub const STATE_WIDTH: usize = STATE_WIDTH
pub const STATE_WIDTH: usize = STATE_WIDTH
Sponge state is set to 12 field elements or 768 bytes; 8 elements are reserved for the rate and the remaining 4 elements are reserved for the capacity.
pub 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 0 through 7 (inclusive).
pub const RATE0_RANGE: Range<usize> = RATE0_RANGE
pub const RATE0_RANGE: Range<usize> = RATE0_RANGE
The first 4-element word of the rate portion.
pub const RATE1_RANGE: Range<usize> = RATE1_RANGE
pub const RATE1_RANGE: Range<usize> = RATE1_RANGE
The second 4-element word of the rate portion.
pub 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 8, 9, 10, and 11.
pub 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 0, 1, 2, and 3 (the first word of the state).
pub const MAT_DIAG: [Felt; 12] = MAT_DIAG
pub const MAT_DIAG: [Felt; 12] = MAT_DIAG
Matrix used for computing the linear layers of internal rounds.
pub const ARK_EXT_INITIAL: [[Felt; 12]; 4] = ARK_EXT_INITIAL
pub const ARK_EXT_INITIAL: [[Felt; 12]; 4] = ARK_EXT_INITIAL
Round constants added to the hasher state.
pub const ARK_EXT_TERMINAL: [[Felt; 12]; 4] = ARK_EXT_TERMINAL
pub const ARK_INT: [Felt; 22] = ARK_INT
pub fn apply_permutation(state: &mut [Felt; 12])
pub fn apply_permutation(state: &mut [Felt; 12])
Applies the Poseidon2 permutation to the provided state in-place.
pub fn hash_elements<E>(elements: &[E]) -> Wordwhere
E: BasedVectorSpace<Felt>,
pub fn hash_elements<E>(elements: &[E]) -> Wordwhere
E: BasedVectorSpace<Felt>,
Returns a hash of the provided field elements.
pub 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.
pub fn merge_many(values: &[Word]) -> Word
pub fn merge_many(values: &[Word]) -> Word
Returns a hash of multiple digests.
pub fn merge_with_int(seed: Word, value: u64) -> Word
pub fn merge_with_int(seed: Word, value: u64) -> Word
Returns a hash of a digest and a u64 value.
pub fn merge_in_domain(values: &[Word; 2], domain: Felt) -> Word
pub fn merge_in_domain(values: &[Word; 2], domain: Felt) -> Word
Returns a hash of two digests and a domain identifier.
pub fn apply_matmul_external(state: &mut [Felt; 12])
pub fn apply_matmul_external(state: &mut [Felt; 12])
Applies the M_E (external) linear layer to the state in-place.
This basically takes any 4 x 4 MDS matrix M and computes the matrix-vector product with
the matrix defined by [[2M, M, ..., M], [M, 2M, ..., M], ..., [M, M, ..., 2M]].
Given the structure of the above matrix, we can compute the product of the state with
matrix [M, M, ..., M] and compute the final result using a few addition.
pub fn matmul_internal(state: &mut [Felt; 12], mat_diag: [Felt; 12])
pub fn matmul_internal(state: &mut [Felt; 12], mat_diag: [Felt; 12])
Applies the M_I (internal) linear layer to the state in-place.
The matrix is given by its diagonal entries with the remaining entries set equal to 1. Hence, given the sum of the state entries, the matrix-vector product is computed using a multiply-and-add per state entry.
pub fn add_rc(state: &mut [Felt; 12], ark: &[Felt; 12])
pub fn add_rc(state: &mut [Felt; 12], ark: &[Felt; 12])
Adds the round constants to the state in-place.
pub fn apply_sbox(state: &mut [Felt; 12])
pub fn apply_sbox(state: &mut [Felt; 12])
Applies the S-box (x^7) to each element of the state in-place.
Trait Implementations§
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 UnsafeUnpin 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,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more§impl<D> OwoColorize for D
impl<D> OwoColorize for D
§fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
§fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
§fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
§fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
§fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
§fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
§fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
§fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
§fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
§fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
§fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
§fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
§fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
§fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
§fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
§fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
§fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
§fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
§fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
§fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
§fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
§fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
§fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
§fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
§fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
§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 more§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§fn fg_rgb<const R: u8, const G: u8, const B: u8>(
&self,
) -> FgColorDisplay<'_, CustomColor<R, G, B>, Self>
fn fg_rgb<const R: u8, const G: u8, const B: u8>( &self, ) -> FgColorDisplay<'_, CustomColor<R, G, B>, Self>
§fn bg_rgb<const R: u8, const G: u8, const B: u8>(
&self,
) -> BgColorDisplay<'_, CustomColor<R, G, B>, Self>
fn bg_rgb<const R: u8, const G: u8, const B: u8>( &self, ) -> BgColorDisplay<'_, CustomColor<R, G, B>, Self>
§fn truecolor(&self, r: u8, g: u8, b: u8) -> FgDynColorDisplay<'_, Rgb, Self>
fn truecolor(&self, r: u8, g: u8, b: u8) -> FgDynColorDisplay<'_, Rgb, Self>
§fn on_truecolor(&self, r: u8, g: u8, b: u8) -> BgDynColorDisplay<'_, Rgb, Self>
fn on_truecolor(&self, r: u8, g: u8, b: u8) -> BgDynColorDisplay<'_, Rgb, Self>
§impl<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere
T: ?Sized,
§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the foreground set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red() and
green(), which have the same functionality but are
pithier.
§Example
Set foreground color to white using fg():
use yansi::{Paint, Color};
painted.fg(Color::White);Set foreground color to white using white().
use yansi::Paint;
painted.white();§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the background set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red() and
on_green(), which have the same functionality but
are pithier.
§Example
Set background color to red using fg():
use yansi::{Paint, Color};
painted.bg(Color::Red);Set background color to red using on_red().
use yansi::Paint;
painted.on_red();§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling [Attribute] value.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold() and
underline(), which have the same functionality
but are pithier.
§Example
Make text bold using attr():
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);Make text bold using using bold().
use yansi::Paint;
painted.bold();§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi [Quirk] value.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask() and
wrap(), which have the same functionality but are
pithier.
§Example
Enable wrapping using .quirk():
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);Enable wrapping using wrap().
use yansi::Paint;
painted.wrap();§fn clear(&self) -> Painted<&T>
👎Deprecated since 1.0.1: renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
fn clear(&self) -> Painted<&T>
renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the [Condition] value
applies. Replaces any previous condition.
See the crate level docs for more details.
§Example
Enable styling painted only when both stdout and stderr are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);