Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Storage And Caching

How the VM stores circuit material and serves hot verifying keys without re-deserializing on every tx.

Source: ZK_STORAGE_AND_CACHING.md.

Why tiers exist

Commitment parameters are large and shared across circuits with the same k. Pinning a full params copy next to every VK wastes memory. The cache therefore:

  • Stores params and cs+vk under separate content-addressed keys
  • Keeps a monolithic circuit blob for integrity and fallback
  • Serves deserialized VKs from pinned memory or a weight-bounded LRU

Cache hierarchy

TierKeyEvictionPersistence
Pinned memory72-byte circuit / 36-byte paramManual pin/unpin onlyLost on process restart
In-memory LRUUnified CacheKey (module / partial / circuit)Weight-based LRULost on restart
File systemHex filenames under zk_*Manual removeSurvives restart

Unified LRU entries:

#![allow(unused)]
fn main() {
// Conceptual — see packages/vm cache module
enum CacheKey {
    Checksum(Checksum),   // Wasm modules
    PartialKey([u8; 36]), // param or vk body
    CircuitKey([u8; 72]), // full circuit
}
}

On-disk layout

base_dir/state/wasm/
  <checksum>.wasm
  zk_param/<36-hex>.bin
  zk_vk/<36-hex>.bin
  zk_circuit/<72-hex>.bin
base_dir/cache/modules/<version>/<target>/<checksum>.module

Keys derive from the footer (to_param_key, to_vk_key, to_circuit_key).

Lifecycle

Store

serialized [params|cs|vk|footer]
  → check_circuit()  (dual SHA-256)
  → save_circuit_parts() → zk_param + zk_vk + zk_circuit
  → pin_circuit(circuit_key)  (typical default after store)

Load

pinned hit  → CachedCircuit        (~µs)
LRU hit     → CachedCircuit
FS hit      → deserialize, promote to LRU
miss        → split param+vk load, or monolithic fallback

Pin / unpin / remove

CallEffect
pin_circuitEnsure deserialized VK is in pinned map (idempotent)
unpin_circuitDrop pinned entry (disk may remain)
remove_circuitUnpin and delete circuit artifacts as implemented

Exact semantics of unpin vs FS module removal are implementation-defined in the cache module; do not assume unpin alone deletes all three on-disk parts without checking current packages/vm behavior.

Memory accounting

LevelUse
Per-VK size_estimateWeighting, metrics
Pinned / LRU totalsObservability, optional limits
Global pinned circuit memoryOptional host limits

Pinned circuits are not automatically evicted. Treat pin as a performance wire for production hot paths.

Operator tips

  1. After restart, re-pin circuits that appear on every block.
  2. Watch pinned hit rate vs FS loads (deserialization is expensive).
  3. Prefer split layout so many circuits share param files for the same k.
  4. Keep consensus zkid mappings correct; cache cannot invent missing registry entries.

Metrics (illustrative)

Hosts expose cache stats (hits on pinned/FS, misses, sizes). Use them when tuning memory limits for nodes that verify many distinct circuits.