2
2
3
3
use super :: { Domain , ExpandMsg , Expander } ;
4
4
use crate :: { Error , Result } ;
5
- use core:: {
6
- fmt,
7
- marker:: PhantomData ,
8
- num:: NonZero ,
9
- ops:: { Div , Mul } ,
10
- } ;
5
+ use core:: { fmt, marker:: PhantomData , num:: NonZero , ops:: Mul } ;
11
6
use digest:: { ExtendableOutput , HashMarker , Update , XofReader } ;
12
7
use hybrid_array:: {
13
8
ArraySize ,
14
- typenum:: { IsLess , U2 , U8 , U256 } ,
9
+ typenum:: { IsLess , U2 , U256 } ,
15
10
} ;
16
11
17
12
/// Implements `expand_message_xof` via the [`ExpandMsg`] trait:
18
13
/// <https://www.rfc-editor.org/rfc/rfc9380.html#name-expand_message_xof>
19
14
///
20
- /// `K` is the target security level in bits :
15
+ /// `K` is the target security level in bytes :
21
16
/// <https://www.rfc-editor.org/rfc/rfc9380.html#section-8.9-2.2>
22
17
/// <https://www.rfc-editor.org/rfc/rfc9380.html#name-target-security-levels>
23
18
///
@@ -27,9 +22,8 @@ use hybrid_array::{
27
22
pub struct ExpandMsgXof < HashT , K >
28
23
where
29
24
HashT : Default + ExtendableOutput + Update + HashMarker ,
30
- U2 : Mul < K > ,
31
- <U2 as Mul < K > >:: Output : Div < U8 > ,
32
- HashSize < K > : ArraySize + IsLess < U256 > ,
25
+ K : Mul < U2 > ,
26
+ <K as Mul < U2 > >:: Output : ArraySize + IsLess < U256 > ,
33
27
{
34
28
reader : <HashT as ExtendableOutput >:: Reader ,
35
29
_k : PhantomData < K > ,
38
32
impl < HashT , K > fmt:: Debug for ExpandMsgXof < HashT , K >
39
33
where
40
34
HashT : Default + ExtendableOutput + Update + HashMarker ,
41
- U2 : Mul < K > ,
42
- <U2 as Mul < K > >:: Output : Div < U8 > ,
43
- HashSize < K > : ArraySize + IsLess < U256 > ,
35
+ K : Mul < U2 > ,
36
+ <K as Mul < U2 > >:: Output : ArraySize + IsLess < U256 > ,
44
37
<HashT as ExtendableOutput >:: Reader : fmt:: Debug ,
45
38
{
46
39
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
@@ -50,17 +43,13 @@ where
50
43
}
51
44
}
52
45
53
- type HashSize < K > = <<U2 as Mul < K > >:: Output as Div < U8 > >:: Output ;
54
-
55
46
impl < ' a , HashT , K > ExpandMsg < ' a > for ExpandMsgXof < HashT , K >
56
47
where
57
48
HashT : Default + ExtendableOutput + Update + HashMarker ,
58
- // If DST is larger than 255 bytes, the length of the computed DST is calculated by
59
- // `2 * k / 8`.
49
+ // If DST is larger than 255 bytes, the length of the computed DST is calculated by `K * 2`.
60
50
// https://www.rfc-editor.org/rfc/rfc9380.html#section-5.3.1-2.1
61
- U2 : Mul < K > ,
62
- <U2 as Mul < K > >:: Output : Div < U8 > ,
63
- HashSize < K > : ArraySize + IsLess < U256 > ,
51
+ K : Mul < U2 > ,
52
+ <K as Mul < U2 > >:: Output : ArraySize + IsLess < U256 > ,
64
53
{
65
54
type Expander = Self ;
66
55
71
60
) -> Result < Self :: Expander > {
72
61
let len_in_bytes = u16:: try_from ( len_in_bytes. get ( ) ) . map_err ( |_| Error ) ?;
73
62
74
- let domain = Domain :: < HashSize < K > > :: xof :: < HashT > ( dsts) ?;
63
+ let domain = Domain :: < < K as Mul < U2 > > :: Output > :: xof :: < HashT > ( dsts) ?;
75
64
let mut reader = HashT :: default ( ) ;
76
65
77
66
for msg in msgs {
92
81
impl < HashT , K > Expander for ExpandMsgXof < HashT , K >
93
82
where
94
83
HashT : Default + ExtendableOutput + Update + HashMarker ,
95
- U2 : Mul < K > ,
96
- <U2 as Mul < K > >:: Output : Div < U8 > ,
97
- HashSize < K > : ArraySize + IsLess < U256 > ,
84
+ K : Mul < U2 > ,
85
+ <K as Mul < U2 > >:: Output : ArraySize + IsLess < U256 > ,
98
86
{
99
87
fn fill_bytes ( & mut self , okm : & mut [ u8 ] ) {
100
88
self . reader . read ( okm) ;
@@ -108,7 +96,7 @@ mod test {
108
96
use hex_literal:: hex;
109
97
use hybrid_array:: {
110
98
Array , ArraySize ,
111
- typenum:: { U32 , U128 } ,
99
+ typenum:: { U16 , U32 , U128 } ,
112
100
} ;
113
101
use sha3:: Shake128 ;
114
102
@@ -146,7 +134,7 @@ mod test {
146
134
{
147
135
assert_message ( self . msg , domain, L :: to_u16 ( ) , self . msg_prime ) ;
148
136
149
- let mut expander = ExpandMsgXof :: < HashT , U128 > :: expand_message (
137
+ let mut expander = ExpandMsgXof :: < HashT , U16 > :: expand_message (
150
138
& [ self . msg ] ,
151
139
& [ dst] ,
152
140
NonZero :: new ( L :: to_usize ( ) ) . ok_or ( Error ) ?,
0 commit comments