1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
//! Tiger: RandomX Pow

// Wei Tang <hi@that.world>
// Copyright 2019. The Tari Project

use std::os::raw::c_uint;

pub const HASH_SIZE: usize = sys::RANDOMX_HASH_SIZE as usize;


#[repr(C)]
#[derive(Copy, Clone)]
pub enum RandomXFlag {
    FlagDefault,
    FlagLargePages,
    FlagHardAES,
    FlagFullMem,
    FlagJIT,
    FlagSecure,
}

impl RandomXFlag {
    pub fn value(self) -> c_uint {
        match self {
            RandomXFlag::FlagDefault => 0,
            RandomXFlag::FlagLargePages => 1,
            RandomXFlag::FlagHardAES => 2,
            RandomXFlag::FlagFullMem => 4,
            RandomXFlag::FlagJIT => 8,
            RandomXFlag::FlagSecure => 16,
        }
    }
}

impl PartialEq for RandomXFlag {
    fn eq(&self, other: &Self) -> bool {
        self.value() == other.value()
    }
}


pub struct FullVM {
	cache_ptr: *mut sys::randomx_cache,
	dataset_ptr: *mut sys::randomx_dataset,
	ptr: *mut sys::randomx_vm,
}

impl FullVM {
	
	pub fn new(key: &[u8]) -> Self {
		let flags = sys::randomx_flags_RANDOMX_FLAG_DEFAULT
			| sys::randomx_flags_RANDOMX_FLAG_JIT
			| sys::randomx_flags_RANDOMX_FLAG_FULL_MEM;

		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 dataset_ptr = unsafe {
			let ptr = sys::randomx_alloc_dataset(flags);
			let count = sys::randomx_dataset_item_count();
			sys::randomx_init_dataset(ptr, cache_ptr, 0, count);
			ptr
		};

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

		Self { cache_ptr, dataset_ptr, ptr }
	}

	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 FullVM {
	fn drop(&mut self) {
		unsafe {
			sys::randomx_release_cache(self.cache_ptr);
			sys::randomx_release_dataset(self.dataset_ptr);
			sys::randomx_destroy_vm(self.ptr);
		}
	}
}

pub struct VM {
	cache_ptr: *mut sys::randomx_cache,
	ptr: *mut sys::randomx_vm,
}

impl 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 }
	}

	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 VM {
	fn drop(&mut self) {
		unsafe {
			sys::randomx_release_cache(self.cache_ptr);
			sys::randomx_destroy_vm(self.ptr);
		}
	}
}

#[cfg(test)]
mod tests {
	use super::*;

	#[test]
	fn should_create_vm() {
		let mut vm = VM::new(&b"RandomX example key"[..]);
		let hash = vm.calculate(&b"RandomX example input"[..]);
		assert_eq!(hash, [210, 164, 216, 149, 3, 68, 116, 1, 239, 110, 111, 48, 180, 102, 53, 180, 91, 84, 242, 90, 101, 12, 71, 70, 75, 83, 17, 249, 214, 253, 71, 89]);
	}

	#[test]
	fn should_work_with_full_vm() {
		let mut vm = VM::new(&b"RandomX example key"[..]);
		let hash = vm.calculate(&b"RandomX example input"[..]);
		let mut full_vm = FullVM::new(&b"RandomX example key"[..]);
		let full_hash = full_vm.calculate(&b"RandomX example input"[..]);
		assert_eq!(hash, full_hash);
	}
}