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/>.
|
|
|
|
*/
|
|
|
|
|
2019-04-21 21:23:13 +00:00
|
|
|
#include <cstring>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <stdexcept>
|
2019-04-20 14:53:06 +00:00
|
|
|
#include "virtual_machine.hpp"
|
2018-12-11 20:00:30 +00:00
|
|
|
#include "common.hpp"
|
2019-04-20 14:53:06 +00:00
|
|
|
#include "aes_hash.hpp"
|
2018-12-19 11:38:10 +00:00
|
|
|
#include "blake2/blake2.h"
|
2019-04-20 14:53:06 +00:00
|
|
|
#include "intrin_portable.h"
|
2019-04-20 09:08:01 +00:00
|
|
|
#include "allocator.hpp"
|
2018-12-31 18:06:45 +00:00
|
|
|
|
2019-04-20 09:08:01 +00:00
|
|
|
randomx_vm::~randomx_vm() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void randomx_vm::resetRoundingMode() {
|
2019-05-14 07:13:38 +00:00
|
|
|
rx_reset_float_state();
|
2019-04-20 09:08:01 +00:00
|
|
|
}
|
|
|
|
|
2019-04-30 19:14:50 +00:00
|
|
|
namespace randomx {
|
|
|
|
|
|
|
|
static inline uint64_t getSmallPositiveFloatBits(uint64_t entropy) {
|
|
|
|
auto exponent = entropy >> 59; //0..31
|
|
|
|
auto mantissa = entropy & mantissaMask;
|
|
|
|
exponent += exponentBias;
|
|
|
|
exponent &= exponentMask;
|
|
|
|
exponent <<= mantissaSize;
|
|
|
|
return exponent | mantissa;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint64_t getStaticExponent(uint64_t entropy) {
|
|
|
|
auto exponent = constExponentBits;
|
|
|
|
exponent |= (entropy >> (64 - staticExponentBits)) << dynamicExponentBits;
|
|
|
|
exponent <<= mantissaSize;
|
|
|
|
return exponent;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint64_t getFloatMask(uint64_t entropy) {
|
|
|
|
constexpr uint64_t mask22bit = (1ULL << 22) - 1;
|
|
|
|
return (entropy & mask22bit) | getStaticExponent(entropy);
|
|
|
|
}
|
|
|
|
|
2019-04-20 09:08:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void randomx_vm::initialize() {
|
2019-04-30 19:14:50 +00:00
|
|
|
store64(®.a[0].lo, randomx::getSmallPositiveFloatBits(program.getEntropy(0)));
|
|
|
|
store64(®.a[0].hi, randomx::getSmallPositiveFloatBits(program.getEntropy(1)));
|
|
|
|
store64(®.a[1].lo, randomx::getSmallPositiveFloatBits(program.getEntropy(2)));
|
|
|
|
store64(®.a[1].hi, randomx::getSmallPositiveFloatBits(program.getEntropy(3)));
|
|
|
|
store64(®.a[2].lo, randomx::getSmallPositiveFloatBits(program.getEntropy(4)));
|
|
|
|
store64(®.a[2].hi, randomx::getSmallPositiveFloatBits(program.getEntropy(5)));
|
|
|
|
store64(®.a[3].lo, randomx::getSmallPositiveFloatBits(program.getEntropy(6)));
|
|
|
|
store64(®.a[3].hi, randomx::getSmallPositiveFloatBits(program.getEntropy(7)));
|
2019-04-20 09:08:01 +00:00
|
|
|
mem.ma = program.getEntropy(8) & randomx::CacheLineAlignMask;
|
|
|
|
mem.mx = program.getEntropy(10);
|
|
|
|
auto addressRegisters = program.getEntropy(12);
|
|
|
|
config.readReg0 = 0 + (addressRegisters & 1);
|
|
|
|
addressRegisters >>= 1;
|
|
|
|
config.readReg1 = 2 + (addressRegisters & 1);
|
|
|
|
addressRegisters >>= 1;
|
|
|
|
config.readReg2 = 4 + (addressRegisters & 1);
|
|
|
|
addressRegisters >>= 1;
|
|
|
|
config.readReg3 = 6 + (addressRegisters & 1);
|
2019-04-26 14:05:30 +00:00
|
|
|
datasetOffset = (program.getEntropy(13) & randomx::DatasetExtraItems) * randomx::CacheLineSize;
|
2019-04-30 19:14:50 +00:00
|
|
|
store64(&config.eMask[0], randomx::getFloatMask(program.getEntropy(14)));
|
|
|
|
store64(&config.eMask[1], randomx::getFloatMask(program.getEntropy(15)));
|
2019-04-20 09:08:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace randomx {
|
2019-02-09 14:45:26 +00:00
|
|
|
|
2019-05-14 07:13:38 +00:00
|
|
|
alignas(16) volatile static rx_vec_i128 aesDummy;
|
2019-04-21 21:23:13 +00:00
|
|
|
|
2019-04-20 09:08:01 +00:00
|
|
|
template<class Allocator, bool softAes>
|
|
|
|
VmBase<Allocator, softAes>::~VmBase() {
|
|
|
|
Allocator::freeMemory(scratchpad, ScratchpadSize);
|
2018-12-11 20:00:30 +00:00
|
|
|
}
|
|
|
|
|
2019-04-20 09:08:01 +00:00
|
|
|
template<class Allocator, bool softAes>
|
2019-04-21 13:04:17 +00:00
|
|
|
void VmBase<Allocator, softAes>::allocate() {
|
2019-04-28 10:44:28 +00:00
|
|
|
if (datasetPtr == nullptr)
|
2019-04-21 21:23:13 +00:00
|
|
|
throw std::invalid_argument("Cache/Dataset not set");
|
|
|
|
if (!softAes) { //if hardware AES is not supported, it's better to fail now than to return a ticking bomb
|
2019-05-14 07:13:38 +00:00
|
|
|
rx_vec_i128 tmp = rx_load_vec_i128((const rx_vec_i128*)&aesDummy);
|
|
|
|
tmp = rx_aesenc_vec_i128(tmp, tmp);
|
|
|
|
rx_store_vec_i128((rx_vec_i128*)&aesDummy, tmp);
|
2019-04-21 21:23:13 +00:00
|
|
|
}
|
2019-04-20 09:08:01 +00:00
|
|
|
scratchpad = (uint8_t*)Allocator::allocMemory(ScratchpadSize);
|
2019-02-09 14:45:26 +00:00
|
|
|
}
|
|
|
|
|
2019-04-20 09:08:01 +00:00
|
|
|
template<class Allocator, bool softAes>
|
|
|
|
void VmBase<Allocator, softAes>::getFinalResult(void* out, size_t outSize) {
|
|
|
|
hashAes1Rx4<softAes>(scratchpad, ScratchpadSize, ®.a);
|
|
|
|
blake2b(out, outSize, ®, sizeof(RegisterFile), nullptr, 0);
|
2018-12-18 21:00:58 +00:00
|
|
|
}
|
2019-02-09 14:45:26 +00:00
|
|
|
|
2019-04-20 10:49:24 +00:00
|
|
|
template<class Allocator, bool softAes>
|
|
|
|
void VmBase<Allocator, softAes>::initScratchpad(void* seed) {
|
|
|
|
fillAes1Rx4<softAes>(seed, ScratchpadSize, scratchpad);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class Allocator, bool softAes>
|
|
|
|
void VmBase<Allocator, softAes>::generateProgram(void* seed) {
|
|
|
|
fillAes1Rx4<softAes>(seed, sizeof(program), &program);
|
|
|
|
}
|
|
|
|
|
2019-04-20 09:08:01 +00:00
|
|
|
template class VmBase<AlignedAllocator<CacheLineSize>, false>;
|
|
|
|
template class VmBase<AlignedAllocator<CacheLineSize>, true>;
|
|
|
|
template class VmBase<LargePageAllocator, false>;
|
|
|
|
template class VmBase<LargePageAllocator, true>;
|
2018-12-11 20:00:30 +00:00
|
|
|
}
|