From 16f8b2939427cb64d7c615110d0776c48ea9084a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Mon, 11 Aug 2025 15:36:50 +0300 Subject: [PATCH 01/14] digest: improve serialization of eager hashes --- Cargo.lock | 2 +- Cargo.toml | 3 ++- digest/src/buffer_macros/fixed.rs | 40 ++++++++++--------------------- digest/tests/dummy_fixed.rs | 7 ++++++ 4 files changed, 22 insertions(+), 30 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a43162635..12b0d2568 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -76,7 +76,7 @@ checksum = "4a859067dcb257cb2ae028cb821399b55140b76fb8b2a360e052fe109019db43" [[package]] name = "block-buffer" version = "0.11.0-rc.4" -source = "git+https://github.com/RustCrypto/utils.git#6fd0e8ddc827d7e7d9242e130f8944fc9ca328cf" +source = "git+https://github.com/RustCrypto/utils?branch=block-buffer%2Fserialization#bf8ff4b38f2a44087a12792146dee9c23bd6bb3f" dependencies = [ "hybrid-array", "zeroize", diff --git a/Cargo.toml b/Cargo.toml index 91b4f49f2..830092604 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,4 +18,5 @@ digest = { path = "digest" } signature = { path = "signature" } # https://github.com/RustCrypto/utils/pull/1192 -block-buffer = { git = "https://github.com/RustCrypto/utils.git" } +# https://github.com/RustCrypto/utils/pull/1200 +block-buffer = { git = "https://github.com/RustCrypto/utils", branch = "block-buffer/serialization" } diff --git a/digest/src/buffer_macros/fixed.rs b/digest/src/buffer_macros/fixed.rs index f1e73b641..7453eae54 100644 --- a/digest/src/buffer_macros/fixed.rs +++ b/digest/src/buffer_macros/fixed.rs @@ -429,49 +429,33 @@ macro_rules! buffer_fixed { impl$(< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)? $crate::crypto_common::hazmat::SerializableState for $name$(< $( $lt ),+ >)? { type SerializedStateSize = $crate::typenum::Sum< <$core_ty as $crate::crypto_common::hazmat::SerializableState>::SerializedStateSize, - $crate::typenum::Add1< - <$core_ty as $crate::block_api::BlockSizeUser>::BlockSize + $crate::block_buffer::SerializedBufferSize< + <$core_ty as $crate::block_api::BlockSizeUser>::BlockSize, + <$core_ty as $crate::block_api::BufferKindUser>::BufferKind, > >; #[inline] fn serialize(&self) -> $crate::crypto_common::hazmat::SerializedState { - use $crate::{ - array::Array, - consts::U1, - block_buffer::BlockBuffer, - crypto_common::hazmat::SerializableState, - }; - let serialized_core = self.core.serialize(); - let pos = u8::try_from(self.buffer.get_pos()).unwrap(); - let serialized_pos: Array = Array([pos]); - let serialized_data = self.buffer.clone().pad_with_zeros(); - - serialized_core - .concat(serialized_pos) - .concat(serialized_data) + let serialized_buf = self.buffer.serialize(); + serialized_core.concat(serialized_buf) } #[inline] fn deserialize( serialized_state: &$crate::crypto_common::hazmat::SerializedState, ) -> Result { - use $crate::{ - block_buffer::BlockBuffer, - consts::U1, - crypto_common::hazmat::{SerializableState, DeserializeStateError}, - }; + use $crate::crypto_common::hazmat::{SerializableState, DeserializeStateError}; - let (serialized_core, remaining_buffer) = serialized_state + let (serialized_core, serialized_buf) = serialized_state .split_ref::<<$core_ty as SerializableState>::SerializedStateSize>(); - let (serialized_pos, serialized_data) = remaining_buffer.split_ref::(); - Ok(Self { - core: <$core_ty as SerializableState>::deserialize(serialized_core)?, - buffer: BlockBuffer::try_new(&serialized_data[..serialized_pos[0].into()]) - .map_err(|_| DeserializeStateError)?, - }) + let core = <$core_ty as SerializableState>::deserialize(serialized_core)?; + let buffer = $crate::block_buffer::BlockBuffer::deserialize(serialized_buf) + .map_err(|_| DeserializeStateError)?; + + Ok(Self { core, buffer }) } } diff --git a/digest/tests/dummy_fixed.rs b/digest/tests/dummy_fixed.rs index cce734f59..86cbe8fc1 100644 --- a/digest/tests/dummy_fixed.rs +++ b/digest/tests/dummy_fixed.rs @@ -111,6 +111,13 @@ digest::buffer_fixed!( impl: FixedHashTraits; ); +#[cfg(feature = "dev")] +digest::hash_serialization_test!( + fixed_hash_serialization_test, + FixedHashWithOidSer, + [19, 19, 19, 19, 19, 19, 19, 19, 1, 19, 0, 0, 0, 0, 0, 0] +); + #[cfg(feature = "zeroize")] /// check for `ZeroizeOnDrop` implementations const _: () = { From a8880a5f45cc31a94b90d65dff45f65cbe2efc6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Mon, 11 Aug 2025 15:51:50 +0300 Subject: [PATCH 02/14] Update Cargo.lock --- Cargo.lock | 57 +++++++++++++++++------------------------------------- 1 file changed, 18 insertions(+), 39 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 12b0d2568..e40014267 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -76,7 +76,7 @@ checksum = "4a859067dcb257cb2ae028cb821399b55140b76fb8b2a360e052fe109019db43" [[package]] name = "block-buffer" version = "0.11.0-rc.4" -source = "git+https://github.com/RustCrypto/utils?branch=block-buffer%2Fserialization#bf8ff4b38f2a44087a12792146dee9c23bd6bb3f" +source = "git+https://github.com/RustCrypto/utils?branch=block-buffer%2Fserialization#62a60ecfd053698fb686ebdceb503ccd92fe0a8e" dependencies = [ "hybrid-array", "zeroize", @@ -99,9 +99,9 @@ checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" [[package]] name = "bytes" -version = "1.10.0" +version = "1.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f61dac84819c6588b558454b194026eb1f09c293b9036ae9b159e74e73ab6cf9" +checksum = "d71b6127be86fdcfddb610f7182ac57211d4b18a3e9c82eb2d17662f2227ad6a" [[package]] name = "cfg-if" @@ -171,9 +171,9 @@ dependencies = [ [[package]] name = "der" -version = "0.8.0-rc.7" +version = "0.8.0-rc.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2fe0a4fafae25053c19a03fefe040607bda956b4941d692ed9fb9d3c18a3193" +checksum = "7050e8041c28720851f7db83183195b6acf375bb7bb28e3b86f0fe6cbd69459d" dependencies = [ "const-oid", "pem-rfc7468", @@ -361,9 +361,9 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.174" +version = "0.2.175" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1171693293099992e19cddea4e8b849964e9846f4acee11b3948bcc337be8776" +checksum = "6a82ae493e598baaea5209805c49bbf2ea7de956d50d7da0da1164f9c6d28543" [[package]] name = "memchr" @@ -430,18 +430,18 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.93" +version = "1.0.96" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "60946a68e5f9d28b0dc1c21bb8a97ee7d018a8b322fa57838ba31cc878e22d99" +checksum = "beef09f85ae72cea1ef96ba6870c51e6382ebfa4f0e85b643459331f3daa5be0" dependencies = [ "unicode-ident", ] [[package]] name = "quote" -version = "1.0.38" +version = "1.0.40" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0e4dccaaaf89514f546c693ddc140f729f958c247918a13380cccc6078391acc" +checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d" dependencies = [ "proc-macro2", ] @@ -460,12 +460,11 @@ checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09" [[package]] name = "rand_core" -version = "0.9.2" +version = "0.9.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a509b1a2ffbe92afab0e55c8fd99dea1c280e8171bd2d88682bb20bc41cbc2c" +checksum = "99d9a13982dcf210057a8a78572b2217b667c3beacbf3a0d8b454f6f82837d38" dependencies = [ "getrandom", - "zerocopy", ] [[package]] @@ -505,14 +504,14 @@ checksum = "5b0276cf7f2c73365f7157c8123c21cd9a50fbbd844757af28ca1f5925fc2a00" dependencies = [ "proc-macro2", "quote", - "syn 2.0.98", + "syn 2.0.104", ] [[package]] name = "serde_json" -version = "1.0.139" +version = "1.0.142" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "44f86c3acccc9c65b153fe1b85a3be07fe5515274ec9f0653b4a0875731c72a6" +checksum = "030fedb782600dcbd6f02d479bf0d817ac3bb40d644745b769d6a96bc3afc5a7" dependencies = [ "itoa", "memchr", @@ -594,9 +593,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.98" +version = "2.0.104" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "36147f1a48ae0ec2b5b3bc5b537d267457555a10dc06f3dbc8cb11ba3006d3b1" +checksum = "17b6f705963418cdb9927482fa304bc562ece2fdd4f616084c50b7023b435a40" dependencies = [ "proc-macro2", "quote", @@ -656,26 +655,6 @@ dependencies = [ "tap", ] -[[package]] -name = "zerocopy" -version = "0.8.26" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1039dd0d3c310cf05de012d8a39ff557cb0d23087fd44cad61df08fc31907a2f" -dependencies = [ - "zerocopy-derive", -] - -[[package]] -name = "zerocopy-derive" -version = "0.8.26" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9ecf5b4cc5364572d7f4c329661bcc82724222973f2cab6f050a4e5c22f75181" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.98", -] - [[package]] name = "zeroize" version = "1.8.1" From d4504a08fc0a73321c3fe67fca0d56d7ac168cf2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Mon, 11 Aug 2025 15:56:16 +0300 Subject: [PATCH 03/14] tweak `SerializableState` impl --- digest/src/buffer_macros/fixed.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/digest/src/buffer_macros/fixed.rs b/digest/src/buffer_macros/fixed.rs index 7453eae54..e84376797 100644 --- a/digest/src/buffer_macros/fixed.rs +++ b/digest/src/buffer_macros/fixed.rs @@ -448,10 +448,10 @@ macro_rules! buffer_fixed { ) -> Result { use $crate::crypto_common::hazmat::{SerializableState, DeserializeStateError}; - let (serialized_core, serialized_buf) = serialized_state - .split_ref::<<$core_ty as SerializableState>::SerializedStateSize>(); + type StateSize = <$core_ty as SerializableState>::SerializedStateSize; + let (serialized_core, serialized_buf) = serialized_state.split_ref::(); - let core = <$core_ty as SerializableState>::deserialize(serialized_core)?; + let core = SerializableState::deserialize(serialized_core)?; let buffer = $crate::block_buffer::BlockBuffer::deserialize(serialized_buf) .map_err(|_| DeserializeStateError)?; From 2dfe98bef202f3812ae05bdc6a2f4b8c672d800d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Mon, 11 Aug 2025 16:04:06 +0300 Subject: [PATCH 04/14] tweak `hash_serialization_test` --- digest/src/dev.rs | 5 +++-- digest/tests/data/fixed_hash_serialization.bin | Bin 0 -> 16 bytes digest/tests/dummy_fixed.rs | 4 ++-- 3 files changed, 5 insertions(+), 4 deletions(-) create mode 100644 digest/tests/data/fixed_hash_serialization.bin diff --git a/digest/src/dev.rs b/digest/src/dev.rs index b3d0203dc..c93a57035 100644 --- a/digest/src/dev.rs +++ b/digest/src/dev.rs @@ -40,7 +40,7 @@ macro_rules! new_test { /// Define hash function serialization test #[macro_export] macro_rules! hash_serialization_test { - ($name:ident, $hasher:ty, $expected_serialized_state:expr) => { + ($name:ident, $hasher:ty, $expected_serialized_state:expr $(,)?) => { #[test] fn $name() { use digest::{ @@ -54,7 +54,8 @@ macro_rules! hash_serialization_test { h.update(&[0x13; <$hasher as BlockSizeUser>::BlockSize::USIZE + 1]); let serialized_state = h.serialize(); - assert_eq!(serialized_state.as_slice(), $expected_serialized_state); + let expected = include_bytes!(concat!("data/", $expected_serialized_state, ".bin")); + assert_eq!(serialized_state.as_slice(), expected); let mut h = <$hasher>::deserialize(&serialized_state).unwrap(); diff --git a/digest/tests/data/fixed_hash_serialization.bin b/digest/tests/data/fixed_hash_serialization.bin new file mode 100644 index 0000000000000000000000000000000000000000..2a034968a1c142c68888ae9fbf5600cd01a655a4 GIT binary patch literal 16 PcmWd@h5$xk1~32s2nhhK literal 0 HcmV?d00001 diff --git a/digest/tests/dummy_fixed.rs b/digest/tests/dummy_fixed.rs index 86cbe8fc1..e0561552a 100644 --- a/digest/tests/dummy_fixed.rs +++ b/digest/tests/dummy_fixed.rs @@ -114,8 +114,8 @@ digest::buffer_fixed!( #[cfg(feature = "dev")] digest::hash_serialization_test!( fixed_hash_serialization_test, - FixedHashWithOidSer, - [19, 19, 19, 19, 19, 19, 19, 19, 1, 19, 0, 0, 0, 0, 0, 0] + FixedHashWithSer, + "fixed_hash_serialization", ); #[cfg(feature = "zeroize")] From b7e96561831aa8d830d57984fe50086a7ea1efa2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Mon, 11 Aug 2025 16:05:48 +0300 Subject: [PATCH 05/14] rename `hash_serialization_test` arg --- digest/src/dev.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/digest/src/dev.rs b/digest/src/dev.rs index c93a57035..cd1842ac5 100644 --- a/digest/src/dev.rs +++ b/digest/src/dev.rs @@ -40,7 +40,7 @@ macro_rules! new_test { /// Define hash function serialization test #[macro_export] macro_rules! hash_serialization_test { - ($name:ident, $hasher:ty, $expected_serialized_state:expr $(,)?) => { + ($name:ident, $hasher:ty, $expected_path:expr $(,)?) => { #[test] fn $name() { use digest::{ @@ -54,7 +54,7 @@ macro_rules! hash_serialization_test { h.update(&[0x13; <$hasher as BlockSizeUser>::BlockSize::USIZE + 1]); let serialized_state = h.serialize(); - let expected = include_bytes!(concat!("data/", $expected_serialized_state, ".bin")); + let expected = include_bytes!(concat!("data/", $expected_path, ".bin")); assert_eq!(serialized_state.as_slice(), expected); let mut h = <$hasher>::deserialize(&serialized_state).unwrap(); From 4b26e2f0efd361a0bc899ecfbe40092489e001b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Mon, 11 Aug 2025 18:10:43 +0300 Subject: [PATCH 06/14] remove use of type alias --- digest/src/buffer_macros/fixed.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/digest/src/buffer_macros/fixed.rs b/digest/src/buffer_macros/fixed.rs index e84376797..407581bb3 100644 --- a/digest/src/buffer_macros/fixed.rs +++ b/digest/src/buffer_macros/fixed.rs @@ -448,8 +448,8 @@ macro_rules! buffer_fixed { ) -> Result { use $crate::crypto_common::hazmat::{SerializableState, DeserializeStateError}; - type StateSize = <$core_ty as SerializableState>::SerializedStateSize; - let (serialized_core, serialized_buf) = serialized_state.split_ref::(); + let (serialized_core, serialized_buf) = serialized_state + .split_ref::<<$core_ty as SerializableState>::SerializedStateSize>(); let core = SerializableState::deserialize(serialized_core)?; let buffer = $crate::block_buffer::BlockBuffer::deserialize(serialized_buf) From 6ccf7cf6f1b2ece25985ad421f9953e7e4a0788f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Mon, 11 Aug 2025 18:14:48 +0300 Subject: [PATCH 07/14] Update Cargo.lock --- Cargo.lock | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 520bc8e24..73e0096b2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -70,12 +70,12 @@ dependencies = [ [[package]] name = "blobby" version = "0.4.0-pre.0" -source = "git+https://github.com/RustCrypto/utils#1227836d92c56dce5fddb3ed36003277f053267b" +source = "git+https://github.com/RustCrypto/utils#0e7e6e662608a3a862a4beb930b8ffedbea6563d" [[package]] name = "block-buffer" version = "0.11.0-rc.4" -source = "git+https://github.com/RustCrypto/utils?branch=block-buffer%2Fserialization#62a60ecfd053698fb686ebdceb503ccd92fe0a8e" +source = "git+https://github.com/RustCrypto/utils?branch=block-buffer%2Fserialization#f57451f62fb3f87c5ee3f71fe4b1afdf3752abf5" dependencies = [ "hybrid-array", "zeroize", From 71236a0514bd72d3591513283d4a976af494d4cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Mon, 11 Aug 2025 18:15:09 +0300 Subject: [PATCH 08/14] Switch args order in `hash_serialization_test!` --- digest/src/dev.rs | 2 +- digest/tests/dummy_fixed.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/digest/src/dev.rs b/digest/src/dev.rs index 3eb2370af..ec6f0b17d 100644 --- a/digest/src/dev.rs +++ b/digest/src/dev.rs @@ -54,7 +54,7 @@ macro_rules! new_test { /// Define hash function serialization test #[macro_export] macro_rules! hash_serialization_test { - ($name:ident, $hasher:ty, $expected_path:expr $(,)?) => { + ($name:ident, $expected_path:expr, $hasher:ty $(,)?) => { #[test] fn $name() { use digest::{ diff --git a/digest/tests/dummy_fixed.rs b/digest/tests/dummy_fixed.rs index e0561552a..482e05fb3 100644 --- a/digest/tests/dummy_fixed.rs +++ b/digest/tests/dummy_fixed.rs @@ -113,9 +113,9 @@ digest::buffer_fixed!( #[cfg(feature = "dev")] digest::hash_serialization_test!( - fixed_hash_serialization_test, - FixedHashWithSer, + fixed_hash_serialization, "fixed_hash_serialization", + FixedHashWithSer, ); #[cfg(feature = "zeroize")] From e8d0ca1f4cf126c0ed073cf5288c756d23cba795 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Mon, 11 Aug 2025 18:26:23 +0300 Subject: [PATCH 09/14] Do not include output size into `SerializedState` for `CtOutWrapper` --- digest/src/block_api/ct_variable.rs | 38 ++++++----------------------- 1 file changed, 7 insertions(+), 31 deletions(-) diff --git a/digest/src/block_api/ct_variable.rs b/digest/src/block_api/ct_variable.rs index 775dd3c34..0e6459932 100644 --- a/digest/src/block_api/ct_variable.rs +++ b/digest/src/block_api/ct_variable.rs @@ -5,16 +5,12 @@ use super::{ #[cfg(feature = "mac")] use crate::MacMarker; use crate::{CollisionResistance, CustomizedInit, HashMarker, VarOutputCustomized}; -use core::{ - fmt, - marker::PhantomData, - ops::{Add, Sub}, -}; +use core::{fmt, marker::PhantomData}; use crypto_common::{ Block, BlockSizeUser, OutputSizeUser, array::{Array, ArraySize}, - hazmat::{DeserializeStateError, SerializableState, SerializedState, SubSerializedStateSize}, - typenum::{IsLess, IsLessOrEqual, Le, NonZero, Sum, True, U1, U256}, + hazmat::{DeserializeStateError, SerializableState, SerializedState}, + typenum::{IsLessOrEqual, True}, }; /// Wrapper around [`VariableOutputCore`] which selects output size at compile time. @@ -177,41 +173,21 @@ where } } -type CtVariableCoreWrapperSerializedStateSize = - Sum<::SerializedStateSize, U1>; - impl SerializableState for CtOutWrapper where T: VariableOutputCore + SerializableState, OutSize: ArraySize + IsLessOrEqual, - T::BlockSize: IsLess, - Le: NonZero, - T::SerializedStateSize: Add, - CtVariableCoreWrapperSerializedStateSize: Sub + ArraySize, - SubSerializedStateSize, T>: ArraySize, { - type SerializedStateSize = CtVariableCoreWrapperSerializedStateSize; + type SerializedStateSize = ::SerializedStateSize; fn serialize(&self) -> SerializedState { - let serialized_inner = self.inner.serialize(); - let serialized_outsize = Array([OutSize::U8]); - - serialized_inner.concat(serialized_outsize) + self.inner.serialize() } fn deserialize( serialized_state: &SerializedState, ) -> Result { - let (serialized_inner, serialized_outsize) = - serialized_state.split_ref::(); - - if serialized_outsize[0] != OutSize::U8 { - return Err(DeserializeStateError); - } - - Ok(Self { - inner: T::deserialize(serialized_inner)?, - _out: PhantomData, - }) + let _out = PhantomData; + T::deserialize(serialized_state).map(|inner| Self { inner, _out }) } } From 0188b1227a8c10c6b973a59529ee84745ea0a03f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Mon, 11 Aug 2025 18:41:38 +0300 Subject: [PATCH 10/14] Change `SerializableState` impl in `buffer_ct_variable!` --- digest/src/buffer_macros/variable_ct.rs | 50 +++++++------------------ 1 file changed, 14 insertions(+), 36 deletions(-) diff --git a/digest/src/buffer_macros/variable_ct.rs b/digest/src/buffer_macros/variable_ct.rs index a767a1ca0..b02b69a57 100644 --- a/digest/src/buffer_macros/variable_ct.rs +++ b/digest/src/buffer_macros/variable_ct.rs @@ -176,54 +176,32 @@ macro_rules! buffer_ct_variable { where $out_size: $crate::array::ArraySize + $crate::typenum::IsLessOrEqual<$max_size, Output = $crate::typenum::True>, { - type SerializedStateSize = $crate::typenum::Add1<$crate::typenum::Sum< - < - $crate::block_api::CtOutWrapper<$core_ty, $out_size> - as $crate::crypto_common::hazmat::SerializableState - >::SerializedStateSize, - <$core_ty as $crate::block_api::BlockSizeUser>::BlockSize, - >>; + type SerializedStateSize = $crate::typenum::Sum< + <$core_ty as $crate::crypto_common::hazmat::SerializableState>::SerializedStateSize, + $crate::block_buffer::SerializedBufferSize< + <$core_ty as $crate::block_api::BlockSizeUser>::BlockSize, + <$core_ty as $crate::block_api::BufferKindUser>::BufferKind, + > + >; #[inline] fn serialize(&self) -> $crate::crypto_common::hazmat::SerializedState { - use $crate::{ - array::Array, - consts::U1, - block_buffer::BlockBuffer, - crypto_common::hazmat::SerializableState, - }; - let serialized_core = self.core.serialize(); - let pos = u8::try_from(self.buffer.get_pos()).unwrap(); - let serialized_pos: Array = Array([pos]); - let serialized_data = self.buffer.clone().pad_with_zeros(); - - serialized_core - .concat(serialized_pos) - .concat(serialized_data) + let serialized_buf = self.buffer.serialize(); + serialized_core.concat(serialized_buf) } #[inline] fn deserialize( serialized_state: &$crate::crypto_common::hazmat::SerializedState, ) -> Result { - use $crate::{ - block_buffer::BlockBuffer, - consts::U1, - block_api::CtOutWrapper, - crypto_common::hazmat::{SerializableState, DeserializeStateError}, - }; - - let (serialized_core, remaining_buffer) = serialized_state - .split_ref::<< - CtOutWrapper<$core_ty, $out_size> - as SerializableState - >::SerializedStateSize>(); - let (serialized_pos, serialized_data) = remaining_buffer.split_ref::(); + use $crate::crypto_common::hazmat::{SerializableState, DeserializeStateError}; + + let (serialized_core, serialized_buf) = serialized_state + .split_ref::<<$core_ty as SerializableState>::SerializedStateSize>(); let core = SerializableState::deserialize(serialized_core)?; - let pos = usize::from(serialized_pos[0]); - let buffer = BlockBuffer::try_new(&serialized_data[..pos]) + let buffer = $crate::block_buffer::BlockBuffer::deserialize(serialized_buf) .map_err(|_| DeserializeStateError)?; Ok(Self { core, buffer }) From e80fb7cb7cd49bcc1b0fdba7f915bdcc5a9e7e23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Mon, 11 Aug 2025 18:47:20 +0300 Subject: [PATCH 11/14] Change `SerializableState` impl in `buffer_xof!` --- digest/src/buffer_macros/xof.rs | 40 ++++++++++----------------------- 1 file changed, 12 insertions(+), 28 deletions(-) diff --git a/digest/src/buffer_macros/xof.rs b/digest/src/buffer_macros/xof.rs index 8687c18b4..c8cbe72c7 100644 --- a/digest/src/buffer_macros/xof.rs +++ b/digest/src/buffer_macros/xof.rs @@ -284,49 +284,33 @@ macro_rules! buffer_xof { impl $crate::crypto_common::hazmat::SerializableState for $name { type SerializedStateSize = $crate::typenum::Sum< <$core_ty as $crate::crypto_common::hazmat::SerializableState>::SerializedStateSize, - $crate::typenum::Add1< - <$core_ty as $crate::block_api::BlockSizeUser>::BlockSize + $crate::block_buffer::SerializedBufferSize< + <$core_ty as $crate::block_api::BlockSizeUser>::BlockSize, + <$core_ty as $crate::block_api::BufferKindUser>::BufferKind, > >; #[inline] fn serialize(&self) -> $crate::crypto_common::hazmat::SerializedState { - use $crate::{ - array::Array, - consts::U1, - block_buffer::BlockBuffer, - crypto_common::hazmat::SerializableState, - }; - let serialized_core = self.core.serialize(); - let pos = u8::try_from(self.buffer.get_pos()).unwrap(); - let serialized_pos: Array = Array([pos]); - let serialized_data = self.buffer.clone().pad_with_zeros(); - - serialized_core - .concat(serialized_pos) - .concat(serialized_data) + let serialized_buf = self.buffer.serialize(); + serialized_core.concat(serialized_buf) } #[inline] fn deserialize( serialized_state: &$crate::crypto_common::hazmat::SerializedState, ) -> Result { - use $crate::{ - block_buffer::BlockBuffer, - consts::U1, - crypto_common::hazmat::{SerializableState, DeserializeStateError}, - }; + use $crate::crypto_common::hazmat::{SerializableState, DeserializeStateError}; - let (serialized_core, remaining_buffer) = serialized_state + let (serialized_core, serialized_buf) = serialized_state .split_ref::<<$core_ty as SerializableState>::SerializedStateSize>(); - let (serialized_pos, serialized_data) = remaining_buffer.split_ref::(); - Ok(Self { - core: <$core_ty as SerializableState>::deserialize(serialized_core)?, - buffer: BlockBuffer::try_new(&serialized_data[..serialized_pos[0].into()]) - .map_err(|_| DeserializeStateError)?, - }) + let core = SerializableState::deserialize(serialized_core)?; + let buffer = $crate::block_buffer::BlockBuffer::deserialize(serialized_buf) + .map_err(|_| DeserializeStateError)?; + + Ok(Self { core, buffer }) } } From 27383b9a01d3689920afec803c4ac922916a0825 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Mon, 11 Aug 2025 18:53:57 +0300 Subject: [PATCH 12/14] use `stringify!` in `hash_serialization_test!` --- digest/src/dev.rs | 4 ++-- digest/tests/dummy_fixed.rs | 6 +----- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/digest/src/dev.rs b/digest/src/dev.rs index ec6f0b17d..abc174f96 100644 --- a/digest/src/dev.rs +++ b/digest/src/dev.rs @@ -54,7 +54,7 @@ macro_rules! new_test { /// Define hash function serialization test #[macro_export] macro_rules! hash_serialization_test { - ($name:ident, $expected_path:expr, $hasher:ty $(,)?) => { + ($name:ident, $hasher:ty $(,)?) => { #[test] fn $name() { use digest::{ @@ -68,7 +68,7 @@ macro_rules! hash_serialization_test { h.update(&[0x13; <$hasher as BlockSizeUser>::BlockSize::USIZE + 1]); let serialized_state = h.serialize(); - let expected = include_bytes!(concat!("data/", $expected_path, ".bin")); + let expected = include_bytes!(concat!("data/", stringify!($name), ".bin")); assert_eq!(serialized_state.as_slice(), expected); let mut h = <$hasher>::deserialize(&serialized_state).unwrap(); diff --git a/digest/tests/dummy_fixed.rs b/digest/tests/dummy_fixed.rs index 482e05fb3..e66b430a0 100644 --- a/digest/tests/dummy_fixed.rs +++ b/digest/tests/dummy_fixed.rs @@ -112,11 +112,7 @@ digest::buffer_fixed!( ); #[cfg(feature = "dev")] -digest::hash_serialization_test!( - fixed_hash_serialization, - "fixed_hash_serialization", - FixedHashWithSer, -); +digest::hash_serialization_test!(fixed_hash_serialization, FixedHashWithSer,); #[cfg(feature = "zeroize")] /// check for `ZeroizeOnDrop` implementations From b739d00503319c5c3f98c7f54e6b1ebeef3dc9c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Wed, 13 Aug 2025 13:36:24 +0300 Subject: [PATCH 13/14] use `block-buffer` from the master branch --- Cargo.lock | 4 ++-- Cargo.toml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 73e0096b2..de2cf1a0b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -70,12 +70,12 @@ dependencies = [ [[package]] name = "blobby" version = "0.4.0-pre.0" -source = "git+https://github.com/RustCrypto/utils#0e7e6e662608a3a862a4beb930b8ffedbea6563d" +source = "git+https://github.com/RustCrypto/utils#f5ac85f6aa02aa39614f62a6a1add9468be67892" [[package]] name = "block-buffer" version = "0.11.0-rc.4" -source = "git+https://github.com/RustCrypto/utils?branch=block-buffer%2Fserialization#f57451f62fb3f87c5ee3f71fe4b1afdf3752abf5" +source = "git+https://github.com/RustCrypto/utils#f5ac85f6aa02aa39614f62a6a1add9468be67892" dependencies = [ "hybrid-array", "zeroize", diff --git a/Cargo.toml b/Cargo.toml index 2db37e753..7f57fc98b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,4 +21,4 @@ signature = { path = "signature" } blobby = { git = "https://github.com/RustCrypto/utils" } # https://github.com/RustCrypto/utils/pull/1192 # https://github.com/RustCrypto/utils/pull/1200 -block-buffer = { git = "https://github.com/RustCrypto/utils", branch = "block-buffer/serialization" } +block-buffer = { git = "https://github.com/RustCrypto/utils" } From a578f5168befde424abb4fe8a52c16a73450c9d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Wed, 13 Aug 2025 13:39:34 +0300 Subject: [PATCH 14/14] Update changelog --- digest/CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/digest/CHANGELOG.md b/digest/CHANGELOG.md index a4fe6b64a..c483c46b2 100644 --- a/digest/CHANGELOG.md +++ b/digest/CHANGELOG.md @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Edition changed to 2024 and MSRV bumped to 1.85 ([#1759]) - `CtVariableCoreWrapper` renamed to `CtOutWrapper` ([#1799]) - Removed the OID type parameter from `CtOutWrapper` ([#1799]) +- Implementations of the `SerializableState` trait ([#1953]) ### Removed - `Mac::new`, `Mac::new_from_slice`, and `Mac::generate_key` methods ([#1173]) @@ -29,6 +30,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 [#1799]: https://github.com/RustCrypto/traits/pull/1799 [#1809]: https://github.com/RustCrypto/traits/pull/1809 [#1820]: https://github.com/RustCrypto/traits/pull/1820 +[#1953]: https://github.com/RustCrypto/traits/pull/1953 ## 0.10.7 (2023-05-19) ### Changed