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:
- Implementing
Digest with incorrect/truncated output
- 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
Problem
The current
Digesttrait has a hardcoded 32-byte output constraint:This makes it impossible to properly implement hashers with different output sizes. For example,
SHA-512produces a 64 byte digest, but the trait forces truncation to 32 bytes.Using truncated
SHA-512(first 32 bytes ofSHA-512output) is not the same asSHA-512/256, which uses different initialization vectors as specified in FIPS 180-4. This creates a confusing situation where implementers must choose between:Digestwith incorrect/truncated outputDigestat all for 64-byte hashersProposed Solution
Add a const generic parameter to the
Digesttrait:This would allow:
Digest<32>as default for existing 32 byte hashers (backward compatible)Digest<64>forSHA-512and other 64 byte hashersContext
This issue was raised while implementing
SHA-512support in 0xMiden/crypto#692.cc @huitseeker