Skip to content

Commit 3b59253

Browse files
committed
migrate rate to const generics
1 parent a0629c0 commit 3b59253

13 files changed

Lines changed: 214 additions & 203 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ascon-xof128/src/cxof.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use digest::{
33
CollisionResistance, CustomizedInit, ExtendableOutput, HashMarker, OutputSizeUser, Update,
44
common::AlgorithmName,
55
common::hazmat::{DeserializeStateError, SerializableState, SerializedState},
6-
consts::{U8, U16, U32, U41},
6+
consts::{U16, U32, U41},
77
};
88
use sponge_cursor::SpongeCursor;
99

@@ -20,7 +20,7 @@ use crate::{AsconXof128Reader, consts::CXOF_INIT_STATE};
2020
#[derive(Clone, Debug)]
2121
pub struct AsconCxof128 {
2222
state: State,
23-
cursor: SpongeCursor<U8>,
23+
cursor: SpongeCursor<8>,
2424
}
2525

2626
impl CustomizedInit for AsconCxof128 {

ascon-xof128/src/reader.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use ascon::State;
2-
use digest::{XofReader, consts::U8};
2+
use digest::XofReader;
33
use sponge_cursor::SpongeCursor;
44

55
/// XOF reader used by Ascon-XOF128 and Ascon-CXOF128
66
#[derive(Clone, Debug)]
77
pub struct AsconXof128Reader {
88
state: State,
9-
cursor: SpongeCursor<U8>,
9+
cursor: SpongeCursor<8>,
1010
}
1111

1212
impl AsconXof128Reader {
@@ -22,7 +22,7 @@ impl XofReader for AsconXof128Reader {
2222
#[inline]
2323
fn read(&mut self, buf: &mut [u8]) {
2424
self.cursor
25-
.squeeze_u64_le(&mut self.state, ascon::permute12, buf);
25+
.squeeze_read_u64_le(&mut self.state, ascon::permute12, buf);
2626
}
2727
}
2828

ascon-xof128/src/xof.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use digest::{
55
Reset, Update,
66
common::AlgorithmName,
77
common::hazmat::{DeserializeStateError, SerializableState, SerializedState},
8-
consts::{U8, U16, U32, U41},
8+
consts::{U16, U32, U41},
99
};
1010
use sponge_cursor::SpongeCursor;
1111

@@ -15,7 +15,7 @@ use crate::{AsconXof128Reader, consts::XOF_INIT_STATE};
1515
#[derive(Clone)]
1616
pub struct AsconXof128 {
1717
state: State,
18-
cursor: SpongeCursor<U8>,
18+
cursor: SpongeCursor<8>,
1919
}
2020

2121
impl Default for AsconXof128 {

cshake/src/lib.rs

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@
1111

1212
pub use digest;
1313

14-
use core::{fmt, marker::PhantomData};
14+
use core::fmt;
1515
use digest::{
1616
CollisionResistance, CustomizedInit, ExtendableOutput, HashMarker, Update, XofReader,
17-
array::ArraySize,
1817
common::{AlgorithmName, BlockSizeUser},
1918
consts::{U16, U32, U136, U168},
2019
};
@@ -25,37 +24,36 @@ const SHAKE_PAD: u8 = 0x1F;
2524
const CSHAKE_PAD: u8 = 0x04;
2625

2726
/// cSHAKE128 hasher.
28-
pub type CShake128 = CShake<U168>;
27+
pub type CShake128 = CShake<168>;
2928
/// cSHAKE256 hasher.
30-
pub type CShake256 = CShake<U136>;
29+
pub type CShake256 = CShake<136>;
3130

3231
/// cSHAKE hasher generic over rate.
3332
///
34-
/// Rate MUST be either [`U168`] or [`U136`] for cSHAKE128 and cSHAKE256 respectively.
33+
/// Rate MUST be either 168 or 136 for cSHAKE128 and cSHAKE256 respectively.
3534
#[derive(Clone)]
36-
pub struct CShake<Rate: ArraySize> {
35+
pub struct CShake<const RATE: usize> {
3736
state: State1600,
38-
cursor: SpongeCursor<Rate>,
37+
cursor: SpongeCursor<RATE>,
3938
pad: u8,
4039
keccak: Keccak,
41-
_pd: PhantomData<Rate>,
4240
}
4341

44-
impl<Rate: ArraySize> Default for CShake<Rate> {
42+
impl<const RATE: usize> Default for CShake<RATE> {
4543
#[inline]
4644
fn default() -> Self {
4745
Self::new_with_function_name(b"", b"")
4846
}
4947
}
5048

51-
impl<Rate: ArraySize> CShake<Rate> {
49+
impl<const RATE: usize> CShake<RATE> {
5250
/// Creates a new cSHAKE instance with the given function name and customization.
5351
///
5452
/// Note that the function name is intended for use by NIST and should only be set to
5553
/// values defined by NIST. You probably don't need to use this function.
5654
pub fn new_with_function_name(function_name: &[u8], customization: &[u8]) -> Self {
5755
const {
58-
assert!(Rate::USIZE == 168 || Rate::USIZE == 136, "unsupported rate");
56+
assert!(RATE == 168 || RATE == 136, "unsupported rate");
5957
}
6058

6159
let keccak = Keccak::new();
@@ -67,7 +65,6 @@ impl<Rate: ArraySize> CShake<Rate> {
6765
cursor: Default::default(),
6866
pad: SHAKE_PAD,
6967
keccak,
70-
_pd: PhantomData,
7168
};
7269
}
7370

@@ -80,11 +77,12 @@ impl<Rate: ArraySize> CShake<Rate> {
8077
}
8178

8279
keccak.with_f1600(|f1600| {
83-
let mut cursor: SpongeCursor<Rate> = Default::default();
80+
let mut cursor: SpongeCursor<RATE> = Default::default();
8481
let state = &mut state;
8582
let mut b = [0u8; 9];
8683

87-
cursor.absorb_u64_le(state, f1600, left_encode(Rate::U64, &mut b));
84+
let rate_u64 = u64::try_from(RATE).expect("RATE is smaller than 200");
85+
cursor.absorb_u64_le(state, f1600, left_encode(rate_u64, &mut b));
8886

8987
let mut encode_str = |str: &[u8]| {
9088
let str_bits_len = 8 * u64::try_from(str.len())
@@ -107,34 +105,29 @@ impl<Rate: ArraySize> CShake<Rate> {
107105
cursor: Default::default(),
108106
pad: CSHAKE_PAD,
109107
keccak,
110-
_pd: PhantomData,
111108
}
112109
}
113110
}
114111

115-
impl<Rate: ArraySize> CustomizedInit for CShake<Rate> {
112+
impl<const RATE: usize> CustomizedInit for CShake<RATE> {
116113
#[inline]
117114
fn new_customized(customization: &[u8]) -> Self {
118115
Self::new_with_function_name(&[], customization)
119116
}
120117
}
121118

122-
impl<Rate: ArraySize> HashMarker for CShake<Rate> {}
119+
impl<const RATE: usize> HashMarker for CShake<RATE> {}
123120

124-
impl<Rate: ArraySize> BlockSizeUser for CShake<Rate> {
125-
type BlockSize = Rate;
126-
}
127-
128-
impl<Rate: ArraySize> Update for CShake<Rate> {
121+
impl<const RATE: usize> Update for CShake<RATE> {
129122
fn update(&mut self, data: &[u8]) {
130123
self.keccak.with_f1600(|f1600| {
131124
self.cursor.absorb_u64_le(&mut self.state, f1600, data);
132125
});
133126
}
134127
}
135128

136-
impl<Rate: ArraySize> ExtendableOutput for CShake<Rate> {
137-
type Reader = CShakeReader<Rate>;
129+
impl<const RATE: usize> ExtendableOutput for CShake<RATE> {
130+
type Reader = CShakeReader<RATE>;
138131

139132
#[inline]
140133
fn finalize_xof(mut self) -> Self::Reader {
@@ -144,7 +137,7 @@ impl<Rate: ArraySize> ExtendableOutput for CShake<Rate> {
144137

145138
let pad = u64::from(self.pad) << (8 * byte_offset);
146139
self.state[word_offset] ^= pad;
147-
self.state[Rate::USIZE / 8 - 1] ^= 1 << 63;
140+
self.state[RATE / 8 - 1] ^= 1 << 63;
148141

149142
// Note that `CShakeReader` applies the permutation to the state before reading from it
150143

@@ -156,9 +149,9 @@ impl<Rate: ArraySize> ExtendableOutput for CShake<Rate> {
156149
}
157150
}
158151

159-
impl<Rate: ArraySize> AlgorithmName for CShake<Rate> {
152+
impl<const RATE: usize> AlgorithmName for CShake<RATE> {
160153
fn write_alg_name(f: &mut fmt::Formatter<'_>) -> fmt::Result {
161-
let alg_name = match Rate::USIZE {
154+
let alg_name = match RATE {
162155
168 => "cSHAKE128",
163156
136 => "cSHAKE256",
164157
_ => unreachable!(),
@@ -167,9 +160,9 @@ impl<Rate: ArraySize> AlgorithmName for CShake<Rate> {
167160
}
168161
}
169162

170-
impl<Rate: ArraySize> fmt::Debug for CShake<Rate> {
163+
impl<const RATE: usize> fmt::Debug for CShake<RATE> {
171164
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
172-
let debug_str = match Rate::USIZE {
165+
let debug_str = match RATE {
173166
168 => "CShake128 { ... }",
174167
136 => "CShake256 { ... }",
175168
_ => unreachable!(),
@@ -178,7 +171,7 @@ impl<Rate: ArraySize> fmt::Debug for CShake<Rate> {
178171
}
179172
}
180173

181-
impl<Rate: ArraySize> Drop for CShake<Rate> {
174+
impl<const RATE: usize> Drop for CShake<RATE> {
182175
fn drop(&mut self) {
183176
#[cfg(feature = "zeroize")]
184177
{
@@ -191,37 +184,37 @@ impl<Rate: ArraySize> Drop for CShake<Rate> {
191184
}
192185

193186
#[cfg(feature = "zeroize")]
194-
impl<Rate: ArraySize> digest::zeroize::ZeroizeOnDrop for CShake<Rate> {}
187+
impl<const RATE: usize> digest::zeroize::ZeroizeOnDrop for CShake<RATE> {}
195188

196189
/// Generic cSHAKE XOF reader
197190
#[derive(Clone)]
198-
pub struct CShakeReader<Rate: ArraySize> {
191+
pub struct CShakeReader<const RATE: usize> {
199192
state: State1600,
200-
cursor: SpongeCursor<Rate>,
193+
cursor: SpongeCursor<RATE>,
201194
keccak: Keccak,
202195
}
203196

204-
impl<Rate: ArraySize> XofReader for CShakeReader<Rate> {
197+
impl<const RATE: usize> XofReader for CShakeReader<RATE> {
205198
#[inline]
206199
fn read(&mut self, buf: &mut [u8]) {
207200
self.keccak.with_f1600(|f1600| {
208-
self.cursor.squeeze_u64_le(&mut self.state, f1600, buf);
201+
self.cursor.squeeze_read_u64_le(&mut self.state, f1600, buf);
209202
});
210203
}
211204
}
212205

213-
impl<Rate: ArraySize> fmt::Debug for CShakeReader<Rate> {
206+
impl<const RATE: usize> fmt::Debug for CShakeReader<RATE> {
214207
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
215-
let debug_str = match Rate::USIZE {
216-
168 => "TurboShakeReader128 { ... }",
217-
136 => "TurboShakeReader256 { ... }",
208+
let debug_str = match RATE {
209+
168 => "CShakeReader128 { ... }",
210+
136 => "CShakeReader256 { ... }",
218211
_ => unreachable!(),
219212
};
220213
f.write_str(debug_str)
221214
}
222215
}
223216

224-
impl<Rate: ArraySize> Drop for CShakeReader<Rate> {
217+
impl<const RATE: usize> Drop for CShakeReader<RATE> {
225218
fn drop(&mut self) {
226219
#[cfg(feature = "zeroize")]
227220
{
@@ -240,3 +233,11 @@ impl CollisionResistance for CShake128 {
240233
impl CollisionResistance for CShake256 {
241234
type CollisionResistance = U32;
242235
}
236+
237+
impl BlockSizeUser for CShake128 {
238+
type BlockSize = U168;
239+
}
240+
241+
impl BlockSizeUser for CShake256 {
242+
type BlockSize = U136;
243+
}

0 commit comments

Comments
 (0)