From fc9b9209e714bcd4b5f439ec32bdc720112f684d Mon Sep 17 00:00:00 2001 From: TheVeloper Date: Mon, 15 Jul 2024 00:04:23 +0200 Subject: [PATCH] refactor: replace Pedersen implementation with type-rs (#614) --- starknet-crypto/Cargo.toml | 2 +- starknet-crypto/src/pedersen_hash.rs | 45 ++++------------------------ 2 files changed, 6 insertions(+), 41 deletions(-) diff --git a/starknet-crypto/Cargo.toml b/starknet-crypto/Cargo.toml index 2c73c00f..07312853 100644 --- a/starknet-crypto/Cargo.toml +++ b/starknet-crypto/Cargo.toml @@ -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"] diff --git a/starknet-crypto/src/pedersen_hash.rs b/starknet-crypto/src/pedersen_hash.rs index a5026750..a59c5345 100644 --- a/starknet-crypto/src/pedersen_hash.rs +++ b/starknet-crypto/src/pedersen_hash.rs @@ -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. /// @@ -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)]