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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <cstdint>
|
2018-12-31 18:06:45 +00:00
|
|
|
#include <iostream>
|
2018-12-11 20:00:30 +00:00
|
|
|
|
|
|
|
namespace RandomX {
|
|
|
|
|
|
|
|
using addr_t = uint32_t;
|
|
|
|
|
|
|
|
constexpr int RoundToNearest = 0;
|
|
|
|
constexpr int RoundDown = 1;
|
|
|
|
constexpr int RoundUp = 2;
|
|
|
|
constexpr int RoundToZero = 3;
|
|
|
|
|
|
|
|
constexpr int SeedSize = 32;
|
2018-12-19 11:38:10 +00:00
|
|
|
constexpr int ResultSize = 32;
|
2018-12-11 20:00:30 +00:00
|
|
|
|
|
|
|
constexpr int CacheBlockSize = 1024;
|
2018-12-15 22:13:17 +00:00
|
|
|
constexpr int CacheShift = CacheBlockSize / 2;
|
2018-12-11 20:00:30 +00:00
|
|
|
constexpr int BlockExpansionRatio = 64;
|
|
|
|
constexpr uint32_t DatasetBlockSize = BlockExpansionRatio * CacheBlockSize;
|
|
|
|
constexpr uint32_t DatasetBlockCount = 65536;
|
|
|
|
constexpr uint32_t CacheSize = DatasetBlockCount * CacheBlockSize;
|
|
|
|
constexpr uint64_t DatasetSize = (uint64_t)DatasetBlockCount * DatasetBlockSize;
|
|
|
|
|
|
|
|
constexpr int ArgonIterations = 12;
|
|
|
|
constexpr uint32_t ArgonMemorySize = 65536; //KiB
|
|
|
|
constexpr int ArgonLanes = 1;
|
|
|
|
const char ArgonSalt[] = "Monero\x1A$";
|
|
|
|
constexpr int ArgonSaltSize = sizeof(ArgonSalt) - 1;
|
|
|
|
|
|
|
|
#ifdef TRACE
|
|
|
|
constexpr bool trace = true;
|
|
|
|
#else
|
|
|
|
constexpr bool trace = false;
|
|
|
|
#endif
|
|
|
|
|
2018-12-19 20:54:44 +00:00
|
|
|
union convertible_t {
|
2018-12-11 20:00:30 +00:00
|
|
|
double f64;
|
|
|
|
int64_t i64;
|
|
|
|
uint64_t u64;
|
|
|
|
int32_t i32;
|
|
|
|
uint32_t u32;
|
2018-12-31 18:06:45 +00:00
|
|
|
struct {
|
|
|
|
int32_t i32lo;
|
|
|
|
int32_t i32hi;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
struct fpu_reg_t {
|
|
|
|
convertible_t lo;
|
|
|
|
convertible_t hi;
|
2018-12-19 20:54:44 +00:00
|
|
|
};
|
2018-12-11 20:00:30 +00:00
|
|
|
|
|
|
|
constexpr int ProgramLength = 512;
|
2018-12-27 20:42:38 +00:00
|
|
|
constexpr uint32_t InstructionCount = 1024 * 1024;
|
2018-12-11 20:00:30 +00:00
|
|
|
constexpr uint32_t ScratchpadSize = 256 * 1024;
|
|
|
|
constexpr uint32_t ScratchpadLength = ScratchpadSize / sizeof(convertible_t);
|
|
|
|
constexpr uint32_t ScratchpadL1 = ScratchpadSize / 16 / sizeof(convertible_t);
|
|
|
|
constexpr uint32_t ScratchpadL2 = ScratchpadSize / sizeof(convertible_t);
|
|
|
|
constexpr int RegistersCount = 8;
|
|
|
|
|
2018-12-19 20:54:44 +00:00
|
|
|
class Cache;
|
|
|
|
|
2018-12-18 21:00:58 +00:00
|
|
|
inline int wrapInstr(int i) {
|
|
|
|
return i % RandomX::ProgramLength;
|
|
|
|
}
|
|
|
|
|
2018-12-19 20:54:44 +00:00
|
|
|
struct LightClientDataset {
|
|
|
|
Cache* cache;
|
2018-12-11 20:00:30 +00:00
|
|
|
uint8_t* block;
|
|
|
|
uint32_t blockNumber;
|
2018-12-19 20:54:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
union dataset_t {
|
|
|
|
uint8_t* dataset;
|
|
|
|
Cache* cache;
|
|
|
|
LightClientDataset* lightDataset;
|
2018-12-11 20:00:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct MemoryRegisters {
|
|
|
|
addr_t ma, mx;
|
2018-12-19 20:54:44 +00:00
|
|
|
dataset_t ds;
|
2018-12-11 20:00:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static_assert(sizeof(MemoryRegisters) == 2 * sizeof(addr_t) + sizeof(uintptr_t), "Invalid alignment of struct RandomX::MemoryRegisters");
|
|
|
|
|
|
|
|
struct RegisterFile {
|
|
|
|
convertible_t r[RegistersCount];
|
2018-12-31 18:06:45 +00:00
|
|
|
fpu_reg_t f[RegistersCount];
|
2018-12-11 20:00:30 +00:00
|
|
|
};
|
|
|
|
|
2018-12-31 18:06:45 +00:00
|
|
|
static_assert(sizeof(RegisterFile) == 3 * RegistersCount * sizeof(convertible_t), "Invalid alignment of struct RandomX::RegisterFile");
|
2018-12-11 20:00:30 +00:00
|
|
|
|
2018-12-13 22:11:55 +00:00
|
|
|
typedef convertible_t(*DatasetReadFunc)(addr_t, MemoryRegisters&);
|
|
|
|
|
2018-12-18 21:00:58 +00:00
|
|
|
typedef void(*ProgramFunc)(RegisterFile&, MemoryRegisters&, convertible_t*);
|
|
|
|
|
2018-12-11 20:00:30 +00:00
|
|
|
extern "C" {
|
2018-12-21 20:09:55 +00:00
|
|
|
void executeProgram(RegisterFile&, MemoryRegisters&, convertible_t*, DatasetReadFunc);
|
2018-12-11 20:00:30 +00:00
|
|
|
}
|
2018-12-31 18:06:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::ostream& operator<<(std::ostream& os, const RandomX::RegisterFile& rf);
|