diff --git a/CMakeLists.txt b/CMakeLists.txt --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,6 +3,12 @@ cmake_minimum_required(VERSION 3.1) project(BitcoinABC) +# Add path for custom modules +set(CMAKE_MODULE_PATH + ${CMAKE_MODULE_PATH} + ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules +) + # If ccache is available, then use it. find_program(CCACHE_FOUND ccache) if(CCACHE_FOUND) diff --git a/cmake/modules/FindGMP.cmake b/cmake/modules/FindGMP.cmake new file mode 100644 --- /dev/null +++ b/cmake/modules/FindGMP.cmake @@ -0,0 +1,19 @@ +# Try to find the GMP librairies +# GMP_FOUND - system has GMP lib +# GMP_INCLUDE_DIR - the GMP include directory +# GMP_LIBRARIES - Libraries needed to use GMP + +if(GMP_INCLUDE_DIR AND GMP_LIBRARIES) + # Already in cache, be silent + set(GMP_FIND_QUIETLY TRUE) +endif() + +find_path(GMP_INCLUDE_DIR NAMES gmp.h) +find_library(GMP_LIBRARIES NAMES gmp libgmp) +find_library(GMPXX_LIBRARIES NAMES gmpxx libgmpxx) +MESSAGE(STATUS "GMP libs: " ${GMP_LIBRARIES} " " ${GMPXX_LIBRARIES}) + +include(FindPackageHandleStandardArgs) +FIND_PACKAGE_HANDLE_STANDARD_ARGS(GMP DEFAULT_MSG GMP_INCLUDE_DIR GMP_LIBRARIES) + +mark_as_advanced(GMP_INCLUDE_DIR GMP_LIBRARIES) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -2,4 +2,5 @@ add_subdirectory(config) add_subdirectory(crypto) +add_subdirectory(secp256k1) add_subdirectory(univalue) diff --git a/src/secp256k1/CMakeLists.txt b/src/secp256k1/CMakeLists.txt new file mode 100644 --- /dev/null +++ b/src/secp256k1/CMakeLists.txt @@ -0,0 +1,80 @@ +# Copyright (c) 2017 The Bitcoin developers + +cmake_minimum_required(VERSION 3.1) +project(secp256k1) + +option(SECP256K1_BUILD_TEST "Build secp256k1's unit tests" ON) + +# TODO: change this to include when possible +include_directories(. include) + +# The library +add_library(secp256k1 src/secp256k1.c) + +# We need to link in GMP +find_package(GMP) +if(GMP_INCLUDE_DIR AND GMP_LIBRARIES) + target_include_directories(secp256k1 PUBLIC ${GMP_INCLUDE_DIR}) + target_link_libraries(secp256k1 ${GMP_LIBRARIES}) + target_compile_definitions(secp256k1 + PUBLIC + HAVE_LIBGMP + USE_NUM_GMP + USE_FIELD_INV_NUM + USE_SCALAR_INV_BUILTIN + ) +else() + target_compile_definitions(secp256k1 + PUBLIC + USE_NUM_NONE + USE_FIELD_INV_BUILTIN + USE_SCALAR_INV_BUILTIN + ) +endif() + +# We make sure __int128 is defined +include(CheckTypeSize) +check_type_size(__int128 SIZEOF___INT128) +if(SIZEOF___INT128 EQUAL 16) + target_compile_definitions(secp256k1 PUBLIC HAVE___INT128) +else() + # If we do not support __int128, we should be falling back + # on 32bits implementations for field and scalar. +endif() + +# Detect if we are on a 32 or 64 bits plateform and chose +# scalar and filed implementation accordingly +if(CMAKE_SIZEOF_VOID_P EQUAL 8) + # 64 bits implementationr require either __int128 or asm support. + # TODO: support asm. + if(NOT SIZEOF___INT128 EQUAL 16) + message(SEND_ERROR "Compiler does not support __int128") + endif() + + target_compile_definitions(secp256k1 PUBLIC USE_SCALAR_4X64) + target_compile_definitions(secp256k1 PUBLIC USE_FIELD_5X52) +else() + target_compile_definitions(secp256k1 PUBLIC USE_SCALAR_8X32) + target_compile_definitions(secp256k1 PUBLIC USE_FIELD_10X26) +endif() + +if(SECP256K1_BUILD_TEST) + enable_testing() + + add_executable(secp256k1_tests src/tests.c) + add_test(NAME secp256k1_tests COMMAND secp256k1_tests) + target_link_libraries(secp256k1_tests secp256k1) + target_compile_definitions(secp256k1_tests PUBLIC VERIFY) + + add_executable(exhaustive_tests src/tests_exhaustive.c) + add_test(NAME exhaustive_tests COMMAND exhaustive_tests) + target_link_libraries(exhaustive_tests secp256k1) + target_compile_definitions(exhaustive_tests PUBLIC SECP256K1_BUILD) + # This should not be enabled at the same time as coverage is. + # TODO: support coverage. + target_compile_definitions(exhaustive_tests PUBLIC VERIFY) +endif(SECP256K1_BUILD_TEST) + +# TODO: emult static precomputation +# TODO: ecdh module +# TODO: RECOVERY module