← Back to Blog

PQC Stack Memory Footprint Comparison

PQC Stack Memory Footprint Comparison - QNSQY post-quantum encryption guide

When you put cryptography on a small microcontroller, two memory budgets matter most: flash (where the code lives) and stack (where the working set of each operation lives). Heap is a third concern, but most embedded crypto libraries avoid heap entirely to make memory deterministic. Stack is where the cryptographic working set lives during a function call: polynomials, intermediate values, hash states, sampled coefficients, all of it.

Post-quantum cryptography uses more stack than the elliptic-curve cryptography it is replacing. ECC fits in a kilobyte or two of stack. In pqm4's stack-optimised Cortex-M4 builds, ML-KEM uses about 2.3 to 3.4 KB and ML-DSA about 2.7 to 8.1 KB; other builds use far more. SLH-DSA's portable build uses about 1.9 to 8.8 KB but has its own runtime issues. These differences matter on devices where total RAM is 16 KB or 32 KB, because the stack budget for crypto is a significant fraction of all available RAM.

This article walks through the stack memory footprint of the major post-quantum schemes, where the memory goes, and how implementations differ. The numbers come primarily from pqm4 (the ARM Cortex-M4 PQC benchmarking project at github.com/mupq/pqm4), which is the most widely-cited source for embedded PQC measurements.

Why Stack Matters

In an embedded system, you allocate stack for each task at compile time or boot time. If a function uses more stack than its task has allocated, you get a stack overflow, which usually means a hard crash. Embedded developers typically pick stack sizes per task and verify that no function in that task ever uses more.

For a TLS handshake task on a 32 KB SRAM device, you might allocate 8 KB to the TLS state machine, 8 KB to the application code, and reserve 16 KB for the BLE stack and OS. If the TLS handshake calls into ML-DSA-44 signing, which uses 10 KB of stack, you have a problem: the call exceeds the budget.

Workarounds exist (move buffers off-stack into static memory, share buffers between tasks, use larger task stacks), but they all complicate the design. The cleanest approach is to know the stack requirements ahead of time and design around them.

ECC Baseline: Sub-Kilobyte

Elliptic-curve cryptography (X25519, Ed25519, ECDSA on secp256k1) typically uses 1-2 KB of stack per operation on Cortex-M4. The biggest contributors are:

  • Field element arrays (32-bytes each, several at a time)
  • Scalar multiplication intermediate values
  • Point arithmetic accumulators

A single X25519 key agreement uses about 600 bytes of stack with a typical implementation. An Ed25519 sign operation uses about 1 KB. Compared to PQC, ECC is essentially free on stack.

This is one reason ECC has dominated embedded cryptography for years. Even the smallest microcontrollers can fit ECC, and the protocols (ECDH, ECDSA) have minimal protocol overhead.

ML-KEM Stack: 2.3 to 3.4 KB When Optimised for Stack

ML-KEM (FIPS 203, formerly Kyber) uses lattice-based cryptography. The working set during an operation includes several polynomials with 256 coefficients each, where each coefficient is a 16-bit integer modulo q (q is 3329 for ML-KEM). A polynomial is 512 bytes; ML-KEM operations work with 3-9 polynomials at a time.

Approximate stack usage on Cortex-M4 with optimized assembly (pqm4 numbers):

  • ML-KEM-512 keygen: 2,300 bytes
  • ML-KEM-512 encapsulate: 2,348 bytes
  • ML-KEM-512 decapsulate: 2,332 bytes
  • ML-KEM-768 keygen: 2,820 bytes
  • ML-KEM-768 encapsulate: 2,860 bytes
  • ML-KEM-768 decapsulate: 2,844 bytes
  • ML-KEM-1024 keygen: 3,332 bytes
  • ML-KEM-1024 encapsulate: 3,372 bytes
  • ML-KEM-1024 decapsulate: 3,356 bytes

These are pqm4's stack-optimised (m4fstack) figures. Its speed-optimised build uses about 7.5 KB for ML-KEM-1024 decapsulation, and its portable reference build about 20 KB.

The stack scales mainly with the polynomial count and the parameter k (which determines the lattice dimension). ML-KEM-512 has k=2; ML-KEM-768 has k=3; ML-KEM-1024 has k=4. Each step up adds polynomials and a bit of stack.

For background on the algorithm, see our ML-KEM explained article.

ML-DSA Stack: 2.7 to 8.1 KB When Optimised for Stack, Far More Otherwise

ML-DSA (FIPS 204, formerly Dilithium) is much heavier on stack. The algorithm involves more polynomials (some of them larger than ML-KEM's), more intermediate values, and uses rejection sampling that requires temporary storage for failed samples.

Stack usage on Cortex-M4 with pqm4's stack-optimised build (m4fstack):

  • ML-DSA-44 keygen: 4,408 bytes
  • ML-DSA-44 sign: 5,080 bytes
  • ML-DSA-44 verify: 2,712 bytes
  • ML-DSA-65 keygen: 4,408 bytes
  • ML-DSA-65 sign: 6,616 bytes
  • ML-DSA-65 verify: 2,712 bytes
  • ML-DSA-87 keygen: 4,408 bytes
  • ML-DSA-87 sign: 8,144 bytes
  • ML-DSA-87 verify: 2,720 bytes

The signing operation is the heaviest because rejection sampling holds rejected polynomials in stack while generating new ones. Verification is the lightest of the three.

Without stack optimisation the numbers are far larger: pqm4's speed-optimised build uses about 45 KB to sign with ML-DSA-44 and about 108 KB with ML-DSA-87, and its portable reference build about 52 KB and 123 KB. The stack-optimised build pays for this in signing time: about 12.1 million cycles for ML-DSA-44 against 3.9 million. For most embedded designs, the standard pqm4 figures are a reasonable budget.

For a chip with 32 KB of SRAM, stack-optimised ML-DSA fits alongside the rest of the system, at a cost in signing time. pqm4's speed-optimised signing does not fit there: it needs about 45 KB for ML-DSA-44 alone.

For more on platform-specific stack constraints, see our PQC on ARM Cortex-M and PQC on RISC-V articles.

SLH-DSA Stack: 2 to 3 KB at the 128-bit Level (But Watch the Time)

SLH-DSA (FIPS 205, formerly SPHINCS+) is hash-based. Its working set is dominated by hash state and Merkle tree path data, not polynomial arithmetic.

Stack usage on Cortex-M4 for SPHINCS+-SHA2-128s in pqm4's portable build (SLH-DSA is the FIPS 205 version of SPHINCS+):

  • Keygen: 3,084 bytes
  • Sign: 2,556 bytes
  • Verify: 1,968 bytes

The "f" (fast) variants have similar stack but very different runtime profiles. SPHINCS+-SHA2-128f signing uses about 2.5 KB of stack but runs much faster than SLH-DSA-128s (at the cost of larger signatures).

The SLH-DSA stack number is smaller than ML-DSA's but the runtime is dramatically worse. SPHINCS+-SHA2-128s signing takes about 7.7 billion cycles on a Cortex-M4 in pqm4, or about 46 seconds at 168 MHz. ML-DSA-44 signing takes about 3.9 million cycles, or about 23 ms at 168 MHz. The signing time is more than 1000x worse for SLH-DSA than ML-DSA. So while the stack is friendlier, the time is essentially unusable for runtime signing on most embedded systems.

For comparison between the two, see our ML-DSA versus SLH-DSA article.

LMS/HSS Stack: Smaller Stack, Stateful

LMS and HSS are stateful hash-based signatures specified in NIST SP 800-208. They have smaller stack requirements than SLH-DSA (typically 2-4 KB) and faster operations, but they are stateful: you must track which one-time keys you have used and never reuse them.

For embedded systems where you need a hash-based signature with reasonable performance, LMS is the right choice. The state management is the catch: you need persistent storage for the LMS state, and you must update it atomically before each signature so that a power loss does not let you reuse a key.

Optimization Patterns: Where the Stack Goes

Reducing stack in PQC implementations involves a few patterns:

Re-using buffer space: temporary polynomials in early phases get overwritten by later phases. The pqm4 implementations do this aggressively. The cost is code complexity.

Streaming the NTT: instead of holding the full polynomial in stack, NTT operations can be streamed: read coefficient by coefficient, transform, write back. This trades cycles for stack.

Off-stack workspace: allocate large polynomial buffers in static memory or heap, pass pointers to the function. Stack only holds local variables. The trade-off is that the buffer is permanently allocated, even when crypto is not running.

Merging phases: ML-DSA's signing has phases that can be combined to reduce intermediate storage. Some optimized implementations do this.

For high-stakes embedded designs (medical, automotive, aerospace), the trade-off is usually toward conservative stack use with off-stack workspace. The application can audit static memory usage at compile time and verify the system fits the device's RAM budget.

Cross-Architecture Variations

Stack usage varies by architecture and implementation. Cortex-M4 with DSP extensions is the most-studied target. The same code on Cortex-M0 (no DSP, smaller register file) can use 10-30 percent more stack because more values get spilled to the stack instead of staying in registers.

RISC-V with 32 GPRs (the standard register set) usually has slightly less spilled stack than Cortex-M4 (which has 16 GPRs), since more values stay in registers. The difference is small (a few hundred bytes per operation).

x86-64 implementations with optimized assembly use more stack in absolute terms (because of larger SIMD register state) but the systems running them have gigabytes of RAM, so it does not matter.

For 64-bit ARM (Cortex-A53, Cortex-A76), the stack usage is similar to x86-64: SIMD-optimized code uses more stack but is running on systems with substantial RAM.

Practical Budgeting

For embedded designs that need PQC, here is a practical stack budget guide:

  • Very small device (16-32 KB SRAM): ML-KEM-512 only. ML-DSA is too heavy. Use ECC for signatures or upgrade the chip.
  • Small device (32-64 KB SRAM): ML-KEM-768 plus ML-DSA-44. Tight but workable.
  • Medium device (64-128 KB SRAM): ML-KEM-1024 plus ML-DSA-65. Comfortable with TLS.
  • Large device (128+ KB SRAM): ML-DSA-87 fits, plus everything else.
  • Server/desktop: not memory constrained. Pick parameters by security level.

If you need stronger PQC than ML-KEM-512 / ML-DSA-44 on a small device, the answer is usually to move to a bigger chip rather than trying to squeeze a heavier algorithm into too little RAM. Adding 32 KB of SRAM to a chip is much cheaper than spending months optimizing crypto for tight stack.

For background on hybrid encryption, see our hybrid encryption explainer.

What This Means for QNSQY

QNSQY runs on x86-64 desktop and server platforms with Linux or Windows. Stack memory is not a meaningful constraint there. Even its largest parameter sets (ML-KEM-1024 plus X25519, ML-DSA-87 plus Ed25519) use negligible RAM on a modern computer.

Embedded stack budgets matter when a small device decrypts data itself, for example a device receiving a firmware update; that device-side code is a PQC library on the device, not QNSQY. The on-device decryption uses ML-KEM-1024 decapsulate (about 3.4 KB of stack with pqm4's stack-optimised build) plus AES-256-GCM (a few hundred bytes), which fits comfortably on any chip with 32 KB or more of SRAM.

So the typical IoT pattern works well: do the encryption work on the build server (no stack constraints) and let the embedded device do the lighter decryption work with its own PQC library.

For more on the IoT use case, see our PQC on IoT microcontrollers article.

FAQ

Why does ML-DSA use so much more stack than ML-KEM? ML-DSA's signing involves rejection sampling: keep generating polynomials until one passes a norm check. The rejected polynomials sit in stack while new ones are generated. ML-KEM does not have this; its operations are deterministic and use fewer simultaneous polynomials.

Can I shrink ML-DSA stack to match ML-KEM? Not at signing. The algorithm requires holding multiple polynomials simultaneously. pqm4's stack-optimised build gets ML-DSA signing down to about 5 to 8 KB, against about 3.4 KB at most for ML-KEM, and it pays for that in signing time.

Should I use ML-KEM for signatures to save stack? ML-KEM is a key encapsulation mechanism, not a signature scheme. It cannot replace ML-DSA. If your device cannot fit ML-DSA, the alternatives are ECC signatures (small but quantum-vulnerable), LMS (small but stateful), or moving to a chip with more RAM.

What about heap? Most embedded PQC libraries avoid heap entirely. They use stack and static memory. This makes memory budgeting deterministic and avoids fragmentation issues. If you see a PQC library that uses heap, evaluate its memory predictability carefully.

How do I measure stack usage on my own MCU? Compile with stack analysis flags (GCC's -fstack-usage produces .su files reporting per-function stack usage). Sum the maximum call chain through your PQC library. For paranoid measurements, fill the stack with a known pattern at boot and measure how far the watermark moves after running each operation.

Sources

  1. NIST FIPS 203 (ML-KEM): https://csrc.nist.gov/pubs/fips/203/final
  2. NIST FIPS 204 (ML-DSA): https://csrc.nist.gov/pubs/fips/204/final
  3. NIST FIPS 205 (SLH-DSA): https://csrc.nist.gov/pubs/fips/205/final
  4. NIST SP 800-208 (Stateful Hash-Based Signatures): https://csrc.nist.gov/pubs/sp/800/208/final
  5. ARM Cortex-M Series Documentation: https://developer.arm.com/Processors/Cortex-M
  6. RISC-V International Specifications: https://riscv.org/technical/specifications/

Related Articles

Protect Your Data Before Q-Day Arrives

QNSQY's NIST-standardized post-quantum encryption protects files against both current and quantum-era threats.

Try QNSQY