Reorganize scrypt function implementations.
authorCryptoManiac <balthazar@yandex.ru>
Thu, 9 Apr 2015 14:18:00 +0000 (17:18 +0300)
committerCryptoManiac <balthazar@yandex.ru>
Thu, 9 Apr 2015 14:21:55 +0000 (17:21 +0300)
* 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.

19 files changed:
MSVC/libcommon/libcommon.vcxproj
MSVC/libcommon/libcommon.vcxproj.filters
novacoin-qt.pro
src/makefile.bsd
src/makefile.linux-mingw
src/makefile.mingw
src/makefile.osx
src/makefile.unix
src/obj/.gitignore
src/scrypt-asm/asm-wrapper.cpp [new file with mode: 0644]
src/scrypt-asm/obj/.gitignore [new file with mode: 0644]
src/scrypt-asm/scrypt-arm.S [moved from src/scrypt-arm.S with 100% similarity]
src/scrypt-asm/scrypt-x86.S [moved from src/scrypt-x86.S with 100% similarity]
src/scrypt-asm/scrypt-x86_64.S [moved from src/scrypt-x86_64.S with 100% similarity]
src/scrypt-generic.cpp [moved from src/scrypt-generic.c with 82% similarity]
src/scrypt-intrin/obj/.gitignore [new file with mode: 0644]
src/scrypt-intrin/scrypt-sse2.cpp [moved from src/scrypt-sse2.cpp with 99% similarity]
src/scrypt.cpp [deleted file]
src/scrypt.h

index 8ddeac1..4b91768 100644 (file)
@@ -20,8 +20,6 @@
   </ItemGroup>
   <ItemGroup>
     <ClCompile Include="..\..\src\alert.cpp" />
-    <ClCompile Include="..\..\src\scrypt-generic.c" />
-    <ClCompile Include="..\..\src\scrypt-sse2.cpp" />
     <ClCompile Include="..\..\src\stun.cpp" />
     <ClCompile Include="..\..\src\txdb-leveldb.cpp" />
     <ClCompile Include="..\..\src\version.cpp" />
@@ -52,7 +50,7 @@
     <ClCompile Include="..\..\src\noui.cpp" />
     <ClCompile Include="..\..\src\kernel.cpp" />
     <ClCompile Include="..\..\src\pbkdf2.cpp" />
-    <ClCompile Include="..\..\src\scrypt.cpp" />
+    <ClCompile Include="..\..\src\scrypt-intrin\scrypt-sse2.cpp" />
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="..\..\src\addrman.h" />
index b358bf8..dffbc2e 100644 (file)
@@ -98,7 +98,7 @@
     <ClCompile Include="..\..\src\pbkdf2.cpp">
       <Filter>Source Files</Filter>
     </ClCompile>
-    <ClCompile Include="..\..\src\scrypt.cpp">
+    <ClCompile Include="..\..\src\scrypt-intrin\scrypt-sse2.cpp">
       <Filter>Source Files</Filter>
     </ClCompile>
     <ClCompile Include="..\..\src\txdb-leveldb.cpp">
     <ClCompile Include="..\..\src\stun.cpp">
       <Filter>Source Files</Filter>
     </ClCompile>
-    <ClCompile Include="..\..\src\scrypt-generic.c">
-      <Filter>Source Files</Filter>
-    </ClCompile>
-    <ClCompile Include="..\..\src\scrypt-sse2.cpp">
-      <Filter>Source Files</Filter>
-    </ClCompile>
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="..\..\src\addrman.h">
index d43cf89..efefa8e 100644 (file)
@@ -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
index 5035b19..0d2babd 100644 (file)
@@ -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:
index e71d542..e11c66d 100644 (file)
@@ -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 ..
 
index a621c9d..71b7cdf 100644 (file)
@@ -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 $@ $<
 
index 937a825..7c37722 100644 (file)
@@ -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:
index 9bc4067..dfbb6ec 100644 (file)
@@ -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:
index c330091..d6b7ef3 100644 (file)
@@ -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 (file)
index 0000000..e235a17
--- /dev/null
@@ -0,0 +1,28 @@
+#include <stdlib.h>
+#include <openssl/evp.h>
+
+#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 (file)
index 0000000..d6b7ef3
--- /dev/null
@@ -0,0 +1,2 @@
+*
+!.gitignore
similarity index 100%
rename from src/scrypt-arm.S
rename to src/scrypt-asm/scrypt-arm.S
similarity index 100%
rename from src/scrypt-x86.S
rename to src/scrypt-asm/scrypt-x86.S
similarity index 82%
rename from src/scrypt-generic.c
rename to src/scrypt-generic.cpp
index c3ad359..fa7b5e0 100644 (file)
  * online backup system.
  */
 
-#include <stdlib.h>
-#include <stdint.h>
 #include <string.h>
+#include <openssl/evp.h>
+
+#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 (file)
index 0000000..d6b7ef3
--- /dev/null
@@ -0,0 +1,2 @@
+*
+!.gitignore
similarity index 99%
rename from src/scrypt-sse2.cpp
rename to src/scrypt-intrin/scrypt-sse2.cpp
index 1f56367..89cacc8 100644 (file)
@@ -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 (file)
index e7cfbda..0000000
+++ /dev/null
@@ -1,77 +0,0 @@
-#include <stdlib.h>
-#include <openssl/evp.h>
-
-#include "scrypt.h"
-
-#include "util.h"
-#include "net.h"
-
-#ifdef USE_SSE2
-#ifdef _MSC_VER
-// MSVC 64bit is unable to use inline asm
-#include <intrin.h>
-#else
-// GCC Linux or i686-w64-mingw32
-#include <cpuid.h>
-#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);
-}
index 3c9e3bd..8a07277 100644 (file)
@@ -11,8 +11,4 @@
 
 uint256 scrypt_blockhash(const uint8_t* input);
 
-#ifdef USE_SSE2
-void scrypt_detect_sse2();
-#endif
-
 #endif // SCRYPT_MINE_H