From 08f253886293bc0a91567111c6e22a9589b0c9c1 Mon Sep 17 00:00:00 2001 From: Romano Date: Mon, 14 Aug 2017 13:36:49 +1000 Subject: [PATCH] Litecoin: Enable support for SSE2 instructions in scrypt via configure --enable-sse2 (#362) --- .travis.yml | 10 +++++----- configure.ac | 18 ++++++++++++++++++ src/Makefile.am | 1 + src/crypto/scrypt-sse2.cpp | 4 ++++ src/crypto/scrypt.cpp | 10 ++++++---- src/crypto/scrypt.h | 3 ++- src/init.cpp | 7 ++++++- src/test/scrypt_tests.cpp | 2 +- 8 files changed, 43 insertions(+), 12 deletions(-) diff --git a/.travis.yml b/.travis.yml index cf14636b4..3bc15e031 100644 --- a/.travis.yml +++ b/.travis.yml @@ -32,13 +32,13 @@ env: # 32-bit + dash - HOST=i686-pc-linux-gnu PACKAGES="g++-multilib python3-zmq" DEP_OPTS="NO_QT=1" RUN_TESTS=true GOAL="install" BITCOIN_CONFIG="--enable-zmq --enable-glibc-back-compat --enable-reduce-exports LDFLAGS=-static-libstdc++" USE_SHELL="/bin/dash" LITECOIN_SCRYPT=1 # Win64 - - HOST=x86_64-w64-mingw32 DPKG_ADD_ARCH="i386" DEP_OPTS="NO_QT=1" PACKAGES="python3 nsis g++-mingw-w64-x86-64 wine1.6" RUN_TESTS=true GOAL="install" BITCOIN_CONFIG="--enable-reduce-exports" LITECOIN_SCRYPT=1 + - HOST=x86_64-w64-mingw32 DPKG_ADD_ARCH="i386" DEP_OPTS="NO_QT=1" PACKAGES="python3 nsis g++-mingw-w64-x86-64 wine1.6" RUN_TESTS=true GOAL="install" BITCOIN_CONFIG="--enable-reduce-exports --enable-sse2" LITECOIN_SCRYPT=1 # x86_64 Linux (uses qt5 dev package instead of depends Qt to speed up build and avoid timeout) - - HOST=x86_64-unknown-linux-gnu PACKAGES="python3-zmq qtbase5-dev qttools5-dev-tools protobuf-compiler libdbus-1-dev libharfbuzz-dev" DEP_OPTS="NO_QT=1 NO_UPNP=1 DEBUG=1 ALLOW_HOST_PACKAGES=1" RUN_TESTS=true GOAL="install" BITCOIN_CONFIG="--enable-zmq --with-gui=qt5 --enable-glibc-back-compat --enable-reduce-exports CPPFLAGS=-DDEBUG_LOCKORDER" LITECOIN_SCRYPT=1 + - HOST=x86_64-unknown-linux-gnu PACKAGES="python3-zmq qtbase5-dev qttools5-dev-tools protobuf-compiler libdbus-1-dev libharfbuzz-dev" DEP_OPTS="NO_QT=1 NO_UPNP=1 DEBUG=1 ALLOW_HOST_PACKAGES=1" RUN_TESTS=true GOAL="install" BITCOIN_CONFIG="--enable-zmq --with-gui=qt5 --enable-glibc-back-compat --enable-reduce-exports --enable-sse2 CPPFLAGS=-DDEBUG_LOCKORDER" LITECOIN_SCRYPT=1 # x86_64 Linux, No wallet - - HOST=x86_64-unknown-linux-gnu PACKAGES="python3" DEP_OPTS="NO_WALLET=1" RUN_TESTS=true GOAL="install" BITCOIN_CONFIG="--enable-glibc-back-compat --enable-reduce-exports" LITECOIN_SCRYPT=1 + - HOST=x86_64-unknown-linux-gnu PACKAGES="python3" DEP_OPTS="NO_WALLET=1" RUN_TESTS=true GOAL="install" BITCOIN_CONFIG="--enable-glibc-back-compat --enable-reduce-exports --enable-sse2" LITECOIN_SCRYPT=1 # Cross-Mac - - HOST=x86_64-apple-darwin11 PACKAGES="cmake imagemagick libcap-dev librsvg2-bin libz-dev libbz2-dev libtiff-tools python-dev" BITCOIN_CONFIG="--enable-gui --enable-reduce-exports --enable-werror" OSX_SDK=10.11 GOAL="deploy" + - HOST=x86_64-apple-darwin11 PACKAGES="cmake imagemagick libcap-dev librsvg2-bin libz-dev libbz2-dev libtiff-tools python-dev" BITCOIN_CONFIG="--enable-gui --enable-reduce-exports --enable-werror --enable-sse2" OSX_SDK=10.11 GOAL="deploy" before_install: - export PATH=$(echo $PATH | tr ':' "\n" | sed '/\/opt\/python/d' | tr "\n" ":" | sed "s|::|:|g") @@ -76,7 +76,7 @@ script: - mkdir build && cd build - ../configure --cache-file=config.cache $BITCOIN_CONFIG_ALL $BITCOIN_CONFIG || ( cat config.log && false) - make distdir VERSION=$HOST - - cd bitcoin-$HOST + - cd litecoin-$HOST - ./configure --cache-file=../config.cache $BITCOIN_CONFIG_ALL $BITCOIN_CONFIG || ( cat config.log && false) - make $MAKEJOBS $GOAL || ( echo "Build failure. Verbose build follows." && make $GOAL V=1 ; false ) - export LD_LIBRARY_PATH=$TRAVIS_BUILD_DIR/depends/$HOST/lib diff --git a/configure.ac b/configure.ac index c865d0e3a..3a3ebcd8c 100644 --- a/configure.ac +++ b/configure.ac @@ -199,6 +199,12 @@ AC_ARG_ENABLE([zmq], [use_zmq=$enableval], [use_zmq=yes]) +AC_ARG_ENABLE([sse2], + [AS_HELP_STRING([--enable-sse2], + [enable SSE2 instructions in the scrypt library. (default is disabled)])], + [use_sse2=$enableval], + [use_sse2=no]) + AC_ARG_WITH([protoc-bindir],[AS_HELP_STRING([--with-protoc-bindir=BIN_DIR],[specify protoc bin path])], [protoc_bin_path=$withval], []) AC_ARG_ENABLE(man, @@ -1185,6 +1191,16 @@ else BUILD_TEST="" fi +AC_MSG_CHECKING([whether to enable sse2 instructions]) +if test x$use_sse2 != xno; then + AC_MSG_RESULT(yes) + AC_DEFINE([USE_SSE2],[1],[Define if SSE2 support should be compiled in]) + use_sse2=yes + CPPFLAGS="$CPPFLAGS -DUSE_SSE2=1" +else + AC_MSG_RESULT(no) +fi + AC_MSG_CHECKING([whether to reduce exports]) if test x$use_reduce_exports = xyes; then AC_MSG_RESULT([yes]) @@ -1205,6 +1221,7 @@ AM_CONDITIONAL([ENABLE_QT],[test x$bitcoin_enable_qt = xyes]) AM_CONDITIONAL([ENABLE_QT_TESTS],[test x$BUILD_TEST_QT = xyes]) AM_CONDITIONAL([ENABLE_BENCH],[test x$use_bench = xyes]) AM_CONDITIONAL([USE_QRCODE], [test x$use_qr = xyes]) +AM_CONDITIONAL([USE_SSE2], [test x$use_sse2 = xyes]) AM_CONDITIONAL([USE_LCOV],[test x$use_lcov = xyes]) AM_CONDITIONAL([GLIBC_BACK_COMPAT],[test x$use_glibc_compat = xyes]) AM_CONDITIONAL([HARDEN],[test x$use_hardening = xyes]) @@ -1246,6 +1263,7 @@ AC_SUBST(SSE42_CXXFLAGS) AC_SUBST(LIBTOOL_APP_LDFLAGS) AC_SUBST(USE_UPNP) AC_SUBST(USE_QRCODE) +AC_SUBST(USE_SSE2) AC_SUBST(BOOST_LIBS) AC_SUBST(TESTDEFS) AC_SUBST(LEVELDB_TARGET_FLAGS) diff --git a/src/Makefile.am b/src/Makefile.am index b6b5e4582..f81b5215b 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -270,6 +270,7 @@ crypto_libbitcoin_crypto_a_SOURCES = \ crypto/ripemd160.cpp \ crypto/ripemd160.h \ crypto/scrypt.cpp \ + crypto/scrypt-sse2.cpp \ crypto/scrypt.h \ crypto/sha1.cpp \ crypto/sha1.h \ diff --git a/src/crypto/scrypt-sse2.cpp b/src/crypto/scrypt-sse2.cpp index d9e7ffccc..9b8c48b8f 100644 --- a/src/crypto/scrypt-sse2.cpp +++ b/src/crypto/scrypt-sse2.cpp @@ -27,6 +27,8 @@ * online backup system. */ +#if defined(USE_SSE2) + #include "crypto/scrypt.h" #include #include @@ -134,3 +136,5 @@ void scrypt_1024_1_1_256_sp_sse2(const char *input, char *output, char *scratchp PBKDF2_SHA256((const uint8_t *)input, 80, B, 128, 1, (uint8_t *)output, 32); } + +#endif // USE_SSE2 diff --git a/src/crypto/scrypt.cpp b/src/crypto/scrypt.cpp index 7f4707a38..c3a8d5653 100644 --- a/src/crypto/scrypt.cpp +++ b/src/crypto/scrypt.cpp @@ -291,10 +291,11 @@ void scrypt_1024_1_1_256_sp_generic(const char *input, char *output, char *scrat // By default, set to generic scrypt function. This will prevent crash in case when scrypt_detect_sse2() wasn't called void (*scrypt_1024_1_1_256_sp_detected)(const char *input, char *output, char *scratchpad) = &scrypt_1024_1_1_256_sp_generic; -void scrypt_detect_sse2() +std::string scrypt_detect_sse2() { + std::string ret; #if defined(USE_SSE2_ALWAYS) - printf("scrypt: using scrypt-sse2 as built.\n"); + ret = "scrypt: using scrypt-sse2 as built."; #else // USE_SSE2_ALWAYS // 32bit x86 Linux or Windows, detect cpuid features unsigned int cpuid_edx=0; @@ -312,14 +313,15 @@ void scrypt_detect_sse2() if (cpuid_edx & 1<<26) { scrypt_1024_1_1_256_sp_detected = &scrypt_1024_1_1_256_sp_sse2; - printf("scrypt: using scrypt-sse2 as detected.\n"); + ret = "scrypt: using scrypt-sse2 as detected"); } else { scrypt_1024_1_1_256_sp_detected = &scrypt_1024_1_1_256_sp_generic; - printf("scrypt: using scrypt-generic, SSE2 unavailable.\n"); + ret = "scrypt: using scrypt-generic, SSE2 unavailable"; } #endif // USE_SSE2_ALWAYS + return ret; } #endif diff --git a/src/crypto/scrypt.h b/src/crypto/scrypt.h index 8c1158a32..81f4dd2a9 100644 --- a/src/crypto/scrypt.h +++ b/src/crypto/scrypt.h @@ -9,6 +9,7 @@ void scrypt_1024_1_1_256(const char *input, char *output); void scrypt_1024_1_1_256_sp_generic(const char *input, char *output, char *scratchpad); #if defined(USE_SSE2) +#include #if defined(_M_X64) || defined(__x86_64__) || defined(_M_AMD64) || (defined(MAC_OSX) && defined(__i386__)) #define USE_SSE2_ALWAYS 1 #define scrypt_1024_1_1_256_sp(input, output, scratchpad) scrypt_1024_1_1_256_sp_sse2((input), (output), (scratchpad)) @@ -16,7 +17,7 @@ void scrypt_1024_1_1_256_sp_generic(const char *input, char *output, char *scrat #define scrypt_1024_1_1_256_sp(input, output, scratchpad) scrypt_1024_1_1_256_sp_detected((input), (output), (scratchpad)) #endif -void scrypt_detect_sse2(); +std::string scrypt_detect_sse2(); void scrypt_1024_1_1_256_sp_sse2(const char *input, char *output, char *scratchpad); extern void (*scrypt_1024_1_1_256_sp_detected)(const char *input, char *output, char *scratchpad); #else diff --git a/src/init.cpp b/src/init.cpp index d8b88968e..14f96569a 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -67,6 +67,10 @@ #include #endif +#ifdef USE_SSE2 +#include "crypto/scrypt.h" +#endif + bool fFeeEstimatesInitialized = false; static const bool DEFAULT_PROXYRANDOMIZE = true; static const bool DEFAULT_REST_ENABLE = false; @@ -1274,7 +1278,8 @@ bool AppInitMain() int64_t nStart; #if defined(USE_SSE2) - scrypt_detect_sse2(); + std::string sse2detect = scrypt_detect_sse2(); + LogPrintf("%s\n", sse2detect); #endif // ********************************************************* Step 5: verify wallet database integrity diff --git a/src/test/scrypt_tests.cpp b/src/test/scrypt_tests.cpp index 2d7f19e4d..214dbd5d9 100644 --- a/src/test/scrypt_tests.cpp +++ b/src/test/scrypt_tests.cpp @@ -14,7 +14,7 @@ BOOST_AUTO_TEST_CASE(scrypt_hashtest) const char* inputhex[HASHCOUNT] = { "020000004c1271c211717198227392b029a64a7971931d351b387bb80db027f270411e398a07046f7d4a08dd815412a8712f874a7ebf0507e3878bd24e20a3b73fd750a667d2f451eac7471b00de6659", "0200000011503ee6a855e900c00cfdd98f5f55fffeaee9b6bf55bea9b852d9de2ce35828e204eef76acfd36949ae56d1fbe81c1ac9c0209e6331ad56414f9072506a77f8c6faf551eac7471b00389d01", "02000000a72c8a177f523946f42f22c3e86b8023221b4105e8007e59e81f6beb013e29aaf635295cb9ac966213fb56e046dc71df5b3f7f67ceaeab24038e743f883aff1aaafaf551eac7471b0166249b", "010000007824bc3a8a1b4628485eee3024abd8626721f7f870f8ad4d2f33a27155167f6a4009d1285049603888fe85a84b6c803a53305a8d497965a5e896e1a00568359589faf551eac7471b0065434e", "0200000050bfd4e4a307a8cb6ef4aef69abc5c0f2d579648bd80d7733e1ccc3fbc90ed664a7f74006cb11bde87785f229ecd366c2d4e44432832580e0608c579e4cb76f383f7f551eac7471b00c36982" }; const char* expected[HASHCOUNT] = { "00000000002bef4107f882f6115e0b01f348d21195dacd3582aa2dabd7985806" , "00000000003a0d11bdd5eb634e08b7feddcfbbf228ed35d250daf19f1c88fc94", "00000000000b40f895f288e13244728a6c2d9d59d8aff29c65f8dd5114a8ca81", "00000000003007005891cd4923031e99d8e8d72f6e8e7edc6a86181897e105fe", "000000000018f0b426a4afc7130ccb47fa02af730d345b4fe7c7724d3800ec8c" }; #if defined(USE_SSE2) - scrypt_detect_sse2(); + (void) scrypt_detect_sse2(); #endif uint256 scrypthash; std::vector inputbytes;