Skip to main content

uhash_core/
challenge.rs

1//! Challenge generation and difficulty encoding.
2
3use uhash_types::{Challenge, Difficulty};
4
5#[cfg(not(feature = "std"))]
6use alloc::vec::Vec;
7
8/// Build PoW input bytes from a challenge header and nonce.
9pub fn build_input(header: &[u8], nonce: u64) -> Vec<u8> {
10    let mut input = Vec::with_capacity(header.len() + 8);
11    input.extend_from_slice(header);
12    input.extend_from_slice(&nonce.to_le_bytes());
13    input
14}
15
16/// Build PoW input from a typed Challenge and nonce.
17pub fn challenge_input(challenge: &Challenge, nonce: u64) -> Vec<u8> {
18    build_input(&challenge.header, nonce)
19}
20
21/// Encode a difficulty as leading zero bit count.
22pub fn encode_difficulty(bits: u32) -> Difficulty {
23    Difficulty::new(bits)
24}