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]],
    ) -> (Self::Digest, Self::Digest);
    fn update(&mut self, bytes: &[u8]) -> &mut Self;
    fn finalize(self) -> (Self, 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]]) -> (Self::Digest, Self::Digest)

Hash two messages, each given as a concatenation of parts, in a single shot.

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.

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementors§