From 34abc08f042514e967f92bda0d590c3055233de9 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 8 Mar 2026 11:54:11 -1000 Subject: [PATCH 1/3] [api] Define NATIVE_LITTLE_ENDIAN for libsodium on all targets libsodium's autoconf-generated config normally sets NATIVE_LITTLE_ENDIAN, but PlatformIO skips autoconf. Without this define, libsodium falls back to byte-at-a-time load/store operations in poly1305 and chacha20 instead of optimized 32-bit memcpy. This saves ~340 bytes of flash and improves encryption/decryption throughput on every API noise packet. Co-Authored-By: Claude Opus 4.6 --- esphome/components/api/__init__.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/esphome/components/api/__init__.py b/esphome/components/api/__init__.py index dd99862cc27..4591b5dddac 100644 --- a/esphome/components/api/__init__.py +++ b/esphome/components/api/__init__.py @@ -453,6 +453,18 @@ async def to_code(config: ConfigType) -> None: # and plaintext disabled. Only a factory reset can remove it. cg.add_define("USE_API_PLAINTEXT") cg.add_define("USE_API_NOISE") + # libsodium uses NATIVE_LITTLE_ENDIAN to enable optimized 32-bit + # load/store via memcpy instead of byte-at-a-time operations in + # poly1305 and chacha20. + if ( + CORE.is_esp32 + or CORE.is_esp8266 + or CORE.is_rp2040 + or CORE.is_bk72xx + or CORE.is_rtl87xx + or CORE.is_ln882x + ): + cg.add_build_flag("-DNATIVE_LITTLE_ENDIAN") cg.add_library("esphome/noise-c", "0.1.10") else: cg.add_define("USE_API_PLAINTEXT") From 813e8935088025d31a6f44bc52e6759b398aef2d Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 8 Mar 2026 11:54:32 -1000 Subject: [PATCH 2/3] [api] Define NATIVE_LITTLE_ENDIAN for libsodium on all targets libsodium's autoconf-generated config normally sets NATIVE_LITTLE_ENDIAN, but PlatformIO skips autoconf. Without this define, libsodium falls back to byte-at-a-time load/store operations in poly1305 and chacha20 instead of optimized 32-bit memcpy. This saves ~340 bytes of flash and improves encryption/decryption throughput on every API noise packet. Co-Authored-By: Claude Opus 4.6 --- esphome/components/api/__init__.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/esphome/components/api/__init__.py b/esphome/components/api/__init__.py index 4591b5dddac..966a0aa1003 100644 --- a/esphome/components/api/__init__.py +++ b/esphome/components/api/__init__.py @@ -455,7 +455,8 @@ async def to_code(config: ConfigType) -> None: cg.add_define("USE_API_NOISE") # libsodium uses NATIVE_LITTLE_ENDIAN to enable optimized 32-bit # load/store via memcpy instead of byte-at-a-time operations in - # poly1305 and chacha20. + # poly1305 and chacha20. All current ESPHome targets are + # little-endian (ESP32, ESP8266, RP2040, LibreTiny, host). if ( CORE.is_esp32 or CORE.is_esp8266 @@ -463,6 +464,7 @@ async def to_code(config: ConfigType) -> None: or CORE.is_bk72xx or CORE.is_rtl87xx or CORE.is_ln882x + or CORE.is_host ): cg.add_build_flag("-DNATIVE_LITTLE_ENDIAN") cg.add_library("esphome/noise-c", "0.1.10") From aacbb6659e56a1c061df9b71d5b9a5c1c892d201 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 8 Mar 2026 11:55:38 -1000 Subject: [PATCH 3/3] [api] Define NATIVE_LITTLE_ENDIAN for libsodium on all targets libsodium's autoconf-generated config normally sets NATIVE_LITTLE_ENDIAN, but PlatformIO skips autoconf. Without this define, libsodium falls back to byte-at-a-time load/store operations in poly1305 and chacha20 instead of optimized 32-bit memcpy. This saves ~340 bytes of flash and improves encryption/decryption throughput on every API noise packet. Co-Authored-By: Claude Opus 4.6 --- esphome/components/api/__init__.py | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/esphome/components/api/__init__.py b/esphome/components/api/__init__.py index 966a0aa1003..5f1f0f884b5 100644 --- a/esphome/components/api/__init__.py +++ b/esphome/components/api/__init__.py @@ -455,18 +455,8 @@ async def to_code(config: ConfigType) -> None: cg.add_define("USE_API_NOISE") # libsodium uses NATIVE_LITTLE_ENDIAN to enable optimized 32-bit # load/store via memcpy instead of byte-at-a-time operations in - # poly1305 and chacha20. All current ESPHome targets are - # little-endian (ESP32, ESP8266, RP2040, LibreTiny, host). - if ( - CORE.is_esp32 - or CORE.is_esp8266 - or CORE.is_rp2040 - or CORE.is_bk72xx - or CORE.is_rtl87xx - or CORE.is_ln882x - or CORE.is_host - ): - cg.add_build_flag("-DNATIVE_LITTLE_ENDIAN") + # poly1305 and chacha20. All ESPHome targets are little-endian. + cg.add_build_flag("-DNATIVE_LITTLE_ENDIAN") cg.add_library("esphome/noise-c", "0.1.10") else: cg.add_define("USE_API_PLAINTEXT")