From e07d46cdec258b91b181519ec4b8f4bc35088eef Mon Sep 17 00:00:00 2001 From: DesWurstes Date: Sun, 12 Aug 2018 11:16:04 +0300 Subject: [PATCH 01/12] Add CMake File --- CMakeLists.txt | 342 +++++++++++++++++++++++++++++++++++++++++++++ configure.ac | 2 +- src/CMakeLists.txt | 14 ++ 3 files changed, 357 insertions(+), 1 deletion(-) create mode 100644 CMakeLists.txt create mode 100644 src/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000000..91aa35fb01 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,342 @@ +cmake_minimum_required(VERSION 3.12) + +project(libsecp256k1 VERSION 0.1 LANGUAGES C) +set(CMAKE_C_STANDARD 90) +set(CMAKE_C_STANDARD_REQUIRED ON) + +if (CMAKE_C_FLAGS STREQUAL "") + set(CMAKE_C_FLAGS "-g") +endif() + +if (CMAKE_CROSSCOMPILING) + message(FATAL_ERROR "Currently Cmake makefile doesn't support cross compiling.") +endif() + +if (APPLE) + find_program ("brew" brew) + if (NOT brew STREQUAL "") + # These Homebrew packages may be keg-only, meaning that they won't be found + # in expected paths because they may conflict with system files. Ask + # Homebrew where each one is located, then adjust paths accordingly. + execute_process(COMMAND "${brew} --prefix openssl 2>/dev/null" OUTPUT_VARIABLE openssl_prefix) + execute_process(COMMAND "${brew} --prefix gmp 2>/dev/null" OUTPUT_VARIABLE gmp_prefix) + if (NOT openssl_prefix STREQUAL "") + set(ENV{PKG_CONFIG_PATH} "${openssl_prefix}/lib/pkgconfig:$PKG_CONFIG_PATH") + endif() + if (NOT gmp_prefix STREQUAL "") + set(ENV{PKG_CONFIG_PATH} "${gmp_prefix}/lib/pkgconfig:$PKG_CONFIG_PATH") + set(GMP_C_FLAGS "-I${$gmp_prefix}/include") + set(GMP_LIBS "-L${$gmp_prefix}/lib") + endif() + else() + find_program ("port" port) + # if homebrew isn't installed and macports is, add the macports default paths + # as a last resort. + if (NOT port STREQUAL "") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -isystem /opt/local/include") + set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -L/opt/local/lib") + endif() + endif() +endif() + +include(CheckCCompilerFlag) + +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -W") +set(WARN_C_FLAGS "-std=c89 -pedantic -Wall -Wextra -Wcast-align -Wnested-externs -Wshadow -Wstrict-prototypes -Wno-unused-function -Wno-long-long -Wno-overlength-strings") +set(SAVED_C_FLAGS "${CMAKE_C_FLAGS}") +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${WARN_C_FLAGS}") +check_c_compiler_flag(CMAKE_C_FLAGS USE_WARN_CFLAGS) +if (NOT USE_WARN_CFLAGS) + set(CMAKE_C_FLAGS "${SAVED_C_FLAGS}") +endif() + +set(SAVED_C_FLAGS "${CMAKE_C_FLAGS}") +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=hidden") +check_c_compiler_flag(CMAKE_C_FLAGS USE_OPAQUE_CFLAGS) +if (NOT USE_OPAQUE_CFLAGS) + set(CMAKE_C_FLAGS "${SAVED_C_FLAGS}") +endif() + +option(BENCHMARK "compile benchmark (default is on)" ON) +option(COVERAGE "enable compiler flags to support kcov coverage analysis" OFF) +option(TESTS "compile tests (default is on)" ON) +option(OPENSSL_TESTS "enable OpenSSL tests, if OpenSSL is available (default is auto)" OFF) +option(EXPERIMENTAL "allow experimental configure options (default is off)" OFF) +option(EXHAUSTIVE_TESTS "compile exhaustive tests (default is on)" ON) +option(ENDOMORPHISM "enable endomorphism (default is off)" OFF) +option(ECMULT_STATIC_PRECOMPUTATION "enable precomputed ecmult table for signing (default is on)" ON) +option(MODULE_ECDH "enable ECDH shared secret computation (experimental)" OFF) +option(MODULE_RECOVERY "enable ECDSA pubkey recovery module (default is off)" OFF) +option(JNI "enable libsecp256k1_jni (default is off)" OFF) + +option(FIELD_64BIT "enable 64 bit field implementation (default is auto)" OFF) +option(FIELD_32BIT "enable 32 bit field implementation (default is auto)" OFF) + +option(BIGNUM_GMP "enable GMP bignum implementation (default is auto)" OFF) +option(BIGNUM_NO "don't use any bignum implementation (default is auto)" OFF) + +option(SCALAR_64BIT "enable 64 bit scalar implementation (default is auto)" OFF) +option(SCALAR_32BIT "enable 32 bit scalar implementation (default is auto)" OFF) + +option(ASM_x86_64 "enable x86_64 assembly optimization (default is auto)" OFF) +option(ASM_ARM "enable ARM assembly optimization (default is auto)" OFF) +option(ASM_NO "don't use any assembly optimization (default is auto)" OFF) + +include(CheckTypeSize) +check_type_size("__int128" INT128) +if (NOT INT128 STREQUAL "") + set(INT128 ON) +else() + set(INT128 OFF) +endif() + +include(CheckCSourceCompiles) +check_c_source_compiles("int main() {__builtin_expect(0,0);return 0;}" BUILTIN_EXPECT) + +if (COVERAGE) + # Define this symbol to compile out all VERIFY code + add_compile_definitions(COVERAGE=1) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0 --coverage") + set(CMAKE_MODULE_LINKER_FLAGS "${LDFLAGS} --coverage") +else() + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3") +endif() + +# TODO: cross-compiler compatiblity +if (ECMULT_STATIC_PRECOMPUTATION) + set(PRECOMP ON) +else() + set(PRECOMP OFF) +endif() + +if (NOT ASM_NO) + check_c_source_compiles("int main() {__builtin_expect(0,0);return 0;}" BUILTIN_EXPECT) + if (NOT ASM_x86_64) + if (NOT ASM_ARM) + # TODO: this doesn't work + check_c_source_compiles("\ + #include \n\ + uint64_t a = 11, tmp; \n\ + int main(void) {__asm__ __volatile__(\"movq \\@S|@0x100000000,%1; mulq %%rsi\" : \"+a\"(a) : \"S\"(tmp) : \"cc\", \"%rdx\");return 0;}" 64BIT_ASM_CHECK) + if (64BIT_ASM_CHECK) + set(ASM_x86_64 ON) + else() + set(ASM_NO ON) + endif() + endif() + else() + check_c_source_compiles("\ + #include \n\ + uint64_t a = 11, tmp; \n\ + int main(void) {__asm__ __volatile__(\"movq \\@S|@0x100000000,%1; mulq %%rsi\" : \"+a\"(a) : \"S\"(tmp) : \"cc\", \"%rdx\");return 0;}" 64BIT_ASM_CHECK) + if (NOT 64BIT_ASM_CHECK) + message(FATAL_ERROR "x86_64 assembly optimization requested but not available.") + endif() + endif() +endif() + +if (NOT FIELD_64BIT AND NOT FIELD_32BIT) + if (ASM_x86_64) + set(FIELD_64BIT ON) + else() + if (INT128) + set(FIELD_64BIT ON) + else() + set(FIELD_32BIT ON) + endif() + endif() +else () + if (FIELD_64BIT) + if (NOT ASM_x86_64) + if (NOT INT128) + message(FATAL_ERROR "64bit field explicitly requested but neither __int128 support or x86_64 assembly available.") + endif() + endif() + else() + set(FIELD_32BIT ON) + endif() +endif() + +if (NOT SCALAR_64BIT AND NOT SCALAR_32BIT) + if (INT128) + set(SCALAR_64BIT ON) + else() + set(SCALAR_32BIT ON) + endif() +else() + if (SCALAR_64BIT) + if (NOT INT128) + message(FATAL_ERROR "64bit scalar explicitly requested but __int128 support not available.") + endif() + else() + set(SCALAR_32BIT ON) + endif() +endif() + +set(CPPFLAGS_TEMP "${CMAKE_CXX_FLAGS}") +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${GMP_C_FLAGS}") +set(LIBS "${CMAKE_MODULE_LINKER_FLAGS}") +set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${GMP_LIBS}") +# No need to append CMAKE_C_FLAGS to CMAKE_REQUIRED_FLAGS +set(CMAKE_REQUIRED_FLAGS "${GMP_C_FLAGS} ${GMP_LIBS}") + + +if (NOT BIGNUM_NO AND NOT BIGNUM_GMP) + check_c_source_compiles("\ + #include \ + mpz_t integ; int main() {return 0;};\ + " BIGNUM_GMP) + if (NOT BIGNUM_GMP) + set(BIGNUM_NO ON) + else() + set(BIGNUM_GMP ON) + # Define this symbol if libgmp is installed + add_compile_definitions(HAVE_LIBGMP=1) + endif() +else() + if (BIGNUM_GMP) + check_c_source_compiles("\ + #include \ + mpz_t integ; int main() {return 0;} \ + " BIGNUM_GMP) + if (NOT BIGNUM_GMP) + message(FATAL_ERROR "gmp bignum explicitly requested but libgmp not available.") + else() + set(BIGNUM_GMP ON) + # Define this symbol if libgmp is installed + add_compile_definitions(HAVE_LIBGMP=1) + endif() + else() + set(BIGNUM_NO ON) + endif() +endif() + +set(CMAKE_CXX_FLAGS "${CPPFLAGS_TEMP}") +set(CMAKE_MODULE_LINKER_FLAGS "${LIBS}") +set(CMAKE_REQUIRED_FLAGS "") + +set(USE_EXTERNAL_ASM OFF) +if (ASM_x86_64) + # Define this symbol to enable x86_64 assembly optimizations + add_compile_definitions(USE_ASM_X86_64=1) +else() + if(ASM_ARM) + set(USE_EXTERNAL_ASM ON) + endif() + # no asm +endif() + +if (FIELD_64BIT) + # Define this symbol to use the FIELD_5X52 implementation + add_compile_definitions(USE_FIELD_5X52=1) +else() + # Define this symbol to use the FIELD_10X26 implementation + add_compile_definitions(USE_FIELD_10X26=1) +endif() + +if (BIGNUM_GMP) + # Define this symbol if libgmp is installed + add_compile_definitions(HAVE_LIBGMP=1) + # Define this symbol to use the gmp implementation for num + add_compile_definitions(USE_NUM_GMP=1) + # Define this symbol to use the num-based field inverse implementation + add_compile_definitions(USE_FIELD_INV_NUM=1) + # Define this symbol to use the num-based scalar inverse implementation + add_compile_definitions(USE_SCALAR_INV_NUM=1) +else() + # Define this symbol to use no num implementation + add_compile_definitions(USE_NUM_NONE=1) + # Define this symbol to use the native field inverse implementation + add_compile_definitions(USE_FIELD_INV_BUILTIN=1) + # Define this symbol to use the native scalar inverse implementation + add_compile_definitions(USE_SCALAR_INV_BUILTIN=1) +endif() + +if (SCALAR_64BIT) + # Define this symbol to use the 4x64 scalar implementation + add_compile_definitions(USE_SCALAR_4X64=1) +else() + # Define this symbol to use the 8x32 scalar implementation + add_compile_definitions(USE_SCALAR_8X32=1) +endif() + +if (TESTS) + # TODO: here add OpenSSL tests + if (MINGW) + endif() +else() + if (OPENSSL_TESTS) + message(FATAL_ERROR "OpenSSL tests requested but tests are not enabled.") + endif() +endif() + +if (JNI) + # TODO: add jni +endif() + +if (BIGNUM_GMP) + set(SECP_LIBS "${SECP_LIBS} ${GMP_LIBS}") + set(SECP_INCLUDES "${SECP_INCLUDES} ${GMP_C_FLAGS}") +endif() + +if (ENDOMORPHISM) + # Define this symbol to use endomorphism optimization + add_compile_definitions(USE_ENDOMORPHISM=1) +endif() + +if (PRECOMP) + # Define this symbol to use a statically generated ecmult table + add_compile_definitions(USE_ECMULT_STATIC_PRECOMPUTATION=1) +endif() + +if (MODULE_ECDH) + # Define this symbol to enable the ECDH module + add_compile_definitions(ENABLE_MODULE_ECDH=1) +endif() + +if (MODULE_RECOVERY) + # Define this symbol to enable the ECDSA pubkey recovery module + add_compile_definitions(ENABLE_MODULE_RECOVERY=1) +endif() + +if (USE_EXTERNAL_ASM) + # Define this symbol if an external (non-inline) assembly implementation is used + add_compile_definitions(USE_EXTERNAL_ASM=1) +endif() + +if (INT128) + add_compile_definitions(HAVE___INT128=1) +endif() + +message("Using static precomputation: ${PRECOMP}") +message("Using x86_64 ASM: ${ASM_x86_64}") +message("Using ARM ASM: ${ASM_ARM}") +message("Using external ASM: ${USE_EXTERNAL_ASM}") +message("Using 64 bit field implementation: ${FIELD_64BIT}") +message("Using GMP bignum implementation: ${BIGNUM_GMP}") +message("Using 64 bit scalar implementation: ${SCALAR_64BIT}") +message("Using endomorphism optimizations: ${ENDOMORPHISM}") +message("Building benchmarks: ${BENCHMARK}") +message("Building for coverage analysis: ${COVERAGE}") +message("Building ECDH module: ${MODULE_ECDH}") +message("Building ECDSA pubkey recovery module: ${MODULE_RECOVERY}") +message("Using JNI: ${JNI}") + +if (EXPERIMENTAL) + message("******") + message("WARNING: experimental build") + message("Experimental features do not have stable APIs or properties, and may not be safe for production use.") + message("Building ECDH module: ${MODULE_ECDH}.") + message("******") +else() + if (MODULE_ECDH) + message(FATAL_ERROR "ECDH module is experimental. Use -DEXPERIMENTAL to allow.") + endif() + if (ASM_ARM) + message(FATAL_ERROR "ARM assembly optimization is experimental. Use --enable-experimental to allow.") + endif() +endif() + +add_subdirectory(src) + +# What should be here? diff --git a/configure.ac b/configure.ac index 68c45a56f0..167ff18338 100644 --- a/configure.ac +++ b/configure.ac @@ -162,7 +162,7 @@ AC_COMPILE_IFELSE([AC_LANG_SOURCE([[void myfunc() {__builtin_expect(0,0);}]])], if test x"$enable_coverage" = x"yes"; then AC_DEFINE(COVERAGE, 1, [Define this symbol to compile out all VERIFY code]) CFLAGS="$CFLAGS -O0 --coverage" - LDFLAGS="--coverage" + LDFLAGS="$LDFLAGS --coverage" else CFLAGS="$CFLAGS -O3" fi diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000000..27754707a4 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,14 @@ +include_directories(${PROJECT_SOURCE_DIR}) +include_directories(${PROJECT_SOURCE_DIR}/include) + +if (PRECOMP) + add_custom_target(precomputation gen_context.c) + execute_process(COMMAND "${PROJECT_SOURCE_DIR}/src/precomputation" WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}") +endif() +add_library(libsecp256k1 STATIC secp256k1.c) +if (TESTS) + add_custom_target(test tests.c) +endif() +if (EXHAUSTIVE_TESTS) + add_custom_target(exhaustive_test tests.c) +endif() From c766e027076dc925bfa413b99261a496851e596e Mon Sep 17 00:00:00 2001 From: DesWurstes Date: Tue, 4 Sep 2018 14:27:30 +0300 Subject: [PATCH 02/12] Lots of improvements --- CMakeLists.txt | 359 ++++++++++++++++++++------------------------- src/CMakeLists.txt | 26 +++- src/gen_context.c | 7 +- 3 files changed, 178 insertions(+), 214 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 91aa35fb01..34f291eadd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,38 +5,38 @@ set(CMAKE_C_STANDARD 90) set(CMAKE_C_STANDARD_REQUIRED ON) if (CMAKE_C_FLAGS STREQUAL "") - set(CMAKE_C_FLAGS "-g") + set(CMAKE_C_FLAGS "-g") endif() if (CMAKE_CROSSCOMPILING) - message(FATAL_ERROR "Currently Cmake makefile doesn't support cross compiling.") + message(FATAL_ERROR "Currently Cmake makefile doesn't support cross compiling.") endif() -if (APPLE) - find_program ("brew" brew) - if (NOT brew STREQUAL "") - # These Homebrew packages may be keg-only, meaning that they won't be found - # in expected paths because they may conflict with system files. Ask - # Homebrew where each one is located, then adjust paths accordingly. - execute_process(COMMAND "${brew} --prefix openssl 2>/dev/null" OUTPUT_VARIABLE openssl_prefix) - execute_process(COMMAND "${brew} --prefix gmp 2>/dev/null" OUTPUT_VARIABLE gmp_prefix) - if (NOT openssl_prefix STREQUAL "") - set(ENV{PKG_CONFIG_PATH} "${openssl_prefix}/lib/pkgconfig:$PKG_CONFIG_PATH") - endif() - if (NOT gmp_prefix STREQUAL "") - set(ENV{PKG_CONFIG_PATH} "${gmp_prefix}/lib/pkgconfig:$PKG_CONFIG_PATH") - set(GMP_C_FLAGS "-I${$gmp_prefix}/include") - set(GMP_LIBS "-L${$gmp_prefix}/lib") - endif() - else() - find_program ("port" port) - # if homebrew isn't installed and macports is, add the macports default paths - # as a last resort. - if (NOT port STREQUAL "") - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -isystem /opt/local/include") - set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -L/opt/local/lib") - endif() +if (APPLE AND NOT BIGNUM_NO) + find_program("brew" brew) + if (NOT brew STREQUAL "") + # These Homebrew packages may be keg-only, meaning that they won't be found + # in expected paths because they may conflict with system files. Ask + # Homebrew where each one is located, then adjust paths accordingly. + execute_process(COMMAND ${brew} --prefix openssl OUTPUT_VARIABLE openssl_prefix OUTPUT_STRIP_TRAILING_WHITESPACE) + execute_process(COMMAND ${brew} --prefix gmp OUTPUT_VARIABLE gmp_prefix OUTPUT_STRIP_TRAILING_WHITESPACE) + if (NOT openssl_prefix STREQUAL "") + set(ENV{PKG_CONFIG_PATH} "${openssl_prefix}/lib/pkgconfig:$PKG_CONFIG_PATH") endif() + if (NOT gmp_prefix STREQUAL "") + set(ENV{PKG_CONFIG_PATH} "${gmp_prefix}/lib/pkgconfig:$PKG_CONFIG_PATH") + set(GMP_C_FLAGS "-I${gmp_prefix}/include") + set(GMP_LIBS "-L${gmp_prefix}/lib -lgmp") + endif() + else() + find_program("port" port) + # if homebrew isn't installed and macports is, add the macports default paths + # as a last resort. + if (NOT port STREQUAL "") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -isystem /opt/local/include") + set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -L/opt/local/lib") + endif() + endif() endif() include(CheckCCompilerFlag) @@ -45,16 +45,16 @@ set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -W") set(WARN_C_FLAGS "-std=c89 -pedantic -Wall -Wextra -Wcast-align -Wnested-externs -Wshadow -Wstrict-prototypes -Wno-unused-function -Wno-long-long -Wno-overlength-strings") set(SAVED_C_FLAGS "${CMAKE_C_FLAGS}") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${WARN_C_FLAGS}") -check_c_compiler_flag(CMAKE_C_FLAGS USE_WARN_CFLAGS) +check_c_compiler_flag(${CMAKE_C_FLAGS} USE_WARN_CFLAGS) if (NOT USE_WARN_CFLAGS) - set(CMAKE_C_FLAGS "${SAVED_C_FLAGS}") + set(CMAKE_C_FLAGS "${SAVED_C_FLAGS}") endif() set(SAVED_C_FLAGS "${CMAKE_C_FLAGS}") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=hidden") -check_c_compiler_flag(CMAKE_C_FLAGS USE_OPAQUE_CFLAGS) +check_c_compiler_flag(${CMAKE_C_FLAGS} USE_OPAQUE_CFLAGS) if (NOT USE_OPAQUE_CFLAGS) - set(CMAKE_C_FLAGS "${SAVED_C_FLAGS}") + set(CMAKE_C_FLAGS "${SAVED_C_FLAGS}") endif() option(BENCHMARK "compile benchmark (default is on)" ON) @@ -85,229 +85,177 @@ option(ASM_NO "don't use any assembly optimization (default is auto)" OFF) include(CheckTypeSize) check_type_size("__int128" INT128) if (NOT INT128 STREQUAL "") - set(INT128 ON) + set(INT128 ON) else() - set(INT128 OFF) + set(INT128 OFF) endif() include(CheckCSourceCompiles) check_c_source_compiles("int main() {__builtin_expect(0,0);return 0;}" BUILTIN_EXPECT) if (COVERAGE) - # Define this symbol to compile out all VERIFY code - add_compile_definitions(COVERAGE=1) - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0 --coverage") - set(CMAKE_MODULE_LINKER_FLAGS "${LDFLAGS} --coverage") + # Define this symbol to compile out all VERIFY code + add_compile_definitions(COVERAGE=1) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0 --coverage") + set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} --coverage") + set(CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} --coverage") + set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} --coverage") else() - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3") endif() -# TODO: cross-compiler compatiblity -if (ECMULT_STATIC_PRECOMPUTATION) - set(PRECOMP ON) -else() - set(PRECOMP OFF) +set(PRECOMP ${ECMULT_STATIC_PRECOMPUTATION}) + +if (NOT ASM_ARM) + check_c_source_compiles("\ + #include \n\ + uint64_t a = 11, tmp; \n\ + int main(void) {__asm__ __volatile__(\"movq 0x100000000,%1; mulq %%rsi\" : \"+a\"(a) : \"S\"(tmp) : \"cc\", \"%rdx\");return 0;}" ASM_64BIT) + if (ASM_64BIT) + set(ASM_x86_64 ON) + elseif(ASM_x86_64) + message(FATAL_ERROR "x86_64 assembly optimization requested but not available.") + # else ASM_NO + endif() endif() -if (NOT ASM_NO) - check_c_source_compiles("int main() {__builtin_expect(0,0);return 0;}" BUILTIN_EXPECT) - if (NOT ASM_x86_64) - if (NOT ASM_ARM) - # TODO: this doesn't work - check_c_source_compiles("\ - #include \n\ - uint64_t a = 11, tmp; \n\ - int main(void) {__asm__ __volatile__(\"movq \\@S|@0x100000000,%1; mulq %%rsi\" : \"+a\"(a) : \"S\"(tmp) : \"cc\", \"%rdx\");return 0;}" 64BIT_ASM_CHECK) - if (64BIT_ASM_CHECK) - set(ASM_x86_64 ON) - else() - set(ASM_NO ON) - endif() - endif() - else() - check_c_source_compiles("\ - #include \n\ - uint64_t a = 11, tmp; \n\ - int main(void) {__asm__ __volatile__(\"movq \\@S|@0x100000000,%1; mulq %%rsi\" : \"+a\"(a) : \"S\"(tmp) : \"cc\", \"%rdx\");return 0;}" 64BIT_ASM_CHECK) - if (NOT 64BIT_ASM_CHECK) - message(FATAL_ERROR "x86_64 assembly optimization requested but not available.") - endif() - endif() +# Assumes the user didn't choose both +if (NOT FIELD_32BIT) + if (ASM_x86_64 OR INT128) + set(FIELD_64BIT ON) + elseif(FIELD_64BIT) + message(FATAL_ERROR "64bit field explicitly requested but neither __int128 support or x86_64 assembly available.") + endif() endif() -if (NOT FIELD_64BIT AND NOT FIELD_32BIT) - if (ASM_x86_64) - set(FIELD_64BIT ON) - else() - if (INT128) - set(FIELD_64BIT ON) - else() - set(FIELD_32BIT ON) - endif() - endif() -else () - if (FIELD_64BIT) - if (NOT ASM_x86_64) - if (NOT INT128) - message(FATAL_ERROR "64bit field explicitly requested but neither __int128 support or x86_64 assembly available.") - endif() - endif() - else() - set(FIELD_32BIT ON) - endif() +# Assumes the user didn't choose both +if (NOT SCALAR_32BIT) + if (INT128) + set(SCALAR_64BIT ON) + elseif(SCALAR_64BIT) + message(FATAL_ERROR "64bit scalar explicitly requested but __int128 support not available.") + # else use SCALAR_32BIT + endif() endif() -if (NOT SCALAR_64BIT AND NOT SCALAR_32BIT) - if (INT128) - set(SCALAR_64BIT ON) - else() - set(SCALAR_32BIT ON) - endif() -else() - if (SCALAR_64BIT) - if (NOT INT128) - message(FATAL_ERROR "64bit scalar explicitly requested but __int128 support not available.") - endif() - else() - set(SCALAR_32BIT ON) - endif() -endif() - -set(CPPFLAGS_TEMP "${CMAKE_CXX_FLAGS}") -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${GMP_C_FLAGS}") -set(LIBS "${CMAKE_MODULE_LINKER_FLAGS}") -set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${GMP_LIBS}") -# No need to append CMAKE_C_FLAGS to CMAKE_REQUIRED_FLAGS -set(CMAKE_REQUIRED_FLAGS "${GMP_C_FLAGS} ${GMP_LIBS}") - - -if (NOT BIGNUM_NO AND NOT BIGNUM_GMP) - check_c_source_compiles("\ - #include \ - mpz_t integ; int main() {return 0;};\ - " BIGNUM_GMP) - if (NOT BIGNUM_GMP) - set(BIGNUM_NO ON) - else() - set(BIGNUM_GMP ON) - # Define this symbol if libgmp is installed - add_compile_definitions(HAVE_LIBGMP=1) - endif() -else() - if (BIGNUM_GMP) - check_c_source_compiles("\ - #include \ - mpz_t integ; int main() {return 0;} \ - " BIGNUM_GMP) - if (NOT BIGNUM_GMP) - message(FATAL_ERROR "gmp bignum explicitly requested but libgmp not available.") - else() - set(BIGNUM_GMP ON) - # Define this symbol if libgmp is installed - add_compile_definitions(HAVE_LIBGMP=1) - endif() - else() - set(BIGNUM_NO ON) - endif() +if (NOT BIGNUM_NO) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${GMP_C_FLAGS} ${GMP_LIBS}") + set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${GMP_LIBS}") + # No need to append CMAKE_C_FLAGS to CMAKE_REQUIRED_FLAGS + check_c_source_compiles("\ + #include \n\ + mpz_t integ; int main() {return 0;}" GMP) + if (GMP) + # Define this symbol if libgmp is installed + add_compile_definitions(HAVE_LIBGMP=1) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${GMP_C_FLAGS} ${GMP_LIBS}") + set(BIGNUM_GMP ON) + elseif(BIGNUM_GMP) + message(FATAL_ERROR "gmp bignum explicitly requested but libgmp not available.") + # else BIGNUM_NO + endif() endif() -set(CMAKE_CXX_FLAGS "${CPPFLAGS_TEMP}") -set(CMAKE_MODULE_LINKER_FLAGS "${LIBS}") set(CMAKE_REQUIRED_FLAGS "") set(USE_EXTERNAL_ASM OFF) if (ASM_x86_64) - # Define this symbol to enable x86_64 assembly optimizations - add_compile_definitions(USE_ASM_X86_64=1) + # Define this symbol to enable x86_64 assembly optimizations + add_compile_definitions(USE_ASM_X86_64=1) else() - if(ASM_ARM) - set(USE_EXTERNAL_ASM ON) - endif() - # no asm + if(ASM_ARM) + set(USE_EXTERNAL_ASM ON) + endif() + # no asm +endif() + +if(BUILTIN_EXPECT) + add_compile_definitions(HAVE_BUILTIN_EXPECT) +endif() + +if ((TESTS OR EXHAUSTIVE_TESTS) AND NOT COVERAGE) + add_compile_definitions(VERIFY) endif() if (FIELD_64BIT) - # Define this symbol to use the FIELD_5X52 implementation - add_compile_definitions(USE_FIELD_5X52=1) + # Define this symbol to use the FIELD_5X52 implementation + add_compile_definitions(USE_FIELD_5X52=1) else() - # Define this symbol to use the FIELD_10X26 implementation - add_compile_definitions(USE_FIELD_10X26=1) + # Define this symbol to use the FIELD_10X26 implementation + add_compile_definitions(USE_FIELD_10X26=1) endif() if (BIGNUM_GMP) - # Define this symbol if libgmp is installed - add_compile_definitions(HAVE_LIBGMP=1) - # Define this symbol to use the gmp implementation for num - add_compile_definitions(USE_NUM_GMP=1) - # Define this symbol to use the num-based field inverse implementation - add_compile_definitions(USE_FIELD_INV_NUM=1) - # Define this symbol to use the num-based scalar inverse implementation - add_compile_definitions(USE_SCALAR_INV_NUM=1) + # Define this symbol if libgmp is installed + add_compile_definitions(HAVE_LIBGMP=1) + # Define this symbol to use the gmp implementation for num + add_compile_definitions(USE_NUM_GMP=1) + # Define this symbol to use the num-based field inverse implementation + add_compile_definitions(USE_FIELD_INV_NUM=1) + # Define this symbol to use the num-based scalar inverse implementation + add_compile_definitions(USE_SCALAR_INV_NUM=1) else() - # Define this symbol to use no num implementation - add_compile_definitions(USE_NUM_NONE=1) - # Define this symbol to use the native field inverse implementation - add_compile_definitions(USE_FIELD_INV_BUILTIN=1) - # Define this symbol to use the native scalar inverse implementation - add_compile_definitions(USE_SCALAR_INV_BUILTIN=1) + # Define this symbol to use no num implementation + add_compile_definitions(USE_NUM_NONE=1) + # Define this symbol to use the native field inverse implementation + add_compile_definitions(USE_FIELD_INV_BUILTIN=1) + # Define this symbol to use the native scalar inverse implementation + add_compile_definitions(USE_SCALAR_INV_BUILTIN=1) endif() if (SCALAR_64BIT) - # Define this symbol to use the 4x64 scalar implementation - add_compile_definitions(USE_SCALAR_4X64=1) + # Define this symbol to use the 4x64 scalar implementation + add_compile_definitions(USE_SCALAR_4X64=1) else() - # Define this symbol to use the 8x32 scalar implementation - add_compile_definitions(USE_SCALAR_8X32=1) + # Define this symbol to use the 8x32 scalar implementation + add_compile_definitions(USE_SCALAR_8X32=1) endif() if (TESTS) - # TODO: here add OpenSSL tests - if (MINGW) - endif() + # TODO: here add OpenSSL tests + if (MINGW) + endif() else() - if (OPENSSL_TESTS) - message(FATAL_ERROR "OpenSSL tests requested but tests are not enabled.") - endif() + if (OPENSSL_TESTS) + message(FATAL_ERROR "OpenSSL tests requested but tests are not enabled.") + endif() endif() if (JNI) - # TODO: add jni -endif() - -if (BIGNUM_GMP) - set(SECP_LIBS "${SECP_LIBS} ${GMP_LIBS}") - set(SECP_INCLUDES "${SECP_INCLUDES} ${GMP_C_FLAGS}") + # TODO: add jni endif() if (ENDOMORPHISM) - # Define this symbol to use endomorphism optimization - add_compile_definitions(USE_ENDOMORPHISM=1) + # Define this symbol to use endomorphism optimization + add_compile_definitions(USE_ENDOMORPHISM=1) endif() if (PRECOMP) - # Define this symbol to use a statically generated ecmult table - add_compile_definitions(USE_ECMULT_STATIC_PRECOMPUTATION=1) + # Define this symbol to use a statically generated ecmult table + add_compile_definitions(USE_ECMULT_STATIC_PRECOMPUTATION=1) endif() if (MODULE_ECDH) - # Define this symbol to enable the ECDH module - add_compile_definitions(ENABLE_MODULE_ECDH=1) + # Define this symbol to enable the ECDH module + add_compile_definitions(ENABLE_MODULE_ECDH=1) endif() if (MODULE_RECOVERY) - # Define this symbol to enable the ECDSA pubkey recovery module - add_compile_definitions(ENABLE_MODULE_RECOVERY=1) + # Define this symbol to enable the ECDSA pubkey recovery module + add_compile_definitions(ENABLE_MODULE_RECOVERY=1) endif() if (USE_EXTERNAL_ASM) - # Define this symbol if an external (non-inline) assembly implementation is used - add_compile_definitions(USE_EXTERNAL_ASM=1) + # Define this symbol if an external (non-inline) assembly implementation is used + add_compile_definitions(USE_EXTERNAL_ASM=1) endif() if (INT128) - add_compile_definitions(HAVE___INT128=1) + add_compile_definitions(HAVE___INT128=1) endif() +add_compile_definitions(SECP256K1_BUILD) + message("Using static precomputation: ${PRECOMP}") message("Using x86_64 ASM: ${ASM_x86_64}") message("Using ARM ASM: ${ASM_ARM}") @@ -323,20 +271,25 @@ message("Building ECDSA pubkey recovery module: ${MODULE_RECOVERY}") message("Using JNI: ${JNI}") if (EXPERIMENTAL) - message("******") - message("WARNING: experimental build") - message("Experimental features do not have stable APIs or properties, and may not be safe for production use.") - message("Building ECDH module: ${MODULE_ECDH}.") - message("******") + message("******") + message("WARNING: experimental build") + message("Experimental features do not have stable APIs or properties, and may not be safe for production use.") + message("Building ECDH module: ${MODULE_ECDH}.") + message("******") else() - if (MODULE_ECDH) - message(FATAL_ERROR "ECDH module is experimental. Use -DEXPERIMENTAL to allow.") - endif() - if (ASM_ARM) - message(FATAL_ERROR "ARM assembly optimization is experimental. Use --enable-experimental to allow.") - endif() + if (MODULE_ECDH) + message(FATAL_ERROR "ECDH module is experimental. Use -DEXPERIMENTAL to allow.") + endif() + if (ASM_ARM) + message(FATAL_ERROR "ARM assembly optimization is experimental. Use -DEXPERIMENTAL to allow.") + endif() endif() +# Use -DBUILD_SHARED_LIBS=ON to generate SHARED library + +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}) + add_subdirectory(src) -# What should be here? +set_target_properties(secp256k1 PROPERTIES PUBLIC_HEADER include/secp256k1.h) +set_target_properties(secp256k1 PROPERTIES OUTPUT_NAME secp256k1) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 27754707a4..1c010b51e7 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,14 +1,24 @@ include_directories(${PROJECT_SOURCE_DIR}) -include_directories(${PROJECT_SOURCE_DIR}/include) -if (PRECOMP) - add_custom_target(precomputation gen_context.c) - execute_process(COMMAND "${PROJECT_SOURCE_DIR}/src/precomputation" WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}") -endif() -add_library(libsecp256k1 STATIC secp256k1.c) +add_library(secp256k1 secp256k1.c) if (TESTS) - add_custom_target(test tests.c) + add_executable(test tests.c) endif() if (EXHAUSTIVE_TESTS) - add_custom_target(exhaustive_test tests.c) + add_executable(exhaustive_test tests_exhaustive.c) +endif() + +if (PRECOMP) + add_executable(precomputation gen_context.c) + set_target_properties(precomputation PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}/src/") + # alternatives considered: + # add_file_dependencies, add_custom_target, add_custom_command + add_dependencies(secp256k1 precomputation) + if (TESTS) + add_dependencies(test precomputation) + endif() + if (EXHAUSTIVE_TESTS) + add_dependencies(exhaustive_test precomputation) + endif() + execute_process(COMMAND "${PROJECT_SOURCE_DIR}/src/precomputation" WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}) endif() diff --git a/src/gen_context.c b/src/gen_context.c index 87d296ebf0..a280db26cf 100644 --- a/src/gen_context.c +++ b/src/gen_context.c @@ -5,6 +5,7 @@ **********************************************************************/ #define USE_BASIC_CONFIG 1 +#undef USE_ECMULT_STATIC_PRECOMPUTATION #include "basic-config.h" #include "include/secp256k1.h" @@ -38,7 +39,7 @@ int main(int argc, char **argv) { fprintf(stderr, "Could not open src/ecmult_static_context.h for writing!\n"); return -1; } - + fprintf(fp, "#ifndef _SECP256K1_ECMULT_STATIC_CONTEXT_\n"); fprintf(fp, "#define _SECP256K1_ECMULT_STATIC_CONTEXT_\n"); fprintf(fp, "#include \"src/group.h\"\n"); @@ -65,10 +66,10 @@ int main(int argc, char **argv) { } fprintf(fp,"};\n"); secp256k1_ecmult_gen_context_clear(&ctx); - + fprintf(fp, "#undef SC\n"); fprintf(fp, "#endif\n"); fclose(fp); - + return 0; } From e4e889be8578cfacafc9f9935466009ebeafefc7 Mon Sep 17 00:00:00 2001 From: DesWurstes Date: Wed, 5 Sep 2018 15:05:41 +0300 Subject: [PATCH 03/12] Fix more bugs --- CMakeLists.txt | 21 ++++++++++++++------- src/CMakeLists.txt | 17 +++++++++++------ 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 34f291eadd..e89b5854ba 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,7 +13,7 @@ if (CMAKE_CROSSCOMPILING) endif() if (APPLE AND NOT BIGNUM_NO) - find_program("brew" brew) + find_program(brew "brew") if (NOT brew STREQUAL "") # These Homebrew packages may be keg-only, meaning that they won't be found # in expected paths because they may conflict with system files. Ask @@ -29,7 +29,7 @@ if (APPLE AND NOT BIGNUM_NO) set(GMP_LIBS "-L${gmp_prefix}/lib -lgmp") endif() else() - find_program("port" port) + find_program(port "port") # if homebrew isn't installed and macports is, add the macports default paths # as a last resort. if (NOT port STREQUAL "") @@ -101,7 +101,16 @@ if (COVERAGE) set(CMAKE_STATIC_LINKER_FLAGS "${CMAKE_STATIC_LINKER_FLAGS} --coverage") set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} --coverage") else() - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3") + check_c_compiler_flag("-O3" OPTIMIZE_O3) + if (OPTIMIZE_O3) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3") + else() + # MSVC + check_c_compiler_flag("/Ox" OPTIMIZE_OX) + if (OPTIMIZE_OX) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Ox") + endif() + endif() endif() set(PRECOMP ${ECMULT_STATIC_PRECOMPUTATION}) @@ -110,7 +119,7 @@ if (NOT ASM_ARM) check_c_source_compiles("\ #include \n\ uint64_t a = 11, tmp; \n\ - int main(void) {__asm__ __volatile__(\"movq 0x100000000,%1; mulq %%rsi\" : \"+a\"(a) : \"S\"(tmp) : \"cc\", \"%rdx\");return 0;}" ASM_64BIT) + int main(void) {__asm__ __volatile__(\"movq $0x100000000,%1; mulq %%rsi\" : \"+a\"(a) : \"S\"(tmp) : \"cc\", \"%rdx\");return 0;}" ASM_64BIT) if (ASM_64BIT) set(ASM_x86_64 ON) elseif(ASM_x86_64) @@ -163,9 +172,7 @@ if (ASM_x86_64) # Define this symbol to enable x86_64 assembly optimizations add_compile_definitions(USE_ASM_X86_64=1) else() - if(ASM_ARM) - set(USE_EXTERNAL_ASM ON) - endif() + set(USE_EXTERNAL_ASM ${ASM_ARM}) # no asm endif() diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 1c010b51e7..1f500a09de 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,4 +1,5 @@ include_directories(${PROJECT_SOURCE_DIR}) +include_directories("${PROJECT_SOURCE_DIR}/include") add_library(secp256k1 secp256k1.c) if (TESTS) @@ -10,15 +11,19 @@ endif() if (PRECOMP) add_executable(precomputation gen_context.c) - set_target_properties(precomputation PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}/src/") # alternatives considered: - # add_file_dependencies, add_custom_target, add_custom_command - add_dependencies(secp256k1 precomputation) + # add_file_dependencies, add_dependencies, add_custom_command + add_custom_target(precomputed_table + COMMAND "${PROJECT_SOURCE_DIR}/precomputation" + BYPRODUCTS "${PROJECT_SOURCE_DIR}/src/ecmult_static_context.h" + DEPENDS precomputation + WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} + ) + add_dependencies(secp256k1 precomputed_table) if (TESTS) - add_dependencies(test precomputation) + add_dependencies(test precomputed_table) endif() if (EXHAUSTIVE_TESTS) - add_dependencies(exhaustive_test precomputation) + add_dependencies(exhaustive_test precomputed_table) endif() - execute_process(COMMAND "${PROJECT_SOURCE_DIR}/src/precomputation" WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}) endif() From 59deef2092a671ed52ab9debe72e7ae0203fdedc Mon Sep 17 00:00:00 2001 From: DesWurstes Date: Wed, 19 Sep 2018 18:22:45 +0300 Subject: [PATCH 04/12] More improvements --- CMakeLists.txt | 49 +++++++++++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e89b5854ba..7264237b17 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,43 +4,38 @@ project(libsecp256k1 VERSION 0.1 LANGUAGES C) set(CMAKE_C_STANDARD 90) set(CMAKE_C_STANDARD_REQUIRED ON) -if (CMAKE_C_FLAGS STREQUAL "") - set(CMAKE_C_FLAGS "-g") -endif() +include(CheckCCompilerFlag) -if (CMAKE_CROSSCOMPILING) - message(FATAL_ERROR "Currently Cmake makefile doesn't support cross compiling.") +if (CMAKE_C_FLAGS STREQUAL "") + check_c_compiler_flag("-g" DEBUG_OPTION) + if (DEBUG_OPTION) + set(CMAKE_C_FLAGS "-g") + endif() endif() if (APPLE AND NOT BIGNUM_NO) find_program(brew "brew") - if (NOT brew STREQUAL "") + if (brew STREQUAL "") + find_program(port "port") + # if homebrew isn't installed and macports is, add the macports default paths + # as a last resort. + if (NOT port STREQUAL "") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -isystem /opt/local/include") + set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -L/opt/local/lib") + endif() + else() # These Homebrew packages may be keg-only, meaning that they won't be found # in expected paths because they may conflict with system files. Ask # Homebrew where each one is located, then adjust paths accordingly. execute_process(COMMAND ${brew} --prefix openssl OUTPUT_VARIABLE openssl_prefix OUTPUT_STRIP_TRAILING_WHITESPACE) execute_process(COMMAND ${brew} --prefix gmp OUTPUT_VARIABLE gmp_prefix OUTPUT_STRIP_TRAILING_WHITESPACE) - if (NOT openssl_prefix STREQUAL "") - set(ENV{PKG_CONFIG_PATH} "${openssl_prefix}/lib/pkgconfig:$PKG_CONFIG_PATH") - endif() if (NOT gmp_prefix STREQUAL "") - set(ENV{PKG_CONFIG_PATH} "${gmp_prefix}/lib/pkgconfig:$PKG_CONFIG_PATH") set(GMP_C_FLAGS "-I${gmp_prefix}/include") set(GMP_LIBS "-L${gmp_prefix}/lib -lgmp") endif() - else() - find_program(port "port") - # if homebrew isn't installed and macports is, add the macports default paths - # as a last resort. - if (NOT port STREQUAL "") - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -isystem /opt/local/include") - set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -L/opt/local/lib") - endif() endif() endif() -include(CheckCCompilerFlag) - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -W") set(WARN_C_FLAGS "-std=c89 -pedantic -Wall -Wextra -Wcast-align -Wnested-externs -Wshadow -Wstrict-prototypes -Wno-unused-function -Wno-long-long -Wno-overlength-strings") set(SAVED_C_FLAGS "${CMAKE_C_FLAGS}") @@ -84,10 +79,10 @@ option(ASM_NO "don't use any assembly optimization (default is auto)" OFF) include(CheckTypeSize) check_type_size("__int128" INT128) -if (NOT INT128 STREQUAL "") - set(INT128 ON) -else() +if (INT128 STREQUAL "") set(INT128 OFF) +else() + set(INT128 ON) endif() include(CheckCSourceCompiles) @@ -261,6 +256,12 @@ if (INT128) add_compile_definitions(HAVE___INT128=1) endif() +include(TestBigEndian) +test_big_endian(BIG_ENDIAN) +if (BIG_ENDIAN) + add_compile_definitions(WORDS_BIGENDIAN=1) +endif() + add_compile_definitions(SECP256K1_BUILD) message("Using static precomputation: ${PRECOMP}") @@ -268,8 +269,8 @@ message("Using x86_64 ASM: ${ASM_x86_64}") message("Using ARM ASM: ${ASM_ARM}") message("Using external ASM: ${USE_EXTERNAL_ASM}") message("Using 64 bit field implementation: ${FIELD_64BIT}") -message("Using GMP bignum implementation: ${BIGNUM_GMP}") message("Using 64 bit scalar implementation: ${SCALAR_64BIT}") +message("Using GMP bignum implementation: ${BIGNUM_GMP}") message("Using endomorphism optimizations: ${ENDOMORPHISM}") message("Building benchmarks: ${BENCHMARK}") message("Building for coverage analysis: ${COVERAGE}") From 718c7fbef6f20e933990ccb39aef9f0808118a97 Mon Sep 17 00:00:00 2001 From: DesWurstes Date: Wed, 19 Sep 2018 18:22:45 +0300 Subject: [PATCH 05/12] More improvements --- CMakeLists.txt | 74 ++++++++++++++++++++++++---------------------- src/CMakeLists.txt | 36 +++++++++++----------- 2 files changed, 57 insertions(+), 53 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e89b5854ba..4012af5e1b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,57 +4,55 @@ project(libsecp256k1 VERSION 0.1 LANGUAGES C) set(CMAKE_C_STANDARD 90) set(CMAKE_C_STANDARD_REQUIRED ON) -if (CMAKE_C_FLAGS STREQUAL "") - set(CMAKE_C_FLAGS "-g") -endif() +include(CheckCCompilerFlag) -if (CMAKE_CROSSCOMPILING) - message(FATAL_ERROR "Currently Cmake makefile doesn't support cross compiling.") +if (CMAKE_C_FLAGS STREQUAL "") + check_c_compiler_flag("-g" DEBUG_OPTION) + if (DEBUG_OPTION) + set(CMAKE_C_FLAGS "-g") + endif() endif() if (APPLE AND NOT BIGNUM_NO) find_program(brew "brew") - if (NOT brew STREQUAL "") + if (brew STREQUAL "") + find_program(port "port") + # if homebrew isn't installed and macports is, add the macports default paths + # as a last resort. + if (NOT port STREQUAL "") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -isystem /opt/local/include") + set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -L/opt/local/lib") + endif() + else() # These Homebrew packages may be keg-only, meaning that they won't be found # in expected paths because they may conflict with system files. Ask # Homebrew where each one is located, then adjust paths accordingly. execute_process(COMMAND ${brew} --prefix openssl OUTPUT_VARIABLE openssl_prefix OUTPUT_STRIP_TRAILING_WHITESPACE) execute_process(COMMAND ${brew} --prefix gmp OUTPUT_VARIABLE gmp_prefix OUTPUT_STRIP_TRAILING_WHITESPACE) - if (NOT openssl_prefix STREQUAL "") - set(ENV{PKG_CONFIG_PATH} "${openssl_prefix}/lib/pkgconfig:$PKG_CONFIG_PATH") - endif() if (NOT gmp_prefix STREQUAL "") - set(ENV{PKG_CONFIG_PATH} "${gmp_prefix}/lib/pkgconfig:$PKG_CONFIG_PATH") set(GMP_C_FLAGS "-I${gmp_prefix}/include") set(GMP_LIBS "-L${gmp_prefix}/lib -lgmp") endif() - else() - find_program(port "port") - # if homebrew isn't installed and macports is, add the macports default paths - # as a last resort. - if (NOT port STREQUAL "") - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -isystem /opt/local/include") - set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -L/opt/local/lib") - endif() endif() endif() -include(CheckCCompilerFlag) +check_c_compiler_flag("-W" WARN_ALL) +if (WARN_ALL) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -W") +endif() + +set(WARN_FLAGS + "-std=c89 -pedantic -Wall -Wextra -Wcast-align -Wnested-externs -Wshadow -Wstrict-prototypes -Wno-unused-function -Wno-long-long -Wno-overlength-strings" +) -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -W") -set(WARN_C_FLAGS "-std=c89 -pedantic -Wall -Wextra -Wcast-align -Wnested-externs -Wshadow -Wstrict-prototypes -Wno-unused-function -Wno-long-long -Wno-overlength-strings") -set(SAVED_C_FLAGS "${CMAKE_C_FLAGS}") -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${WARN_C_FLAGS}") -check_c_compiler_flag(${CMAKE_C_FLAGS} USE_WARN_CFLAGS) -if (NOT USE_WARN_CFLAGS) - set(CMAKE_C_FLAGS "${SAVED_C_FLAGS}") +check_c_compiler_flag(${WARN_FLAGS} USE_WARN_CFLAGS) +if (USE_WARN_CFLAGS) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${WARN_FLAGS}") endif() -set(SAVED_C_FLAGS "${CMAKE_C_FLAGS}") -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=hidden") -check_c_compiler_flag(${CMAKE_C_FLAGS} USE_OPAQUE_CFLAGS) -if (NOT USE_OPAQUE_CFLAGS) - set(CMAKE_C_FLAGS "${SAVED_C_FLAGS}") +check_c_compiler_flag("-fvisibility=hidden" USE_OPAQUE_CFLAGS) +if (USE_OPAQUE_CFLAGS) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=hidden") endif() option(BENCHMARK "compile benchmark (default is on)" ON) @@ -84,10 +82,10 @@ option(ASM_NO "don't use any assembly optimization (default is auto)" OFF) include(CheckTypeSize) check_type_size("__int128" INT128) -if (NOT INT128 STREQUAL "") - set(INT128 ON) -else() +if (INT128 STREQUAL "") set(INT128 OFF) +else() + set(INT128 ON) endif() include(CheckCSourceCompiles) @@ -261,6 +259,12 @@ if (INT128) add_compile_definitions(HAVE___INT128=1) endif() +include(TestBigEndian) +test_big_endian(BIG_ENDIAN) +if (BIG_ENDIAN) + add_compile_definitions(WORDS_BIGENDIAN=1) +endif() + add_compile_definitions(SECP256K1_BUILD) message("Using static precomputation: ${PRECOMP}") @@ -268,8 +272,8 @@ message("Using x86_64 ASM: ${ASM_x86_64}") message("Using ARM ASM: ${ASM_ARM}") message("Using external ASM: ${USE_EXTERNAL_ASM}") message("Using 64 bit field implementation: ${FIELD_64BIT}") -message("Using GMP bignum implementation: ${BIGNUM_GMP}") message("Using 64 bit scalar implementation: ${SCALAR_64BIT}") +message("Using GMP bignum implementation: ${BIGNUM_GMP}") message("Using endomorphism optimizations: ${ENDOMORPHISM}") message("Building benchmarks: ${BENCHMARK}") message("Building for coverage analysis: ${COVERAGE}") diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 1f500a09de..4c145ea24e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -3,27 +3,27 @@ include_directories("${PROJECT_SOURCE_DIR}/include") add_library(secp256k1 secp256k1.c) if (TESTS) - add_executable(test tests.c) + add_executable(test tests.c) endif() if (EXHAUSTIVE_TESTS) - add_executable(exhaustive_test tests_exhaustive.c) + add_executable(exhaustive_test tests_exhaustive.c) endif() if (PRECOMP) - add_executable(precomputation gen_context.c) - # alternatives considered: - # add_file_dependencies, add_dependencies, add_custom_command - add_custom_target(precomputed_table - COMMAND "${PROJECT_SOURCE_DIR}/precomputation" - BYPRODUCTS "${PROJECT_SOURCE_DIR}/src/ecmult_static_context.h" - DEPENDS precomputation - WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} - ) - add_dependencies(secp256k1 precomputed_table) - if (TESTS) - add_dependencies(test precomputed_table) - endif() - if (EXHAUSTIVE_TESTS) - add_dependencies(exhaustive_test precomputed_table) - endif() + add_executable(precomputation gen_context.c) + # alternatives considered: + # add_file_dependencies, add_dependencies, add_custom_command + add_custom_target(precomputed_table + COMMAND "${PROJECT_SOURCE_DIR}/precomputation" + BYPRODUCTS "${PROJECT_SOURCE_DIR}/src/ecmult_static_context.h" + DEPENDS precomputation + WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} + ) + add_dependencies(secp256k1 precomputed_table) + if (TESTS) + add_dependencies(test precomputed_table) + endif() + if (EXHAUSTIVE_TESTS) + add_dependencies(exhaustive_test precomputed_table) + endif() endif() From 11a4372754bd080075e0b881bec777d37cbcdd84 Mon Sep 17 00:00:00 2001 From: DesWurstes Date: Wed, 26 Sep 2018 18:39:37 +0300 Subject: [PATCH 06/12] Silence some warnings --- CMakeLists.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index dca6e71bd9..d8e4726913 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -99,7 +99,7 @@ else() endif() include(CheckCSourceCompiles) -check_c_source_compiles("int main() {__builtin_expect(0,0);return 0;}" BUILTIN_EXPECT) +check_c_source_compiles("int main(void) {__builtin_expect(0,0);return 0;}" BUILTIN_EXPECT) if (COVERAGE) # Define this symbol to compile out all VERIFY code @@ -156,7 +156,7 @@ if (NOT BIGNUM_NO) # No need to append CMAKE_C_FLAGS to CMAKE_REQUIRED_FLAGS check_c_source_compiles("\ #include \n\ - mpz_t integ; int main() {return 0;}" GMP) + mpz_t integ; int main(void) {return 0;}" GMP) set(CMAKE_REQUIRED_FLAGS "") if (GMP) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${GMP_C_FLAGS}") @@ -178,7 +178,7 @@ if (NOT OPENSSL_TESTS_NO) #include \n\ #include \n\ #include \n\ - int main() {\n\ + int main(void) {\n\ EC_KEY *eckey = EC_KEY_new_by_curve_name(NID_secp256k1);\n\ ECDSA_sign(0, NULL, 0, NULL, NULL, eckey);\n\ ECDSA_verify(0, NULL, 0, NULL, 0, eckey);\n\ From 40da97a99d49d24670d5fc46f287e334a59d559b Mon Sep 17 00:00:00 2001 From: DesWurstes Date: Wed, 26 Sep 2018 18:41:18 +0300 Subject: [PATCH 07/12] squashme --- CMakeLists.txt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d8e4726913..5a9a95629d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -180,11 +180,10 @@ if (NOT OPENSSL_TESTS_NO) #include \n\ int main(void) {\n\ EC_KEY *eckey = EC_KEY_new_by_curve_name(NID_secp256k1);\n\ + ECDSA_SIG *sig_openssl = ECDSA_SIG_new();\n\ ECDSA_sign(0, NULL, 0, NULL, NULL, eckey);\n\ ECDSA_verify(0, NULL, 0, NULL, 0, eckey);\n\ EC_KEY_free(eckey);\n\ - ECDSA_SIG *sig_openssl;\n\ - sig_openssl = ECDSA_SIG_new();\n\ ECDSA_SIG_free(sig_openssl);\n\ return 0;\n\ }" OPENSSL) From ab6d4436216b658e7af45d89a513b68d797eb5b6 Mon Sep 17 00:00:00 2001 From: DesWurstes Date: Wed, 26 Sep 2018 18:49:50 +0300 Subject: [PATCH 08/12] remove linker flags --- CMakeLists.txt | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5a9a95629d..15ce727e62 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,8 +20,7 @@ if (APPLE AND NOT (BIGNUM_NO AND OPENSSL_TESTS_NO)) # if homebrew isn't installed and macports is, add the macports default paths # as a last resort. if (NOT port STREQUAL "") - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -isystem /opt/local/include") - set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -L/opt/local/lib") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -isystem /opt/local/include -L/opt/local/lib") endif() else() # These Homebrew packages may be keg-only, meaning that they won't be found @@ -105,7 +104,6 @@ if (COVERAGE) # Define this symbol to compile out all VERIFY code add_compile_definitions(COVERAGE=1) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0 --coverage") - set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} --coverage") else() check_c_compiler_flag("-O3" OPTIMIZE_O3) if (OPTIMIZE_O3) @@ -159,8 +157,7 @@ if (NOT BIGNUM_NO) mpz_t integ; int main(void) {return 0;}" GMP) set(CMAKE_REQUIRED_FLAGS "") if (GMP) - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${GMP_C_FLAGS}") - set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${GMP_LIBS}") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${GMP_C_FLAGS} ${GMP_LIBS}") set(BIGNUM_GMP ON) elseif(BIGNUM_GMP) message(FATAL_ERROR "gmp bignum explicitly requested but libgmp not available.") @@ -193,8 +190,7 @@ if (NOT OPENSSL_TESTS_NO) add_compile_definitions(ENABLE_OPENSSL_TESTS=1) # Define this symbol if libcrypto is installed add_compile_definitions(HAVE_LIBCRYPTO=1) - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OPENSSL_C_FLAGS}") - set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${OPENSSL_LIBS}") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OPENSSL_C_FLAGS} ${OPENSSL_LIBS}") elseif(OPENSSL_TESTS) message(FATAL_ERROR "openssl tests explicitly requested but openssl not available.") # else BIGNUM_NO From 27598f072c4155ce1e16edf8bd9a6f35ce5cc92a Mon Sep 17 00:00:00 2001 From: DesWurstes Date: Mon, 8 Oct 2018 18:54:51 +0300 Subject: [PATCH 09/12] Typo --- src/CMakeLists.txt | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 6c043ac591..31cbdec4ab 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -3,18 +3,18 @@ include_directories("${PROJECT_SOURCE_DIR}/include") add_library(secp256k1 secp256k1.c) if (TESTS) - add_executable(test tests.c) + add_executable(tests tests.c) endif() if (EXHAUSTIVE_TESTS) - add_executable(exhaustive_test tests_exhaustive.c) + add_executable(exhaustive_tests tests_exhaustive.c) endif() if (NOT COVERAGE) if (TESTS) - target_compile_definitions(test PRIVATE VERIFY) + target_compile_definitions(tests PRIVATE VERIFY) endif() if (EXHAUSTIVE_TESTS) - target_compile_definitions(exhaustive_test PRIVATE VERIFY) + target_compile_definitions(exhaustive_tests PRIVATE VERIFY) endif() endif() @@ -30,9 +30,9 @@ if (ECMULT_STATIC_PRECOMPUTATION) ) add_dependencies(secp256k1 precomputed_table) if (TESTS) - add_dependencies(test precomputed_table) + add_dependencies(tests precomputed_table) endif() if (EXHAUSTIVE_TESTS) - add_dependencies(exhaustive_test precomputed_table) + add_dependencies(exhaustive_tests precomputed_table) endif() endif() From f2abbc59526e4e0a18bfe1df848b94df94a65465 Mon Sep 17 00:00:00 2001 From: DesWurstes Date: Mon, 8 Oct 2018 19:35:49 +0300 Subject: [PATCH 10/12] Add bench --- src/CMakeLists.txt | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 31cbdec4ab..f211cce6db 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -2,22 +2,32 @@ include_directories(${PROJECT_SOURCE_DIR}) include_directories("${PROJECT_SOURCE_DIR}/include") add_library(secp256k1 secp256k1.c) + if (TESTS) add_executable(tests tests.c) + if (NOT COVERAGE) + target_compile_definitions(tests PRIVATE VERIFY) + endif() endif() + if (EXHAUSTIVE_TESTS) add_executable(exhaustive_tests tests_exhaustive.c) -endif() - -if (NOT COVERAGE) - if (TESTS) - target_compile_definitions(tests PRIVATE VERIFY) - endif() - if (EXHAUSTIVE_TESTS) + if (NOT COVERAGE) target_compile_definitions(exhaustive_tests PRIVATE VERIFY) endif() endif() +if (BENCHMARK) + add_executable(bench_verify bench_verify.c) + set_target_properties(bench_verify PROPERTIES LINK_FLAGS libsecp256k1.a) + add_dependencies(bench_verify secp256k1) + add_executable(bench_sign bench_sign.c) + set_target_properties(bench_sign PROPERTIES LINK_FLAGS libsecp256k1.a) + add_dependencies(bench_sign secp256k1) + add_executable(bench_internal bench_internal.c) + add_executable(bench_ecmult bench_ecmult.c) +endif() + if (ECMULT_STATIC_PRECOMPUTATION) add_executable(precomputation gen_context.c) # alternatives considered: @@ -35,4 +45,8 @@ if (ECMULT_STATIC_PRECOMPUTATION) if (EXHAUSTIVE_TESTS) add_dependencies(exhaustive_tests precomputed_table) endif() + if (BENCHMARK) + add_dependencies(bench_internal precomputed_table) + add_dependencies(bench_ecmult precomputed_table) + endif() endif() From 5e6d4c4a058374f69bf7b571a199c0eb9f210c29 Mon Sep 17 00:00:00 2001 From: DesWurstes Date: Sat, 24 Nov 2018 09:57:24 +0300 Subject: [PATCH 11/12] Add linker flags --- CMakeLists.txt | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 15ce727e62..98497d20ca 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,7 +20,8 @@ if (APPLE AND NOT (BIGNUM_NO AND OPENSSL_TESTS_NO)) # if homebrew isn't installed and macports is, add the macports default paths # as a last resort. if (NOT port STREQUAL "") - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -isystem /opt/local/include -L/opt/local/lib") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -isystem /opt/local/include") + set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -L/opt/local/lib") endif() else() # These Homebrew packages may be keg-only, meaning that they won't be found @@ -29,15 +30,15 @@ if (APPLE AND NOT (BIGNUM_NO AND OPENSSL_TESTS_NO)) if (NOT OPENSSL_TESTS_NO) execute_process(COMMAND ${brew} --prefix openssl OUTPUT_VARIABLE openssl_prefix OUTPUT_STRIP_TRAILING_WHITESPACE) if (NOT openssl_prefix STREQUAL "") - set(OPENSSL_C_FLAGS "-I${openssl_prefix}/include -lcrypto") - set(OPENSSL_LIBS "-L${openssl_prefix}/lib") + set(OPENSSL_C_FLAGS "-I${openssl_prefix}/include") + set(OPENSSL_LIBS "-L${openssl_prefix}/lib -lcrypto") endif() endif() if (NOT BIGNUM_NO) execute_process(COMMAND ${brew} --prefix gmp OUTPUT_VARIABLE gmp_prefix OUTPUT_STRIP_TRAILING_WHITESPACE) if (NOT gmp_prefix STREQUAL "") - set(GMP_C_FLAGS "-I${gmp_prefix}/include -lgmp") - set(GMP_LIBS "-L${gmp_prefix}/lib") + set(GMP_C_FLAGS "-I${gmp_prefix}/include") + set(GMP_LIBS "-L${gmp_prefix}/lib -lgmp") endif() endif() endif() @@ -104,6 +105,7 @@ if (COVERAGE) # Define this symbol to compile out all VERIFY code add_compile_definitions(COVERAGE=1) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0 --coverage") + set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} --coverage") else() check_c_compiler_flag("-O3" OPTIMIZE_O3) if (OPTIMIZE_O3) @@ -150,14 +152,15 @@ if (NOT SCALAR_32BIT) endif() if (NOT BIGNUM_NO) - set(CMAKE_REQUIRED_FLAGS "${GMP_C_FLAGS} ${GMP_LIBS}") + set(CMAKE_REQUIRED_FLAGS "${GMP_C_FLAGS} ${GMP_LIBS} ${CMAKE_MODULE_LINKER_FLAGS}") # No need to append CMAKE_C_FLAGS to CMAKE_REQUIRED_FLAGS check_c_source_compiles("\ #include \n\ mpz_t integ; int main(void) {return 0;}" GMP) set(CMAKE_REQUIRED_FLAGS "") if (GMP) - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${GMP_C_FLAGS} ${GMP_LIBS}") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${GMP_C_FLAGS}") + set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${GMP_LIBS}") set(BIGNUM_GMP ON) elseif(BIGNUM_GMP) message(FATAL_ERROR "gmp bignum explicitly requested but libgmp not available.") @@ -169,7 +172,7 @@ if (NOT OPENSSL_TESTS_NO) if (NOT TESTS AND OPENSSL_TESTS) message(FATAL_ERROR "OpenSSL tests requested but tests are not enabled.") endif() - set(CMAKE_REQUIRED_FLAGS "${OPENSSL_C_FLAGS} ${OPENSSL_LIBS}") + set(CMAKE_REQUIRED_FLAGS "${OPENSSL_C_FLAGS} ${OPENSSL_LIBS} ${CMAKE_MODULE_LINKER_FLAGS}") # No need to append CMAKE_C_FLAGS to CMAKE_REQUIRED_FLAGS check_c_source_compiles("\ #include \n\ @@ -190,7 +193,8 @@ if (NOT OPENSSL_TESTS_NO) add_compile_definitions(ENABLE_OPENSSL_TESTS=1) # Define this symbol if libcrypto is installed add_compile_definitions(HAVE_LIBCRYPTO=1) - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OPENSSL_C_FLAGS} ${OPENSSL_LIBS}") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OPENSSL_C_FLAGS}") + set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${OPENSSL_LIBS}") elseif(OPENSSL_TESTS) message(FATAL_ERROR "openssl tests explicitly requested but openssl not available.") # else BIGNUM_NO From 4ddd7659c51cc8054887e5ca960ef93a02943341 Mon Sep 17 00:00:00 2001 From: DesWurstes Date: Sat, 24 Nov 2018 18:21:16 +0300 Subject: [PATCH 12/12] Stabilize --- CMakeLists.txt | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 98497d20ca..1efd3651c0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,22 +6,24 @@ set(CMAKE_C_STANDARD_REQUIRED ON) include(CheckCCompilerFlag) -if (CMAKE_C_FLAGS STREQUAL "") +if ("${CMAKE_C_FLAGS}" STREQUAL "") check_c_compiler_flag("-g" DEBUG_OPTION) if (DEBUG_OPTION) set(CMAKE_C_FLAGS "-g") endif() endif() +set(LINKER_FLAGS "") + if (APPLE AND NOT (BIGNUM_NO AND OPENSSL_TESTS_NO)) find_program(brew "brew") - if (brew STREQUAL "") + if ("${brew}" STREQUAL "") find_program(port "port") # if homebrew isn't installed and macports is, add the macports default paths # as a last resort. - if (NOT port STREQUAL "") + if (NOT "${port}" STREQUAL "") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -isystem /opt/local/include") - set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -L/opt/local/lib") + set(LINKER_FLAGS "-L/opt/local/lib") endif() else() # These Homebrew packages may be keg-only, meaning that they won't be found @@ -29,14 +31,14 @@ if (APPLE AND NOT (BIGNUM_NO AND OPENSSL_TESTS_NO)) # Homebrew where each one is located, then adjust paths accordingly. if (NOT OPENSSL_TESTS_NO) execute_process(COMMAND ${brew} --prefix openssl OUTPUT_VARIABLE openssl_prefix OUTPUT_STRIP_TRAILING_WHITESPACE) - if (NOT openssl_prefix STREQUAL "") + if (NOT "${openssl_prefix}" STREQUAL "") set(OPENSSL_C_FLAGS "-I${openssl_prefix}/include") set(OPENSSL_LIBS "-L${openssl_prefix}/lib -lcrypto") endif() endif() if (NOT BIGNUM_NO) execute_process(COMMAND ${brew} --prefix gmp OUTPUT_VARIABLE gmp_prefix OUTPUT_STRIP_TRAILING_WHITESPACE) - if (NOT gmp_prefix STREQUAL "") + if (NOT "${gmp_prefix}" STREQUAL "") set(GMP_C_FLAGS "-I${gmp_prefix}/include") set(GMP_LIBS "-L${gmp_prefix}/lib -lgmp") endif() @@ -92,7 +94,7 @@ option(ASM_NO "don't use any assembly optimization (default is auto)" OFF) include(CheckTypeSize) check_type_size("__int128" INT128) -if (INT128 STREQUAL "") +if ("${INT128}" STREQUAL "") set(INT128 OFF) else() set(INT128 ON) @@ -105,7 +107,7 @@ if (COVERAGE) # Define this symbol to compile out all VERIFY code add_compile_definitions(COVERAGE=1) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0 --coverage") - set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} --coverage") + set(LINKER_FLAGS "${LINKER_FLAGS} --coverage") else() check_c_compiler_flag("-O3" OPTIMIZE_O3) if (OPTIMIZE_O3) @@ -152,7 +154,10 @@ if (NOT SCALAR_32BIT) endif() if (NOT BIGNUM_NO) - set(CMAKE_REQUIRED_FLAGS "${GMP_C_FLAGS} ${GMP_LIBS} ${CMAKE_MODULE_LINKER_FLAGS}") + if ("${GMP_LIBS}" STREQUAL "") + set(GMP_LIBS "-lgmp") + endif() + set(CMAKE_REQUIRED_FLAGS "${GMP_C_FLAGS} ${GMP_LIBS} ${LINKER_FLAGS}") # No need to append CMAKE_C_FLAGS to CMAKE_REQUIRED_FLAGS check_c_source_compiles("\ #include \n\ @@ -160,7 +165,7 @@ if (NOT BIGNUM_NO) set(CMAKE_REQUIRED_FLAGS "") if (GMP) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${GMP_C_FLAGS}") - set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${GMP_LIBS}") + set(LINKER_FLAGS "${LINKER_FLAGS} ${GMP_LIBS}") set(BIGNUM_GMP ON) elseif(BIGNUM_GMP) message(FATAL_ERROR "gmp bignum explicitly requested but libgmp not available.") @@ -172,7 +177,10 @@ if (NOT OPENSSL_TESTS_NO) if (NOT TESTS AND OPENSSL_TESTS) message(FATAL_ERROR "OpenSSL tests requested but tests are not enabled.") endif() - set(CMAKE_REQUIRED_FLAGS "${OPENSSL_C_FLAGS} ${OPENSSL_LIBS} ${CMAKE_MODULE_LINKER_FLAGS}") + if ("${OPENSSL_LIBS}" STREQUAL "") + set(OPENSSL_LIBS "-lcrypto") + endif() + set(CMAKE_REQUIRED_FLAGS "${OPENSSL_C_FLAGS} ${OPENSSL_LIBS} ${LINKER_FLAGS}") # No need to append CMAKE_C_FLAGS to CMAKE_REQUIRED_FLAGS check_c_source_compiles("\ #include \n\ @@ -194,7 +202,7 @@ if (NOT OPENSSL_TESTS_NO) # Define this symbol if libcrypto is installed add_compile_definitions(HAVE_LIBCRYPTO=1) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OPENSSL_C_FLAGS}") - set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${OPENSSL_LIBS}") + set(LINKER_FLAGS "${LINKER_FLAGS} ${OPENSSL_LIBS}") elseif(OPENSSL_TESTS) message(FATAL_ERROR "openssl tests explicitly requested but openssl not available.") # else BIGNUM_NO @@ -320,7 +328,10 @@ endif() # Use -DBUILD_SHARED_LIBS=ON to generate SHARED library -set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}) +set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${LINKER_FLAGS}") +set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${LINKER_FLAGS}") + +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}") add_subdirectory(src)