Skip to content

Commit

Permalink
implement Pedersen struct
Browse files Browse the repository at this point in the history
  • Loading branch information
pefontana committed Nov 1, 2023
1 parent 75049ee commit 529dd0d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 20 deletions.
20 changes: 6 additions & 14 deletions crates/starknet-types-core/src/hash/pedersen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,20 @@ use super::traits::StarkHash;

pub struct Pedersen;

impl Pedersen {
pub fn new() -> Pedersen {
Pedersen
}
}

impl StarkHash for Pedersen {
/// Computes Pedersen hash using STARK curve on two elements, as defined
/// Computes the Pedersen hash of two Felts, as defined
/// in <https://docs.starknet.io/documentation/architecture_and_concepts/Hashing/hash-functions/#pedersen_hash.>
fn hash(&self, felt_0: &Felt, felt_1: &Felt) -> Felt {
fn hash(felt_0: &Felt, felt_1: &Felt) -> Felt {
let pedersen = PedersenLambdaworks::default();

let hash = pedersen.hash(&felt_0.0, &felt_1.0);

Felt(hash)
}

/// Computes Pedersen hash using STARK curve on an array of elements, as defined
/// Computes the Pedersen hash of an array of Felts, as defined
/// in <https://docs.starknet.io/documentation/architecture_and_concepts/Hashing/hash-functions/#array_hashing.>
fn hash_array(&self, felts: &[Felt]) -> Felt {
fn hash_array(felts: &[Felt]) -> Felt {
let pedersen = PedersenLambdaworks::default();
let data_len =
Felt::from_u128(u128::try_from(felts.len()).expect("Got 2^128 felts or more."))
Expand All @@ -47,7 +41,6 @@ mod tests {

#[test]
fn test_pedersen_hash() {
let pedersen = Pedersen::new();
let x =
Felt::from_hex("0x03d937c035c878245caf64531a5756109c53068da139362728feb561405371cb")
.unwrap();
Expand All @@ -56,21 +49,20 @@ mod tests {
.unwrap();

assert_eq!(
pedersen.hash(&x, &y),
Pedersen::hash(&x, &y),
Felt::from_hex("0x030e480bed5fe53fa909cc0f8c4d99b8f9f2c016be4c41e13a4848797979c662")
.unwrap()
);
}

#[test]
fn test_pedersen_hash_array() {
let pedersen = Pedersen::new();
let a = Felt::from_hex("0xaa").unwrap();
let b = Felt::from_hex("0xbb").unwrap();
let c = Felt::from_hex("0xcc").unwrap();
let expected =
Felt::from_hex("0x10808e8929644950878c4f71326e47c6b584d9cfea2de0415daf8def0f5e89f")
.unwrap();
assert_eq!(pedersen.hash_array(&[a, b, c]), expected);
assert_eq!(Pedersen::hash_array(&[a, b, c]), expected);
}
}
11 changes: 5 additions & 6 deletions crates/starknet-types-core/src/hash/traits.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use crate::felt::Felt;

pub trait StarkHash {
/// Computes Pedersen hash using STARK curve on two elements, as defined
/// in <https://docs.starknet.io/documentation/architecture_and_concepts/Hashing/hash-functions/#pedersen_hash.>
fn hash(&self, felt_0: &Felt, felt_1: &Felt) -> Felt;
/// Computes the hash of two Felt
fn hash(felt_0: &Felt, felt_1: &Felt) -> Felt;

/// Computes Pedersen hash using STARK curve on an array of elements, as defined
/// in <https://docs.starknet.io/documentation/architecture_and_concepts/Hashing/hash-functions/#array_hashing.>
fn hash_array(&self, felts: &[Felt]) -> Felt;
/// Computes the hash of an array of Felts,
/// as defined in <https://docs.starknet.io/documentation/architecture_and_concepts/Hashing/hash-functions/#array_hashing.>
fn hash_array(felts: &[Felt]) -> Felt;
}

0 comments on commit 529dd0d

Please sign in to comment.