Skip to content

Commit 2ab0f99

Browse files
committed
Revert "Various fixes and improvements to hash2curve (#1813)"
This reverts commit 5d3e031. Temporarily reverting to unblock updating https://github.com/RustCrypto/elliptic-curves
1 parent d676b98 commit 2ab0f99

File tree

5 files changed

+50
-102
lines changed

5 files changed

+50
-102
lines changed

elliptic-curve/src/hash2curve/group_digest.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
use super::{ExpandMsg, FromOkm, MapToCurve, hash_to_field};
44
use crate::{CurveArithmetic, ProjectivePoint, Result};
55
use group::cofactor::CofactorGroup;
6-
use hybrid_array::typenum::Unsigned;
76

87
/// Adds hashing arbitrary byte sequences to a valid group element
98
pub trait GroupDigest: CurveArithmetic
@@ -13,11 +12,6 @@ where
1312
/// The field element representation for a group value with multiple elements
1413
type FieldElement: FromOkm + MapToCurve<Output = ProjectivePoint<Self>> + Default + Copy;
1514

16-
/// The target security level in bytes:
17-
/// <https://www.rfc-editor.org/rfc/rfc9380.html#section-8.9-2.2>
18-
/// <https://www.rfc-editor.org/rfc/rfc9380.html#name-target-security-levels>
19-
type K: Unsigned;
20-
2115
/// Computes the hash to curve routine.
2216
///
2317
/// From <https://www.ietf.org/archive/id/draft-irtf-cfrg-hash-to-curve-13.html>:

elliptic-curve/src/hash2curve/hash2field.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,15 @@
44
55
mod expand_msg;
66

7-
use core::num::NonZeroUsize;
8-
97
pub use expand_msg::{xmd::*, xof::*, *};
108

119
use crate::{Error, Result};
12-
use hybrid_array::{
13-
Array, ArraySize,
14-
typenum::{NonZero, Unsigned},
15-
};
10+
use hybrid_array::{Array, ArraySize, typenum::Unsigned};
1611

1712
/// The trait for helping to convert to a field element.
1813
pub trait FromOkm {
1914
/// The number of bytes needed to convert to a field element.
20-
type Length: ArraySize + NonZero;
15+
type Length: ArraySize;
2116

2217
/// Convert a byte sequence into a field element.
2318
fn from_okm(data: &Array<u8, Self::Length>) -> Self;
@@ -42,10 +37,7 @@ where
4237
E: ExpandMsg<'a>,
4338
T: FromOkm + Default,
4439
{
45-
let len_in_bytes = T::Length::to_usize()
46-
.checked_mul(out.len())
47-
.and_then(NonZeroUsize::new)
48-
.ok_or(Error)?;
40+
let len_in_bytes = T::Length::to_usize().checked_mul(out.len()).ok_or(Error)?;
4941
let mut tmp = Array::<u8, <T as FromOkm>::Length>::default();
5042
let mut expander = E::expand_message(data, domain, len_in_bytes)?;
5143
for o in out.iter_mut() {

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
pub(super) mod xmd;
44
pub(super) mod xof;
55

6-
use core::num::NonZero;
7-
86
use crate::{Error, Result};
97
use digest::{Digest, ExtendableOutput, Update, XofReader};
108
use hybrid_array::typenum::{IsLess, U256};
@@ -30,7 +28,7 @@ pub trait ExpandMsg<'a> {
3028
fn expand_message(
3129
msgs: &[&[u8]],
3230
dsts: &'a [&'a [u8]],
33-
len_in_bytes: NonZero<usize>,
31+
len_in_bytes: usize,
3432
) -> Result<Self::Expander>;
3533
}
3634

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

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,59 @@
11
//! `expand_message_xmd` based on a hash function.
22
3-
use core::{marker::PhantomData, num::NonZero, ops::Mul};
3+
use core::marker::PhantomData;
44

55
use super::{Domain, ExpandMsg, Expander};
66
use crate::{Error, Result};
77
use digest::{
88
FixedOutput, HashMarker,
99
array::{
1010
Array,
11-
typenum::{IsGreaterOrEqual, IsLess, IsLessOrEqual, U2, U256, Unsigned},
11+
typenum::{IsLess, IsLessOrEqual, U256, Unsigned},
1212
},
1313
core_api::BlockSizeUser,
1414
};
1515

16-
/// Implements `expand_message_xof` via the [`ExpandMsg`] trait:
17-
/// <https://www.rfc-editor.org/rfc/rfc9380.html#name-expand_message_xmd>
18-
///
19-
/// `K` is the target security level in bytes:
20-
/// <https://www.rfc-editor.org/rfc/rfc9380.html#section-8.9-2.2>
21-
/// <https://www.rfc-editor.org/rfc/rfc9380.html#name-target-security-levels>
16+
/// Placeholder type for implementing `expand_message_xmd` based on a hash function
2217
///
2318
/// # Errors
2419
/// - `dst.is_empty()`
20+
/// - `len_in_bytes == 0`
2521
/// - `len_in_bytes > u16::MAX`
2622
/// - `len_in_bytes > 255 * HashT::OutputSize`
2723
#[derive(Debug)]
28-
pub struct ExpandMsgXmd<HashT, K>(PhantomData<(HashT, K)>)
24+
pub struct ExpandMsgXmd<HashT>(PhantomData<HashT>)
2925
where
3026
HashT: BlockSizeUser + Default + FixedOutput + HashMarker,
3127
HashT::OutputSize: IsLess<U256>,
32-
HashT::OutputSize: IsLessOrEqual<HashT::BlockSize>,
33-
K: Mul<U2>,
34-
HashT::OutputSize: IsGreaterOrEqual<<K as Mul<U2>>::Output>;
28+
HashT::OutputSize: IsLessOrEqual<HashT::BlockSize>;
3529

36-
impl<'a, HashT, K> ExpandMsg<'a> for ExpandMsgXmd<HashT, K>
30+
/// ExpandMsgXmd implements expand_message_xmd for the ExpandMsg trait
31+
impl<'a, HashT> ExpandMsg<'a> for ExpandMsgXmd<HashT>
3732
where
3833
HashT: BlockSizeUser + Default + FixedOutput + HashMarker,
39-
// If DST is larger than 255 bytes, the length of the computed DST will depend on the output
40-
// size of the hash, which is still not allowed to be larger than 256:
34+
// If `len_in_bytes` is bigger then 256, length of the `DST` will depend on
35+
// the output size of the hash, which is still not allowed to be bigger then 256:
4136
// https://www.ietf.org/archive/id/draft-irtf-cfrg-hash-to-curve-13.html#section-5.4.1-6
4237
HashT::OutputSize: IsLess<U256>,
4338
// Constraint set by `expand_message_xmd`:
4439
// https://www.ietf.org/archive/id/draft-irtf-cfrg-hash-to-curve-13.html#section-5.4.1-4
4540
HashT::OutputSize: IsLessOrEqual<HashT::BlockSize>,
46-
// The number of bits output by `HashT` MUST be larger or equal to `K * 2`:
47-
// https://www.rfc-editor.org/rfc/rfc9380.html#section-5.3.1-2.1
48-
K: Mul<U2>,
49-
HashT::OutputSize: IsGreaterOrEqual<<K as Mul<U2>>::Output>,
5041
{
5142
type Expander = ExpanderXmd<'a, HashT>;
5243

5344
fn expand_message(
5445
msgs: &[&[u8]],
5546
dsts: &'a [&'a [u8]],
56-
len_in_bytes: NonZero<usize>,
47+
len_in_bytes: usize,
5748
) -> Result<Self::Expander> {
58-
let len_in_bytes_u16 = u16::try_from(len_in_bytes.get()).map_err(|_| Error)?;
59-
60-
// `255 * <b_in_bytes>` can not exceed `u16::MAX`
61-
if len_in_bytes_u16 > 255 * HashT::OutputSize::to_u16() {
49+
if len_in_bytes == 0 {
6250
return Err(Error);
6351
}
6452

53+
let len_in_bytes_u16 = u16::try_from(len_in_bytes).map_err(|_| Error)?;
54+
6555
let b_in_bytes = HashT::OutputSize::to_usize();
66-
let ell = u8::try_from(len_in_bytes.get().div_ceil(b_in_bytes)).map_err(|_| Error)?;
56+
let ell = u8::try_from(len_in_bytes.div_ceil(b_in_bytes)).map_err(|_| Error)?;
6757

6858
let domain = Domain::xmd::<HashT>(dsts)?;
6959
let mut b_0 = HashT::default();
@@ -167,7 +157,7 @@ mod test {
167157
use hex_literal::hex;
168158
use hybrid_array::{
169159
ArraySize,
170-
typenum::{U4, U8, U32, U128},
160+
typenum::{U32, U128},
171161
};
172162
use sha2::Sha256;
173163

@@ -219,17 +209,13 @@ mod test {
219209
) -> Result<()>
220210
where
221211
HashT: BlockSizeUser + Default + FixedOutput + HashMarker,
222-
HashT::OutputSize: IsLess<U256> + IsLessOrEqual<HashT::BlockSize> + Mul<U8>,
223-
HashT::OutputSize: IsGreaterOrEqual<<U4 as Mul<U2>>::Output>,
212+
HashT::OutputSize: IsLess<U256> + IsLessOrEqual<HashT::BlockSize>,
224213
{
225214
assert_message::<HashT>(self.msg, domain, L::to_u16(), self.msg_prime);
226215

227216
let dst = [dst];
228-
let mut expander = ExpandMsgXmd::<HashT, U4>::expand_message(
229-
&[self.msg],
230-
&dst,
231-
NonZero::new(L::to_usize()).ok_or(Error)?,
232-
)?;
217+
let mut expander =
218+
ExpandMsgXmd::<HashT>::expand_message(&[self.msg], &dst, L::to_usize())?;
233219

234220
let mut uniform_bytes = Array::<u8, L>::default();
235221
expander.fill_bytes(&mut uniform_bytes);

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

Lines changed: 27 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,26 @@
22
33
use super::{Domain, ExpandMsg, Expander};
44
use crate::{Error, Result};
5-
use core::{fmt, marker::PhantomData, num::NonZero, ops::Mul};
6-
use digest::{ExtendableOutput, HashMarker, Update, XofReader};
7-
use hybrid_array::{
8-
ArraySize,
9-
typenum::{IsLess, U2, U256},
10-
};
11-
12-
/// Implements `expand_message_xof` via the [`ExpandMsg`] trait:
13-
/// <https://www.rfc-editor.org/rfc/rfc9380.html#name-expand_message_xof>
14-
///
15-
/// `K` is the target security level in bytes:
16-
/// <https://www.rfc-editor.org/rfc/rfc9380.html#section-8.9-2.2>
17-
/// <https://www.rfc-editor.org/rfc/rfc9380.html#name-target-security-levels>
5+
use core::fmt;
6+
use digest::{ExtendableOutput, Update, XofReader};
7+
use hybrid_array::typenum::U32;
8+
9+
/// Placeholder type for implementing `expand_message_xof` based on an extendable output function
1810
///
1911
/// # Errors
2012
/// - `dst.is_empty()`
13+
/// - `len_in_bytes == 0`
2114
/// - `len_in_bytes > u16::MAX`
22-
pub struct ExpandMsgXof<HashT, K>
15+
pub struct ExpandMsgXof<HashT>
2316
where
24-
HashT: Default + ExtendableOutput + Update + HashMarker,
25-
K: Mul<U2>,
26-
<K as Mul<U2>>::Output: ArraySize + IsLess<U256>,
17+
HashT: Default + ExtendableOutput + Update,
2718
{
2819
reader: <HashT as ExtendableOutput>::Reader,
29-
_k: PhantomData<K>,
3020
}
3121

32-
impl<HashT, K> fmt::Debug for ExpandMsgXof<HashT, K>
22+
impl<HashT> fmt::Debug for ExpandMsgXof<HashT>
3323
where
34-
HashT: Default + ExtendableOutput + Update + HashMarker,
35-
K: Mul<U2>,
36-
<K as Mul<U2>>::Output: ArraySize + IsLess<U256>,
24+
HashT: Default + ExtendableOutput + Update,
3725
<HashT as ExtendableOutput>::Reader: fmt::Debug,
3826
{
3927
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
@@ -43,24 +31,25 @@ where
4331
}
4432
}
4533

46-
impl<'a, HashT, K> ExpandMsg<'a> for ExpandMsgXof<HashT, K>
34+
/// ExpandMsgXof implements `expand_message_xof` for the [`ExpandMsg`] trait
35+
impl<'a, HashT> ExpandMsg<'a> for ExpandMsgXof<HashT>
4736
where
48-
HashT: Default + ExtendableOutput + Update + HashMarker,
49-
// If DST is larger than 255 bytes, the length of the computed DST is calculated by `K * 2`.
50-
// https://www.rfc-editor.org/rfc/rfc9380.html#section-5.3.1-2.1
51-
K: Mul<U2>,
52-
<K as Mul<U2>>::Output: ArraySize + IsLess<U256>,
37+
HashT: Default + ExtendableOutput + Update,
5338
{
5439
type Expander = Self;
5540

5641
fn expand_message(
5742
msgs: &[&[u8]],
5843
dsts: &'a [&'a [u8]],
59-
len_in_bytes: NonZero<usize>,
44+
len_in_bytes: usize,
6045
) -> Result<Self::Expander> {
61-
let len_in_bytes = u16::try_from(len_in_bytes.get()).map_err(|_| Error)?;
46+
if len_in_bytes == 0 {
47+
return Err(Error);
48+
}
49+
50+
let len_in_bytes = u16::try_from(len_in_bytes).map_err(|_| Error)?;
6251

63-
let domain = Domain::<<K as Mul<U2>>::Output>::xof::<HashT>(dsts)?;
52+
let domain = Domain::<U32>::xof::<HashT>(dsts)?;
6453
let mut reader = HashT::default();
6554

6655
for msg in msgs {
@@ -71,18 +60,13 @@ where
7160
domain.update_hash(&mut reader);
7261
reader.update(&[domain.len()]);
7362
let reader = reader.finalize_xof();
74-
Ok(Self {
75-
reader,
76-
_k: PhantomData,
77-
})
63+
Ok(Self { reader })
7864
}
7965
}
8066

81-
impl<HashT, K> Expander for ExpandMsgXof<HashT, K>
67+
impl<HashT> Expander for ExpandMsgXof<HashT>
8268
where
83-
HashT: Default + ExtendableOutput + Update + HashMarker,
84-
K: Mul<U2>,
85-
<K as Mul<U2>>::Output: ArraySize + IsLess<U256>,
69+
HashT: Default + ExtendableOutput + Update,
8670
{
8771
fn fill_bytes(&mut self, okm: &mut [u8]) {
8872
self.reader.read(okm);
@@ -94,10 +78,7 @@ mod test {
9478
use super::*;
9579
use core::mem::size_of;
9680
use hex_literal::hex;
97-
use hybrid_array::{
98-
Array, ArraySize,
99-
typenum::{U16, U32, U128},
100-
};
81+
use hybrid_array::{Array, ArraySize, typenum::U128};
10182
use sha3::Shake128;
10283

10384
fn assert_message(msg: &[u8], domain: &Domain<'_, U32>, len_in_bytes: u16, bytes: &[u8]) {
@@ -129,16 +110,13 @@ mod test {
129110
#[allow(clippy::panic_in_result_fn)]
130111
fn assert<HashT, L>(&self, dst: &'static [u8], domain: &Domain<'_, U32>) -> Result<()>
131112
where
132-
HashT: Default + ExtendableOutput + Update + HashMarker,
113+
HashT: Default + ExtendableOutput + Update,
133114
L: ArraySize,
134115
{
135116
assert_message(self.msg, domain, L::to_u16(), self.msg_prime);
136117

137-
let mut expander = ExpandMsgXof::<HashT, U16>::expand_message(
138-
&[self.msg],
139-
&[dst],
140-
NonZero::new(L::to_usize()).ok_or(Error)?,
141-
)?;
118+
let mut expander =
119+
ExpandMsgXof::<HashT>::expand_message(&[self.msg], &[dst], L::to_usize())?;
142120

143121
let mut uniform_bytes = Array::<u8, L>::default();
144122
expander.fill_bytes(&mut uniform_bytes);

0 commit comments

Comments
 (0)