1
- use starknet_curve:: curve_params;
2
- use starknet_types_core:: curve:: { AffinePoint , ProjectivePoint } ;
3
- use starknet_types_core:: felt:: Felt ;
4
-
5
- use crate :: pedersen_points:: * ;
1
+ use starknet_types_core:: {
2
+ felt:: Felt ,
3
+ hash:: { Pedersen , StarkHash } ,
4
+ } ;
6
5
7
6
/// Computes the Starkware version of the Pedersen hash of x and y. All inputs are little-endian.
8
7
///
@@ -11,41 +10,7 @@ use crate::pedersen_points::*;
11
10
/// * `x`: The x coordinate
12
11
/// * `y`: The y coordinate
13
12
pub fn pedersen_hash ( x : & Felt , y : & Felt ) -> Felt {
14
- let x = x. to_bits_le ( ) ;
15
- let y = y. to_bits_le ( ) ;
16
-
17
- // Preprocessed material is lookup-tables for each chunk of bits
18
- let table_size = ( 1 << CURVE_CONSTS_BITS ) - 1 ;
19
- let add_points = |acc : & mut ProjectivePoint , bits : & [ bool ] , prep : & [ AffinePoint ] | {
20
- bits. chunks ( CURVE_CONSTS_BITS )
21
- . enumerate ( )
22
- . for_each ( |( i, v) | {
23
- let offset = v
24
- . iter ( )
25
- . rev ( )
26
- . fold ( 0 , |acc, & bit| ( acc << 1 ) + bit as usize ) ;
27
- if offset > 0 {
28
- // Table lookup at 'offset-1' in table for chunk 'i'
29
- * acc += & prep[ i * table_size + offset - 1 ] ;
30
- }
31
- } ) ;
32
- } ;
33
-
34
- // Compute hash
35
- let mut acc =
36
- ProjectivePoint :: from_affine ( curve_params:: SHIFT_POINT . x ( ) , curve_params:: SHIFT_POINT . y ( ) )
37
- . unwrap ( ) ;
38
-
39
- add_points ( & mut acc, & x[ ..248 ] , & CURVE_CONSTS_P0 ) ; // Add a_low * P1
40
- add_points ( & mut acc, & x[ 248 ..252 ] , & CURVE_CONSTS_P1 ) ; // Add a_high * P2
41
- add_points ( & mut acc, & y[ ..248 ] , & CURVE_CONSTS_P2 ) ; // Add b_low * P3
42
- add_points ( & mut acc, & y[ 248 ..252 ] , & CURVE_CONSTS_P3 ) ; // Add b_high * P4
43
-
44
- // Convert to affine
45
- let result = acc. to_affine ( ) . unwrap ( ) ;
46
-
47
- // Return x-coordinate
48
- result. x ( )
13
+ Pedersen :: hash ( x, y)
49
14
}
50
15
51
16
#[ cfg( test) ]
0 commit comments