Skip to content

Commit eb3b9cc

Browse files
committed
Squashed 'src/secp256k1/' changes from 4187a46649..51d2ecd6e9
51d2ecd6e9 cmake: add a helper for linking into static libs 201b2b8f06 Merge bitcoin-core/secp256k1#1675: cmake: Bump minimum required CMake version to 3.22 3af71987a8 cmake: Bump minimum required CMake version to 3.22 92394476e9 Merge bitcoin-core/secp256k1#1673: Assert field magnitude at control-flow join 3a4f448cb4 Assert field magnitude at control-flow join 9fab425256 Merge bitcoin-core/secp256k1#1668: bench_ecmult: add benchmark for ecmult_const_xonly 05445377f4 bench_ecmult: add benchmark for ecmult_const_xonly bb597b3d39 Merge bitcoin-core/secp256k1#1670: tests: update wycheproof files d73ed99479 tests: update wycheproof files git-subtree-dir: src/secp256k1 git-subtree-split: 51d2ecd6e9f8ec1048d04fae34c2430c749d3bff
1 parent c31fcaa commit eb3b9cc

9 files changed

+70
-39
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ We strongly recommend updating to 0.3.1 if you use or plan to use Clang >=14 to
115115
- 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.
116116

117117
#### Added
118-
- 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.
118+
- Added tests against [Project Wycheproof's](https://github.com/C2SP/wycheproof/) set of ECDSA test vectors (Bitcoin "low-S" variant), a fixed set of test cases designed to trigger various edge cases.
119119

120120
#### Changed
121121
- Increased minimum required CMake version to 3.13. CMake builds remain experimental.

CMakeLists.txt

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cmake_minimum_required(VERSION 3.16)
1+
cmake_minimum_required(VERSION 3.22)
22

33
#=============================
44
# Project / Package metadata
@@ -15,17 +15,6 @@ project(libsecp256k1
1515
enable_testing()
1616
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
1717

18-
if(CMAKE_VERSION VERSION_LESS 3.21)
19-
# Emulates CMake 3.21+ behavior.
20-
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
21-
set(PROJECT_IS_TOP_LEVEL ON)
22-
set(${PROJECT_NAME}_IS_TOP_LEVEL ON)
23-
else()
24-
set(PROJECT_IS_TOP_LEVEL OFF)
25-
set(${PROJECT_NAME}_IS_TOP_LEVEL OFF)
26-
endif()
27-
endif()
28-
2918
# The library version is based on libtool versioning of the ABI. The set of
3019
# rules for updating the version can be found here:
3120
# https://www.gnu.org/software/libtool/manual/html_node/Updating-version-info.html

CMakePresets.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
{
2-
"cmakeMinimumRequired": {"major": 3, "minor": 21, "patch": 0},
32
"version": 3,
43
"configurePresets": [
54
{

src/CMakeLists.txt

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,19 @@ add_library(secp256k1_precomputed OBJECT EXCLUDE_FROM_ALL
1010
# from being exported.
1111
add_library(secp256k1 secp256k1.c $<TARGET_OBJECTS:secp256k1_precomputed>)
1212

13+
# Create a helper lib that parent projects can use to link secp256k1 into a
14+
# static lib.
15+
add_library(secp256k1_objs INTERFACE)
16+
target_sources(secp256k1_objs INTERFACE $<TARGET_OBJECTS:secp256k1> $<TARGET_OBJECTS:secp256k1_precomputed>)
17+
1318
add_library(secp256k1_asm INTERFACE)
1419
if(SECP256K1_ASM STREQUAL "arm32")
1520
add_library(secp256k1_asm_arm OBJECT EXCLUDE_FROM_ALL)
1621
target_sources(secp256k1_asm_arm PUBLIC
1722
asm/field_10x26_arm.s
1823
)
1924
target_sources(secp256k1 PRIVATE $<TARGET_OBJECTS:secp256k1_asm_arm>)
25+
target_sources(secp256k1_objs INTERFACE $<TARGET_OBJECTS:secp256k1_asm_arm>)
2026
target_link_libraries(secp256k1_asm INTERFACE secp256k1_asm_arm)
2127
endif()
2228

@@ -31,12 +37,21 @@ endif()
3137
get_target_property(use_pic secp256k1 POSITION_INDEPENDENT_CODE)
3238
set_target_properties(secp256k1_precomputed PROPERTIES POSITION_INDEPENDENT_CODE ${use_pic})
3339

34-
target_include_directories(secp256k1 INTERFACE
35-
# Add the include path for parent projects so that they don't have to manually add it.
40+
# Add the include path for parent projects so that they don't have to manually add it.
41+
set(_include_directory_for_parent_projects
3642
$<BUILD_INTERFACE:$<$<NOT:$<BOOL:${PROJECT_IS_TOP_LEVEL}>>:${PROJECT_SOURCE_DIR}/include>>
43+
)
44+
45+
target_include_directories(secp256k1 INTERFACE
46+
${_include_directory_for_parent_projects}
3747
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
3848
)
3949

50+
target_include_directories(secp256k1_objs INTERFACE
51+
${_include_directory_for_parent_projects}
52+
)
53+
unset(_include_directory_for_parent_projects)
54+
4055
# This emulates Libtool to make sure Libtool and CMake agree on the ABI version,
4156
# see below "Calculate the version variables" in build-aux/ltmain.sh.
4257
math(EXPR ${PROJECT_NAME}_soversion "${${PROJECT_NAME}_LIB_VERSION_CURRENT} - ${${PROJECT_NAME}_LIB_VERSION_AGE}")
@@ -48,20 +63,12 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
4863
VERSION ${${PROJECT_NAME}_soversion}.${${PROJECT_NAME}_LIB_VERSION_AGE}.${${PROJECT_NAME}_LIB_VERSION_REVISION}
4964
)
5065
elseif(APPLE)
51-
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.17)
52-
math(EXPR ${PROJECT_NAME}_compatibility_version "${${PROJECT_NAME}_LIB_VERSION_CURRENT} + 1")
53-
set_target_properties(secp256k1 PROPERTIES
54-
MACHO_COMPATIBILITY_VERSION ${${PROJECT_NAME}_compatibility_version}
55-
MACHO_CURRENT_VERSION ${${PROJECT_NAME}_compatibility_version}.${${PROJECT_NAME}_LIB_VERSION_REVISION}
56-
)
57-
unset(${PROJECT_NAME}_compatibility_version)
58-
elseif(BUILD_SHARED_LIBS)
59-
message(WARNING
60-
"The 'compatibility version' and 'current version' values of the DYLIB "
61-
"will diverge from the values set by the GNU Libtool. To ensure "
62-
"compatibility, it is recommended to upgrade CMake to at least version 3.17."
63-
)
64-
endif()
66+
math(EXPR ${PROJECT_NAME}_compatibility_version "${${PROJECT_NAME}_LIB_VERSION_CURRENT} + 1")
67+
set_target_properties(secp256k1 PROPERTIES
68+
MACHO_COMPATIBILITY_VERSION ${${PROJECT_NAME}_compatibility_version}
69+
MACHO_CURRENT_VERSION ${${PROJECT_NAME}_compatibility_version}.${${PROJECT_NAME}_LIB_VERSION_REVISION}
70+
)
71+
unset(${PROJECT_NAME}_compatibility_version)
6572
elseif(CMAKE_SYSTEM_NAME STREQUAL "Windows")
6673
set(${PROJECT_NAME}_windows "secp256k1")
6774
if(MSVC)

src/bench_ecmult.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ typedef struct {
5656

5757
/* Benchmark output. */
5858
secp256k1_gej* output;
59+
secp256k1_fe* output_xonly;
5960
} bench_data;
6061

6162
/* Hashes x into [0, POINTS) twice and store the result in offset1 and offset2. */
@@ -123,6 +124,32 @@ static void bench_ecmult_const_teardown(void* arg, int iters) {
123124
bench_ecmult_teardown_helper(data, &data->offset1, &data->offset2, NULL, iters);
124125
}
125126

127+
static void bench_ecmult_const_xonly(void* arg, int iters) {
128+
bench_data* data = (bench_data*)arg;
129+
int i;
130+
131+
for (i = 0; i < iters; ++i) {
132+
const secp256k1_ge* pubkey = &data->pubkeys[(data->offset1+i) % POINTS];
133+
const secp256k1_scalar* scalar = &data->scalars[(data->offset2+i) % POINTS];
134+
int known_on_curve = 1;
135+
secp256k1_ecmult_const_xonly(&data->output_xonly[i], &pubkey->x, NULL, scalar, known_on_curve);
136+
}
137+
}
138+
139+
static void bench_ecmult_const_xonly_teardown(void* arg, int iters) {
140+
bench_data* data = (bench_data*)arg;
141+
int i;
142+
143+
/* verify by comparing with x coordinate of regular ecmult result */
144+
for (i = 0; i < iters; ++i) {
145+
const secp256k1_gej* pubkey_gej = &data->pubkeys_gej[(data->offset1+i) % POINTS];
146+
const secp256k1_scalar* scalar = &data->scalars[(data->offset2+i) % POINTS];
147+
secp256k1_gej expected_gej;
148+
secp256k1_ecmult(&expected_gej, pubkey_gej, scalar, NULL);
149+
CHECK(secp256k1_gej_eq_x_var(&data->output_xonly[i], &expected_gej));
150+
}
151+
}
152+
126153
static void bench_ecmult_1p(void* arg, int iters) {
127154
bench_data* data = (bench_data*)arg;
128155
int i;
@@ -171,6 +198,8 @@ static void run_ecmult_bench(bench_data* data, int iters) {
171198
run_benchmark(str, bench_ecmult_gen, bench_ecmult_setup, bench_ecmult_gen_teardown, data, 10, iters);
172199
sprintf(str, "ecmult_const");
173200
run_benchmark(str, bench_ecmult_const, bench_ecmult_setup, bench_ecmult_const_teardown, data, 10, iters);
201+
sprintf(str, "ecmult_const_xonly");
202+
run_benchmark(str, bench_ecmult_const_xonly, bench_ecmult_setup, bench_ecmult_const_xonly_teardown, data, 10, iters);
174203
/* ecmult with non generator point */
175204
sprintf(str, "ecmult_1p");
176205
run_benchmark(str, bench_ecmult_1p, bench_ecmult_setup, bench_ecmult_1p_teardown, data, 10, iters);
@@ -319,6 +348,7 @@ int main(int argc, char **argv) {
319348
data.pubkeys_gej = malloc(sizeof(secp256k1_gej) * POINTS);
320349
data.expected_output = malloc(sizeof(secp256k1_gej) * (iters + 1));
321350
data.output = malloc(sizeof(secp256k1_gej) * (iters + 1));
351+
data.output_xonly = malloc(sizeof(secp256k1_fe) * (iters + 1));
322352

323353
/* Generate a set of scalars, and private/public keypairs. */
324354
secp256k1_gej_set_ge(&data.pubkeys_gej[0], &secp256k1_ge_const_g);
@@ -361,6 +391,7 @@ int main(int argc, char **argv) {
361391
free(data.pubkeys);
362392
free(data.pubkeys_gej);
363393
free(data.seckeys);
394+
free(data.output_xonly);
364395
free(data.output);
365396
free(data.expected_output);
366397

src/ecmult_const_impl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,8 @@ static int secp256k1_ecmult_const_xonly(secp256k1_fe* r, const secp256k1_fe *n,
373373
}
374374
}
375375

376+
SECP256K1_FE_VERIFY_MAGNITUDE(&g, 2);
377+
376378
/* Compute base point P = (n*g, g^2), the effective affine version of (n*g, g^2, v), which has
377379
* corresponding affine X coordinate n/d. */
378380
secp256k1_fe_mul(&p.x, &g, n);

src/wycheproof/WYCHEPROOF_COPYING

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
* The file `ecdsa_secp256k1_sha256_bitcoin_test.json` in this directory
2-
comes from Google's project Wycheproof with git commit
3-
`b063b4aedae951c69df014cd25fa6d69ae9e8cb9`, see
4-
https://github.com/google/wycheproof/blob/b063b4aedae951c69df014cd25fa6d69ae9e8cb9/testvectors_v1/ecdsa_secp256k1_sha256_bitcoin_test.json
2+
comes from project Wycheproof with git commit
3+
`df4e933efef449fc88af0c06e028d425d84a9495`, see
4+
https://github.com/C2SP/wycheproof/blob/df4e933efef449fc88af0c06e028d425d84a9495/testvectors_v1/ecdsa_secp256k1_sha256_bitcoin_test.json
55

66
* The file `ecdh_secp256k1_test.json` in this directory
7-
comes from Google's project Wycheproof with git commit
8-
`d9f6ec7d8bd8c96da05368999094e4a75ba5cb3d`, see
9-
https://github.com/google/wycheproof/blob/d9f6ec7d8bd8c96da05368999094e4a75ba5cb3d/testvectors_v1/ecdh_secp256k1_test.json
7+
comes from project Wycheproof with git commit
8+
`df4e933efef449fc88af0c06e028d425d84a9495`, see
9+
https://github.com/C2SP/wycheproof/blob/df4e933efef449fc88af0c06e028d425d84a9495/testvectors_v1/ecdh_secp256k1_test.json
1010

1111
* The file `ecdsa_secp256k1_sha256_bitcoin_test.h` is generated from
1212
`ecdsa_secp256k1_sha256_bitcoin_test.json` using the script

src/wycheproof/ecdh_secp256k1_test.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{
22
"algorithm" : "ECDH",
33
"schema" : "ecdh_test_schema.json",
4-
"generatorVersion" : "0.9rc5",
54
"numberOfTests" : 752,
65
"header" : [
76
"Test vectors of type EcdhTest are intended for",
@@ -124,6 +123,10 @@
124123
"testGroups" : [
125124
{
126125
"type" : "EcdhTest",
126+
"source" : {
127+
"name" : "google-wycheproof",
128+
"version" : "0.9rc5"
129+
},
127130
"curve" : "secp256k1",
128131
"encoding" : "asn",
129132
"tests" : [

src/wycheproof/ecdsa_secp256k1_sha256_bitcoin_test.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"notes" : {
1111
"ArithmeticError" : {
1212
"bugType" : "EDGE_CASE",
13-
"description" : "Some implementations of ECDSA have arithmetic errors that occur when intermediate results have extreme values. This test vector has been constructed to test such occurences.",
13+
"description" : "Some implementations of ECDSA have arithmetic errors that occur when intermediate results have extreme values. This test vector has been constructed to test such occurrences.",
1414
"cves" : [
1515
"CVE-2017-18146"
1616
]
@@ -95,7 +95,7 @@
9595
},
9696
"SignatureMalleabilityBitcoin" : {
9797
"bugType" : "SIGNATURE_MALLEABILITY",
98-
"description" : "\"BitCoins\"-curves are curves where signature malleability can be a serious issue. An implementation should only accept a signature s where s < n/2. If an implementation is not meant for uses cases that require signature malleability then this implemenation should be tested with another set of test vectors.",
98+
"description" : "\"BitCoins\"-curves are curves where signature malleability can be a serious issue. An implementation should only accept a signature s where s < n/2. If an implementation is not meant for uses cases that require signature malleability then this implementation should be tested with another set of test vectors.",
9999
"effect" : "In bitcoin exchanges, it may be used to make a double deposits or double withdrawals",
100100
"links" : [
101101
"https://en.bitcoin.it/wiki/Transaction_malleability",

0 commit comments

Comments
 (0)