diff --git a/crates/starknet-types-core/src/hash/mod.rs b/crates/starknet-types-core/src/hash/mod.rs index ed33d07..b79e592 100644 --- a/crates/starknet-types-core/src/hash/mod.rs +++ b/crates/starknet-types-core/src/hash/mod.rs @@ -1,5 +1,7 @@ mod pedersen; +mod poseidon; mod traits; pub use self::pedersen::*; +pub use self::poseidon::*; pub use self::traits::*; diff --git a/crates/starknet-types-core/src/hash/poseidon.rs b/crates/starknet-types-core/src/hash/poseidon.rs new file mode 100644 index 0000000..f25afab --- /dev/null +++ b/crates/starknet-types-core/src/hash/poseidon.rs @@ -0,0 +1,58 @@ +use crate::felt::Felt; +use lambdaworks_crypto::hash::poseidon::{ + starknet::PoseidonCairoStark252, Poseidon as PoseidonLambdaworks, +}; +use lambdaworks_math::field::{ + element::FieldElement, fields::fft_friendly::stark_252_prime_field::Stark252PrimeField, +}; + +use super::traits::StarkHash; + +pub struct Poseidon; + +impl StarkHash for Poseidon { + /// Computes the Poseidon hash of two Felts, as defined + /// in + fn hash(felt_0: &Felt, felt_1: &Felt) -> Felt { + Felt(PoseidonCairoStark252::hash(&felt_0.0, &felt_1.0)) + } + /// Computes the Poseidon hash of an array of Felts, as defined + /// in + fn hash_array(felts: &[Felt]) -> Felt { + Felt(PoseidonCairoStark252::hash_many(unsafe { + core::mem::transmute::<&[Felt], &[FieldElement]>(felts) + })) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_poseidon_hash() { + let x = + Felt::from_hex("0x03d937c035c878245caf64531a5756109c53068da139362728feb561405371cb") + .unwrap(); + let y = + Felt::from_hex("0x0208a0a10250e382e1e4bbe2880906c2791bf6275695e02fbbc6aeff9cd8b31a") + .unwrap(); + + assert_eq!( + Poseidon::hash(&x, &y), + Felt::from_hex("0x67c6a2e2d0c7867f97444ae17956dbc89d40ad22255bb06f5f6c515958926ed") + .unwrap() + ); + } + + #[test] + fn test_pedersen_hash_array() { + let a = Felt::from_hex("0xaa").unwrap(); + let b = Felt::from_hex("0xbb").unwrap(); + let c = Felt::from_hex("0xcc").unwrap(); + let expected = + Felt::from_hex("0x2742e049f7e1613e4a014efeec0d742882a798ae0af8b8dd730358c23848775") + .unwrap(); + assert_eq!(Poseidon::hash_array(&[a, b, c]), expected); + } +} diff --git a/crates/starknet-types-rpc/Cargo.toml b/crates/starknet-types-rpc/Cargo.toml index 38ae114..b3a66e6 100644 --- a/crates/starknet-types-rpc/Cargo.toml +++ b/crates/starknet-types-rpc/Cargo.toml @@ -20,4 +20,4 @@ starknet-types-core = { path = "../starknet-types-core", default-features = fals serde = { version = "1", default-features = false, features = ["derive"] } [dev-dependencies] -serde_json = "1" \ No newline at end of file +serde_json = "1"