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-20 14:53:06 +00:00
|
|
|
/* Original code from Argon2 reference source code package used under CC0 Licence
|
|
|
|
* https://github.com/P-H-C/phc-winner-argon2
|
|
|
|
* Copyright 2015
|
|
|
|
* Daniel Dinu, Dmitry Khovratovich, Jean-Philippe Aumasson, and Samuel Neves
|
|
|
|
*/
|
|
|
|
|
2018-12-11 20:00:30 +00:00
|
|
|
#include <new>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <stdexcept>
|
2018-12-15 22:13:17 +00:00
|
|
|
#include <cstring>
|
2019-03-10 22:14:03 +00:00
|
|
|
#include <limits>
|
2019-04-20 09:08:01 +00:00
|
|
|
#include <cstring>
|
2018-12-11 20:00:30 +00:00
|
|
|
|
2018-12-19 20:54:44 +00:00
|
|
|
#include "common.hpp"
|
|
|
|
#include "dataset.hpp"
|
2019-04-20 14:53:06 +00:00
|
|
|
#include "virtual_memory.hpp"
|
|
|
|
#include "superscalar.hpp"
|
|
|
|
#include "blake2_generator.hpp"
|
2019-04-20 09:08:01 +00:00
|
|
|
#include "reciprocal.h"
|
2019-02-09 14:45:26 +00:00
|
|
|
#include "blake2/endian.h"
|
2019-04-20 09:08:01 +00:00
|
|
|
#include "argon2.h"
|
|
|
|
#include "argon2_core.h"
|
2018-12-11 20:00:30 +00:00
|
|
|
|
2019-04-20 09:08:01 +00:00
|
|
|
static_assert(RANDOMX_ARGON_MEMORY % (RANDOMX_ARGON_LANES * ARGON2_SYNC_POINTS) == 0, "RANDOMX_ARGON_MEMORY - invalid value");
|
2019-04-26 14:05:30 +00:00
|
|
|
static_assert(ARGON2_BLOCK_SIZE == randomx::ArgonBlockSize, "Unpexpected value of ARGON2_BLOCK_SIZE");
|
2019-04-20 09:08:01 +00:00
|
|
|
|
|
|
|
namespace randomx {
|
2019-03-15 17:00:51 +00:00
|
|
|
|
2019-04-28 10:44:28 +00:00
|
|
|
void initCache(randomx_cache* cache, const void* seed, size_t seedSize) {
|
|
|
|
uint32_t memory_blocks, segment_length;
|
|
|
|
argon2_instance_t instance;
|
|
|
|
argon2_context context;
|
|
|
|
|
|
|
|
context.out = nullptr;
|
|
|
|
context.outlen = 0;
|
|
|
|
context.pwd = CONST_CAST(uint8_t *)seed;
|
|
|
|
context.pwdlen = (uint32_t)seedSize;
|
|
|
|
context.salt = CONST_CAST(uint8_t *)RANDOMX_ARGON_SALT;
|
|
|
|
context.saltlen = (uint32_t)randomx::ArgonSaltSize;
|
|
|
|
context.secret = NULL;
|
|
|
|
context.secretlen = 0;
|
|
|
|
context.ad = NULL;
|
|
|
|
context.adlen = 0;
|
|
|
|
context.t_cost = RANDOMX_ARGON_ITERATIONS;
|
|
|
|
context.m_cost = RANDOMX_ARGON_MEMORY;
|
|
|
|
context.lanes = RANDOMX_ARGON_LANES;
|
|
|
|
context.threads = 1;
|
|
|
|
context.allocate_cbk = NULL;
|
|
|
|
context.free_cbk = NULL;
|
|
|
|
context.flags = ARGON2_DEFAULT_FLAGS;
|
|
|
|
context.version = ARGON2_VERSION_NUMBER;
|
|
|
|
|
|
|
|
/* 2. Align memory size */
|
|
|
|
/* Minimum memory_blocks = 8L blocks, where L is the number of lanes */
|
|
|
|
memory_blocks = context.m_cost;
|
|
|
|
|
|
|
|
segment_length = memory_blocks / (context.lanes * ARGON2_SYNC_POINTS);
|
|
|
|
|
|
|
|
instance.version = context.version;
|
|
|
|
instance.memory = NULL;
|
|
|
|
instance.passes = context.t_cost;
|
|
|
|
instance.memory_blocks = memory_blocks;
|
|
|
|
instance.segment_length = segment_length;
|
|
|
|
instance.lane_length = segment_length * ARGON2_SYNC_POINTS;
|
|
|
|
instance.lanes = context.lanes;
|
|
|
|
instance.threads = context.threads;
|
|
|
|
instance.type = Argon2_d;
|
|
|
|
instance.memory = (block*)cache->memory;
|
|
|
|
|
|
|
|
if (instance.threads > instance.lanes) {
|
|
|
|
instance.threads = instance.lanes;
|
|
|
|
}
|
2018-12-11 20:00:30 +00:00
|
|
|
|
2019-04-28 10:44:28 +00:00
|
|
|
/* 3. Initialization: Hashing inputs, allocating memory, filling first
|
|
|
|
* blocks
|
|
|
|
*/
|
|
|
|
argon_initialize(&instance, &context);
|
|
|
|
|
|
|
|
fill_memory_blocks(&instance);
|
|
|
|
|
|
|
|
cache->reciprocalCache.clear();
|
|
|
|
randomx::Blake2Generator gen(seed, seedSize);
|
|
|
|
for (int i = 0; i < RANDOMX_CACHE_ACCESSES; ++i) {
|
|
|
|
randomx::generateSuperscalar(cache->programs[i], gen);
|
|
|
|
for (unsigned j = 0; j < cache->programs[i].getSize(); ++j) {
|
|
|
|
auto& instr = cache->programs[i](j);
|
|
|
|
if (instr.opcode == randomx::SuperscalarInstructionType::IMUL_RCP) {
|
|
|
|
auto rcp = randomx_reciprocal(instr.getImm32());
|
|
|
|
instr.setImm32(cache->reciprocalCache.size());
|
|
|
|
cache->reciprocalCache.push_back(rcp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-13 12:47:25 +00:00
|
|
|
}
|
2018-12-11 20:00:30 +00:00
|
|
|
|
2019-04-28 10:44:28 +00:00
|
|
|
void initCacheCompile(randomx_cache* cache, const void* seed, size_t seedSize) {
|
|
|
|
initCache(cache, seed, seedSize);
|
|
|
|
cache->jit->generateSuperscalarHash(cache->programs, cache->reciprocalCache);
|
|
|
|
cache->jit->generateDatasetInitCode();
|
2018-12-11 20:00:30 +00:00
|
|
|
}
|
|
|
|
|
2019-04-20 09:08:01 +00:00
|
|
|
constexpr uint64_t superscalarMul0 = 6364136223846793005ULL;
|
2019-04-26 14:05:30 +00:00
|
|
|
constexpr uint64_t superscalarAdd1 = 9298411001130361340ULL;
|
2019-04-20 09:08:01 +00:00
|
|
|
constexpr uint64_t superscalarAdd2 = 12065312585734608966ULL;
|
2019-04-26 14:05:30 +00:00
|
|
|
constexpr uint64_t superscalarAdd3 = 9306329213124626780ULL;
|
2019-04-20 09:08:01 +00:00
|
|
|
constexpr uint64_t superscalarAdd4 = 5281919268842080866ULL;
|
|
|
|
constexpr uint64_t superscalarAdd5 = 10536153434571861004ULL;
|
|
|
|
constexpr uint64_t superscalarAdd6 = 3398623926847679864ULL;
|
|
|
|
constexpr uint64_t superscalarAdd7 = 9549104520008361294ULL;
|
|
|
|
|
|
|
|
static inline uint8_t* getMixBlock(uint64_t registerValue, uint8_t *memory) {
|
2019-04-26 14:05:30 +00:00
|
|
|
constexpr uint32_t mask = CacheSize / CacheLineSize - 1;
|
2019-04-20 09:08:01 +00:00
|
|
|
return memory + (registerValue & mask) * CacheLineSize;
|
2018-12-11 20:00:30 +00:00
|
|
|
}
|
|
|
|
|
2019-04-22 13:13:41 +00:00
|
|
|
void initDatasetItem(randomx_cache* cache, uint8_t* out, uint64_t itemNumber) {
|
2019-04-20 09:08:01 +00:00
|
|
|
int_reg_t rl[8];
|
|
|
|
uint8_t* mixBlock;
|
2019-04-22 13:13:41 +00:00
|
|
|
uint64_t registerValue = itemNumber;
|
|
|
|
rl[0] = (itemNumber + 1) * superscalarMul0;
|
2019-04-20 09:08:01 +00:00
|
|
|
rl[1] = rl[0] ^ superscalarAdd1;
|
|
|
|
rl[2] = rl[0] ^ superscalarAdd2;
|
|
|
|
rl[3] = rl[0] ^ superscalarAdd3;
|
|
|
|
rl[4] = rl[0] ^ superscalarAdd4;
|
|
|
|
rl[5] = rl[0] ^ superscalarAdd5;
|
|
|
|
rl[6] = rl[0] ^ superscalarAdd6;
|
|
|
|
rl[7] = rl[0] ^ superscalarAdd7;
|
|
|
|
for (unsigned i = 0; i < RANDOMX_CACHE_ACCESSES; ++i) {
|
|
|
|
mixBlock = getMixBlock(registerValue, cache->memory);
|
|
|
|
SuperscalarProgram& prog = cache->programs[i];
|
|
|
|
|
|
|
|
executeSuperscalar(rl, prog, &cache->reciprocalCache);
|
|
|
|
|
|
|
|
for (unsigned q = 0; q < 8; ++q)
|
2019-04-24 16:37:58 +00:00
|
|
|
rl[q] ^= load64_native(mixBlock + 8 * q);
|
2019-04-20 09:08:01 +00:00
|
|
|
|
|
|
|
registerValue = rl[prog.getAddressRegister()];
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(out, &rl, CacheLineSize);
|
2019-01-14 23:01:11 +00:00
|
|
|
}
|
2018-12-11 20:00:30 +00:00
|
|
|
|
2019-04-22 13:13:41 +00:00
|
|
|
void initDataset(randomx_cache* cache, uint8_t* dataset, uint32_t startItem, uint32_t endItem) {
|
|
|
|
for (uint32_t itemNumber = startItem; itemNumber < endItem; ++itemNumber, dataset += CacheLineSize)
|
|
|
|
initDatasetItem(cache, dataset, itemNumber);
|
2019-04-20 09:08:01 +00:00
|
|
|
}
|
2018-12-15 22:13:17 +00:00
|
|
|
}
|