Skip to content

Commit

Permalink
cmake: Build bitcoin_crypto library
Browse files Browse the repository at this point in the history
  • Loading branch information
hebasto committed Feb 21, 2023
1 parent dd0901f commit 138ca75
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/module)
# Configurable options.
# When adding a new option, end the <help_text> 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)
Expand Down Expand Up @@ -110,6 +111,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")
Expand Down
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
111 changes: 111 additions & 0 deletions src/crypto/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# 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)
string(REPLACE ";" " " CMAKE_REQUIRED_FLAGS "${SSE41_CXXFLAGS}")
check_cxx_source_compiles("
#include <immintrin.h>
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)
string(REPLACE ";" " " CMAKE_REQUIRED_FLAGS "${AVX2_CXXFLAGS}")
check_cxx_source_compiles("
#include <immintrin.h>
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)
string(REPLACE ";" " " CMAKE_REQUIRED_FLAGS "${X86_SHANI_CXXFLAGS}")
check_cxx_source_compiles("
#include <immintrin.h>
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, i, k), 0);
}
" HAVE_X86_SHANI
)

# Check for ARMv8 SHA-NI intrinsics.
set(ARM_SHANI_CXXFLAGS -march=armv8-a+crypto)
string(REPLACE ";" " " CMAKE_REQUIRED_FLAGS "${ARM_SHANI_CXXFLAGS}")
check_cxx_source_compiles("
#include <arm_neon.h>
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
)

set(CMAKE_REQUIRED_FLAGS)
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
$<$<BOOL:${ASM}>:sha256_sse4.cpp>
$<$<BOOL:${HAVE_SSE41}>:sha256_sse41.cpp>
$<$<BOOL:${HAVE_AVX2}>:sha256_avx2.cpp>
$<$<BOOL:${HAVE_X86_SHANI}>:sha256_x86_shani.cpp>
$<$<BOOL:${HAVE_ARM_SHANI}>: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
$<$<BOOL:${ASM}>:USE_ASM>
$<$<BOOL:${HAVE_SSE41}>:ENABLE_SSE41>
$<$<BOOL:${HAVE_AVX2}>:ENABLE_AVX2>
$<$<BOOL:${HAVE_X86_SHANI}>:ENABLE_X86_SHANI>
$<$<BOOL:${HAVE_ARM_SHANI}>:ENABLE_ARM_SHANI>
$<$<BOOL:${HAVE_TIMINGSAFE_BCMP}>:HAVE_TIMINGSAFE_BCMP>
)
target_compile_options(bitcoin_crypto
PRIVATE
$<$<BOOL:${HAVE_SSE41}>:${SSE41_CXXFLAGS}>
$<$<BOOL:${HAVE_AVX2}>:${AVX2_CXXFLAGS}>
$<$<BOOL:${HAVE_X86_SHANI}>:${X86_SHANI_CXXFLAGS}>
$<$<BOOL:${HAVE_ARM_SHANI}>:${ARM_SHANI_CXXFLAGS}>
)

0 comments on commit 138ca75

Please sign in to comment.