libcrux_traits/digest/
owned.rs

1//! This module contains the traits and related errors for hashers that take array references as
2//! arguments and return values as arrays.
3//!
4
5use super::arrayref;
6
7pub use arrayref::HashError;
8
9/// A trait for oneshot hashing, where the output is returned as an array.
10pub trait Hash<const OUTPUT_LEN: usize> {
11    /// Returns the digest for the given input byte slice, as an array, in immediate mode.
12    fn hash(payload: &[u8]) -> Result<[u8; OUTPUT_LEN], HashError>;
13}
14
15/// A trait for incremental hashing, where the output is returned as an array.
16pub trait DigestIncremental<const OUTPUT_LEN: usize>: super::DigestIncrementalBase {
17    /// Returns the digest as an array.
18    ///
19    /// Note that the digest state can be continued to be used, to extend the digest.
20    fn finish(state: &mut Self::IncrementalState) -> [u8; OUTPUT_LEN];
21}
22
23impl<const OUTPUT_LEN: usize, D: arrayref::Hash<OUTPUT_LEN>> Hash<OUTPUT_LEN> for D {
24    fn hash(payload: &[u8]) -> Result<[u8; OUTPUT_LEN], HashError> {
25        let mut digest = [0; OUTPUT_LEN];
26        Self::hash(&mut digest, payload).map(|_| digest)
27    }
28}
29impl<const OUTPUT_LEN: usize, D: arrayref::DigestIncremental<OUTPUT_LEN>>
30    DigestIncremental<OUTPUT_LEN> for D
31{
32    fn finish(state: &mut Self::IncrementalState) -> [u8; OUTPUT_LEN] {
33        let mut digest = [0; OUTPUT_LEN];
34        Self::finish(state, &mut digest);
35        digest
36    }
37}