wildtiger 0.0.3

Proof-of-work (PoW) algorithm that is optimized for general-purpose CPUs
Documentation
//! Verification Client that uses less memory

use crate::x::HASH_SIZE;


/// Verification VM consumes less memory
pub struct LightCheck {
    /// Cachelines
    cache_ptr: *mut sys::randomx_cache,
    /// Compute Layer
    ptr: *mut sys::randomx_vm,
}

impl LightCheck {
    /// New Light Mode VM
    pub fn new(key: &[u8]) -> Self {
        let flags = sys::randomx_flags_RANDOMX_FLAG_DEFAULT | sys::randomx_flags_RANDOMX_FLAG_JIT;

        let cache_ptr = unsafe {
            let ptr = sys::randomx_alloc_cache(flags);
            sys::randomx_init_cache(ptr, key.as_ptr() as *const std::ffi::c_void, key.len());

            ptr
        };

        let ptr = unsafe { sys::randomx_create_vm(flags, cache_ptr, std::ptr::null_mut()) };

        Self { cache_ptr, ptr }
    }

    /// Derive Hash
    pub fn calculate(&mut self, input: &[u8]) -> [u8; HASH_SIZE] {
        let ret = [0u8; HASH_SIZE];

        unsafe {
            sys::randomx_calculate_hash(
                self.ptr,
                input.as_ptr() as *const std::ffi::c_void,
                input.len(),
                ret.as_ptr() as *mut std::ffi::c_void,
            );
        }

        ret
    }
}

impl Drop for LightCheck {
    fn drop(&mut self) {
        unsafe {
            sys::randomx_release_cache(self.cache_ptr);
            sys::randomx_destroy_vm(self.ptr);
        }
    }
}