2018-12-11 20:00:30 +00:00
|
|
|
/*
|
|
|
|
Copyright (c) 2018 tevador
|
|
|
|
|
|
|
|
This file is part of RandomX.
|
|
|
|
|
|
|
|
RandomX is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
RandomX is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with RandomX. If not, see<http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "VirtualMachine.hpp"
|
|
|
|
#include "common.hpp"
|
|
|
|
#include "dataset.hpp"
|
2018-12-19 20:54:44 +00:00
|
|
|
#include "Cache.hpp"
|
2018-12-19 11:38:10 +00:00
|
|
|
#include "t1ha/t1ha.h"
|
|
|
|
#include "blake2/blake2.h"
|
2018-12-11 20:00:30 +00:00
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
namespace RandomX {
|
|
|
|
VirtualMachine::VirtualMachine(bool softAes) : softAes(softAes), lightClient(false) {
|
2018-12-19 20:54:44 +00:00
|
|
|
mem.ds.dataset = nullptr;
|
2018-12-11 20:00:30 +00:00
|
|
|
}
|
|
|
|
|
2018-12-19 20:54:44 +00:00
|
|
|
VirtualMachine::~VirtualMachine() {
|
2018-12-11 20:00:30 +00:00
|
|
|
if (lightClient) {
|
2018-12-19 20:54:44 +00:00
|
|
|
delete mem.ds.lightDataset->block;
|
|
|
|
delete mem.ds.lightDataset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VirtualMachine::setDataset(dataset_t ds, bool light) {
|
|
|
|
if (mem.ds.dataset != nullptr) {
|
|
|
|
throw std::runtime_error("Dataset is already initialized");
|
2018-12-11 20:00:30 +00:00
|
|
|
}
|
|
|
|
lightClient = light;
|
|
|
|
if (light) {
|
2018-12-19 20:54:44 +00:00
|
|
|
auto lds = mem.ds.lightDataset = new LightClientDataset();
|
|
|
|
lds->cache = ds.cache;
|
|
|
|
lds->block = (uint8_t*)_mm_malloc(DatasetBlockSize, sizeof(__m128i));
|
|
|
|
lds->blockNumber = -1;
|
|
|
|
if (lds->block == nullptr) {
|
|
|
|
throw std::bad_alloc();
|
|
|
|
}
|
2018-12-11 20:00:30 +00:00
|
|
|
if (softAes) {
|
|
|
|
readDataset = &datasetReadLight<true>;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
readDataset = &datasetReadLight<false>;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2018-12-19 20:54:44 +00:00
|
|
|
mem.ds = ds;
|
2018-12-11 20:00:30 +00:00
|
|
|
readDataset = &datasetRead;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VirtualMachine::initializeScratchpad(uint32_t index) {
|
|
|
|
if (lightClient) {
|
2018-12-19 20:54:44 +00:00
|
|
|
auto cache = mem.ds.lightDataset->cache;
|
2018-12-11 20:00:30 +00:00
|
|
|
if (softAes) {
|
2018-12-15 22:13:17 +00:00
|
|
|
for (int i = 0; i < ScratchpadSize / DatasetBlockSize; ++i) {
|
2018-12-19 20:54:44 +00:00
|
|
|
initBlock<true>(cache->getCache(), ((uint8_t*)scratchpad) + DatasetBlockSize * i, (ScratchpadSize / DatasetBlockSize) * index + i, cache->getKeys());
|
2018-12-15 22:13:17 +00:00
|
|
|
}
|
2018-12-11 20:00:30 +00:00
|
|
|
}
|
|
|
|
else {
|
2018-12-15 22:13:17 +00:00
|
|
|
for (int i = 0; i < ScratchpadSize / DatasetBlockSize; ++i) {
|
2018-12-19 20:54:44 +00:00
|
|
|
initBlock<false>(cache->getCache(), ((uint8_t*)scratchpad) + DatasetBlockSize * i, (ScratchpadSize / DatasetBlockSize) * index + i, cache->getKeys());
|
2018-12-15 22:13:17 +00:00
|
|
|
}
|
2018-12-11 20:00:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2018-12-19 20:54:44 +00:00
|
|
|
memcpy(scratchpad, mem.ds.dataset + ScratchpadSize * index, ScratchpadSize);
|
2018-12-11 20:00:30 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-18 21:00:58 +00:00
|
|
|
|
|
|
|
void VirtualMachine::getResult(void* out) {
|
2018-12-19 11:38:10 +00:00
|
|
|
uint64_t smallState[sizeof(RegisterFile) / sizeof(uint64_t) + 2];
|
|
|
|
memcpy(smallState, ®, sizeof(RegisterFile));
|
|
|
|
smallState[17] = t1ha2_atonce128(&smallState[16], scratchpad, ScratchpadSize, reg.r[0].u64);
|
|
|
|
blake2b(out, ResultSize, smallState, sizeof(smallState), nullptr, 0);
|
2018-12-18 21:00:58 +00:00
|
|
|
}
|
2018-12-11 20:00:30 +00:00
|
|
|
}
|