-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
90 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod pedersen; | ||
pub mod traits; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(¤t_hash, &felt.0), | ||
); | ||
Felt(pedersen.hash(¤t_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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |