Skip to content

Commit

Permalink
feat: pedersen hasher type (#684)
Browse files Browse the repository at this point in the history
  • Loading branch information
xJonathanLEI authored Jan 1, 2025
1 parent 8755b68 commit 21e0573
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion starknet-crypto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ mod test_utils;

pub use starknet_types_core::felt::Felt;

pub use pedersen_hash::pedersen_hash;
pub use pedersen_hash::{pedersen_hash, PedersenHasher};

pub use poseidon_hash::{
poseidon_hash, poseidon_hash_many, poseidon_hash_single, poseidon_permute_comp, PoseidonHasher,
Expand Down
27 changes: 27 additions & 0 deletions starknet-crypto/src/pedersen_hash/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use starknet_types_core::felt::Felt;

#[cfg(not(feature = "pedersen_no_lookup"))]
mod default;
#[cfg(not(feature = "pedersen_no_lookup"))]
Expand All @@ -8,6 +10,31 @@ mod no_lookup;
#[cfg(feature = "pedersen_no_lookup")]
pub use no_lookup::pedersen_hash;

/// A stateful hasher for Starknet Pedersen hash.
#[derive(Debug, Default)]
pub struct PedersenHasher {
hash: Felt,
len: usize,
}

impl PedersenHasher {
/// Creates a new [`PedersenHasher`].
pub fn new() -> Self {
Self::default()
}

/// Absorbs message into the hash.
pub fn update(&mut self, msg: Felt) {
self.hash = pedersen_hash(&self.hash, &msg);
self.len += 1;
}

/// Finishes and returns hash.
pub fn finalize(&self) -> Felt {
pedersen_hash(&self.hash, &self.len.into())
}
}

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

0 comments on commit 21e0573

Please sign in to comment.