Skip to content

Commit

Permalink
refactor: replace Pedersen implementation with type-rs (#614)
Browse files Browse the repository at this point in the history
  • Loading branch information
thetheveloper authored Jul 14, 2024
1 parent ee149a4 commit fc9b920
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 41 deletions.
2 changes: 1 addition & 1 deletion starknet-crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ rfc6979 = { version = "0.4.0", default-features = false }
sha2 = { version = "0.10.6", default-features = false }
zeroize = { version = "1.6.0", default-features = false }
hex = { version = "0.4.3", default-features = false, optional = true }
starknet-types-core = { version = "0.1.3", default-features = false, features = ["curve"] }
starknet-types-core = { version = "0.1.3", default-features = false, features = ["curve", "hash"] }

[features]
default = ["std", "signature-display"]
Expand Down
45 changes: 5 additions & 40 deletions starknet-crypto/src/pedersen_hash.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use starknet_curve::curve_params;
use starknet_types_core::curve::{AffinePoint, ProjectivePoint};
use starknet_types_core::felt::Felt;

use crate::pedersen_points::*;
use starknet_types_core::{
felt::Felt,
hash::{Pedersen, StarkHash},
};

/// Computes the Starkware version of the Pedersen hash of x and y. All inputs are little-endian.
///
Expand All @@ -11,41 +10,7 @@ use crate::pedersen_points::*;
/// * `x`: The x coordinate
/// * `y`: The y coordinate
pub fn pedersen_hash(x: &Felt, y: &Felt) -> Felt {
let x = x.to_bits_le();
let y = y.to_bits_le();

// Preprocessed material is lookup-tables for each chunk of bits
let table_size = (1 << CURVE_CONSTS_BITS) - 1;
let add_points = |acc: &mut ProjectivePoint, bits: &[bool], prep: &[AffinePoint]| {
bits.chunks(CURVE_CONSTS_BITS)
.enumerate()
.for_each(|(i, v)| {
let offset = v
.iter()
.rev()
.fold(0, |acc, &bit| (acc << 1) + bit as usize);
if offset > 0 {
// Table lookup at 'offset-1' in table for chunk 'i'
*acc += &prep[i * table_size + offset - 1];
}
});
};

// Compute hash
let mut acc =
ProjectivePoint::from_affine(curve_params::SHIFT_POINT.x(), curve_params::SHIFT_POINT.y())
.unwrap();

add_points(&mut acc, &x[..248], &CURVE_CONSTS_P0); // Add a_low * P1
add_points(&mut acc, &x[248..252], &CURVE_CONSTS_P1); // Add a_high * P2
add_points(&mut acc, &y[..248], &CURVE_CONSTS_P2); // Add b_low * P3
add_points(&mut acc, &y[248..252], &CURVE_CONSTS_P3); // Add b_high * P4

// Convert to affine
let result = acc.to_affine().unwrap();

// Return x-coordinate
result.x()
Pedersen::hash(x, y)
}

#[cfg(test)]
Expand Down

0 comments on commit fc9b920

Please sign in to comment.