@@ -17,6 +17,9 @@ impl StarkHash for Pedersen {
17
17
18
18
/// Computes the Pedersen hash of an array of Felts, as defined
19
19
/// in <https://docs.starknet.io/documentation/architecture_and_concepts/Hashing/hash-functions/#array_hashing.>
20
+ ///
21
+ /// Warning: there is room for collision as:
22
+ /// Pedersen::hash_array([value]) and Pedersen::hash(Pedersen::hash(0, value), 1) will return the same values
20
23
fn hash_array ( felts : & [ Felt ] ) -> Felt {
21
24
let data_len = Felt :: from ( felts. len ( ) ) ;
22
25
let current_hash: FieldElement < Stark252PrimeField > = felts
@@ -26,12 +29,43 @@ impl StarkHash for Pedersen {
26
29
} ) ;
27
30
Felt ( PedersenStarkCurve :: hash ( & current_hash, & data_len. 0 ) )
28
31
}
32
+
33
+ /// Computes the Pedersen hash of a single Felt
34
+ ///
35
+ /// Warning: there is room for collision as:
36
+ /// Pedersen::hash_single(value) and Pedersen::hash(value, 0) will return the same values
37
+ fn hash_single ( felt : & Felt ) -> Felt {
38
+ Felt ( PedersenStarkCurve :: hash ( & felt. 0 , & Felt :: from ( 0 ) . 0 ) )
39
+ }
29
40
}
30
41
31
42
#[ cfg( test) ]
32
43
mod tests {
33
44
use super :: * ;
34
45
46
+ #[ test]
47
+ fn test_pedersen_hash_single ( ) {
48
+ let x =
49
+ Felt :: from_hex ( "0x03d937c035c878245caf64531a5756109c53068da139362728feb561405371cb" )
50
+ . unwrap ( ) ;
51
+ assert_eq ! (
52
+ Pedersen :: hash_single( & x) ,
53
+ Felt :: from_hex( "0x460ded9dacd215bcfc43f1b30b2a02690378e00f82a2283617d47d948c7b7f1" )
54
+ . unwrap( )
55
+ )
56
+ }
57
+
58
+ #[ test]
59
+ fn test_pedersen_hash_collision ( ) {
60
+ let x =
61
+ Felt :: from_hex ( "0x03d937c035c878245caf64531a5756109c53068da139362728feb561405371cb" )
62
+ . unwrap ( ) ;
63
+ assert_eq ! (
64
+ Pedersen :: hash_single( & x) ,
65
+ Pedersen :: hash( & x, & Felt :: from( 0 ) )
66
+ )
67
+ }
68
+
35
69
#[ test]
36
70
fn test_pedersen_hash ( ) {
37
71
let x =
0 commit comments