Skip to content

Commit

Permalink
Add trait StarkHash
Browse files Browse the repository at this point in the history
  • Loading branch information
pefontana committed Nov 1, 2023
1 parent 236dfa1 commit 75049ee
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 57 deletions.
2 changes: 1 addition & 1 deletion crates/starknet-types-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ arbitrary = { version = "1.3.0", optional = true, default-features = false }
num-traits = { version = "0.2.16", default-features = false }

[features]
default = ["std", "serde", "curve"]
default = ["std", "serde", "curve", "hash"]
curve = []
hash = ["dep:lambdaworks-crypto"]
std = []
Expand Down
56 changes: 0 additions & 56 deletions crates/starknet-types-core/src/hash.rs

This file was deleted.

2 changes: 2 additions & 0 deletions crates/starknet-types-core/src/hash/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod pedersen;
pub mod traits;
76 changes: 76 additions & 0 deletions crates/starknet-types-core/src/hash/pedersen.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use crate::felt::Felt;
use lambdaworks_crypto::hash::pedersen::Pedersen as PedersenLambdaworks;
use lambdaworks_math::field::{
element::FieldElement, fields::fft_friendly::stark_252_prime_field::Stark252PrimeField,
};
use num_traits::FromPrimitive;

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
/// in <https://docs.starknet.io/documentation/architecture_and_concepts/Hashing/hash-functions/#pedersen_hash.>
fn hash(&self, 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
/// in <https://docs.starknet.io/documentation/architecture_and_concepts/Hashing/hash-functions/#array_hashing.>
fn hash_array(&self, 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."))
.unwrap();
let current_hash: FieldElement<Stark252PrimeField> = felts.iter().fold(
FieldElement::<Stark252PrimeField>::zero(),
|current_hash, felt| pedersen.hash(&current_hash, &felt.0),
);
Felt(pedersen.hash(&current_hash, &data_len.0))
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_pedersen_hash() {
let pedersen = Pedersen::new();
let x =
Felt::from_hex("0x03d937c035c878245caf64531a5756109c53068da139362728feb561405371cb")
.unwrap();
let y =
Felt::from_hex("0x0208a0a10250e382e1e4bbe2880906c2791bf6275695e02fbbc6aeff9cd8b31a")
.unwrap();

assert_eq!(
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);
}
}
11 changes: 11 additions & 0 deletions crates/starknet-types-core/src/hash/traits.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
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 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;
}

0 comments on commit 75049ee

Please sign in to comment.