libcrux_traits/digest/
arrayref.rs

1//! This module contains the traits and related errors for hashers that take array references as
2//! arguments and write the outputs to mutable array references.
3//!
4
5#[derive(Debug, PartialEq)]
6/// Error indicating that hashing failed.
7pub enum HashError {
8    /// The length of the provided payload is invalid.
9    InvalidPayloadLength,
10    /// Indicates an unknown error.
11    Unknown,
12}
13
14impl core::fmt::Display for HashError {
15    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
16        let text = match self {
17            HashError::InvalidPayloadLength => "the length of the provided payload is invalid",
18            HashError::Unknown => "indicates an unknown error",
19        };
20
21        f.write_str(text)
22    }
23}
24
25#[cfg(feature = "error-in-core")]
26mod error_in_core {
27
28    impl core::error::Error for super::HashError {}
29}
30
31/// A trait for incremental hashing, where the output is written into a provided buffer.
32pub trait DigestIncremental<const OUTPUT_LEN: usize>: super::DigestIncrementalBase {
33    /// Writes the digest into `digest`.
34    ///
35    /// Note that the digest state can be continued to be used, to extend the digest.
36    fn finish(state: &mut Self::IncrementalState, digest: &mut [u8; OUTPUT_LEN]);
37}
38
39/// A trait for oneshot hashing, where the output is written into a provided buffer.
40pub trait Hash<const OUTPUT_LEN: usize> {
41    /// Writes the digest for the given input byte slice, into `digest` in immediate mode.
42    fn hash(digest: &mut [u8; OUTPUT_LEN], payload: &[u8]) -> Result<(), HashError>;
43}