diff --git a/crates/starknet-types-core/src/hash/poseidon.rs b/crates/starknet-types-core/src/hash/poseidon.rs index 92b5d1c..ec6fc68 100644 --- a/crates/starknet-types-core/src/hash/poseidon.rs +++ b/crates/starknet-types-core/src/hash/poseidon.rs @@ -30,6 +30,18 @@ impl StarkHash for Poseidon { } } +impl Poseidon { + /// Computes the Hades permutation over a mutable state of 3 Felts, as defined + /// in + pub fn hades_permutation(state: &mut [Felt; 3]) { + let mut state_inner = [state[0].0, state[1].0, state[2].0]; + PoseidonCairoStark252::hades_permutation(&mut state_inner); + for i in 0..3 { + state[i] = Felt(state_inner[i]); + } + } +} + #[cfg(test)] mod tests { use super::*; @@ -60,4 +72,24 @@ mod tests { .unwrap(); assert_eq!(Poseidon::hash_array(&[a, b, c]), expected); } + + #[test] + fn test_hades_permutation() { + let mut state = [ + Felt::from_hex("0x9").unwrap(), + Felt::from_hex("0xb").unwrap(), + Felt::from_hex("0x2").unwrap(), + ]; + let expected = [ + Felt::from_hex("0x510f3a3faf4084e3b1e95fd44c30746271b48723f7ea9c8be6a9b6b5408e7e6") + .unwrap(), + Felt::from_hex("0x4f511749bd4101266904288021211333fb0a514cb15381af087462fa46e6bd9") + .unwrap(), + Felt::from_hex("0x186f6dd1a6e79cb1b66d505574c349272cd35c07c223351a0990410798bb9d8") + .unwrap(), + ]; + Poseidon::hades_permutation(&mut state); + + assert_eq!(state, expected); + } }