Skip to content

Commit fc9b920

Browse files
refactor: replace Pedersen implementation with type-rs (#614)
1 parent ee149a4 commit fc9b920

File tree

2 files changed

+6
-41
lines changed

2 files changed

+6
-41
lines changed

starknet-crypto/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ rfc6979 = { version = "0.4.0", default-features = false }
2525
sha2 = { version = "0.10.6", default-features = false }
2626
zeroize = { version = "1.6.0", default-features = false }
2727
hex = { version = "0.4.3", default-features = false, optional = true }
28-
starknet-types-core = { version = "0.1.3", default-features = false, features = ["curve"] }
28+
starknet-types-core = { version = "0.1.3", default-features = false, features = ["curve", "hash"] }
2929

3030
[features]
3131
default = ["std", "signature-display"]

starknet-crypto/src/pedersen_hash.rs

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
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+
};
65

76
/// Computes the Starkware version of the Pedersen hash of x and y. All inputs are little-endian.
87
///
@@ -11,41 +10,7 @@ use crate::pedersen_points::*;
1110
/// * `x`: The x coordinate
1211
/// * `y`: The y coordinate
1312
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)
4914
}
5015

5116
#[cfg(test)]

0 commit comments

Comments
 (0)