2019-07-01 18:11:51 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <stdexcept>
|
2019-08-15 20:18:35 +00:00
|
|
|
#include "crypto/randomx/virtual_machine.hpp"
|
|
|
|
#include "crypto/randomx/common.hpp"
|
|
|
|
#include "crypto/randomx/aes_hash.hpp"
|
|
|
|
#include "crypto/randomx/blake2/blake2.h"
|
|
|
|
#include "crypto/randomx/intrin_portable.h"
|
|
|
|
#include "crypto/randomx/allocator.hpp"
|
2019-07-01 18:11:51 +00:00
|
|
|
|
|
|
|
randomx_vm::~randomx_vm() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void randomx_vm::resetRoundingMode() {
|
|
|
|
rx_reset_float_state();
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void randomx_vm::initialize() {
|
|
|
|
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)));
|
|
|
|
mem.ma = program.getEntropy(8) & 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);
|
|
|
|
datasetOffset = (program.getEntropy(13) % (DatasetExtraItems + 1)) * randomx::CacheLineSize;
|
|
|
|
store64(&config.eMask[0], randomx::getFloatMask(program.getEntropy(14)));
|
|
|
|
store64(&config.eMask[1], randomx::getFloatMask(program.getEntropy(15)));
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace randomx {
|
|
|
|
|
2019-07-16 21:33:11 +00:00
|
|
|
template<bool softAes>
|
|
|
|
VmBase<softAes>::~VmBase() {
|
2019-07-01 18:11:51 +00:00
|
|
|
}
|
|
|
|
|
2019-07-16 21:33:11 +00:00
|
|
|
template<bool softAes>
|
|
|
|
void VmBase<softAes>::setScratchpad(uint8_t *scratchpad) {
|
|
|
|
if (datasetPtr == nullptr) {
|
2019-07-01 18:11:51 +00:00
|
|
|
throw std::invalid_argument("Cache/Dataset not set");
|
|
|
|
}
|
2019-07-16 21:33:11 +00:00
|
|
|
|
|
|
|
this->scratchpad = scratchpad;
|
2019-07-01 18:11:51 +00:00
|
|
|
}
|
|
|
|
|
2019-07-16 21:33:11 +00:00
|
|
|
template<bool softAes>
|
|
|
|
void VmBase<softAes>::getFinalResult(void* out, size_t outSize) {
|
2019-07-01 18:11:51 +00:00
|
|
|
hashAes1Rx4<softAes>(scratchpad, ScratchpadSize, ®.a);
|
2019-08-15 20:18:35 +00:00
|
|
|
rx_blake2b(out, outSize, ®, sizeof(RegisterFile), nullptr, 0);
|
2019-07-01 18:11:51 +00:00
|
|
|
}
|
|
|
|
|
2019-07-16 21:33:11 +00:00
|
|
|
template<bool softAes>
|
|
|
|
void VmBase<softAes>::initScratchpad(void* seed) {
|
2019-07-01 18:11:51 +00:00
|
|
|
fillAes1Rx4<softAes>(seed, ScratchpadSize, scratchpad);
|
|
|
|
}
|
|
|
|
|
2019-07-16 21:33:11 +00:00
|
|
|
template<bool softAes>
|
|
|
|
void VmBase<softAes>::generateProgram(void* seed) {
|
2019-07-25 14:38:33 +00:00
|
|
|
fillAes4Rx4<softAes>(seed, 128 + RandomX_CurrentConfig.ProgramSize * 8, &program);
|
2019-07-01 18:11:51 +00:00
|
|
|
}
|
|
|
|
|
2019-07-16 21:33:11 +00:00
|
|
|
template class VmBase<false>;
|
|
|
|
template class VmBase<true>;
|
|
|
|
}
|