2019-04-20 09:08:01 +00:00
|
|
|
/*
|
2019-05-18 12:21:47 +00:00
|
|
|
Copyright (c) 2018-2019, tevador <tevador@gmail.com>
|
|
|
|
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions are met:
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in the
|
|
|
|
documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of the copyright holder nor the
|
|
|
|
names of its contributors may be used to endorse or promote products
|
|
|
|
derived from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2019-04-20 09:08:01 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "randomx.h"
|
|
|
|
#include "dataset.hpp"
|
2019-04-20 14:53:06 +00:00
|
|
|
#include "vm_interpreted.hpp"
|
|
|
|
#include "vm_interpreted_light.hpp"
|
|
|
|
#include "vm_compiled.hpp"
|
|
|
|
#include "vm_compiled_light.hpp"
|
2019-04-20 09:08:01 +00:00
|
|
|
#include "blake2/blake2.h"
|
2019-10-08 21:09:35 +00:00
|
|
|
#include "cpu.hpp"
|
2019-06-29 16:53:49 +00:00
|
|
|
#include <cassert>
|
2019-07-09 18:26:50 +00:00
|
|
|
#include <limits>
|
2019-04-20 09:08:01 +00:00
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
2019-10-08 21:09:35 +00:00
|
|
|
randomx_flags randomx_get_flags() {
|
|
|
|
randomx_flags flags = RANDOMX_HAVE_COMPILER ? RANDOMX_FLAG_JIT : RANDOMX_FLAG_DEFAULT;
|
|
|
|
randomx::Cpu cpu;
|
2019-10-09 20:06:04 +00:00
|
|
|
if (HAVE_AES && cpu.hasAes()) {
|
2019-10-08 21:09:35 +00:00
|
|
|
flags |= RANDOMX_FLAG_HARD_AES;
|
|
|
|
}
|
|
|
|
if (randomx_argon2_impl_avx2() != nullptr && cpu.hasAvx2()) {
|
|
|
|
flags |= RANDOMX_FLAG_ARGON2_AVX2;
|
|
|
|
}
|
2019-10-09 19:37:14 +00:00
|
|
|
if (randomx_argon2_impl_ssse3() != nullptr && cpu.hasSsse3()) {
|
2019-10-08 21:09:35 +00:00
|
|
|
flags |= RANDOMX_FLAG_ARGON2_SSSE3;
|
|
|
|
}
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
2019-04-20 09:08:01 +00:00
|
|
|
randomx_cache *randomx_alloc_cache(randomx_flags flags) {
|
2019-10-05 22:43:01 +00:00
|
|
|
randomx_cache *cache = nullptr;
|
2019-10-09 19:37:14 +00:00
|
|
|
auto impl = randomx::selectArgonImpl(flags);
|
|
|
|
if (impl == nullptr) {
|
|
|
|
return cache;
|
|
|
|
}
|
2019-04-20 09:08:01 +00:00
|
|
|
|
2019-04-20 22:20:21 +00:00
|
|
|
try {
|
2019-06-29 16:53:49 +00:00
|
|
|
cache = new randomx_cache();
|
2019-10-09 19:37:14 +00:00
|
|
|
cache->argonImpl = impl;
|
2019-10-10 05:48:03 +00:00
|
|
|
switch ((int)(flags & (RANDOMX_FLAG_JIT | RANDOMX_FLAG_LARGE_PAGES))) {
|
2019-04-21 13:04:17 +00:00
|
|
|
case RANDOMX_FLAG_DEFAULT:
|
2019-04-28 10:44:28 +00:00
|
|
|
cache->dealloc = &randomx::deallocCache<randomx::DefaultAllocator>;
|
|
|
|
cache->jit = nullptr;
|
|
|
|
cache->initialize = &randomx::initCache;
|
|
|
|
cache->datasetInit = &randomx::initDataset;
|
|
|
|
cache->memory = (uint8_t*)randomx::DefaultAllocator::allocMemory(randomx::CacheSize);
|
2019-04-21 13:04:17 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case RANDOMX_FLAG_JIT:
|
2019-04-28 10:44:28 +00:00
|
|
|
cache->dealloc = &randomx::deallocCache<randomx::DefaultAllocator>;
|
2019-05-12 20:31:48 +00:00
|
|
|
cache->jit = new randomx::JitCompiler();
|
2019-04-28 10:44:28 +00:00
|
|
|
cache->initialize = &randomx::initCacheCompile;
|
|
|
|
cache->datasetInit = cache->jit->getDatasetInitFunc();
|
|
|
|
cache->memory = (uint8_t*)randomx::DefaultAllocator::allocMemory(randomx::CacheSize);
|
2019-04-21 13:04:17 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case RANDOMX_FLAG_LARGE_PAGES:
|
2019-04-28 10:44:28 +00:00
|
|
|
cache->dealloc = &randomx::deallocCache<randomx::LargePageAllocator>;
|
|
|
|
cache->jit = nullptr;
|
|
|
|
cache->initialize = &randomx::initCache;
|
|
|
|
cache->datasetInit = &randomx::initDataset;
|
|
|
|
cache->memory = (uint8_t*)randomx::LargePageAllocator::allocMemory(randomx::CacheSize);
|
2019-04-21 13:04:17 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case RANDOMX_FLAG_JIT | RANDOMX_FLAG_LARGE_PAGES:
|
2019-04-28 10:44:28 +00:00
|
|
|
cache->dealloc = &randomx::deallocCache<randomx::LargePageAllocator>;
|
2019-05-12 20:31:48 +00:00
|
|
|
cache->jit = new randomx::JitCompiler();
|
2019-04-28 10:44:28 +00:00
|
|
|
cache->initialize = &randomx::initCacheCompile;
|
|
|
|
cache->datasetInit = cache->jit->getDatasetInitFunc();
|
|
|
|
cache->memory = (uint8_t*)randomx::LargePageAllocator::allocMemory(randomx::CacheSize);
|
2019-04-21 13:04:17 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
UNREACHABLE;
|
|
|
|
}
|
2019-04-20 22:20:21 +00:00
|
|
|
}
|
|
|
|
catch (std::exception &ex) {
|
2019-06-29 16:53:49 +00:00
|
|
|
if (cache != nullptr) {
|
|
|
|
randomx_release_cache(cache);
|
|
|
|
cache = nullptr;
|
|
|
|
}
|
2019-04-20 09:08:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return cache;
|
|
|
|
}
|
|
|
|
|
2019-05-01 12:40:13 +00:00
|
|
|
void randomx_init_cache(randomx_cache *cache, const void *key, size_t keySize) {
|
2019-06-29 16:53:49 +00:00
|
|
|
assert(cache != nullptr);
|
|
|
|
assert(keySize == 0 || key != nullptr);
|
2019-10-04 06:31:32 +00:00
|
|
|
std::string cacheKey;
|
|
|
|
cacheKey.assign((const char *)key, keySize);
|
|
|
|
if (cache->cacheKey != cacheKey || !cache->isInitialized()) {
|
|
|
|
cache->initialize(cache, key, keySize);
|
|
|
|
cache->cacheKey = cacheKey;
|
|
|
|
}
|
2019-04-20 09:08:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void randomx_release_cache(randomx_cache* cache) {
|
2019-06-29 16:53:49 +00:00
|
|
|
assert(cache != nullptr);
|
2019-10-05 22:43:01 +00:00
|
|
|
if (cache->memory != nullptr) {
|
|
|
|
cache->dealloc(cache);
|
|
|
|
}
|
2019-04-20 09:08:01 +00:00
|
|
|
delete cache;
|
|
|
|
}
|
|
|
|
|
|
|
|
randomx_dataset *randomx_alloc_dataset(randomx_flags flags) {
|
2019-07-09 18:26:50 +00:00
|
|
|
|
|
|
|
//fail on 32-bit systems if DatasetSize is >= 4 GiB
|
|
|
|
if (randomx::DatasetSize > std::numeric_limits<size_t>::max()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-10-05 22:43:01 +00:00
|
|
|
randomx_dataset *dataset = nullptr;
|
2019-04-20 22:20:21 +00:00
|
|
|
|
|
|
|
try {
|
2019-06-29 16:53:49 +00:00
|
|
|
dataset = new randomx_dataset();
|
2019-04-21 13:04:17 +00:00
|
|
|
if (flags & RANDOMX_FLAG_LARGE_PAGES) {
|
2019-04-28 10:44:28 +00:00
|
|
|
dataset->dealloc = &randomx::deallocDataset<randomx::LargePageAllocator>;
|
|
|
|
dataset->memory = (uint8_t*)randomx::LargePageAllocator::allocMemory(randomx::DatasetSize);
|
2019-04-21 13:04:17 +00:00
|
|
|
}
|
|
|
|
else {
|
2019-04-28 10:44:28 +00:00
|
|
|
dataset->dealloc = &randomx::deallocDataset<randomx::DefaultAllocator>;
|
|
|
|
dataset->memory = (uint8_t*)randomx::DefaultAllocator::allocMemory(randomx::DatasetSize);
|
2019-04-21 13:04:17 +00:00
|
|
|
}
|
2019-04-20 22:20:21 +00:00
|
|
|
}
|
|
|
|
catch (std::exception &ex) {
|
2019-06-29 16:53:49 +00:00
|
|
|
if (dataset != nullptr) {
|
|
|
|
randomx_release_dataset(dataset);
|
|
|
|
dataset = nullptr;
|
|
|
|
}
|
2019-04-20 09:08:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return dataset;
|
|
|
|
}
|
|
|
|
|
2019-06-29 16:53:49 +00:00
|
|
|
constexpr unsigned long DatasetItemCount = randomx::DatasetSize / RANDOMX_DATASET_ITEM_SIZE;
|
|
|
|
|
2019-04-23 20:15:55 +00:00
|
|
|
unsigned long randomx_dataset_item_count() {
|
2019-06-29 16:53:49 +00:00
|
|
|
return DatasetItemCount;
|
2019-04-23 20:15:55 +00:00
|
|
|
}
|
|
|
|
|
2019-04-22 13:13:41 +00:00
|
|
|
void randomx_init_dataset(randomx_dataset *dataset, randomx_cache *cache, unsigned long startItem, unsigned long itemCount) {
|
2019-06-29 16:53:49 +00:00
|
|
|
assert(dataset != nullptr);
|
|
|
|
assert(cache != nullptr);
|
|
|
|
assert(startItem < DatasetItemCount && itemCount <= DatasetItemCount);
|
|
|
|
assert(startItem + itemCount <= DatasetItemCount);
|
2019-04-28 10:44:28 +00:00
|
|
|
cache->datasetInit(cache, dataset->memory + startItem * randomx::CacheLineSize, startItem, startItem + itemCount);
|
2019-04-20 09:08:01 +00:00
|
|
|
}
|
|
|
|
|
2019-04-23 20:15:55 +00:00
|
|
|
void *randomx_get_dataset_memory(randomx_dataset *dataset) {
|
2019-06-29 16:53:49 +00:00
|
|
|
assert(dataset != nullptr);
|
2019-04-23 20:15:55 +00:00
|
|
|
return dataset->memory;
|
|
|
|
}
|
|
|
|
|
2019-04-20 09:08:01 +00:00
|
|
|
void randomx_release_dataset(randomx_dataset *dataset) {
|
2019-06-29 16:53:49 +00:00
|
|
|
assert(dataset != nullptr);
|
2019-04-28 10:44:28 +00:00
|
|
|
dataset->dealloc(dataset);
|
2019-04-20 09:08:01 +00:00
|
|
|
delete dataset;
|
|
|
|
}
|
|
|
|
|
2019-04-21 21:23:13 +00:00
|
|
|
randomx_vm *randomx_create_vm(randomx_flags flags, randomx_cache *cache, randomx_dataset *dataset) {
|
2019-06-29 16:53:49 +00:00
|
|
|
assert(cache != nullptr || (flags & RANDOMX_FLAG_FULL_MEM));
|
|
|
|
assert(cache == nullptr || cache->isInitialized());
|
|
|
|
assert(dataset != nullptr || !(flags & RANDOMX_FLAG_FULL_MEM));
|
|
|
|
|
2019-04-21 13:04:17 +00:00
|
|
|
randomx_vm *vm = nullptr;
|
|
|
|
|
|
|
|
try {
|
2019-10-10 05:48:03 +00:00
|
|
|
switch ((int)(flags & (RANDOMX_FLAG_FULL_MEM | RANDOMX_FLAG_JIT | RANDOMX_FLAG_HARD_AES | RANDOMX_FLAG_LARGE_PAGES))) {
|
2019-04-21 21:23:13 +00:00
|
|
|
case RANDOMX_FLAG_DEFAULT:
|
2019-04-21 13:04:17 +00:00
|
|
|
vm = new randomx::InterpretedLightVmDefault();
|
|
|
|
break;
|
|
|
|
|
2019-04-21 21:23:13 +00:00
|
|
|
case RANDOMX_FLAG_FULL_MEM:
|
2019-04-21 13:04:17 +00:00
|
|
|
vm = new randomx::InterpretedVmDefault();
|
|
|
|
break;
|
|
|
|
|
2019-04-21 21:23:13 +00:00
|
|
|
case RANDOMX_FLAG_JIT:
|
2019-08-25 11:47:40 +00:00
|
|
|
if (flags & RANDOMX_FLAG_SECURE) {
|
|
|
|
vm = new randomx::CompiledLightVmDefaultSecure();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
vm = new randomx::CompiledLightVmDefault();
|
|
|
|
}
|
2019-04-21 13:04:17 +00:00
|
|
|
break;
|
|
|
|
|
2019-04-21 21:23:13 +00:00
|
|
|
case RANDOMX_FLAG_FULL_MEM | RANDOMX_FLAG_JIT:
|
2019-08-25 11:47:40 +00:00
|
|
|
if (flags & RANDOMX_FLAG_SECURE) {
|
|
|
|
vm = new randomx::CompiledVmDefaultSecure();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
vm = new randomx::CompiledVmDefault();
|
|
|
|
}
|
2019-04-21 13:04:17 +00:00
|
|
|
break;
|
|
|
|
|
2019-04-21 21:23:13 +00:00
|
|
|
case RANDOMX_FLAG_HARD_AES:
|
2019-04-21 13:04:17 +00:00
|
|
|
vm = new randomx::InterpretedLightVmHardAes();
|
|
|
|
break;
|
2019-04-20 09:08:01 +00:00
|
|
|
|
2019-04-21 21:23:13 +00:00
|
|
|
case RANDOMX_FLAG_FULL_MEM | RANDOMX_FLAG_HARD_AES:
|
2019-04-21 13:04:17 +00:00
|
|
|
vm = new randomx::InterpretedVmHardAes();
|
|
|
|
break;
|
|
|
|
|
2019-04-21 21:23:13 +00:00
|
|
|
case RANDOMX_FLAG_JIT | RANDOMX_FLAG_HARD_AES:
|
2019-08-25 11:47:40 +00:00
|
|
|
if (flags & RANDOMX_FLAG_SECURE) {
|
|
|
|
vm = new randomx::CompiledLightVmHardAesSecure();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
vm = new randomx::CompiledLightVmHardAes();
|
|
|
|
}
|
2019-04-21 13:04:17 +00:00
|
|
|
break;
|
|
|
|
|
2019-04-21 21:23:13 +00:00
|
|
|
case RANDOMX_FLAG_FULL_MEM | RANDOMX_FLAG_JIT | RANDOMX_FLAG_HARD_AES:
|
2019-08-25 11:47:40 +00:00
|
|
|
if (flags & RANDOMX_FLAG_SECURE) {
|
|
|
|
vm = new randomx::CompiledVmHardAesSecure();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
vm = new randomx::CompiledVmHardAes();
|
|
|
|
}
|
2019-04-21 13:04:17 +00:00
|
|
|
break;
|
|
|
|
|
2019-04-21 21:23:13 +00:00
|
|
|
case RANDOMX_FLAG_LARGE_PAGES:
|
2019-04-21 13:04:17 +00:00
|
|
|
vm = new randomx::InterpretedLightVmLargePage();
|
|
|
|
break;
|
|
|
|
|
2019-04-21 21:23:13 +00:00
|
|
|
case RANDOMX_FLAG_FULL_MEM | RANDOMX_FLAG_LARGE_PAGES:
|
2019-04-21 13:04:17 +00:00
|
|
|
vm = new randomx::InterpretedVmLargePage();
|
|
|
|
break;
|
|
|
|
|
2019-04-21 21:23:13 +00:00
|
|
|
case RANDOMX_FLAG_JIT | RANDOMX_FLAG_LARGE_PAGES:
|
2019-08-25 11:47:40 +00:00
|
|
|
if (flags & RANDOMX_FLAG_SECURE) {
|
|
|
|
vm = new randomx::CompiledLightVmLargePageSecure();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
vm = new randomx::CompiledLightVmLargePage();
|
|
|
|
}
|
2019-04-21 13:04:17 +00:00
|
|
|
break;
|
|
|
|
|
2019-04-21 21:23:13 +00:00
|
|
|
case RANDOMX_FLAG_FULL_MEM | RANDOMX_FLAG_JIT | RANDOMX_FLAG_LARGE_PAGES:
|
2019-08-25 11:47:40 +00:00
|
|
|
if (flags & RANDOMX_FLAG_SECURE) {
|
|
|
|
vm = new randomx::CompiledVmLargePageSecure();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
vm = new randomx::CompiledVmLargePage();
|
|
|
|
}
|
2019-04-21 13:04:17 +00:00
|
|
|
break;
|
|
|
|
|
2019-04-21 21:23:13 +00:00
|
|
|
case RANDOMX_FLAG_HARD_AES | RANDOMX_FLAG_LARGE_PAGES:
|
2019-04-21 13:04:17 +00:00
|
|
|
vm = new randomx::InterpretedLightVmLargePageHardAes();
|
|
|
|
break;
|
|
|
|
|
2019-04-21 21:23:13 +00:00
|
|
|
case RANDOMX_FLAG_FULL_MEM | RANDOMX_FLAG_HARD_AES | RANDOMX_FLAG_LARGE_PAGES:
|
2019-04-21 13:04:17 +00:00
|
|
|
vm = new randomx::InterpretedVmLargePageHardAes();
|
|
|
|
break;
|
|
|
|
|
2019-04-21 21:23:13 +00:00
|
|
|
case RANDOMX_FLAG_JIT | RANDOMX_FLAG_HARD_AES | RANDOMX_FLAG_LARGE_PAGES:
|
2019-08-25 11:47:40 +00:00
|
|
|
if (flags & RANDOMX_FLAG_SECURE) {
|
|
|
|
vm = new randomx::CompiledLightVmLargePageHardAesSecure();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
vm = new randomx::CompiledLightVmLargePageHardAes();
|
|
|
|
}
|
2019-04-21 13:04:17 +00:00
|
|
|
break;
|
|
|
|
|
2019-04-21 21:23:13 +00:00
|
|
|
case RANDOMX_FLAG_FULL_MEM | RANDOMX_FLAG_JIT | RANDOMX_FLAG_HARD_AES | RANDOMX_FLAG_LARGE_PAGES:
|
2019-08-25 11:47:40 +00:00
|
|
|
if (flags & RANDOMX_FLAG_SECURE) {
|
|
|
|
vm = new randomx::CompiledVmLargePageHardAesSecure();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
vm = new randomx::CompiledVmLargePageHardAes();
|
|
|
|
}
|
2019-04-21 13:04:17 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
UNREACHABLE;
|
|
|
|
}
|
|
|
|
|
2019-10-04 06:31:32 +00:00
|
|
|
if(cache != nullptr) {
|
2019-04-21 21:23:13 +00:00
|
|
|
vm->setCache(cache);
|
2019-10-04 06:31:32 +00:00
|
|
|
vm->cacheKey = cache->cacheKey;
|
|
|
|
}
|
2019-04-21 21:23:13 +00:00
|
|
|
|
|
|
|
if(dataset != nullptr)
|
|
|
|
vm->setDataset(dataset);
|
|
|
|
|
2019-04-21 13:04:17 +00:00
|
|
|
vm->allocate();
|
|
|
|
}
|
|
|
|
catch (std::exception &ex) {
|
2019-04-20 09:08:01 +00:00
|
|
|
delete vm;
|
|
|
|
vm = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return vm;
|
|
|
|
}
|
|
|
|
|
|
|
|
void randomx_vm_set_cache(randomx_vm *machine, randomx_cache* cache) {
|
2019-06-29 16:53:49 +00:00
|
|
|
assert(machine != nullptr);
|
|
|
|
assert(cache != nullptr && cache->isInitialized());
|
2019-10-04 06:31:32 +00:00
|
|
|
if (machine->cacheKey != cache->cacheKey) {
|
|
|
|
machine->setCache(cache);
|
|
|
|
machine->cacheKey = cache->cacheKey;
|
|
|
|
}
|
2019-04-20 09:08:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void randomx_vm_set_dataset(randomx_vm *machine, randomx_dataset *dataset) {
|
2019-06-29 16:53:49 +00:00
|
|
|
assert(machine != nullptr);
|
|
|
|
assert(dataset != nullptr);
|
2019-04-20 09:08:01 +00:00
|
|
|
machine->setDataset(dataset);
|
|
|
|
}
|
|
|
|
|
|
|
|
void randomx_destroy_vm(randomx_vm *machine) {
|
2019-06-29 16:53:49 +00:00
|
|
|
assert(machine != nullptr);
|
2019-04-20 09:08:01 +00:00
|
|
|
delete machine;
|
|
|
|
}
|
|
|
|
|
2019-04-20 22:20:21 +00:00
|
|
|
void randomx_calculate_hash(randomx_vm *machine, const void *input, size_t inputSize, void *output) {
|
2019-06-29 16:53:49 +00:00
|
|
|
assert(machine != nullptr);
|
|
|
|
assert(inputSize == 0 || input != nullptr);
|
|
|
|
assert(output != nullptr);
|
2019-04-21 13:04:17 +00:00
|
|
|
alignas(16) uint64_t tempHash[8];
|
2019-06-29 16:53:49 +00:00
|
|
|
int blakeResult = blake2b(tempHash, sizeof(tempHash), input, inputSize, nullptr, 0);
|
|
|
|
assert(blakeResult == 0);
|
2019-04-21 13:04:17 +00:00
|
|
|
machine->initScratchpad(&tempHash);
|
2019-04-20 09:08:01 +00:00
|
|
|
machine->resetRoundingMode();
|
|
|
|
for (int chain = 0; chain < RANDOMX_PROGRAM_COUNT - 1; ++chain) {
|
2019-04-21 13:04:17 +00:00
|
|
|
machine->run(&tempHash);
|
2019-06-29 16:53:49 +00:00
|
|
|
blakeResult = blake2b(tempHash, sizeof(tempHash), machine->getRegisterFile(), sizeof(randomx::RegisterFile), nullptr, 0);
|
|
|
|
assert(blakeResult == 0);
|
2019-04-20 09:08:01 +00:00
|
|
|
}
|
2019-04-21 13:04:17 +00:00
|
|
|
machine->run(&tempHash);
|
2019-04-20 22:20:21 +00:00
|
|
|
machine->getFinalResult(output, RANDOMX_HASH_SIZE);
|
2019-04-20 09:08:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|