Skip to main content

Crate zk_nalloc

Crate zk_nalloc 

Source
Expand description

nalloc: A ZK-Proof optimized memory allocator.

This crate provides a high-performance, deterministic memory allocator specifically designed for Zero-Knowledge proof systems. It is framework-agnostic and works with any ZK system: Halo2, Plonky2, Risc0, SP1, Miden, Cairo, Arkworks, etc.

§Features

  • Arena-based allocation: Pre-reserved memory pools for different workload types
  • Bump allocation: O(1) allocation via atomic pointer increment
  • Security-first: Volatile secure wiping for witness data
  • Cache-optimized: 64-byte alignment for FFT/NTT SIMD operations
  • Cross-platform: Linux, macOS, Windows, and Unix support
  • Zero ZK dependencies: Pure memory primitive, no framework lock-in
  • Fallback support: Gracefully falls back to system allocator when arena exhausted

§Cargo Features

  • fallback (default): Fall back to system allocator when arena is exhausted
  • huge-pages: Enable Linux 2MB/1GB huge page support
  • guard-pages: Add guard pages at arena boundaries for overflow detection
  • mlock: Lock witness memory to prevent swapping (security)

§Usage

As a global allocator:

use zk_nalloc::NAlloc;

#[global_allocator]
static ALLOC: NAlloc = NAlloc::new();

fn main() {
    let data = vec![0u64; 1000];
    println!("Allocated {} elements", data.len());
}

Using specialized arenas directly:

use zk_nalloc::NAlloc;

let alloc = NAlloc::new();
let witness = alloc.witness();
let ptr = witness.alloc(1024, 8);
assert!(!ptr.is_null());

// Securely wipe when done
unsafe { witness.secure_wipe(); }

Re-exports§

pub use arena::ArenaManager;
pub use arena::ArenaStats;
pub use bump::BumpAlloc;
pub use platform::sys;
pub use platform::GuardedAlloc;
pub use platform::HugePageSize;
pub use platform::AllocErrorKind;
pub use platform::AllocFailed;
pub use polynomial::PolynomialArena;
pub use witness::WitnessArena;
pub use config::*;

Modules§

arena
Arena Manager for nalloc.
bump
Core bump allocator for nalloc.
config
Configuration constants for nalloc.
platform
Platform-specific memory allocation interface.
polynomial
Polynomial Arena for nalloc.
witness
Witness Arena for nalloc.

Structs§

NAlloc
The global ZK-optimized allocator.