Skip to main content

libcrux_blake2/impl_hacl/
lengths.rs

1use super::{Blake2b, Blake2s, LengthBounds};
2
3/// A marker trait indicating whether the hash supports keys of length LEN.
4/// This trait is sealed to protect the soundness of the compile time bounds checks.
5#[allow(private_bounds)]
6pub trait SupportsKeyLen<const LEN: usize>: SupportsKeyLenInternal<LEN> {}
7
8/// A marker trait indicating whether the hash supports outputs of length LEN.
9/// This trait is sealed to protect the soundness of the compile time bounds checks.
10#[allow(private_bounds)]
11pub trait SupportsOutLen<const LEN: usize>: SupportsOutLenInternal<LEN> {}
12
13/// A marker trait indicating whether the hash supports outputs of length LEN.
14/// This is internal to seal the actual trait.
15pub(super) trait SupportsOutLenInternal<const LEN: usize> {}
16
17/// A marker trait indicating whether the hash supports keys of length LEN.
18/// This is internal to seal the actual trait.
19pub(super) trait SupportsKeyLenInternal<const LEN: usize> {}
20
21// blanket impl the public part of the sealed trait
22#[allow(private_bounds)]
23impl<T, const LEN: usize> SupportsKeyLen<LEN> for T where T: SupportsKeyLenInternal<LEN> {}
24
25// blanket impl the public part of the sealed trait
26#[allow(private_bounds)]
27impl<T, const LEN: usize> SupportsOutLen<LEN> for T where T: SupportsOutLenInternal<LEN> {}
28
29/// this helps us implement SupportsLen for more than one number
30macro_rules! support_lens {
31    ($ty:ty, $trait:ident, $($supported:expr),*) => {
32        $( impl $trait<$supported> for $ty {})*
33    };
34}
35
36// implement the private parts of the sealed trait
37
38#[rustfmt::skip]
39support_lens!(
40    Blake2b<LengthBounds>, SupportsOutLenInternal,
41     1,  2,  3,  4,  5,  6,  7,  8,
42     9, 10, 11, 12, 13, 14, 15, 16,
43    17, 18, 19, 20, 21, 22, 23, 24,
44    25, 26, 27, 28, 29, 30, 31, 32,
45    33, 34, 35, 36, 37, 38, 39, 40,
46    41, 42, 43, 44, 45, 46, 47, 48,
47    49, 50, 51, 52, 53, 54, 55, 56,
48    57, 58, 59, 60, 61, 62, 63, 64
49);
50
51#[rustfmt::skip]
52support_lens!(
53    Blake2s<LengthBounds>, SupportsOutLenInternal,
54     1,  2,  3,  4,  5,  6,  7,  8,
55     9, 10, 11, 12, 13, 14, 15, 16,
56    17, 18, 19, 20, 21, 22, 23, 24,
57    25, 26, 27, 28, 29, 30, 31, 32
58);
59
60#[rustfmt::skip]
61support_lens!(
62    Blake2b<LengthBounds>, SupportsKeyLenInternal,
63     0,
64     1,  2,  3,  4,  5,  6,  7,  8,
65     9, 10, 11, 12, 13, 14, 15, 16,
66    17, 18, 19, 20, 21, 22, 23, 24,
67    25, 26, 27, 28, 29, 30, 31, 32,
68    33, 34, 35, 36, 37, 38, 39, 40,
69    41, 42, 43, 44, 45, 46, 47, 48,
70    49, 50, 51, 52, 53, 54, 55, 56,
71    57, 58, 59, 60, 61, 62, 63, 64
72);
73
74#[rustfmt::skip]
75support_lens!(
76    Blake2s<LengthBounds>, SupportsKeyLenInternal,
77     0,
78     1,  2,  3,  4,  5,  6,  7,  8,
79     9, 10, 11, 12, 13, 14, 15, 16,
80    17, 18, 19, 20, 21, 22, 23, 24,
81    25, 26, 27, 28, 29, 30, 31, 32
82);