Skip to content

Commit 4b37715

Browse files
authored
Use Output = True instead of NonZero (#1850)
Bounds like `IsLess` aren't actually enforced unless the `IsLess::Output` is checked. This was done by adding `Le<...>: NonZero`. This PR simplifies this check by adding `IsLess<..., Output = True>`.
1 parent 26119c2 commit 4b37715

File tree

4 files changed

+28
-32
lines changed

4 files changed

+28
-32
lines changed

crypto-common/src/lib.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,11 @@ pub trait BlockSizes: ArraySize + sealed::BlockSizes {}
7979
impl<T: ArraySize + sealed::BlockSizes> BlockSizes for T {}
8080

8181
mod sealed {
82-
use crate::typenum::{Gr, IsGreater, IsLess, Le, NonZero, U0, U256, Unsigned};
82+
use crate::typenum::{IsLess, NonZero, True, U256, Unsigned};
8383

8484
pub trait BlockSizes {}
8585

86-
impl<T: Unsigned> BlockSizes for T
87-
where
88-
Self: IsLess<U256> + IsGreater<U0>,
89-
Le<Self, U256>: NonZero,
90-
Gr<Self, U0>: NonZero,
91-
{
92-
}
86+
impl<T: Unsigned> BlockSizes for T where Self: IsLess<U256, Output = True> + NonZero {}
9387
}
9488

9589
/// Types which can process blocks in parallel.

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use core::num::NonZero;
77

88
use crate::{Error, Result};
99
use digest::{Digest, ExtendableOutput, Update, XofReader};
10-
use hybrid_array::typenum::{IsLess, U256};
10+
use hybrid_array::typenum::{IsLess, True, U256};
1111
use hybrid_array::{Array, ArraySize};
1212

1313
/// Salt when the DST is too long
@@ -48,7 +48,7 @@ pub trait Expander {
4848
#[derive(Debug)]
4949
pub(crate) enum Domain<'a, L>
5050
where
51-
L: ArraySize + IsLess<U256>,
51+
L: ArraySize + IsLess<U256, Output = True>,
5252
{
5353
/// > 255
5454
Hashed(Array<u8, L>),
@@ -58,7 +58,7 @@ where
5858

5959
impl<'a, L> Domain<'a, L>
6060
where
61-
L: ArraySize + IsLess<U256>,
61+
L: ArraySize + IsLess<U256, Output = True>,
6262
{
6363
pub fn xof<X>(dsts: &'a [&'a [u8]]) -> Result<Self>
6464
where

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

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use digest::{
88
FixedOutput, HashMarker,
99
array::{
1010
Array,
11-
typenum::{IsGreaterOrEqual, IsLess, IsLessOrEqual, U2, U256, Unsigned},
11+
typenum::{IsGreaterOrEqual, IsLess, IsLessOrEqual, True, U2, U256, Unsigned},
1212
},
1313
core_api::BlockSizeUser,
1414
};
@@ -28,25 +28,25 @@ use digest::{
2828
pub struct ExpandMsgXmd<HashT, K>(PhantomData<(HashT, K)>)
2929
where
3030
HashT: BlockSizeUser + Default + FixedOutput + HashMarker,
31-
HashT::OutputSize: IsLess<U256>,
32-
HashT::OutputSize: IsLessOrEqual<HashT::BlockSize>,
31+
HashT::OutputSize: IsLess<U256, Output = True>,
32+
HashT::OutputSize: IsLessOrEqual<HashT::BlockSize, Output = True>,
3333
K: Mul<U2>,
34-
HashT::OutputSize: IsGreaterOrEqual<<K as Mul<U2>>::Output>;
34+
HashT::OutputSize: IsGreaterOrEqual<<K as Mul<U2>>::Output, Output = True>;
3535

3636
impl<'a, HashT, K> ExpandMsg<'a> for ExpandMsgXmd<HashT, K>
3737
where
3838
HashT: BlockSizeUser + Default + FixedOutput + HashMarker,
3939
// If DST is larger than 255 bytes, the length of the computed DST will depend on the output
4040
// size of the hash, which is still not allowed to be larger than 256:
4141
// https://www.ietf.org/archive/id/draft-irtf-cfrg-hash-to-curve-13.html#section-5.4.1-6
42-
HashT::OutputSize: IsLess<U256>,
42+
HashT::OutputSize: IsLess<U256, Output = True>,
4343
// Constraint set by `expand_message_xmd`:
4444
// https://www.ietf.org/archive/id/draft-irtf-cfrg-hash-to-curve-13.html#section-5.4.1-4
45-
HashT::OutputSize: IsLessOrEqual<HashT::BlockSize>,
45+
HashT::OutputSize: IsLessOrEqual<HashT::BlockSize, Output = True>,
4646
// The number of bits output by `HashT` MUST be larger or equal to `K * 2`:
4747
// https://www.rfc-editor.org/rfc/rfc9380.html#section-5.3.1-2.1
4848
K: Mul<U2>,
49-
HashT::OutputSize: IsGreaterOrEqual<<K as Mul<U2>>::Output>,
49+
HashT::OutputSize: IsGreaterOrEqual<<K as Mul<U2>>::Output, Output = True>,
5050
{
5151
type Expander = ExpanderXmd<'a, HashT>;
5252

@@ -102,8 +102,8 @@ where
102102
pub struct ExpanderXmd<'a, HashT>
103103
where
104104
HashT: BlockSizeUser + Default + FixedOutput + HashMarker,
105-
HashT::OutputSize: IsLess<U256>,
106-
HashT::OutputSize: IsLessOrEqual<HashT::BlockSize>,
105+
HashT::OutputSize: IsLess<U256, Output = True>,
106+
HashT::OutputSize: IsLessOrEqual<HashT::BlockSize, Output = True>,
107107
{
108108
b_0: Array<u8, HashT::OutputSize>,
109109
b_vals: Array<u8, HashT::OutputSize>,
@@ -116,8 +116,8 @@ where
116116
impl<HashT> ExpanderXmd<'_, HashT>
117117
where
118118
HashT: BlockSizeUser + Default + FixedOutput + HashMarker,
119-
HashT::OutputSize: IsLess<U256>,
120-
HashT::OutputSize: IsLessOrEqual<HashT::BlockSize>,
119+
HashT::OutputSize: IsLess<U256, Output = True>,
120+
HashT::OutputSize: IsLessOrEqual<HashT::BlockSize, Output = True>,
121121
{
122122
fn next(&mut self) -> bool {
123123
if self.index < self.ell {
@@ -146,8 +146,8 @@ where
146146
impl<HashT> Expander for ExpanderXmd<'_, HashT>
147147
where
148148
HashT: BlockSizeUser + Default + FixedOutput + HashMarker,
149-
HashT::OutputSize: IsLess<U256>,
150-
HashT::OutputSize: IsLessOrEqual<HashT::BlockSize>,
149+
HashT::OutputSize: IsLess<U256, Output = True>,
150+
HashT::OutputSize: IsLessOrEqual<HashT::BlockSize, Output = True>,
151151
{
152152
fn fill_bytes(&mut self, okm: &mut [u8]) {
153153
for b in okm {
@@ -178,7 +178,7 @@ mod test {
178178
bytes: &[u8],
179179
) where
180180
HashT: BlockSizeUser + Default + FixedOutput + HashMarker,
181-
HashT::OutputSize: IsLess<U256>,
181+
HashT::OutputSize: IsLess<U256, Output = True>,
182182
{
183183
let block = HashT::BlockSize::to_usize();
184184
assert_eq!(
@@ -219,8 +219,10 @@ mod test {
219219
) -> Result<()>
220220
where
221221
HashT: BlockSizeUser + Default + FixedOutput + HashMarker,
222-
HashT::OutputSize: IsLess<U256> + IsLessOrEqual<HashT::BlockSize> + Mul<U8>,
223-
HashT::OutputSize: IsGreaterOrEqual<<U4 as Mul<U2>>::Output>,
222+
HashT::OutputSize: IsLess<U256, Output = True>
223+
+ IsLessOrEqual<HashT::BlockSize, Output = True>
224+
+ Mul<U8>,
225+
HashT::OutputSize: IsGreaterOrEqual<<U4 as Mul<U2>>::Output, Output = True>,
224226
{
225227
assert_message::<HashT>(self.msg, domain, L::to_u16(), self.msg_prime);
226228

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use core::{fmt, marker::PhantomData, num::NonZero, ops::Mul};
66
use digest::{ExtendableOutput, HashMarker, Update, XofReader};
77
use hybrid_array::{
88
ArraySize,
9-
typenum::{IsLess, U2, U256},
9+
typenum::{IsLess, True, U2, U256},
1010
};
1111

1212
/// Implements `expand_message_xof` via the [`ExpandMsg`] trait:
@@ -23,7 +23,7 @@ pub struct ExpandMsgXof<HashT, K>
2323
where
2424
HashT: Default + ExtendableOutput + Update + HashMarker,
2525
K: Mul<U2>,
26-
<K as Mul<U2>>::Output: ArraySize + IsLess<U256>,
26+
<K as Mul<U2>>::Output: ArraySize + IsLess<U256, Output = True>,
2727
{
2828
reader: <HashT as ExtendableOutput>::Reader,
2929
_k: PhantomData<K>,
@@ -33,7 +33,7 @@ impl<HashT, K> fmt::Debug for ExpandMsgXof<HashT, K>
3333
where
3434
HashT: Default + ExtendableOutput + Update + HashMarker,
3535
K: Mul<U2>,
36-
<K as Mul<U2>>::Output: ArraySize + IsLess<U256>,
36+
<K as Mul<U2>>::Output: ArraySize + IsLess<U256, Output = True>,
3737
<HashT as ExtendableOutput>::Reader: fmt::Debug,
3838
{
3939
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
@@ -49,7 +49,7 @@ where
4949
// If DST is larger than 255 bytes, the length of the computed DST is calculated by `K * 2`.
5050
// https://www.rfc-editor.org/rfc/rfc9380.html#section-5.3.1-2.1
5151
K: Mul<U2>,
52-
<K as Mul<U2>>::Output: ArraySize + IsLess<U256>,
52+
<K as Mul<U2>>::Output: ArraySize + IsLess<U256, Output = True>,
5353
{
5454
type Expander = Self;
5555

@@ -82,7 +82,7 @@ impl<HashT, K> Expander for ExpandMsgXof<HashT, K>
8282
where
8383
HashT: Default + ExtendableOutput + Update + HashMarker,
8484
K: Mul<U2>,
85-
<K as Mul<U2>>::Output: ArraySize + IsLess<U256>,
85+
<K as Mul<U2>>::Output: ArraySize + IsLess<U256, Output = True>,
8686
{
8787
fn fill_bytes(&mut self, okm: &mut [u8]) {
8888
self.reader.read(okm);

0 commit comments

Comments
 (0)