Skip to content

Commit 4dbd047

Browse files
committed
crypto: remove use of BUILD_BITCOIN_INTERNAL macro in sha256
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.
1 parent 65c05db commit 4dbd047

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

src/Makefile.am

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -906,7 +906,7 @@ lib_LTLIBRARIES += $(LIBBITCOINKERNEL)
906906

907907
libbitcoinkernel_la_LDFLAGS = $(AM_LDFLAGS) -no-undefined $(RELDFLAGS) $(PTHREAD_FLAGS)
908908
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)
909+
libbitcoinkernel_la_CPPFLAGS = $(AM_CPPFLAGS) -I$(builddir)/obj -I$(srcdir)/secp256k1/include $(BOOST_CPPFLAGS) $(LEVELDB_CPPFLAGS)
910910

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

10131013
libbitcoinconsensus_la_LDFLAGS = $(AM_LDFLAGS) -no-undefined $(RELDFLAGS)
10141014
libbitcoinconsensus_la_LIBADD = $(LIBSECP256K1)
1015-
libbitcoinconsensus_la_CPPFLAGS = $(AM_CPPFLAGS) -I$(builddir)/obj -I$(srcdir)/secp256k1/include -DBUILD_BITCOIN_INTERNAL
1015+
libbitcoinconsensus_la_CPPFLAGS = $(AM_CPPFLAGS) -I$(builddir)/obj -I$(srcdir)/secp256k1/include -DBUILD_BITCOIN_INTERNAL -DDISABLE_OPTIMIZED_SHA256
10161016
libbitcoinconsensus_la_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
10171017

10181018
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)