From 997aabbdbc42e01a4fc57ec14b68355754950ef4 Mon Sep 17 00:00:00 2001 From: varun-doshi Date: Fri, 10 Jan 2025 19:40:18 +0530 Subject: [PATCH] feat : impl pedersen hash_single --- .../starknet-types-core/src/hash/pedersen.rs | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/crates/starknet-types-core/src/hash/pedersen.rs b/crates/starknet-types-core/src/hash/pedersen.rs index 2175fac..2524a4f 100644 --- a/crates/starknet-types-core/src/hash/pedersen.rs +++ b/crates/starknet-types-core/src/hash/pedersen.rs @@ -17,6 +17,9 @@ impl StarkHash for Pedersen { /// Computes the Pedersen hash of an array of Felts, as defined /// in + /// + /// Warning: there is room for collision as: + /// pedersen_array([value]) and pedersen(pedersen(0, value), 1) will return the same values fn hash_array(felts: &[Felt]) -> Felt { let data_len = Felt::from(felts.len()); let current_hash: FieldElement = felts @@ -26,9 +29,13 @@ impl StarkHash for Pedersen { }); Felt(PedersenStarkCurve::hash(¤t_hash, &data_len.0)) } - + + /// Computes the Pedersen hash of a single Felt + /// + /// Warning: there is room for collision as: + /// pedersen(value) and pedersen(value, 0) will return the same values fn hash_single(felt: &Felt) -> Felt { - todo!() + Felt(PedersenStarkCurve::hash(&felt.0, &Felt::from(0).0)) } } @@ -36,6 +43,18 @@ impl StarkHash for Pedersen { mod tests { use super::*; + #[test] + fn test_pedersen_hash_single() { + let x = + Felt::from_hex("0x03d937c035c878245caf64531a5756109c53068da139362728feb561405371cb") + .unwrap(); + assert_eq!( + Pedersen::hash_single(&x), + Felt::from_hex("0x460ded9dacd215bcfc43f1b30b2a02690378e00f82a2283617d47d948c7b7f1") + .unwrap() + ) + } + #[test] fn test_pedersen_hash() { let x =