Skip to content

Commit

Permalink
Add Flex algo (for KCN coin) algo support
Browse files Browse the repository at this point in the history
  • Loading branch information
MoneroOcean committed May 27, 2024
1 parent 7445870 commit bf3831c
Show file tree
Hide file tree
Showing 43 changed files with 5,160 additions and 23 deletions.
11 changes: 11 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,17 @@ set(SOURCES_CRYPTO
src/crypto/common/MemoryPool.cpp
src/crypto/common/Nonce.cpp
src/crypto/common/VirtualMemory.cpp
src/crypto/flex/flex.cpp
src/crypto/flex/cryptonote/cryptonight_dark.c
src/crypto/flex/cryptonote/cryptonight_dark_lite.c
src/crypto/flex/cryptonote/cryptonight_fast.c
src/crypto/flex/cryptonote/cryptonight_lite.c
src/crypto/flex/cryptonote/cryptonight_turtle.c
src/crypto/flex/cryptonote/cryptonight_turtle_lite.c
src/crypto/flex/cryptonote/crypto/oaes_lib.c
src/crypto/flex/cryptonote/crypto/aesb.c
src/crypto/flex/cryptonote/crypto/hash.c
src/crypto/flex/cryptonote/crypto/c_keccak.c
)

if (WITH_MO_BENCHMARK)
Expand Down
5 changes: 4 additions & 1 deletion src/backend/cpu/CpuConfig_gen.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,10 @@ size_t inline generate<Algorithm::ARGON2>(Threads<CpuThreads> &threads, uint32_t
template<>
size_t inline generate<Algorithm::GHOSTRIDER>(Threads<CpuThreads>& threads, uint32_t limit)
{
return generate(Algorithm::kGHOSTRIDER, threads, Algorithm::GHOSTRIDER_RTM, limit);
size_t count = 0;
count += generate(Algorithm::kGHOSTRIDER, threads, Algorithm::GHOSTRIDER_RTM, limit);
count += generate(Algorithm::kFLEX, threads, Algorithm::FLEX_KCN, limit);
return count;
}
#endif

Expand Down
44 changes: 37 additions & 7 deletions src/backend/cpu/CpuWorker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "crypto/rx/RxDataset.h"
#include "crypto/rx/RxVm.h"
#include "crypto/ghostrider/ghostrider.h"
#include "crypto/flex/flex.h"
#include "net/JobResults.h"


Expand Down Expand Up @@ -168,7 +169,13 @@ bool xmrig::CpuWorker<N>::selfTest()

# ifdef XMRIG_ALGO_GHOSTRIDER
if (m_algorithm.family() == Algorithm::GHOSTRIDER) {
return (N == 8) && verify(Algorithm::GHOSTRIDER_RTM, test_output_gr);
switch (m_algorithm.id()) {
case Algorithm::GHOSTRIDER_RTM:
return (N == 8) && verify(Algorithm::GHOSTRIDER_RTM, test_output_gr);
case Algorithm::FLEX_KCN:
return verify(Algorithm::FLEX_KCN, test_output_flex);
default:;
}
}
# endif

Expand Down Expand Up @@ -327,11 +334,20 @@ void xmrig::CpuWorker<N>::start()

# ifdef XMRIG_ALGO_GHOSTRIDER
case Algorithm::GHOSTRIDER:
if (N == 8) {
ghostrider::hash_octa(m_job.blob(), job.size(), m_hash, m_ctx, m_ghHelper);
}
else {
valid = false;
switch (job.algorithm()) {
case Algorithm::GHOSTRIDER_RTM:
if (N == 8) {
ghostrider::hash_octa(m_job.blob(), job.size(), m_hash, m_ctx, m_ghHelper);
}
else {
valid = false;
}
break;
case Algorithm::FLEX_KCN:
flex_hash(reinterpret_cast<const char*>(m_job.blob()), reinterpret_cast<char*>(m_hash), m_ctx);
break;
default:
valid = false;
}
break;
# endif
Expand Down Expand Up @@ -398,7 +414,8 @@ template<size_t N>
bool xmrig::CpuWorker<N>::verify(const Algorithm &algorithm, const uint8_t *referenceValue)
{
# ifdef XMRIG_ALGO_GHOSTRIDER
if (algorithm == Algorithm::GHOSTRIDER_RTM) {
switch (algorithm) {
case Algorithm::GHOSTRIDER_RTM: {
uint8_t blob[N * 80] = {};
for (size_t i = 0; i < N; ++i) {
blob[i * 80 + 0] = static_cast<uint8_t>(i);
Expand All @@ -425,6 +442,19 @@ bool xmrig::CpuWorker<N>::verify(const Algorithm &algorithm, const uint8_t *refe
}

return true;
}
case Algorithm::FLEX_KCN: {
const uint8_t header[80] = {
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1c, 0xcc, 0xa6, 0x6a,
0x44, 0xf8, 0xbd, 0x55, 0x45, 0xc3, 0x16, 0x4a, 0x3a, 0x76, 0xda, 0x50, 0x39, 0x53, 0x28, 0xc9, 0x07, 0x56, 0x33, 0x77,
0x5b, 0xc4, 0xc8, 0x79, 0x8f, 0xd6, 0x77, 0x2b, 0x70, 0x0d, 0x21, 0x5c, 0xf0, 0xff, 0x0f, 0x1e, 0x00, 0x00, 0x00, 0x00
};
char hash[32] = {};
flex_hash(reinterpret_cast<const char*>(header), hash, m_ctx);
return memcmp(referenceValue, hash, sizeof hash) == 0;
}
default:;
}
# endif

Expand Down
8 changes: 7 additions & 1 deletion src/base/crypto/Algorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ const char *Algorithm::kKAWPOW_RVN = "kawpow";
#ifdef XMRIG_ALGO_GHOSTRIDER
const char* Algorithm::kGHOSTRIDER = "ghostrider";
const char* Algorithm::kGHOSTRIDER_RTM = "ghostrider";
const char* Algorithm::kFLEX = "flex";
const char* Algorithm::kFLEX_KCN = "flex";
#endif

#ifdef XMRIG_ALGO_RANDOMX
Expand Down Expand Up @@ -178,6 +180,7 @@ static const std::map<uint32_t, const char *> kAlgorithmNames = {

# ifdef XMRIG_ALGO_GHOSTRIDER
ALGO_NAME(GHOSTRIDER_RTM),
ALGO_NAME(FLEX_KCN),
# endif
};

Expand Down Expand Up @@ -303,6 +306,8 @@ static const std::map<const char *, Algorithm::Id, aliasCompare> kAlgorithmAlias
# ifdef XMRIG_ALGO_GHOSTRIDER
ALGO_ALIAS_AUTO(GHOSTRIDER_RTM), ALGO_ALIAS(GHOSTRIDER_RTM, "ghostrider/rtm"),
ALGO_ALIAS(GHOSTRIDER_RTM, "gr"),
ALGO_ALIAS_AUTO(FLEX_KCN), ALGO_ALIAS(FLEX_KCN, "flex/kcn"),
ALGO_ALIAS(FLEX_KCN, "flex"),
# endif
};

Expand Down Expand Up @@ -380,7 +385,8 @@ std::vector<xmrig::Algorithm> xmrig::Algorithm::all(const std::function<bool(con
RX_XLA,
AR2_CHUKWA, AR2_CHUKWA_V2, AR2_WRKZ,
KAWPOW_RVN,
GHOSTRIDER_RTM
GHOSTRIDER_RTM,
FLEX_KCN
};

Algorithms out;
Expand Down
7 changes: 5 additions & 2 deletions src/base/crypto/Algorithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class Algorithm
CN_GR_4 = 0x63120104, // "cn/turtle" GhostRider
CN_GR_5 = 0x63120105, // "cn/turtle-lite" GhostRider
GHOSTRIDER_RTM = 0x6c150000, // "ghostrider" GhostRider
FLEX_KCN = 0x6c150001, // "flex" Flex
RX_0 = 0x72151200, // "rx/0" RandomX (reference configuration).
RX_WOW = 0x72141177, // "rx/wow" RandomWOW (Wownero).
RX_ARQ = 0x72121061, // "rx/arq" RandomARQ (Arqma).
Expand Down Expand Up @@ -172,6 +173,8 @@ class Algorithm
# ifdef XMRIG_ALGO_GHOSTRIDER
static const char* kGHOSTRIDER;
static const char* kGHOSTRIDER_RTM;
static const char* kFLEX;
static const char* kFLEX_KCN;
# endif

inline Algorithm() = default;
Expand All @@ -193,8 +196,8 @@ class Algorithm
inline Id id() const { return m_id; }
inline size_t l2() const { return l2(m_id); }
inline uint32_t family() const { return family(m_id); }
inline uint32_t minIntensity() const { return ((m_id == GHOSTRIDER_RTM) ? 8 : 1); };
inline uint32_t maxIntensity() const { return isCN() ? 5 : ((m_id == GHOSTRIDER_RTM) ? 8 : 1); };
inline uint32_t minIntensity() const { return ((family(m_id) == GHOSTRIDER) ? 8 : 1); };
inline uint32_t maxIntensity() const { return isCN() ? 5 : ((family(m_id) == GHOSTRIDER) ? 8 : 1); };

inline size_t l3() const { return l3(m_id); }

Expand Down
10 changes: 5 additions & 5 deletions src/base/net/stratum/EthStratumClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ int64_t xmrig::EthStratumClient::submit(const JobResult& result)
params.PushBack(result.jobId.toJSON(), allocator);

# ifdef XMRIG_ALGO_GHOSTRIDER
if (m_pool.algorithm().id() == Algorithm::GHOSTRIDER_RTM) {
if (m_pool.algorithm().family() == Algorithm::GHOSTRIDER) {
params.PushBack(Value("00000000000000000000000000000000", static_cast<uint32_t>(m_extraNonce2Size * 2)), allocator);
params.PushBack(Value(m_ntime.data(), allocator), allocator);

Expand Down Expand Up @@ -114,7 +114,7 @@ int64_t xmrig::EthStratumClient::submit(const JobResult& result)
uint64_t actual_diff;

# ifdef XMRIG_ALGO_GHOSTRIDER
if (result.algorithm == Algorithm::GHOSTRIDER_RTM) {
if (result.algorithm.family() == Algorithm::GHOSTRIDER) {
actual_diff = reinterpret_cast<const uint64_t*>(result.result())[3];
}
else
Expand Down Expand Up @@ -202,7 +202,7 @@ void xmrig::EthStratumClient::parseNotification(const char *method, const rapidj
return;
}

if (m_pool.algorithm().id() != Algorithm::GHOSTRIDER_RTM) {
if (m_pool.algorithm().family() != Algorithm::GHOSTRIDER) {
return;
}

Expand Down Expand Up @@ -236,7 +236,7 @@ void xmrig::EthStratumClient::parseNotification(const char *method, const rapidj
algo = m_pool.coin().algorithm();
}

const size_t min_arr_size = (algo.id() == Algorithm::GHOSTRIDER_RTM) ? 8 : 6;
const size_t min_arr_size = (algo.family() == Algorithm::GHOSTRIDER) ? 8 : 6;

if (arr.Size() < min_arr_size) {
LOG_ERR("%s " RED("invalid mining.notify notification: params array has wrong size"), tag());
Expand All @@ -257,7 +257,7 @@ void xmrig::EthStratumClient::parseNotification(const char *method, const rapidj
std::stringstream s;

# ifdef XMRIG_ALGO_GHOSTRIDER
if (algo.id() == Algorithm::GHOSTRIDER_RTM) {
if (algo.family() == Algorithm::GHOSTRIDER) {
// Raptoreum uses Bitcoin's Stratum protocol
// https://en.bitcoinwiki.org/wiki/Stratum_mining_protocol#mining.notify

Expand Down
2 changes: 1 addition & 1 deletion src/base/net/stratum/benchmark/BenchClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ xmrig::BenchClient::BenchClient(const std::shared_ptr<BenchConfig> &benchmark, I
blob.back() = '\0';

# ifdef XMRIG_ALGO_GHOSTRIDER
if (m_benchmark->algorithm() == Algorithm::GHOSTRIDER_RTM) {
if (m_benchmark->algorithm().family() == Algorithm::GHOSTRIDER) {
const uint32_t q = (benchmark->rotation() / 20) & 1;
const uint32_t r = benchmark->rotation() % 20;

Expand Down
11 changes: 11 additions & 0 deletions src/core/MoBenchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ double MoBenchmark::get_algo_perf(Algorithm::Id algo) const {
case Algorithm::RX_ARQ: return m_bench_algo_perf[BenchAlgo::RX_ARQ];
case Algorithm::RX_XLA: return m_bench_algo_perf[BenchAlgo::RX_XLA];
case Algorithm::GHOSTRIDER_RTM: return m_bench_algo_perf[BenchAlgo::GHOSTRIDER_RTM];
case Algorithm::FLEX_KCN: return m_bench_algo_perf[BenchAlgo::FLEX_KCN];
default: return 0.0f;
}
}
Expand All @@ -165,6 +166,7 @@ void MoBenchmark::start(const BenchAlgo bench_algo) {
break;

case BenchAlgo::GHOSTRIDER_RTM:
case BenchAlgo::FLEX_KCN:
job.setBlob("000000208c246d0b90c3b389c4086e8b672ee040d64db5b9648527133e217fbfa48da64c0f3c0a0b0e8350800568b40fbb323ac3ccdf2965de51b9aaeb939b4f11ff81c49b74a16156ff251c00000000");
job.setDiff(1000);
break;
Expand Down Expand Up @@ -229,6 +231,15 @@ void MoBenchmark::onJobResult(const JobResult& result) {
m_bench_algo_perf[m_bench_algo] = hashrate; // store hashrate result
LOG_INFO("%s " BRIGHT_BLACK_BG(WHITE_BOLD_S " Algo " MAGENTA_BOLD_S "%s" WHITE_BOLD_S " hashrate: " CYAN_BOLD_S "%f "), Tags::benchmark(), Algorithm(ba2a[m_bench_algo]).name(), hashrate);
run_next_bench_algo(m_bench_algo);
} else switch(m_bench_algo) { // Update GhostRider algo job to produce more accurate perf results
case BenchAlgo::GHOSTRIDER_RTM: {
Job& job = *m_bench_job[m_bench_algo];
uint8_t* blob = job.blob();
++ *reinterpret_cast<uint32_t*>(blob+4);
m_controller->miner()->setJob(job, false);
break;
}
default:;
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/core/MoBenchmark.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class Job;
class MoBenchmark : public IJobResultListener {

enum BenchAlgo : int {
FLEX_KCN, // "flex" Flex
GHOSTRIDER_RTM, // "ghostrider" GhostRider
CN_R, // "cn/r" CryptoNightR (Monero's variant 4).
CN_LITE_1, // "cn-lite/1" CryptoNight-Lite variant 1.
Expand All @@ -53,6 +54,7 @@ class MoBenchmark : public IJobResultListener {
};

const Algorithm::Id ba2a[BenchAlgo::MAX] = {
Algorithm::FLEX_KCN,
Algorithm::GHOSTRIDER_RTM,
Algorithm::CN_R,
Algorithm::CN_LITE_1,
Expand Down
2 changes: 1 addition & 1 deletion src/core/config/ConfigTransform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ static const char *kEnabled = "enabled";
static const char *kIntensity = "intensity";
static const char *kThreads = "threads";
#ifdef XMRIG_ALGO_KAWPOW
static const char *kKawPow = "kawpow";
//static const char *kKawPow = "kawpow";
#endif


Expand Down
5 changes: 5 additions & 0 deletions src/crypto/cn/CryptoNight_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,11 @@ const static uint8_t test_output_gr[256] = {
0xC4, 0xA7, 0xC7, 0x77, 0xAD, 0xF8, 0x09, 0x61, 0x16, 0xBB, 0xAA, 0x7E, 0xAB, 0xC3, 0x00, 0x25,
0xBA, 0xA8, 0x97, 0xC7, 0x7D, 0x38, 0x46, 0x0E, 0x59, 0xAC, 0xCB, 0xAE, 0xFE, 0x3C, 0x6F, 0x01
};
// "Flex"
const static uint8_t test_output_flex[32] = {
0x2e, 0x4f, 0x85, 0x7a, 0xa8, 0x10, 0x08, 0xc4, 0xd1, 0xfe, 0x9a, 0xcd, 0x74, 0x89, 0xe8, 0x4d,
0x3b, 0xc5, 0x5b, 0x70, 0x54, 0xe6, 0xc0, 0x2b, 0x2c, 0x0e, 0x1b, 0x76, 0xcc, 0xa0, 0xda, 0x7b
};
#endif


Expand Down
4 changes: 1 addition & 3 deletions src/crypto/cn/CryptoNight_x86.h
Original file line number Diff line number Diff line change
Expand Up @@ -1238,9 +1238,7 @@ static NOINLINE void cryptonight_single_hash_gr_sse41(const uint8_t* __restrict_

keccak(input, size, ctx[0]->state);

if (props.half_mem()) {
ctx[0]->first_half = true;
}
if (props.half_mem()) ctx[0]->first_half = true;
cn_explode_scratchpad<ALGO, false, 0>(ctx[0]);

VARIANT1_INIT(0);
Expand Down
Loading

0 comments on commit bf3831c

Please sign in to comment.