Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Litecoin: enable support for SSE2 instructions in scrypt via configure --enable-sse2 #362

Merged
merged 2 commits into from
Aug 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ env:
# 32-bit + dash
- HOST=i686-pc-linux-gnu PACKAGES="g++-multilib bc python3-pip python3-dev 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 python3-dev python3-pip nsis g++-mingw-w64-x86-64 wine1.6 bc" 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 python3-dev python3-pip nsis g++-mingw-w64-x86-64 wine1.6 bc" RUN_TESTS=true GOAL="install" BITCOIN_CONFIG="--enable-reduce-exports --enable-sse2" LITECOIN_SCRYPT=1
# bitcoind
- HOST=x86_64-unknown-linux-gnu PACKAGES="bc python3-zmq python3-dev python3-pip" DEP_OPTS="NO_QT=1 NO_UPNP=1 DEBUG=1" RUN_TESTS=true GOAL="install" BITCOIN_CONFIG="--enable-zmq --enable-glibc-back-compat --enable-reduce-exports CPPFLAGS=-DDEBUG_LOCKORDER" LITECOIN_SCRYPT=1
# No wallet
- HOST=x86_64-unknown-linux-gnu PACKAGES="python3 python3-dev python3-pip" DEP_OPTS="NO_WALLET=1" RUN_TESTS=true GOAL="install" BITCOIN_CONFIG="--enable-glibc-back-compat --enable-reduce-exports" 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" 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-sse2" OSX_SDK=10.11 GOAL="deploy"

before_install:
- export PATH=$(echo $PATH | tr ':' "\n" | sed '/\/opt\/python/d' | tr "\n" ":" | sed "s|::|:|g")
Expand Down
18 changes: 18 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,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,
Expand Down Expand Up @@ -1041,6 +1047,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])
Expand All @@ -1061,6 +1077,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])
Expand Down Expand Up @@ -1099,6 +1116,7 @@ AC_SUBST(PIE_FLAGS)
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)
Expand Down
1 change: 1 addition & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,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 \
Expand Down
4 changes: 4 additions & 0 deletions src/crypto/scrypt-sse2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
* online backup system.
*/

#if defined(USE_SSE2)

#include "crypto/scrypt.h"
#include <stdlib.h>
#include <stdint.h>
Expand Down Expand Up @@ -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
10 changes: 6 additions & 4 deletions src/crypto/scrypt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,10 +290,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;
Expand All @@ -311,14 +312,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

Expand Down
3 changes: 2 additions & 1 deletion src/crypto/scrypt.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ 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 <string>
#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))
#else
#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
Expand Down
7 changes: 6 additions & 1 deletion src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@
#include "zmq/zmqnotificationinterface.h"
#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;
Expand Down Expand Up @@ -1205,7 +1209,8 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
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
Expand Down
2 changes: 1 addition & 1 deletion src/test/scrypt_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<unsigned char> inputbytes;
Expand Down