From 319f8e98bd523065946530c3cbdaa1c993642c48 Mon Sep 17 00:00:00 2001 From: alex Date: Fri, 23 Aug 2013 20:23:30 +0400 Subject: [PATCH] Scrypt hashing implementation update * block_header structure removed; * srypt_hash splitted in scrypt_hash(data, datalen) and scrypt_blockhash(data). --- src/main.cpp | 3 --- src/main.h | 11 +---------- src/scrypt_mine.cpp | 35 ++++++++++++++++++----------------- src/scrypt_mine.h | 15 ++------------- 4 files changed, 21 insertions(+), 43 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 20f499d..894bad6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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; diff --git a/src/main.h b/src/main.h index 4b36ac4..3711793 100644 --- a/src/main.h +++ b/src/main.h @@ -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 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 diff --git a/src/scrypt_mine.cpp b/src/scrypt_mine.cpp index c424e49..cd556fa 100644 --- a/src/scrypt_mine.cpp +++ b/src/scrypt_mine.cpp @@ -29,7 +29,6 @@ #include #include -#include #include "scrypt_mine.h" #include "pbkdf2.h" @@ -37,39 +36,41 @@ #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); } diff --git a/src/scrypt_mine.h b/src/scrypt_mine.h index 0260cde..2d2112d 100644 --- a/src/scrypt_mine.h +++ b/src/scrypt_mine.h @@ -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 -- 1.7.1