Skip to content

Commit 00848f9

Browse files
authored
Merge pull request #12 from garymathews/hash
fix hash_structure_good function
2 parents 5a023ff + c51fc15 commit 00848f9

File tree

1 file changed

+22
-53
lines changed

1 file changed

+22
-53
lines changed

src/lib.rs

Lines changed: 22 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -410,28 +410,11 @@ pub fn hash(salt: &[u8], rom: &Rom, nb_loops: u32, nb_instrs: u32) -> [u8; 64] {
410410
vm.finalize()
411411
}
412412

413-
pub fn hash_structure_good(hash: &[u8], zero_bits: usize) -> bool {
414-
let full_bytes = zero_bits / 8; // Number of full zero bytes
415-
let remaining_bits = zero_bits % 8; // Bits to check in the next byte
416-
417-
// Check full zero bytes
418-
if hash.len() < full_bytes || hash[..full_bytes].iter().any(|&b| b != 0) {
419-
return false;
420-
}
421-
422-
if remaining_bits == 0 {
423-
return true;
424-
}
425-
if hash.len() > full_bytes {
426-
// Mask for the most significant bits
427-
let mask = 0xFF << (8 - remaining_bits);
428-
hash[full_bytes] & mask == 0
429-
} else {
430-
false
431-
}
413+
pub fn hash_structure_good(hash: &[u8], difficulty_mask: u32) -> bool {
414+
let value = u32::from_be_bytes(hash[..4].try_into().unwrap());
415+
(value | difficulty_mask) == difficulty_mask
432416
}
433417

434-
435418
// --------------------------------------------------------------------------
436419
// SCAVENGE LOGIC
437420
// --------------------------------------------------------------------------
@@ -442,12 +425,11 @@ pub struct Thread {}
442425
#[derive(Clone)]
443426
pub struct ChallengeParams {
444427
pub rom_key: String, // no_pre_mine hex string (used for ROM init)
445-
pub difficulty_mask: String, // difficulty hex string (used for submission check)
428+
pub difficulty_mask: u32, // difficulty mask (used for submission check)
446429
pub address: String, // Registered Cardano address
447430
pub challenge_id: String,
448431
pub latest_submission: String,
449432
pub no_pre_mine_hour: String,
450-
pub required_zero_bits: usize, // Derived from difficulty_mask
451433
pub rom: Arc<Rom>,
452434
}
453435

@@ -462,7 +444,7 @@ pub fn build_preimage(
462444
nonce: u64,
463445
address: &str,
464446
challenge_id: &str,
465-
difficulty: &str,
447+
difficulty_mask: u32,
466448
no_pre_mine: &str,
467449
latest_submission: &str,
468450
no_pre_mine_hour: &str,
@@ -472,26 +454,16 @@ pub fn build_preimage(
472454
preimage.push_str(&nonce_hex);
473455
preimage.push_str(address);
474456
preimage.push_str(challenge_id);
475-
preimage.push_str(difficulty);
457+
preimage.push_str(&format!("{:08X}", difficulty_mask));
476458
preimage.push_str(no_pre_mine);
477459
preimage.push_str(latest_submission);
478460
preimage.push_str(no_pre_mine_hour);
479461
preimage
480462
}
481463

482-
// Utility function to convert difficulty mask (e.g., "000FFFFF") to number of required zero bits
483-
fn difficulty_to_zero_bits(difficulty_hex: &str) -> usize {
484-
let difficulty_bytes = hex::decode(difficulty_hex).unwrap();
485-
let mut zero_bits = 0;
486-
for &byte in difficulty_bytes.iter() {
487-
if byte == 0x00 {
488-
zero_bits += 8;
489-
} else {
490-
zero_bits += byte.leading_zeros() as usize;
491-
break;
492-
}
493-
}
494-
zero_bits
464+
fn update_preimage_nonce(preimage_string: &mut String, nonce: u64) {
465+
let nonce_str = format!("{:016x}", nonce);
466+
preimage_string.replace_range(0..16, &nonce_str);
495467
}
496468

497469
// The worker thread function
@@ -501,22 +473,21 @@ fn spin(params: ChallengeParams, sender: Sender<Result>, stop_signal: Arc<Atomic
501473
const NB_LOOPS: u32 = 8;
502474
const NB_INSTRS: u32 = 256;
503475

504-
let my_address = &params.address;
476+
let mut preimage_string = build_preimage(
477+
nonce_value,
478+
&params.address,
479+
&params.challenge_id,
480+
params.difficulty_mask,
481+
&params.rom_key,
482+
&params.latest_submission,
483+
&params.no_pre_mine_hour,
484+
);
505485

506486
while !stop_signal.load(Ordering::Relaxed) {
507-
let preimage_string = build_preimage(
508-
nonce_value,
509-
my_address,
510-
&params.challenge_id,
511-
&params.difficulty_mask,
512-
&params.rom_key,
513-
&params.latest_submission,
514-
&params.no_pre_mine_hour,
515-
);
516487
let preimage_bytes = preimage_string.as_bytes();
517488
let h = hash(preimage_bytes, &params.rom, NB_LOOPS, NB_INSTRS);
518489

519-
if hash_structure_good(&h, params.required_zero_bits) {
490+
if hash_structure_good(&h, params.difficulty_mask) {
520491
if sender.send(Result::Found(nonce_value)).is_ok() {
521492
// Sent the found nonce
522493
}
@@ -529,6 +500,7 @@ fn spin(params: ChallengeParams, sender: Sender<Result>, stop_signal: Arc<Atomic
529500

530501
// Increment nonce by the thread step size
531502
nonce_value = nonce_value.wrapping_add(step_size);
503+
update_preimage_nonce(&mut preimage_string, nonce_value);
532504
}
533505
}
534506

@@ -545,9 +517,7 @@ pub fn scavenge(
545517
const MB: usize = 1024 * 1024;
546518
const GB: usize = 1024 * MB;
547519

548-
let required_zero_bits = difficulty_to_zero_bits(&difficulty);
549-
550-
// We rely on the caller to print required_zero_bits
520+
let difficulty_mask = u32::from_str_radix(&difficulty, 16).unwrap();
551521

552522
let nb_threads_u64 = nb_threads as u64;
553523
let step_size = nb_threads_u64;
@@ -570,12 +540,11 @@ pub fn scavenge(
570540

571541
let common_params = ChallengeParams {
572542
rom_key: no_pre_mine_key.clone(),
573-
difficulty_mask: difficulty.clone(),
543+
difficulty_mask,
574544
address: my_registered_address.clone(),
575545
challenge_id: challenge_id.clone(),
576546
latest_submission: latest_submission.clone(),
577547
no_pre_mine_hour: no_pre_mine_hour.clone(),
578-
required_zero_bits,
579548
rom: Arc::new(rom),
580549
};
581550

0 commit comments

Comments
 (0)