Skip to content

Commit 496d087

Browse files
committed
build: include wally in the firmware directly when amalgamating
In many cases we check/assert our arguments before calling wally functions. By amalgamating wally directly, the compiler can remove code that redundantly re-checks already asserted conditions. In some cases further optimizations are possible. For example explicit rangeproofs do not contain a side channel message and thus the secp code to generate it (and the 4k stack buffer it uses) can be eliminated. We perform the wally amalgamation by compiling the external library with no code, and then including the wally sources in the main firmware directly. This approach lets us avoid trying to selectively disable the wally component during amalgamated builds, as this is not supported by the idf.py build machinery.
1 parent e9e000b commit 496d087

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

components/libwally-core/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ set_source_files_properties(
2929
"-Wno-nonnull-compare -Wno-unused-function -Wno-error=maybe-uninitialized -Wno-type-limits -Wno-error=array-parameter"
3030
)
3131

32+
if(CONFIG_AMALGAMATED_BUILD)
33+
target_compile_definitions(${COMPONENT_TARGET} PRIVATE "-DWALLY_NO_AMALGAMATION=1")
34+
endif()
35+
3236
# fortify/performance flags
3337
target_compile_definitions(${COMPONENT_TARGET} PRIVATE NDEBUG=1)
3438
target_compile_options(${COMPONENT_TARGET} PRIVATE "-fstack-protector-strong")

main/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ else()
3131
list(APPEND logo_files ${PROJECT_DIR}/logo/icon_qrguide_qvga_large.bin.gz ${PROJECT_DIR}/logo/icon_qrguide_qvga_small.bin.gz)
3232
endif()
3333

34+
if(CONFIG_AMALGAMATED_BUILD)
35+
set(wallydir "${PROJECT_DIR}/components/libwally-core")
36+
list(APPEND wallydirs "${wallydir}" "${wallydir}/upstream" "${wallydir}/upstream/src" "${wallydir}/upstream/src/ccan")
37+
set(secpdir "${wallydir}/upstream/src/secp256k1/include")
38+
endif()
39+
40+
3441
idf_component_register(SRC_DIRS "."
3542
"process"
3643
"utils"
@@ -45,6 +52,8 @@ idf_component_register(SRC_DIRS "."
4552
"${qemudir}"
4653
"${attestdir}"
4754
"${usbdir}"
55+
"${wallydirs}"
56+
"${secpdir}"
4857
PRIV_REQUIRES assets libwally-core libsodium esp32-rotary-encoder esp32-quirc bootloader_support app_update nvs_flash bt autogenlang cbor esp_netif esp32_bsdiff esp32_deflate nghttp esp32_bc-ur driver mbedtls http_parser esp_hw_support efuse esp_eth esp_http_server esp_lcd usb vfs app_trace spi_flash
4958
EMBED_FILES ${PROJECT_DIR}/pinserver_public_key.pub ${logo_files} ${qemu_display_file})
5059

main/amalgamated.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,27 @@
11
#ifdef AMALGAMATED_BUILD
22
#undef AMALGAMATED_BUILD
3+
34
#include <stdlib.h>
5+
void __wrap_abort(void);
46
#define abort __wrap_abort
7+
8+
#define BUILD_ELEMENTS 1
9+
#define BUILD_MINIMAL 1
10+
#define HAVE_MBEDTLS_SHA256_H
11+
#define HAVE_MBEDTLS_SHA512_H
12+
#define ECMULT_WINDOW_SIZE 8
13+
#define ENABLE_MODULE_ECDH 1
14+
#define ENABLE_MODULE_ECDSA_S2C 1
15+
#define ENABLE_MODULE_EXTRAKEYS 1
16+
#define ENABLE_MODULE_GENERATOR 1
17+
#define ENABLE_MODULE_RANGEPROOF 1
18+
#define ENABLE_MODULE_RECOVERY 1
19+
#define ENABLE_MODULE_SCHNORRSIG 1
20+
#define ENABLE_MODULE_SURJECTIONPROOF 1
21+
#define ENABLE_MODULE_WHITELIST 1
22+
#define HAVE_BUILTIN_POPCOUNT 1
23+
#include "../components/libwally-core/upstream/src/amalgamation/combined.c"
24+
525
#include "./aes.c"
626
#include "./assets.c"
727
#ifdef CONFIG_IDF_TARGET_ESP32S3

0 commit comments

Comments
 (0)