Scrypt hashing implementation update
authoralex <alex@alex-VirtualBox.(none)>
Fri, 23 Aug 2013 16:23:30 +0000 (20:23 +0400)
committeralex <alex@alex-VirtualBox.(none)>
Fri, 23 Aug 2013 16:23:30 +0000 (20:23 +0400)
* block_header structure removed;
* srypt_hash splitted in scrypt_hash(data, datalen) and scrypt_blockhash(data).

src/main.cpp
src/main.h
src/scrypt_mine.cpp
src/scrypt_mine.h

index 20f499d..894bad6 100644 (file)
@@ -72,9 +72,6 @@ CScript COINBASE_FLAGS;
 
 const string strMessageMagic = "NovaCoin Signed Message:\n";
 
-double dHashesPerSec;
-int64 nHPSTimerStart;
-
 // Settings
 int64 nTransactionFee = MIN_TX_FEE;
 bool fStakeUsePooledKeys = false;
index 4b36ac4..3711793 100644 (file)
@@ -80,8 +80,6 @@ extern uint64 nLastBlockTx;
 extern uint64 nLastBlockSize;
 extern int64 nLastCoinStakeSearchInterval;
 extern const std::string strMessageMagic;
-extern double dHashesPerSec;
-extern int64 nHPSTimerStart;
 extern int64 nTimeBestReceived;
 extern CCriticalSection cs_setpwalletRegistered;
 extern std::set<CWallet*> setpwalletRegistered;
@@ -912,14 +910,7 @@ public:
 
     uint256 GetHash() const
     {
-        uint256 thash;
-        void * scratchbuff = scrypt_buffer_alloc();
-
-        scrypt_hash(CVOIDBEGIN(nVersion), sizeof(block_header), UINTBEGIN(thash), scratchbuff);
-
-        scrypt_buffer_free(scratchbuff);
-
-        return thash;
+        return scrypt_blockhash(CVOIDBEGIN(nVersion));
     }
 
     int64 GetBlockTime() const
index c424e49..cd556fa 100644 (file)
@@ -29,7 +29,6 @@
 
 #include <stdlib.h>
 #include <stdint.h>
-#include <xmmintrin.h>
 
 #include "scrypt_mine.h"
 #include "pbkdf2.h"
 #include "util.h"
 #include "net.h"
 
-#if defined (__x86_64__) || defined (__i386__)
 #define SCRYPT_BUFFER_SIZE (131072 + 63)
+
+#if defined (__x86_64__) || defined (__i386__)
 extern "C" void scrypt_core(uint32_t *X, uint32_t *V);
+#else
+// TODO: Add cross-platform scrypt_core implementation
 #endif
 
-void *scrypt_buffer_alloc() {
-    return malloc(SCRYPT_BUFFER_SIZE);
-}
-
-void scrypt_buffer_free(void *scratchpad)
-{
-    free(scratchpad);
-}
-
 /* cpu and memory intensive function to transform a 80 byte buffer into a 32 byte output
    scratchpad size needs to be at least 63 + (128 * r * p) + (256 * r + 64) + (128 * r * N) bytes
    r = 1, p = 1, N = 1024
  */
 
-static void scrypt(const void* input, size_t inputlen, uint32_t *res, void *scratchpad)
+static uint256 scrypt(const void* input, size_t inputlen, void *scratchpad)
 {
     uint32_t *V;
     uint32_t X[32];
+    uint256 result;
     V = (uint32_t *)(((uintptr_t)(scratchpad) + 63) & ~ (uintptr_t)(63));
 
-    PBKDF2_SHA256((const uint8_t*)input, inputlen, (const uint8_t*)input, sizeof(block_header), 1, (uint8_t *)X, 128);
-
+    PBKDF2_SHA256((const uint8_t*)input, inputlen, (const uint8_t*)input, inputlen, 1, (uint8_t *)X, 128);
     scrypt_core(X, V);
+    PBKDF2_SHA256((const uint8_t*)input, inputlen, (uint8_t *)X, 128, 1, (uint8_t*)&result, 32);
 
-    PBKDF2_SHA256((const uint8_t*)input, inputlen, (uint8_t *)X, 128, 1, (uint8_t*)res, 32);
+    return result;
+}
+
+uint256 scrypt_hash(const void* input, size_t inputlen)
+{
+    unsigned char scratchpad[SCRYPT_BUFFER_SIZE];
+    return scrypt(input, inputlen, scratchpad);
 }
 
-void scrypt_hash(const void* input, size_t inputlen, uint32_t *res, void *scratchpad)
+uint256 scrypt_blockhash(const void* input)
 {
-    return scrypt(input, inputlen, res, scratchpad);
+    unsigned char scratchpad[SCRYPT_BUFFER_SIZE];
+    return scrypt(input, 80, scratchpad);
 }
index 0260cde..2d2112d 100644 (file)
@@ -7,18 +7,7 @@
 #include "util.h"
 #include "net.h"
 
-typedef struct
-{
-    unsigned int version;
-    uint256 prev_block;
-    uint256 merkle_root;
-    unsigned int timestamp;
-    unsigned int bits;
-    unsigned int nonce;
-} block_header;
-
-void *scrypt_buffer_alloc();
-void scrypt_buffer_free(void *scratchpad);
-void scrypt_hash(const void* input, size_t inputlen, uint32_t *res, void *scratchpad);
+uint256 scrypt_hash(const void* input, size_t inputlen);
+uint256 scrypt_blockhash(const void* input);
 
 #endif // SCRYPT_MINE_H