diff --git a/CMakeLists.txt b/CMakeLists.txt index aeb2fe5c0d2c7..bf6b8a62e393b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,6 +38,7 @@ list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/module) # Configurable options. # When adding a new option, end the with a full stop for consistency. include(CMakeDependentOption) +option(ASM "Use assembly routines." ON) cmake_dependent_option(CXX20 "Enable compilation in C++20 mode." OFF "NOT MSVC" ON) if(CXX20) @@ -125,6 +126,7 @@ else() message(" - LDFLAGS for executables ............ ${CMAKE_EXE_LINKER_FLAGS_RELEASE}") message(" - LDFLAGS for shared libraries ....... ${CMAKE_SHARED_LINKER_FLAGS_RELEASE}") endif() +message("Use assembly routines ................. ${ASM}") message("\n") if(configure_warnings) message(" ******\n") diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 72b989682b06d..8a77ed465e110 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -6,6 +6,7 @@ configure_file(${CMAKE_SOURCE_DIR}/cmake/bitcoin-config.h.in config/bitcoin-conf add_compile_definitions(HAVE_CONFIG_H) include_directories(${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}) +add_subdirectory(crypto) add_subdirectory(univalue) diff --git a/src/crypto/CMakeLists.txt b/src/crypto/CMakeLists.txt new file mode 100644 index 0000000000000..6e98e522d0029 --- /dev/null +++ b/src/crypto/CMakeLists.txt @@ -0,0 +1,105 @@ +# Copyright (c) 2023 The Bitcoin Core developers +# Distributed under the MIT software license, see the accompanying +# file COPYING or http://www.opensource.org/licenses/mit-license.php. + +if(ASM AND NOT MSVC) + # Check for SSE4.1 intrinsics. + set(SSE41_CXXFLAGS -msse4.1) + check_cxx_source_compiles_with_flags("${SSE41_CXXFLAGS}" " + #include + + int main() + { + __m128i l = _mm_set1_epi32(0); + return _mm_extract_epi32(l, 3); + } + " HAVE_SSE41 + ) + + # Check for AVX2 intrinsics. + set(AVX2_CXXFLAGS -mavx -mavx2) + check_cxx_source_compiles_with_flags("${AVX2_CXXFLAGS}" " + #include + + int main() + { + __m256i l = _mm256_set1_epi32(0); + return _mm256_extract_epi32(l, 7); + } + " HAVE_AVX2 + ) + + # Check for x86 SHA-NI intrinsics. + set(X86_SHANI_CXXFLAGS -msse4 -msha) + check_cxx_source_compiles_with_flags("${X86_SHANI_CXXFLAGS}" " + #include + + int main() + { + __m128i i = _mm_set1_epi32(0); + __m128i j = _mm_set1_epi32(1); + __m128i k = _mm_set1_epi32(2); + return _mm_extract_epi32(_mm_sha256rnds2_epu32(i, j, k), 0); + } + " HAVE_X86_SHANI + ) + + # Check for ARMv8 SHA-NI intrinsics. + set(ARM_SHANI_CXXFLAGS -march=armv8-a+crypto) + check_cxx_source_compiles_with_flags("${ARM_SHANI_CXXFLAGS}" " + #include + + int main() + { + uint32x4_t a, b, c; + vsha256h2q_u32(a, b, c); + vsha256hq_u32(a, b, c); + vsha256su0q_u32(a, b); + vsha256su1q_u32(a, b, c); + } + " HAVE_ARM_SHANI + ) +endif() + +include(CheckCXXSymbolExists) +check_cxx_symbol_exists(timingsafe_bcmp "string.h" HAVE_TIMINGSAFE_BCMP) + +# The `bitcoin_crypto` must be of an `OBJECT` type to include +# its object files into the output static library archives. +add_library(bitcoin_crypto OBJECT EXCLUDE_FROM_ALL + $<$:sha256_sse4.cpp> + $<$:sha256_sse41.cpp> + $<$:sha256_avx2.cpp> + $<$:sha256_x86_shani.cpp> + $<$:sha256_arm_shani.cpp> + aes.cpp + chacha_poly_aead.cpp + chacha20.cpp + hkdf_sha256_32.cpp + hmac_sha256.cpp + hmac_sha512.cpp + poly1305.cpp + muhash.cpp + ripemd160.cpp + sha1.cpp + sha256.cpp + sha3.cpp + sha512.cpp + siphash.cpp +) +target_compile_definitions(bitcoin_crypto + PRIVATE + $<$:USE_ASM> + $<$:ENABLE_SSE41> + $<$:ENABLE_AVX2> + $<$:ENABLE_X86_SHANI> + $<$:ENABLE_ARM_SHANI> + $<$:HAVE_TIMINGSAFE_BCMP> +) +target_compile_options(bitcoin_crypto + PRIVATE + $<$:${SSE41_CXXFLAGS}> + $<$:${AVX2_CXXFLAGS}> + $<$:${X86_SHANI_CXXFLAGS}> + $<$:${ARM_SHANI_CXXFLAGS}> +)