Remove the Tag::engine method

Now we have an associated const we can do away with the `engine` trait
method all together. Users can call `Hash<FooTag>::engine` instead. This
is better because its an API more similar to the other hash types and
therefor easier to discover and remember.
This commit is contained in:
Tobin C. Harding 2025-02-07 07:14:04 +11:00
parent ba6425947f
commit 5ce8781162
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
2 changed files with 12 additions and 16 deletions

View File

@ -1564,7 +1564,6 @@ mod sealed {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use hashes::sha256; use hashes::sha256;
use hashes::sha256t::Tag;
use hex::{DisplayHex, FromHex}; use hex::{DisplayHex, FromHex};
use secp256k1::VerifyOnly; use secp256k1::VerifyOnly;
@ -1590,12 +1589,12 @@ mod test {
#[test] #[test]
fn midstates() { fn midstates() {
use sha256t::Hash; use sha256t::{Hash, Tag};
// test that engine creation roundtrips // test that engine creation roundtrips
assert_eq!(tag_engine("TapLeaf").midstate(), TapLeafTag::engine().midstate()); assert_eq!(tag_engine("TapLeaf").midstate().unwrap(), TapLeafTag::MIDSTATE);
assert_eq!(tag_engine("TapBranch").midstate(), TapBranchTag::engine().midstate()); assert_eq!(tag_engine("TapBranch").midstate().unwrap(), TapBranchTag::MIDSTATE);
assert_eq!(tag_engine("TapTweak").midstate(), TapTweakTag::engine().midstate()); assert_eq!(tag_engine("TapTweak").midstate().unwrap(), TapTweakTag::MIDSTATE);
assert_eq!(tag_engine("TapSighash").midstate(), TapSighashTag::engine().midstate()); assert_eq!(tag_engine("TapSighash").midstate().unwrap(), TapSighashTag::MIDSTATE);
// check that hash creation is the same as building into the same engine // check that hash creation is the same as building into the same engine
fn empty_hash(tag_name: &str) -> [u8; 32] { fn empty_hash(tag_name: &str) -> [u8; 32] {
@ -1618,19 +1617,19 @@ mod test {
// CHashWriter writer = HasherTapLeaf; // CHashWriter writer = HasherTapLeaf;
// writer.GetSHA256().GetHex() // writer.GetSHA256().GetHex()
assert_eq!( assert_eq!(
Hash::<TapLeafTag>::from_engine(TapLeafTag::engine()).to_string(), Hash::<TapLeafTag>::from_engine(Hash::<TapLeafTag>::engine()).to_string(),
"5212c288a377d1f8164962a5a13429f9ba6a7b84e59776a52c6637df2106facb" "5212c288a377d1f8164962a5a13429f9ba6a7b84e59776a52c6637df2106facb"
); );
assert_eq!( assert_eq!(
Hash::<TapBranchTag>::from_engine(TapBranchTag::engine()).to_string(), Hash::<TapBranchTag>::from_engine(Hash::<TapBranchTag>::engine()).to_string(),
"53c373ec4d6f3c53c1f5fb2ff506dcefe1a0ed74874f93fa93c8214cbe9ffddf" "53c373ec4d6f3c53c1f5fb2ff506dcefe1a0ed74874f93fa93c8214cbe9ffddf"
); );
assert_eq!( assert_eq!(
Hash::<TapTweakTag>::from_engine(TapTweakTag::engine()).to_string(), Hash::<TapTweakTag>::from_engine(Hash::<TapTweakTag>::engine()).to_string(),
"8aa4229474ab0100b2d6f0687f031d1fc9d8eef92a042ad97d279bff456b15e4" "8aa4229474ab0100b2d6f0687f031d1fc9d8eef92a042ad97d279bff456b15e4"
); );
assert_eq!( assert_eq!(
Hash::<TapSighashTag>::from_engine(TapSighashTag::engine()).to_string(), Hash::<TapSighashTag>::from_engine(Hash::<TapSighashTag>::engine()).to_string(),
"dabc11914abcd8072900042a2681e52f8dba99ce82e224f97b5fdb7cd4b9c803" "dabc11914abcd8072900042a2681e52f8dba99ce82e224f97b5fdb7cd4b9c803"
); );

View File

@ -13,11 +13,6 @@ type HashEngine = sha256::HashEngine;
pub trait Tag { pub trait Tag {
/// The [`Midstate`] after pre-tagging the hash engine. /// The [`Midstate`] after pre-tagging the hash engine.
const MIDSTATE: sha256::Midstate; 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 {
sha256::HashEngine::from_midstate(Self::MIDSTATE)
}
} }
/// Output of the SHA256t hash function. /// Output of the SHA256t hash function.
@ -65,7 +60,9 @@ where
pub fn from_engine(e: HashEngine) -> Hash<T> { from_engine(e) } pub fn from_engine(e: HashEngine) -> Hash<T> { from_engine(e) }
/// Constructs a new engine. /// Constructs a new engine.
pub fn engine() -> HashEngine { T::engine() } pub fn engine() -> HashEngine {
sha256::HashEngine::from_midstate(T::MIDSTATE)
}
/// Hashes some bytes. /// Hashes some bytes.
#[allow(clippy::self_named_constructors)] // Hash is a noun and a verb. #[allow(clippy::self_named_constructors)] // Hash is a noun and a verb.