From: CryptoManiac Date: Thu, 9 Apr 2015 14:18:00 +0000 (+0300) Subject: Reorganize scrypt function implementations. X-Git-Tag: nvc-v0.5.3~44 X-Git-Url: https://git.novaco.in/?p=novacoin.git;a=commitdiff_plain;h=6512a48cfcd772c6ae6b17fa0b3e8c001f1657fb Reorganize scrypt function implementations. * Move assembler and intrinsic implementations to separate folders; * New cpp wrapper for assembler implementations; * Generic implementation now presented by fully autonomous scrypt-generic.cpp module. --- diff --git a/MSVC/libcommon/libcommon.vcxproj b/MSVC/libcommon/libcommon.vcxproj index 8ddeac1..4b91768 100644 --- a/MSVC/libcommon/libcommon.vcxproj +++ b/MSVC/libcommon/libcommon.vcxproj @@ -20,8 +20,6 @@ - - @@ -52,7 +50,7 @@ - + diff --git a/MSVC/libcommon/libcommon.vcxproj.filters b/MSVC/libcommon/libcommon.vcxproj.filters index b358bf8..dffbc2e 100644 --- a/MSVC/libcommon/libcommon.vcxproj.filters +++ b/MSVC/libcommon/libcommon.vcxproj.filters @@ -98,7 +98,7 @@ Source Files - + Source Files @@ -107,12 +107,6 @@ Source Files - - Source Files - - - Source Files - diff --git a/novacoin-qt.pro b/novacoin-qt.pro index d43cf89..efefa8e 100644 --- a/novacoin-qt.pro +++ b/novacoin-qt.pro @@ -135,22 +135,20 @@ contains(USE_LEVELDB, 1) { # use: qmake "USE_ASM=1" contains(USE_ASM, 1) { - message(Using assembler scrypt core implementation) - SOURCES += src/scrypt-arm.S src/scrypt-x86.S src/scrypt-x86_64.S + message(Using assembler scrypt implementation) + SOURCES += src/scrypt-asm/scrypt-arm.S src/scrypt-asm/scrypt-x86.S src/scrypt-asm/scrypt-x86_64.S src/scrypt-asm/asm-wrapper.cpp } else { # use: qmake "USE_SSE2=1" contains(USE_SSE2, 1) { message(Using SSE2 intrinsic scrypt implementation) - SOURCES += src/scrypt-sse2.cpp + SOURCES += src/scrypt-intrin/scrypt-sse2.cpp DEFINES += USE_SSE2 QMAKE_CXXFLAGS += -msse2 QMAKE_CFLAGS += -msse2 } else { - message(Using generic scrypt core implementation) + message(Using generic scrypt implementation) + SOURCES += src/scrypt-generic.cpp } - - # For now, generic module is required in both cases - SOURCES += src/scrypt-generic.c } # regenerate src/build.h @@ -343,7 +341,6 @@ SOURCES += src/qt/bitcoin.cpp src/qt/bitcoingui.cpp \ src/qt/rpcconsole.cpp \ src/noui.cpp \ src/kernel.cpp \ - src/scrypt.cpp \ src/qt/multisigaddressentry.cpp \ src/qt/multisiginputentry.cpp \ src/qt/multisigdialog.cpp diff --git a/src/makefile.bsd b/src/makefile.bsd index 5035b19..0d2babd 100644 --- a/src/makefile.bsd +++ b/src/makefile.bsd @@ -127,8 +127,7 @@ OBJS= \ obj/wallet.o \ obj/walletdb.o \ obj/noui.o \ - obj/kernel.o \ - obj/scrypt.o + obj/kernel.o all: novacoind @@ -149,28 +148,36 @@ OBJS += obj/txdb-bdb.o endif ifeq (${USE_ASM}, 1) -OBJS += obj/scrypt-arm.o obj/scrypt-x86.o obj/scrypt-x86_64.o +# Assembler implementation +OBJS += scrypt-asm/obj/scrypt-arm.o scrypt-asm/obj/scrypt-x86.o scrypt-asm/obj/scrypt-x86_64.o scrypt-asm/obj/asm-wrapper.o -obj/scrypt-x86.o: scrypt-x86.S +scrypt-asm/obj/scrypt-x86.o: scrypt-asm/scrypt-x86.S $(CXX) -c $(xCXXFLAGS) -MMD -o $@ $< -obj/scrypt-x86_64.o: scrypt-x86_64.S +scrypt-asm/obj/scrypt-x86_64.o: scrypt-asm/scrypt-x86_64.S $(CXX) -c $(xCXXFLAGS) -MMD -o $@ $< -obj/scrypt-arm.o: scrypt-arm.S +scrypt-asm/obj/scrypt-arm.o: scrypt-asm/scrypt-arm.S $(CXX) -c $(xCXXFLAGS) -MMD -o $@ $< -endif -ifneq (${USE_ASM}, 1) -OBJS += obj/scrypt-generic.o - -obj/scrypt-generic.o: scrypt-generic.c - $(CC) -c $(xCXXFLAGS) -MMD -o $@ $< +scrypt-asm/obj/asm-wrapper.o: scrypt-asm/asm-wrapper.cpp + $(CXX) -c $(xCXXFLAGS) -MMD -o $@ $< +else ifeq (${USE_SSE2}, 1) +# Intrinsic implementation DEFS += -DUSE_SSE2 -OBJS += obj/scrypt-sse2.o -obj/scrypt-sse2.o: scrypt-sse2.cpp - $(CXX) -c $(CFLAGS) -MMD -o $@ $< +OBJS += scrypt-intrin/obj/scrypt-sse2.o + +scrypt-intrin/obj/scrypt-sse2.o: scrypt-intrin/scrypt-sse2.cpp + $(CXX) -c $(xCXXFLAGS) -MMD -o $@ $< +else +ifneq (${USE_ASM}, 1) +# Generic implementation +OBJS += obj/scrypt-generic.o + +obj/scrypt-generic.o: scrypt-generic.cpp + $(CXX) -c $(xCXXFLAGS) -MMD -o $@ $< +endif endif endif @@ -197,6 +204,13 @@ clean: -rm -f novacoind -rm -f obj/*.o -rm -f obj/*.P + -rm -f obj/*.d + -rm -f scrypt-asm/obj/*.o + -rm -f scrypt-asm/obj/*.P + -rm -f scrypt-asm/obj/*.d + -rm -f scrypt-intrin/obj/*.o + -rm -f scrypt-intrin/obj/*.P + -rm -f scrypt-intrin/obj/*.d -rm -f obj/build.h FORCE: diff --git a/src/makefile.linux-mingw b/src/makefile.linux-mingw index e71d542..e11c66d 100644 --- a/src/makefile.linux-mingw +++ b/src/makefile.linux-mingw @@ -103,8 +103,7 @@ OBJS= \ obj/wallet.o \ obj/walletdb.o \ obj/noui.o \ - obj/kernel.o \ - obj/scrypt.o + obj/kernel.o all: novacoind.exe @@ -124,29 +123,39 @@ OBJS += obj/txdb-bdb.o endif ifeq (${USE_ASM}, 1) -OBJS += obj/scrypt-x86.o obj/scrypt-x86_64.o +# Assembler implementation +OBJS += scrypt-asm/obj/scrypt-arm.o scrypt-asm/obj/scrypt-x86.o scrypt-asm/obj/scrypt-x86_64.o scrypt-asm/obj/asm-wrapper.o -obj/scrypt-x86.o: scrypt-x86.S +scrypt-asm/obj/scrypt-x86.o: scrypt-asm/scrypt-x86.S $(CXX) -c $(xCXXFLAGS) -MMD -o $@ $< -obj/scrypt-x86_64.o: scrypt-x86_64.S +scrypt-asm/obj/scrypt-x86_64.o: scrypt-asm/scrypt-x86_64.S $(CXX) -c $(xCXXFLAGS) -MMD -o $@ $< -endif -ifneq (${USE_ASM}, 1) -OBJS += obj/scrypt-generic.o - -obj/scrypt-generic.o: scrypt-generic.c - $(CC) -c $(xCXXFLAGS) -MMD -o $@ $< +scrypt-asm/obj/scrypt-arm.o: scrypt-asm/scrypt-arm.S + $(CXX) -c $(xCXXFLAGS) -MMD -o $@ $< +scrypt-asm/obj/asm-wrapper.o: scrypt-asm/asm-wrapper.cpp + $(CXX) -c $(xCXXFLAGS) -MMD -o $@ $< +else ifeq (${USE_SSE2}, 1) +# Intrinsic implementation DEFS += -DUSE_SSE2 -OBJS += obj/scrypt-sse2.o +OBJS += scrypt-intrin/obj/scrypt-sse2.o -obj/scrypt-sse2.o: scrypt-sse2.cpp $(HEADERS) - $(CCX) -c $(CFLAGS) -MMD -o $@ $< +scrypt-intrin/obj/scrypt-sse2.o: scrypt-intrin/scrypt-sse2.cpp $(HEADERS) + $(CXX) -c $(xCXXFLAGS) -MMD -o $@ $< +else +ifneq (${USE_ASM}, 1) +# Generic implementation +OBJS += obj/scrypt-generic.o + +obj/scrypt-generic.o: scrypt-generic.cpp + $(CXX) -c $(xCXXFLAGS) -MMD -o $@ $< endif endif +endif + obj/build.h: FORCE @@ -162,8 +171,16 @@ novacoind.exe: $(OBJS:obj/%=obj/%) $(STRIP) novacoind.exe clean: - -rm -f obj/*.o -rm -f novacoind.exe + -rm -f obj/*.o + -rm -f obj/*.P + -rm -f obj/*.d + -rm -f scrypt-asm/obj/*.o + -rm -f scrypt-asm/obj/*.P + -rm -f scrypt-asm/obj/*.d + -rm -f scrypt-intrin/obj/*.o + -rm -f scrypt-intrin/obj/*.P + -rm -f scrypt-intrin/obj/*.d -rm -f obj/build.h cd leveldb && TARGET_OS=OS_WINDOWS_CROSSCOMPILE $(MAKE) clean && cd .. diff --git a/src/makefile.mingw b/src/makefile.mingw index a621c9d..71b7cdf 100644 --- a/src/makefile.mingw +++ b/src/makefile.mingw @@ -92,8 +92,7 @@ OBJS= \ obj/wallet.o \ obj/walletdb.o \ obj/noui.o \ - obj/kernel.o \ - obj/scrypt.o + obj/kernel.o all: novacoind.exe @@ -113,30 +112,38 @@ OBJS += obj/txdb-bdb.o endif ifdef USE_ASM -OBJS += obj/scrypt-x86.o obj/scrypt-x86_64.o +# Assembler implementation +OBJS += scrypt-asm/obj/scrypt-arm.o scrypt-asm/obj/scrypt-x86.o scrypt-asm/obj/scrypt-x86_64.o scrypt-asm/obj/asm-wrapper.o -obj/scrypt-x86.o: scrypt-x86.S +scrypt-asm/obj/scrypt-x86.o: scrypt-asm/scrypt-x86.S $(CXX) -c $(xCXXFLAGS) -MMD -o $@ $< -obj/scrypt-x86_64.o: scrypt-x86_64.S +scrypt-asm/obj/scrypt-x86_64.o: scrypt-asm/scrypt-x86_64.S $(CXX) -c $(xCXXFLAGS) -MMD -o $@ $< -else -OBJS += obj/scrypt-generic.o -obj/scrypt-generic.o: scrypt-generic.c - $(CC) -c $(xCXXFLAGS) -MMD -o $@ $< +scrypt-asm/obj/scrypt-arm.o: scrypt-asm/scrypt-arm.S + $(CXX) -c $(xCXXFLAGS) -MMD -o $@ $< +scrypt-asm/obj/asm-wrapper.o: scrypt-asm/asm-wrapper.cpp + $(CXX) -c $(xCXXFLAGS) -MMD -o $@ $< +else ifdef USE_SSE2 +# Intrinsic implementation DEFS += -DUSE_SSE2 -OBJS += obj/scrypt-sse2.o +OBJS += scrypt-intrin/obj/scrypt-sse2.o -obj/scrypt-sse2.o: scrypt-sse2.cpp $(HEADERS) - g++ -c $(CFLAGS) -MMD -o $@ $< +scrypt-intrin/obj/scrypt-sse2.o: scrypt-intrin/scrypt-sse2.cpp + $(CXX) -c $(xCXXFLAGS) -MMD -o $@ $< +else +# Generic implementation +OBJS += obj/scrypt-generic.o + +obj/scrypt-generic.o: scrypt-generic.cpp + $(CXX) -c $(xCXXFLAGS) -MMD -o $@ $< endif endif - obj/%.o: %.cpp $(HEADERS) g++ -c $(CFLAGS) -o $@ $< diff --git a/src/makefile.osx b/src/makefile.osx index 937a825..7c37722 100644 --- a/src/makefile.osx +++ b/src/makefile.osx @@ -93,8 +93,7 @@ OBJS= \ obj/wallet.o \ obj/walletdb.o \ obj/noui.o \ - obj/kernel.o \ - obj/scrypt.o + obj/kernel.o ifndef USE_UPNP override USE_UPNP = - @@ -130,26 +129,36 @@ OBJS += obj/txdb-bdb.o endif ifeq (${USE_ASM}, 1) -OBJS += obj/scrypt-x86.o obj/scrypt-x86_64.o +# Assembler implementation +OBJS += scrypt-asm/obj/scrypt-arm.o scrypt-asm/obj/scrypt-x86.o scrypt-asm/obj/scrypt-x86_64.o scrypt-asm/obj/asm-wrapper.o -obj/scrypt-x86.o: scrypt-x86.S +scrypt-asm/obj/scrypt-x86.o: scrypt-asm/scrypt-x86.S $(CXX) -c $(xCXXFLAGS) -MMD -o $@ $< -obj/scrypt-x86_64.o: scrypt-x86_64.S +scrypt-asm/obj/scrypt-x86_64.o: scrypt-asm/scrypt-x86_64.S $(CXX) -c $(xCXXFLAGS) -MMD -o $@ $< -endif -ifneq (${USE_ASM}, 1) -OBJS += obj/scrypt-generic.o -obj/scrypt-generic.o: scrypt-generic.c - $(CC) -c $(xCXXFLAGS) -MMD -o $@ $< +scrypt-asm/obj/scrypt-arm.o: scrypt-asm/scrypt-arm.S + $(CXX) -c $(xCXXFLAGS) -MMD -o $@ $< -ifneq (${USE_SSE2}, 1) and eq (${USE_ASM}, 1) +scrypt-asm/obj/asm-wrapper.o: scrypt-asm/asm-wrapper.cpp + $(CXX) -c $(xCXXFLAGS) -MMD -o $@ $< +else +ifeq (${USE_SSE2}, 1) +# Intrinsic implementation DEFS += -DUSE_SSE2 -OBJS += obj/scrypt-sse2.o +OBJS += scrypt-intrin/obj/scrypt-sse2.o -obj/scrypt-sse2.o: scrypt-sse2.cpp - $(CXX) -c $(CFLAGS) -MMD -o $@ $< +scrypt-intrin/obj/scrypt-sse2.o: scrypt-intrin/scrypt-sse2.cpp + $(CXX) -c $(xCXXFLAGS) -MMD -o $@ $< +else +ifneq (${USE_ASM}, 1) +# Generic implementation +OBJS += obj/scrypt-generic.o + +obj/scrypt-generic.o: scrypt-generic.cpp + $(CXX) -c $(xCXXFLAGS) -MMD -o $@ $< +endif endif endif @@ -176,6 +185,13 @@ clean: -rm -f novacoind -rm -f obj/*.o -rm -f obj/*.P + -rm -f obj/*.d + -rm -f scrypt-asm/obj/*.o + -rm -f scrypt-asm/obj/*.P + -rm -f scrypt-asm/obj/*.d + -rm -f scrypt-intrin/obj/*.o + -rm -f scrypt-intrin/obj/*.P + -rm -f scrypt-intrin/obj/*.d -rm -f obj/build.h FORCE: diff --git a/src/makefile.unix b/src/makefile.unix index 9bc4067..dfbb6ec 100644 --- a/src/makefile.unix +++ b/src/makefile.unix @@ -134,8 +134,7 @@ OBJS= \ obj/wallet.o \ obj/walletdb.o \ obj/noui.o \ - obj/kernel.o \ - obj/scrypt.o + obj/kernel.o all: novacoind @@ -156,28 +155,33 @@ OBJS += obj/txdb-bdb.o endif ifeq (${USE_ASM}, 1) -OBJS += obj/scrypt-arm.o obj/scrypt-x86.o obj/scrypt-x86_64.o +# Assembler implementation +OBJS += scrypt-asm/obj/scrypt-arm.o scrypt-asm/obj/scrypt-x86.o scrypt-asm/obj/scrypt-x86_64.o scrypt-asm/obj/asm-wrapper.o -obj/scrypt-x86.o: scrypt-x86.S +scrypt-asm/obj/scrypt-x86.o: scrypt-asm/scrypt-x86.S $(CXX) -c $(xCXXFLAGS) -MMD -o $@ $< -obj/scrypt-x86_64.o: scrypt-x86_64.S +scrypt-asm/obj/scrypt-x86_64.o: scrypt-asm/scrypt-x86_64.S $(CXX) -c $(xCXXFLAGS) -MMD -o $@ $< -obj/scrypt-arm.o: scrypt-arm.S +scrypt-asm/obj/scrypt-arm.o: scrypt-asm/scrypt-arm.S $(CXX) -c $(xCXXFLAGS) -MMD -o $@ $< -endif -ifneq (${USE_ASM}, 1) -OBJS += obj/scrypt-generic.o - -obj/scrypt-generic.o: scrypt-generic.c - $(CC) -c $(xCXXFLAGS) -MMD -o $@ $< +scrypt-asm/obj/asm-wrapper.o: scrypt-asm/asm-wrapper.cpp + $(CXX) -c $(xCXXFLAGS) -MMD -o $@ $< +else ifeq (${USE_SSE2}, 1) +# Intrinsic implementation DEFS += -DUSE_SSE2 -OBJS += obj/scrypt-sse2.o +OBJS += scrypt-intrin/obj/scrypt-sse2.o + +scrypt-intrin/obj/scrypt-sse2.o: scrypt-intrin/scrypt-sse2.cpp + $(CXX) -c $(xCXXFLAGS) -MMD -o $@ $< +else +# Generic implementation +OBJS += obj/scrypt-generic.o -obj/scrypt-sse2.o: scrypt-sse2.cpp +obj/scrypt-generic.o: scrypt-generic.cpp $(CXX) -c $(xCXXFLAGS) -MMD -o $@ $< endif endif @@ -206,6 +210,13 @@ clean: -rm -f novacoind -rm -f obj/*.o -rm -f obj/*.P + -rm -f obj/*.d + -rm -f scrypt-asm/obj/*.o + -rm -f scrypt-asm/obj/*.P + -rm -f scrypt-asm/obj/*.d + -rm -f scrypt-intrin/obj/*.o + -rm -f scrypt-intrin/obj/*.P + -rm -f scrypt-intrin/obj/*.d -rm -f obj/build.h FORCE: diff --git a/src/obj/.gitignore b/src/obj/.gitignore index c330091..d6b7ef3 100644 --- a/src/obj/.gitignore +++ b/src/obj/.gitignore @@ -1,3 +1,2 @@ * !.gitignore -!zerocoin diff --git a/src/scrypt-asm/asm-wrapper.cpp b/src/scrypt-asm/asm-wrapper.cpp new file mode 100644 index 0000000..e235a17 --- /dev/null +++ b/src/scrypt-asm/asm-wrapper.cpp @@ -0,0 +1,28 @@ +#include +#include + +#include "scrypt.h" + +#include "util.h" +#include "net.h" + +extern "C" void scrypt_core(uint32_t *X, uint32_t *V); + +/* 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 + */ +uint256 scrypt_blockhash(const uint8_t* input) +{ + uint8_t scratchpad[SCRYPT_BUFFER_SIZE]; + uint32_t X[32]; + uint256 result = 0; + + uint32_t *V = (uint32_t *)(((uintptr_t)(scratchpad) + 63) & ~ (uintptr_t)(63)); + + PKCS5_PBKDF2_HMAC((const char*)input, 80, input, 80, 1, EVP_sha256(), 128, (unsigned char *)X); + scrypt_core(X, V); + PKCS5_PBKDF2_HMAC((const char*)input, 80, (const unsigned char*)X, 128, 1, EVP_sha256(), 32, (unsigned char*)&result); + + return result; +} diff --git a/src/scrypt-asm/obj/.gitignore b/src/scrypt-asm/obj/.gitignore new file mode 100644 index 0000000..d6b7ef3 --- /dev/null +++ b/src/scrypt-asm/obj/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore diff --git a/src/scrypt-arm.S b/src/scrypt-asm/scrypt-arm.S similarity index 100% rename from src/scrypt-arm.S rename to src/scrypt-asm/scrypt-arm.S diff --git a/src/scrypt-x86.S b/src/scrypt-asm/scrypt-x86.S similarity index 100% rename from src/scrypt-x86.S rename to src/scrypt-asm/scrypt-x86.S diff --git a/src/scrypt-x86_64.S b/src/scrypt-asm/scrypt-x86_64.S similarity index 100% rename from src/scrypt-x86_64.S rename to src/scrypt-asm/scrypt-x86_64.S diff --git a/src/scrypt-generic.c b/src/scrypt-generic.cpp similarity index 82% rename from src/scrypt-generic.c rename to src/scrypt-generic.cpp index c3ad359..fa7b5e0 100644 --- a/src/scrypt-generic.c +++ b/src/scrypt-generic.cpp @@ -27,9 +27,10 @@ * online backup system. */ -#include -#include #include +#include + +#include "scrypt.h" #ifdef _MSC_VER #define INLINE __inline @@ -107,7 +108,7 @@ static INLINE void xor_salsa8(uint32_t B[16], const uint32_t Bx[16]) B[15] += x15; } -void scrypt_core(uint32_t *X, uint32_t *V) +INLINE void scrypt_core(uint32_t *X, uint32_t *V) { uint16_t i, j, k; @@ -124,3 +125,22 @@ void scrypt_core(uint32_t *X, uint32_t *V) xor_salsa8(&X[16], &X[0]); } } + +/* 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 + */ +uint256 scrypt_blockhash(const uint8_t* input) +{ + uint8_t scratchpad[SCRYPT_BUFFER_SIZE]; + uint32_t X[32]; + uint256 result = 0; + + uint32_t *V = (uint32_t *)(((uintptr_t)(scratchpad) + 63) & ~ (uintptr_t)(63)); + + PKCS5_PBKDF2_HMAC((const char*)input, 80, input, 80, 1, EVP_sha256(), 128, (unsigned char *)X); + scrypt_core(X, V); + PKCS5_PBKDF2_HMAC((const char*)input, 80, (const unsigned char*)X, 128, 1, EVP_sha256(), 32, (unsigned char*)&result); + + return result; +} diff --git a/src/scrypt-intrin/obj/.gitignore b/src/scrypt-intrin/obj/.gitignore new file mode 100644 index 0000000..d6b7ef3 --- /dev/null +++ b/src/scrypt-intrin/obj/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore diff --git a/src/scrypt-sse2.cpp b/src/scrypt-intrin/scrypt-sse2.cpp similarity index 99% rename from src/scrypt-sse2.cpp rename to src/scrypt-intrin/scrypt-sse2.cpp index 1f56367..89cacc8 100644 --- a/src/scrypt-sse2.cpp +++ b/src/scrypt-intrin/scrypt-sse2.cpp @@ -108,7 +108,7 @@ static inline void xor_salsa8_sse2(__m128i B[4], const __m128i Bx[4]) B[3] = _mm_add_epi32(B[3], X3); } -uint256 scrypt_blockhash__sse2(const uint8_t* input) +uint256 scrypt_blockhash(const uint8_t* input) { uint256 result = 0; uint8_t scratchpad[SCRYPT_BUFFER_SIZE]; diff --git a/src/scrypt.cpp b/src/scrypt.cpp deleted file mode 100644 index e7cfbda..0000000 --- a/src/scrypt.cpp +++ /dev/null @@ -1,77 +0,0 @@ -#include -#include - -#include "scrypt.h" - -#include "util.h" -#include "net.h" - -#ifdef USE_SSE2 -#ifdef _MSC_VER -// MSVC 64bit is unable to use inline asm -#include -#else -// GCC Linux or i686-w64-mingw32 -#include -#endif -#endif - -extern "C" void scrypt_core(uint32_t *X, uint32_t *V); -#ifdef USE_SSE2 -extern uint256 scrypt_blockhash__sse2(const uint8_t* input); -#endif -/* 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 - */ -uint256 scrypt_blockhash_generic(const uint8_t* input) -{ - uint8_t scratchpad[SCRYPT_BUFFER_SIZE]; - uint32_t X[32]; - uint256 result = 0; - - uint32_t *V = (uint32_t *)(((uintptr_t)(scratchpad) + 63) & ~ (uintptr_t)(63)); - - PKCS5_PBKDF2_HMAC((const char*)input, 80, input, 80, 1, EVP_sha256(), 128, (unsigned char *)X); - scrypt_core(X, V); - PKCS5_PBKDF2_HMAC((const char*)input, 80, (const unsigned char*)X, 128, 1, EVP_sha256(), 32, (unsigned char*)&result); - - return result; -} - -// By default, set to generic scrypt function. This will prevent crash in case when scrypt_detect_sse2() wasn't called -uint256 (*scrypt_blockhash_detected)(const uint8_t* input) = &scrypt_blockhash_generic; - -#ifdef USE_SSE2 -void scrypt_detect_sse2() -{ - // 32bit x86 Linux or Windows, detect cpuid features - unsigned int cpuid_edx=0; -#if defined(_MSC_VER) - // MSVC - int x86cpuid[4]; - __cpuid(x86cpuid, 1); - cpuid_edx = (unsigned int)x86cpuid[3]; -#else // _MSC_VER - // Linux or i686-w64-mingw32 (gcc-4.6.3) - unsigned int eax, ebx, ecx; - __get_cpuid(1, &eax, &ebx, &ecx, &cpuid_edx); -#endif // _MSC_VER - - if (cpuid_edx & 1<<26) - { - scrypt_blockhash_detected = &scrypt_blockhash__sse2; - printf("scrypt: using scrypt-sse2 as detected.\n"); - } - else - { - scrypt_blockhash_detected = &scrypt_blockhash_generic; - printf("scrypt: using scrypt-generic, SSE2 unavailable.\n"); - } -} -#endif - -uint256 scrypt_blockhash(const uint8_t* input) -{ - return scrypt_blockhash_detected(input); -} diff --git a/src/scrypt.h b/src/scrypt.h index 3c9e3bd..8a07277 100644 --- a/src/scrypt.h +++ b/src/scrypt.h @@ -11,8 +11,4 @@ uint256 scrypt_blockhash(const uint8_t* input); -#ifdef USE_SSE2 -void scrypt_detect_sse2(); -#endif - #endif // SCRYPT_MINE_H