Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions jh/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 0.2.1 (UNRELEASED)
### Changed
- Implementation of the `SerializableState` trait ([#889])

[#889]: https://github.com/RustCrypto/hashes/pull/889

## 0.2.0 (2026-03-27)
### Added
- `alloc` crate feature ([#678])
Expand Down
30 changes: 29 additions & 1 deletion jh/src/block_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use digest::{
AlgorithmName, Block, BlockSizeUser, Buffer, BufferKindUser, Eager, OutputSizeUser,
TruncSide, UpdateCore, VariableOutputCore,
},
typenum::{U64, Unsigned},
common::hazmat::{DeserializeStateError, SerializableState, SerializedState},
typenum::{U64, U128, U136, Unsigned},
};

use crate::consts;
Expand Down Expand Up @@ -89,6 +90,33 @@ impl fmt::Debug for JhCore {
}
}

impl SerializableState for JhCore {
type SerializedStateSize = U136;

#[inline]
fn serialize(&self) -> SerializedState<Self> {
let mut serialized_state = SerializedState::<Self>::default();
let (state_dst, block_len_dst) = serialized_state.split_at_mut(128);

state_dst.copy_from_slice(self.state.finalize());
block_len_dst.copy_from_slice(&self.block_len.to_le_bytes());

serialized_state
}

#[inline]
fn deserialize(
serialized_state: &SerializedState<Self>,
) -> Result<Self, DeserializeStateError> {
let (serialized_state, serialized_block_len) = serialized_state.split::<U128>();

Ok(Self {
state: Compressor::new(serialized_state.0),
block_len: u64::from_le_bytes(serialized_block_len.0),
})
}
}

impl Drop for JhCore {
fn drop(&mut self) {
#[cfg(feature = "zeroize")]
Expand Down
8 changes: 4 additions & 4 deletions jh/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,20 @@ use digest::{
digest::buffer_fixed!(
/// JH-224 hasher.
pub struct Jh224(CtOutWrapper<block_api::JhCore, U28>);
impl: BaseFixedTraits AlgorithmName Default Clone HashMarker Reset FixedOutputReset;
impl: FixedHashTraits;
);
digest::buffer_fixed!(
/// JH-256 hasher.
pub struct Jh256(CtOutWrapper<block_api::JhCore, U32>);
impl: BaseFixedTraits AlgorithmName Default Clone HashMarker Reset FixedOutputReset;
impl: FixedHashTraits;
);
digest::buffer_fixed!(
/// JH-384 hasher.
pub struct Jh384(CtOutWrapper<block_api::JhCore, U48>);
impl: BaseFixedTraits AlgorithmName Default Clone HashMarker Reset FixedOutputReset;
impl: FixedHashTraits;
);
digest::buffer_fixed!(
/// JH-512 hasher.
pub struct Jh512(CtOutWrapper<block_api::JhCore, U64>);
impl: BaseFixedTraits AlgorithmName Default Clone HashMarker Reset FixedOutputReset;
impl: FixedHashTraits;
);
Binary file added jh/tests/data/jh224_serialization.bin
Binary file not shown.
Binary file added jh/tests/data/jh256_serialization.bin
Binary file not shown.
Binary file added jh/tests/data/jh384_serialization.bin
Binary file not shown.
Binary file added jh/tests/data/jh512_serialization.bin
Binary file not shown.
7 changes: 6 additions & 1 deletion jh/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use digest::{dev::fixed_test, new_test};
use digest::{dev::fixed_test, hash_serialization_test, new_test};

new_test!(jh224_long_kat, jh::Jh224, fixed_test);
new_test!(jh256_long_kat, jh::Jh256, fixed_test);
Expand All @@ -9,3 +9,8 @@ new_test!(jh224_short_kat, jh::Jh224, fixed_test);
new_test!(jh256_short_kat, jh::Jh256, fixed_test);
new_test!(jh384_short_kat, jh::Jh384, fixed_test);
new_test!(jh512_short_kat, jh::Jh512, fixed_test);

hash_serialization_test!(jh224_serialization, jh::Jh224);
hash_serialization_test!(jh256_serialization, jh::Jh256);
hash_serialization_test!(jh384_serialization, jh::Jh384);
hash_serialization_test!(jh512_serialization, jh::Jh512);