Skip to content

Commit 49c52ea

Browse files
committed
Squashed 'src/secp256k1/' changes from 427bc3c..3e3d125
3e3d125 Merge bitcoin-core/secp256k1#1309: changelog: Catch up e8295d0 Merge bitcoin-core/secp256k1#1311: Revert "Remove unused scratch space from API" 697e1cc changelog: Catch up 3ad1027 Revert "Remove unused scratch space from API" 76b43f3 changelog: Add entry for #1303 7d4f86d Merge bitcoin-core/secp256k1#1307: Mark more assembly outputs as early clobber b54a067 Merge bitcoin-core/secp256k1#1304: build: Rename arm to arm32 and check if it's really supported c6bb29b build: Rename `64bit` to `x86_64` 8c9ae37 Add release note 0324645 autotools: Add `SECP_ARM32_ASM_CHECK` macro ed4ba23 cmake: Add `check_arm32_assembly` function 350b4bd Mark stack variables as early clobber for technical correctness 0c729ba Bugfix: mark outputs as early clobber in scalar x86_64 asm 3353d3c Merge bitcoin-core/secp256k1#1207: Split fe_set_b32 into reducing and normalizing variants 5b32602 Split fe_set_b32 into reducing and normalizing variants 006ddc1 Merge bitcoin-core/secp256k1#1306: build: Make tests work with external default callbacks 1907f0f build: Make tests work with external default callbacks fb3a806 Merge bitcoin-core/secp256k1#1133: schnorrsig: Add test vectors for variable-length messages cd54ac7 schnorrsig: Improve docs of schnorrsig_sign_custom 28687b0 schnorrsig: Add BIP340 varlen test vectors 97a98be schnorrsig: Refactor test vector code to allow varlen messages ab5a917 Merge bitcoin-core/secp256k1#1303: ct: Use more volatile 9eb6934 Merge bitcoin-core/secp256k1#1305: Remove unused scratch space from API 073d98a Merge bitcoin-core/secp256k1#1292: refactor: Make 64-bit shift explicit 17fa217 ct: Be cautious and use volatile trick in more "conditional" paths 5fb336f ct: Use volatile trick in scalar_cond_negate 712e7f8 Remove unused scratch space from API 54d34b6 Merge bitcoin-core/secp256k1#1300: Avoid normalize conditional on VERIFY c63ec88 Merge bitcoin-core/secp256k1#1066: Abstract out and merge all the magnitude/normalized logic 7fc642f Simplify secp256k1_fe_{impl_,}verify 4e176ad Abstract out verify logic for fe_is_square_var 4371f98 Abstract out verify logic for fe_add_int 89e324c Abstract out verify logic for fe_half 283cd80 Abstract out verify logic for fe_get_bounds d5aa2f0 Abstract out verify logic for fe_inv{,_var} 3167646 Abstract out verify logic for fe_from_storage 76d31e5 Abstract out verify logic for fe_to_storage 1e6894b Abstract out verify logic for fe_cmov be82bd8 Improve comments/checks for fe_sqrt 6ab3508 Abstract out verify logic for fe_sqr 4c25f6e Abstract out verify logic for fe_mul e179e65 Abstract out verify logic for fe_add 7e7ad7f Abstract out verify logic for fe_mul_int 65d82a3 Abstract out verify logic for fe_negate 1446708 Abstract out verify logic for fe_get_b32 f7a7666 Abstract out verify logic for fe_set_b32 ce4d209 Abstract out verify logic for fe_cmp_var 7d7d43c Improve comments/check for fe_equal{,_var} c5e788d Abstract out verify logic for fe_is_odd d3f3fe8 Abstract out verify logic for fe_is_zero c701d9a Abstract out verify logic for fe_clear 19a2bfe Abstract out verify logic for fe_set_int 864f9db Abstract out verify logic for fe_normalizes_to_zero{,_var} 6c31371 Abstract out verify logic for fe_normalize_var e28b51f Abstract out verify logic for fe_normalize_weak b6b6f9c Abstract out verify logic for fe_normalize 7fa5195 Bugfix: correct SECP256K1_FE_CONST mag/norm fields e5cf4bf build: Rename `arm` to `arm32` b29566c Merge magnitude/normalized fields, move/improve comments 97c63b9 Avoid normalize conditional on VERIFY 341cc19 Merge bitcoin-core/secp256k1#1299: Infinity handling: ecmult_const(infinity) works, and group verification bbc8344 Avoid secp256k1_ge_set_gej_zinv with uninitialized z 0a2e0b2 Make secp256k1_{fe,ge,gej}_verify work as no-op if non-VERIFY f202667 Add invariant checking to group elements a18821d Always initialize output coordinates in secp256k1_ge_set_gej 3086cb9 Expose secp256k1_fe_verify to other modules a0e696f Make secp256k1_ecmult_const handle infinity 24c768a Merge bitcoin-core/secp256k1#1301: Avoid using bench_verify_data as bench_sign_data; merge them 2e65f1f Avoid using bench_verify_data as bench_sign_data; merge them 1cf15eb Merge bitcoin-core/secp256k1#1296: docs: complete interface description for `secp256k1_schnorrsig_sign_custom` 149c41c docs: complete interface description for `secp256k1_schnorrsig_sign_custom` f30c748 Merge bitcoin-core/secp256k1#1270: cmake: Fix library ABI versioning d1e48e5 refactor: Make 64-bit shift explicit b2e29e4 ci: Treat all compiler warnings as errors in "Windows (VS 2022)" task 3c81838 Merge bitcoin-core/secp256k1#1289: cmake: Use full signature of `add_test()` command 755629b cmake: Use full signature of `add_test()` command bef448f cmake: Fix library ABI versioning 4b0f711 Merge bitcoin-core/secp256k1#1277: autotools: Clean up after adding Wycheproof 222ecaf Merge bitcoin-core/secp256k1#1284: cmake: Some improvements using `PROJECT_IS_TOP_LEVEL` variable 71f746c cmake: Include `include` directory for subtree builds 024a409 Merge bitcoin-core/secp256k1#1240: cmake: Improve and document compiler flag checks a8d059f cmake, doc: Document compiler flags 6ece150 cmake, refactor: Rename `try_add_compile_option` to `try_append_cflags` 19516ed cmake: Use `add_compile_options()` in `try_add_compile_option()` 4b84f4b Merge bitcoin-core/secp256k1#1239: cmake: Bugfix and other improvements after bumping CMake up to 3.13 596b336 Merge bitcoin-core/secp256k1#1234: cmake: Add dev-mode 6b7e5b7 Merge bitcoin-core/secp256k1#1275: build: Fix C4005 "macro redefinition" MSVC warnings in examples 1c89536 Merge bitcoin-core/secp256k1#1286: tests: remove extra semicolon in macro c4062d6 debug: move helper for printing buffers into util.h 7e977b3 autotools: Take VPATH builds into account when generating testvectors 2418d32 autotools: Create src/wycheproof dir before creating file in it 8764034 autotools: Make all "pregenerated" targets .PHONY e1b9ce8 autotools: Use same conventions for all pregenerated files 3858bad tests: remove extra semicolon in macro 1f33bb2 Merge bitcoin-core/secp256k1#1205: field: Improve docs +tests of secp256k1_fe_set_b32 162da73 tests: Add debug helper for printing buffers e9fd3df field: Improve docs and tests of secp256k1_fe_set_b32 f6bef03 Merge bitcoin-core/secp256k1#1283: Get rid of secp256k1_fe_const_b 5431b9d cmake: Make `SECP256K1_INSTALL` default depend on `PROJECT_IS_TOP_LEVEL` 5ec1333 Merge bitcoin-core/secp256k1#1285: bench: Make sys/time.h a system include 68b16a1 bench: Make sys/time.h a system include 162608c cmake: Emulate `PROJECT_IS_TOP_LEVEL` for CMake<3.21 69e1ec0 Get rid of secp256k1_fe_const_b ce5ba9e gitignore: Add CMakeUserPresets.json 0a446a3 cmake: Add dev-mode CMake preset a6f4bcf Merge bitcoin-core/secp256k1#1231: Move `SECP256K1_INLINE` macro definition out from `include/secp256k1.h` a273d74 cmake: Improve version comparison 6a58b48 cmake: Use `if(... IN_LIST ...)` command 2445808 cmake: Use dedicated `GENERATOR_IS_MULTI_CONFIG` property 9f8703e cmake: Use dedicated `CMAKE_HOST_APPLE` variable 8c20170 cmake: Use recommended `add_compile_definitions` command 04d4cc0 cmake: Add `DESCRIPTION` and `HOMEPAGE_URL` options to `project` command 8a8b653 cmake: Use `SameMinorVersion` compatibility mode 5b0444a Merge bitcoin-core/secp256k1#1263: cmake: Make installation optional 47ac3d6 cmake: Make installation optional 2e035af Merge bitcoin-core/secp256k1#1273: build: Make `SECP_VALGRIND_CHECK` preserve `CPPFLAGS` 5be353d Merge bitcoin-core/secp256k1#1279: tests: lint wycheproof's python script 08f4b16 autotools: Move code around to tidy Makefile 04bf3f6 Merge bitcoin-core/secp256k1#1230: Build: allow static or shared but not both 9ce9984 Merge bitcoin-core/secp256k1#1265: Remove bits argument from secp256k1_wnaf_const{_xonly} 566faa1 Merge bitcoin-core/secp256k1#1267: doc: clarify process for patch releases ef49a11 build: allow static or shared but not both 35ada3b tests: lint wycheproof's python script 4258c54 Merge bitcoin-core/secp256k1#1276: autotools: Don't regenerate Wycheproof header automatically 529b54d autotools: Move Wycheproof header from EXTRA_DIST to noinst_HEADERS 06c67de autotools: Don't regenerate Wycheproof header automatically dc0657c build: Fix C4005 "macro redefinition" MSVC warnings in examples 1ecb94e build: Make `SECP_VALGRIND_CHECK` preserve `CPPFLAGS` 3bab71c Merge bitcoin-core/secp256k1#1268: release cleanup: bump version after 0.3.1 656c6ea release cleanup: bump version after 0.3.1 346a053 Merge bitcoin-core/secp256k1#1269: changelog: Fix link 6a37b2a changelog: Fix link ec98fce Merge bitcoin-core/secp256k1#1266: release: Prepare for 0.3.1 1b6fb55 doc: clarify process for patch releases 898e1c6 release: Prepare for 0.3.1 1d9a13f changelog: Remove inconsistent newlines 0e09166 changelog: Catch up in preparation of 0.3.1 7b7503d Merge bitcoin-core/secp256k1#1245: tests: Add Wycheproof ECDSA vectors a575339 Remove bits argument from secp256k1_wnaf_const (always 256) 145078c Merge bitcoin-core/secp256k1#1118: Add x-only ecmult_const version with x specified as n/d e5de454 tests: Add Wycheproof ECDSA vectors 0f86420 Add exhaustive tests for ecmult_const_xonly 4485926 Add x-only ecmult_const version for x=n/d a0f4644 Merge bitcoin-core/secp256k1#1252: Make position of * in pointer declarations in include/ consistent 4e68262 Merge bitcoin-core/secp256k1#1226: Add CMake instructions to release process 2d51a45 Merge bitcoin-core/secp256k1#1257: ct: Use volatile "trick" in all fe/scalar cmov implementations 4a496a3 ct: Use volatile "trick" in all fe/scalar cmov implementations 3d1f430 Make position of * in pointer declarations in include/ consistent 2bca0a5 Merge bitcoin-core/secp256k1#1241: build: Improve `SECP_TRY_APPEND_DEFAULT_CFLAGS` macro afd8b23 Merge bitcoin-core/secp256k1#1244: Suppress `-Wunused-parameter` when building for coverage analysis 1d8f367 Merge bitcoin-core/secp256k1#1250: No need to subtract 1 before doing a right shift 3e43041 No need to subtract 1 before doing a right shift 3addb4c build: Improve `SECP_TRY_APPEND_DEFAULT_CFLAGS` macro 0c07c82 Add CMake instructions to release process 464a911 Merge bitcoin-core/secp256k1#1242: Set ARM ASM symbol visibility to `hidden` f16a709 Merge bitcoin-core/secp256k1#1247: Apply Checks only in VERIFY mode. 70be3ca Merge bitcoin-core/secp256k1#1246: Typo 4ebd828 Apply Checks only in VERIFY mode. d1e7ca1 Typo 36b0adf build: remove warning until it's reproducible 5bb03c2 Replace `SECP256K1_ECMULT_TABLE_VERIFY` macro by a function 9c8c4f4 Merge bitcoin-core/secp256k1#1238: build: bump CMake minimum requirement to 3.13 0cf2fb9 Merge bitcoin-core/secp256k1#1243: build: Ensure no optimization when building for coverage analysis fd2a408 Set ARM ASM symbol visibility to `hidden` 4429a8c Suppress `-Wunused-parameter` when building for coverage analysis 8e79c7e build: Ensure no optimization when building for coverage analysis 96dd062 build: bump CMake minimum requirement to 3.13 8e142ca Move `SECP256K1_INLINE` macro definition out from `include/secp256k1.h` 7744589 Remove `SECP256K1_INLINE` usage from examples ca92a35 field: Simplify code in secp256k1_fe_set_b32 d93f62e field: Verify field element even after secp256k1_fe_set_b32 fails git-subtree-dir: src/secp256k1 git-subtree-split: 3e3d125b83f8426fb70535c469e7ef6aac412a2a
1 parent dd3ab6a commit 49c52ea

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+10339
-1092
lines changed

.cirrus.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ task:
176176
CTIMETESTS: no
177177
matrix:
178178
- env: {}
179-
- env: {EXPERIMENTAL: yes, ASM: arm}
179+
- env: {EXPERIMENTAL: yes, ASM: arm32}
180180
<< : *MERGE_BASE
181181
test_script:
182182
- ./ci/cirrus.sh
@@ -397,13 +397,13 @@ task:
397397
- PowerShell -NoLogo -Command if ($env:CIRRUS_PR -ne $null) { git fetch $env:CIRRUS_REPO_CLONE_URL pull/$env:CIRRUS_PR/merge; git reset --hard FETCH_HEAD; }
398398
configure_script:
399399
- '%x64_NATIVE_TOOLS%'
400-
- cmake -G "Visual Studio 17 2022" -A x64 -S . -B build -DSECP256K1_ENABLE_MODULE_RECOVERY=ON -DSECP256K1_BUILD_EXAMPLES=ON
400+
- cmake -E env CFLAGS="/WX" cmake -G "Visual Studio 17 2022" -A x64 -S . -B build -DSECP256K1_ENABLE_MODULE_RECOVERY=ON -DSECP256K1_BUILD_EXAMPLES=ON
401401
build_script:
402402
- '%x64_NATIVE_TOOLS%'
403403
- cmake --build build --config RelWithDebInfo -- -property:UseMultiToolTask=true;CL_MPcount=5
404404
check_script:
405405
- '%x64_NATIVE_TOOLS%'
406-
- ctest --test-dir build -j 5
406+
- ctest -C RelWithDebInfo --test-dir build -j 5
407407
- build\src\RelWithDebInfo\bench_ecmult.exe
408408
- build\src\RelWithDebInfo\bench_internal.exe
409409
- build\src\RelWithDebInfo\bench.exe

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,7 @@ build-aux/compile
5959
build-aux/test-driver
6060
libsecp256k1.pc
6161

62+
### CMake
63+
/CMakeUserPresets.json
6264
# Default CMake build directory.
6365
/build

CHANGELOG.md

+30-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,34 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
#### Security
11+
- Module `ecdh`: Fix "constant-timeness" issue with GCC 13.1 (and potentially future versions of GCC) that could leave applications using libsecp256k1's ECDH module vulnerable to a timing side-channel attack. The fix avoids secret-dependent control flow during ECDH computations when libsecp256k1 is compiled with GCC 13.1.
12+
13+
#### Fixed
14+
- Fixed an old bug that permitted compilers to potentially output bad assembly code on x86_64. In theory, it could lead to a crash or a read of unrelated memory, but this has never been observed on any compilers so far.
15+
16+
#### Changed
17+
- Various improvements and changes to CMake builds. CMake builds remain experimental.
18+
- Made API versioning consistent with GNU Autotools builds.
19+
- Switched to `BUILD_SHARED_LIBS` variable for controlling whether to build a static or a shared library.
20+
- Added `SECP256K1_INSTALL` variable for the controlling whether to install the build artefacts.
21+
- Renamed asm build option `arm` to `arm32`. Use `--with-asm=arm32` instead of `--with-asm=arm` (GNU Autotools), and `-DSECP256K1_ASM=arm32` instead of `-DSECP256K1_ASM=arm` (CMake).
22+
23+
## [0.3.1] - 2023-04-10
24+
We strongly recommend updating to 0.3.1 if you use or plan to use Clang >=14 to compile libsecp256k1, e.g., Xcode >=14 on macOS has Clang >=14. When in doubt, check the Clang version using `clang -v`.
25+
26+
#### Security
27+
- Fix "constant-timeness" issue with Clang >=14 that could leave applications using libsecp256k1 vulnerable to a timing side-channel attack. The fix avoids secret-dependent control flow and secret-dependent memory accesses in conditional moves of memory objects when libsecp256k1 is compiled with Clang >=14.
28+
29+
#### Added
30+
- Added tests against [Project Wycheproof's](https://github.com/google/wycheproof/) set of ECDSA test vectors (Bitcoin "low-S" variant), a fixed set of test cases designed to trigger various edge cases.
31+
32+
#### Changed
33+
- Increased minimum required CMake version to 3.13. CMake builds remain experimental.
34+
35+
#### ABI Compatibility
36+
The ABI is compatible with version 0.3.0.
37+
1038
## [0.3.0] - 2023-03-08
1139

1240
#### Added
@@ -25,7 +53,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2553
- Removed the configuration header `src/libsecp256k1-config.h`. We recommend passing flags to `./configure` or `cmake` to set configuration options (see `./configure --help` or `cmake -LH`). If you cannot or do not want to use one of the supported build systems, pass configuration flags such as `-DSECP256K1_ENABLE_MODULE_SCHNORRSIG` manually to the compiler (see the file `configure.ac` for supported flags).
2654

2755
#### ABI Compatibility
28-
2956
Due to changes in the API regarding `secp256k1_context_static` described above, the ABI is *not* compatible with previous versions.
3057

3158
## [0.2.0] - 2022-12-12
@@ -45,7 +72,6 @@ Due to changes in the API regarding `secp256k1_context_static` described above,
4572
- Module `schnorrsig`: renamed `secp256k1_schnorrsig_sign` to `secp256k1_schnorrsig_sign32`.
4673

4774
#### ABI Compatibility
48-
4975
Since this is the first release, we do not compare application binary interfaces.
5076
However, there are earlier unreleased versions of libsecp256k1 that are *not* ABI compatible with this version.
5177

@@ -55,7 +81,8 @@ This version was in fact never released.
5581
The number was given by the build system since the introduction of autotools in Jan 2014 (ea0fe5a5bf0c04f9cc955b2966b614f5f378c6f6).
5682
Therefore, this version number does not uniquely identify a set of source files.
5783

58-
[unreleased]: https://github.com/bitcoin-core/secp256k1/compare/v0.3.0...HEAD
84+
[unreleased]: https://github.com/bitcoin-core/secp256k1/compare/v0.3.1...HEAD
85+
[0.3.1]: https://github.com/bitcoin-core/secp256k1/compare/v0.3.0...v0.3.1
5986
[0.3.0]: https://github.com/bitcoin-core/secp256k1/compare/v0.2.0...v0.3.0
6087
[0.2.0]: https://github.com/bitcoin-core/secp256k1/compare/423b6d19d373f1224fd671a982584d7e7900bc93..v0.2.0
6188
[0.1.0]: https://github.com/bitcoin-core/secp256k1/commit/423b6d19d373f1224fd671a982584d7e7900bc93

CMakeLists.txt

+100-71
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,79 @@
1-
cmake_minimum_required(VERSION 3.1)
1+
cmake_minimum_required(VERSION 3.13)
22

3-
if(CMAKE_VERSION VERSION_GREATER 3.14)
3+
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.15)
44
# MSVC runtime library flags are selected by the CMAKE_MSVC_RUNTIME_LIBRARY abstraction.
55
cmake_policy(SET CMP0091 NEW)
66
# MSVC warning flags are not in CMAKE_<LANG>_FLAGS by default.
77
cmake_policy(SET CMP0092 NEW)
88
endif()
99

10-
# The package (a.k.a. release) version is based on semantic versioning 2.0.0 of
11-
# the API. All changes in experimental modules are treated as
12-
# backwards-compatible and therefore at most increase the minor version.
13-
project(libsecp256k1 VERSION 0.3.1 LANGUAGES C)
10+
project(libsecp256k1
11+
# The package (a.k.a. release) version is based on semantic versioning 2.0.0 of
12+
# the API. All changes in experimental modules are treated as
13+
# backwards-compatible and therefore at most increase the minor version.
14+
VERSION 0.3.2
15+
DESCRIPTION "Optimized C library for ECDSA signatures and secret/public key operations on curve secp256k1."
16+
HOMEPAGE_URL "https://github.com/bitcoin-core/secp256k1"
17+
LANGUAGES C
18+
)
19+
20+
if(CMAKE_VERSION VERSION_LESS 3.21)
21+
get_directory_property(parent_directory PARENT_DIRECTORY)
22+
if(parent_directory)
23+
set(PROJECT_IS_TOP_LEVEL OFF CACHE INTERNAL "Emulates CMake 3.21+ behavior.")
24+
set(${PROJECT_NAME}_IS_TOP_LEVEL OFF CACHE INTERNAL "Emulates CMake 3.21+ behavior.")
25+
else()
26+
set(PROJECT_IS_TOP_LEVEL ON CACHE INTERNAL "Emulates CMake 3.21+ behavior.")
27+
set(${PROJECT_NAME}_IS_TOP_LEVEL ON CACHE INTERNAL "Emulates CMake 3.21+ behavior.")
28+
endif()
29+
unset(parent_directory)
30+
endif()
1431

1532
# The library version is based on libtool versioning of the ABI. The set of
1633
# rules for updating the version can be found here:
1734
# https://www.gnu.org/software/libtool/manual/html_node/Updating-version-info.html
1835
# All changes in experimental modules are treated as if they don't affect the
1936
# interface and therefore only increase the revision.
2037
set(${PROJECT_NAME}_LIB_VERSION_CURRENT 2)
21-
set(${PROJECT_NAME}_LIB_VERSION_REVISION 1)
38+
set(${PROJECT_NAME}_LIB_VERSION_REVISION 2)
2239
set(${PROJECT_NAME}_LIB_VERSION_AGE 0)
2340

2441
set(CMAKE_C_STANDARD 90)
2542
set(CMAKE_C_EXTENSIONS OFF)
2643

2744
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
2845

29-
# We do not use CMake's BUILD_SHARED_LIBS option.
30-
option(SECP256K1_BUILD_SHARED "Build shared library." ON)
31-
option(SECP256K1_BUILD_STATIC "Build static library." ON)
32-
if(NOT SECP256K1_BUILD_SHARED AND NOT SECP256K1_BUILD_STATIC)
33-
message(FATAL_ERROR "At least one of SECP256K1_BUILD_SHARED and SECP256K1_BUILD_STATIC must be enabled.")
46+
option(BUILD_SHARED_LIBS "Build shared libraries." ON)
47+
option(SECP256K1_DISABLE_SHARED "Disable shared library. Overrides BUILD_SHARED_LIBS." OFF)
48+
if(SECP256K1_DISABLE_SHARED)
49+
set(BUILD_SHARED_LIBS OFF)
3450
endif()
3551

52+
option(SECP256K1_INSTALL "Enable installation." ${PROJECT_IS_TOP_LEVEL})
53+
3654
option(SECP256K1_ENABLE_MODULE_ECDH "Enable ECDH module." ON)
3755
if(SECP256K1_ENABLE_MODULE_ECDH)
38-
add_definitions(-DENABLE_MODULE_ECDH=1)
56+
add_compile_definitions(ENABLE_MODULE_ECDH=1)
3957
endif()
4058

4159
option(SECP256K1_ENABLE_MODULE_RECOVERY "Enable ECDSA pubkey recovery module." OFF)
4260
if(SECP256K1_ENABLE_MODULE_RECOVERY)
43-
add_definitions(-DENABLE_MODULE_RECOVERY=1)
61+
add_compile_definitions(ENABLE_MODULE_RECOVERY=1)
4462
endif()
4563

4664
option(SECP256K1_ENABLE_MODULE_EXTRAKEYS "Enable extrakeys module." ON)
4765
option(SECP256K1_ENABLE_MODULE_SCHNORRSIG "Enable schnorrsig module." ON)
4866
if(SECP256K1_ENABLE_MODULE_SCHNORRSIG)
4967
set(SECP256K1_ENABLE_MODULE_EXTRAKEYS ON)
50-
add_definitions(-DENABLE_MODULE_SCHNORRSIG=1)
68+
add_compile_definitions(ENABLE_MODULE_SCHNORRSIG=1)
5169
endif()
5270
if(SECP256K1_ENABLE_MODULE_EXTRAKEYS)
53-
add_definitions(-DENABLE_MODULE_EXTRAKEYS=1)
71+
add_compile_definitions(ENABLE_MODULE_EXTRAKEYS=1)
5472
endif()
5573

5674
option(SECP256K1_USE_EXTERNAL_DEFAULT_CALLBACKS "Enable external default callback functions." OFF)
5775
if(SECP256K1_USE_EXTERNAL_DEFAULT_CALLBACKS)
58-
add_definitions(-DUSE_EXTERNAL_DEFAULT_CALLBACKS=1)
76+
add_compile_definitions(USE_EXTERNAL_DEFAULT_CALLBACKS=1)
5977
endif()
6078

6179
set(SECP256K1_ECMULT_WINDOW_SIZE "AUTO" CACHE STRING "Window size for ecmult precomputation for verification, specified as integer in range [2..24]. \"AUTO\" is a reasonable setting for desktop machines (currently 15). [default=AUTO]")
@@ -65,37 +83,43 @@ check_string_option_value(SECP256K1_ECMULT_WINDOW_SIZE)
6583
if(SECP256K1_ECMULT_WINDOW_SIZE STREQUAL "AUTO")
6684
set(SECP256K1_ECMULT_WINDOW_SIZE 15)
6785
endif()
68-
add_definitions(-DECMULT_WINDOW_SIZE=${SECP256K1_ECMULT_WINDOW_SIZE})
86+
add_compile_definitions(ECMULT_WINDOW_SIZE=${SECP256K1_ECMULT_WINDOW_SIZE})
6987

7088
set(SECP256K1_ECMULT_GEN_PREC_BITS "AUTO" CACHE STRING "Precision bits to tune the precomputed table size for signing, specified as integer 2, 4 or 8. \"AUTO\" is a reasonable setting for desktop machines (currently 4). [default=AUTO]")
7189
set_property(CACHE SECP256K1_ECMULT_GEN_PREC_BITS PROPERTY STRINGS "AUTO" 2 4 8)
7290
check_string_option_value(SECP256K1_ECMULT_GEN_PREC_BITS)
7391
if(SECP256K1_ECMULT_GEN_PREC_BITS STREQUAL "AUTO")
7492
set(SECP256K1_ECMULT_GEN_PREC_BITS 4)
7593
endif()
76-
add_definitions(-DECMULT_GEN_PREC_BITS=${SECP256K1_ECMULT_GEN_PREC_BITS})
94+
add_compile_definitions(ECMULT_GEN_PREC_BITS=${SECP256K1_ECMULT_GEN_PREC_BITS})
7795

7896
set(SECP256K1_TEST_OVERRIDE_WIDE_MULTIPLY "OFF" CACHE STRING "Test-only override of the (autodetected by the C code) \"widemul\" setting. Legal values are: \"OFF\", \"int128_struct\", \"int128\" or \"int64\". [default=OFF]")
7997
set_property(CACHE SECP256K1_TEST_OVERRIDE_WIDE_MULTIPLY PROPERTY STRINGS "OFF" "int128_struct" "int128" "int64")
8098
check_string_option_value(SECP256K1_TEST_OVERRIDE_WIDE_MULTIPLY)
8199
if(SECP256K1_TEST_OVERRIDE_WIDE_MULTIPLY)
82100
string(TOUPPER "${SECP256K1_TEST_OVERRIDE_WIDE_MULTIPLY}" widemul_upper_value)
83-
add_definitions(-DUSE_FORCE_WIDEMUL_${widemul_upper_value}=1)
101+
add_compile_definitions(USE_FORCE_WIDEMUL_${widemul_upper_value}=1)
84102
endif()
85103
mark_as_advanced(FORCE SECP256K1_TEST_OVERRIDE_WIDE_MULTIPLY)
86104

87-
set(SECP256K1_ASM "AUTO" CACHE STRING "Assembly optimizations to use: \"AUTO\", \"OFF\", \"x86_64\" or \"arm\" (experimental). [default=AUTO]")
88-
set_property(CACHE SECP256K1_ASM PROPERTY STRINGS "AUTO" "OFF" "x86_64" "arm")
105+
set(SECP256K1_ASM "AUTO" CACHE STRING "Assembly optimizations to use: \"AUTO\", \"OFF\", \"x86_64\" or \"arm32\" (experimental). [default=AUTO]")
106+
set_property(CACHE SECP256K1_ASM PROPERTY STRINGS "AUTO" "OFF" "x86_64" "arm32")
89107
check_string_option_value(SECP256K1_ASM)
90-
if(SECP256K1_ASM STREQUAL "arm")
108+
if(SECP256K1_ASM STREQUAL "arm32")
91109
enable_language(ASM)
92-
add_definitions(-DUSE_EXTERNAL_ASM=1)
110+
include(CheckArm32Assembly)
111+
check_arm32_assembly()
112+
if(HAVE_ARM32_ASM)
113+
add_compile_definitions(USE_EXTERNAL_ASM=1)
114+
else()
115+
message(FATAL_ERROR "ARM32 assembly optimization requested but not available.")
116+
endif()
93117
elseif(SECP256K1_ASM)
94-
include(Check64bitAssembly)
95-
check_64bit_assembly()
96-
if(HAS_64BIT_ASM)
118+
include(CheckX86_64Assembly)
119+
check_x86_64_assembly()
120+
if(HAVE_X86_64_ASM)
97121
set(SECP256K1_ASM "x86_64")
98-
add_definitions(-DUSE_ASM_X86_64=1)
122+
add_compile_definitions(USE_ASM_X86_64=1)
99123
elseif(SECP256K1_ASM STREQUAL "AUTO")
100124
set(SECP256K1_ASM "OFF")
101125
else()
@@ -105,8 +129,8 @@ endif()
105129

106130
option(SECP256K1_EXPERIMENTAL "Allow experimental configuration options." OFF)
107131
if(NOT SECP256K1_EXPERIMENTAL)
108-
if(SECP256K1_ASM STREQUAL "arm")
109-
message(FATAL_ERROR "ARM assembly optimization is experimental. Use -DSECP256K1_EXPERIMENTAL=ON to allow.")
132+
if(SECP256K1_ASM STREQUAL "arm32")
133+
message(FATAL_ERROR "ARM32 assembly optimization is experimental. Use -DSECP256K1_EXPERIMENTAL=ON to allow.")
110134
endif()
111135
endif()
112136

@@ -118,7 +142,7 @@ if(SECP256K1_VALGRIND)
118142
if(Valgrind_FOUND)
119143
set(SECP256K1_VALGRIND ON)
120144
include_directories(${Valgrind_INCLUDE_DIR})
121-
add_definitions(-DVALGRIND)
145+
add_compile_definitions(VALGRIND)
122146
elseif(SECP256K1_VALGRIND STREQUAL "AUTO")
123147
set(SECP256K1_VALGRIND OFF)
124148
else()
@@ -147,7 +171,7 @@ else()
147171
endif()
148172

149173
# Define custom "Coverage" build type.
150-
set(CMAKE_C_FLAGS_COVERAGE "${CMAKE_C_FLAGS_RELWITHDEBINFO} -O0 -DCOVERAGE=1 --coverage -Wno-unused-parameter" CACHE STRING
174+
set(CMAKE_C_FLAGS_COVERAGE "${CMAKE_C_FLAGS_RELWITHDEBINFO} -O0 -DCOVERAGE=1 --coverage" CACHE STRING
151175
"Flags used by the C compiler during \"Coverage\" builds."
152176
FORCE
153177
)
@@ -165,49 +189,49 @@ mark_as_advanced(
165189
CMAKE_SHARED_LINKER_FLAGS_COVERAGE
166190
)
167191

168-
if(CMAKE_CONFIGURATION_TYPES)
169-
set(CMAKE_CONFIGURATION_TYPES "RelWithDebInfo" "Release" "Debug" "MinSizeRel" "Coverage")
170-
endif()
171-
172-
get_property(cached_cmake_build_type CACHE CMAKE_BUILD_TYPE PROPERTY TYPE)
173-
if(cached_cmake_build_type)
192+
get_property(is_multi_config GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
193+
set(default_build_type "RelWithDebInfo")
194+
if(is_multi_config)
195+
set(CMAKE_CONFIGURATION_TYPES "${default_build_type}" "Release" "Debug" "MinSizeRel" "Coverage" CACHE STRING
196+
"Supported configuration types."
197+
FORCE
198+
)
199+
else()
174200
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY
175-
STRINGS "RelWithDebInfo" "Release" "Debug" "MinSizeRel" "Coverage"
201+
STRINGS "${default_build_type}" "Release" "Debug" "MinSizeRel" "Coverage"
176202
)
203+
if(NOT CMAKE_BUILD_TYPE)
204+
message(STATUS "Setting build type to \"${default_build_type}\" as none was specified")
205+
set(CMAKE_BUILD_TYPE "${default_build_type}" CACHE STRING
206+
"Choose the type of build."
207+
FORCE
208+
)
209+
endif()
177210
endif()
178211

179-
set(default_build_type "RelWithDebInfo")
180-
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
181-
message(STATUS "Setting build type to \"${default_build_type}\" as none was specified")
182-
set(CMAKE_BUILD_TYPE "${default_build_type}" CACHE STRING "Choose the type of build." FORCE)
183-
endif()
184-
185-
include(TryAddCompileOption)
212+
include(TryAppendCFlags)
186213
if(MSVC)
187-
try_add_compile_option(/W2)
188-
try_add_compile_option(/wd4146)
214+
# Keep the following commands ordered lexicographically.
215+
try_append_c_flags(/W2) # Moderate warning level.
216+
try_append_c_flags(/wd4146) # Disable warning C4146 "unary minus operator applied to unsigned type, result still unsigned".
189217
else()
190-
try_add_compile_option(-pedantic)
191-
try_add_compile_option(-Wall)
192-
try_add_compile_option(-Wcast-align)
193-
try_add_compile_option(-Wcast-align=strict)
194-
try_add_compile_option(-Wconditional-uninitialized)
195-
try_add_compile_option(-Wextra)
196-
try_add_compile_option(-Wnested-externs)
197-
try_add_compile_option(-Wno-long-long)
198-
try_add_compile_option(-Wno-overlength-strings)
199-
try_add_compile_option(-Wno-unused-function)
200-
try_add_compile_option(-Wreserved-identifier)
201-
try_add_compile_option(-Wshadow)
202-
try_add_compile_option(-Wstrict-prototypes)
203-
try_add_compile_option(-Wundef)
218+
# Keep the following commands ordered lexicographically.
219+
try_append_c_flags(-pedantic)
220+
try_append_c_flags(-Wall) # GCC >= 2.95 and probably many other compilers.
221+
try_append_c_flags(-Wcast-align) # GCC >= 2.95.
222+
try_append_c_flags(-Wcast-align=strict) # GCC >= 8.0.
223+
try_append_c_flags(-Wconditional-uninitialized) # Clang >= 3.0 only.
224+
try_append_c_flags(-Wextra) # GCC >= 3.4, this is the newer name of -W, which we don't use because older GCCs will warn about unused functions.
225+
try_append_c_flags(-Wnested-externs)
226+
try_append_c_flags(-Wno-long-long) # GCC >= 3.0, -Wlong-long is implied by -pedantic.
227+
try_append_c_flags(-Wno-overlength-strings) # GCC >= 4.2, -Woverlength-strings is implied by -pedantic.
228+
try_append_c_flags(-Wno-unused-function) # GCC >= 3.0, -Wunused-function is implied by -Wall.
229+
try_append_c_flags(-Wreserved-identifier) # Clang >= 13.0 only.
230+
try_append_c_flags(-Wshadow)
231+
try_append_c_flags(-Wstrict-prototypes)
232+
try_append_c_flags(-Wundef)
204233
endif()
205234

206-
if(CMAKE_VERSION VERSION_GREATER 3.2)
207-
# Honor visibility properties for all target types.
208-
# See: https://cmake.org/cmake/help/latest/policy/CMP0063.html
209-
cmake_policy(SET CMP0063 NEW)
210-
endif()
211235
set(CMAKE_C_VISIBILITY_PRESET hidden)
212236

213237
# Ask CTest to create a "check" target (e.g., make check) as alias for the "test" target.
@@ -230,8 +254,13 @@ message("\n")
230254
message("secp256k1 configure summary")
231255
message("===========================")
232256
message("Build artifacts:")
233-
message(" shared library ...................... ${SECP256K1_BUILD_SHARED}")
234-
message(" static library ...................... ${SECP256K1_BUILD_STATIC}")
257+
if(BUILD_SHARED_LIBS)
258+
set(library_type "Shared")
259+
else()
260+
set(library_type "Static")
261+
endif()
262+
263+
message(" library type ........................ ${library_type}")
235264
message("Optional modules:")
236265
message(" ECDH ................................ ${SECP256K1_ENABLE_MODULE_ECDH}")
237266
message(" ECDSA pubkey recovery ............... ${SECP256K1_ENABLE_MODULE_RECOVERY}")
@@ -273,15 +302,15 @@ message("CFLAGS ................................ ${CMAKE_C_FLAGS}")
273302
get_directory_property(compile_options COMPILE_OPTIONS)
274303
string(REPLACE ";" " " compile_options "${compile_options}")
275304
message("Compile options ....................... " ${compile_options})
276-
if(DEFINED CMAKE_BUILD_TYPE)
305+
if(NOT is_multi_config)
277306
message("Build type:")
278307
message(" - CMAKE_BUILD_TYPE ................... ${CMAKE_BUILD_TYPE}")
279308
string(TOUPPER "${CMAKE_BUILD_TYPE}" build_type)
280309
message(" - CFLAGS ............................. ${CMAKE_C_FLAGS_${build_type}}")
281310
message(" - LDFLAGS for executables ............ ${CMAKE_EXE_LINKER_FLAGS_${build_type}}")
282311
message(" - LDFLAGS for shared libraries ....... ${CMAKE_SHARED_LINKER_FLAGS_${build_type}}")
283312
else()
284-
message("Available configurations .............. ${CMAKE_CONFIGURATION_TYPES}")
313+
message("Supported configurations .............. ${CMAKE_CONFIGURATION_TYPES}")
285314
message("RelWithDebInfo configuration:")
286315
message(" - CFLAGS ............................. ${CMAKE_C_FLAGS_RELWITHDEBINFO}")
287316
message(" - LDFLAGS for executables ............ ${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO}")

0 commit comments

Comments
 (0)