From 29f2dd4b9e02338c015aa728255a8c820b006900 Mon Sep 17 00:00:00 2001
From: SChernykh <sergey.v.chernykh@gmail.com>
Date: Wed, 16 Jun 2021 11:47:17 +0200
Subject: [PATCH] Cleanup

---
 src/base/tools/cryptonote/BlockTemplate.cpp | 22 ++++++---------------
 src/base/tools/cryptonote/Signatures.cpp    | 17 +++++++++++++---
 src/base/tools/cryptonote/Signatures.h      |  2 ++
 src/base/tools/cryptonote/crypto-ops.h      |  8 --------
 4 files changed, 22 insertions(+), 27 deletions(-)

diff --git a/src/base/tools/cryptonote/BlockTemplate.cpp b/src/base/tools/cryptonote/BlockTemplate.cpp
index 207bff81b..6980eb047 100644
--- a/src/base/tools/cryptonote/BlockTemplate.cpp
+++ b/src/base/tools/cryptonote/BlockTemplate.cpp
@@ -126,13 +126,11 @@ bool CBlockTemplate::Init(const String& blockTemplate)
 void CBlockTemplate::CalculateMinerTxHash(uint8_t* hash)
 {
     uint8_t hashes[HASH_SIZE * 3];
-    uint8_t md[200];
 
     // Calculate 3 partial hashes
 
     // 1. Prefix
-    keccak(raw_blob.data() + miner_tx_prefix_begin_index, miner_tx_prefix_end_index - miner_tx_prefix_begin_index, md);
-    memcpy(hashes, md, HASH_SIZE);
+    keccak(raw_blob.data() + miner_tx_prefix_begin_index, miner_tx_prefix_end_index - miner_tx_prefix_begin_index, hashes, HASH_SIZE);
 
     // 2. Base RCT, single 0 byte in miner tx
     static const uint8_t known_second_hash[HASH_SIZE] = {
@@ -144,23 +142,18 @@ void CBlockTemplate::CalculateMinerTxHash(uint8_t* hash)
     memset(hashes + HASH_SIZE * 2, 0, HASH_SIZE);
 
     // Calculate miner transaction hash
-    keccak(hashes, sizeof(hashes), md);
-
-    memcpy(hash, md, HASH_SIZE);
+    keccak(hashes, sizeof(hashes), hash, HASH_SIZE);
 }
 
 
 
 void CBlockTemplate::CalculateMerkleTreeHash(const uint8_t* hashes, size_t count, uint8_t* root_hash)
 {
-    uint8_t md[200];
-
     if (count == 1) {
         memcpy(root_hash, hashes, HASH_SIZE);
     }
     else if (count == 2) {
-        keccak(hashes, HASH_SIZE * 2, md);
-        memcpy(root_hash, md, HASH_SIZE);
+        keccak(hashes, HASH_SIZE * 2, root_hash, HASH_SIZE);
     }
     else {
         size_t i, j;
@@ -172,20 +165,17 @@ void CBlockTemplate::CalculateMerkleTreeHash(const uint8_t* hashes, size_t count
         memcpy(ints.data(), hashes, (cnt * 2 - count) * HASH_SIZE);
 
         for (i = cnt * 2 - count, j = cnt * 2 - count; j < cnt; i += 2, ++j) {
-            keccak(hashes + i * HASH_SIZE, HASH_SIZE * 2, md);
-            memcpy(ints.data() + j * HASH_SIZE, md, HASH_SIZE);
+            keccak(hashes + i * HASH_SIZE, HASH_SIZE * 2, ints.data() + j * HASH_SIZE, HASH_SIZE);
         }
 
         while (cnt > 2) {
             cnt >>= 1;
             for (i = 0, j = 0; j < cnt; i += 2, ++j) {
-                keccak(ints.data() + i * HASH_SIZE, HASH_SIZE * 2, md);
-                memcpy(ints.data() + j * HASH_SIZE, md, HASH_SIZE);
+                keccak(ints.data() + i * HASH_SIZE, HASH_SIZE * 2, ints.data() + j * HASH_SIZE, HASH_SIZE);
             }
         }
 
-        keccak(ints.data(), HASH_SIZE * 2, md);
-        memcpy(root_hash, md, HASH_SIZE);
+        keccak(ints.data(), HASH_SIZE * 2, root_hash, HASH_SIZE);
     }
 }
 
diff --git a/src/base/tools/cryptonote/Signatures.cpp b/src/base/tools/cryptonote/Signatures.cpp
index 6dffce5d4..c058df1df 100644
--- a/src/base/tools/cryptonote/Signatures.cpp
+++ b/src/base/tools/cryptonote/Signatures.cpp
@@ -21,7 +21,13 @@
 
 #include "base/crypto/keccak.h"
 #include "base/tools/cryptonote/Signatures.h"
+
+extern "C" {
+
 #include "base/tools/cryptonote/crypto-ops.h"
+
+}
+
 #include "base/tools/Cvt.h"
 
 
@@ -73,9 +79,7 @@ static void random_scalar(ec_scalar& res)
 
 static void hash_to_scalar(const void* data, size_t length, ec_scalar& res)
 {
-    uint8_t md[200];
-    xmrig::keccak((const char*) data, length, md);
-    memcpy(&res, md, sizeof(res));
+    xmrig::keccak((const uint8_t*) data, length, (uint8_t*) &res, sizeof(res));
     sc_reduce32((uint8_t*) &res);
 }
 
@@ -193,4 +197,11 @@ void derive_secret_key(const uint8_t* derivation, size_t output_index, const uin
 }
 
 
+void derive_view_secret_key(const uint8_t* spend_secret_key, uint8_t* view_secret_key)
+{
+    keccak(spend_secret_key, 32, view_secret_key, 32);
+    sc_reduce32(view_secret_key);
+}
+
+
 } /* namespace xmrig */
diff --git a/src/base/tools/cryptonote/Signatures.h b/src/base/tools/cryptonote/Signatures.h
index 7bc9755f6..0e2bea821 100644
--- a/src/base/tools/cryptonote/Signatures.h
+++ b/src/base/tools/cryptonote/Signatures.h
@@ -34,6 +34,8 @@ bool check_signature(const uint8_t* prefix_hash, const uint8_t* pub, const uint8
 bool generate_key_derivation(const uint8_t* key1, const uint8_t* key2, uint8_t* derivation);
 void derive_secret_key(const uint8_t* derivation, size_t output_index, const uint8_t* base, uint8_t* derived_key);
 
+void derive_view_secret_key(const uint8_t* spend_secret_key, uint8_t* view_secret_key);
+
 } /* namespace xmrig */
 
 
diff --git a/src/base/tools/cryptonote/crypto-ops.h b/src/base/tools/cryptonote/crypto-ops.h
index 8d68e41c9..22f76974b 100644
--- a/src/base/tools/cryptonote/crypto-ops.h
+++ b/src/base/tools/cryptonote/crypto-ops.h
@@ -30,10 +30,6 @@
 
 #pragma once
 
-#ifdef __cplusplus
-extern "C" {
-#endif
-
 /* From fe.h */
 
 typedef int32_t fe[10];
@@ -167,7 +163,3 @@ void fe_tobytes(unsigned char *, const fe);
 void fe_invert(fe out, const fe z);
 
 int ge_p3_is_point_at_infinity(const ge_p3 *p);
-
-#ifdef __cplusplus
-}
-#endif