I'm relatively new to multithreading, as this post may suggest, but I'm trying to have threads end once the nonce in the following code is found.
I'm also having a bit of trouble actually getting the values out from said threads
Any other suggestions regarding said code are also very much appreciated!
use num_bigint::BigUint;
use num_traits::One;
use crypto::digest::Digest;
use crypto::sha2::Sha256;
use hex::ToHex;
use std::time::Instant;
use std::thread;
use std::sync::{Arc, Mutex};
//todo: move into inputs
const LEADING_ZEROS: usize = 5;
const MAX_NONCE: u64 = 1_000_000_000;
fn main() {
//todo add back in where appropriate
//let before = Instant::now();
//println!("Elapsed time: {:2?}", before.elapsed());
let input = "The Times 03/Jan/2009 Chancellor on brink of second bailout for banks";
let mut vec: Vec<&str> = Vec::with_capacity(LEADING_ZEROS * 3); //TODO: limit threads
for _ in 0..LEADING_ZEROS * 3 {
vec.push(input);
}
//
let mut lower = 0;
let mut upper = MAX_NONCE / (LEADING_ZEROS as u64 * 3);
let noncer = Arc::new(Mutex::new(0));
let mut handles = vec![];
for i in vec {
let noncer = Arc::clone(&noncer);
let handle = thread::spawn(move || {
find_nonce(i, lower, upper)
});
handles.push(handle);
let temp = lower;
lower = upper;
upper = upper + temp;
}
let nonce_vec: Vec<u64> = vec![];
for handle in handles {
println!("handle? {:?}", handle);
handle.join().unwrap();
}
//println!("handle? {:?}", handles);
println!("nonce: {}", *noncer.lock().unwrap());
let max_hash = BigUint::one() << (256 - 4 * LEADING_ZEROS); //>
//println!("{:?}", nonce);
println!("old hash: {}", hash_without_nonce(input));
//println!("nonced hash: {}", nonce_hash(input, nonce));
}
const HASH_BYTE_SIZE: usize = 32;
pub type Sha256Hash = [u8; HASH_BYTE_SIZE];
pub fn find_nonce(input: &str, lower: u64, upper: u64) -> u64 {
let mut nonce = 0;
//println!("{}", input);
for nonces in lower..upper {
//println!("{}", nonces);
let max_hash = BigUint::one() << (256 - 4 * LEADING_ZEROS); //>
if found_nonce(input, nonces, max_hash) {
nonce = nonces;
return nonce
}
}
nonce
}
pub fn nonce_hash(input: &str, nonce: u64) -> String {
let mut contents = Vec::new();
contents.extend_from_slice(input.as_bytes());
contents.extend_from_slice(&convert_u64_little_endian(nonce));
let mut hasher = Sha256::new();
hasher.input(&contents);
let mut hash = Sha256Hash::default();
hasher.result(&mut hash);
let result: String = hash.to_hex();
result
}
pub fn found_nonce(input: &str, nonce: u64, max_hash: BigUint) -> bool {
let mut contents = Vec::new();
contents.extend_from_slice(input.as_bytes());
contents.extend_from_slice(&convert_u64_little_endian(nonce));
let mut hasher = Sha256::new();
hasher.input(&contents);
let mut hash = Sha256Hash::default();
hasher.result(&mut hash);
let hash_int = BigUint::from_bytes_be(&hash);
//println!("{}", hash.to_hex());
if hash_int < max_hash {
println!("{}", nonce);
true
} else {
false
}
}
pub fn hash_without_nonce(input: &str) -> String {
let mut hasher = Sha256::new();
hasher.input(input.as_bytes());
let mut hash = Sha256Hash::default();
hasher.result(&mut hash);
let result = hash.to_hex();
result
}
pub fn convert_u64_little_endian(val: u64) -> [u8; 8] {
return [
val as u8,
(val >> 8) as u8,
(val >> 16) as u8,
(val >> 24) as u8,
(val >> 32) as u8,
(val >> 40) as u8,
(val >> 48) as u8,
(val >> 56) as u8,
]
}