use uhash_types::{Difficulty, Proof};
#[inline(always)]
pub fn meets_difficulty(hash: &[u8; 32], difficulty: u32) -> bool {
let mut zero_bits = 0u32;
for byte in hash.iter() {
if *byte == 0 {
zero_bits += 8;
} else {
zero_bits += byte.leading_zeros();
break;
}
}
zero_bits >= difficulty
}
pub fn verify(header: &[u8], proof: &Proof, difficulty: Difficulty) -> bool {
let mut input = Vec::with_capacity(header.len() + 8);
input.extend_from_slice(header);
input.extend_from_slice(&proof.nonce.to_le_bytes());
let computed = crate::hash::hash(&input);
computed == proof.hash && meets_difficulty(&computed, difficulty.bits())
}
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;