use core::fmt;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(transparent))]
pub struct Hash32([u8; 32]);
impl Hash32 {
pub const ZERO: Self = Self([0u8; 32]);
#[inline]
pub const fn from_bytes(bytes: [u8; 32]) -> Self {
Self(bytes)
}
#[inline]
pub const fn as_bytes(&self) -> &[u8; 32] {
&self.0
}
#[cfg(feature = "blake3")]
#[inline]
pub fn from_blake3(data: &[u8]) -> Self {
let hash = blake3::hash(data);
Self(*hash.as_bytes())
}
#[inline(never)]
pub fn eq_ct(&self, other: &Self) -> bool {
let diff = self
.0
.iter()
.zip(other.0.iter())
.fold(0u8, |acc, (a, b)| acc | (a ^ b));
diff == 0
}
}
impl From<[u8; 32]> for Hash32 {
#[inline]
fn from(bytes: [u8; 32]) -> Self {
Self(bytes)
}
}
impl fmt::Debug for Hash32 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Hash32(")?;
for b in &self.0 {
write!(f, "{b:02x}")?;
}
write!(f, ")")
}
}
impl fmt::Display for Hash32 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for b in &self.0 {
write!(f, "{b:02x}")?;
}
Ok(())
}
}