nalloc: ZK-Optimized Memory Allocator
A high-performance, deterministic, and security-hardened memory allocator specifically engineered for Zero-Knowledge Proof (ZKP) systems and cryptographic provers.
Why nalloc?
General-purpose allocators (malloc, jemalloc) are designed for long-lived, heterogeneous workloads. ZK provers, however, exhibit extreme memory patterns: massive short-lived vectors, sensitive witness data, and performance-critical FFT/NTT operations.
nalloc addresses these unique requirements:
- Performance: O(1) allocation via Atomic Bump Allocation.
- Cache-Friendliness: Guaranteed 64-byte alignment (AVX-512/SIMD optimal) for polynomials.
- Security: Hardened volatile wiping of witness data to prevent leakage.
- Framework-Agnostic: Works with any ZK system — no external ZK dependencies.
- Production-Ready: Panic-free error handling with graceful fallback.
v0.2.3 Features
🛡️ Industrial-Grade Improvements
| Feature | Description |
|---|---|
| Drop Safety | NAlloc properly frees arena memory on drop (no leaks) |
| Panic-Free | Gracefully falls back to system allocator on errors |
| Volatile Zeroing | Recycled witness memory zeroed with write_volatile (DSE-safe) |
| Fallback Allocator | Continues working when arena exhausted |
| Huge Pages | 2MB/1GB huge page support (Linux) |
| Guard Pages | Buffer overflow protection at arena boundaries |
| Memory Locking | Prevent witness data from being swapped |
Cargo Features
[]
= { = "0.2.3", = ["fallback"] }
| Feature | Description | Default |
|---|---|---|
fallback |
Fall back to system allocator when arena exhausted | ✅ |
huge-pages |
Linux 2MB/1GB huge page support | Optional |
guard-pages |
Guard pages at arena boundaries (Linux) | Optional |
mlock |
Lock witness memory to prevent swapping | Optional |
Supported ZK Systems
nalloc is a pure memory primitive designed to work with any ZK framework:
| Framework | Use Case |
|---|---|
| Halo2 | Plonkish circuits, recursive proofs |
| Plonky2 | Fast recursive STARKs |
| Risc0 | zkVM execution |
| SP1 | Succinct zkVM |
| Miden | STARK-based VM |
| Cairo | StarkNet proofs |
| Circom/SnarkJS | Groth16, PLONK circuits |
| Arkworks | General-purpose ZK toolkit |
Architecture: Specialized Arenas
nalloc partitions memory into three specialized pools to eliminate fragmentation and enforce security boundaries:
| Arena | Purpose | Optimization | Security |
|---|---|---|---|
| Witness | Secret inputs / Witnesses | Zero-on-recycled-alloc (volatile) | Secure Wipe (platform-specific) |
| Polynomial | FFT / NTT Vectors | 64-byte & Page Alignment | Isolated from scratch |
| Scratch | Temp computation space | High-speed bump allocation | O(1) Batch Reset |
Core Features
1. Hardened Witness Security
Witness data is handled with extreme caution. The secure_wipe() method uses platform-specific primitives that the compiler cannot optimize away:
- Linux:
explicit_bzero - macOS:
memset_s - Windows:
RtlSecureZeroMemory - Fallback: Volatile write loops with
SeqCstmemory fences.
Recycled witness memory is also zeroed with volatile writes in WitnessArena::alloc, preventing Dead-Store Elimination from silently skipping the zero.
2. Framework-Agnostic Design
nalloc has zero external ZK dependencies. It provides pure memory primitives that any proving system can utilize. Lock-free AtomicPtr initialization prevents recursive allocation deadlocks during prover startup.
3. Graceful Error Handling
use NAlloc;
// Fallible initialization
match try_new
// Or use the auto-fallback version
let alloc = new; // Falls back to system allocator if needed
if alloc.is_fallback_mode
4. Monitoring & Stats
Easily track your circuit's memory footprint:
let stats = alloc.stats.expect;
println!;
println!;
println!;
Usage
As a Global Allocator
Add to your Cargo.toml:
[]
= "0.2.3"
In your main.rs or lib.rs:
use NAlloc;
static ALLOC: NAlloc = new;
Manual Arena Control
For maximum performance, access arenas directly:
use NAlloc;
With Guard Pages (Security)
use ArenaManager;
// Allocate with guard pages for buffer overflow detection
let manager = with_guard_pages?;
With Memory Locking (Anti-Swap)
Platform Support
nalloc provides cross-platform abstractions for low-level memory management:
- macOS:
mach_vm_allocate/mach_vm_deallocate - Linux:
mmap/munmap(viarustix) with huge page support - Windows:
VirtualAlloc/VirtualFreewith guard pages
Performance Benchmark
| Task | System Alloc | nalloc | Speedup |
|---|---|---|---|
| 10k Small Allocs | ~150 μs | ~50 μs | 3x |
| Large FFT Vector | ~10 μs | ~8 μs | 1.2x |
| Batch Dealloc | O(N) | O(1) | ∞ |
Support the Project
zk-nalloc is MIT/Apache-2.0 licensed and free for everyone to use. If it saves you time or improves your prover's performance, consider supporting ongoing development:
- ⭐ Star the repo — helps other ZK developers discover the library
- 💖 GitHub Sponsors — fund new features, platform ports, and maintenance
- 🐛 Open an issue — bug reports and feature requests are always welcome
- 🔧 Submit a PR — contributions for new platform backends, benchmarks, or integrations are very welcome
ZK Ecosystem Grants
If you work at a ZK protocol foundation and rely on nalloc, consider sponsoring development through your ecosystem grant program (Ethereum Foundation, StarkWare, Risc0, Aztec, zkSync, etc.).
License
Licensed under either of:
- MIT License (LICENSE-MIT)
- Apache License, Version 2.0 (LICENSE-APACHE)
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this project shall be dual-licensed as above, without any additional terms or conditions.