From ba6425947fa9b9a4f43c4977cb3b4bf477d4ce1e Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Fri, 7 Feb 2025 07:05:30 +1100 Subject: [PATCH] hashes: Use associated cost for pre-tagging Instead of requiring users of the `Tag` trait to implement the `engine` method we can have an associated const and provide an `engine` method with a default implementation. Use an associated const for the pre-tagged hash engine. Fro now keep the `engine` trait method but have a default impl that returns the const. We will remove it as a separate patch to assist review. --- hashes/src/macros.rs | 6 +----- hashes/src/sha256t/mod.rs | 13 +++++++------ hashes/tests/regression.rs | 5 +---- 3 files changed, 9 insertions(+), 15 deletions(-) diff --git a/hashes/src/macros.rs b/hashes/src/macros.rs index dc134577a..12ca01520 100644 --- a/hashes/src/macros.rs +++ b/hashes/src/macros.rs @@ -33,11 +33,7 @@ macro_rules! sha256t_tag { $crate::sha256t_tag_struct!($tag_vis, $tag, stringify!($hash_name), $(#[$($tag_attr)*])*); impl $crate::sha256t::Tag for $tag { - #[inline] - fn engine() -> $crate::sha256::HashEngine { - const MIDSTATE: $crate::sha256::Midstate = $crate::sha256t_tag_constructor!($constructor, $($tag_value)+); - $crate::sha256::HashEngine::from_midstate(MIDSTATE) - } + const MIDSTATE: $crate::sha256::Midstate = $crate::sha256t_tag_constructor!($constructor, $($tag_value)+); } } } diff --git a/hashes/src/sha256t/mod.rs b/hashes/src/sha256t/mod.rs index 2e7ddf89d..4a4ce84ee 100644 --- a/hashes/src/sha256t/mod.rs +++ b/hashes/src/sha256t/mod.rs @@ -11,8 +11,13 @@ type HashEngine = sha256::HashEngine; /// Trait representing a tag that can be used as a context for SHA256t hashes. pub trait Tag { + /// The [`Midstate`] after pre-tagging the hash engine. + const MIDSTATE: sha256::Midstate; + /// Returns a hash engine that is pre-tagged and is ready to be used for the data. - fn engine() -> sha256::HashEngine; + fn engine() -> sha256::HashEngine { + sha256::HashEngine::from_midstate(Self::MIDSTATE) + } } /// Output of the SHA256t hash function. @@ -190,11 +195,7 @@ mod tests { pub struct TestHashTag; impl sha256t::Tag for TestHashTag { - fn engine() -> sha256::HashEngine { - // The TapRoot TapLeaf midstate. - let midstate = sha256::Midstate::new(TEST_MIDSTATE, 64); - sha256::HashEngine::from_midstate(midstate) - } + const MIDSTATE: sha256::Midstate = sha256::Midstate::new(TEST_MIDSTATE, 64); } // We support manually implementing `Tag` and creating a tagged hash from it. diff --git a/hashes/tests/regression.rs b/hashes/tests/regression.rs index 0b6856794..a4ffddc2f 100644 --- a/hashes/tests/regression.rs +++ b/hashes/tests/regression.rs @@ -40,10 +40,7 @@ impl_regression_test! { pub struct RegHashTag; impl sha256t::Tag for RegHashTag { - fn engine() -> sha256::HashEngine { - let midstate = sha256::Midstate::new([0xab; 32], 64); - sha256::HashEngine::from_midstate(midstate) - } + const MIDSTATE: sha256::Midstate = sha256::Midstate::new([0xab; 32], 64); } type RegHash = sha256t::Hash;