Merge elementsproject/secp256k1-zkp#324: Upstream PRs 1662, 1669, 1492, 1670, 1668, 1673, 1675, 1680, 1679, 1690, 1683, 1678
145ae3e28dcmake: add a helper for linking into static libs (Cory Fields)819210974bREADME: add link to musig example, generalize module enabling hint (Sebastian Falbesoner)add146e101ci: Bump GCC snapshot major version to 16 (Hennadii Stepanov)6f67151ee2cmake: Use `PUBLIC_HEADER` target property (Hennadii Stepanov)c32715b2a0cmake, move-only: Move module option processing to `src/CMakeLists.txt` (Hennadii Stepanov)3f31ac43e0doc: Promote "Building with CMake" to standard procedure (Hennadii Stepanov)3af71987a8cmake: Bump minimum required CMake version to 3.22 (Hennadii Stepanov)3a4f448cb4Assert field magnitude at control-flow join (Peter.Dettman)05445377f4bench_ecmult: add benchmark for ecmult_const_xonly (Sebastian Falbesoner)d73ed99479tests: update wycheproof files (RandomLattice)e266ba11aetests: Add Wycheproof ECDH vectors (RandomLattice)c1bcb03276gitignore: Add Python cache files (Tim Ruffing)6b3fe51fb6bench: add ellswift to bench help output (Jonas Nick) Pull request description: ACKs for top commit: jonasnick: ACKcc4a92b510Tree-SHA512: ca0b28461c5e663a39ff11cb83bd09c1446e49e33c8806a96dd0912c0d14646be4b33ee6eaee88021d342986d17db254358d622a9567d4b6c91e2ab603224cb8
This commit is contained in:
21 files changed
+10875
-183
No files matched your search
@@ -70,3 +70,7 @@ contrib/gh-pr-create.sh
|
||||
/CMakeUserPresets.json
|
||||
# Default CMake build directory.
|
||||
/build
|
||||
|
||||
### Python
|
||||
__pycache__/
|
||||
*.py[oc]
|
||||
+1
-1
@@ -118,7 +118,7 @@ We strongly recommend updating to 0.3.1 if you use or plan to use Clang >=14 to
|
||||
- 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.
|
||||
|
||||
#### Added
|
||||
- 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.
|
||||
- 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.
|
||||
|
||||
#### Changed
|
||||
- Increased minimum required CMake version to 3.13. CMake builds remain experimental.
|
||||
|
||||
+1
-91
@@ -1,4 +1,4 @@
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
cmake_minimum_required(VERSION 3.22)
|
||||
|
||||
#=============================
|
||||
# Project / Package metadata
|
||||
@@ -15,17 +15,6 @@ project(libsecp256k1
|
||||
enable_testing()
|
||||
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
|
||||
|
||||
if(CMAKE_VERSION VERSION_LESS 3.21)
|
||||
# Emulates CMake 3.21+ behavior.
|
||||
if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
|
||||
set(PROJECT_IS_TOP_LEVEL ON)
|
||||
set(${PROJECT_NAME}_IS_TOP_LEVEL ON)
|
||||
else()
|
||||
set(PROJECT_IS_TOP_LEVEL OFF)
|
||||
set(${PROJECT_NAME}_IS_TOP_LEVEL OFF)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# The library version is based on libtool versioning of the ABI. The set of
|
||||
# rules for updating the version can be found here:
|
||||
# https://www.gnu.org/software/libtool/manual/html_node/Updating-version-info.html
|
||||
@@ -71,85 +60,6 @@ option(SECP256K1_ENABLE_MODULE_ECDSA_ADAPTOR "Enable ecdsa adaptor signatures mo
|
||||
option(SECP256K1_ENABLE_MODULE_ECDSA_S2C "Enable ECDSA sign-to-contract module." ON)
|
||||
option(SECP256K1_ENABLE_MODULE_BPPP "Enable Bulletproofs++ module." ON)
|
||||
|
||||
# Processing must be done in a topological sorting of the dependency graph
|
||||
# (dependent module first).
|
||||
if(SECP256K1_ENABLE_MODULE_BPPP)
|
||||
if(DEFINED SECP256K1_ENABLE_MODULE_GENERATOR AND NOT SECP256K1_ENABLE_MODULE_GENERATOR)
|
||||
message(FATAL_ERROR "Module dependency error: You have disabled the generator module explicitly, but it is required by the bppp module.")
|
||||
endif()
|
||||
set(SECP256K1_ENABLE_MODULE_GENERATOR ON)
|
||||
add_compile_definitions(ENABLE_MODULE_BPPP=1)
|
||||
endif()
|
||||
|
||||
if(SECP256K1_ENABLE_MODULE_ECDSA_S2C)
|
||||
add_compile_definitions(ENABLE_MODULE_ECDSA_S2C=1)
|
||||
endif()
|
||||
|
||||
if(SECP256K1_ENABLE_MODULE_ECDSA_ADAPTOR)
|
||||
add_compile_definitions(ENABLE_MODULE_ECDSA_ADAPTOR=1)
|
||||
endif()
|
||||
|
||||
if(SECP256K1_ENABLE_MODULE_WHITELIST)
|
||||
if(DEFINED SECP256K1_ENABLE_MODULE_RANGEPROOF AND NOT SECP256K1_ENABLE_MODULE_RANGEPROOF)
|
||||
message(FATAL_ERROR "Module dependency error: You have disabled the rangeproof module explicitly, but it is required by the whitelist module.")
|
||||
endif()
|
||||
set(SECP256K1_ENABLE_MODULE_RANGEPROOF ON)
|
||||
add_compile_definitions(ENABLE_MODULE_WHITELIST=1)
|
||||
endif()
|
||||
|
||||
if(SECP256K1_ENABLE_MODULE_SURJECTIONPROOF)
|
||||
if(DEFINED SECP256K1_ENABLE_MODULE_RANGEPROOF AND NOT SECP256K1_ENABLE_MODULE_RANGEPROOF)
|
||||
message(FATAL_ERROR "Module dependency error: You have disabled the rangeproof module explicitly, but it is required by the surjectionproof module.")
|
||||
endif()
|
||||
set(SECP256K1_ENABLE_MODULE_RANGEPROOF ON)
|
||||
add_compile_definitions(ENABLE_MODULE_SURJECTIONPROOF=1)
|
||||
endif()
|
||||
|
||||
if(SECP256K1_ENABLE_MODULE_RANGEPROOF)
|
||||
if(DEFINED SECP256K1_ENABLE_MODULE_GENERATOR AND NOT SECP256K1_ENABLE_MODULE_GENERATOR)
|
||||
message(FATAL_ERROR "Module dependency error: You have disabled the generator module explicitly, but it is required by the rangeproof module.")
|
||||
endif()
|
||||
set(SECP256K1_ENABLE_MODULE_GENERATOR ON)
|
||||
add_compile_definitions(ENABLE_MODULE_RANGEPROOF=1)
|
||||
endif()
|
||||
|
||||
if(SECP256K1_ENABLE_MODULE_GENERATOR)
|
||||
add_compile_definitions(ENABLE_MODULE_GENERATOR=1)
|
||||
endif()
|
||||
|
||||
|
||||
if(SECP256K1_ENABLE_MODULE_ELLSWIFT)
|
||||
add_compile_definitions(ENABLE_MODULE_ELLSWIFT=1)
|
||||
endif()
|
||||
|
||||
if(SECP256K1_ENABLE_MODULE_MUSIG)
|
||||
if(DEFINED SECP256K1_ENABLE_MODULE_SCHNORRSIG AND NOT SECP256K1_ENABLE_MODULE_SCHNORRSIG)
|
||||
message(FATAL_ERROR "Module dependency error: You have disabled the schnorrsig module explicitly, but it is required by the musig module.")
|
||||
endif()
|
||||
set(SECP256K1_ENABLE_MODULE_SCHNORRSIG ON)
|
||||
add_compile_definitions(ENABLE_MODULE_MUSIG=1)
|
||||
endif()
|
||||
|
||||
if(SECP256K1_ENABLE_MODULE_SCHNORRSIG)
|
||||
if(DEFINED SECP256K1_ENABLE_MODULE_EXTRAKEYS AND NOT SECP256K1_ENABLE_MODULE_EXTRAKEYS)
|
||||
message(FATAL_ERROR "Module dependency error: You have disabled the extrakeys module explicitly, but it is required by the schnorrsig module.")
|
||||
endif()
|
||||
set(SECP256K1_ENABLE_MODULE_EXTRAKEYS ON)
|
||||
add_compile_definitions(ENABLE_MODULE_SCHNORRSIG=1)
|
||||
endif()
|
||||
|
||||
if(SECP256K1_ENABLE_MODULE_EXTRAKEYS)
|
||||
add_compile_definitions(ENABLE_MODULE_EXTRAKEYS=1)
|
||||
endif()
|
||||
|
||||
if(SECP256K1_ENABLE_MODULE_RECOVERY)
|
||||
add_compile_definitions(ENABLE_MODULE_RECOVERY=1)
|
||||
endif()
|
||||
|
||||
if(SECP256K1_ENABLE_MODULE_ECDH)
|
||||
add_compile_definitions(ENABLE_MODULE_ECDH=1)
|
||||
endif()
|
||||
|
||||
option(SECP256K1_USE_EXTERNAL_DEFAULT_CALLBACKS "Enable external default callback functions." OFF)
|
||||
if(SECP256K1_USE_EXTERNAL_DEFAULT_CALLBACKS)
|
||||
add_compile_definitions(USE_EXTERNAL_DEFAULT_CALLBACKS=1)
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
{
|
||||
"cmakeMinimumRequired": {"major": 3, "minor": 21, "patch": 0},
|
||||
"version": 3,
|
||||
"configurePresets": [
|
||||
{
|
||||
|
||||
+12
-2
@@ -252,9 +252,17 @@ maintainer-clean-local: clean-precomp
|
||||
### (see the comments in the previous section for detailed rationale)
|
||||
TESTVECTORS = src/wycheproof/ecdsa_secp256k1_sha256_bitcoin_test.h
|
||||
|
||||
if ENABLE_MODULE_ECDH
|
||||
TESTVECTORS += src/wycheproof/ecdh_secp256k1_test.h
|
||||
endif
|
||||
|
||||
src/wycheproof/ecdsa_secp256k1_sha256_bitcoin_test.h:
|
||||
mkdir -p $(@D)
|
||||
python3 $(top_srcdir)/tools/tests_wycheproof_generate.py $(top_srcdir)/src/wycheproof/ecdsa_secp256k1_sha256_bitcoin_test.json > $@
|
||||
python3 $(top_srcdir)/tools/tests_wycheproof_generate_ecdsa.py $(top_srcdir)/src/wycheproof/ecdsa_secp256k1_sha256_bitcoin_test.json > $@
|
||||
|
||||
src/wycheproof/ecdh_secp256k1_test.h:
|
||||
mkdir -p $(@D)
|
||||
python3 $(top_srcdir)/tools/tests_wycheproof_generate_ecdh.py $(top_srcdir)/src/wycheproof/ecdh_secp256k1_test.json > $@
|
||||
|
||||
testvectors: $(TESTVECTORS)
|
||||
|
||||
@@ -278,7 +286,9 @@ EXTRA_DIST += sage/secp256k1_params.sage
|
||||
EXTRA_DIST += sage/weierstrass_prover.sage
|
||||
EXTRA_DIST += src/wycheproof/WYCHEPROOF_COPYING
|
||||
EXTRA_DIST += src/wycheproof/ecdsa_secp256k1_sha256_bitcoin_test.json
|
||||
EXTRA_DIST += tools/tests_wycheproof_generate.py
|
||||
EXTRA_DIST += src/wycheproof/ecdh_secp256k1_test.json
|
||||
EXTRA_DIST += tools/tests_wycheproof_generate_ecdsa.py
|
||||
EXTRA_DIST += tools/tests_wycheproof_generate_ecdh.py
|
||||
|
||||
if ENABLE_MODULE_SCHNORRSIG_HALFAGG
|
||||
include src/modules/schnorrsig_halfagg/Makefile.am.include
|
||||
|
||||
@@ -64,8 +64,8 @@ Building with Autotools
|
||||
|
||||
To compile optional modules (such as Schnorr signatures), you need to run `./configure` with additional flags (such as `--enable-module-schnorrsig`). Run `./configure --help` to see the full list of available flags. For experimental modules, you will also need `--enable-experimental` as well as a flag for each individual module, e.g. `--enable-module-rangeproof`.
|
||||
|
||||
Building with CMake (experimental)
|
||||
----------------------------------
|
||||
Building with CMake
|
||||
-------------------
|
||||
|
||||
To maintain a pristine source tree, CMake encourages to perform an out-of-source build by using a separate dedicated build tree.
|
||||
|
||||
@@ -108,8 +108,9 @@ Usage examples can be found in the [examples](examples) directory. To compile th
|
||||
* [Schnorr signatures example](examples/schnorr.c)
|
||||
* [Deriving a shared secret (ECDH) example](examples/ecdh.c)
|
||||
* [ElligatorSwift key exchange example](examples/ellswift.c)
|
||||
* [MuSig2 Schnorr multi-signatures example](examples/musig.c)
|
||||
|
||||
To compile the Schnorr signature and ECDH examples, you need to enable the corresponding module by providing a flag to the `configure` script, for example `--enable-module-schnorrsig`.
|
||||
To compile the examples, make sure the corresponding modules are enabled.
|
||||
|
||||
Benchmark
|
||||
------------
|
||||
|
||||
@@ -40,7 +40,7 @@ RUN apt-get update && apt-get install --no-install-recommends -y \
|
||||
apt-get clean && rm -rf /var/lib/apt/lists/*
|
||||
|
||||
# Build and install gcc snapshot
|
||||
ARG GCC_SNAPSHOT_MAJOR=15
|
||||
ARG GCC_SNAPSHOT_MAJOR=16
|
||||
RUN apt-get update && apt-get install --no-install-recommends -y wget libgmp-dev libmpfr-dev libmpc-dev flex && \
|
||||
mkdir gcc && cd gcc && \
|
||||
wget --progress=dot:giga --https-only --recursive --accept '*.tar.xz' --level 1 --no-directories "https://gcc.gnu.org/pub/gcc/snapshots/LATEST-${GCC_SNAPSHOT_MAJOR}" && \
|
||||
|
||||
+120
-66
@@ -1,5 +1,101 @@
|
||||
# Must be included before CMAKE_INSTALL_INCLUDEDIR is used.
|
||||
include(GNUInstallDirs)
|
||||
add_library(secp256k1)
|
||||
|
||||
set_property(TARGET secp256k1 PROPERTY PUBLIC_HEADER
|
||||
${PROJECT_SOURCE_DIR}/include/secp256k1.h
|
||||
${PROJECT_SOURCE_DIR}/include/secp256k1_preallocated.h
|
||||
)
|
||||
|
||||
# Processing must be done in a topological sorting of the dependency graph
|
||||
# (dependent module first).
|
||||
if(SECP256K1_ENABLE_MODULE_BPPP)
|
||||
if(DEFINED SECP256K1_ENABLE_MODULE_GENERATOR AND NOT SECP256K1_ENABLE_MODULE_GENERATOR)
|
||||
message(FATAL_ERROR "Module dependency error: You have disabled the generator module explicitly, but it is required by the bppp module.")
|
||||
endif()
|
||||
set(SECP256K1_ENABLE_MODULE_GENERATOR ON)
|
||||
add_compile_definitions(ENABLE_MODULE_BPPP=1)
|
||||
set_property(TARGET secp256k1 APPEND PROPERTY PUBLIC_HEADER ${PROJECT_SOURCE_DIR}/include/secp256k1_bppp.h)
|
||||
endif()
|
||||
|
||||
if(SECP256K1_ENABLE_MODULE_ECDSA_S2C)
|
||||
add_compile_definitions(ENABLE_MODULE_ECDSA_S2C=1)
|
||||
set_property(TARGET secp256k1 APPEND PROPERTY PUBLIC_HEADER ${PROJECT_SOURCE_DIR}/include/secp256k1_ecdsa_s2c.h)
|
||||
endif()
|
||||
|
||||
if(SECP256K1_ENABLE_MODULE_ECDSA_ADAPTOR)
|
||||
add_compile_definitions(ENABLE_MODULE_ECDSA_ADAPTOR=1)
|
||||
set_property(TARGET secp256k1 APPEND PROPERTY PUBLIC_HEADER ${PROJECT_SOURCE_DIR}/include/secp256k1_ecdsa_adaptor.h)
|
||||
endif()
|
||||
|
||||
if(SECP256K1_ENABLE_MODULE_WHITELIST)
|
||||
if(DEFINED SECP256K1_ENABLE_MODULE_RANGEPROOF AND NOT SECP256K1_ENABLE_MODULE_RANGEPROOF)
|
||||
message(FATAL_ERROR "Module dependency error: You have disabled the rangeproof module explicitly, but it is required by the whitelist module.")
|
||||
endif()
|
||||
set(SECP256K1_ENABLE_MODULE_RANGEPROOF ON)
|
||||
add_compile_definitions(ENABLE_MODULE_WHITELIST=1)
|
||||
set_property(TARGET secp256k1 APPEND PROPERTY PUBLIC_HEADER ${PROJECT_SOURCE_DIR}/include/secp256k1_whitelist.h)
|
||||
endif()
|
||||
|
||||
if(SECP256K1_ENABLE_MODULE_SURJECTIONPROOF)
|
||||
if(DEFINED SECP256K1_ENABLE_MODULE_RANGEPROOF AND NOT SECP256K1_ENABLE_MODULE_RANGEPROOF)
|
||||
message(FATAL_ERROR "Module dependency error: You have disabled the rangeproof module explicitly, but it is required by the surjectionproof module.")
|
||||
endif()
|
||||
set(SECP256K1_ENABLE_MODULE_RANGEPROOF ON)
|
||||
add_compile_definitions(ENABLE_MODULE_SURJECTIONPROOF=1)
|
||||
set_property(TARGET secp256k1 APPEND PROPERTY PUBLIC_HEADER ${PROJECT_SOURCE_DIR}/include/secp256k1_surjectionproof.h)
|
||||
endif()
|
||||
|
||||
if(SECP256K1_ENABLE_MODULE_RANGEPROOF)
|
||||
if(DEFINED SECP256K1_ENABLE_MODULE_GENERATOR AND NOT SECP256K1_ENABLE_MODULE_GENERATOR)
|
||||
message(FATAL_ERROR "Module dependency error: You have disabled the generator module explicitly, but it is required by the rangeproof module.")
|
||||
endif()
|
||||
set(SECP256K1_ENABLE_MODULE_GENERATOR ON)
|
||||
add_compile_definitions(ENABLE_MODULE_RANGEPROOF=1)
|
||||
set_property(TARGET secp256k1 APPEND PROPERTY PUBLIC_HEADER ${PROJECT_SOURCE_DIR}/include/secp256k1_rangeproof.h)
|
||||
endif()
|
||||
|
||||
if(SECP256K1_ENABLE_MODULE_GENERATOR)
|
||||
add_compile_definitions(ENABLE_MODULE_GENERATOR=1)
|
||||
set_property(TARGET secp256k1 APPEND PROPERTY PUBLIC_HEADER ${PROJECT_SOURCE_DIR}/include/secp256k1_generator.h)
|
||||
endif()
|
||||
|
||||
|
||||
if(SECP256K1_ENABLE_MODULE_ELLSWIFT)
|
||||
add_compile_definitions(ENABLE_MODULE_ELLSWIFT=1)
|
||||
set_property(TARGET secp256k1 APPEND PROPERTY PUBLIC_HEADER ${PROJECT_SOURCE_DIR}/include/secp256k1_ellswift.h)
|
||||
endif()
|
||||
|
||||
if(SECP256K1_ENABLE_MODULE_MUSIG)
|
||||
if(DEFINED SECP256K1_ENABLE_MODULE_SCHNORRSIG AND NOT SECP256K1_ENABLE_MODULE_SCHNORRSIG)
|
||||
message(FATAL_ERROR "Module dependency error: You have disabled the schnorrsig module explicitly, but it is required by the musig module.")
|
||||
endif()
|
||||
set(SECP256K1_ENABLE_MODULE_SCHNORRSIG ON)
|
||||
add_compile_definitions(ENABLE_MODULE_MUSIG=1)
|
||||
set_property(TARGET secp256k1 APPEND PROPERTY PUBLIC_HEADER ${PROJECT_SOURCE_DIR}/include/secp256k1_musig.h)
|
||||
endif()
|
||||
|
||||
if(SECP256K1_ENABLE_MODULE_SCHNORRSIG)
|
||||
if(DEFINED SECP256K1_ENABLE_MODULE_EXTRAKEYS AND NOT SECP256K1_ENABLE_MODULE_EXTRAKEYS)
|
||||
message(FATAL_ERROR "Module dependency error: You have disabled the extrakeys module explicitly, but it is required by the schnorrsig module.")
|
||||
endif()
|
||||
set(SECP256K1_ENABLE_MODULE_EXTRAKEYS ON)
|
||||
add_compile_definitions(ENABLE_MODULE_SCHNORRSIG=1)
|
||||
set_property(TARGET secp256k1 APPEND PROPERTY PUBLIC_HEADER ${PROJECT_SOURCE_DIR}/include/secp256k1_schnorrsig.h)
|
||||
endif()
|
||||
|
||||
if(SECP256K1_ENABLE_MODULE_EXTRAKEYS)
|
||||
add_compile_definitions(ENABLE_MODULE_EXTRAKEYS=1)
|
||||
set_property(TARGET secp256k1 APPEND PROPERTY PUBLIC_HEADER ${PROJECT_SOURCE_DIR}/include/secp256k1_extrakeys.h)
|
||||
endif()
|
||||
|
||||
if(SECP256K1_ENABLE_MODULE_RECOVERY)
|
||||
add_compile_definitions(ENABLE_MODULE_RECOVERY=1)
|
||||
set_property(TARGET secp256k1 APPEND PROPERTY PUBLIC_HEADER ${PROJECT_SOURCE_DIR}/include/secp256k1_recovery.h)
|
||||
endif()
|
||||
|
||||
if(SECP256K1_ENABLE_MODULE_ECDH)
|
||||
add_compile_definitions(ENABLE_MODULE_ECDH=1)
|
||||
set_property(TARGET secp256k1 APPEND PROPERTY PUBLIC_HEADER ${PROJECT_SOURCE_DIR}/include/secp256k1_ecdh.h)
|
||||
endif()
|
||||
|
||||
add_library(secp256k1_precomputed OBJECT EXCLUDE_FROM_ALL
|
||||
precomputed_ecmult.c
|
||||
@@ -8,7 +104,12 @@ add_library(secp256k1_precomputed OBJECT EXCLUDE_FROM_ALL
|
||||
|
||||
# Add objects explicitly rather than linking to the object libs to keep them
|
||||
# from being exported.
|
||||
add_library(secp256k1 secp256k1.c $<TARGET_OBJECTS:secp256k1_precomputed>)
|
||||
target_sources(secp256k1 PRIVATE secp256k1.c $<TARGET_OBJECTS:secp256k1_precomputed>)
|
||||
|
||||
# Create a helper lib that parent projects can use to link secp256k1 into a
|
||||
# static lib.
|
||||
add_library(secp256k1_objs INTERFACE)
|
||||
target_sources(secp256k1_objs INTERFACE $<TARGET_OBJECTS:secp256k1> $<TARGET_OBJECTS:secp256k1_precomputed>)
|
||||
|
||||
add_library(secp256k1_asm INTERFACE)
|
||||
if(SECP256K1_ASM STREQUAL "arm32")
|
||||
@@ -17,6 +118,7 @@ if(SECP256K1_ASM STREQUAL "arm32")
|
||||
asm/field_10x26_arm.s
|
||||
)
|
||||
target_sources(secp256k1 PRIVATE $<TARGET_OBJECTS:secp256k1_asm_arm>)
|
||||
target_sources(secp256k1_objs INTERFACE $<TARGET_OBJECTS:secp256k1_asm_arm>)
|
||||
target_link_libraries(secp256k1_asm INTERFACE secp256k1_asm_arm)
|
||||
endif()
|
||||
|
||||
@@ -31,10 +133,12 @@ endif()
|
||||
get_target_property(use_pic secp256k1 POSITION_INDEPENDENT_CODE)
|
||||
set_target_properties(secp256k1_precomputed PROPERTIES POSITION_INDEPENDENT_CODE ${use_pic})
|
||||
|
||||
# Add the include path for parent projects so that they don't have to manually add it.
|
||||
target_include_directories(secp256k1 INTERFACE
|
||||
# Add the include path for parent projects so that they don't have to manually add it.
|
||||
$<BUILD_INTERFACE:$<$<NOT:$<BOOL:${PROJECT_IS_TOP_LEVEL}>>:${PROJECT_SOURCE_DIR}/include>>
|
||||
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
|
||||
)
|
||||
set_target_properties(secp256k1_objs PROPERTIES
|
||||
INTERFACE_INCLUDE_DIRECTORIES "$<TARGET_PROPERTY:secp256k1,INTERFACE_INCLUDE_DIRECTORIES>"
|
||||
)
|
||||
|
||||
# This emulates Libtool to make sure Libtool and CMake agree on the ABI version,
|
||||
@@ -48,20 +152,12 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
|
||||
VERSION ${${PROJECT_NAME}_soversion}.${${PROJECT_NAME}_LIB_VERSION_AGE}.${${PROJECT_NAME}_LIB_VERSION_REVISION}
|
||||
)
|
||||
elseif(APPLE)
|
||||
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.17)
|
||||
math(EXPR ${PROJECT_NAME}_compatibility_version "${${PROJECT_NAME}_LIB_VERSION_CURRENT} + 1")
|
||||
set_target_properties(secp256k1 PROPERTIES
|
||||
MACHO_COMPATIBILITY_VERSION ${${PROJECT_NAME}_compatibility_version}
|
||||
MACHO_CURRENT_VERSION ${${PROJECT_NAME}_compatibility_version}.${${PROJECT_NAME}_LIB_VERSION_REVISION}
|
||||
)
|
||||
unset(${PROJECT_NAME}_compatibility_version)
|
||||
elseif(BUILD_SHARED_LIBS)
|
||||
message(WARNING
|
||||
"The 'compatibility version' and 'current version' values of the DYLIB "
|
||||
"will diverge from the values set by the GNU Libtool. To ensure "
|
||||
"compatibility, it is recommended to upgrade CMake to at least version 3.17."
|
||||
)
|
||||
endif()
|
||||
math(EXPR ${PROJECT_NAME}_compatibility_version "${${PROJECT_NAME}_LIB_VERSION_CURRENT} + 1")
|
||||
set_target_properties(secp256k1 PROPERTIES
|
||||
MACHO_COMPATIBILITY_VERSION ${${PROJECT_NAME}_compatibility_version}
|
||||
MACHO_CURRENT_VERSION ${${PROJECT_NAME}_compatibility_version}.${${PROJECT_NAME}_LIB_VERSION_REVISION}
|
||||
)
|
||||
unset(${PROJECT_NAME}_compatibility_version)
|
||||
elseif(CMAKE_SYSTEM_NAME STREQUAL "Windows")
|
||||
set(${PROJECT_NAME}_windows "secp256k1")
|
||||
if(MSVC)
|
||||
@@ -110,58 +206,16 @@ if(SECP256K1_BUILD_CTIME_TESTS)
|
||||
endif()
|
||||
|
||||
if(SECP256K1_INSTALL)
|
||||
include(GNUInstallDirs)
|
||||
target_include_directories(secp256k1 INTERFACE
|
||||
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
|
||||
)
|
||||
install(TARGETS secp256k1
|
||||
EXPORT ${PROJECT_NAME}-targets
|
||||
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
|
||||
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||
)
|
||||
set(${PROJECT_NAME}_headers
|
||||
"${PROJECT_SOURCE_DIR}/include/secp256k1.h"
|
||||
"${PROJECT_SOURCE_DIR}/include/secp256k1_preallocated.h"
|
||||
)
|
||||
if(SECP256K1_ENABLE_MODULE_BPPP)
|
||||
list(APPEND ${PROJECT_NAME}_headers "${PROJECT_SOURCE_DIR}/include/secp256k1_bppp.h")
|
||||
endif()
|
||||
if(SECP256K1_ENABLE_MODULE_ECDSA_S2C)
|
||||
list(APPEND ${PROJECT_NAME}_headers "${PROJECT_SOURCE_DIR}/include/secp256k1_ecdsa_s2c.h")
|
||||
endif()
|
||||
if(SECP256K1_ENABLE_MODULE_ECDSA_ADAPTOR)
|
||||
list(APPEND ${PROJECT_NAME}_headers "${PROJECT_SOURCE_DIR}/include/secp256k1_ecdsa_adaptor.h")
|
||||
endif()
|
||||
if(SECP256K1_ENABLE_MODULE_WHITELIST)
|
||||
list(APPEND ${PROJECT_NAME}_headers "${PROJECT_SOURCE_DIR}/include/secp256k1_whitelist.h")
|
||||
endif()
|
||||
if(SECP256K1_ENABLE_MODULE_SURJECTIONPROOF)
|
||||
list(APPEND ${PROJECT_NAME}_headers "${PROJECT_SOURCE_DIR}/include/secp256k1_surjectionproof.h")
|
||||
endif()
|
||||
if(SECP256K1_ENABLE_MODULE_RANGEPROOF)
|
||||
list(APPEND ${PROJECT_NAME}_headers "${PROJECT_SOURCE_DIR}/include/secp256k1_rangeproof.h")
|
||||
endif()
|
||||
if(SECP256K1_ENABLE_MODULE_GENERATOR)
|
||||
list(APPEND ${PROJECT_NAME}_headers "${PROJECT_SOURCE_DIR}/include/secp256k1_generator.h")
|
||||
endif()
|
||||
|
||||
if(SECP256K1_ENABLE_MODULE_ECDH)
|
||||
list(APPEND ${PROJECT_NAME}_headers "${PROJECT_SOURCE_DIR}/include/secp256k1_ecdh.h")
|
||||
endif()
|
||||
if(SECP256K1_ENABLE_MODULE_RECOVERY)
|
||||
list(APPEND ${PROJECT_NAME}_headers "${PROJECT_SOURCE_DIR}/include/secp256k1_recovery.h")
|
||||
endif()
|
||||
if(SECP256K1_ENABLE_MODULE_EXTRAKEYS)
|
||||
list(APPEND ${PROJECT_NAME}_headers "${PROJECT_SOURCE_DIR}/include/secp256k1_extrakeys.h")
|
||||
endif()
|
||||
if(SECP256K1_ENABLE_MODULE_SCHNORRSIG)
|
||||
list(APPEND ${PROJECT_NAME}_headers "${PROJECT_SOURCE_DIR}/include/secp256k1_schnorrsig.h")
|
||||
endif()
|
||||
if(SECP256K1_ENABLE_MODULE_MUSIG)
|
||||
list(APPEND ${PROJECT_NAME}_headers "${PROJECT_SOURCE_DIR}/include/secp256k1_musig.h")
|
||||
endif()
|
||||
if(SECP256K1_ENABLE_MODULE_ELLSWIFT)
|
||||
list(APPEND ${PROJECT_NAME}_headers "${PROJECT_SOURCE_DIR}/include/secp256k1_ellswift.h")
|
||||
endif()
|
||||
install(FILES ${${PROJECT_NAME}_headers}
|
||||
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
|
||||
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
|
||||
)
|
||||
|
||||
install(EXPORT ${PROJECT_NAME}-targets
|
||||
|
||||
+8
-4
@@ -16,18 +16,22 @@ static void help(int default_iters) {
|
||||
printf("Benchmarks the following algorithms:\n");
|
||||
printf(" - ECDSA signing/verification\n");
|
||||
|
||||
#ifdef ENABLE_MODULE_ECDH
|
||||
printf(" - ECDH key exchange (optional module)\n");
|
||||
#endif
|
||||
|
||||
#ifdef ENABLE_MODULE_RECOVERY
|
||||
printf(" - Public key recovery (optional module)\n");
|
||||
#endif
|
||||
|
||||
#ifdef ENABLE_MODULE_ECDH
|
||||
printf(" - ECDH key exchange (optional module)\n");
|
||||
#endif
|
||||
|
||||
#ifdef ENABLE_MODULE_SCHNORRSIG
|
||||
printf(" - Schnorr signatures (optional module)\n");
|
||||
#endif
|
||||
|
||||
#ifdef ENABLE_MODULE_ELLSWIFT
|
||||
printf(" - ElligatorSwift (optional module)\n");
|
||||
#endif
|
||||
|
||||
printf("\n");
|
||||
printf("The default number of iterations for each benchmark is %d. This can be\n", default_iters);
|
||||
printf("customized using the SECP256K1_BENCH_ITERS environment variable.\n");
|
||||
|
||||
@@ -56,6 +56,7 @@ typedef struct {
|
||||
|
||||
/* Benchmark output. */
|
||||
secp256k1_gej* output;
|
||||
secp256k1_fe* output_xonly;
|
||||
} bench_data;
|
||||
|
||||
/* 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) {
|
||||
bench_ecmult_teardown_helper(data, &data->offset1, &data->offset2, NULL, iters);
|
||||
}
|
||||
|
||||
static void bench_ecmult_const_xonly(void* arg, int iters) {
|
||||
bench_data* data = (bench_data*)arg;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < iters; ++i) {
|
||||
const secp256k1_ge* pubkey = &data->pubkeys[(data->offset1+i) % POINTS];
|
||||
const secp256k1_scalar* scalar = &data->scalars[(data->offset2+i) % POINTS];
|
||||
int known_on_curve = 1;
|
||||
secp256k1_ecmult_const_xonly(&data->output_xonly[i], &pubkey->x, NULL, scalar, known_on_curve);
|
||||
}
|
||||
}
|
||||
|
||||
static void bench_ecmult_const_xonly_teardown(void* arg, int iters) {
|
||||
bench_data* data = (bench_data*)arg;
|
||||
int i;
|
||||
|
||||
/* verify by comparing with x coordinate of regular ecmult result */
|
||||
for (i = 0; i < iters; ++i) {
|
||||
const secp256k1_gej* pubkey_gej = &data->pubkeys_gej[(data->offset1+i) % POINTS];
|
||||
const secp256k1_scalar* scalar = &data->scalars[(data->offset2+i) % POINTS];
|
||||
secp256k1_gej expected_gej;
|
||||
secp256k1_ecmult(&expected_gej, pubkey_gej, scalar, NULL);
|
||||
CHECK(secp256k1_gej_eq_x_var(&data->output_xonly[i], &expected_gej));
|
||||
}
|
||||
}
|
||||
|
||||
static void bench_ecmult_1p(void* arg, int iters) {
|
||||
bench_data* data = (bench_data*)arg;
|
||||
int i;
|
||||
@@ -171,6 +198,8 @@ static void run_ecmult_bench(bench_data* data, int iters) {
|
||||
run_benchmark(str, bench_ecmult_gen, bench_ecmult_setup, bench_ecmult_gen_teardown, data, 10, iters);
|
||||
sprintf(str, "ecmult_const");
|
||||
run_benchmark(str, bench_ecmult_const, bench_ecmult_setup, bench_ecmult_const_teardown, data, 10, iters);
|
||||
sprintf(str, "ecmult_const_xonly");
|
||||
run_benchmark(str, bench_ecmult_const_xonly, bench_ecmult_setup, bench_ecmult_const_xonly_teardown, data, 10, iters);
|
||||
/* ecmult with non generator point */
|
||||
sprintf(str, "ecmult_1p");
|
||||
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) {
|
||||
data.pubkeys_gej = malloc(sizeof(secp256k1_gej) * POINTS);
|
||||
data.expected_output = malloc(sizeof(secp256k1_gej) * (iters + 1));
|
||||
data.output = malloc(sizeof(secp256k1_gej) * (iters + 1));
|
||||
data.output_xonly = malloc(sizeof(secp256k1_fe) * (iters + 1));
|
||||
|
||||
/* Generate a set of scalars, and private/public keypairs. */
|
||||
secp256k1_gej_set_ge(&data.pubkeys_gej[0], &secp256k1_ge_const_g);
|
||||
@@ -361,6 +391,7 @@ int main(int argc, char **argv) {
|
||||
free(data.pubkeys);
|
||||
free(data.pubkeys_gej);
|
||||
free(data.seckeys);
|
||||
free(data.output_xonly);
|
||||
free(data.output);
|
||||
free(data.expected_output);
|
||||
|
||||
|
||||
@@ -373,6 +373,8 @@ static int secp256k1_ecmult_const_xonly(secp256k1_fe* r, const secp256k1_fe *n,
|
||||
}
|
||||
}
|
||||
|
||||
SECP256K1_FE_VERIFY_MAGNITUDE(&g, 2);
|
||||
|
||||
/* Compute base point P = (n*g, g^2), the effective affine version of (n*g, g^2, v), which has
|
||||
* corresponding affine X coordinate n/d. */
|
||||
secp256k1_fe_mul(&p.x, &g, n);
|
||||
|
||||
@@ -2,3 +2,4 @@ include_HEADERS += include/secp256k1_ecdh.h
|
||||
noinst_HEADERS += src/modules/ecdh/main_impl.h
|
||||
noinst_HEADERS += src/modules/ecdh/tests_impl.h
|
||||
noinst_HEADERS += src/modules/ecdh/bench_impl.h
|
||||
noinst_HEADERS += src/wycheproof/ecdh_secp256k1_test.h
|
||||
@@ -7,6 +7,13 @@
|
||||
#ifndef SECP256K1_MODULE_ECDH_TESTS_H
|
||||
#define SECP256K1_MODULE_ECDH_TESTS_H
|
||||
|
||||
static int ecdh_hash_function_test_xpassthru(unsigned char *output, const unsigned char *x, const unsigned char *y, void *data) {
|
||||
(void)y;
|
||||
(void)data;
|
||||
memcpy(output, x, 32);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int ecdh_hash_function_test_fail(unsigned char *output, const unsigned char *x, const unsigned char *y, void *data) {
|
||||
(void)output;
|
||||
(void)x;
|
||||
@@ -142,11 +149,45 @@ static void test_result_basepoint(void) {
|
||||
}
|
||||
}
|
||||
|
||||
static void test_ecdh_wycheproof(void) {
|
||||
#include "../../wycheproof/ecdh_secp256k1_test.h"
|
||||
int t;
|
||||
for (t = 0; t < SECP256K1_ECDH_WYCHEPROOF_NUMBER_TESTVECTORS; t++) {
|
||||
int parsed_ok;
|
||||
secp256k1_pubkey point;
|
||||
const unsigned char *pk;
|
||||
const unsigned char *sk;
|
||||
const unsigned char *expected_shared_secret;
|
||||
unsigned char output_ecdh[65] = { 0 };
|
||||
|
||||
int expected_result;
|
||||
|
||||
memset(&point, 0, sizeof(point));
|
||||
pk = &wycheproof_ecdh_public_keys[testvectors[t].pk_offset];
|
||||
parsed_ok = secp256k1_ec_pubkey_parse(CTX, &point, pk, testvectors[t].pk_len);
|
||||
|
||||
expected_result = testvectors[t].expected_result;
|
||||
CHECK(parsed_ok == expected_result);
|
||||
if (!parsed_ok) {
|
||||
continue;
|
||||
}
|
||||
|
||||
sk = &wycheproof_ecdh_private_keys[testvectors[t].sk_offset];
|
||||
CHECK(testvectors[t].sk_len == 32);
|
||||
|
||||
CHECK(secp256k1_ecdh(CTX, output_ecdh, &point, sk, ecdh_hash_function_test_xpassthru, NULL) == 1);
|
||||
expected_shared_secret = &wycheproof_ecdh_shared_secrets[testvectors[t].shared_offset];
|
||||
|
||||
CHECK(secp256k1_memcmp_var(output_ecdh, expected_shared_secret, testvectors[t].shared_len) == 0);
|
||||
}
|
||||
}
|
||||
|
||||
static void run_ecdh_tests(void) {
|
||||
test_ecdh_api();
|
||||
test_ecdh_generator_basepoint();
|
||||
test_bad_scalar();
|
||||
test_result_basepoint();
|
||||
test_ecdh_wycheproof();
|
||||
}
|
||||
|
||||
#endif /* SECP256K1_MODULE_ECDH_TESTS_H */
|
||||
@@ -1,11 +1,20 @@
|
||||
* The file `ecdsa_secp256k1_sha256_bitcoin_test.json` in this directory
|
||||
comes from Google's project Wycheproof with git commit
|
||||
`b063b4aedae951c69df014cd25fa6d69ae9e8cb9`, see
|
||||
https://github.com/google/wycheproof/blob/b063b4aedae951c69df014cd25fa6d69ae9e8cb9/testvectors_v1/ecdsa_secp256k1_sha256_bitcoin_test.json
|
||||
comes from project Wycheproof with git commit
|
||||
`df4e933efef449fc88af0c06e028d425d84a9495`, see
|
||||
https://github.com/C2SP/wycheproof/blob/df4e933efef449fc88af0c06e028d425d84a9495/testvectors_v1/ecdsa_secp256k1_sha256_bitcoin_test.json
|
||||
|
||||
* The file `ecdh_secp256k1_test.json` in this directory
|
||||
comes from project Wycheproof with git commit
|
||||
`df4e933efef449fc88af0c06e028d425d84a9495`, see
|
||||
https://github.com/C2SP/wycheproof/blob/df4e933efef449fc88af0c06e028d425d84a9495/testvectors_v1/ecdh_secp256k1_test.json
|
||||
|
||||
* The file `ecdsa_secp256k1_sha256_bitcoin_test.h` is generated from
|
||||
`ecdsa_secp256k1_sha256_bitcoin_test.json` using the script
|
||||
`tests_wycheproof_generate.py`.
|
||||
`tests_wycheproof_generate_ecdsa.py`.
|
||||
|
||||
* The file `ecdh_secp256k1_test.h` is generated from
|
||||
`ecdh_secp256k1_test.json` using the script
|
||||
`tests_wycheproof_generate_ecdh.py`.
|
||||
|
||||
-------------------------------------------------------------------------------
|
||||
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
@@ -1,4 +1,4 @@
|
||||
/* Note: this file was autogenerated using tests_wycheproof_generate.py. Do not edit. */
|
||||
/* Note: this file was autogenerated using tests_wycheproof_generate_ecdsa.py. Do not edit. */
|
||||
#define SECP256K1_ECDSA_WYCHEPROOF_NUMBER_TESTVECTORS (463)
|
||||
|
||||
typedef struct {
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
"notes" : {
|
||||
"ArithmeticError" : {
|
||||
"bugType" : "EDGE_CASE",
|
||||
"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.",
|
||||
"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.",
|
||||
"cves" : [
|
||||
"CVE-2017-18146"
|
||||
]
|
||||
@@ -95,7 +95,7 @@
|
||||
},
|
||||
"SignatureMalleabilityBitcoin" : {
|
||||
"bugType" : "SIGNATURE_MALLEABILITY",
|
||||
"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.",
|
||||
"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.",
|
||||
"effect" : "In bitcoin exchanges, it may be used to make a double deposits or double withdrawals",
|
||||
"links" : [
|
||||
"https://en.bitcoin.it/wiki/Transaction_malleability",
|
||||
|
||||
Executable
+166
@@ -0,0 +1,166 @@
|
||||
#!/usr/bin/env python3
|
||||
# Copyright (c) 2024 Random "Randy" Lattice and Sean Andersen
|
||||
# Distributed under the MIT software license, see the accompanying
|
||||
# file COPYING or https://www.opensource.org/licenses/mit-license.php.
|
||||
'''
|
||||
Generate a C file with ECDH testvectors from the Wycheproof project.
|
||||
'''
|
||||
|
||||
import json
|
||||
import sys
|
||||
|
||||
from binascii import hexlify, unhexlify
|
||||
from wycheproof_utils import to_c_array
|
||||
|
||||
def should_skip_flags(test_vector_flags):
|
||||
# skip these vectors because they are for ASN.1 encoding issues and other curves.
|
||||
# for more details, see https://github.com/bitcoin-core/secp256k1/pull/1492#discussion_r1572491546
|
||||
flags_to_skip = {"InvalidAsn", "WrongCurve"}
|
||||
return any(flag in test_vector_flags for flag in flags_to_skip)
|
||||
|
||||
def should_skip_tcid(test_vector_tcid):
|
||||
# We skip some test case IDs that have a public key whose custom ASN.1 representation explicitly
|
||||
# encodes some curve parameters that are invalid. libsecp256k1 never parses this part so we do
|
||||
# not care testing those. See https://github.com/bitcoin-core/secp256k1/pull/1492#discussion_r1572491546
|
||||
tcids_to_skip = [496, 497, 502, 503, 504, 505, 507]
|
||||
return test_vector_tcid in tcids_to_skip
|
||||
|
||||
# Rudimentary ASN.1 DER public key parser.
|
||||
# This should not be used for anything other than parsing Wycheproof test vectors.
|
||||
def parse_der_pk(s):
|
||||
tag = s[0]
|
||||
L = int(s[1])
|
||||
offset = 0
|
||||
if L & 0x80:
|
||||
if L == 0x81:
|
||||
L = int(s[2])
|
||||
offset = 1
|
||||
elif L == 0x82:
|
||||
L = 256 * int(s[2]) + int(s[3])
|
||||
offset = 2
|
||||
else:
|
||||
raise ValueError("invalid L")
|
||||
value = s[(offset + 2):(L + 2 + offset)]
|
||||
rest = s[(L + 2 + offset):]
|
||||
|
||||
if len(rest) > 0 or tag == 0x06: # OBJECT IDENTIFIER
|
||||
return parse_der_pk(rest)
|
||||
if tag == 0x03: # BIT STRING
|
||||
return value
|
||||
if tag == 0x30: # SEQUENCE
|
||||
return parse_der_pk(value)
|
||||
raise ValueError("unknown tag")
|
||||
|
||||
def parse_public_key(pk):
|
||||
der_pub_key = parse_der_pk(unhexlify(pk)) # Convert back to str and strip off the `0x`
|
||||
return hexlify(der_pub_key).decode()[2:]
|
||||
|
||||
def normalize_private_key(sk):
|
||||
# Ensure the private key is at most 64 characters long, retaining the last 64 if longer.
|
||||
# In the wycheproof test vectors, some private keys have leading zeroes
|
||||
normalized = sk[-64:].zfill(64)
|
||||
if len(normalized) != 64:
|
||||
raise ValueError("private key must be exactly 64 characters long.")
|
||||
return normalized
|
||||
|
||||
def normalize_expected_result(er):
|
||||
result_mapping = {"invalid": 0, "valid": 1, "acceptable": 1}
|
||||
return result_mapping[er]
|
||||
|
||||
filename_input = sys.argv[1]
|
||||
|
||||
with open(filename_input) as f:
|
||||
doc = json.load(f)
|
||||
|
||||
num_vectors = 0
|
||||
offset_sk_running, offset_pk_running, offset_shared = 0, 0, 0
|
||||
test_vectors_out = ""
|
||||
private_keys = ""
|
||||
shared_secrets = ""
|
||||
public_keys = ""
|
||||
cache_sks = {}
|
||||
cache_public_keys = {}
|
||||
|
||||
for group in doc['testGroups']:
|
||||
assert group["type"] == "EcdhTest"
|
||||
assert group["curve"] == "secp256k1"
|
||||
for test_vector in group['tests']:
|
||||
if should_skip_flags(test_vector['flags']) or should_skip_tcid(test_vector['tcId']):
|
||||
continue
|
||||
|
||||
public_key = parse_public_key(test_vector['public'])
|
||||
private_key = normalize_private_key(test_vector['private'])
|
||||
expected_result = normalize_expected_result(test_vector['result'])
|
||||
|
||||
# // 2 to convert hex to byte length
|
||||
shared_size = len(test_vector['shared']) // 2
|
||||
sk_size = len(private_key) // 2
|
||||
pk_size = len(public_key) // 2
|
||||
|
||||
new_sk = False
|
||||
sk = to_c_array(private_key)
|
||||
sk_offset = offset_sk_running
|
||||
|
||||
# check for repeated sk
|
||||
if sk not in cache_sks:
|
||||
if num_vectors != 0 and sk_size != 0:
|
||||
private_keys += ",\n "
|
||||
cache_sks[sk] = offset_sk_running
|
||||
private_keys += sk
|
||||
new_sk = True
|
||||
else:
|
||||
sk_offset = cache_sks[sk]
|
||||
|
||||
new_pk = False
|
||||
pk = to_c_array(public_key) if public_key != '0x' else ''
|
||||
|
||||
pk_offset = offset_pk_running
|
||||
# check for repeated pk
|
||||
if pk not in cache_public_keys:
|
||||
if num_vectors != 0 and len(pk) != 0:
|
||||
public_keys += ",\n "
|
||||
cache_public_keys[pk] = offset_pk_running
|
||||
public_keys += pk
|
||||
new_pk = True
|
||||
else:
|
||||
pk_offset = cache_public_keys[pk]
|
||||
|
||||
|
||||
shared_secrets += ",\n " if num_vectors and shared_size else ""
|
||||
shared_secrets += to_c_array(test_vector['shared'])
|
||||
wycheproof_tcid = test_vector['tcId']
|
||||
|
||||
test_vectors_out += " /" + "* tcId: " + str(test_vector['tcId']) + ". " + test_vector['comment'] + " *" + "/\n"
|
||||
test_vectors_out += f" {{{pk_offset}, {pk_size}, {sk_offset}, {sk_size}, {offset_shared}, {shared_size}, {expected_result}, {wycheproof_tcid} }},\n"
|
||||
if new_sk:
|
||||
offset_sk_running += sk_size
|
||||
if new_pk:
|
||||
offset_pk_running += pk_size
|
||||
offset_shared += shared_size
|
||||
num_vectors += 1
|
||||
|
||||
struct_definition = """
|
||||
typedef struct {
|
||||
size_t pk_offset;
|
||||
size_t pk_len;
|
||||
size_t sk_offset;
|
||||
size_t sk_len;
|
||||
size_t shared_offset;
|
||||
size_t shared_len;
|
||||
int expected_result;
|
||||
int wycheproof_tcid;
|
||||
} wycheproof_ecdh_testvector;
|
||||
"""
|
||||
|
||||
print("/* Note: this file was autogenerated using tests_wycheproof_ecdh.py. Do not edit. */")
|
||||
print(f"#define SECP256K1_ECDH_WYCHEPROOF_NUMBER_TESTVECTORS ({num_vectors})")
|
||||
|
||||
print(struct_definition)
|
||||
|
||||
print("static const unsigned char wycheproof_ecdh_private_keys[] = { " + private_keys + "};\n")
|
||||
print("static const unsigned char wycheproof_ecdh_public_keys[] = { " + public_keys + "};\n")
|
||||
print("static const unsigned char wycheproof_ecdh_shared_secrets[] = { " + shared_secrets + "};\n")
|
||||
|
||||
print("static const wycheproof_ecdh_testvector testvectors[SECP256K1_ECDH_WYCHEPROOF_NUMBER_TESTVECTORS] = {")
|
||||
print(test_vectors_out)
|
||||
print("};")
|
||||
@@ -9,6 +9,8 @@ Generate a C file with ECDSA testvectors from the Wycheproof project.
|
||||
import json
|
||||
import sys
|
||||
|
||||
from wycheproof_utils import to_c_array
|
||||
|
||||
filename_input = sys.argv[1]
|
||||
|
||||
with open(filename_input) as f:
|
||||
@@ -16,12 +18,6 @@ with open(filename_input) as f:
|
||||
|
||||
num_groups = len(doc['testGroups'])
|
||||
|
||||
def to_c_array(x):
|
||||
if x == "":
|
||||
return ""
|
||||
s = ',0x'.join(a+b for a,b in zip(x[::2], x[1::2]))
|
||||
return "0x" + s
|
||||
|
||||
|
||||
num_vectors = 0
|
||||
offset_msg_running, offset_pk_running, offset_sig = 0, 0, 0
|
||||
@@ -101,7 +97,7 @@ typedef struct {
|
||||
"""
|
||||
|
||||
|
||||
print("/* Note: this file was autogenerated using tests_wycheproof_generate.py. Do not edit. */")
|
||||
print("/* Note: this file was autogenerated using tests_wycheproof_generate_ecdsa.py. Do not edit. */")
|
||||
print(f"#define SECP256K1_ECDSA_WYCHEPROOF_NUMBER_TESTVECTORS ({num_vectors})")
|
||||
|
||||
print(struct_definition)
|
||||
@@ -0,0 +1,12 @@
|
||||
# Copyright (c) 2024 Random "Randy" Lattice and Sean Andersen
|
||||
# Distributed under the MIT software license, see the accompanying
|
||||
# file COPYING or https://www.opensource.org/licenses/mit-license.php.
|
||||
'''
|
||||
Utility functions for generating C files for testvectors from the Wycheproof project.
|
||||
'''
|
||||
|
||||
def to_c_array(x):
|
||||
if x == "":
|
||||
return ""
|
||||
s = ',0x'.join(a + b for a, b in zip(x[::2], x[1::2]))
|
||||
return "0x" + s
|
||||
Reference in new issue
Block a user