Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve poseidon API #109

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions crates/starknet-types-core/src/hash/pedersen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ impl StarkHash for Pedersen {

/// 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.>
///
/// Warning: there is room for collision as:
/// Pedersen::hash_array([value]) and Pedersen::hash(Pedersen::hash(0, value), 1) will return the same values
fn hash_array(felts: &[Felt]) -> Felt {
let data_len = Felt::from(felts.len());
let current_hash: FieldElement<Stark252PrimeField> = felts
Expand All @@ -26,12 +29,32 @@ impl StarkHash for Pedersen {
});
Felt(PedersenStarkCurve::hash(&current_hash, &data_len.0))
}

/// Computes the Pedersen hash of a single Felt
///
/// Warning: there is room for collision as:
/// Pedersen::hash_single(value) and Pedersen::hash(value, 0) will return the same values
jbcaron marked this conversation as resolved.
Show resolved Hide resolved
fn hash_single(felt: &Felt) -> Felt {
Felt(PedersenStarkCurve::hash(&felt.0, &Felt::from(0).0))
}
}

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

#[test]
fn test_pedersen_hash_single() {
let x =
Felt::from_hex("0x03d937c035c878245caf64531a5756109c53068da139362728feb561405371cb")
.unwrap();
assert_eq!(
Pedersen::hash_single(&x),
Felt::from_hex("0x460ded9dacd215bcfc43f1b30b2a02690378e00f82a2283617d47d948c7b7f1")
.unwrap()
)
}

#[test]
fn test_pedersen_hash() {
let x =
Expand Down
15 changes: 15 additions & 0 deletions crates/starknet-types-core/src/hash/poseidon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ impl StarkHash for Poseidon {
)
}))
}

fn hash_single(felt: &Felt) -> Felt {
Felt(PoseidonCairoStark252::hash_single(&felt.0))
}
}

impl Poseidon {
Expand All @@ -46,6 +50,17 @@ impl Poseidon {
mod tests {
use super::*;

#[test]
fn test_poseidon_single() {
let x = Felt::from_hex("0x9").unwrap();

assert_eq!(
Poseidon::hash_single(&x),
Felt::from_hex("0x3bb3b91c714cb47003947f36dadc98326176963c434cd0a10320b8146c948b3")
.unwrap()
);
}

#[test]
fn test_poseidon_hash() {
let x =
Expand Down
2 changes: 2 additions & 0 deletions crates/starknet-types-core/src/hash/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ pub trait StarkHash {
/// 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;

fn hash_single(felt: &Felt) -> Felt;
}
Loading