Skip to main content

Hasher

Trait Hasher 

Source
pub trait Hasher:
    Default
    + Send
    + Sync
    + 'static {
    type Digest: Digest;

    // Required methods
    fn hash(parts: &[&[u8]]) -> Self::Digest;
    fn hash_pair(
        left: &[&[u8]],
        right: &[&[u8]],
        strategy: &impl Strategy,
    ) -> (Self::Digest, Self::Digest);
    fn update(&mut self, bytes: &[u8]) -> &mut Self;
    fn finalize(self) -> (Self, Self::Digest);

    // Provided methods
    fn hash_across(parts: &[&[u8]], _strategy: &impl Strategy) -> Self::Digest { ... }
    fn hash_many<M: AsRef<[u8]> + Sync>(
        messages: &[M],
        strategy: &impl Strategy,
    ) -> Vec<Self::Digest> { ... }
}
Expand description

Interface that commonware crates rely on for hashing.

Hash functions in commonware primitives are not typically hardcoded to a specific algorithm (e.g. SHA-256) because different hash functions may work better with different cryptographic schemes, may be more efficient to use in STARK/SNARK proofs, or provide different levels of security (with some performance/size penalty).

Hashers are cheap to construct: callers that need a fresh hasher should create one with Default rather than duplicating an existing instance.

Required Associated Types§

Source

type Digest: Digest

Digest generated by the hasher.

Required Methods§

Source

fn hash(parts: &[&[u8]]) -> Self::Digest

Hash the concatenation of parts in a single shot.

This is the preferred entrypoint for hashing data that is fully available up-front. Implementations are free to specialize this for small, fixed-shape inputs (e.g. hashing a pair of digests) to avoid the overhead of the streaming machinery.

Source

fn hash_pair( left: &[&[u8]], right: &[&[u8]], strategy: &impl Strategy, ) -> (Self::Digest, Self::Digest)

Hash two messages, each given as a concatenation of parts, in a single shot, splitting the work across strategy when the hash function allows it.

Must be equivalent to hashing each message with Hasher::hash.

Source

fn update(&mut self, bytes: &[u8]) -> &mut Self

Append bytes to the hasher’s running state.

Source

fn finalize(self) -> (Self, Self::Digest)

Consume the hasher, returning a freshly-reset hasher alongside the digest of everything written so far.

Provided Methods§

Source

fn hash_across(parts: &[&[u8]], _strategy: &impl Strategy) -> Self::Digest

Hash the concatenation of parts in a single shot, splitting the work across strategy when the hash function allows it.

Returns the same digest as Hasher::hash, which the default implementation calls.

Source

fn hash_many<M: AsRef<[u8]> + Sync>( messages: &[M], strategy: &impl Strategy, ) -> Vec<Self::Digest>

Hash multiple independent byte slices, splitting them across strategy.

Returns one digest per input in the same order. Inputs may be empty, differ in length, or overlap. Output position i is equivalent to Self::hash(&[messages[i].as_ref()]).

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§