Skip to main content

opaque_vx/
hash.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2// Copyright (c) VexaHub and contributors.
3// Copyright (c) Meta Platforms, Inc. and affiliates.
4
5//! A convenience trait for digest bounds used throughout the library
6
7use digest::block_api::{
8    BlockSizeUser, BufferKindUser, CoreProxy, FixedOutputCore, SmallBlockSizeUser,
9};
10use digest::block_buffer::Eager;
11use digest::{Digest, FixedOutputReset, HashMarker, OutputSizeUser};
12use generic_array::typenum::{IsLess, Le, NonZero, U256};
13
14pub(crate) type OutputSize<H> = <<H as CoreProxy>::Core as OutputSizeUser>::OutputSize;
15
16/// Trait to simplify requirements for [`Hash`].
17pub trait ProxyHash:
18    HashMarker + FixedOutputCore + BufferKindUser<BufferKind = Eager> + OutputSizeUser + Default + Clone
19where
20    <Self as SmallBlockSizeUser>::_BlockSize: IsLess<U256>,
21    Le<<Self as SmallBlockSizeUser>::_BlockSize, U256>: NonZero,
22{
23}
24
25impl<
26    T: HashMarker
27        + FixedOutputCore
28        + BufferKindUser<BufferKind = Eager>
29        + OutputSizeUser
30        + Default
31        + Clone,
32> ProxyHash for T
33where
34    <T as SmallBlockSizeUser>::_BlockSize: IsLess<U256>,
35    Le<<T as SmallBlockSizeUser>::_BlockSize, U256>: NonZero,
36{
37}
38
39/// Trait inheriting the requirements from [`Digest`] for compatibility
40/// with HKDF and HMAC Associated types could be simplified when they are made
41/// as defaults: <https://github.com/rust-lang/rust/issues/29661>
42pub trait Hash:
43    Default
44    + HashMarker
45    + Digest
46    + OutputSizeUser<OutputSize = OutputSize<Self>>
47    + BlockSizeUser
48    + FixedOutputReset
49    + CoreProxy
50    + Clone
51    + zeroize::ZeroizeOnDrop
52where
53    <Self as CoreProxy>::Core: ProxyHash,
54    <<Self as CoreProxy>::Core as SmallBlockSizeUser>::_BlockSize: IsLess<U256>,
55    Le<<<Self as CoreProxy>::Core as SmallBlockSizeUser>::_BlockSize, U256>: NonZero,
56    OutputSize<Self>: generic_array::ArrayLength,
57{
58}
59
60impl<
61    T: Default
62        + HashMarker
63        + Digest
64        + OutputSizeUser<OutputSize = OutputSize<Self>>
65        + BlockSizeUser
66        + FixedOutputReset
67        + CoreProxy
68        + Clone
69        + zeroize::ZeroizeOnDrop,
70> Hash for T
71where
72    <T as CoreProxy>::Core: ProxyHash,
73    <<T as CoreProxy>::Core as SmallBlockSizeUser>::_BlockSize: IsLess<U256>,
74    Le<<<T as CoreProxy>::Core as SmallBlockSizeUser>::_BlockSize, U256>: NonZero,
75    OutputSize<T>: generic_array::ArrayLength,
76{
77}