Skip to content

Make Digest trait generic over output size #406

Description

@Farukest

Problem

The current Digest trait has a hardcoded 32-byte output constraint:

fn as_bytes(&self) -> [u8; 32];

This makes it impossible to properly implement hashers with different output sizes. For example, SHA-512 produces a 64 byte digest, but the trait forces truncation to 32 bytes.

Using truncated SHA-512 (first 32 bytes of SHA-512 output) is not the same as SHA-512/256, which uses different initialization vectors as specified in FIPS 180-4. This creates a confusing situation where implementers must choose between:

  1. Implementing Digest with incorrect/truncated output
  2. Not implementing Digest at all for 64-byte hashers

Proposed Solution

Add a const generic parameter to the Digest trait:

pub trait Digest<const N: usize = 32>: Debug + Default + Copy + Clone + Eq + PartialEq + Send + Sync + Serializable + Deserializable {
    fn as_bytes(&self) -> [u8; N];
}

This would allow:

  • Digest<32> as default for existing 32 byte hashers (backward compatible)
  • Digest<64> for SHA-512 and other 64 byte hashers
  • Future flexibility for other digest sizes

Context

This issue was raised while implementing SHA-512 support in 0xMiden/crypto#692.

cc @huitseeker

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions