virtual_machine.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. Copyright (c) 2018 tevador
  3. This file is part of RandomX.
  4. RandomX is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. RandomX is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with RandomX. If not, see<http://www.gnu.org/licenses/>.
  14. */
  15. #include <cstring>
  16. #include <iomanip>
  17. #include <stdexcept>
  18. #include "virtual_machine.hpp"
  19. #include "common.hpp"
  20. #include "aes_hash.hpp"
  21. #include "blake2/blake2.h"
  22. #include "intrin_portable.h"
  23. #include "allocator.hpp"
  24. randomx_vm::~randomx_vm() {
  25. }
  26. void randomx_vm::resetRoundingMode() {
  27. rx_reset_float_state();
  28. }
  29. namespace randomx {
  30. static inline uint64_t getSmallPositiveFloatBits(uint64_t entropy) {
  31. auto exponent = entropy >> 59; //0..31
  32. auto mantissa = entropy & mantissaMask;
  33. exponent += exponentBias;
  34. exponent &= exponentMask;
  35. exponent <<= mantissaSize;
  36. return exponent | mantissa;
  37. }
  38. static inline uint64_t getStaticExponent(uint64_t entropy) {
  39. auto exponent = constExponentBits;
  40. exponent |= (entropy >> (64 - staticExponentBits)) << dynamicExponentBits;
  41. exponent <<= mantissaSize;
  42. return exponent;
  43. }
  44. static inline uint64_t getFloatMask(uint64_t entropy) {
  45. constexpr uint64_t mask22bit = (1ULL << 22) - 1;
  46. return (entropy & mask22bit) | getStaticExponent(entropy);
  47. }
  48. }
  49. void randomx_vm::initialize() {
  50. store64(&reg.a[0].lo, randomx::getSmallPositiveFloatBits(program.getEntropy(0)));
  51. store64(&reg.a[0].hi, randomx::getSmallPositiveFloatBits(program.getEntropy(1)));
  52. store64(&reg.a[1].lo, randomx::getSmallPositiveFloatBits(program.getEntropy(2)));
  53. store64(&reg.a[1].hi, randomx::getSmallPositiveFloatBits(program.getEntropy(3)));
  54. store64(&reg.a[2].lo, randomx::getSmallPositiveFloatBits(program.getEntropy(4)));
  55. store64(&reg.a[2].hi, randomx::getSmallPositiveFloatBits(program.getEntropy(5)));
  56. store64(&reg.a[3].lo, randomx::getSmallPositiveFloatBits(program.getEntropy(6)));
  57. store64(&reg.a[3].hi, randomx::getSmallPositiveFloatBits(program.getEntropy(7)));
  58. mem.ma = program.getEntropy(8) & randomx::CacheLineAlignMask;
  59. mem.mx = program.getEntropy(10);
  60. auto addressRegisters = program.getEntropy(12);
  61. config.readReg0 = 0 + (addressRegisters & 1);
  62. addressRegisters >>= 1;
  63. config.readReg1 = 2 + (addressRegisters & 1);
  64. addressRegisters >>= 1;
  65. config.readReg2 = 4 + (addressRegisters & 1);
  66. addressRegisters >>= 1;
  67. config.readReg3 = 6 + (addressRegisters & 1);
  68. datasetOffset = (program.getEntropy(13) & randomx::DatasetExtraItems) * randomx::CacheLineSize;
  69. store64(&config.eMask[0], randomx::getFloatMask(program.getEntropy(14)));
  70. store64(&config.eMask[1], randomx::getFloatMask(program.getEntropy(15)));
  71. }
  72. namespace randomx {
  73. alignas(16) volatile static rx_vec_i128 aesDummy;
  74. template<class Allocator, bool softAes>
  75. VmBase<Allocator, softAes>::~VmBase() {
  76. Allocator::freeMemory(scratchpad, ScratchpadSize);
  77. }
  78. template<class Allocator, bool softAes>
  79. void VmBase<Allocator, softAes>::allocate() {
  80. if (datasetPtr == nullptr)
  81. throw std::invalid_argument("Cache/Dataset not set");
  82. if (!softAes) { //if hardware AES is not supported, it's better to fail now than to return a ticking bomb
  83. rx_vec_i128 tmp = rx_load_vec_i128((const rx_vec_i128*)&aesDummy);
  84. tmp = rx_aesenc_vec_i128(tmp, tmp);
  85. rx_store_vec_i128((rx_vec_i128*)&aesDummy, tmp);
  86. }
  87. scratchpad = (uint8_t*)Allocator::allocMemory(ScratchpadSize);
  88. }
  89. template<class Allocator, bool softAes>
  90. void VmBase<Allocator, softAes>::getFinalResult(void* out, size_t outSize) {
  91. hashAes1Rx4<softAes>(scratchpad, ScratchpadSize, &reg.a);
  92. blake2b(out, outSize, &reg, sizeof(RegisterFile), nullptr, 0);
  93. }
  94. template<class Allocator, bool softAes>
  95. void VmBase<Allocator, softAes>::initScratchpad(void* seed) {
  96. fillAes1Rx4<softAes>(seed, ScratchpadSize, scratchpad);
  97. }
  98. template<class Allocator, bool softAes>
  99. void VmBase<Allocator, softAes>::generateProgram(void* seed) {
  100. fillAes1Rx4<softAes>(seed, sizeof(program), &program);
  101. }
  102. template class VmBase<AlignedAllocator<CacheLineSize>, false>;
  103. template class VmBase<AlignedAllocator<CacheLineSize>, true>;
  104. template class VmBase<LargePageAllocator, false>;
  105. template class VmBase<LargePageAllocator, true>;
  106. }