mirror of
https://github.com/TecharoHQ/anubis.git
synced 2026-04-16 05:15:03 +00:00
@@ -38,9 +38,21 @@ mod hostimport {
|
|||||||
|
|
||||||
#[cfg(not(target_arch = "wasm32"))]
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
mod hostimport {
|
mod hostimport {
|
||||||
|
use crate::{DATA_BUFFER, DATA_LENGTH};
|
||||||
|
|
||||||
pub fn update_nonce(_nonce: u32) {
|
pub fn update_nonce(_nonce: u32) {
|
||||||
// This is intentionally blank
|
// This is intentionally blank
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn data_ptr() -> *const u8 {
|
||||||
|
let challenge = &DATA_BUFFER;
|
||||||
|
challenge.as_ptr()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_data_length(len: u32) {
|
||||||
|
let mut data_length = DATA_LENGTH.lock().unwrap();
|
||||||
|
*data_length = len as usize;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The data buffer is a bit weird in that it doesn't have an explicit length as it can
|
/// The data buffer is a bit weird in that it doesn't have an explicit length as it can
|
||||||
@@ -57,4 +69,4 @@ mod hostimport {
|
|||||||
pub static DATA_BUFFER: LazyLock<[u8; 4096]> = LazyLock::new(|| [0; 4096]);
|
pub static DATA_BUFFER: LazyLock<[u8; 4096]> = LazyLock::new(|| [0; 4096]);
|
||||||
pub static DATA_LENGTH: LazyLock<Mutex<usize>> = LazyLock::new(|| Mutex::new(0));
|
pub static DATA_LENGTH: LazyLock<Mutex<usize>> = LazyLock::new(|| Mutex::new(0));
|
||||||
|
|
||||||
pub use hostimport::update_nonce;
|
pub use hostimport::{data_ptr, set_data_length, update_nonce};
|
||||||
|
|||||||
@@ -55,10 +55,12 @@ fn anubis_work_inner(
|
|||||||
let data = &DATA_BUFFER;
|
let data = &DATA_BUFFER;
|
||||||
let data_len = *DATA_LENGTH.lock().unwrap();
|
let data_len = *DATA_LENGTH.lock().unwrap();
|
||||||
let data_slice = &data[..data_len];
|
let data_slice = &data[..data_len];
|
||||||
|
let mut i = 0;
|
||||||
|
|
||||||
let h = HashX::new(data_slice)?;
|
let h = HashX::new(data_slice)?;
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
|
i += 1;
|
||||||
let hash = h.hash_to_bytes(nonce as u64);
|
let hash = h.hash_to_bytes(nonce as u64);
|
||||||
|
|
||||||
if validate(&hash, difficulty) {
|
if validate(&hash, difficulty) {
|
||||||
@@ -69,24 +71,18 @@ fn anubis_work_inner(
|
|||||||
return Ok(nonce);
|
return Ok(nonce);
|
||||||
}
|
}
|
||||||
|
|
||||||
let old_nonce = nonce;
|
|
||||||
nonce = nonce.wrapping_add(iterand);
|
nonce = nonce.wrapping_add(iterand);
|
||||||
|
|
||||||
// send a progress update every 1024 iterations. since each thread checks
|
if i == 1024 {
|
||||||
// separate values, one simple way to do this is by bit masking the
|
|
||||||
// nonce for multiples of 1024. unfortunately, if the number of threads
|
|
||||||
// is not prime, only some of the threads will be sending the status
|
|
||||||
// update and they will get behind the others. this is slightly more
|
|
||||||
// complicated but ensures an even distribution between threads.
|
|
||||||
if nonce > old_nonce | 1023 && (nonce >> 10) % iterand == initial_nonce {
|
|
||||||
update_nonce(nonce);
|
update_nonce(nonce);
|
||||||
|
i = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// This function is the main entrypoint for the Anubis proof of work implementation.
|
/// This function is the main entrypoint for the Anubis proof of work implementation.
|
||||||
///
|
///
|
||||||
/// This expects `DATA_BUFFER` to be pre-populated with the challenge value as "raw bytes".
|
/// This expects `DATA_BUFFER` to be prepopulated with the challenge value as "raw bytes".
|
||||||
/// The definition of what goes in the data buffer is an exercise for the implementor, but
|
/// The definition of what goes in the data buffer is an exercise for the implementor, but
|
||||||
/// for SHA-256 we store the hash as "raw bytes". The data buffer is intentionally oversized
|
/// for SHA-256 we store the hash as "raw bytes". The data buffer is intentionally oversized
|
||||||
/// so that the challenge value can be expanded in the future.
|
/// so that the challenge value can be expanded in the future.
|
||||||
|
|||||||
@@ -62,7 +62,6 @@ fn compute_hash(nonce: u32) -> [u8; 32] {
|
|||||||
let data = &DATA_BUFFER;
|
let data = &DATA_BUFFER;
|
||||||
let data_len = *DATA_LENGTH.lock().unwrap();
|
let data_len = *DATA_LENGTH.lock().unwrap();
|
||||||
let use_le = data[data_len - 1] >= 128;
|
let use_le = data[data_len - 1] >= 128;
|
||||||
|
|
||||||
let data_slice = &data[..data_len];
|
let data_slice = &data[..data_len];
|
||||||
|
|
||||||
let mut hasher = Sha256::new();
|
let mut hasher = Sha256::new();
|
||||||
@@ -77,7 +76,7 @@ fn compute_hash(nonce: u32) -> [u8; 32] {
|
|||||||
|
|
||||||
/// This function is the main entrypoint for the Anubis proof of work implementation.
|
/// This function is the main entrypoint for the Anubis proof of work implementation.
|
||||||
///
|
///
|
||||||
/// This expects `DATA_BUFFER` to be pre-populated with the challenge value as "raw bytes".
|
/// This expects `DATA_BUFFER` to be prepopulated with the challenge value as "raw bytes".
|
||||||
/// The definition of what goes in the data buffer is an exercise for the implementor, but
|
/// The definition of what goes in the data buffer is an exercise for the implementor, but
|
||||||
/// for SHA-256 we store the hash as "raw bytes". The data buffer is intentionally oversized
|
/// for SHA-256 we store the hash as "raw bytes". The data buffer is intentionally oversized
|
||||||
/// so that the challenge value can be expanded in the future.
|
/// so that the challenge value can be expanded in the future.
|
||||||
@@ -96,8 +95,10 @@ fn compute_hash(nonce: u32) -> [u8; 32] {
|
|||||||
#[unsafe(no_mangle)]
|
#[unsafe(no_mangle)]
|
||||||
pub extern "C" fn anubis_work(difficulty: u32, initial_nonce: u32, iterand: u32) -> u32 {
|
pub extern "C" fn anubis_work(difficulty: u32, initial_nonce: u32, iterand: u32) -> u32 {
|
||||||
let mut nonce = initial_nonce;
|
let mut nonce = initial_nonce;
|
||||||
|
let mut i = 0;
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
|
i += 1;
|
||||||
let hash = compute_hash(nonce);
|
let hash = compute_hash(nonce);
|
||||||
|
|
||||||
if validate(&hash, difficulty) {
|
if validate(&hash, difficulty) {
|
||||||
@@ -108,17 +109,11 @@ pub extern "C" fn anubis_work(difficulty: u32, initial_nonce: u32, iterand: u32)
|
|||||||
return nonce;
|
return nonce;
|
||||||
}
|
}
|
||||||
|
|
||||||
let old_nonce = nonce;
|
|
||||||
nonce = nonce.wrapping_add(iterand);
|
nonce = nonce.wrapping_add(iterand);
|
||||||
|
|
||||||
// send a progress update every 1024 iterations. since each thread checks
|
if i == 1024 {
|
||||||
// separate values, one simple way to do this is by bit masking the
|
|
||||||
// nonce for multiples of 1024. unfortunately, if the number of threads
|
|
||||||
// is not prime, only some of the threads will be sending the status
|
|
||||||
// update and they will get behind the others. this is slightly more
|
|
||||||
// complicated but ensures an even distribution between threads.
|
|
||||||
if nonce > old_nonce | 1023 && (nonce >> 10) % iterand == initial_nonce {
|
|
||||||
update_nonce(nonce);
|
update_nonce(nonce);
|
||||||
|
i = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user