Skip to content

Commit 73a8faf

Browse files
authored
turboshake: introduce separate customizable type aliases (#866)
Change `TurboShake128/256` type aliases to use the default domain separator (DS) and introduce separate `CTurboShake128/256` type aliases generic over DS. It's effectively a work around for the subpar handling of default type/const parameters in Rust.
1 parent d994610 commit 73a8faf

4 files changed

Lines changed: 40 additions & 21 deletions

File tree

turboshake/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,21 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

88
## 0.7.0 (UNRELEASED)
9+
### Added
10+
- `CTurboShake128` and `CTurboShake256` type aliases generic over domain separator ([#866])
11+
912
### Changed
1013
- Internal implementation by removing unnecessary buffering ([#849])
1114
- `Rate: BlockSizes` generic parameter to `const RATE: usize` ([#849])
15+
- `TurboShake128` and `TurboShake256` type aliases are no longer generic over the domain separator
16+
and use the default value instead ([#866])
1217

1318
### Removed
1419
- Implementations of `BlockSizeUser` ([#856])
1520

1621
[#849]: https://github.com/RustCrypto/hashes/pull/849
1722
[#856]: https://github.com/RustCrypto/hashes/pull/856
23+
[#866]: https://github.com/RustCrypto/hashes/pull/866
1824

1925
## 0.6.0 (2026-04-24)
2026
Note: the crate was transferred to RustCrypto from https://github.com/itzmeanjan/turboshake

turboshake/README.md

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,32 @@ XOF reader from which results of arbitrary length can be read. Note that
1616
these functions do not implement `Digest`, so lower-level traits have to
1717
be imported:
1818

19+
TurboSHAKE supports limited customization using "domain separator" value.
20+
This implementation handles it using the const generic parameter `DS`.
21+
22+
With the default domain separator:
1923
```rust
2024
use turboshake::TurboShake128;
2125
use turboshake::digest::{Update, ExtendableOutput, XofReader};
2226
use hex_literal::hex;
2327

24-
// With the default domain separator.
25-
//
26-
// Note that we have to use `<TurboShake128>` because of
27-
// the inadequate handling of defaults in Rust.
28-
// Alternatively, you could use `let mut hasher: TurboShake128 = Default::default();`
29-
// or `TurboShake128::<DEFAULT_DS>::default()`.
30-
let mut hasher = <TurboShake128>::default();
28+
let mut hasher = TurboShake128::default();
3129
hasher.update(b"abc");
3230
let mut reader = hasher.finalize_xof();
3331
let mut buf = [0u8; 10];
3432
reader.read(&mut buf);
3533
assert_eq!(buf, hex!("dcf1646dfe993a8eb6b7"));
3634
reader.read(&mut buf);
3735
assert_eq!(buf, hex!("82d1faaca6d82416a5dc"));
36+
```
37+
38+
With a custom domain separator:
39+
```rust
40+
use turboshake::CTurboShake128;
41+
use turboshake::digest::{Update, ExtendableOutput, XofReader};
42+
use hex_literal::hex;
3843

39-
// With a custom domain separator
40-
let mut hasher = TurboShake128::<0x10>::default();
44+
let mut hasher = CTurboShake128::<0x10>::default();
4145
hasher.update(b"abc");
4246
let mut reader = hasher.finalize_xof();
4347
let mut buf = [0u8; 10];

turboshake/src/lib.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ impl<const RATE: usize, const DS: u8> Drop for TurboShake<RATE, DS> {
142142
{
143143
use digest::zeroize::Zeroize;
144144
self.state.zeroize();
145-
// self.buffer is zeroized by its `Drop`
145+
self.cursor.zeroize();
146146
}
147147
}
148148
}
@@ -192,22 +192,31 @@ impl<const RATE: usize> Drop for TurboShakeReader<RATE> {
192192
#[cfg(feature = "zeroize")]
193193
impl<const RATE: usize> digest::zeroize::ZeroizeOnDrop for TurboShakeReader<RATE> {}
194194

195-
/// TurboSHAKE128 hasher with domain separator.
196-
pub type TurboShake128<const DS: u8 = DEFAULT_DS> = TurboShake<168, DS>;
197-
/// TurboSHAKE256 hasher with domain separator.
198-
pub type TurboShake256<const DS: u8 = DEFAULT_DS> = TurboShake<136, DS>;
195+
/// TurboSHAKE128 hasher with a custom domain separator.
196+
///
197+
/// Domain separator `DS` MUST be in the range `0x01..=0x7f`.
198+
pub type CTurboShake128<const DS: u8> = TurboShake<168, DS>;
199+
/// TurboSHAKE256 hasher with a custom domain separator.
200+
///
201+
/// Domain separator `DS` MUST be in the range `0x01..=0x7f`.
202+
pub type CTurboShake256<const DS: u8> = TurboShake<136, DS>;
203+
204+
/// TurboSHAKE128 hasher with the default domain separator.
205+
pub type TurboShake128 = CTurboShake128<DEFAULT_DS>;
206+
/// TurboSHAKE256 hasher with the default domain separator.
207+
pub type TurboShake256 = CTurboShake256<DEFAULT_DS>;
199208

200209
/// TurboSHAKE128 XOF reader.
201210
pub type TurboShake128Reader = TurboShakeReader<168>;
202211
/// TurboSHAKE256 XOF reader.
203212
pub type TurboShake256Reader = TurboShakeReader<136>;
204213

205-
impl<const DS: u8> CollisionResistance for TurboShake128<DS> {
214+
impl<const DS: u8> CollisionResistance for CTurboShake128<DS> {
206215
// https://www.ietf.org/archive/id/draft-irtf-cfrg-kangarootwelve-17.html#section-7-7
207216
type CollisionResistance = U16;
208217
}
209218

210-
impl<const DS: u8> CollisionResistance for TurboShake256<DS> {
219+
impl<const DS: u8> CollisionResistance for CTurboShake256<DS> {
211220
// https://www.ietf.org/archive/id/draft-irtf-cfrg-kangarootwelve-17.html#section-7-8
212221
type CollisionResistance = U32;
213222
}

turboshake/tests/turboshake.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use core::fmt::Debug;
22
use digest::ExtendableOutput;
3-
use turboshake::{TurboShake128, TurboShake256};
3+
use turboshake::{CTurboShake128, CTurboShake256};
44

55
#[derive(Debug, Clone, Copy)]
66
pub struct TestVector {
@@ -104,25 +104,25 @@ macro_rules! new_test {
104104
new_test!(
105105
turboshake128_6,
106106
"turboshake128_6",
107-
TurboShake128<6>,
107+
CTurboShake128<6>,
108108
turbo_shake_test,
109109
);
110110
new_test!(
111111
turboshake128_7,
112112
"turboshake128_7",
113-
TurboShake128<7>,
113+
CTurboShake128<7>,
114114
turbo_shake_test,
115115
);
116116
new_test!(
117117
turboshake256_6,
118118
"turboshake256_6",
119-
TurboShake256<6>,
119+
CTurboShake256<6>,
120120
turbo_shake_test,
121121
);
122122

123123
new_test!(
124124
turboshake256_7,
125125
"turboshake256_7",
126-
TurboShake256<7>,
126+
CTurboShake256<7>,
127127
turbo_shake_test,
128128
);

0 commit comments

Comments
 (0)