Skip to content

Commit e499ef4

Browse files
authored
jh: implement SerializableState (#889)
1 parent 2b23923 commit e499ef4

8 files changed

Lines changed: 45 additions & 6 deletions

File tree

‎jh/CHANGELOG.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
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

8+
## 0.2.1 (UNRELEASED)
9+
### Changed
10+
- Implementation of the `SerializableState` trait ([#889])
11+
12+
[#889]: https://github.com/RustCrypto/hashes/pull/889
13+
814
## 0.2.0 (2026-03-27)
915
### Added
1016
- `alloc` crate feature ([#678])

‎jh/src/block_api.rs‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ use digest::{
66
AlgorithmName, Block, BlockSizeUser, Buffer, BufferKindUser, Eager, OutputSizeUser,
77
TruncSide, UpdateCore, VariableOutputCore,
88
},
9-
typenum::{U64, Unsigned},
9+
common::hazmat::{DeserializeStateError, SerializableState, SerializedState},
10+
typenum::{U64, U128, U136, Unsigned},
1011
};
1112

1213
use crate::consts;
@@ -89,6 +90,33 @@ impl fmt::Debug for JhCore {
8990
}
9091
}
9192

93+
impl SerializableState for JhCore {
94+
type SerializedStateSize = U136;
95+
96+
#[inline]
97+
fn serialize(&self) -> SerializedState<Self> {
98+
let mut serialized_state = SerializedState::<Self>::default();
99+
let (state_dst, block_len_dst) = serialized_state.split_at_mut(128);
100+
101+
state_dst.copy_from_slice(self.state.finalize());
102+
block_len_dst.copy_from_slice(&self.block_len.to_le_bytes());
103+
104+
serialized_state
105+
}
106+
107+
#[inline]
108+
fn deserialize(
109+
serialized_state: &SerializedState<Self>,
110+
) -> Result<Self, DeserializeStateError> {
111+
let (serialized_state, serialized_block_len) = serialized_state.split::<U128>();
112+
113+
Ok(Self {
114+
state: Compressor::new(serialized_state.0),
115+
block_len: u64::from_le_bytes(serialized_block_len.0),
116+
})
117+
}
118+
}
119+
92120
impl Drop for JhCore {
93121
fn drop(&mut self) {
94122
#[cfg(feature = "zeroize")]

‎jh/src/lib.rs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,20 @@ use digest::{
2222
digest::buffer_fixed!(
2323
/// JH-224 hasher.
2424
pub struct Jh224(CtOutWrapper<block_api::JhCore, U28>);
25-
impl: BaseFixedTraits AlgorithmName Default Clone HashMarker Reset FixedOutputReset;
25+
impl: FixedHashTraits;
2626
);
2727
digest::buffer_fixed!(
2828
/// JH-256 hasher.
2929
pub struct Jh256(CtOutWrapper<block_api::JhCore, U32>);
30-
impl: BaseFixedTraits AlgorithmName Default Clone HashMarker Reset FixedOutputReset;
30+
impl: FixedHashTraits;
3131
);
3232
digest::buffer_fixed!(
3333
/// JH-384 hasher.
3434
pub struct Jh384(CtOutWrapper<block_api::JhCore, U48>);
35-
impl: BaseFixedTraits AlgorithmName Default Clone HashMarker Reset FixedOutputReset;
35+
impl: FixedHashTraits;
3636
);
3737
digest::buffer_fixed!(
3838
/// JH-512 hasher.
3939
pub struct Jh512(CtOutWrapper<block_api::JhCore, U64>);
40-
impl: BaseFixedTraits AlgorithmName Default Clone HashMarker Reset FixedOutputReset;
40+
impl: FixedHashTraits;
4141
);
200 Bytes
Binary file not shown.
200 Bytes
Binary file not shown.
200 Bytes
Binary file not shown.
200 Bytes
Binary file not shown.

‎jh/tests/mod.rs‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use digest::{dev::fixed_test, new_test};
1+
use digest::{dev::fixed_test, hash_serialization_test, new_test};
22

33
new_test!(jh224_long_kat, jh::Jh224, fixed_test);
44
new_test!(jh256_long_kat, jh::Jh256, fixed_test);
@@ -9,3 +9,8 @@ new_test!(jh224_short_kat, jh::Jh224, fixed_test);
99
new_test!(jh256_short_kat, jh::Jh256, fixed_test);
1010
new_test!(jh384_short_kat, jh::Jh384, fixed_test);
1111
new_test!(jh512_short_kat, jh::Jh512, fixed_test);
12+
13+
hash_serialization_test!(jh224_serialization, jh::Jh224);
14+
hash_serialization_test!(jh256_serialization, jh::Jh256);
15+
hash_serialization_test!(jh384_serialization, jh::Jh384);
16+
hash_serialization_test!(jh512_serialization, jh::Jh512);

0 commit comments

Comments
 (0)