Skip to content

Commit 5fbcc8f

Browse files
committed
Merge bitcoin/bitcoin#29180: crypto: remove use of BUILD_BITCOIN_INTERNAL macro in sha256
bbf218d crypto: remove sha256_sse4 from the base crypto helper lib (Cory Fields) 4dbd047 crypto: remove use of BUILD_BITCOIN_INTERNAL macro in sha256 (Cory Fields) Pull request description: Replace it with a more explicit `DISABLE_OPTIMIZED_SHA256` and clean up some. The macro was originally used by libbitcoinconsensus which opts out of optimized sha256 for the sake of simplicity. Also remove the `BUILD_BITCOIN_INTERNAL` define from libbitcoinkernel for now as it does not export an api. When it does we can pick a less confusing define to control its exports. Removing the define should have the effect of enabling sha256 optimizations for the kernel. ACKs for top commit: TheCharlatan: Re-ACK bbf218d hebasto: re-ACK bbf218d Tree-SHA512: 7c17592bb2d3e671779f96903cb36887c5785408213bffbda1ae37b66e6bcfaffaefd0c1bf2d1a407060cd377e3d4881cde3a73c429a1aacb677f370314a066a
2 parents ff0eac0 + bbf218d commit 5fbcc8f

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

src/Makefile.am

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ LIBBITCOIN_WALLET_TOOL=libbitcoin_wallet_tool.a
5050
endif
5151

5252
LIBBITCOIN_CRYPTO = $(LIBBITCOIN_CRYPTO_BASE)
53+
if USE_ASM
54+
LIBBITCOIN_CRYPTO_SSE4 = crypto/libbitcoin_crypto_sse4.la
55+
LIBBITCOIN_CRYPTO += $(LIBBITCOIN_CRYPTO_SSE4)
56+
endif
5357
if ENABLE_SSE41
5458
LIBBITCOIN_CRYPTO_SSE41 = crypto/libbitcoin_crypto_sse41.la
5559
LIBBITCOIN_CRYPTO += $(LIBBITCOIN_CRYPTO_SSE41)
@@ -541,6 +545,10 @@ libbitcoin_wallet_tool_a_SOURCES = \
541545
#
542546

543547
# crypto #
548+
549+
# crypto_base contains the unspecialized (unoptimized) versions of our
550+
# crypto functions. Functions that require custom compiler flags and/or
551+
# runtime opt-in are omitted.
544552
crypto_libbitcoin_crypto_base_la_CPPFLAGS = $(AM_CPPFLAGS)
545553

546554
# Specify -static in both CXXFLAGS and LDFLAGS so libtool will only build a
@@ -581,9 +589,12 @@ crypto_libbitcoin_crypto_base_la_SOURCES = \
581589
crypto/siphash.cpp \
582590
crypto/siphash.h
583591

584-
if USE_ASM
585-
crypto_libbitcoin_crypto_base_la_SOURCES += crypto/sha256_sse4.cpp
586-
endif
592+
# See explanation for -static in crypto_libbitcoin_crypto_base_la's LDFLAGS and
593+
# CXXFLAGS above
594+
crypto_libbitcoin_crypto_sse4_la_LDFLAGS = $(AM_LDFLAGS) -static
595+
crypto_libbitcoin_crypto_sse4_la_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS) -static
596+
crypto_libbitcoin_crypto_sse4_la_CPPFLAGS = $(AM_CPPFLAGS)
597+
crypto_libbitcoin_crypto_sse4_la_SOURCES = crypto/sha256_sse4.cpp
587598

588599
# See explanation for -static in crypto_libbitcoin_crypto_base_la's LDFLAGS and
589600
# CXXFLAGS above
@@ -906,7 +917,7 @@ lib_LTLIBRARIES += $(LIBBITCOINKERNEL)
906917

907918
libbitcoinkernel_la_LDFLAGS = $(AM_LDFLAGS) -no-undefined $(RELDFLAGS) $(PTHREAD_FLAGS)
908919
libbitcoinkernel_la_LIBADD = $(LIBBITCOIN_CRYPTO) $(LIBLEVELDB) $(LIBMEMENV) $(LIBSECP256K1)
909-
libbitcoinkernel_la_CPPFLAGS = $(AM_CPPFLAGS) -I$(builddir)/obj -I$(srcdir)/secp256k1/include -DBUILD_BITCOIN_INTERNAL $(BOOST_CPPFLAGS) $(LEVELDB_CPPFLAGS)
920+
libbitcoinkernel_la_CPPFLAGS = $(AM_CPPFLAGS) -I$(builddir)/obj -I$(srcdir)/secp256k1/include $(BOOST_CPPFLAGS) $(LEVELDB_CPPFLAGS)
910921

911922
# libbitcoinkernel requires default symbol visibility, explicitly specify that
912923
# here so that things still work even when user configures with
@@ -1012,7 +1023,7 @@ libbitcoinconsensus_la_SOURCES = support/cleanse.cpp $(crypto_libbitcoin_crypto_
10121023

10131024
libbitcoinconsensus_la_LDFLAGS = $(AM_LDFLAGS) -no-undefined $(RELDFLAGS)
10141025
libbitcoinconsensus_la_LIBADD = $(LIBSECP256K1)
1015-
libbitcoinconsensus_la_CPPFLAGS = $(AM_CPPFLAGS) -I$(builddir)/obj -I$(srcdir)/secp256k1/include -DBUILD_BITCOIN_INTERNAL
1026+
libbitcoinconsensus_la_CPPFLAGS = $(AM_CPPFLAGS) -I$(builddir)/obj -I$(srcdir)/secp256k1/include -DBUILD_BITCOIN_INTERNAL -DDISABLE_OPTIMIZED_SHA256
10161027
libbitcoinconsensus_la_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
10171028

10181029
endif

src/crypto/sha256.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88
#include <assert.h>
99
#include <string.h>
1010

11+
#if !defined(DISABLE_OPTIMIZED_SHA256)
1112
#include <compat/cpuid.h>
1213

13-
#if defined(__linux__) && defined(ENABLE_ARM_SHANI) && !defined(BUILD_BITCOIN_INTERNAL)
14+
#if defined(__linux__) && defined(ENABLE_ARM_SHANI)
1415
#include <sys/auxv.h>
1516
#include <asm/hwcap.h>
1617
#endif
1718

18-
#if defined(MAC_OSX) && defined(ENABLE_ARM_SHANI) && !defined(BUILD_BITCOIN_INTERNAL)
19+
#if defined(MAC_OSX) && defined(ENABLE_ARM_SHANI)
1920
#include <sys/types.h>
2021
#include <sys/sysctl.h>
2122
#endif
@@ -58,6 +59,7 @@ namespace sha256d64_arm_shani
5859
{
5960
void Transform_2way(unsigned char* out, const unsigned char* in);
6061
}
62+
#endif // DISABLE_OPTIMIZED_SHA256
6163

6264
// Internal implementation code.
6365
namespace
@@ -567,6 +569,7 @@ bool SelfTest() {
567569
return true;
568570
}
569571

572+
#if !defined(DISABLE_OPTIMIZED_SHA256)
570573
#if defined(USE_ASM) && (defined(__x86_64__) || defined(__amd64__) || defined(__i386__))
571574
/** Check whether the OS has enabled AVX registers. */
572575
bool AVXEnabled()
@@ -576,6 +579,7 @@ bool AVXEnabled()
576579
return (a & 6) == 6;
577580
}
578581
#endif
582+
#endif // DISABLE_OPTIMIZED_SHA256
579583
} // namespace
580584

581585

@@ -588,6 +592,7 @@ std::string SHA256AutoDetect(sha256_implementation::UseImplementation use_implem
588592
TransformD64_4way = nullptr;
589593
TransformD64_8way = nullptr;
590594

595+
#if !defined(DISABLE_OPTIMIZED_SHA256)
591596
#if defined(USE_ASM) && defined(HAVE_GETCPUID)
592597
bool have_sse4 = false;
593598
bool have_xsave = false;
@@ -616,7 +621,7 @@ std::string SHA256AutoDetect(sha256_implementation::UseImplementation use_implem
616621
}
617622
}
618623

619-
#if defined(ENABLE_X86_SHANI) && !defined(BUILD_BITCOIN_INTERNAL)
624+
#if defined(ENABLE_X86_SHANI)
620625
if (have_x86_shani) {
621626
Transform = sha256_x86_shani::Transform;
622627
TransformD64 = TransformD64Wrapper<sha256_x86_shani::Transform>;
@@ -633,21 +638,21 @@ std::string SHA256AutoDetect(sha256_implementation::UseImplementation use_implem
633638
TransformD64 = TransformD64Wrapper<sha256_sse4::Transform>;
634639
ret = "sse4(1way)";
635640
#endif
636-
#if defined(ENABLE_SSE41) && !defined(BUILD_BITCOIN_INTERNAL)
641+
#if defined(ENABLE_SSE41)
637642
TransformD64_4way = sha256d64_sse41::Transform_4way;
638643
ret += ",sse41(4way)";
639644
#endif
640645
}
641646

642-
#if defined(ENABLE_AVX2) && !defined(BUILD_BITCOIN_INTERNAL)
647+
#if defined(ENABLE_AVX2)
643648
if (have_avx2 && have_avx && enabled_avx) {
644649
TransformD64_8way = sha256d64_avx2::Transform_8way;
645650
ret += ",avx2(8way)";
646651
}
647652
#endif
648653
#endif // defined(USE_ASM) && defined(HAVE_GETCPUID)
649654

650-
#if defined(ENABLE_ARM_SHANI) && !defined(BUILD_BITCOIN_INTERNAL)
655+
#if defined(ENABLE_ARM_SHANI)
651656
bool have_arm_shani = false;
652657
if (use_implementation & sha256_implementation::USE_SHANI) {
653658
#if defined(__linux__)
@@ -679,6 +684,7 @@ std::string SHA256AutoDetect(sha256_implementation::UseImplementation use_implem
679684
ret = "arm_shani(1way,2way)";
680685
}
681686
#endif
687+
#endif // DISABLE_OPTIMIZED_SHA256
682688

683689
assert(SelfTest());
684690
return ret;

0 commit comments

Comments
 (0)