-
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
3 changed files
with
62 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
mod pedersen; | ||
mod poseidon; | ||
mod traits; | ||
|
||
pub use self::pedersen::*; | ||
pub use self::poseidon::*; | ||
pub use self::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,59 @@ | ||
use crate::felt::Felt; | ||
use lambdaworks_crypto::hash::poseidon::{ | ||
starknet::PoseidonCairoStark252, Poseidon as PoseidonLambdaworks, | ||
}; | ||
use lambdaworks_math::field::{ | ||
element::FieldElement, fields::fft_friendly::stark_252_prime_field::Stark252PrimeField, | ||
}; | ||
|
||
use super::traits::StarkHash; | ||
|
||
pub struct Poseidon; | ||
|
||
impl StarkHash for Poseidon { | ||
/// Computes the Poseidon hash of two Felts, as defined | ||
/// in <https://docs.starknet.io/documentation/architecture_and_concepts/Hashing/hash-functions/#pedersen_hash.> | ||
fn hash(felt_0: &Felt, felt_1: &Felt) -> Felt { | ||
Felt(PoseidonCairoStark252::hash(&felt_0.0, &felt_1.0)) | ||
} | ||
/// Computes the Poseidon 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 { | ||
let res = PoseidonCairoStark252::hash_many(unsafe { | ||
core::mem::transmute::<&[Felt], &[FieldElement<Stark252PrimeField>]>(felts) | ||
}); | ||
Felt(res) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_poseidon_hash() { | ||
let x = | ||
Felt::from_hex("0x03d937c035c878245caf64531a5756109c53068da139362728feb561405371cb") | ||
.unwrap(); | ||
let y = | ||
Felt::from_hex("0x0208a0a10250e382e1e4bbe2880906c2791bf6275695e02fbbc6aeff9cd8b31a") | ||
.unwrap(); | ||
|
||
assert_eq!( | ||
Poseidon::hash(&x, &y), | ||
Felt::from_hex("0x67c6a2e2d0c7867f97444ae17956dbc89d40ad22255bb06f5f6c515958926ed") | ||
.unwrap() | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_pedersen_hash_array() { | ||
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("0x2742e049f7e1613e4a014efeec0d742882a798ae0af8b8dd730358c23848775") | ||
.unwrap(); | ||
assert_eq!(Poseidon::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