From 5fdf5516ff36c0543442a062a328ef4e18e4ba7b Mon Sep 17 00:00:00 2001
From: Chris <chriskobasiuk@gmail.com>
Date: Fri, 27 Aug 2021 08:19:54 -0600
Subject: [PATCH 1/2] Added Graft RandonX

---
 scripts/generate_cl.js                        |  1 +
 src/backend/opencl/cl/cn/algorithm.cl         |  1 +
 src/backend/opencl/cl/rx/randomx.cl           |  2 +
 .../opencl/cl/rx/randomx_constants_graft.h    | 96 +++++++++++++++++++
 src/base/crypto/Algorithm.cpp                 |  6 +-
 src/base/crypto/Algorithm.h                   |  2 +
 src/base/crypto/Coin.cpp                      |  4 +
 src/base/crypto/Coin.h                        |  1 +
 src/crypto/randomx/randomx.cpp                | 10 ++
 src/crypto/randomx/randomx.h                  |  2 +
 src/crypto/rx/RxAlgo.cpp                      |  3 +
 11 files changed, 127 insertions(+), 1 deletion(-)
 create mode 100644 src/backend/opencl/cl/rx/randomx_constants_graft.h

diff --git a/scripts/generate_cl.js b/scripts/generate_cl.js
index 7e19290b5..c3fffbf98 100644
--- a/scripts/generate_cl.js
+++ b/scripts/generate_cl.js
@@ -51,6 +51,7 @@ function rx()
         'randomx_constants_wow.h',
         'randomx_constants_arqma.h',
         'randomx_constants_keva.h',
+        'randomx_constants_graft.h',
         'aes.cl',
         'blake2b.cl',
         'randomx_vm.cl',
diff --git a/src/backend/opencl/cl/cn/algorithm.cl b/src/backend/opencl/cl/cn/algorithm.cl
index 6253f40cf..dcf43d445 100644
--- a/src/backend/opencl/cl/cn/algorithm.cl
+++ b/src/backend/opencl/cl/cn/algorithm.cl
@@ -23,6 +23,7 @@
 #define ALGO_RX_ARQMA       0x72121061
 #define ALGO_RX_SFX         0x72151273
 #define ALGO_RX_KEVA        0x7214116b
+#define ALGO_RX_GRAFT       0x7257ef81
 #define ALGO_AR2_CHUKWA     0x61130000
 #define ALGO_AR2_CHUKWA_V2  0x61140000
 #define ALGO_AR2_WRKZ       0x61120000
diff --git a/src/backend/opencl/cl/rx/randomx.cl b/src/backend/opencl/cl/rx/randomx.cl
index 18c3b1812..a08b8ba27 100644
--- a/src/backend/opencl/cl/rx/randomx.cl
+++ b/src/backend/opencl/cl/rx/randomx.cl
@@ -8,6 +8,8 @@
 #include "randomx_constants_arqma.h"
 #elif (ALGO == ALGO_RX_KEVA)
 #include "randomx_constants_keva.h"
+#elif (ALGO == ALGO_RX_GRAFT)
+#include "randomx_constants_graft.h"
 #endif
 
 #include "aes.cl"
diff --git a/src/backend/opencl/cl/rx/randomx_constants_graft.h b/src/backend/opencl/cl/rx/randomx_constants_graft.h
new file mode 100644
index 000000000..ab0647b17
--- /dev/null
+++ b/src/backend/opencl/cl/rx/randomx_constants_graft.h
@@ -0,0 +1,96 @@
+/*
+Copyright (c) 2019 SChernykh
+
+This file is part of RandomX OpenCL.
+
+RandomX OpenCL 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 OpenCL 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 OpenCL. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+//Dataset base size in bytes. Must be a power of 2.
+#define RANDOMX_DATASET_BASE_SIZE  2147483648
+
+//Dataset extra size. Must be divisible by 64.
+#define RANDOMX_DATASET_EXTRA_SIZE 33554368
+
+//Scratchpad L3 size in bytes. Must be a power of 2.
+#define RANDOMX_SCRATCHPAD_L3      2097152
+
+//Scratchpad L2 size in bytes. Must be a power of two and less than or equal to RANDOMX_SCRATCHPAD_L3.
+#define RANDOMX_SCRATCHPAD_L2      262144
+
+//Scratchpad L1 size in bytes. Must be a power of two (minimum 64) and less than or equal to RANDOMX_SCRATCHPAD_L2.
+#define RANDOMX_SCRATCHPAD_L1      16384
+
+//Jump condition mask size in bits.
+#define RANDOMX_JUMP_BITS          8
+
+//Jump condition mask offset in bits. The sum of RANDOMX_JUMP_BITS and RANDOMX_JUMP_OFFSET must not exceed 16.
+#define RANDOMX_JUMP_OFFSET        8
+
+//Integer instructions
+#define RANDOMX_FREQ_IADD_RS       16
+#define RANDOMX_FREQ_IADD_M         7
+#define RANDOMX_FREQ_ISUB_R        16
+#define RANDOMX_FREQ_ISUB_M         7
+#define RANDOMX_FREQ_IMUL_R        16
+#define RANDOMX_FREQ_IMUL_M         4
+#define RANDOMX_FREQ_IMULH_R        4
+#define RANDOMX_FREQ_IMULH_M        1
+#define RANDOMX_FREQ_ISMULH_R       4
+#define RANDOMX_FREQ_ISMULH_M       1
+#define RANDOMX_FREQ_IMUL_RCP       8
+#define RANDOMX_FREQ_INEG_R         2
+#define RANDOMX_FREQ_IXOR_R        15
+#define RANDOMX_FREQ_IXOR_M         5
+#define RANDOMX_FREQ_IROR_R         7
+#define RANDOMX_FREQ_IROL_R         3
+#define RANDOMX_FREQ_ISWAP_R        4
+
+//Floating point instructions
+#define RANDOMX_FREQ_FSWAP_R        4
+#define RANDOMX_FREQ_FADD_R        16
+#define RANDOMX_FREQ_FADD_M         5
+#define RANDOMX_FREQ_FSUB_R        16
+#define RANDOMX_FREQ_FSUB_M         5
+#define RANDOMX_FREQ_FSCAL_R        6
+#define RANDOMX_FREQ_FMUL_R        32
+#define RANDOMX_FREQ_FDIV_M         4
+#define RANDOMX_FREQ_FSQRT_R        6
+
+//Control instructions
+#define RANDOMX_FREQ_CBRANCH       25
+#define RANDOMX_FREQ_CFROUND        1
+
+//Store instruction
+#define RANDOMX_FREQ_ISTORE        16
+
+//No-op instruction
+#define RANDOMX_FREQ_NOP            0
+
+#define RANDOMX_DATASET_ITEM_SIZE 64
+
+#define RANDOMX_PROGRAM_SIZE 280
+
+#define HASH_SIZE 64
+#define ENTROPY_SIZE (128 + RANDOMX_PROGRAM_SIZE * 8)
+#define REGISTERS_SIZE 256
+#define IMM_BUF_SIZE (RANDOMX_PROGRAM_SIZE * 4 - REGISTERS_SIZE)
+#define IMM_INDEX_COUNT ((IMM_BUF_SIZE / 4) - 2)
+#define VM_STATE_SIZE (REGISTERS_SIZE + IMM_BUF_SIZE + RANDOMX_PROGRAM_SIZE * 4)
+#define ROUNDING_MODE (RANDOMX_FREQ_CFROUND ? -1 : 0)
+
+// Scratchpad L1/L2/L3 bits
+#define LOC_L1 (32 - 14)
+#define LOC_L2 (32 - 18)
+#define LOC_L3 (32 - 21)
diff --git a/src/base/crypto/Algorithm.cpp b/src/base/crypto/Algorithm.cpp
index 18adcf4e0..dd2df673d 100644
--- a/src/base/crypto/Algorithm.cpp
+++ b/src/base/crypto/Algorithm.cpp
@@ -79,6 +79,7 @@ const char *Algorithm::kRX              = "rx";
 const char *Algorithm::kRX_0            = "rx/0";
 const char *Algorithm::kRX_WOW          = "rx/wow";
 const char *Algorithm::kRX_ARQ          = "rx/arq";
+const char *Algorithm::kRX_GRAFT        = "rx/graft";
 const char *Algorithm::kRX_SFX          = "rx/sfx";
 const char *Algorithm::kRX_KEVA         = "rx/keva";
 #endif
@@ -149,6 +150,7 @@ static const std::map<uint32_t, const char *> kAlgorithmNames = {
     ALGO_NAME(RX_0),
     ALGO_NAME(RX_WOW),
     ALGO_NAME(RX_ARQ),
+    ALGO_NAME(RX_GRAFT),
     ALGO_NAME(RX_SFX),
     ALGO_NAME(RX_KEVA),
 #   endif
@@ -260,6 +262,8 @@ static const std::map<const char *, Algorithm::Id, aliasCompare> kAlgorithmAlias
                                     ALGO_ALIAS(RX_WOW,          "randomwow"),
     ALGO_ALIAS_AUTO(RX_ARQ),        ALGO_ALIAS(RX_ARQ,          "randomx/arq"),
                                     ALGO_ALIAS(RX_ARQ,          "randomarq"),
+    ALGO_ALIAS_AUTO(RX_GRAFT),      ALGO_ALIAS(RX_GRAFT,        "randomx/graft"),
+                                    ALGO_ALIAS(RX_GRAFT,        "randomgraft"),
     ALGO_ALIAS_AUTO(RX_SFX),        ALGO_ALIAS(RX_SFX,          "randomx/sfx"),
                                     ALGO_ALIAS(RX_SFX,          "randomsfx"),
     ALGO_ALIAS_AUTO(RX_KEVA),       ALGO_ALIAS(RX_KEVA,         "randomx/keva"),
@@ -350,7 +354,7 @@ std::vector<xmrig::Algorithm> xmrig::Algorithm::all(const std::function<bool(con
         CN_HEAVY_0, CN_HEAVY_TUBE, CN_HEAVY_XHV,
         CN_PICO_0, CN_PICO_TLO,
         CN_UPX2,
-        RX_0, RX_WOW, RX_ARQ, RX_SFX, RX_KEVA,
+        RX_0, RX_WOW, RX_ARQ, RX_GRAFT, RX_SFX, RX_KEVA,
         AR2_CHUKWA, AR2_CHUKWA_V2, AR2_WRKZ,
         ASTROBWT_DERO,
         KAWPOW_RVN
diff --git a/src/base/crypto/Algorithm.h b/src/base/crypto/Algorithm.h
index 4fc6de7a4..062e3b469 100644
--- a/src/base/crypto/Algorithm.h
+++ b/src/base/crypto/Algorithm.h
@@ -68,6 +68,7 @@ public:
         RX_0            = 0x72151200,   // "rx/0"             RandomX (reference configuration).
         RX_WOW          = 0x72141177,   // "rx/wow"           RandomWOW (Wownero).
         RX_ARQ          = 0x72121061,   // "rx/arq"           RandomARQ (Arqma).
+        RX_GRAFT        = 0x7257ef81,   // "rx/graft"         RandomGRAFT (Graft).
         RX_SFX          = 0x72151273,   // "rx/sfx"           RandomSFX (Safex Cash).
         RX_KEVA         = 0x7214116b,   // "rx/keva"          RandomKEVA (Keva).
         AR2_CHUKWA      = 0x61130000,   // "argon2/chukwa"    Argon2id (Chukwa).
@@ -134,6 +135,7 @@ public:
     static const char *kRX_0;
     static const char *kRX_WOW;
     static const char *kRX_ARQ;
+    static const char *kRX_GRAFT;
     static const char *kRX_SFX;
     static const char *kRX_KEVA;
 #   endif
diff --git a/src/base/crypto/Coin.cpp b/src/base/crypto/Coin.cpp
index a70ca0751..82d773f15 100644
--- a/src/base/crypto/Coin.cpp
+++ b/src/base/crypto/Coin.cpp
@@ -52,6 +52,7 @@ static CoinName const coin_names[] = {
     { "arqma",      Coin::ARQMA   },
     { "arq",        Coin::ARQMA   },
     { "dero",       Coin::DERO    },
+    { "graft",      Coin::GRAFT   },
     { "keva",       Coin::KEVA    },
     { "ravencoin",  Coin::RAVEN   },
     { "raven",      Coin::RAVEN   },
@@ -80,6 +81,9 @@ xmrig::Algorithm::Id xmrig::Coin::algorithm(uint8_t blobVersion) const
     case KEVA:
         return (blobVersion >= 11) ? Algorithm::RX_KEVA : Algorithm::CN_R;
 
+    case GRAFT:
+        return Algorithm::RX_GRAFT;
+
     case RAVEN:
         return Algorithm::KAWPOW_RVN;
 
diff --git a/src/base/crypto/Coin.h b/src/base/crypto/Coin.h
index 985335cb4..10e4de326 100644
--- a/src/base/crypto/Coin.h
+++ b/src/base/crypto/Coin.h
@@ -42,6 +42,7 @@ public:
         MONERO,
         ARQMA,
         DERO,
+        GRAFT,
         KEVA,
         RAVEN,
         CONCEAL,
diff --git a/src/crypto/randomx/randomx.cpp b/src/crypto/randomx/randomx.cpp
index fe438b61e..1e0f1e63e 100644
--- a/src/crypto/randomx/randomx.cpp
+++ b/src/crypto/randomx/randomx.cpp
@@ -86,6 +86,15 @@ RandomX_ConfigurationArqma::RandomX_ConfigurationArqma()
 	ScratchpadL3_Size = 262144;
 }
 
+RandomX_ConfigurationGraft::RandomX_ConfigurationGraft()
+{
+    ArgonLanes = 2;
+    ArgonSalt = "RandomX-Graft\x01";
+    ProgramSize = 280;
+	RANDOMX_FREQ_IROR_R = 7;
+	RANDOMX_FREQ_IROL_R = 3;
+}
+
 RandomX_ConfigurationSafex::RandomX_ConfigurationSafex()
 {
 	ArgonSalt = "RandomSFX\x01";
@@ -346,6 +355,7 @@ typedef void(randomx::JitCompilerX86::* InstructionGeneratorX86_2)(const randomx
 RandomX_ConfigurationMonero RandomX_MoneroConfig;
 RandomX_ConfigurationWownero RandomX_WowneroConfig;
 RandomX_ConfigurationArqma RandomX_ArqmaConfig;
+RandomX_ConfigurationGraft RandomX_GraftConfig;
 RandomX_ConfigurationSafex RandomX_SafexConfig;
 RandomX_ConfigurationKeva RandomX_KevaConfig;
 
diff --git a/src/crypto/randomx/randomx.h b/src/crypto/randomx/randomx.h
index 937a0844c..92e252646 100644
--- a/src/crypto/randomx/randomx.h
+++ b/src/crypto/randomx/randomx.h
@@ -144,12 +144,14 @@ struct RandomX_ConfigurationBase
 struct RandomX_ConfigurationMonero : public RandomX_ConfigurationBase {};
 struct RandomX_ConfigurationWownero : public RandomX_ConfigurationBase { RandomX_ConfigurationWownero(); };
 struct RandomX_ConfigurationArqma : public RandomX_ConfigurationBase { RandomX_ConfigurationArqma(); };
+struct RandomX_ConfigurationGraft : public RandomX_ConfigurationBase { RandomX_ConfigurationGraft(); };
 struct RandomX_ConfigurationSafex : public RandomX_ConfigurationBase { RandomX_ConfigurationSafex(); };
 struct RandomX_ConfigurationKeva : public RandomX_ConfigurationBase { RandomX_ConfigurationKeva(); };
 
 extern RandomX_ConfigurationMonero RandomX_MoneroConfig;
 extern RandomX_ConfigurationWownero RandomX_WowneroConfig;
 extern RandomX_ConfigurationArqma RandomX_ArqmaConfig;
+extern RandomX_ConfigurationGraft RandomX_GraftConfig;
 extern RandomX_ConfigurationSafex RandomX_SafexConfig;
 extern RandomX_ConfigurationKeva RandomX_KevaConfig;
 
diff --git a/src/crypto/rx/RxAlgo.cpp b/src/crypto/rx/RxAlgo.cpp
index b7d2b0834..63ea2256e 100644
--- a/src/crypto/rx/RxAlgo.cpp
+++ b/src/crypto/rx/RxAlgo.cpp
@@ -39,6 +39,9 @@ const RandomX_ConfigurationBase *xmrig::RxAlgo::base(Algorithm::Id algorithm)
     case Algorithm::RX_ARQ:
         return &RandomX_ArqmaConfig;
 
+    case Algorithm::RX_GRAFT:
+        return &RandomX_GraftConfig;
+
     case Algorithm::RX_SFX:
         return &RandomX_SafexConfig;
 

From 7f2771b4660a09678ae7a2d350ee4f7916d7e2ca Mon Sep 17 00:00:00 2001
From: Chris <chriskobasiuk@gmail.com>
Date: Fri, 27 Aug 2021 10:31:36 -0600
Subject: [PATCH 2/2] Fixed Algorithm id

Algorithm id should be 0x72151267, second and third byte encode L3 and L2 size.
0x72 = 'r'
0x15 = 1 << 0x15 (L3 size)
0x12 = 1 << 0x12 (L2 size)
0x67 = 'g'
---
 src/backend/opencl/cl/cn/algorithm.cl | 2 +-
 src/base/crypto/Algorithm.h           | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/backend/opencl/cl/cn/algorithm.cl b/src/backend/opencl/cl/cn/algorithm.cl
index dcf43d445..7258479d1 100644
--- a/src/backend/opencl/cl/cn/algorithm.cl
+++ b/src/backend/opencl/cl/cn/algorithm.cl
@@ -23,7 +23,7 @@
 #define ALGO_RX_ARQMA       0x72121061
 #define ALGO_RX_SFX         0x72151273
 #define ALGO_RX_KEVA        0x7214116b
-#define ALGO_RX_GRAFT       0x7257ef81
+#define ALGO_RX_GRAFT       0x72151267
 #define ALGO_AR2_CHUKWA     0x61130000
 #define ALGO_AR2_CHUKWA_V2  0x61140000
 #define ALGO_AR2_WRKZ       0x61120000
diff --git a/src/base/crypto/Algorithm.h b/src/base/crypto/Algorithm.h
index 062e3b469..15df2a2c0 100644
--- a/src/base/crypto/Algorithm.h
+++ b/src/base/crypto/Algorithm.h
@@ -68,7 +68,7 @@ public:
         RX_0            = 0x72151200,   // "rx/0"             RandomX (reference configuration).
         RX_WOW          = 0x72141177,   // "rx/wow"           RandomWOW (Wownero).
         RX_ARQ          = 0x72121061,   // "rx/arq"           RandomARQ (Arqma).
-        RX_GRAFT        = 0x7257ef81,   // "rx/graft"         RandomGRAFT (Graft).
+        RX_GRAFT        = 0x72151267,   // "rx/graft"         RandomGRAFT (Graft).
         RX_SFX          = 0x72151273,   // "rx/sfx"           RandomSFX (Safex Cash).
         RX_KEVA         = 0x7214116b,   // "rx/keva"          RandomKEVA (Keva).
         AR2_CHUKWA      = 0x61130000,   // "argon2/chukwa"    Argon2id (Chukwa).