Skip to content

Deduplicate crypto libraries, saving ~49KB flash#1632

Open
ViezeVingertjes wants to merge 2 commits intomeshcore-dev:devfrom
ViezeVingertjes:reduce-crypto-duplication
Open

Deduplicate crypto libraries, saving ~49KB flash#1632
ViezeVingertjes wants to merge 2 commits intomeshcore-dev:devfrom
ViezeVingertjes:reduce-crypto-duplication

Conversation

@ViezeVingertjes
Copy link
Contributor

@ViezeVingertjes ViezeVingertjes commented Feb 8, 2026

Fixes a stack overflow in ed25519_verify() by making large local variables static, removing the need for rweather's Ed25519::verify() workaround.

Only vendors the AES128+SHA256 subset of rweather/Crypto instead of all 37 files.

No issues observed so far. Additional testing and confirmation are welcome. This change frees up stack space for future improvements and improves support for resource-limited devices.

Fixes ed25519_verify() stack overflow by making large locals static,
removing the need for rweather's Ed25519::verify() workaround. Vendors
only the AES128+SHA256 subset of rweather/Crypto instead of all 37 files.
@ViezeVingertjes ViezeVingertjes marked this pull request as ready for review February 8, 2026 15:36
Copy link
Contributor

@weebl2000 weebl2000 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice improvement — vendoring the minimal subset is clean and the stack savings are real (~1.8KB moved to BSS). One concern about the static locals in ge.c.

Comment on lines +68 to +70
static signed char aslide[256];
static signed char bslide[256];
static ge_cached Ai[8]; /* A,3A,5A,7A,9A,11A,13A,15A */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Making these static saves ~1.8KB of stack which fixes the overflow, but it also makes ge_double_scalarmult_vartime() non-reentrant. If ed25519_verify() is ever called concurrently from multiple FreeRTOS tasks, these shared buffers will be silently corrupted.

Currently this looks safe — Identity::verify() is only called from the packet receive path in Mesh.cpp — but it's worth documenting so future maintainers don't get bitten.

Suggested change
static signed char aslide[256];
static signed char bslide[256];
static ge_cached Ai[8]; /* A,3A,5A,7A,9A,11A,13A,15A */
static signed char aslide[256]; /* static: saves ~1.8KB stack; NOT reentrant */
static signed char bslide[256];
static ge_cached Ai[8]; /* A,3A,5A,7A,9A,11A,13A,15A */

Copy link
Contributor Author

@ViezeVingertjes ViezeVingertjes Feb 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you thnk about an Assert? just in case its not read... (guilty of it sometimes)

48019bf

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, the assert makes the constraint very visible. Two minor notes:

  1. assert() is typically compiled out in release builds (NDEBUG), so this is really a development-time guard. That's fine for this purpose — the comment does the heavy lifting for future readers.
  2. The static volatile bool check + set isn't atomic, so there's a theoretical TOCTOU race. In practice this doesn't matter — ESP32 FreeRTOS tasks on a single core won't preempt mid-statement, and even on dual-core the window is vanishingly small. Just mentioning it for completeness.

Overall this looks good to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants