From 06b2c4043c7d04509acf9443ac874c101e9cb00a Mon Sep 17 00:00:00 2001
From: Dmitriy Gerasimov <dm@cifercom.com>
Date: Fri, 12 Jan 2018 06:25:56 +0700
Subject: [PATCH] [+] Hash funcsion for Keccak, for mining and declarations for
 HashFusion

---
 core/dap_math_ops.h            |  2 +-
 crypt/CMakeLists.txt           | 28 ++++++++++++++++++++++++++--
 crypt/dap_hash.c               |  4 ++++
 crypt/dap_hash.h               | 30 ++++++++++++++++++++++++++++++
 crypt/dap_hash_fusion.c        |  4 ++++
 crypt/dap_hash_fusion.h        |  5 +++++
 crypt/dap_hash_keccak.h        | 10 ++++++++++
 crypt/dap_hash_slow.h          | 21 +++++++++++++++++++++
 crypt/monero_crypto/hash-ops.h |  7 ++++---
 9 files changed, 105 insertions(+), 6 deletions(-)
 create mode 100644 crypt/dap_hash.c
 create mode 100644 crypt/dap_hash.h
 create mode 100644 crypt/dap_hash_fusion.c
 create mode 100644 crypt/dap_hash_fusion.h
 create mode 100644 crypt/dap_hash_keccak.h
 create mode 100644 crypt/dap_hash_slow.h

diff --git a/core/dap_math_ops.h b/core/dap_math_ops.h
index 0a8b8690c1..ee801913ee 100644
--- a/core/dap_math_ops.h
+++ b/core/dap_math_ops.h
@@ -2,7 +2,7 @@
 #define _DAP_MATH_OPS_H_
 
 #include <stdint.h>
-
+#include "monero_crypto/common/int-util.h"
 #if defined(__GNUC__) ||defined (__clang__)
 
 #if __SIZEOF_INT128__ == 16
diff --git a/crypt/CMakeLists.txt b/crypt/CMakeLists.txt
index 0a45aaaddd..0bbc5088dc 100644
--- a/crypt/CMakeLists.txt
+++ b/crypt/CMakeLists.txt
@@ -1,7 +1,31 @@
 cmake_minimum_required(VERSION 2.8)
 project (dap_crypto)
   
-set(CRYPT_SRCS dap_enc.c dap_enc_aes.c dap_enc_newhope.c dap_enc_key.c  )
+set(CRYPTO_SRCS
+    dap_enc.c
+
+    dap_enc_aes.c
+    dap_enc_newhope.c
+
+    dap_enc_key.c
+
+    dap_hash.c
+    dap_hash_fusion.c
+    )
+
+set(CRYPTO_HEADERS
+    dap_enc.h
+
+    dap_enc_aes.h
+    dap_enc_newhope.h
+
+    dap_enc_key.h
+
+    dap_hash.h
+    dap_hash_fusion.h
+    dap_hash_slow.h
+    dap_hash_keccak.h
+    )
 
 add_subdirectory(monero_crypto)
 
@@ -9,7 +33,7 @@ include_directories("${dap_core_INCLUDE_DIRS}")
 include_directories("${monero_crypto_INCLUDE_DIRS}")
 add_definitions ("${dap_core_DEFINITIONS}")
 add_definitions ("${monero_crypto_DEFINITIONS}")
-add_library(${PROJECT_NAME} STATIC ${CRYPT_SRCS} ${crypto_sources} )
+add_library(${PROJECT_NAME} STATIC ${CRYPTO_SRCS} ${CRYPTO_HEADERS} )
 
 set(${PROJECT_NAME}_DEFINITIONS CACHE INTERNAL "${PROJECT_NAME}: Definitions" FORCE)
 set(${PROJECT_NAME}_INCLUDE_DIRS ${PROJECT_SOURCE_DIR} CACHE INTERNAL "${PROJECT_NAME}: Include Directories" FORCE)
diff --git a/crypt/dap_hash.c b/crypt/dap_hash.c
new file mode 100644
index 0000000000..c8d3a7fe20
--- /dev/null
+++ b/crypt/dap_hash.c
@@ -0,0 +1,4 @@
+#include "dap_common.h"
+#include "dap_hash.h"
+
+#define LOG_TAG "dap_hash"
diff --git a/crypt/dap_hash.h b/crypt/dap_hash.h
new file mode 100644
index 0000000000..a3b57560b9
--- /dev/null
+++ b/crypt/dap_hash.h
@@ -0,0 +1,30 @@
+#ifndef _DAP_HASH_H_
+#define _DAP_HASH_H_
+#include <stddef.h>
+
+#include "dap_hash_slow.h"
+#include "dap_hash_keccak.h"
+
+
+
+typedef enum dap_hash_type {
+    DAP_HASH_TYPE_KECCAK = 0,
+    DAP_HASH_TYPE_SLOW_0 = 1,
+} dap_hash_type_t;
+
+inline void dap_hash(void * a_data_in, size_t a_data_in_size,
+                     void * a_data_out, size_t a_data_out_size,
+                     dap_hash_type_t a_type ){
+    switch (a_type){
+        case DAP_HASH_TYPE_KECCAK:
+            dap_hash_keccak(a_data_in,a_data_in_size, a_data_out,a_data_out_size);
+        break;
+        case DAP_HASH_TYPE_SLOW_0:
+            if( a_data_out_size>= dap_hash_slow_size() ){
+                dap_hash_slow(a_data_in,a_data_in_size,(char*) a_data_out);
+            }
+        break;
+    }
+
+}
+#endif
diff --git a/crypt/dap_hash_fusion.c b/crypt/dap_hash_fusion.c
new file mode 100644
index 0000000000..e1a46eeb2b
--- /dev/null
+++ b/crypt/dap_hash_fusion.c
@@ -0,0 +1,4 @@
+#include "dap_common.h"
+#include "dap_hash_fusion.h"
+
+#define LOG_TAG "dap_hash_fusion"
diff --git a/crypt/dap_hash_fusion.h b/crypt/dap_hash_fusion.h
new file mode 100644
index 0000000000..12346aaae7
--- /dev/null
+++ b/crypt/dap_hash_fusion.h
@@ -0,0 +1,5 @@
+#ifndef _DAP_HASH_FUSION_H_
+#define _DAP_HASH_FUSION_H_
+
+
+#endif
diff --git a/crypt/dap_hash_keccak.h b/crypt/dap_hash_keccak.h
new file mode 100644
index 0000000000..6381fe4923
--- /dev/null
+++ b/crypt/dap_hash_keccak.h
@@ -0,0 +1,10 @@
+#pragma once
+#include "keccak.h"
+
+
+inline void dap_hash_keccak(const void * a_in, size_t a_in_size, void * a_out, size_t a_out_size)
+{
+    keccak((const uint8_t*) a_in,a_in_size, (uint8_t *) a_out,  a_out_size );
+}
+
+
diff --git a/crypt/dap_hash_slow.h b/crypt/dap_hash_slow.h
new file mode 100644
index 0000000000..61a90525ed
--- /dev/null
+++ b/crypt/dap_hash_slow.h
@@ -0,0 +1,21 @@
+#ifndef _DAP_HASH_SLOW_H_
+#define _DAP_HASH_SLOW_H_
+
+#include "hash-ops.h"
+
+#define DAP_HASH_SLOW_SIZE HASH_SIZE
+
+/**
+ * @brief dap_hash_slow
+ * @param a_in
+ * @param a_in_length
+ * @param a_out Must be allocated with enought space
+ */
+inline void dap_hash_slow(const void *a_in, size_t a_in_length, char * a_out)
+{
+    cn_slow_hash(a_in,a_in_length,a_out);
+}
+
+inline size_t dap_hash_slow_size() { return DAP_HASH_SLOW_SIZE; }
+//cn_slow_hash(data, length, reinterpret_cast<char *>(&hash));
+#endif
diff --git a/crypt/monero_crypto/hash-ops.h b/crypt/monero_crypto/hash-ops.h
index 6e3a5c6c9b..75593d748f 100644
--- a/crypt/monero_crypto/hash-ops.h
+++ b/crypt/monero_crypto/hash-ops.h
@@ -1,3 +1,4 @@
+// Copyright (c) 2017-2018, The Demlabs Inc. Project
 // Copyright (c) 2014-2017, The Monero Project
 // 
 // All rights reserved.
@@ -27,10 +28,10 @@
 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 // 
 // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
-
 #pragma once
 
-#if !defined(__cplusplus)
+
+//#if !defined(__cplusplus)
 
 #include <assert.h>
 #include <stdbool.h>
@@ -71,7 +72,7 @@ static_assert(sizeof(union hash_state) == 200, "Invalid structure size");
 void hash_permutation(union hash_state *state);
 void hash_process(union hash_state *state, const uint8_t *buf, size_t count);
 
-#endif
+//#endif
 
 enum {
   HASH_SIZE = 32,
-- 
GitLab