# PQC Stack Memory Footprint Comparison

**Source**: https://quantumsequrity.com/blog/pqc-stack-memory-analysis
**Category**: Hardware & Embedded

---

[← Back to Blog](../../blog) Hardware & Embedded

# PQC Stack Memory Footprint Comparison

11 min read

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. ML-KEM uses 3-4 KB. ML-DSA uses 10-17 KB. SLH-DSA uses 7-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: 3-4 KB

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: ~2700 bytes
- ML-KEM-512 encapsulate: ~2700 bytes
- ML-KEM-512 decapsulate: ~2900 bytes
- ML-KEM-768 keygen: ~3000 bytes
- ML-KEM-768 encapsulate: ~3100 bytes
- ML-KEM-768 decapsulate: ~3300 bytes
- ML-KEM-1024 keygen: ~3300 bytes
- ML-KEM-1024 encapsulate: ~3400 bytes
- ML-KEM-1024 decapsulate: ~3700 bytes

These numbers are with stack-optimized implementations. Some general-purpose libraries use 50-100 percent more.

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](../../blog/ml-kem-explained) article.

## ML-DSA Stack: 10-17 KB

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.

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

- ML-DSA-44 keygen: ~9000 bytes
- ML-DSA-44 sign: ~10500 bytes
- ML-DSA-44 verify: ~9500 bytes
- ML-DSA-65 keygen: ~12500 bytes
- ML-DSA-65 sign: ~13700 bytes
- ML-DSA-65 verify: ~13000 bytes
- ML-DSA-87 keygen: ~16000 bytes
- ML-DSA-87 sign: ~17500 bytes
- ML-DSA-87 verify: ~16500 bytes

The signing operation is the heaviest because rejection sampling holds rejected polynomials in stack while generating new ones. Verification is closer to keygen since it does not have the rejection-sampling overhead.

These numbers can be reduced by 20-30 percent with aggressive memory-aware implementations that re-use stack space between phases, but at some cost in code complexity. For most embedded designs, the standard pqm4 figures are a reasonable budget.

For a chip with 32 KB of SRAM, fitting ML-DSA-44 alongside the rest of the system is achievable but tight. ML-DSA-65 needs at least 64 KB of SRAM total, comfortably 128 KB. ML-DSA-87 needs 128 KB of SRAM minimum, comfortably 256 KB.

For more on platform-specific stack constraints, see our [PQC on ARM Cortex-M](pqc-on-arm-cortex-m.md) and [PQC on RISC-V](pqc-on-risc-v.md) articles.

## SLH-DSA Stack: ~7 KB (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.

Approximate stack usage on Cortex-M4 for SLH-DSA-128s:

- Keygen: ~6500 bytes
- Sign: ~7000 bytes
- Verify: ~6500 bytes

The "f" (fast) variants have similar stack but very different runtime profiles. SLH-DSA-128f signing uses about 7 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. SLH-DSA-128s signing takes about 56 seconds on a Cortex-M4 at 168 MHz. ML-DSA-44 signing takes about 25 ms median. 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](../../blog/mldsa-vs-slhdsa) 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](../../blog/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 4 KB of stack) 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](pqc-on-iot-mcu.md) 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 really. The algorithm requires holding multiple polynomials simultaneously. Implementations can do better than the naive 14-17 KB, but the lower bound is 8-10 KB. ML-DSA is fundamentally heavier than ML-KEM on stack.

**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

- [What is Post-Quantum Cryptography](../../blog/what-is-post-quantum-cryptography)
- [ML-KEM Explained](../../blog/ml-kem-explained)
- [ML-DSA vs SLH-DSA](../../blog/mldsa-vs-slhdsa)
- [Hybrid Encryption](../../blog/hybrid-encryption)
- [PQC IoT Smart Home Devices](../../blog/pqc-iot-smart-home-devices)

---

### 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](../../pricing)
