From 07e8e5d3a67dc175b3a05bdee3bee7e7a81d1f11 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Thu, 11 Jul 2024 13:55:00 +1000 Subject: [PATCH] Stop using macro for Midstate In preparation for changing the `sha256::Midstate` internals stop using the `arr_newtype_fmt_impl` macro and implement the `fmt` traits manually. In doing so, remove the `DISPLAY_BACKWARDS` const but keep the current behaviour of displaying the midstate backwards. --- hashes/src/sha256.rs | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/hashes/src/sha256.rs b/hashes/src/sha256.rs index 56b455c34..97e1e36fd 100644 --- a/hashes/src/sha256.rs +++ b/hashes/src/sha256.rs @@ -6,9 +6,9 @@ use core::arch::x86::*; #[cfg(all(feature = "std", target_arch = "x86_64"))] use core::arch::x86_64::*; -use core::cmp; use core::ops::Index; use core::slice::SliceIndex; +use core::{cmp, fmt}; #[cfg(doc)] use crate::sha256t; @@ -143,11 +143,6 @@ impl Midstate { /// Length of the midstate, in bytes. const LEN: usize = 32; - /// Flag indicating whether user-visible serializations of this hash - /// should be backward. For some reason Satoshi decided this should be - /// true for `Sha256dHash`, so here we are. - const DISPLAY_BACKWARD: bool = true; - /// Construct a new [`Midstate`] from the inner value. pub const fn from_byte_array(inner: [u8; 32]) -> Self { Midstate(inner) } @@ -183,7 +178,26 @@ impl Midstate { } } -crate::internal_macros::arr_newtype_fmt_impl!(Midstate, 32); +impl fmt::LowerHex for Midstate { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + hex::fmt_hex_exact!(f, 32, self.0.iter().rev(), hex::Case::Lower) + } +} + +impl fmt::UpperHex for Midstate { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + hex::fmt_hex_exact!(f, 32, self.0.iter().rev(), hex::Case::Upper) + } +} + +impl fmt::Display for Midstate { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::LowerHex::fmt(&self, f) } +} + +impl fmt::Debug for Midstate { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{:#}", self) } +} + serde_impl!(Midstate, 32); borrow_slice_impl!(Midstate);