Skip to content

Commit aa3703c

Browse files
committed
external: add and use secp256k1-zkp directly, remove libwally-core
We currently use secp256k1-zkp as bundled by libwally-core. As we remove libwally-core as dependency, and need to directly include secp256k1-zkp. We use a new branch of our fork rebased on current upstream master, because they added CMake support. This removes another ~6.4kB from the resulting multi binary.
1 parent 30ae684 commit aa3703c

File tree

7 files changed

+23
-75
lines changed

7 files changed

+23
-75
lines changed

.gitmodules

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
[submodule "external/cryptoauthlib"]
22
path = external/cryptoauthlib
33
url = https://github.com/BitBoxSwiss/cryptoauthlib.git
4-
[submodule "external/libwally-core"]
5-
path = external/libwally-core
6-
url = https://github.com/BitBoxSwiss/libwally-core.git
74
[submodule "tools/ttf2ugui"]
85
path = tools/ttf2ugui
96
url = https://github.com/BitBoxSwiss/ttf2ugui
@@ -13,3 +10,6 @@
1310
[submodule "external/embedded-swd"]
1411
path = external/embedded-swd
1512
url = https://github.com/BitBoxSwiss/embedded-swd
13+
[submodule "external/secp256k1-zkp"]
14+
path = external/secp256k1-zkp
15+
url = https://github.com/BitBoxSwiss/secp256k1-zkp.git

external/CMakeLists.txt

Lines changed: 14 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
include(ExternalProject)
22

3+
# Tell `add_library()` to default to STATIC.
4+
# Needed because secp256k1-zkp/CMakeLists.txt sets it to ON.
5+
set(BUILD_SHARED_LIBS OFF)
6+
37
if(CMAKE_CROSSCOMPILING)
48
set(CONFIGURE_FLAGS
59
--host=${CMAKE_SYSTEM_PROCESSOR}-none-eabi
@@ -14,71 +18,19 @@ string(REPLACE "-mfloat-abi=softfp" "" MODIFIED_C_FLAGS_TMP ${CMAKE_C_FLAGS})
1418
string(REPLACE "-mfpu=fpv4-sp-d16" "" MODIFIED_C_FLAGS ${MODIFIED_C_FLAGS_TMP})
1519

1620
#----------------------
17-
# wally-core
18-
19-
# configure flags for secp256k1 bundled in libwally core, to reduce memory consumption
20-
set(LIBWALLY_SECP256k1_FLAGS --with-ecmult-window=2 --with-ecmult-gen-precision=2 --enable-ecmult-static-precomputation --enable-module-schnorrsig --enable-module-ecdsa-adaptor)
21-
set(LIBWALLY_CONFIGURE_FLAGS --enable-static --disable-shared --disable-tests ${LIBWALLY_SECP256k1_FLAGS})
22-
if(SANITIZE_ADDRESS)
23-
set(LIBWALLY_CFLAGS "-fsanitize=address")
24-
endif()
25-
if(SANITIZE_UNDEFINED)
26-
set(LIBWALLY_CFLAGS "${LIBWALLY_CFLAGS} -fsanitize=undefined")
27-
endif()
28-
# _DEFAULT_SOURCE enables the BSD explicit_bzero function referenced by libwally.
29-
set(LIBWALLY_CFLAGS "\
30-
${LIBWALLY_CFLAGS} \
31-
${MODIFIED_C_FLAGS} ${CMAKE_C_FLAGS_${CMAKE_BUILD_TYPE}} \
32-
-D_DEFAULT_SOURCE \
33-
-fno-strict-aliasing \
34-
")
35-
if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
36-
string(APPEND LIBWALLY_CFLAGS " -mmacosx-version-min=${CMAKE_OSX_DEPLOYMENT_TARGET}")
37-
endif()
3821

39-
# Hide some warnings
40-
set(LIBWALLY_CFLAGS "${LIBWALLY_CFLAGS} -Wno-cast-qual -Wno-cast-align \
41-
-Wno-missing-prototypes -Wno-redundant-decls \
42-
-Wno-switch-default -Wno-missing-declarations \
43-
-Wno-array-bounds -Wno-unused-label -Wno-sign-compare -Wno-type-limits \
44-
")
45-
if(CMAKE_CROSSCOMPILING)
46-
set(LIBWALLY_LDFLAGS --specs=nosys.specs)
47-
endif()
48-
set(LIBWALLY_LDFLAGS "${LIBWALLY_LDFLAGS} ${CMAKE_C_LINK_FLAGS}")
22+
## secp256k1-zkp
4923

50-
ExternalProject_Add(libwally-core
51-
PREFIX ${CMAKE_CURRENT_BINARY_DIR}/libwally-core
52-
STEP_TARGETS build-libwally
53-
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libwally-core
54-
CONFIGURE_COMMAND ${CMAKE_COMMAND} -E chdir ${CMAKE_CURRENT_SOURCE_DIR}/libwally-core tools/autogen.sh
55-
COMMAND ${CMAKE_COMMAND} -E env
56-
"CFLAGS=${LIBWALLY_CFLAGS}"
57-
"LDFLAGS=${LIBWALLY_LDFLAGS}"
58-
${CMAKE_CURRENT_SOURCE_DIR}/libwally-core/configure
59-
${CONFIGURE_FLAGS}
60-
${LIBWALLY_CONFIGURE_FLAGS}
61-
INSTALL_COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}
62-
COMMAND ${CMAKE_COMMAND} -E copy
63-
${CMAKE_CURRENT_BINARY_DIR}/libwally-core/src/libwally-core-build/src/.libs/libwallycore.a
64-
${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}/libwallycore.a
65-
COMMAND ${CMAKE_COMMAND} -E copy
66-
${CMAKE_CURRENT_BINARY_DIR}/libwally-core/src/libwally-core-build/src/secp256k1/.libs/libsecp256k1.a
67-
${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}/libsecp256k1.a
24+
# Override
25+
set(SECP256K1_ENABLE_MODULE_RECOVERY ON) # needed only in Rust unit tests.
26+
set(SECP256K1_ECMULT_WINDOW_SIZE 2 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]" FORCE)
27+
set(SECP256K1_ECMULT_GEN_PREC_BITS 2 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]" FORCE)
28+
# Suppress all warnings in this directory, we don't have control over them.
29+
set_directory_properties(PROPERTIES
30+
COMPILE_OPTIONS "-w"
31+
DIRECTORY secp256k1-zkp
6832
)
69-
70-
add_library(wallycore STATIC IMPORTED GLOBAL)
71-
set_property(TARGET wallycore
72-
PROPERTY IMPORTED_LOCATION ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}/libwallycore.a)
73-
set_target_properties(wallycore PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_CURRENT_SOURCE_DIR}/libwally-core/include)
74-
set_target_properties(wallycore PROPERTIES INTERFACE_SYSTEM_INCLUDE_DIRECTORIES ${CMAKE_CURRENT_SOURCE_DIR}/libwally-core/include)
75-
76-
add_library(secp256k1 STATIC IMPORTED GLOBAL)
77-
set_property(TARGET secp256k1
78-
PROPERTY IMPORTED_LOCATION ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}/libsecp256k1.a)
79-
set_target_properties(secp256k1 PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_CURRENT_SOURCE_DIR}/libwally-core/src/secp256k1/include)
80-
set_target_properties(secp256k1 PROPERTIES INTERFACE_SYSTEM_INCLUDE_DIRECTORIES ${CMAKE_CURRENT_SOURCE_DIR}/libwally-core/src/secp256k1/include)
81-
33+
add_subdirectory(secp256k1-zkp)
8234

8335
if(CMAKE_CROSSCOMPILING)
8436
# Cortex Microcontroller Software Interface Standard

external/libwally-core

Lines changed: 0 additions & 1 deletion
This file was deleted.

external/secp256k1-zkp

Submodule secp256k1-zkp added at edc3c3e

src/CMakeLists.txt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ else()
235235
set(RUSTFLAGS "--remap-path-prefix=${CMAKE_CURRENT_SOURCE_DIR}/rust=src --remap-path-prefix=$ENV{HOME}=")
236236
endif()
237237

238-
# Use libsecp256k1 that we link ourselves (included via external/libwally-core).
238+
# Use libsecp256k1 that we link ourselves.
239239
# See https://github.com/rust-bitcoin/rust-secp256k1/tree/7c8270a8506e31731e540fab7ee1abde1f48314e/secp256k1-sys#linking-to-external-symbols
240240
set(RUSTFLAGS "${RUSTFLAGS} --cfg=rust_secp_no_symbol_renaming")
241241

@@ -277,7 +277,7 @@ add_custom_target(rust-cbindgen
277277

278278
# Test rust crates that contain business logic. Avoid testing crates that depend on hardware.
279279
if(NOT CMAKE_CROSSCOMPILING)
280-
set(RUSTFLAGS_TESTS ${RUSTFLAGS} -L${CMAKE_ARCHIVE_OUTPUT_DIRECTORY} -lbitbox_merged -lwallycore -lsecp256k1 -lfatfs -lhardware-fakes)
280+
set(RUSTFLAGS_TESTS ${RUSTFLAGS} -L${CMAKE_ARCHIVE_OUTPUT_DIRECTORY} -lbitbox_merged -lsecp256k1 -lfatfs -lhardware-fakes)
281281

282282
# Since we build with all features we need to use a separate build directory.
283283
# Otherwise we invalidate the result from the normal compilation that uses a
@@ -293,7 +293,7 @@ if(NOT CMAKE_CROSSCOMPILING)
293293
${CARGO} test $<$<BOOL:${CMAKE_VERBOSE_MAKEFILE}>:-v> --all-features --target-dir ${RUST_BINARY_DIR}/all-features ${RUST_CARGO_FLAGS} -- --nocapture --test-threads 1
294294
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/rust/
295295
)
296-
add_dependencies(rust-test generate-protobufs bitbox_merged libwally-core fatfs)
296+
add_dependencies(rust-test generate-protobufs bitbox_merged fatfs)
297297

298298
add_custom_target(rust-clippy
299299
COMMAND
@@ -520,8 +520,7 @@ if(CMAKE_CROSSCOMPILING)
520520
target_link_libraries(${elf} PRIVATE "-Wl,-Map=\"${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${firmware}.map\" -T\"${CMAKE_SOURCE_DIR}/firmware.ld\"")
521521
target_link_libraries(${elf} PRIVATE -Wl,--defsym=STACK_SIZE=${STACK_SIZE} -Wl,-defsym=HEAP_SIZE=${HEAP_SIZE})
522522

523-
add_dependencies(${elf} libwally-core)
524-
target_link_libraries(${elf} PRIVATE wallycore secp256k1)
523+
target_link_libraries(${elf} PRIVATE secp256k1)
525524
target_link_libraries(${elf} PRIVATE ${QTOUCHLIB_A} ${QTOUCHLIB_B} ${QTOUCHLIB_T})
526525

527526
# Select the smaller version of libc called nano.

src/rust/bitbox02-sys/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ pub fn main() -> Result<(), &'static str> {
213213
"../../usb/class/hid/hww",
214214
"../../usb/class/hid/u2f",
215215
// $SECP256k1_INCLUDES
216-
"../../../external/libwally-core/src/secp256k1/include",
216+
"../../../external/secp256k1-zkp/include",
217217
];
218218

219219
if cross_compiling {

test/hardware-fakes/CMakeLists.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,8 @@ target_include_directories(
5555
${INCLUDES}
5656
${CMAKE_BINARY_DIR}/src
5757
$<TARGET_PROPERTY:fatfs,INTERFACE_INCLUDE_DIRECTORIES>
58-
$<TARGET_PROPERTY:wallycore,INTERFACE_INCLUDE_DIRECTORIES>
5958
)
6059

61-
target_link_libraries(hardware-fakes PUBLIC wallycore)
62-
6360
if(SANITIZE_ADDRESS)
6461
target_compile_options(hardware-fakes PUBLIC "-fsanitize=address")
6562
endif()

0 commit comments

Comments
 (0)