diff --git a/CHANGELOG.md b/CHANGELOG.md index e262ca9b..bd5749f7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `VestingWallet` contract. #402 - `Erc1155Burnable` extension. #417 - `Erc1155MetadataUri` extension. #416 +- `Poseidon2` sponge hash function. #388 ### Changed @@ -34,7 +35,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - ERC-1155 Multi Token Standard. #275 - `SafeErc20` Utility. #289 -- Finite Fields arithmetics. #376 +- Finite Fields arithmetic. #376 - `Ownable2Step` contract. #352 - `IOwnable` trait. #352 @@ -46,7 +47,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed -- Mini alloc is now used by default via the stylus-sdk. This avoids conflicts with duplicate `#[global_allocator]` definitions. #373 +- Mini alloc is now used by default via the stylus-sdk. This avoids conflicts with duplicate `#[global_allocator]` + definitions. #373 - Removed the panic handler from the library, making it easier for `std` and `no_std` projects to use the library. #373 ## [0.1.0] - 2024-10-17 diff --git a/Cargo.toml b/Cargo.toml index 03bcca2f..413dba71 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -142,3 +142,10 @@ incremental = false [profile.dev] panic = "abort" + +[workspace.metadata.typos] +default = { extend-ignore-identifiers-re = [ + # ignore hex data samples. + "[0-9a-fA-F][0-9a-fA-F]", +] } +files = { extend-exclude = [] } diff --git a/lib/crypto/src/bigint.rs b/lib/crypto/src/bigint.rs index a9b49090..1c245a64 100644 --- a/lib/crypto/src/bigint.rs +++ b/lib/crypto/src/bigint.rs @@ -12,6 +12,7 @@ use core::{ #[allow(clippy::module_name_repetitions)] pub use crypto_bigint; use crypto_bigint::{Integer, Limb, Uint, Word, Zero}; +use num_traits::ConstZero; use zeroize::Zeroize; use crate::bits::BitIteratorBE; @@ -150,6 +151,9 @@ impl BitIteratorBE for Uint { /// Parse a number from a string in a given radix. /// +/// This implementation can be slow on big numbers and possibly fail constant +/// compilation by timeout. +/// /// I.e., convert string encoded integer `s` to base-`radix` number. #[must_use] pub const fn from_str_radix( @@ -157,6 +161,7 @@ pub const fn from_str_radix( radix: u32, ) -> Uint { let bytes = s.as_bytes(); + assert!(!bytes.is_empty(), "empty string"); // The lowest order number is at the end of the string. // Begin parsing from the last index of the string. @@ -167,14 +172,7 @@ pub const fn from_str_radix( let uint_radix = Uint::from_u32(radix); loop { - // Try to parse a digit from utf-8 byte - let ch = parse_utf8_byte(bytes[index]); - let digit = match ch.to_digit(radix) { - None => { - panic!("invalid digit"); - } - Some(digit) => Uint::from_u32(digit), - }; + let digit = Uint::from_u32(parse_digit(bytes[index], radix)); // Add a digit multiplied by order. uint = add(&uint, &mul(&digit, &order)); @@ -192,9 +190,53 @@ pub const fn from_str_radix( } } +/// Parse a number from a hex string. +/// +/// This implementation performs faster than [`from_str_radix`], since it +/// assumes the radix is already `16`. +/// +/// If the string number is shorter, then [`Uint`] can store. +/// Returns a [`Uint`] with leading zeroes. +#[must_use] +pub const fn from_str_hex(s: &str) -> Uint { + let bytes = s.as_bytes(); + assert!(!bytes.is_empty(), "empty string"); + + // The lowest order number is at the end of the string. + // Begin parsing from the last index of the string. + let mut index = bytes.len() - 1; + + // The lowest order limb is at the beginning of the `num` array. + // Begin indexing from `0`. + let mut num = [Word::ZERO; LIMBS]; + let mut num_index = 0; + + let digit_radix = 16; + let digit_size = 4; // Size of a hex digit in bits (2^4 = 16). + let digits_in_limb = Limb::BITS / digit_size; + + loop { + let digit = parse_digit(bytes[index], digit_radix) as Word; + + // Since a base-16 digit can be represented with the same bits, we can + // copy these bits. + let digit_mask = digit << ((num_index % digits_in_limb) * digit_size); + num[num_index / digits_in_limb] |= digit_mask; + + // If we reached the beginning of the string, return the number. + if index == 0 { + return Uint::from_words(num); + } + + // Move to the next digit. + index -= 1; + num_index += 1; + } +} + /// Multiply two numbers and panic on overflow. #[must_use] -pub const fn mul( +const fn mul( a: &Uint, b: &Uint, ) -> Uint { @@ -205,7 +247,7 @@ pub const fn mul( /// Add two numbers and panic on overflow. #[must_use] -pub const fn add( +const fn add( a: &Uint, b: &Uint, ) -> Uint { @@ -214,6 +256,17 @@ pub const fn add( low } +// Try to parse a digit from utf-8 byte. +const fn parse_digit(utf8_digit: u8, digit_radix: u32) -> u32 { + let ch = parse_utf8_byte(utf8_digit); + match ch.to_digit(digit_radix) { + None => { + panic!("invalid digit"); + } + Some(digit) => digit, + } +} + /// Parse a single UTF-8 byte. pub(crate) const fn parse_utf8_byte(byte: u8) -> char { match byte { @@ -234,17 +287,19 @@ macro_rules! from_num { #[macro_export] macro_rules! from_hex { ($num:literal) => { - $crate::bigint::crypto_bigint::Uint::from_be_hex($num) + $crate::bigint::from_str_hex($num) }; } #[cfg(all(test, feature = "std"))] mod test { + use proptest::proptest; + use super::*; #[test] fn convert_from_str_radix() { - let uint_from_base10 = from_str_radix::<4>( + let uint_from_base10: Uint<4> = from_str_radix( "28948022309329048855892746252171976963363056481941647379679742748393362948097", 10 ); @@ -257,14 +312,25 @@ mod test { ]); assert_eq!(uint_from_base10, expected); - let uint_from_base10 = from_str_radix::<1>("18446744069414584321", 10); - let uint_from_binary = from_str_radix::<1>( + let uint_from_base10: Uint<1> = + from_str_radix("18446744069414584321", 10); + let uint_from_binary: Uint<1> = from_str_radix( "1111111111111111111111111111111100000000000000000000000000000001", 2, ); assert_eq!(uint_from_base10, uint_from_binary); } + #[test] + fn convert_from_str_hex() { + // Test different implementations of hex parsing on random hex inputs. + proptest!(|(s in "[0-9a-fA-F]{1,64}")| { + let uint_from_hex: Uint<4> = from_str_hex(&s); + let expected: Uint<4> = from_str_radix(&s, 16); + assert_eq!(uint_from_hex, expected); + }); + } + #[test] fn uint_bit_iterator_be() { let words: [Word; 4] = [0b1100, 0, 0, 0]; diff --git a/lib/crypto/src/bits.rs b/lib/crypto/src/bits.rs index 0fab01ea..307b3657 100644 --- a/lib/crypto/src/bits.rs +++ b/lib/crypto/src/bits.rs @@ -32,7 +32,6 @@ impl_bit_iter_be!(usize); #[cfg(all(test, feature = "std"))] mod tests { - use super::*; #[test] diff --git a/lib/crypto/src/field/fp.rs b/lib/crypto/src/field/fp.rs index c840dd98..d5ee9052 100644 --- a/lib/crypto/src/field/fp.rs +++ b/lib/crypto/src/field/fp.rs @@ -859,9 +859,7 @@ macro_rules! fp_from_num { #[macro_export] macro_rules! fp_from_hex { ($num:literal) => {{ - $crate::field::fp::Fp::new( - $crate::bigint::crypto_bigint::Uint::from_be_hex($num), - ) + $crate::field::fp::Fp::new($crate::bigint::from_str_hex($num)) }}; } diff --git a/lib/crypto/src/lib.rs b/lib/crypto/src/lib.rs index 2077f5dc..76080443 100644 --- a/lib/crypto/src/lib.rs +++ b/lib/crypto/src/lib.rs @@ -30,5 +30,6 @@ pub mod field; pub mod hash; pub mod keccak; pub mod merkle; +pub mod poseidon2; pub use keccak::KeccakBuilder; diff --git a/lib/crypto/src/poseidon2/instance/babybear.rs b/lib/crypto/src/poseidon2/instance/babybear.rs new file mode 100644 index 00000000..8418bf33 --- /dev/null +++ b/lib/crypto/src/poseidon2/instance/babybear.rs @@ -0,0 +1,846 @@ +//! This module contains the poseidon sponge hash function parameters for +//! [`FpBabyBear`] field instance. + +use crate::{ + field::instance::FpBabyBear, fp_from_hex, poseidon2::params::PoseidonParams, +}; + +/// Poseidon sponge hash function parameters for [`FpBabyBear`] field instance. +pub struct BabyBear24Params; + +#[rustfmt::skip] +impl PoseidonParams for BabyBear24Params { + const T: usize = 24; + const D: u8 = 7; + const CAPACITY: usize = 1; + const ROUNDS_F: usize = 8; + const ROUNDS_P: usize = 21; + const MAT_INTERNAL_DIAG_M_1: &'static [FpBabyBear] = &[ + fp_from_hex!("409133f0"), + fp_from_hex!("1667a8a1"), + fp_from_hex!("06a6c7b6"), + fp_from_hex!("6f53160e"), + fp_from_hex!("273b11d1"), + fp_from_hex!("03176c5d"), + fp_from_hex!("72f9bbf9"), + fp_from_hex!("73ceba91"), + fp_from_hex!("5cdef81d"), + fp_from_hex!("01393285"), + fp_from_hex!("46daee06"), + fp_from_hex!("065d7ba6"), + fp_from_hex!("52d72d6f"), + fp_from_hex!("05dd05e0"), + fp_from_hex!("3bab4b63"), + fp_from_hex!("6ada3842"), + fp_from_hex!("2fc5fbec"), + fp_from_hex!("770d61b0"), + fp_from_hex!("5715aae9"), + fp_from_hex!("03ef0e90"), + fp_from_hex!("75b6c770"), + fp_from_hex!("242adf5f"), + fp_from_hex!("00d0ca4c"), + fp_from_hex!("36c0e388"), + ]; + const ROUND_CONSTANTS: &'static [&'static [FpBabyBear]] = &[ + &[ + fp_from_hex!("0fa20c37"), + fp_from_hex!("0795bb97"), + fp_from_hex!("12c60b9c"), + fp_from_hex!("0eabd88e"), + fp_from_hex!("096485ca"), + fp_from_hex!("07093527"), + fp_from_hex!("1b1d4e50"), + fp_from_hex!("30a01ace"), + fp_from_hex!("3bd86f5a"), + fp_from_hex!("69af7c28"), + fp_from_hex!("3f94775f"), + fp_from_hex!("731560e8"), + fp_from_hex!("465a0ecd"), + fp_from_hex!("574ef807"), + fp_from_hex!("62fd4870"), + fp_from_hex!("52ccfe44"), + fp_from_hex!("14772b14"), + fp_from_hex!("4dedf371"), + fp_from_hex!("260acd7c"), + fp_from_hex!("1f51dc58"), + fp_from_hex!("75125532"), + fp_from_hex!("686a4d7b"), + fp_from_hex!("54bac179"), + fp_from_hex!("31947706"), + ], + &[ + fp_from_hex!("29799d3b"), + fp_from_hex!("6e01ae90"), + fp_from_hex!("203a7a64"), + fp_from_hex!("4f7e25be"), + fp_from_hex!("72503f77"), + fp_from_hex!("45bd3b69"), + fp_from_hex!("769bd6b4"), + fp_from_hex!("5a867f08"), + fp_from_hex!("4fdba082"), + fp_from_hex!("251c4318"), + fp_from_hex!("28f06201"), + fp_from_hex!("6788c43a"), + fp_from_hex!("4c6d6a99"), + fp_from_hex!("357784a8"), + fp_from_hex!("2abaf051"), + fp_from_hex!("770f7de6"), + fp_from_hex!("1794b784"), + fp_from_hex!("4796c57a"), + fp_from_hex!("724b7a10"), + fp_from_hex!("449989a7"), + fp_from_hex!("64935cf1"), + fp_from_hex!("59e14aac"), + fp_from_hex!("0e620bb8"), + fp_from_hex!("3af5a33b"), + ], + &[ + fp_from_hex!("4465cc0e"), + fp_from_hex!("019df68f"), + fp_from_hex!("4af8d068"), + fp_from_hex!("08784f82"), + fp_from_hex!("0cefdeae"), + fp_from_hex!("6337a467"), + fp_from_hex!("32fa7a16"), + fp_from_hex!("486f62d6"), + fp_from_hex!("386a7480"), + fp_from_hex!("20f17c4a"), + fp_from_hex!("54e50da8"), + fp_from_hex!("2012cf03"), + fp_from_hex!("5fe52950"), + fp_from_hex!("09afb6cd"), + fp_from_hex!("2523044e"), + fp_from_hex!("5c54d0ef"), + fp_from_hex!("71c01f3c"), + fp_from_hex!("60b2c4fb"), + fp_from_hex!("4050b379"), + fp_from_hex!("5e6a70a5"), + fp_from_hex!("418543f5"), + fp_from_hex!("71debe56"), + fp_from_hex!("1aad2994"), + fp_from_hex!("3368a483"), + ], + &[ + fp_from_hex!("07a86f3a"), + fp_from_hex!("5ea43ff1"), + fp_from_hex!("2443780e"), + fp_from_hex!("4ce444f7"), + fp_from_hex!("146f9882"), + fp_from_hex!("3132b089"), + fp_from_hex!("197ea856"), + fp_from_hex!("667030c3"), + fp_from_hex!("2317d5dc"), + fp_from_hex!("0c2c48a7"), + fp_from_hex!("56b2df66"), + fp_from_hex!("67bd81e9"), + fp_from_hex!("4fcdfb19"), + fp_from_hex!("4baaef32"), + fp_from_hex!("0328d30a"), + fp_from_hex!("6235760d"), + fp_from_hex!("12432912"), + fp_from_hex!("0a49e258"), + fp_from_hex!("030e1b70"), + fp_from_hex!("48caeb03"), + fp_from_hex!("49e4d9e9"), + fp_from_hex!("1051b5c6"), + fp_from_hex!("6a36dbbe"), + fp_from_hex!("4cff27a5"), + ], + &[ + fp_from_hex!("1da78ec2"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + ], + &[ + fp_from_hex!("730b0924"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + ], + &[ + fp_from_hex!("3eb56cf3"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + ], + &[ + fp_from_hex!("5bd93073"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + ], + &[ + fp_from_hex!("37204c97"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + ], + &[ + fp_from_hex!("51642d89"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + ], + &[ + fp_from_hex!("66e943e8"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + ], + &[ + fp_from_hex!("1a3e72de"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + ], + &[ + fp_from_hex!("70beb1e9"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + ], + &[ + fp_from_hex!("30ff3b3f"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + ], + &[ + fp_from_hex!("4240d1c4"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + ], + &[ + fp_from_hex!("12647b8d"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + ], + &[ + fp_from_hex!("65d86965"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + ], + &[ + fp_from_hex!("49ef4d7c"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + ], + &[ + fp_from_hex!("47785697"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + ], + &[ + fp_from_hex!("46b3969f"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + ], + &[ + fp_from_hex!("5c7b7a0e"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + ], + &[ + fp_from_hex!("7078fc60"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + ], + &[ + fp_from_hex!("4f22d482"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + ], + &[ + fp_from_hex!("482a9aee"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + ], + &[ + fp_from_hex!("6beb839d"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + fp_from_hex!("00000000"), + ], + &[ + fp_from_hex!("032959ad"), + fp_from_hex!("2b18af6a"), + fp_from_hex!("55d3dc8c"), + fp_from_hex!("43bd26c8"), + fp_from_hex!("0c41595f"), + fp_from_hex!("7048d2e2"), + fp_from_hex!("00db8983"), + fp_from_hex!("2af563d7"), + fp_from_hex!("6e84758f"), + fp_from_hex!("611d64e1"), + fp_from_hex!("1f9977e2"), + fp_from_hex!("64163a0a"), + fp_from_hex!("5c5fc27b"), + fp_from_hex!("02e22561"), + fp_from_hex!("3a2d75db"), + fp_from_hex!("1ba7b71a"), + fp_from_hex!("34343f64"), + fp_from_hex!("7406b35d"), + fp_from_hex!("19df8299"), + fp_from_hex!("6ff4480a"), + fp_from_hex!("514a81c8"), + fp_from_hex!("57ab52ce"), + fp_from_hex!("6ad69f52"), + fp_from_hex!("3e0c0e0d"), + ], + &[ + fp_from_hex!("48126114"), + fp_from_hex!("2a9d62cc"), + fp_from_hex!("17441f23"), + fp_from_hex!("485762bb"), + fp_from_hex!("2f218674"), + fp_from_hex!("06fdc64a"), + fp_from_hex!("0861b7f2"), + fp_from_hex!("3b36eee6"), + fp_from_hex!("70a11040"), + fp_from_hex!("04b31737"), + fp_from_hex!("3722a872"), + fp_from_hex!("2a351c63"), + fp_from_hex!("623560dc"), + fp_from_hex!("62584ab2"), + fp_from_hex!("382c7c04"), + fp_from_hex!("3bf9edc7"), + fp_from_hex!("0e38fe51"), + fp_from_hex!("376f3b10"), + fp_from_hex!("5381e178"), + fp_from_hex!("3afc61c7"), + fp_from_hex!("5c1bcb4d"), + fp_from_hex!("6643ce1f"), + fp_from_hex!("2d0af1c1"), + fp_from_hex!("08f583cc"), + ], + &[ + fp_from_hex!("5d6ff60f"), + fp_from_hex!("6324c1e5"), + fp_from_hex!("74412fb7"), + fp_from_hex!("70c0192e"), + fp_from_hex!("0b72f141"), + fp_from_hex!("4067a111"), + fp_from_hex!("57388c4f"), + fp_from_hex!("351009ec"), + fp_from_hex!("0974c159"), + fp_from_hex!("539a58b3"), + fp_from_hex!("038c0cff"), + fp_from_hex!("476c0392"), + fp_from_hex!("3f7bc15f"), + fp_from_hex!("4491dd2c"), + fp_from_hex!("4d1fef55"), + fp_from_hex!("04936ae3"), + fp_from_hex!("58214dd4"), + fp_from_hex!("683c6aad"), + fp_from_hex!("1b42f16b"), + fp_from_hex!("6dc79135"), + fp_from_hex!("2d4e71ec"), + fp_from_hex!("3e2946ea"), + fp_from_hex!("59dce8db"), + fp_from_hex!("6cee892a"), + ], + &[ + fp_from_hex!("47f07350"), + fp_from_hex!("7106ce93"), + fp_from_hex!("3bd4a7a9"), + fp_from_hex!("2bfe636a"), + fp_from_hex!("430011e9"), + fp_from_hex!("001cd66a"), + fp_from_hex!("307faf5b"), + fp_from_hex!("0d9ef3fe"), + fp_from_hex!("6d40043a"), + fp_from_hex!("2e8f470c"), + fp_from_hex!("1b6865e8"), + fp_from_hex!("0c0e6c01"), + fp_from_hex!("4d41981f"), + fp_from_hex!("423b9d3d"), + fp_from_hex!("410408cc"), + fp_from_hex!("263f0884"), + fp_from_hex!("5311bbd0"), + fp_from_hex!("4dae58d8"), + fp_from_hex!("30401cea"), + fp_from_hex!("09afa575"), + fp_from_hex!("4b3d5b42"), + fp_from_hex!("63ac0b37"), + fp_from_hex!("5fe5bb14"), + fp_from_hex!("5244e9d4"), + ], + ]; +} + +#[allow(unused_imports)] +#[cfg(test)] +mod tests { + use crate::{ + field::instance::FpBabyBear, + fp_from_hex, + poseidon2::{instance::babybear::BabyBear24Params, *}, + }; + + type Scalar = FpBabyBear; + + #[test] + fn smoke() { + let mut poseidon2 = Poseidon2::::new(); + for i in 1..BabyBear24Params::T { + poseidon2.absorb(&Scalar::from(i as u64)); + } + let mut perm = poseidon2 + .squeeze_batch(BabyBear24Params::T - BabyBear24Params::CAPACITY) + .into_iter(); + assert_eq!(perm.next().unwrap(), fp_from_hex!("12921fb0")); + assert_eq!(perm.next().unwrap(), fp_from_hex!("0e659e79")); + assert_eq!(perm.next().unwrap(), fp_from_hex!("61d81dc9")); + assert_eq!(perm.next().unwrap(), fp_from_hex!("32bae33b")); + assert_eq!(perm.next().unwrap(), fp_from_hex!("62486ae3")); + assert_eq!(perm.next().unwrap(), fp_from_hex!("1e681b60")); + assert_eq!(perm.next().unwrap(), fp_from_hex!("24b91325")); + assert_eq!(perm.next().unwrap(), fp_from_hex!("2a2ef5b9")); + assert_eq!(perm.next().unwrap(), fp_from_hex!("50e8593e")); + assert_eq!(perm.next().unwrap(), fp_from_hex!("5bc818ec")); + assert_eq!(perm.next().unwrap(), fp_from_hex!("10691997")); + assert_eq!(perm.next().unwrap(), fp_from_hex!("35a14520")); + assert_eq!(perm.next().unwrap(), fp_from_hex!("2ba6a3c5")); + assert_eq!(perm.next().unwrap(), fp_from_hex!("279d47ec")); + assert_eq!(perm.next().unwrap(), fp_from_hex!("55014e81")); + assert_eq!(perm.next().unwrap(), fp_from_hex!("5953a67f")); + assert_eq!(perm.next().unwrap(), fp_from_hex!("2f403111")); + assert_eq!(perm.next().unwrap(), fp_from_hex!("6b8828ff")); + assert_eq!(perm.next().unwrap(), fp_from_hex!("1801301f")); + assert_eq!(perm.next().unwrap(), fp_from_hex!("2749207a")); + assert_eq!(perm.next().unwrap(), fp_from_hex!("3dc9cf21")); + assert_eq!(perm.next().unwrap(), fp_from_hex!("3c985ba2")); + assert_eq!(perm.next().unwrap(), fp_from_hex!("57a99864")); + } +} diff --git a/lib/crypto/src/poseidon2/instance/bls12.rs b/lib/crypto/src/poseidon2/instance/bls12.rs new file mode 100644 index 00000000..dd6c11de --- /dev/null +++ b/lib/crypto/src/poseidon2/instance/bls12.rs @@ -0,0 +1,1097 @@ +//! This module contains the poseidon sponge hash function parameters for +//! [`FpBLS12`] field instance. + +use crate::{ + field::instance::FpBLS12, fp_from_hex, poseidon2::params::PoseidonParams, +}; + +/// Poseidon sponge hash function parameters for [`FpBLS12`] field instance with +/// state size of `2`. +pub struct BLS2Params; + +#[rustfmt::skip] +impl PoseidonParams for BLS2Params { + const T: usize = 2; + const D: u8 = 5; + const CAPACITY: usize = 1; + const ROUNDS_F: usize = 8; + const ROUNDS_P: usize = 56; + const MAT_INTERNAL_DIAG_M_1: &'static [FpBLS12] = &[ + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000001"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000002"), + ]; + const ROUND_CONSTANTS: &'static [&'static [FpBLS12]] = &[ + &[ + fp_from_hex!("6267f5556c88257324c1c8b00d5871b2eba13cc39d72aa10dde6b69bc44c41c7"), + fp_from_hex!("30347723511438a085118166c68bf0c4f4ab5c10a2c55adb5cf87cc9e030f60f"), + ], + &[ + fp_from_hex!("10db856965e40038eb6427303181e7b7439f1a051aa4630c26cf86d0a0451a4b"), + fp_from_hex!("5a3d2dcd541e4faaae7eb143eec847a0f652b6dc1b92e3f39ec23c808b3a5d63"), + ], + &[ + fp_from_hex!("3b07f0ff7edcf93b1dd0487bc9fab1c6905f9ceee38dcce83efeb3a320398526"), + fp_from_hex!("40c73c524b9fd0fab63128175befe07b5c63ccdde9ca10e1a37205c9607fdf8a"), + ], + &[ + fp_from_hex!("3a933861cf23752376d94dbb24b0f3c61630787928875c07672b68abfb9191e0"), + fp_from_hex!("71cc165e208570b2d5ef81db84e3c5e714ea4edfb36fc7fb11ef65a64b2d9755"), + ], + &[ + fp_from_hex!("6c0dc9eb332b5d968bec8ad68fe24ce34087ea54093f153618434475bce402f8"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("0af5bafd335dae5c86967b11d5dcefb986a54c9d60d35eb06dc7a3fd779b3906"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("6e12847918f030f2626c150ab69e4be0f13d202ae1f8bc87ea74323e93372e3b"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("5565d40e21d059a26db241ca125d9316283eadf144b1318e604e253eeae1fe9a"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("608e01b42d3dca09fed9b54eadaaba3e4ce6aefe92b0dc954a0fa4683a9678f2"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("16bbe434b24f94e2c40ed1f4f9bd7d17e5be96c3aec15579b35fd80f0f80de9e"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("0d1be811a8e73220cab01ce981d475522c3d7dd9e2716c3a2cf4ddd541546890"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("5997a3affb18f942868b86f8ee10a68966e90bac7bbd8c65ede7e6e5ef1f6320"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("4d92e86d270041061eec80278079fca771499dea5ccdc99682a953bb3a038b8e"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("616c8c5ce232b9314f694fc6a968446ea9daf7a4079ce1a75fcc950741d680bb"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("677e31e7846d9131bdc350eaf11a8ff918dd258ddd800444424afab34dfdfe3d"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("4e7d7f85aefc110b233525ee3e53851aee7d3241e2a132585e0e25005eee0b0e"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("06a8b4539488b7dddc48c3a226dbda313f906e106f844196d55013d321244f13"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("5091517b6a85783108999f8e6bda3c793bef3f2e9589641d260bdfde8bdef00d"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("0d2703e5b30f54d7f414e901802d54f8c14cd6355415df6e0f063d16bef9c43a"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("56f69096811148eb38eec143d32565c077b3d1a4a4351f2b458f43b1659d4495"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("622d94d38d1ded428afd062008c5709b43a678f6ba518ec56383e8ffba473504"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("2730c607bba7333723a4a44577819b7db82a24574f6d13eee4c856c1ca3de9c7"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("01ac5f59256c5004dc1043c53b23800a3fbab53eb1a83f551056f227b514b9f6"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("0790b92523c973f1c95b94937afbb5796d89481e7a56328b44bab5ba81ae42f3"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("1d63b59d97bc269d13964fb3e8771d0acc749bc83eb2f0372484e266142bb8c0"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("1a52d04e5f14a3a05f7a01262df9e68c77fdf7e2bfb56c8b252d2140efdf0914"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("5aa9b3b808812b284857e8622843a8717fa5cb49b217017f31d79e8d0f963fc0"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("6a3d18fdbeb1d77ec1304539b00e6188786dbbc4435269b4c6281367f42656e3"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("4743e860df269a85dd76fb99dbe9d840eb669dc859754b3f74805e57ba288b00"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("6c32cac3946825f80a434c5ab397fc1a1c6a9bdfaab53175d4cf3d29ddb6cbc6"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("333b0eea5da7ed1e3959d16280a361aa77dd24ecbfb28e1b2583ac4e9894305c"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("3b503fc333b795ccc0c5bb3ae26b077dc3742cb745ec8821648c5ce7ebd9df18"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("4fa5853188d9f728a17532d94bee6fb28fee510380a5d50927c6c5b1ce283444"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("5d2ed8a6603a905bac490ebfb9e6c18f0bc9da1bbc2173291b18de6b6186118f"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("2d830a53584c5556264852f075c78f7f9eb068016ae88af9cda933d6ae52eca7"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("0250f4d6780ad29ae60e55f135b9ac80ccc7c81e3add37db276c26f1a2b1b86e"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("6e3e9595f59220599e23e830728d4a0c4d62515ec1ed10b72446cf4df5b4c308"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("2cd3314555d6faf23ee90cdb884f1c4697ebe98e3a450a624c4d896233b93cd5"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("584a408d0f370543b8413fee70a060a394e561f504d8679f7bece4bf222e4108"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("499cd53437b9fcbf7479c00fcc21295759074ce9bd1bb1fbd3460237aef4759e"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("56a9b567bd0646effd0608d74d537991136098d9a06af6cb3ff8f010efb57578"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("6a5fae2b00d968b931441b374e27ba4d03b306bd602d48731677169e75a67e8c"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("2e1cc28e390e64aa1d60edb99c0aeda7c8c32bdb01ba11abbad5026b46eccb27"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("2d4820000675df7c276beac408fe2e851e734a7008ae09bbcb3c96c70024f71b"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("0c2fe101a2b52b538b902c6b2dc992cb266f7636e05b0c068385b5fa19e97142"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("209b790b78c0e7927c6a178ef2f00b8687fc7bd4f21a9e02578551535002bc95"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("2dd0926cf56bbaaec6491513d08a9983f94a910852a7b4ea4bd4222b93e14c10"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("4316b39dd7d65b1bb575198104d409b169236a7ade371f7ab176fcbae75a5f0d"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("540276d61041b91f6ea3068ec260a9338b6e3da15d934e648c24f35aee04e535"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("37af612900b839977b146324c84772c58a4ccc0f6494cc054571827e74bfd2d3"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("2af00c93d59ed14c9911e5cb3781d772371e83228e4267bbce11d065c1955338"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("62b48779b0cf7ff2c10fd9b91a6ff7b7a99f935e961a5a94aa38f9d4f71c8b4c"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("540bf5bbe01f28563bcbe11a2ce346d8231a2cdd0fe07641f9fa89e5c21978e3"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("232b6c847a6d23912cb10ecbe50b53491f67f71e9b87a4a30446f2218017874b"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("0ab34adbe77b8f1e57a370e4fd626071eea74b3f0b66644a629efaa0e96456c0"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("1a83e43ef118c90046b1bdbeab8dd5cdcab632807c2cd0dc9147cbc5b7084be8"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("1ec6fa41b41b672d9005468720918130b642567462a3d557a595d4dc6c56f2f9"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("01f81a153199a751a111b8f5212cfc5bf82aacf0287d03e1864f8e5713fe4a17"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("2617307587a675f4ecd73a54a7b206162d751cabf3d9fd007bcca4de2c6f0649"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("1647be94c515178c7974a245624b642bb1ae6e2d4e1682087e362d7f98bc953f"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("6e690b956e00b9e339dec49d675586f661f9b081ee3fa7696d73977658aa6fea"), + fp_from_hex!("660b85bc22de06d476c47bf084ad436f59874f1d630c0f5c91fbef51d5e738c5"), + ], + &[ + fp_from_hex!("32bf3d451b69dde075fc370eaa8c1b77b5c0bc2aab1c7b46da7ef9d1840b0419"), + fp_from_hex!("73924b40beaa9c1ce4074c2154d1af4d658c09395a568b99b2fbcc3b5685e810"), + ], + &[ + fp_from_hex!("17cbb3ee0adcb9d977e96e9152b36042925244fdd0aa184c7a89a58a2dc40097"), + fp_from_hex!("29d76a821e3220775c552f6b5977ab94956e52b8dac36ef88ace050d553766a3"), + ], + &[ + fp_from_hex!("62b1a6c06ab26881a1fe57eceac56b5aec0b96da7211557f4e27ec24296d7db6"), + fp_from_hex!("0dfc474151e5c605a693a51ae8227cc0a99fdc4524fc2810c6eda9035d04334d"), + ], + ]; +} + +/// Poseidon sponge hash function parameters for [`FpBLS12`] field instance with +// /// state size of `3`. +pub struct BLS3Params; +#[rustfmt::skip] +impl PoseidonParams for BLS3Params { + const T: usize = 3; + const D: u8 = 5; + const CAPACITY: usize = 1; + const ROUNDS_F: usize = 8; + const ROUNDS_P: usize = 56; + const MAT_INTERNAL_DIAG_M_1: &'static [FpBLS12] = &[ + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000001"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000001"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000002"), + ]; + const ROUND_CONSTANTS: &'static [&'static [FpBLS12]] = &[ + &[ + fp_from_hex!("6f007a551156b3a449e44936b7c093644a0ed33f33eaccc628e942e836c1a875"), + fp_from_hex!("360d7470611e473d353f628f76d110f34e71162f31003b7057538c2596426303"), + fp_from_hex!("4b5fec3aa073df44019091f007a44ca996484965f7036dce3e9d0977edcdc0f6"), + ], + &[ + fp_from_hex!("67cf1868af6396c0b84cce715e539f849e06cd1c383ac5b06100c76bcc973a11"), + fp_from_hex!("555db4d1dced819f5d3de70fde83f1c7d3e8c98968e516a23a771a5c9c8257aa"), + fp_from_hex!("2bab94d7ae222d135dc3c6c5febfaa314908ac2f12ebe06fbdb74213bf63188b"), + ], + &[ + fp_from_hex!("66f44be5296682c4fa7882799d6dd049b6d7d2c950ccf98cf2e50d6d1ebb77c2"), + fp_from_hex!("150c93fef652fb1c2bf03e1a29aa871fef77e7d736766c5d0939d92753cc5dc8"), + fp_from_hex!("3270661e68928b3a955d55db56dc57c103cc0a60141e894e14259dce537782b2"), + ], + &[ + fp_from_hex!("073f116f04122e25a0b7afe4e2057299b407c370f2b5a1ccce9fb9ffc345afb3"), + fp_from_hex!("409fda22558cfe4d3dd8dce24f69e76f8c2aaeb1dd0f09d65e654c71f32aa23f"), + fp_from_hex!("2a32ec5c4ee5b1837affd09c1f53f5fd55c9cd2061ae93ca8ebad76fc71554d8"), + ], + &[ + fp_from_hex!("5848ebeb5923e92555b7124fffba5d6bd571c6f984195eb9cfd3a3e8eb55b1d4"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("270326ee039df19e651e2cfc740628ca634d24fc6e2559f22d8ccbe292efeead"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("27c6642ac633bc66dc100fe7fcfa54918af895bce012f182a068fc37c182e274"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("1bdfd8b01401c70ad27f57396989129d710e1fb6ab976a459ca18682e26d7ff9"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("491b9ba6983bcf9f05fe4794adb44a30879bf8289662e1f57d90f672414e8a4a"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("162a14c62f9a89b814b9d6a9c84dd678f4f6fb3f9054d373c832d824261a35ea"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("2d193e0f76de586b2af6f79e3127feeaac0a1fc71e2cf0c0f79824667b5b6bec"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("46efd8a9a262d6d8fdc9ca5c04b0982f24ddcc6e9863885a6a732a3906a07b95"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("509717e0c200e3c92d8dca2973b3db45f0788294351ad07ae75cbb780693a798"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("7299b28464a8c94fb9d4df61380f39c0dca9c2c014118789e227252820f01bfc"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("044ca3cc4a85d73b81696ef1104e674f4feff82984990ff85d0bf58dc8a4aa94"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("1cbaf2b371dac6a81d0453416d3e235cb8d9e2d4f314f46f6198785f0cd6b9af"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("1d5b2777692c205b0e6c49d061b6b5f4293c4ab038fdbbdc343e07610f3fede5"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("56ae7c7a5293bdc23e85e1698c81c77f8ad88c4b33a5780437ad047c6edb59ba"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("2e9bdbba3dd34bffaa30535bdd749a7e06a9adb0c1e6f962f60e971b8d73b04f"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("2de11886b18011ca8bd5bae36969299fde40fbe26d047b05035a13661f22418b"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("2e07de1780b8a70d0d5b4a3f1841dcd82ab9395c449be947bc998884ba96a721"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("0f69f1854d20ca0cbbdb63dbd52dad16250440a99d6b8af3825e4c2bb74925ca"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("5dc987318e6e59c1afb87b655dd58cc1d22e513a05838cd4585d04b135b957ca"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("48b725758571c9df6c01dc639a85f07297696b1bb678633a29dc91de95ef53f6"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("5e565e08c0821099256b56490eaee1d573afd10bb6d17d13ca4e5c611b2a3718"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("2eb1b25417fe17670d135dc639fb09a46ce5113507f96de9816c059422dc705e"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("115cd0a0643cfb988c24cb44c3fab48aff36c661d26cc42db8b1bdf4953bd82c"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("26ca293f7b2c462d066d7378b999868bbb57ddf14e0f958ade801612311d04cd"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("4147400d8e1aaccf311a6b5b762011ab3e45326e4d4b9de26992816b99c528ac"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("6b0db7dccc4ba1b268f6bdcc4d372848d4a72976c268ea30519a2f73e6db4d55"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("17bf1b93c4c7e01a2a830aa162412cd90f160bf9f71e967ff5209d14b24820ca"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("4b431cd9efedbc94cf1eca6f9e9c1839d0e66a8bffa8c8464cac81a39d3cf8f1"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("35b41a7ac4f3c571a24f8456369c85dfe03c0354bd8cfd3805c86f2e7dc293c5"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("3b1480080523c439435927994849bea964e14d3beb2dddde72ac156af435d09e"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("2cc6810031dc1b0d4950856dc907d57508e286442a2d3eb2271618d874b14c6d"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("6f4141c8401c5a395ba6790efd71c70c04afea06c3c92826bcabdd5cb5477d51"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("25bdbbeda1bde8c1059618e2afd2ef999e517aa93b78341d91f318c09f0cb566"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("392a4a8758e06ee8b95f33c25dde8ac02a5ed0a27b61926cc6313487073f7f7b"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("272a55878a08442b9aa6111f4de009485e6a6fd15db89365e7bbcef02eb5866c"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("631ec1d6d28dd9e824ee89a30730aef7ab463acfc9d184b355aa05fd6938eab5"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("4eb6fda10fd0fbde02c7449bfbddc35bcd8225e7e5c3833a0818a100409dc6f2"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("2d5b308b0cf02cdfefa13c4e60e26239a6ebba011694dd129b925b3c5b21e0e2"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("16549fc6af2f3b72dd5d293d72e2e5f244dff42f18b46c56ef38c57c311673ac"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("42332677ff359c5e8db836d9f5fb54822e39bd5e22340bb9ba975ba1a92be382"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("49d7d2c0b449e5179bc5ccc3b44c6075d9849b5610465f09ea725ddc97723a94"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("64c20fb90d7a003831757cc4c6226f6e4985fc9ecb416b9f684ca0351d967904"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("59cff40de83b52b41bc443d7979510d771c940b9758ca820fe73b5c8d5580934"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("53db2731730c39b04edd875fe3b7c882808285cdbc621d7af4f80dd53ebb71b0"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("1b10bb7a82afce39fa69c3a2ad52f76d76398265344203119b7126d9b46860df"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("561b6012d666bfe179c4dd7f84cdd1531596d3aac7c5700ceb319f91046a63c9"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("0f1e7505ebd91d2fc79c2df7dc98a3bed1b36968ba0405c090d27f6a00b7dfc8"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("2f313faf0d3f6187537a7497a3b43f46797fd6e3f18eb1caff457756b819bb20"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("3a5cbb6de450b481fa3ca61c0ed15bc55cad11ebf0f7ceb8f0bc3e732ecb26f6"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("681d93411bf8ce63f6716aefbd0e24506454c0348ee38fabeb264702714ccf94"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("5178e940f50004312646b436727f0e80a7b8f2e9ee1fdc677c4831a7672777fb"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("3dab54bc9bef688dd92086e253b439d651baa6e20f892b62865527cbca915982"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("4b3ce75311218f9ae905f84eaa5b2b3818448bbf3972e1aad69de321009015d0"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("06dbfb42b979884de280d31670123f744c24b33b410fefd4368045acf2b71ae3"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("068d6b4608aae810c6f039ea1973a63eb8d2de72e3d2c9eca7fc32d22f18b9d3"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("4c5c254589a92a36084a57d3b1d964278acc7e4fe8f69f2955954f27a79cebef"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("6cbac5e1700984ebc32da15b4bb9683faabab55f67ccc4f71d9560b3475a77eb"), + fp_from_hex!("4603c403bbfa9a17738a5c6278eaab1c37ec30b0737aa2409fc4898069eb983c"), + fp_from_hex!("6894e7e22b2c1d5c70a712a6345ae6b192a9c833a9234c31c56aacd16bc2f100"), + ], + &[ + fp_from_hex!("5be2cbbc44053ad08afa4d1eabc7f3d231eea799b93f226e905b7d4d65c58ebb"), + fp_from_hex!("58e55f287b453a9808624a8c2a353d528da0f7e713a5c6d0d7711e47063fa611"), + fp_from_hex!("366ebfafa3ad381c0ee258c9b8fdfccdb868a7d7e1f1f69a2b5dfcc5572555df"), + ], + &[ + fp_from_hex!("45766ab728968c642f90d97ccf5504ddc10518a819ebbcc4d09c3f5d784d67ce"), + fp_from_hex!("39678f65512f1ee404db3024f41d3f567ef66d89d044d022e6bc229e95bc76b1"), + fp_from_hex!("463aed1d2f1f955e3078be5bf7bfc46fc0eb8c51551906a8868f18ffae30cf4f"), + ], + &[ + fp_from_hex!("21668f016a8063c0d58b7750a3bc2fe1cf82c25f99dc01a4e534c88fe53d85fe"), + fp_from_hex!("39d00994a8a5046a1bc749363e98a768e34dea56439fe1954bef429bc5331608"), + fp_from_hex!("4d7f5dcd78ece9a933984de32c0b48fac2bba91f261996b8e9d1021773bd07cc"), + ], + ]; +} + +/// Poseidon sponge hash function parameters for [`FpBLS12`] field instance with +// /// state size of `4`. +pub struct BLS4Params; +#[rustfmt::skip] +impl PoseidonParams for BLS4Params { + const T: usize = 4; + const D: u8 = 5; + const CAPACITY: usize = 1; + const ROUNDS_F: usize = 8; + const ROUNDS_P: usize = 56; + const MAT_INTERNAL_DIAG_M_1: &'static [FpBLS12] = &[ + fp_from_hex!("07564ad691bf01c8601d68757a561d224f00f313ada673ab83e6255fb4fd5b3d"), + fp_from_hex!("6184e3be38549f7c0850cd069b32f6decbfde312dd4b8c18349b1b3776a6eaa4"), + fp_from_hex!("419289088178ad742be6f78425c0156b6546a18fd338f0169937dea46cfb64d2"), + fp_from_hex!("3244cdec173b71a4659e2529b499362dac10cb2fd17562860c8bb9d0fd45b787"), + ]; + const ROUND_CONSTANTS: &'static [&'static [FpBLS12]] = &[ + &[ + fp_from_hex!("1a3bdcbfc11dabfb6ed0dd5f5a9b38191488bce9eecd811c10f9378b32db8c61"), + fp_from_hex!("52b733e857912fdd2248dc9638dd79b1ce18b285b27792238b44c2b23c0f5d5f"), + fp_from_hex!("47d6df02d73e6c78ced550ec8df1a459ac41f318d8b904a37652b581b2b766b8"), + fp_from_hex!("11ec284726dddcf3cbb2b81862c9cd95e9de81ce0317302e1ef432d59b913388"), + ], + &[ + fp_from_hex!("19e21d749905904f3e10cd57f7817c2564c06ec1b1e229def2129e79a7a77738"), + fp_from_hex!("6eeecf4c83e1ec164217f3c00956d83c3bd845bee2d86b263cf3bf89a345e5e1"), + fp_from_hex!("10d5808ab47295f7a950dc72f968398b9c67426a217811b9bd7e97f2a261992f"), + fp_from_hex!("25d334fbadcbfd26449c4ce1472f961f7d401a3ddf40a70af5c10ac9176d151b"), + ], + &[ + fp_from_hex!("3ef8a46ec07ee551ecba60b1601cbf6e3664418331a254729b7c6a5c3d13a6c6"), + fp_from_hex!("30d373e298c09d2a6efe661d708fcfa6163dc61ae1eb103d98cf88c63e482125"), + fp_from_hex!("18769839949b2dda9ef3f111acd86e10250ffad4c4c4263ea970e4f4726cfad7"), + fp_from_hex!("2c941286d1534ff3be3a66f9dd59833e65b1fa67db23511f7b4b2ae3b3d66bc9"), + ], + &[ + fp_from_hex!("08b6d9c90b4139ea70497ac8a22b30fb9b76b8ceabf70449d282b57d98c60ddd"), + fp_from_hex!("3a8a2dd917222a71ca678b5c0a803a43c62de2f2c9fe37931b5b2a017fe64638"), + fp_from_hex!("2dc17308abd0e731b3cffafd296cc3e6e2403dc563baa1c797a68da9754dd207"), + fp_from_hex!("1d04e542f54431c23f9d1812392a0c87203144e343c459d3ee640b04e203be6c"), + ], + &[ + fp_from_hex!("500760e2ef6bf463fdecbf7b47f4adaa8214c797e59359439d63169e1cdb9dfb"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("61f36faac1c12d756dcc3b5491f2b22565409a4e952b8f8e726ee126c0e1c0e6"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("49e72db524a48243849fea068e58d0c078b0c738cd482a2e2160602657456ccb"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("4a74677fff94821e97b20b86063c36d35c6a2908c0a19a9f130edde9c8ceb6b5"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("62d27fd5080b44a436e13066ce1c80ddef2af086c2a4806bd99fa02de9726ded"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("15a26c92fc51dd285a61c2c1379f6731377aeb29c62696aa6a9c109c2991f200"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("1db900a5e9b2685be9f8f02498123ac71bd7bfa40b6853ab527a8d2512b9aefb"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("6ff755d237742498e0b78829cd8358bdb831e96fef08eb192b7e334123080afa"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("1595447e5b94a64c8b3681f57068e19ee3696440ef785e35123886311f48cd1e"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("2b0eabc08eef3abae3304cbb2efc67e7b437bd96b6f7d175daed6da5a2144e8b"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("3a345ee8e715f3226e52838652219202efb4e6d4f057f904d69d3fbd781e39f5"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("49eb2a07b5da81b57b7aa4fab19e691f9a684baef4cfb5afb6f46d23cbf1a9cd"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("3051663ef7de7674506f2c873409725363849564815b8bd291f00b9c60b4daf0"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("041a632a7bc167f67ca9c3825021a05b13f81f814ee101d37716e12efec3121f"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("6a5ae006b70bfebc1a485eed2c078654dc0f10514e8c739ca2a7019f42444ce5"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("0f3dba730b64dda2bc2eaa50ede0fcaa86a37c74c3a17be97a9d969fca21fb37"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("6d6c02004f2a4cbb965f9e73799041fafb49f61aee66232f45ae09972af08e86"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("60391f436a32fdb0eeba8578313579c04c48c126f4eb6a0c49e249ff1c1fbf4a"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("570a30710a42539d4440ce9a2cc7d9ac102409a188960d0fbd249fc4352bf3e5"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("35cb6ce55352599332471ad8be46ddc0cf598e0e36b8a8fa5fae6f26277b318e"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("48a8116b254b171052251f353c93ca3816734e4e0baeea10b4269bbab9a2be55"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("126258211a4bb2035121c7ca129eae719d4ddfefa99921a8befb7a160e0f845f"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("5bf6985473bc5500f140844f5b66cea85815772cce03596eda60e6c7dcb4ebbd"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("5af46d3dd02261d893c418582188d328a8a84ff8f6ac6c1997d3aaadf64220d9"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("5e3c84925609016da58df82c1f2b51590e3b91e5502dca2eba13d8354916ddbe"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("410e6fb4390379a8b726cfb22a9eaab7232bc4b60eaddb629a4aacfc7dcdbfcf"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("2ca5debb9de1c4bbfa0204543dd9757241611cd28b5eac2d7f37baa293fa2618"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("4a1554023154a99b4572572ccd5d89fa8745c117a2a85e5786ef6ef39410d611"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("2ca75fb22881b238b6c96da6b908f922da8b54f909237051775c64fec8d5920c"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("546ea8ad3dffe14c4c96e4d6d8c43c644212fb663486165b34cb26ddd717f341"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("6b168ee87f2c532efdb378116dfe462f632a18eccf1e9bcfc740d35e535b2735"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("4cf917a780b850292f44502b80865d3664d75213f89e8ae5861dbcb52aa95e76"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("02e8dfe9046d8b951532466bdf1b62571b85e551b34c8bbfb1ccd3216271b73d"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("4a34d2abd01f09f743cc56a3f87febd60ae5d88e224918d840656da973d9252e"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("0a3fad26c6318d471fa376fbaf02d341e41e687db6f3da88266de21c7157e940"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("131c9df7176eadb9af3a14f0f53a9b87196ef60bbb89b3630142ff8d9d69137b"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("5753682a8120106b42241c86deee879263d81fe8e3423e01f567316d9524647e"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("5ed9725af6a91090027acfc12100733a9dea89771048aa2dfe40daea9546645b"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("2c7b7a87f31ebd2464be9f211d41719b453939d53ff7be07a2cc21741d48b467"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("118159eb07353fef60e9733077cc82f92a6e2bd361c88d8122e9a292217757e0"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("5b4626e787d4a4b00ba4693fd7900ecaa19624be9f2fee6b9c3b538d4f896b8a"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("18cad22ae06bb0e8d043f7928477219e4d38ac544ce81c4c685267089e4c7003"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("0511149594758e789b440ce534b52b6c508ae1881669d1fa4a7e551ef84e4dc4"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("428b8532efa194a7f5cbf4c296786346ddfcfe8f2bab26f25321f5d984dfc307"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("00fd035cc129e91eac66d841e2864989d1ff4853662166143535603bf9460621"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("2c167565114475412c76f0df1961eaca9deb17622c9fb6f5ac8a89a29f42df69"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("10cac3ff02125419ac84ff80295589d1124a1e65a94e8d9b63a411317c7f5728"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("7213c2f17aec536d0a5196e877d1626a63d7db5b4e3bd77327eb3617b932aee7"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("1b86ee2e45fbcd59fa43edee6630dda5cb3b894eb4701eff5fd53967abad2666"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("0b941850091bd16875f408f183fb93d250c538e5ee1e8979e960b5b499b2f070"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("031f75d53bdd5ac603c252925148a74fa5f3bf5d9ffabe64c6e932be904204ec"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("55296a48dd399c65798cdbda11e89f26f998f2d33cf328e87a1c364e05be492b"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("191175ec337e64a48785a083559176168bcea3db2822630c48f1d497d03e1e35"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("33200b518e6b1a1e96ba214cad18929e7f5b4e3336e522c5fa1febf69c0818c6"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("2f989e24f97dc06c075bb53404e3eaaae32a1b6d89d9e09acdeee65712aa4216"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("0de4b77163f1572bbaa325f48bde7a429b6b0f803ade45a0e72cd2bdbd84680c"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("6af270d408f492402b7271432b6c443abbd1f0e220888d73f47498d59044d7cc"), + fp_from_hex!("3d5b5310e7b6be63be8f12594ecd68108f4b3f6266683b13178d0c80d8530c2f"), + fp_from_hex!("5f69d748d06e22916088bd21fb472334ddbe5223ff494b6c0dcdf62db69d43ac"), + fp_from_hex!("6c1fd7dbb3298da86dfe5d0435b26e0a91fe6ad6aa25b9407d5da544e6b9cd27"), + ], + &[ + fp_from_hex!("37172dd49bf14ebca52e1c31b60f31de8213644e330bcf2cc59684606e62537d"), + fp_from_hex!("509cf829b68e58861c3d6bce6046ac1b6e9c065378fe9556348c0c1814808e20"), + fp_from_hex!("529d5dd132f5cb4b71994ae6b330b7944f0fd8ba319582d2414b9b10768b7448"), + fp_from_hex!("5d251391abee228833dbd24be99d163ba9f8c92a2cf73a4e1f8a87e9c4ca97ba"), + ], + &[ + fp_from_hex!("1abe1d77226f2eff6f1a6bfc6c8d9e00dde1da9d67858e6984e53bc2aceba535"), + fp_from_hex!("4ca74ba72baea6d84fa8f2a05e387a1c41a5a1b2f1a479fb11bbf9d5e09bd01a"), + fp_from_hex!("232bfa6a3120224814967315ebf3d8c88b2eb0ea8c20c79ce854f90aec7a9b91"), + fp_from_hex!("109f5bd722c8cf28ab6a3852970bcb665f398a8dc9d331958dc291d49a1ba9b4"), + ], + &[ + fp_from_hex!("4a181a5de6c79a05c02533350ab51deee24b8f5b9154c5cf09ba5e25862ae176"), + fp_from_hex!("6e063b99c16028df94b1eb719164ae8c8336894791516ca2c37c5a1a394bb0d7"), + fp_from_hex!("6bbd2d3a643d301d25a8cf156542165abee3039068fecf501e9d39f313ca2a87"), + fp_from_hex!("04690fe1be7c7b8c10c81e63f5e508fe93853c61f0435f81eabc9997fa3b99f3"), + ], + ]; +} + +#[allow(unused_imports)] +#[cfg(test)] +mod tests { + use crate::{ + field::instance::FpBLS12, + fp_from_hex, + poseidon2::{ + instance::bls12::{BLS2Params, BLS3Params, BLS4Params}, + params::PoseidonParams, + Poseidon2, + }, + }; + + type Scalar = FpBLS12; + + #[test] + fn smoke() { + let mut poseidon2 = Poseidon2::::new(); + for i in 1..BLS2Params::T { + poseidon2.absorb(&Scalar::from(i as u64)); + } + let perm = poseidon2.squeeze_batch(1); + assert_eq!( + perm[0], + fp_from_hex!( + "1f0e305ee21c9366d5793b80251405032a3fee32b9dd0b5f4578262891b043b4" + ) + ); + + let mut poseidon2 = Poseidon2::::new(); + for i in 1..BLS3Params::T { + poseidon2.absorb(&Scalar::from(i as u64)); + } + let perm = poseidon2.squeeze_batch(2); + assert_eq!( + perm[0], + fp_from_hex!( + "4c5793c87d51bdc2c08a32108437dc0000bd0275868f09ebc5f36919af5b3891" + ) + ); + assert_eq!( + perm[1], + fp_from_hex!( + "1fc8ed171e67902ca49863159fe5ba6325318843d13976143b8125f08b50dc6b" + ) + ); + + let mut poseidon2 = Poseidon2::::new(); + for i in 1..BLS4Params::T { + poseidon2.absorb(&Scalar::from(i as u64)); + } + let perm = poseidon2.squeeze_batch(3); + assert_eq!( + perm[0], + fp_from_hex!( + "0e56f2b6fad25075aa93560185b70e2b180ed7e269159c507c288b6747a0db2d" + ) + ); + assert_eq!( + perm[1], + fp_from_hex!( + "6d8196f28da6006bb89b3df94600acdc03d0ba7c2b0f3f4409a54c1db6bf30d0" + ) + ); + assert_eq!( + perm[2], + fp_from_hex!( + "07cfb49540ee456cce38b8a7d1a930a57ffc6660737f6589ef184c5e15334e36" + ) + ); + } +} diff --git a/lib/crypto/src/poseidon2/instance/bn256.rs b/lib/crypto/src/poseidon2/instance/bn256.rs new file mode 100644 index 00000000..ca12b20b --- /dev/null +++ b/lib/crypto/src/poseidon2/instance/bn256.rs @@ -0,0 +1,384 @@ +//! This module contains the poseidon sponge hash function parameters for +//! [`FpBN256`] field instance. + +use crate::{ + field::instance::FpBN256, fp_from_hex, poseidon2::params::PoseidonParams, +}; + +/// Poseidon sponge hash function parameters for [`FpBN256`] field instance. +pub struct BN256Params; + +#[rustfmt::skip] +impl PoseidonParams for BN256Params { + const T: usize = 3; + const D: u8 = 5; + const CAPACITY: usize = 1; + const ROUNDS_F: usize = 8; + const ROUNDS_P: usize = 56; + const MAT_INTERNAL_DIAG_M_1: &'static [FpBN256] = &[ + fp_from_hex!( + "0000000000000000000000000000000000000000000000000000000000000001" + ), + fp_from_hex!( + "0000000000000000000000000000000000000000000000000000000000000001" + ), + fp_from_hex!( + "0000000000000000000000000000000000000000000000000000000000000002" + ), + ]; + const ROUND_CONSTANTS: &'static [&'static [FpBN256]] = &[ + &[ + fp_from_hex!("1d066a255517b7fd8bddd3a93f7804ef7f8fcde48bb4c37a59a09a1a97052816"), + fp_from_hex!("29daefb55f6f2dc6ac3f089cebcc6120b7c6fef31367b68eb7238547d32c1610"), + fp_from_hex!("1f2cb1624a78ee001ecbd88ad959d7012572d76f08ec5c4f9e8b7ad7b0b4e1d1"), + ], + &[ + fp_from_hex!("0aad2e79f15735f2bd77c0ed3d14aa27b11f092a53bbc6e1db0672ded84f31e5"), + fp_from_hex!("2252624f8617738cd6f661dd4094375f37028a98f1dece66091ccf1595b43f28"), + fp_from_hex!("1a24913a928b38485a65a84a291da1ff91c20626524b2b87d49f4f2c9018d735"), + ], + &[ + fp_from_hex!("22fc468f1759b74d7bfc427b5f11ebb10a41515ddff497b14fd6dae1508fc47a"), + fp_from_hex!("1059ca787f1f89ed9cd026e9c9ca107ae61956ff0b4121d5efd65515617f6e4d"), + fp_from_hex!("02be9473358461d8f61f3536d877de982123011f0bf6f155a45cbbfae8b981ce"), + ], + &[ + fp_from_hex!("0ec96c8e32962d462778a749c82ed623aba9b669ac5b8736a1ff3a441a5084a4"), + fp_from_hex!("292f906e073677405442d9553c45fa3f5a47a7cdb8c99f9648fb2e4d814df57e"), + fp_from_hex!("274982444157b86726c11b9a0f5e39a5cc611160a394ea460c63f0b2ffe5657e"), + ], + &[ + fp_from_hex!("1a1d063e54b1e764b63e1855bff015b8cedd192f47308731499573f23597d4b5"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("26abc66f3fdf8e68839d10956259063708235dccc1aa3793b91b002c5b257c37"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("0c7c64a9d887385381a578cfed5aed370754427aabca92a70b3c2b12ff4d7be8"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("1cf5998769e9fab79e17f0b6d08b2d1eba2ebac30dc386b0edd383831354b495"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("0f5e3a8566be31b7564ca60461e9e08b19828764a9669bc17aba0b97e66b0109"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("18df6a9d19ea90d895e60e4db0794a01f359a53a180b7d4b42bf3d7a531c976e"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("04f7bf2c5c0538ac6e4b782c3c6e601ad0ea1d3a3b9d25ef4e324055fa3123dc"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("29c76ce22255206e3c40058523748531e770c0584aa2328ce55d54628b89ebe6"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("198d425a45b78e85c053659ab4347f5d65b1b8e9c6108dbe00e0e945dbc5ff15"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("25ee27ab6296cd5e6af3cc79c598a1daa7ff7f6878b3c49d49d3a9a90c3fdf74"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("138ea8e0af41a1e024561001c0b6eb1505845d7d0c55b1b2c0f88687a96d1381"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("306197fb3fab671ef6e7c2cba2eefd0e42851b5b9811f2ca4013370a01d95687"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("1a0c7d52dc32a4432b66f0b4894d4f1a21db7565e5b4250486419eaf00e8f620"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("2b46b418de80915f3ff86a8e5c8bdfccebfbe5f55163cd6caa52997da2c54a9f"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("12d3e0dc0085873701f8b777b9673af9613a1af5db48e05bfb46e312b5829f64"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("263390cf74dc3a8870f5002ed21d089ffb2bf768230f648dba338a5cb19b3a1f"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("0a14f33a5fe668a60ac884b4ca607ad0f8abb5af40f96f1d7d543db52b003dcd"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("28ead9c586513eab1a5e86509d68b2da27be3a4f01171a1dd847df829bc683b9"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("1c6ab1c328c3c6430972031f1bdb2ac9888f0ea1abe71cffea16cda6e1a7416c"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("1fc7e71bc0b819792b2500239f7f8de04f6decd608cb98a932346015c5b42c94"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("03e107eb3a42b2ece380e0d860298f17c0c1e197c952650ee6dd85b93a0ddaa8"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("2d354a251f381a4669c0d52bf88b772c46452ca57c08697f454505f6941d78cd"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("094af88ab05d94baf687ef14bc566d1c522551d61606eda3d14b4606826f794b"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("19705b783bf3d2dc19bcaeabf02f8ca5e1ab5b6f2e3195a9d52b2d249d1396f7"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("09bf4acc3a8bce3f1fcc33fee54fc5b28723b16b7d740a3e60cef6852271200e"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("1803f8200db6013c50f83c0c8fab62843413732f301f7058543a073f3f3b5e4e"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("0f80afb5046244de30595b160b8d1f38bf6fb02d4454c0add41f7fef2faf3e5c"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("126ee1f8504f15c3d77f0088c1cfc964abcfcf643f4a6fea7dc3f98219529d78"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("23c203d10cfcc60f69bfb3d919552ca10ffb4ee63175ddf8ef86f991d7d0a591"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("2a2ae15d8b143709ec0d09705fa3a6303dec1ee4eec2cf747c5a339f7744fb94"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("07b60dee586ed6ef47e5c381ab6343ecc3d3b3006cb461bbb6b5d89081970b2b"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("27316b559be3edfd885d95c494c1ae3d8a98a320baa7d152132cfe583c9311bd"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("1d5c49ba157c32b8d8937cb2d3f84311ef834cc2a743ed662f5f9af0c0342e76"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("2f8b124e78163b2f332774e0b850b5ec09c01bf6979938f67c24bd5940968488"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("1e6843a5457416b6dc5b7aa09a9ce21b1d4cba6554e51d84665f75260113b3d5"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("11cdf00a35f650c55fca25c9929c8ad9a68daf9ac6a189ab1f5bc79f21641d4b"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("21632de3d3bbc5e42ef36e588158d6d4608b2815c77355b7e82b5b9b7eb560bc"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("0de625758452efbd97b27025fbd245e0255ae48ef2a329e449d7b5c51c18498a"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("2ad253c053e75213e2febfd4d976cc01dd9e1e1c6f0fb6b09b09546ba0838098"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("1d6b169ed63872dc6ec7681ec39b3be93dd49cdd13c813b7d35702e38d60b077"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("1660b740a143664bb9127c4941b67fed0be3ea70a24d5568c3a54e706cfef7fe"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("0065a92d1de81f34114f4ca2deef76e0ceacdddb12cf879096a29f10376ccbfe"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("1f11f065202535987367f823da7d672c353ebe2ccbc4869bcf30d50a5871040d"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("26596f5c5dd5a5d1b437ce7b14a2c3dd3bd1d1a39b6759ba110852d17df0693e"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("16f49bc727e45a2f7bf3056efcf8b6d38539c4163a5f1e706743db15af91860f"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("1abe1deb45b3e3119954175efb331bf4568feaf7ea8b3dc5e1a4e7438dd39e5f"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("0e426ccab66984d1d8993a74ca548b779f5db92aaec5f102020d34aea15fba59"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("0e7c30c2e2e8957f4933bd1942053f1f0071684b902d534fa841924303f6a6c6"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("0812a017ca92cf0a1622708fc7edff1d6166ded6e3528ead4c76e1f31d3fc69d"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("21a5ade3df2bc1b5bba949d1db96040068afe5026edd7a9c2e276b47cf010d54"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("01f3035463816c84ad711bf1a058c6c6bd101945f50e5afe72b1a5233f8749ce"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("0b115572f038c0e2028c2aafc2d06a5e8bf2f9398dbd0fdf4dcaa82b0f0c1c8b"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("1c38ec0b99b62fd4f0ef255543f50d2e27fc24db42bc910a3460613b6ef59e2f"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("1c89c6d9666272e8425c3ff1f4ac737b2f5d314606a297d4b1d0b254d880c53e"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("03326e643580356bf6d44008ae4c042a21ad4880097a5eb38b71e2311bb88f8f"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("268076b0054fb73f67cee9ea0e51e3ad50f27a6434b5dceb5bdde2299910a4c9"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("1acd63c67fbc9ab1626ed93491bda32e5da18ea9d8e4f10178d04aa6f8747ad0"), + fp_from_hex!("19f8a5d670e8ab66c4e3144be58ef6901bf93375e2323ec3ca8c86cd2a28b5a5"), + fp_from_hex!("1c0dc443519ad7a86efa40d2df10a011068193ea51f6c92ae1cfbb5f7b9b6893"), + ], + &[ + fp_from_hex!("14b39e7aa4068dbe50fe7190e421dc19fbeab33cb4f6a2c4180e4c3224987d3d"), + fp_from_hex!("1d449b71bd826ec58f28c63ea6c561b7b820fc519f01f021afb1e35e28b0795e"), + fp_from_hex!("1ea2c9a89baaddbb60fa97fe60fe9d8e89de141689d1252276524dc0a9e987fc"), + ], + &[ + fp_from_hex!("0478d66d43535a8cb57e9c1c3d6a2bd7591f9a46a0e9c058134d5cefdb3c7ff1"), + fp_from_hex!("19272db71eece6a6f608f3b2717f9cd2662e26ad86c400b21cde5e4a7b00bebe"), + fp_from_hex!("14226537335cab33c749c746f09208abb2dd1bd66a87ef75039be846af134166"), + ], + &[ + fp_from_hex!("01fd6af15956294f9dfe38c0d976a088b21c21e4a1c2e823f912f44961f9a9ce"), + fp_from_hex!("18e5abedd626ec307bca190b8b2cab1aaee2e62ed229ba5a5ad8518d4e5f2a57"), + fp_from_hex!("0fc1bbceba0590f5abbdffa6d3b35e3297c021a3a409926d0e2d54dc1c84fda6"), + ], + ]; +} + +#[allow(unused_imports)] +#[cfg(test)] +mod tests { + use crate::{ + field::instance::FpBN256, + fp_from_hex, + poseidon2::{instance::bn256::BN256Params, *}, + }; + + type Scalar = FpBN256; + + #[test] + fn smoke() { + let mut poseidon2 = Poseidon2::::new(); + for i in 1..BN256Params::T { + poseidon2.absorb(&Scalar::from(i as u64)); + } + let perm = poseidon2.squeeze_batch(2); + assert_eq!( + perm[0], + fp_from_hex!( + "303b6f7c86d043bfcbcc80214f26a30277a15d3f74ca654992defe7ff8d03570" + ) + ); + assert_eq!( + perm[1], + fp_from_hex!( + "1ed25194542b12eef8617361c3ba7c52e660b145994427cc86296242cf766ec8" + ) + ); + } +} diff --git a/lib/crypto/src/poseidon2/instance/goldilocks.rs b/lib/crypto/src/poseidon2/instance/goldilocks.rs new file mode 100644 index 00000000..ce450363 --- /dev/null +++ b/lib/crypto/src/poseidon2/instance/goldilocks.rs @@ -0,0 +1,491 @@ +//! This module contains the poseidon sponge hash function parameters for +//! [`FpGoldiLocks`] field instance. + +use crate::{ + field::instance::FpGoldiLocks, fp_from_hex, + poseidon2::params::PoseidonParams, +}; + +/// Poseidon sponge hash function parameters for [`FpGoldiLocks`] field +/// instance. +pub struct Goldilocks12Params; + +#[rustfmt::skip] +impl PoseidonParams for Goldilocks12Params { + const T: usize = 12; + const D: u8 = 7; + const CAPACITY: usize = 1; + const ROUNDS_F: usize = 8; + const ROUNDS_P: usize = 22; + const MAT_INTERNAL_DIAG_M_1: &'static [FpGoldiLocks] = &[ + fp_from_hex!("c3b6c08e23ba9300"), + fp_from_hex!("d84b5de94a324fb6"), + fp_from_hex!("0d0c371c5b35b84f"), + fp_from_hex!("7964f570e7188037"), + fp_from_hex!("5daf18bbd996604b"), + fp_from_hex!("6743bc47b9595257"), + fp_from_hex!("5528b9362c59bb70"), + fp_from_hex!("ac45e25b7127b68b"), + fp_from_hex!("a2077d7dfbb606b5"), + fp_from_hex!("f3faac6faee378ae"), + fp_from_hex!("0c6388b51545e883"), + fp_from_hex!("d27dbb6944917b60"), + ]; + const ROUND_CONSTANTS: &'static [&'static [FpGoldiLocks]] = &[ + &[ + fp_from_hex!("13dcf33aba214f46"), + fp_from_hex!("30b3b654a1da6d83"), + fp_from_hex!("1fc634ada6159b56"), + fp_from_hex!("937459964dc03466"), + fp_from_hex!("edd2ef2ca7949924"), + fp_from_hex!("ede9affde0e22f68"), + fp_from_hex!("8515b9d6bac9282d"), + fp_from_hex!("6b5c07b4e9e900d8"), + fp_from_hex!("1ec66368838c8a08"), + fp_from_hex!("9042367d80d1fbab"), + fp_from_hex!("400283564a3c3799"), + fp_from_hex!("4a00be0466bca75e"), + ], + &[ + fp_from_hex!("7913beee58e3817f"), + fp_from_hex!("f545e88532237d90"), + fp_from_hex!("22f8cb8736042005"), + fp_from_hex!("6f04990e247a2623"), + fp_from_hex!("fe22e87ba37c38cd"), + fp_from_hex!("d20e32c85ffe2815"), + fp_from_hex!("117227674048fe73"), + fp_from_hex!("4e9fb7ea98a6b145"), + fp_from_hex!("e0866c232b8af08b"), + fp_from_hex!("00bbc77916884964"), + fp_from_hex!("7031c0fb990d7116"), + fp_from_hex!("240a9e87cf35108f"), + ], + &[ + fp_from_hex!("2e6363a5a12244b3"), + fp_from_hex!("5e1c3787d1b5011c"), + fp_from_hex!("4132660e2a196e8b"), + fp_from_hex!("3a013b648d3d4327"), + fp_from_hex!("f79839f49888ea43"), + fp_from_hex!("fe85658ebafe1439"), + fp_from_hex!("b6889825a14240bd"), + fp_from_hex!("578453605541382b"), + fp_from_hex!("4508cda8f6b63ce9"), + fp_from_hex!("9c3ef35848684c91"), + fp_from_hex!("0812bde23c87178c"), + fp_from_hex!("fe49638f7f722c14"), + ], + &[ + fp_from_hex!("8e3f688ce885cbf5"), + fp_from_hex!("b8e110acf746a87d"), + fp_from_hex!("b4b2e8973a6dabef"), + fp_from_hex!("9e714c5da3d462ec"), + fp_from_hex!("6438f9033d3d0c15"), + fp_from_hex!("24312f7cf1a27199"), + fp_from_hex!("23f843bb47acbf71"), + fp_from_hex!("9183f11a34be9f01"), + fp_from_hex!("839062fbb9d45dbf"), + fp_from_hex!("24b56e7e6c2e43fa"), + fp_from_hex!("e1683da61c962a72"), + fp_from_hex!("a95c63971a19bfa7"), + ], + &[ + fp_from_hex!("4adf842aa75d4316"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + ], + &[ + fp_from_hex!("f8fbb871aa4ab4eb"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + ], + &[ + fp_from_hex!("68e85b6eb2dd6aeb"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + ], + &[ + fp_from_hex!("07a0b06b2d270380"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + ], + &[ + fp_from_hex!("d94e0228bd282de4"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + ], + &[ + fp_from_hex!("8bdd91d3250c5278"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + ], + &[ + fp_from_hex!("209c68b88bba778f"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + ], + &[ + fp_from_hex!("b5e18cdab77f3877"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + ], + &[ + fp_from_hex!("b296a3e808da93fa"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + ], + &[ + fp_from_hex!("8370ecbda11a327e"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + ], + &[ + fp_from_hex!("3f9075283775dad8"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + ], + &[ + fp_from_hex!("b78095bb23c6aa84"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + ], + &[ + fp_from_hex!("3f36b9fe72ad4e5f"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + ], + &[ + fp_from_hex!("69bc96780b10b553"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + ], + &[ + fp_from_hex!("3f1d341f2eb7b881"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + ], + &[ + fp_from_hex!("4e939e9815838818"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + ], + &[ + fp_from_hex!("da366b3ae2a31604"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + ], + &[ + fp_from_hex!("bc89db1e7287d509"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + ], + &[ + fp_from_hex!("6102f411f9ef5659"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + ], + &[ + fp_from_hex!("58725c5e7ac1f0ab"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + ], + &[ + fp_from_hex!("0df5856c798883e7"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + ], + &[ + fp_from_hex!("f7bb62a8da4c961b"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + fp_from_hex!("0000000000000000"), + ], + &[ + fp_from_hex!("c68be7c94882a24d"), + fp_from_hex!("af996d5d5cdaedd9"), + fp_from_hex!("9717f025e7daf6a5"), + fp_from_hex!("6436679e6e7216f4"), + fp_from_hex!("8a223d99047af267"), + fp_from_hex!("bb512e35a133ba9a"), + fp_from_hex!("fbbf44097671aa03"), + fp_from_hex!("f04058ebf6811e61"), + fp_from_hex!("5cca84703fac7ffb"), + fp_from_hex!("9b55c7945de6469f"), + fp_from_hex!("8e05bf09808e934f"), + fp_from_hex!("2ea900de876307d7"), + ], + &[ + fp_from_hex!("7748fff2b38dfb89"), + fp_from_hex!("6b99a676dd3b5d81"), + fp_from_hex!("ac4bb7c627cf7c13"), + fp_from_hex!("adb6ebe5e9e2f5ba"), + fp_from_hex!("2d33378cafa24ae3"), + fp_from_hex!("1e5b73807543f8c2"), + fp_from_hex!("09208814bfebb10f"), + fp_from_hex!("782e64b6bb5b93dd"), + fp_from_hex!("add5a48eac90b50f"), + fp_from_hex!("add4c54c736ea4b1"), + fp_from_hex!("d58dbb86ed817fd8"), + fp_from_hex!("6d5ed1a533f34ddd"), + ], + &[ + fp_from_hex!("28686aa3e36b7cb9"), + fp_from_hex!("591abd3476689f36"), + fp_from_hex!("047d766678f13875"), + fp_from_hex!("a2a11112625f5b49"), + fp_from_hex!("21fd10a3f8304958"), + fp_from_hex!("f9b40711443b0280"), + fp_from_hex!("d2697eb8b2bde88e"), + fp_from_hex!("3493790b51731b3f"), + fp_from_hex!("11caf9dd73764023"), + fp_from_hex!("7acfb8f72878164e"), + fp_from_hex!("744ec4db23cefc26"), + fp_from_hex!("1e00e58f422c6340"), + ], + &[ + fp_from_hex!("21dd28d906a62dda"), + fp_from_hex!("f32a46ab5f465b5f"), + fp_from_hex!("bfce13201f3f7e6b"), + fp_from_hex!("f30d2e7adb5304e2"), + fp_from_hex!("ecdf4ee4abad48e9"), + fp_from_hex!("f94e82182d395019"), + fp_from_hex!("4ee52e3744d887c5"), + fp_from_hex!("a1341c7cac0083b2"), + fp_from_hex!("2302fb26c30c834a"), + fp_from_hex!("aea3c587273bf7d3"), + fp_from_hex!("f798e24961823ec7"), + fp_from_hex!("962deba3e9a2cd94"), + ], + ]; +} + +#[allow(unused_imports)] +#[cfg(test)] +mod tests { + use crate::{ + field::instance::FpGoldiLocks, + fp_from_hex, + poseidon2::{instance::goldilocks::Goldilocks12Params, *}, + }; + + type Scalar = FpGoldiLocks; + + #[test] + fn smoke() { + let mut poseidon2 = Poseidon2::::new(); + for i in 1..Goldilocks12Params::T { + poseidon2.absorb(&Scalar::from(i as u64)); + } + let mut perm = poseidon2 + .squeeze_batch(Goldilocks12Params::T - Goldilocks12Params::CAPACITY) + .into_iter(); + + assert_eq!(perm.next().unwrap(), fp_from_hex!("1f0d2cc525b2540c")); + assert_eq!(perm.next().unwrap(), fp_from_hex!("6282c1dfe1e0358d")); + assert_eq!(perm.next().unwrap(), fp_from_hex!("e780d721f698e1e6")); + assert_eq!(perm.next().unwrap(), fp_from_hex!("280c0b6f753d833b")); + assert_eq!(perm.next().unwrap(), fp_from_hex!("1b942dd5023156ab")); + assert_eq!(perm.next().unwrap(), fp_from_hex!("43f0df3fcccb8398")); + assert_eq!(perm.next().unwrap(), fp_from_hex!("e8e8190585489025")); + assert_eq!(perm.next().unwrap(), fp_from_hex!("56bdbf72f77ada22")); + assert_eq!(perm.next().unwrap(), fp_from_hex!("7911c32bf9dcd705")); + assert_eq!(perm.next().unwrap(), fp_from_hex!("ec467926508fbe67")); + assert_eq!(perm.next().unwrap(), fp_from_hex!("6a50450ddf85a6ed")); + } +} diff --git a/lib/crypto/src/poseidon2/instance/mod.rs b/lib/crypto/src/poseidon2/instance/mod.rs new file mode 100644 index 00000000..b5482d29 --- /dev/null +++ b/lib/crypto/src/poseidon2/instance/mod.rs @@ -0,0 +1,8 @@ +//! This module contains the poseidon hash instances for some popular finite +//! field instances. +pub mod babybear; +pub mod bls12; +pub mod bn256; +pub mod goldilocks; +pub mod pallas; +pub mod vesta; diff --git a/lib/crypto/src/poseidon2/instance/pallas.rs b/lib/crypto/src/poseidon2/instance/pallas.rs new file mode 100644 index 00000000..7b3dd451 --- /dev/null +++ b/lib/crypto/src/poseidon2/instance/pallas.rs @@ -0,0 +1,383 @@ +//! This module contains the poseidon sponge hash function parameters for +//! [`FpPallas`] field instance. + +use crate::{ + field::instance::FpPallas, fp_from_hex, poseidon2::params::PoseidonParams, +}; + +/// Poseidon sponge hash function parameters for [`FpPallas`] field instance. +pub struct PallasParams; + +#[rustfmt::skip] +impl PoseidonParams for PallasParams { + const T: usize = 3; + const D: u8 = 5; + const CAPACITY: usize = 1; + const ROUNDS_F: usize = 8; + const ROUNDS_P: usize = 56; + const MAT_INTERNAL_DIAG_M_1: &'static [FpPallas] = &[ + fp_from_hex!( + "0000000000000000000000000000000000000000000000000000000000000001" + ), + fp_from_hex!( + "0000000000000000000000000000000000000000000000000000000000000001" + ), + fp_from_hex!( + "0000000000000000000000000000000000000000000000000000000000000002" + ), + ]; + const ROUND_CONSTANTS: &'static [&'static [FpPallas]] = &[ + &[ + fp_from_hex!("360d7470611e473d353f628f76d110f34e71162f31003b7057538c2596426303"), + fp_from_hex!("2bab94d7ae222d135dc3c6c5febfaa314908ac2f12ebe06fbdb74213bf63188b"), + fp_from_hex!("150c93fef652fb1c2bf03e1a29aa871fef77e7d736766c5d0939d92753cc5dc8"), + ], + &[ + fp_from_hex!("3270661e68928b3a955d55db56dc57c103cc0a60141e894e14259dce537782b2"), + fp_from_hex!("073f116f04122e25a0b7afe4e2057299b407c370f2b5a1ccce9fb9ffc345afb3"), + fp_from_hex!("2a32ec5c4ee5b1837affd09c1f53f5fd55c9cd2061ae93ca8ebad76fc71554d8"), + ], + &[ + fp_from_hex!("270326ee039df19e651e2cfc740628ca634d24fc6e2559f22d8ccbe292efeead"), + fp_from_hex!("27c6642ac633bc66dc100fe7fcfa54918af895bce012f182a068fc37c182e274"), + fp_from_hex!("1bdfd8b01401c70ad27f57396989129d710e1fb6ab976a459ca18682e26d7ff9"), + ], + &[ + fp_from_hex!("162a14c62f9a89b814b9d6a9c84dd678f4f6fb3f9054d373c832d824261a35ea"), + fp_from_hex!("2d193e0f76de586b2af6f79e3127feeaac0a1fc71e2cf0c0f79824667b5b6bec"), + fp_from_hex!("044ca3cc4a85d73b81696ef1104e674f4feff82984990ff85d0bf58dc8a4aa94"), + ], + &[ + fp_from_hex!("1cbaf2b371dac6a81d0453416d3e235cb8d9e2d4f314f46f6198785f0cd6b9af"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("1d5b2777692c205b0e6c49d061b6b5f4293c4ab038fdbbdc343e07610f3fede5"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("2e9bdbba3dd34bffaa30535bdd749a7e06a9adb0c1e6f962f60e971b8d73b04f"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("2de11886b18011ca8bd5bae36969299fde40fbe26d047b05035a13661f22418b"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("2e07de1780b8a70d0d5b4a3f1841dcd82ab9395c449be947bc998884ba96a721"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("0f69f1854d20ca0cbbdb63dbd52dad16250440a99d6b8af3825e4c2bb74925ca"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("2eb1b25417fe17670d135dc639fb09a46ce5113507f96de9816c059422dc705e"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("115cd0a0643cfb988c24cb44c3fab48aff36c661d26cc42db8b1bdf4953bd82c"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("26ca293f7b2c462d066d7378b999868bbb57ddf14e0f958ade801612311d04cd"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("17bf1b93c4c7e01a2a830aa162412cd90f160bf9f71e967ff5209d14b24820ca"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("35b41a7ac4f3c571a24f8456369c85dfe03c0354bd8cfd3805c86f2e7dc293c5"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("3b1480080523c439435927994849bea964e14d3beb2dddde72ac156af435d09e"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[fp_from_hex!("2cc6810031dc1b0d4950856dc907d57508e286442a2d3eb2271618d874b14c6d"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("25bdbbeda1bde8c1059618e2afd2ef999e517aa93b78341d91f318c09f0cb566"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("392a4a8758e06ee8b95f33c25dde8ac02a5ed0a27b61926cc6313487073f7f7b"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("272a55878a08442b9aa6111f4de009485e6a6fd15db89365e7bbcef02eb5866c"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("2d5b308b0cf02cdfefa13c4e60e26239a6ebba011694dd129b925b3c5b21e0e2"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("16549fc6af2f3b72dd5d293d72e2e5f244dff42f18b46c56ef38c57c311673ac"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("1b10bb7a82afce39fa69c3a2ad52f76d76398265344203119b7126d9b46860df"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("0f1e7505ebd91d2fc79c2df7dc98a3bed1b36968ba0405c090d27f6a00b7dfc8"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("2f313faf0d3f6187537a7497a3b43f46797fd6e3f18eb1caff457756b819bb20"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("3a5cbb6de450b481fa3ca61c0ed15bc55cad11ebf0f7ceb8f0bc3e732ecb26f6"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("3dab54bc9bef688dd92086e253b439d651baa6e20f892b62865527cbca915982"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("06dbfb42b979884de280d31670123f744c24b33b410fefd4368045acf2b71ae3"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("068d6b4608aae810c6f039ea1973a63eb8d2de72e3d2c9eca7fc32d22f18b9d3"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("366ebfafa3ad381c0ee258c9b8fdfccdb868a7d7e1f1f69a2b5dfcc5572555df"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("39678f65512f1ee404db3024f41d3f567ef66d89d044d022e6bc229e95bc76b1"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("21668f016a8063c0d58b7750a3bc2fe1cf82c25f99dc01a4e534c88fe53d85fe"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("39d00994a8a5046a1bc749363e98a768e34dea56439fe1954bef429bc5331608"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("1f9dbdc3f84312636b203bbe12fb3425b163d41605d39f99770c956f60d881b3"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("027745a9cddfad95e5f17b9e0ee0cab6be0bc829fe5e66c69794a9f7c336eab2"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("1cec0803c504b635788d695c61e932122fa43fe20a45c78d52025657abd8aee0"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("123523d75e9fabc172077448ef87cc6eed5082c8dbf31365d3872a9559a03a73"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("1723d1452c9cf02df419b848e5d694bf27feba35975ee7e5001779e3a1d357f4"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("1739d180a16010bdfcc0573d7e61369421c3f776f572836d9dab1ee4dcf96622"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("2d4e6354da9cc554acce32391794b627fafa96fbeb0ab89370290452042d048d"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("153ee6142e535e334a869553c9d007f88f3bd43f99260621670bcf6f8b485dcd"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("0c45bfd3a69aaa65635ef7e7a430b486968ad4424af83700d258d2e2b7782172"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("0adfd53b256a6957f2d56aec831446006897ac0a8ffa5ff10e5633d251f73307"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("315d2ac8ebdbac3c8cd1726b7cbab8ee3f87b28f1c1be4bdac9d36a8b7516d63"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("1b8472712d02eef4cfaec23d2b16883fc9bb60d1f6959879299ce44ea423d8e1"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("3c1cd07efda6ff24bd0b70fa2255eb6f367d2c54e36928c9c4a5404198adf70c"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("136052d26bb3d373687f4e51b2e1dcd34a16073f738f7e0cbbe523aef9ab107a"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("16c96beef6a0a848c1bdd859a1232a1d7b3cfbb873032681676c36c24ef967dd"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("284b38c57ff65c262ab7fed8f499a9fb012387bab4f1662d067eec7f2d6340c4"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("0c5993d175e81f6639e242198897d17cfc06772c1c0411a6af1dff204c922f86"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("03bf7a3f7bd043dafcda655d1ba9c8f9f24887ad48e17759bbf53f67b1f87b15"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("3188fe4ee9f9fafbb0cf999567f00e734c8f9cbe69f0e8279b5cd09e36d8be62"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("171f528ccf6584375a39768c480d61e13af5bf77c1c42652afea99a2ec6c595a"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("12f4175c4ab45afc196e41859b35ef88812c3286ee7000675a0563b9b8e9f1d5"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("3a509e155cb7ebfd8f8fdcf800a9ac697e23e1aabe96cfab0e74d4d369118b79"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("10f2a685df4a27c81a89920e2504c3b3984bc8f2e4c1b69e98712c65678cfd30"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("09e5f49790c8a0e21d8d93d54ab91a0e54573c9333c56321e8a16728cc9d4918"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("352d69bed80ee3e52bf35705d9f84a3442d17ed6ee0fab7e609a740347cf5fea"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("058ee73ba9f3f293491562faf2b190d3c634debd281b76a63a758af6fa84e0e8"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("232f99cc911eddd9cd0f1fc55b1a3250092cb92119bc76be621a132510a43904"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("201beed7b8f3ab8186c22c6c5d4869f0f9efd52ca6bc2961c3b97c1e301bc213"), + fp_from_hex!("1376dce6580030c6a1c9291d58602f5129388842744a1210bf6b3431ba94e9bc"), + fp_from_hex!("1793199e6fd6ba342b3356c38238f761072ba8b02d92e7226454843c5486d7b3"), + ], + &[ + fp_from_hex!("22de7a7488dcc7359fee9c20c87a67df3c66160dc62aacac06a3f1d3b433311b"), + fp_from_hex!("3514d5e9066bb160df8ff37fe2d8edf8dbe0b77fae77e1d030d6e3fd516b47a8"), + fp_from_hex!("30cd3006931ad636f919a00dabbf5fa5ff453d6f900f144a19377427137a81c7"), + ], + &[ + fp_from_hex!("253d1a5c5293412741f81a5cf613c8df8f9e4b2cae2ebb515b6a74220692b506"), + fp_from_hex!("035b461c02d79d19a35e9613e7f5fe92851b3a59c990fafc73f666cb86a48e8e"), + fp_from_hex!("23a9928079d175bd5bc00eedd56b93e092b1283c2d5fccde7cfbf86a3aa04780"), + ], + &[ + fp_from_hex!("13a7785ae134ea92f1594a0763c611abb5e2ea3436eef957f1e4ccd73fa00a82"), + fp_from_hex!("39fce308b7d43c574962ae3c0da17e313889c57863446d88bbf04f5252de4279"), + fp_from_hex!("1aae18833f8e1d3ac0fdf01662f60d22bef00a08c6ed38d23b57e34489b53fad"), + ], + ]; +} + +#[allow(unused_imports)] +#[cfg(test)] +mod tests { + use crate::{ + field::instance::FpPallas, + fp_from_hex, + poseidon2::{instance::pallas::PallasParams, *}, + }; + + type Scalar = FpPallas; + + #[test] + fn smoke() { + let mut poseidon2 = Poseidon2::::new(); + for i in 1..PallasParams::T { + poseidon2.absorb(&Scalar::from(i as u64)); + } + let perm = poseidon2.squeeze_batch(2); + assert_eq!( + perm[0], + fp_from_hex!( + "1c48ea0994a7d7984ea338a54dbf0c8681f5af883fe988d59ba3380c9f7901fc" + ) + ); + assert_eq!( + perm[1], + fp_from_hex!( + "079ddd0a80a3e9414489b526a2770448964766685f4c4842c838f8a23120b401" + ) + ); + } +} diff --git a/lib/crypto/src/poseidon2/instance/vesta.rs b/lib/crypto/src/poseidon2/instance/vesta.rs new file mode 100644 index 00000000..aac8c65e --- /dev/null +++ b/lib/crypto/src/poseidon2/instance/vesta.rs @@ -0,0 +1,375 @@ +//! This module contains the poseidon sponge hash function parameters for +//! [`FpVesta`] field instance. + +use crate::{ + field::instance::FpVesta, fp_from_hex, poseidon2::params::PoseidonParams, +}; + +/// Poseidon sponge hash function parameters for [`FpVesta`] field instance. +pub struct VestaParams; + +#[rustfmt::skip] +impl PoseidonParams for VestaParams { + const T: usize = 3; + const D: u8 = 5; + const CAPACITY: usize = 1; + const ROUNDS_F: usize = 8; + const ROUNDS_P: usize = 56; + const MAT_INTERNAL_DIAG_M_1: &'static [FpVesta] = &[ + fp_from_hex!( + "0000000000000000000000000000000000000000000000000000000000000001" + ), + fp_from_hex!( + "0000000000000000000000000000000000000000000000000000000000000001" + ), + fp_from_hex!( + "0000000000000000000000000000000000000000000000000000000000000002" + ), + ]; + const ROUND_CONSTANTS: &'static [&'static [FpVesta]] = &[ + &[ + fp_from_hex!("360d7470611e473d353f628f76d110f34e71162f31003b7057538c2596426303"), + fp_from_hex!("2bab94d7ae222d135dc3c6c5febfaa314908ac2f12ebe06fbdb74213bf63188b"), + fp_from_hex!("150c93fef652fb1c2bf03e1a29aa871fef77e7d736766c5d0939d92753cc5dc8"), + ], + &[ + fp_from_hex!("3270661e68928b3a955d55db56dc57c103cc0a60141e894e14259dce537782b2"), + fp_from_hex!("073f116f04122e25a0b7afe4e2057299b407c370f2b5a1ccce9fb9ffc345afb3"), + fp_from_hex!("2a32ec5c4ee5b1837affd09c1f53f5fd55c9cd2061ae93ca8ebad76fc71554d8"), + ], + &[ + fp_from_hex!("270326ee039df19e651e2cfc740628ca634d24fc6e2559f22d8ccbe292efeead"), + fp_from_hex!("27c6642ac633bc66dc100fe7fcfa54918af895bce012f182a068fc37c182e274"), + fp_from_hex!("1bdfd8b01401c70ad27f57396989129d710e1fb6ab976a459ca18682e26d7ff9"), + ], + &[ + fp_from_hex!("162a14c62f9a89b814b9d6a9c84dd678f4f6fb3f9054d373c832d824261a35ea"), + fp_from_hex!("2d193e0f76de586b2af6f79e3127feeaac0a1fc71e2cf0c0f79824667b5b6bec"), + fp_from_hex!("044ca3cc4a85d73b81696ef1104e674f4feff82984990ff85d0bf58dc8a4aa94"), + ], + &[ + fp_from_hex!("1cbaf2b371dac6a81d0453416d3e235cb8d9e2d4f314f46f6198785f0cd6b9af"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("1d5b2777692c205b0e6c49d061b6b5f4293c4ab038fdbbdc343e07610f3fede5"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("2e9bdbba3dd34bffaa30535bdd749a7e06a9adb0c1e6f962f60e971b8d73b04f"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("2de11886b18011ca8bd5bae36969299fde40fbe26d047b05035a13661f22418b"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("2e07de1780b8a70d0d5b4a3f1841dcd82ab9395c449be947bc998884ba96a721"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("0f69f1854d20ca0cbbdb63dbd52dad16250440a99d6b8af3825e4c2bb74925ca"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("2eb1b25417fe17670d135dc639fb09a46ce5113507f96de9816c059422dc705e"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("115cd0a0643cfb988c24cb44c3fab48aff36c661d26cc42db8b1bdf4953bd82c"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("26ca293f7b2c462d066d7378b999868bbb57ddf14e0f958ade801612311d04cd"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("17bf1b93c4c7e01a2a830aa162412cd90f160bf9f71e967ff5209d14b24820ca"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("35b41a7ac4f3c571a24f8456369c85dfe03c0354bd8cfd3805c86f2e7dc293c5"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("3b1480080523c439435927994849bea964e14d3beb2dddde72ac156af435d09e"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[fp_from_hex!("2cc6810031dc1b0d4950856dc907d57508e286442a2d3eb2271618d874b14c6d"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("25bdbbeda1bde8c1059618e2afd2ef999e517aa93b78341d91f318c09f0cb566"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("392a4a8758e06ee8b95f33c25dde8ac02a5ed0a27b61926cc6313487073f7f7b"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("272a55878a08442b9aa6111f4de009485e6a6fd15db89365e7bbcef02eb5866c"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("2d5b308b0cf02cdfefa13c4e60e26239a6ebba011694dd129b925b3c5b21e0e2"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("16549fc6af2f3b72dd5d293d72e2e5f244dff42f18b46c56ef38c57c311673ac"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("1b10bb7a82afce39fa69c3a2ad52f76d76398265344203119b7126d9b46860df"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("0f1e7505ebd91d2fc79c2df7dc98a3bed1b36968ba0405c090d27f6a00b7dfc8"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("2f313faf0d3f6187537a7497a3b43f46797fd6e3f18eb1caff457756b819bb20"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("3a5cbb6de450b481fa3ca61c0ed15bc55cad11ebf0f7ceb8f0bc3e732ecb26f6"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("3dab54bc9bef688dd92086e253b439d651baa6e20f892b62865527cbca915982"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("06dbfb42b979884de280d31670123f744c24b33b410fefd4368045acf2b71ae3"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("068d6b4608aae810c6f039ea1973a63eb8d2de72e3d2c9eca7fc32d22f18b9d3"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("366ebfafa3ad381c0ee258c9b8fdfccdb868a7d7e1f1f69a2b5dfcc5572555df"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("39678f65512f1ee404db3024f41d3f567ef66d89d044d022e6bc229e95bc76b1"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("21668f016a8063c0d58b7750a3bc2fe1cf82c25f99dc01a4e534c88fe53d85fe"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("39d00994a8a5046a1bc749363e98a768e34dea56439fe1954bef429bc5331608"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("1f9dbdc3f84312636b203bbe12fb3425b163d41605d39f99770c956f60d881b3"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("027745a9cddfad95e5f17b9e0ee0cab6be0bc829fe5e66c69794a9f7c336eab2"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("1cec0803c504b635788d695c61e932122fa43fe20a45c78d52025657abd8aee0"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("123523d75e9fabc172077448ef87cc6eed5082c8dbf31365d3872a9559a03a73"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("1723d1452c9cf02df419b848e5d694bf27feba35975ee7e5001779e3a1d357f4"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("1739d180a16010bdfcc0573d7e61369421c3f776f572836d9dab1ee4dcf96622"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("2d4e6354da9cc554acce32391794b627fafa96fbeb0ab89370290452042d048d"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("153ee6142e535e334a869553c9d007f88f3bd43f99260621670bcf6f8b485dcd"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("0c45bfd3a69aaa65635ef7e7a430b486968ad4424af83700d258d2e2b7782172"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("0adfd53b256a6957f2d56aec831446006897ac0a8ffa5ff10e5633d251f73307"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("315d2ac8ebdbac3c8cd1726b7cbab8ee3f87b28f1c1be4bdac9d36a8b7516d63"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("1b8472712d02eef4cfaec23d2b16883fc9bb60d1f6959879299ce44ea423d8e1"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("3c1cd07efda6ff24bd0b70fa2255eb6f367d2c54e36928c9c4a5404198adf70c"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("136052d26bb3d373687f4e51b2e1dcd34a16073f738f7e0cbbe523aef9ab107a"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("16c96beef6a0a848c1bdd859a1232a1d7b3cfbb873032681676c36c24ef967dd"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("284b38c57ff65c262ab7fed8f499a9fb012387bab4f1662d067eec7f2d6340c4"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("0c5993d175e81f6639e242198897d17cfc06772c1c0411a6af1dff204c922f86"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("03bf7a3f7bd043dafcda655d1ba9c8f9f24887ad48e17759bbf53f67b1f87b15"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("3188fe4ee9f9fafbb0cf999567f00e734c8f9cbe69f0e8279b5cd09e36d8be62"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("171f528ccf6584375a39768c480d61e13af5bf77c1c42652afea99a2ec6c595a"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("12f4175c4ab45afc196e41859b35ef88812c3286ee7000675a0563b9b8e9f1d5"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("3a509e155cb7ebfd8f8fdcf800a9ac697e23e1aabe96cfab0e74d4d369118b79"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("10f2a685df4a27c81a89920e2504c3b3984bc8f2e4c1b69e98712c65678cfd30"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("09e5f49790c8a0e21d8d93d54ab91a0e54573c9333c56321e8a16728cc9d4918"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("352d69bed80ee3e52bf35705d9f84a3442d17ed6ee0fab7e609a740347cf5fea"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("058ee73ba9f3f293491562faf2b190d3c634debd281b76a63a758af6fa84e0e8"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("232f99cc911eddd9cd0f1fc55b1a3250092cb92119bc76be621a132510a43904"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + fp_from_hex!("0000000000000000000000000000000000000000000000000000000000000000"), + ], + &[ + fp_from_hex!("201beed7b8f3ab8186c22c6c5d4869f0f9efd52ca6bc2961c3b97c1e301bc213"), + fp_from_hex!("1376dce6580030c6a1c9291d58602f5129388842744a1210bf6b3431ba94e9bc"), + fp_from_hex!("1793199e6fd6ba342b3356c38238f761072ba8b02d92e7226454843c5486d7b3"), + ], + &[ + fp_from_hex!("22de7a7488dcc7359fee9c20c87a67df3c66160dc62aacac06a3f1d3b433311b"), + fp_from_hex!("3514d5e9066bb160df8ff37fe2d8edf8dbe0b77fae77e1d030d6e3fd516b47a8"), + fp_from_hex!("30cd3006931ad636f919a00dabbf5fa5ff453d6f900f144a19377427137a81c7"), + ], + &[ + fp_from_hex!("253d1a5c5293412741f81a5cf613c8df8f9e4b2cae2ebb515b6a74220692b506"), + fp_from_hex!("035b461c02d79d19a35e9613e7f5fe92851b3a59c990fafc73f666cb86a48e8e"), + fp_from_hex!("23a9928079d175bd5bc00eedd56b93e092b1283c2d5fccde7cfbf86a3aa04780"), + ], + &[ + fp_from_hex!("13a7785ae134ea92f1594a0763c611abb5e2ea3436eef957f1e4ccd73fa00a82"), + fp_from_hex!("39fce308b7d43c574962ae3c0da17e313889c57863446d88bbf04f5252de4279"), + fp_from_hex!("1aae18833f8e1d3ac0fdf01662f60d22bef00a08c6ed38d23b57e34489b53fad"), + ], + ]; +} + +#[allow(unused_imports)] +#[cfg(test)] +mod tests { + use crate::{ + field::instance::FpVesta, + fp_from_hex, + poseidon2::{ + instance::vesta::VestaParams, params::PoseidonParams, Poseidon2, + }, + }; + + type Scalar = FpVesta; + + #[test] + fn smoke() { + let mut poseidon2 = Poseidon2::::new(); + for i in 1..VestaParams::T { + poseidon2.absorb(&Scalar::from(i as u64)); + } + let perm = poseidon2.squeeze_batch(2); + assert_eq!(perm[0], fp_from_hex!("2c76327e0b7653873263158cf8545c282364b183880fcdea93ca8526d518c66f")); + assert_eq!(perm[1], fp_from_hex!("262316c0ce5244838c75873299b59d763ae0849d2dd31bdc95caf7db1c2901bf")); + } +} diff --git a/lib/crypto/src/poseidon2/mod.rs b/lib/crypto/src/poseidon2/mod.rs new file mode 100644 index 00000000..6933f929 --- /dev/null +++ b/lib/crypto/src/poseidon2/mod.rs @@ -0,0 +1,300 @@ +//! This module contains the Poseidon hash ([whitepaper]) function implemented +//! as a [Sponge Function]. +//! +//! Poseidon permutation here follows referenced in [whitepaper] original [rust +//! implementation] with slight improvements. +//! +//! [Sponge function]: https://en.wikipedia.org/wiki/Sponge_function +//! [whitepaper]: https://eprint.iacr.org/2023/323.pdf +//! [rust implementation]: https://github.com/HorizenLabs/poseidon2 + +pub mod instance; +pub mod params; + +use alloc::{boxed::Box, vec, vec::Vec}; + +use crate::{field::prime::PrimeField, poseidon2::params::PoseidonParams}; + +/// Determines whether poseidon sponge in absorbing or squeezing state. +/// In squeezing state, sponge can only squeeze elements. +#[derive(Clone, Copy, Debug, PartialEq)] +enum Mode { + Absorbing, + Squeezing, +} + +/// Poseidon2 sponge that can absorb any number of [`F`] field elements and be +/// squeezed to a finite number of [`F`] field elements. +#[derive(Clone, Debug)] +pub struct Poseidon2, F: PrimeField> { + phantom: core::marker::PhantomData

, + state: Box<[F]>, + mode: Mode, + index: usize, +} + +impl, F: PrimeField> Default for Poseidon2 { + fn default() -> Self { + Self::new() + } +} + +impl, F: PrimeField> Poseidon2 { + /// Create a new Poseidon sponge. + #[must_use] + pub fn new() -> Self { + Self { + phantom: core::marker::PhantomData, + state: vec![F::zero(); P::T].into_boxed_slice(), + mode: Mode::Absorbing, + // Begin index from `CAPACITY`. Skip capacity elements. + index: P::CAPACITY, + } + } + + /// Size of poseidon sponge's state. + #[must_use] + pub const fn state_size() -> usize { + P::T + } + + /// Start index of partial rounds. + #[must_use] + const fn partial_round_start() -> usize { + P::ROUNDS_F / 2 + } + + /// End index of partial rounds (noninclusive). + #[must_use] + const fn partial_round_end() -> usize { + Self::partial_round_start() + P::ROUNDS_P + } + + /// Total number of rounds. + #[must_use] + const fn rounds() -> usize { + P::ROUNDS_F + P::ROUNDS_P + } + + /// Absorb a single element into the sponge. + /// + /// # Panics + /// + /// May panic if absorbing while squeezing. + pub fn absorb(&mut self, elem: &F) { + if let Mode::Squeezing = self.mode { + panic!("cannot absorb while squeezing"); + } + + if self.index == Self::state_size() { + self.permute(); + self.index = P::CAPACITY; + } + + self.state[self.index] += elem; + self.index += 1; + } + + /// Absorb batch of elements into the sponge. + pub fn absorb_batch(&mut self, elems: &[F]) { + for elem in elems { + self.absorb(elem); + } + } + + /// Permute elements in the sponge. + pub fn permute(&mut self) { + // Linear layer at the beginning. + self.matmul_external(); + + // Run the first half of the full round. + for round in 0..Self::partial_round_start() { + self.external_round(round); + } + + // Run the partial round. + for round in Self::partial_round_start()..Self::partial_round_end() { + self.internal_round(round); + } + + // Run the second half of the full round. + for round in Self::partial_round_end()..Self::rounds() { + self.external_round(round); + } + } + + /// Apply external round to the state. + fn external_round(&mut self, round: usize) { + self.add_rc_external(round); + self.apply_sbox_external(); + self.matmul_external(); + } + + /// Apply internal round to the state. + fn internal_round(&mut self, round: usize) { + self.add_rc_internal(round); + self.apply_sbox_internal(); + self.matmul_internal(); + } + + /// Squeeze a single element from the sponge. + pub fn squeeze(&mut self) -> F { + if self.mode == Mode::Absorbing || self.index == Self::state_size() { + self.permute(); + self.mode = Mode::Squeezing; + self.index = P::CAPACITY; + } + + let elem = self.state[self.index]; + self.index += 1; + elem + } + + /// Squeeze a batch of elements from the sponge. + pub fn squeeze_batch(&mut self, n: usize) -> Vec { + (0..n).map(|_| self.squeeze()).collect() + } + + /// Apply sbox to the entire state in the external round. + fn apply_sbox_external(&mut self) { + for elem in &mut self.state { + *elem = elem.pow(P::D); + } + } + + /// Apply sbox to the first element in the internal round. + fn apply_sbox_internal(&mut self) { + self.state[0] = self.state[0].pow(P::D); + } + + /// Apply the external MDS matrix `M_E` to the state. + #[allow(clippy::needless_range_loop)] + fn matmul_external(&mut self) { + let t = Self::state_size(); + match t { + 2 => { + // Matrix circ(2, 1) + let sum = self.state[0] + self.state[1]; + self.state[0] += sum; + self.state[1] += sum; + } + 3 => { + // Matrix circ(2, 1, 1). + let sum = self.state[0] + self.state[1] + self.state[2]; + self.state[0] += sum; + self.state[1] += sum; + self.state[2] += sum; + } + 4 => { + self.matmul_m4(); + } + 8 | 12 | 16 | 20 | 24 => { + self.matmul_m4(); + + // Applying second cheap matrix for t > 4. + let t4 = t / 4; + let mut stored = [F::zero(); 4]; + for l in 0..4 { + stored[l] = self.state[l]; + for j in 1..t4 { + stored[l] += &self.state[4 * j + l]; + } + } + for i in 0..self.state.len() { + self.state[i] += &stored[i % 4]; + } + } + _ => { + panic!("not supported state size") + } + } + } + + /// Apply the cheap 4x4 MDS matrix to each 4-element part of the state. + fn matmul_m4(&mut self) { + let state = &mut self.state; + let t = Self::state_size(); + let t4 = t / 4; + for i in 0..t4 { + let start_index = i * 4; + let mut t_0 = state[start_index]; + t_0 += &state[start_index + 1]; + let mut t_1 = state[start_index + 2]; + t_1 += &state[start_index + 3]; + let mut t_2 = state[start_index + 1]; + t_2.double_in_place(); + t_2 += &t_1; + let mut t_3 = state[start_index + 3]; + t_3.double_in_place(); + t_3 += &t_0; + let mut t_4 = t_1; + t_4.double_in_place(); + t_4.double_in_place(); + t_4 += &t_3; + let mut t_5 = t_0; + t_5.double_in_place(); + t_5.double_in_place(); + t_5 += &t_2; + let mut t_6 = t_3; + t_6 += &t_5; + let mut t_7 = t_2; + t_7 += &t_4; + state[start_index] = t_6; + state[start_index + 1] = t_5; + state[start_index + 2] = t_7; + state[start_index + 3] = t_4; + } + } + + /// Apply the internal MDS matrix `M_I` to the state. + fn matmul_internal(&mut self) { + let t = Self::state_size(); + + match t { + 2 => { + // [2, 1] + // [1, 3] + let sum = self.state[0] + self.state[1]; + self.state[0] += ∑ + self.state[1].double_in_place(); + self.state[1] += ∑ + } + 3 => { + // [2, 1, 1] + // [1, 2, 1] + // [1, 1, 3] + let sum = self.state[0] + self.state[1] + self.state[2]; + self.state[0] += ∑ + self.state[1] += ∑ + self.state[2].double_in_place(); + self.state[2] += ∑ + } + 4 | 8 | 12 | 16 | 20 | 24 => { + let sum = self.state.iter().sum(); + + // Add sum + diag entry * element to each element. + for i in 0..self.state.len() { + self.state[i] *= &P::MAT_INTERNAL_DIAG_M_1[i]; + self.state[i] += ∑ + } + } + _ => { + panic!("not supported state size") + } + } + } + + /// Add a round constant to the entire state in external round. + fn add_rc_external(&mut self, round: usize) { + for (a, b) in + self.state.iter_mut().zip(P::ROUND_CONSTANTS[round].iter()) + { + *a += b; + } + } + + // Add a round constant to the first state element in internal round. + fn add_rc_internal(&mut self, round: usize) { + self.state[0] += P::ROUND_CONSTANTS[round][0]; + } +} diff --git a/lib/crypto/src/poseidon2/params.rs b/lib/crypto/src/poseidon2/params.rs new file mode 100644 index 00000000..b9d03028 --- /dev/null +++ b/lib/crypto/src/poseidon2/params.rs @@ -0,0 +1,35 @@ +//! This module contains a trait with poseidon hash parameters. +//! +//! Consumer of this trait should implement the parameters for the specific +//! poseidon hash instance. +//! Or use the existing instances in the [`crate::poseidon2::instance`] module. + +use crate::field::prime::PrimeField; + +/// Poseidon hash parameters. +pub trait PoseidonParams { + /// State size. + const T: usize; + + /// Sbox degree. + const D: u8; + + /// Capacity of the sponge construction. + /// Determines the number of elements not affected directly by input + /// or not reflected in the output of the sponge hash function. + const CAPACITY: usize; + + /// Number of full rounds. + const ROUNDS_F: usize; + + /// Number of partial rounds. + const ROUNDS_P: usize; + + /// MDS (Maximum Distance Separable) matrix used in the Poseidon + /// permutation. + const MAT_INTERNAL_DIAG_M_1: &'static [F]; + + /// The round constants used in the full and partial rounds of the Poseidon + /// permutation. + const ROUND_CONSTANTS: &'static [&'static [F]]; +}