feat: change difficulty from byte count to bits

This commit is contained in:
2025-07-13 21:59:43 +02:00
parent ab57216b6b
commit 6af774d74f
9 changed files with 114 additions and 42 deletions
+25 -4
View File
@@ -7,16 +7,37 @@ use sha2::{Digest, Sha256};
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn solve(nonce: &[u8], target: &[u8]) -> Box<[u8]> {
pub fn solve(nonce: &[u8], target: &[u8], difficulty_bits: u32) -> Box<[u8]> {
let mut buf = vec![0u8; 8 + nonce.len()];
buf[8..].copy_from_slice(nonce);
let target_whole_bytes = &target[0..(difficulty_bits as usize / 8)];
let target_rest = {
let rest_bits = difficulty_bits % 8;
match rest_bits {
0 => None,
_ => {
let mask = 0xffu8.unbounded_shl(8 - rest_bits);
let rest = target[target_whole_bytes.len()] & mask;
Some((mask, rest))
}
}
};
for i in 0u64.. {
let i_bytes = u64::to_le_bytes(i);
buf[0..=7].copy_from_slice(&i_bytes);
let hash = Sha256::digest(&buf);
if &hash[0..target.len()] == target {
return i_bytes.into();
if &hash[0..target_whole_bytes.len()] == target_whole_bytes {
let target_rest_ok = match target_rest {
None => true,
Some((mask, rest)) => (hash[target_whole_bytes.len()] & mask) == rest,
};
if target_rest_ok {
return i_bytes.into();
}
}
}
@@ -28,7 +49,7 @@ mod tests {
#[test]
fn solve() {
assert_eq!(
super::solve(&[1, 2], &[3, 4]).as_ref(),
super::solve(&[1, 2], &[3, 4, 5], 18).as_ref(),
[45, 176, 0, 0, 0, 0, 0, 0]
);
}