Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Implement RandomX algorithm #81

Open
wants to merge 2 commits into
base: feature/8.22.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ if test "x${ARFLAGS+set}" != "xset"; then
ARFLAGS="cr"
fi

AM_PROG_AS

AC_CANONICAL_HOST

AH_TOP([#ifndef DIGIBYTE_CONFIG_H])
Expand Down
40 changes: 36 additions & 4 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ libdigibyte_wallet_tool_a_SOURCES = \
$(DIGIBYTE_CORE_H)

# crypto primitives library
crypto_libdigibyte_crypto_base_a_CPPFLAGS = $(AM_CPPFLAGS)
crypto_libdigibyte_crypto_base_a_CPPFLAGS = $(AM_CPPFLAGS) $(DIGIBYTE_INCLUDES)
crypto_libdigibyte_crypto_base_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
crypto_libdigibyte_crypto_base_a_SOURCES = \
crypto/aes.cpp \
Expand Down Expand Up @@ -476,6 +476,35 @@ crypto_libdigibyte_crypto_base_a_SOURCES = \
crypto/siphash.cpp \
crypto/siphash.h

crypto_libdigibyte_crypto_base_a_SOURCES += \
crypto/randomx/aes_hash.cpp \
crypto/randomx/allocator.cpp \
crypto/randomx/argon2_avx2.c \
crypto/randomx/argon2_core.c \
crypto/randomx/argon2_ref.c \
crypto/randomx/argon2_ssse3.c \
crypto/randomx/assembly_generator_x86.cpp \
crypto/randomx/blake2/blake2b.c \
crypto/randomx/blake2_generator.cpp \
crypto/randomx/bytecode_machine.cpp \
crypto/randomx/cpu.cpp \
crypto/randomx/dataset.cpp \
crypto/randomx/instruction.cpp \
crypto/randomx/instructions_portable.cpp \
crypto/randomx/jit_compiler_x86.cpp \
crypto/randomx/jit_compiler_x86_static.S \
crypto/randomx/randomx.cpp \
crypto/randomx/reciprocal.c \
crypto/randomx/soft_aes.cpp \
crypto/randomx/superscalar.cpp \
crypto/randomx/virtual_machine.cpp \
crypto/randomx/virtual_memory.cpp \
crypto/randomx/vm_compiled.cpp \
crypto/randomx/vm_compiled_light.cpp \
crypto/randomx/vm_interpreted.cpp \
crypto/randomx/vm_interpreted_light.cpp \
crypto/randomx.cpp \
crypto/seedman.cpp

if USE_ASM
crypto_libdigibyte_crypto_base_a_SOURCES += crypto/sha256_sse4.cpp
Expand Down Expand Up @@ -548,6 +577,8 @@ libdigibyte_consensus_a_SOURCES = \
prevector.h \
primitives/block.cpp \
primitives/block.h \
primitives/cryptonote.cpp \
primitives/cryptonote.h \
primitives/transaction.cpp \
primitives/transaction.h \
pubkey.cpp \
Expand Down Expand Up @@ -755,7 +786,7 @@ digibyte_wallet_SOURCES = digibyte-wallet.cpp
digibyte_wallet_CPPFLAGS = $(digibyte_bin_cppflags)
digibyte_wallet_CXXFLAGS = $(digibyte_bin_cxxflags)
digibyte_wallet_LDFLAGS = $(digibyte_bin_ldflags)
digibyte_wallet_LDADD = $(LIBDIGIBYTE_WALLET_TOOL) $(digibyte_bin_ldadd)
digibyte_wallet_LDADD = $(EXTRA_LIBRARIES) $(LIBDIGIBYTE_WALLET_TOOL) $(digibyte_bin_ldadd)

if TARGET_WINDOWS
digibyte_wallet_SOURCES += digibyte-wallet-res.rc
Expand All @@ -778,7 +809,8 @@ digibyte_util_LDADD = \
$(LIBUNIVALUE) \
$(LIBDIGIBYTE_CONSENSUS) \
$(LIBDIGIBYTE_CRYPTO) \
$(LIBSECP256K1)
$(LIBSECP256K1) \
$(EXTRA_LIBRARIES)

digibyte_util_LDADD += $(BOOST_LIBS)
#
Expand All @@ -794,7 +826,7 @@ endif

libdigibyteconsensus_la_LDFLAGS = $(AM_LDFLAGS) -no-undefined $(RELDFLAGS)
libdigibyteconsensus_la_LIBADD = $(LIBSECP256K1)
libdigibyteconsensus_la_CPPFLAGS = $(AM_CPPFLAGS) -I$(builddir)/obj -I$(srcdir)/secp256k1/include -DBUILD_DIGIBYTE_INTERNAL
libdigibyteconsensus_la_CPPFLAGS = $(AM_CPPFLAGS) $(DIGIBYTE_INCLUDES) -I$(builddir)/obj -I$(srcdir)/secp256k1/include -DBUILD_DIGIBYTE_INTERNAL
libdigibyteconsensus_la_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)

endif
Expand Down
2 changes: 2 additions & 0 deletions src/chain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ int GetAlgoWorkFactor(int nHeight, int algo)
return 4 * 6;
case ALGO_QUBIT:
return 128 * 8;
case ALGO_RANDOMX:
return 1;
default:
return 1;
}
Expand Down
2 changes: 1 addition & 1 deletion src/chain.h
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ class CBlockIndex
uint256 GetBlockPoWHash() const
{
CBlockHeader block = GetBlockHeader();
return GetPoWAlgoHash(block);
return GetPoWAlgoHash(block, nHeight);
}

int GetAlgo() const
Expand Down
16 changes: 7 additions & 9 deletions src/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,8 @@ class CTestNetParams : public CChainParams {
public:
CTestNetParams() {
strNetworkID = "test";
consensus.powLimit = ArithToUint256(~arith_uint256(0) >> 20);
consensus.initialTarget[ALGO_ODO] = ArithToUint256(~arith_uint256(0) >> 36); // 16 difficulty
consensus.powLimit = uint256S("0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
consensus.initialTarget[ALGO_ODO] = uint256S("0x000000000fffffffffffffffffffffffffffffffffffffffffffffffffffffff");
consensus.nSubsidyHalvingInterval = 300;
consensus.nPowTargetTimespan = 14 * 24 * 60 * 60; // two weeks
consensus.nPowTargetSpacing = 60 / 4;
Expand All @@ -300,9 +300,7 @@ class CTestNetParams : public CChainParams {
consensus.SegwitHeight = 0; // SEGWIT is always activated on regtest unless overridden
consensus.ReserveAlgoBitsHeight = 0;
consensus.OdoHeight = 600;

consensus.MinBIP9WarningHeight = 0;
consensus.powLimit = uint256S("7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");

/** Current DigiByte 2017 Difficulty Adjustment Code & Block Target. See explanation here:
https://github.com/digibyte/digibyte-old/pull/36
Expand Down Expand Up @@ -359,13 +357,13 @@ class CTestNetParams : public CChainParams {


// DigiByte Hard Fork Block Heights
consensus.multiAlgoDiffChangeTarget = 100; // Block 145,000 MultiAlgo Hard Fork
consensus.multiAlgoDiffChangeTarget = 0; // Block 145,000 MultiAlgo Hard Fork
consensus.alwaysUpdateDiffChangeTarget = 400; // Block 400,000 MultiShield Hard Fork
consensus.workComputationChangeTarget = 1430; // Block 1,430,000 DigiSpeed Hard Fork
consensus.algoSwapChangeTarget = 20000; // Block 9,000,000 Odo PoW Hard Fork

consensus.fPowAllowMinDifficultyBlocks = true;
consensus.fPowNoRetargeting = true;
consensus.fPowAllowMinDifficultyBlocks = false;
consensus.fPowNoRetargeting = false;
consensus.nRuleChangeActivationThreshold = 4032; // 4032 - 70% of 5760
consensus.nMinerConfirmationWindow = 5760; // 1 day of blocks on testnet
consensus.fRbfEnabled = false;
Expand Down Expand Up @@ -398,9 +396,9 @@ class CTestNetParams : public CChainParams {
m_assumed_blockchain_size = 40;
m_assumed_chain_state_size = 2;

genesis = CreateGenesisBlock(1516939474, 2411473, 0x1e0ffff0, 1, 8000 * COIN);
genesis = CreateGenesisBlock(1516939474, 0, 0x2007ffff, 1, 8000 * COIN);
consensus.hashGenesisBlock = genesis.GetHash();
assert(consensus.hashGenesisBlock == uint256S("0x308ea0711d5763be2995670dd9ca9872753561285a84da1d58be58acaa822252"));
assert(consensus.hashGenesisBlock == uint256S("0xa5e8d47bd25ae46716c9f0ff031efdac0fc1ea4b3238a30da656158092fab288"));
assert(genesis.hashMerkleRoot == uint256S("0x72ddd9496b004221ed0557358846d9248ecd4c440ebd28ed901efc18757d0fad"));

vFixedSeeds.clear();
Expand Down
85 changes: 85 additions & 0 deletions src/crypto/randomx.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright (c) 2021 barrystyle
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <crypto/randomx.h>

#include <chainparams.h>
#include <crypto/randomx/randomx.h>
#include <crypto/seedman.h>
#include <primitives/block.h>
#include <primitives/cryptonote.h>
#include <sync.h>

SeedManager seedmanager;
RandomXManager rxmanager;

void RandomXManager::CacheInit()
{
if (!cache) {
flags = randomx_get_flags();
cache = randomx_alloc_cache(flags);
}

randomx_init_cache(cache, &seed, 32);
}

void RandomXManager::VmInit()
{
if (vm) {
randomx_destroy_vm(vm);
}

vm = randomx_create_vm(flags, cache, nullptr);
}

void RandomXManager::Shutoff()
{
if (vm) {
randomx_destroy_vm(vm);
vm = nullptr;
}

if (cache) {
randomx_release_cache(cache);
cache = nullptr;
}
}

bool RandomXManager::HasSeedChanged(int height)
{
uint256 seed_for_height = seedmanager.GetSeedForHeight(height);
if (seed != seed_for_height) {
return true;
}

return false;
}

void RandomXManager::UpdateSeed(int height)
{
seed = seedmanager.GetSeedForHeight(height);
LogPrintf("seed changed to %s at height %d\n", seed.ToString(), height);
}

void RandomXManager::Hash(const char* input, char* output, int height)
{
if (!cache) {
CacheInit();
}

if (!vm) {
VmInit();
}

bool refresh = HasSeedChanged(height);

if (refresh) {
UpdateSeed(height);
Shutoff();
CacheInit();
VmInit();
}

randomx_calculate_hash(vm, input, 76, output);
}
46 changes: 46 additions & 0 deletions src/crypto/randomx.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) 2021 barrystyle
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef DIGIBYTE_RANDOMX_H
#define DIGIBYTE_RANDOMX_H

#include <crypto/randomx/randomx.h>
#include <crypto/seedman.h>
#include <uint256.h>

class RandomXManager;
class uint256;
class CBlockIndex;
class CBlockHeader;

extern SeedManager seedmanager;
extern RandomXManager rxmanager;

class RandomXManager {

public:
uint256 seed;

private:
randomx_flags flags;
randomx_vm* vm;
randomx_cache* cache;

public:
RandomXManager()
{
seed = uint256();
vm = nullptr;
cache = nullptr;
}

void CacheInit();
void VmInit();
void Shutoff();
bool HasSeedChanged(int height);
void UpdateSeed(int height);
void Hash(const char* input, char* output, int height);
};

#endif // DIGIBYTE_RANDOMX_H
Loading