Skip to main content

zk_nalloc/
config.rs

1//! Configuration constants for nalloc.
2//!
3//! This module centralizes all tunable parameters and magic numbers
4//! to make the allocator easily configurable.
5
6// ============================================================================
7// Arena Sizes
8// ============================================================================
9
10/// Size of the Witness Arena in bytes.
11/// Used for private ZK inputs requiring secure wiping.
12pub const WITNESS_ARENA_SIZE: usize = 128 * 1024 * 1024; // 128 MB
13
14/// Size of the Polynomial Arena in bytes.
15/// Used for FFT/NTT coefficient vectors - needs to be large for complex circuits.
16pub const POLY_ARENA_SIZE: usize = 1024 * 1024 * 1024; // 1 GB
17
18/// Size of the Scratch Arena in bytes.
19/// Used for temporary computation buffers.
20pub const SCRATCH_ARENA_SIZE: usize = 256 * 1024 * 1024; // 256 MB
21
22// ============================================================================
23// Allocation Thresholds
24// ============================================================================
25
26/// Allocations larger than this threshold go to the Polynomial Arena.
27/// Smaller allocations go to the Scratch Arena via GlobalAlloc.
28pub const LARGE_ALLOC_THRESHOLD: usize = 1024 * 1024; // 1 MB
29
30// ============================================================================
31// Alignment Constants
32// ============================================================================
33
34/// Cache line size for SIMD-friendly FFT/NTT operations.
35/// 64 bytes is optimal for AVX-512 and most modern CPUs.
36pub const CACHE_LINE_ALIGN: usize = 64;
37
38/// Page alignment for huge vector allocations.
39/// 4KB works across Linux, macOS, and Windows.
40pub const PAGE_ALIGN: usize = 4096;
41
42/// Default minimum alignment for all allocations.
43pub const DEFAULT_ALIGN: usize = 8;
44
45/// 2MB huge page size (Linux).
46#[cfg(feature = "huge-pages")]
47pub const HUGE_PAGE_2MB: usize = 2 * 1024 * 1024;
48
49/// 1GB huge page size (Linux, requires explicit kernel config).
50#[cfg(feature = "huge-pages")]
51pub const HUGE_PAGE_1GB: usize = 1024 * 1024 * 1024;
52
53// ============================================================================
54// Security Constants
55// ============================================================================
56
57/// Pattern used for memory poisoning in debug builds.
58/// 0xDE is a distinctive pattern that helps identify use-after-free.
59#[cfg(debug_assertions)]
60pub const POISON_PATTERN: u8 = 0xDE;
61
62/// Secure wipe pattern (zero is standard for cryptographic applications).
63pub const SECURE_WIPE_PATTERN: u8 = 0x00;
64
65// ============================================================================
66// Timeout and Retry Constants
67// ============================================================================
68
69/// Maximum number of CAS retries before giving up (for extreme contention).
70/// On CI runners with 2 CPUs and 8 concurrent threads the initialising thread
71/// can be starved, so we allow many more iterations. Each outer loop now also
72/// calls `thread::yield_now()` every 10 iterations so the OS can schedule the
73/// thread that is actually doing the initialisation work.
74pub const MAX_CAS_RETRIES: usize = 50_000;
75
76/// Spin loop hint iterations before each state check.
77pub const SPIN_ITERATIONS: usize = 32;
78
79// ============================================================================
80// Guard Page Constants
81// ============================================================================
82
83/// Size of guard pages (usually one page).
84#[cfg(feature = "guard-pages")]
85pub const GUARD_PAGE_SIZE: usize = PAGE_ALIGN;