Skip to content

Commit 1f2fd8b

Browse files
committed
Change K to represent bytes instead of bits
1 parent 76a991a commit 1f2fd8b

File tree

3 files changed

+26
-41
lines changed

3 files changed

+26
-41
lines changed

elliptic-curve/src/hash2curve/group_digest.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ where
1313
/// The field element representation for a group value with multiple elements
1414
type FieldElement: FromOkm + MapToCurve<Output = ProjectivePoint<Self>> + Default + Copy;
1515

16-
/// The target security level in bits:
16+
/// The target security level in bytes:
1717
/// <https://www.rfc-editor.org/rfc/rfc9380.html#section-8.9-2.2>
1818
/// <https://www.rfc-editor.org/rfc/rfc9380.html#name-target-security-levels>
1919
type K: Unsigned;

elliptic-curve/src/hash2curve/hash2field/expand_msg/xmd.rs

+10-13
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ use digest::{
88
FixedOutput, HashMarker,
99
array::{
1010
Array,
11-
typenum::{IsGreaterOrEqual, IsLess, IsLessOrEqual, U2, U8, U256, Unsigned},
11+
typenum::{IsGreaterOrEqual, IsLess, IsLessOrEqual, U2, U256, Unsigned},
1212
},
1313
core_api::BlockSizeUser,
1414
};
1515

1616
/// Implements `expand_message_xof` via the [`ExpandMsg`] trait:
1717
/// <https://www.rfc-editor.org/rfc/rfc9380.html#name-expand_message_xmd>
1818
///
19-
/// `K` is the target security level in bits:
19+
/// `K` is the target security level in bytes:
2020
/// <https://www.rfc-editor.org/rfc/rfc9380.html#section-8.9-2.2>
2121
/// <https://www.rfc-editor.org/rfc/rfc9380.html#name-target-security-levels>
2222
///
@@ -30,9 +30,8 @@ where
3030
HashT: BlockSizeUser + Default + FixedOutput + HashMarker,
3131
HashT::OutputSize: IsLess<U256>,
3232
HashT::OutputSize: IsLessOrEqual<HashT::BlockSize>,
33-
HashT::OutputSize: Mul<U8>,
34-
U2: Mul<K>,
35-
<HashT::OutputSize as Mul<U8>>::Output: IsGreaterOrEqual<<U2 as Mul<K>>::Output>;
33+
K: Mul<U2>,
34+
HashT::OutputSize: IsGreaterOrEqual<<K as Mul<U2>>::Output>;
3635

3736
impl<'a, HashT, K> ExpandMsg<'a> for ExpandMsgXmd<HashT, K>
3837
where
@@ -44,11 +43,10 @@ where
4443
// Constraint set by `expand_message_xmd`:
4544
// https://www.ietf.org/archive/id/draft-irtf-cfrg-hash-to-curve-13.html#section-5.4.1-4
4645
HashT::OutputSize: IsLessOrEqual<HashT::BlockSize>,
47-
// The number of bits output by `HashT` MUST be larger or equal to `2 * K`:
46+
// The number of bits output by `HashT` MUST be larger or equal to `K * 2`:
4847
// https://www.rfc-editor.org/rfc/rfc9380.html#section-5.3.1-2.1
49-
HashT::OutputSize: Mul<U8>,
50-
U2: Mul<K>,
51-
<HashT::OutputSize as Mul<U8>>::Output: IsGreaterOrEqual<<U2 as Mul<K>>::Output>,
48+
K: Mul<U2>,
49+
HashT::OutputSize: IsGreaterOrEqual<<K as Mul<U2>>::Output>,
5250
{
5351
type Expander = ExpanderXmd<'a, HashT>;
5452

@@ -169,7 +167,7 @@ mod test {
169167
use hex_literal::hex;
170168
use hybrid_array::{
171169
ArraySize,
172-
typenum::{U8, U32, U128},
170+
typenum::{U8, U4, U32, U128},
173171
};
174172
use sha2::Sha256;
175173

@@ -222,13 +220,12 @@ mod test {
222220
where
223221
HashT: BlockSizeUser + Default + FixedOutput + HashMarker,
224222
HashT::OutputSize: IsLess<U256> + IsLessOrEqual<HashT::BlockSize> + Mul<U8>,
225-
U2: Mul<U32>,
226-
<HashT::OutputSize as Mul<U8>>::Output: IsGreaterOrEqual<<U2 as Mul<U32>>::Output>,
223+
HashT::OutputSize: IsGreaterOrEqual<<U4 as Mul<U2>>::Output>,
227224
{
228225
assert_message::<HashT>(self.msg, domain, L::to_u16(), self.msg_prime);
229226

230227
let dst = [dst];
231-
let mut expander = ExpandMsgXmd::<HashT, U32>::expand_message(
228+
let mut expander = ExpandMsgXmd::<HashT, U4>::expand_message(
232229
&[self.msg],
233230
&dst,
234231
NonZero::new(L::to_usize()).ok_or(Error)?,

elliptic-curve/src/hash2curve/hash2field/expand_msg/xof.rs

+15-27
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,17 @@
22
33
use super::{Domain, ExpandMsg, Expander};
44
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};
116
use digest::{ExtendableOutput, HashMarker, Update, XofReader};
127
use hybrid_array::{
138
ArraySize,
14-
typenum::{IsLess, U2, U8, U256},
9+
typenum::{IsLess, U2, U256},
1510
};
1611

1712
/// Implements `expand_message_xof` via the [`ExpandMsg`] trait:
1813
/// <https://www.rfc-editor.org/rfc/rfc9380.html#name-expand_message_xof>
1914
///
20-
/// `K` is the target security level in bits:
15+
/// `K` is the target security level in bytes:
2116
/// <https://www.rfc-editor.org/rfc/rfc9380.html#section-8.9-2.2>
2217
/// <https://www.rfc-editor.org/rfc/rfc9380.html#name-target-security-levels>
2318
///
@@ -27,9 +22,8 @@ use hybrid_array::{
2722
pub struct ExpandMsgXof<HashT, K>
2823
where
2924
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>,
3327
{
3428
reader: <HashT as ExtendableOutput>::Reader,
3529
_k: PhantomData<K>,
@@ -38,9 +32,8 @@ where
3832
impl<HashT, K> fmt::Debug for ExpandMsgXof<HashT, K>
3933
where
4034
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>,
4437
<HashT as ExtendableOutput>::Reader: fmt::Debug,
4538
{
4639
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
@@ -50,17 +43,13 @@ where
5043
}
5144
}
5245

53-
type HashSize<K> = <<U2 as Mul<K>>::Output as Div<U8>>::Output;
54-
5546
impl<'a, HashT, K> ExpandMsg<'a> for ExpandMsgXof<HashT, K>
5647
where
5748
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`.
6050
// 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>,
6453
{
6554
type Expander = Self;
6655

@@ -71,7 +60,7 @@ where
7160
) -> Result<Self::Expander> {
7261
let len_in_bytes = u16::try_from(len_in_bytes.get()).map_err(|_| Error)?;
7362

74-
let domain = Domain::<HashSize<K>>::xof::<HashT>(dsts)?;
63+
let domain = Domain::<<K as Mul<U2>>::Output>::xof::<HashT>(dsts)?;
7564
let mut reader = HashT::default();
7665

7766
for msg in msgs {
@@ -92,9 +81,8 @@ where
9281
impl<HashT, K> Expander for ExpandMsgXof<HashT, K>
9382
where
9483
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>,
9886
{
9987
fn fill_bytes(&mut self, okm: &mut [u8]) {
10088
self.reader.read(okm);
@@ -108,7 +96,7 @@ mod test {
10896
use hex_literal::hex;
10997
use hybrid_array::{
11098
Array, ArraySize,
111-
typenum::{U32, U128},
99+
typenum::{U16, U32, U128},
112100
};
113101
use sha3::Shake128;
114102

@@ -146,7 +134,7 @@ mod test {
146134
{
147135
assert_message(self.msg, domain, L::to_u16(), self.msg_prime);
148136

149-
let mut expander = ExpandMsgXof::<HashT, U128>::expand_message(
137+
let mut expander = ExpandMsgXof::<HashT, U16>::expand_message(
150138
&[self.msg],
151139
&[dst],
152140
NonZero::new(L::to_usize()).ok_or(Error)?,

0 commit comments

Comments
 (0)