2026-07-01 20:48:44 +02:00
|
|
|
// SPDX-License-Identifier: MIT OR Apache-2.0
|
|
|
|
|
// Copyright (c) VexaHub and contributors.
|
2023-05-22 23:04:26 -07:00
|
|
|
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
2020-07-27 15:25:04 -07:00
|
|
|
|
2020-10-21 18:20:43 -04:00
|
|
|
//! A convenience trait for digest bounds used throughout the library
|
|
|
|
|
|
2026-07-01 11:52:12 +02:00
|
|
|
use digest::block_api::{
|
|
|
|
|
BlockSizeUser, BufferKindUser, CoreProxy, FixedOutputCore, SmallBlockSizeUser,
|
|
|
|
|
};
|
2022-01-06 00:10:57 +01:00
|
|
|
use digest::block_buffer::Eager;
|
2026-07-01 11:52:12 +02:00
|
|
|
use digest::{Digest, FixedOutputReset, HashMarker, OutputSizeUser};
|
2022-01-06 00:10:57 +01:00
|
|
|
use generic_array::typenum::{IsLess, Le, NonZero, U256};
|
|
|
|
|
|
2025-05-19 22:56:25 +02:00
|
|
|
pub(crate) type OutputSize<H> = <<H as CoreProxy>::Core as OutputSizeUser>::OutputSize;
|
2022-01-06 00:10:57 +01:00
|
|
|
|
|
|
|
|
/// Trait to simplify requirements for [`Hash`].
|
|
|
|
|
pub trait ProxyHash:
|
2026-07-01 11:52:12 +02:00
|
|
|
HashMarker + FixedOutputCore + BufferKindUser<BufferKind = Eager> + OutputSizeUser + Default + Clone
|
2022-01-06 00:10:57 +01:00
|
|
|
where
|
2026-07-01 11:52:12 +02:00
|
|
|
<Self as SmallBlockSizeUser>::_BlockSize: IsLess<U256>,
|
|
|
|
|
Le<<Self as SmallBlockSizeUser>::_BlockSize, U256>: NonZero,
|
2022-01-06 00:10:57 +01:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-01 11:52:12 +02:00
|
|
|
impl<
|
|
|
|
|
T: HashMarker
|
|
|
|
|
+ FixedOutputCore
|
|
|
|
|
+ BufferKindUser<BufferKind = Eager>
|
|
|
|
|
+ OutputSizeUser
|
|
|
|
|
+ Default
|
|
|
|
|
+ Clone,
|
|
|
|
|
> ProxyHash for T
|
2022-01-06 00:10:57 +01:00
|
|
|
where
|
2026-07-01 11:52:12 +02:00
|
|
|
<T as SmallBlockSizeUser>::_BlockSize: IsLess<U256>,
|
|
|
|
|
Le<<T as SmallBlockSizeUser>::_BlockSize, U256>: NonZero,
|
2022-01-06 00:10:57 +01:00
|
|
|
{
|
|
|
|
|
}
|
2020-07-27 15:25:04 -07:00
|
|
|
|
2026-07-01 11:52:12 +02:00
|
|
|
/// Trait inheriting the requirements from [`Digest`] for compatibility
|
2023-10-08 21:48:43 +02:00
|
|
|
/// with HKDF and HMAC Associated types could be simplified when they are made
|
|
|
|
|
/// as defaults: <https://github.com/rust-lang/rust/issues/29661>
|
2022-01-06 00:10:57 +01:00
|
|
|
pub trait Hash:
|
2023-03-05 05:31:07 +01:00
|
|
|
Default
|
|
|
|
|
+ HashMarker
|
2026-07-01 11:52:12 +02:00
|
|
|
+ Digest
|
2022-01-06 00:10:57 +01:00
|
|
|
+ OutputSizeUser<OutputSize = OutputSize<Self>>
|
|
|
|
|
+ BlockSizeUser
|
|
|
|
|
+ FixedOutputReset
|
|
|
|
|
+ CoreProxy
|
|
|
|
|
+ Clone
|
|
|
|
|
where
|
|
|
|
|
<Self as CoreProxy>::Core: ProxyHash,
|
2026-07-01 11:52:12 +02:00
|
|
|
<<Self as CoreProxy>::Core as SmallBlockSizeUser>::_BlockSize: IsLess<U256>,
|
|
|
|
|
Le<<<Self as CoreProxy>::Core as SmallBlockSizeUser>::_BlockSize, U256>: NonZero,
|
|
|
|
|
OutputSize<Self>: generic_array::ArrayLength,
|
2022-01-06 00:10:57 +01:00
|
|
|
{
|
|
|
|
|
}
|
2020-07-27 15:25:04 -07:00
|
|
|
|
2022-01-06 00:10:57 +01:00
|
|
|
impl<
|
2025-07-10 21:55:41 +02:00
|
|
|
T: Default
|
|
|
|
|
+ HashMarker
|
2026-07-01 11:52:12 +02:00
|
|
|
+ Digest
|
2025-07-10 21:55:41 +02:00
|
|
|
+ OutputSizeUser<OutputSize = OutputSize<Self>>
|
|
|
|
|
+ BlockSizeUser
|
|
|
|
|
+ FixedOutputReset
|
|
|
|
|
+ CoreProxy
|
|
|
|
|
+ Clone,
|
|
|
|
|
> Hash for T
|
2022-01-06 00:10:57 +01:00
|
|
|
where
|
2026-07-01 11:52:12 +02:00
|
|
|
<T as CoreProxy>::Core: ProxyHash,
|
|
|
|
|
<<T as CoreProxy>::Core as SmallBlockSizeUser>::_BlockSize: IsLess<U256>,
|
|
|
|
|
Le<<<T as CoreProxy>::Core as SmallBlockSizeUser>::_BlockSize, U256>: NonZero,
|
|
|
|
|
OutputSize<T>: generic_array::ArrayLength,
|
2022-01-06 00:10:57 +01:00
|
|
|
{
|
|
|
|
|
}
|