host_cache_keys
Key material for host-level caches shared across DAG-run processes.
The Compute host generates one HostCacheKey (a random master key plus a
non-secret scope naming the host process) and holds it only in memory. Each
cache derives its own purpose key from the master, and the model memo derives
its key id, entry names and per-entry weights keys from its purpose key:
| Key | Derived from | HKDF info | Length |
|---|---|---|---|
master M | os.urandom | (none) | 32 |
K_mm (model memo) | M | PURPOSE_INFO_PREFIX + b"model-memo" | 32 |
K_wc (wave cache) | M | PURPOSE_INFO_PREFIX + b"wave-cache" | 32 |
| key id (public) | K_mm | MEMO_KEY_ID_INFO | 8, as hex |
K_name (entry names) | K_mm | MEMO_ENTRY_NAME_INFO | 32 |
K_w (one entry's weights) | K_mm, salt: entry name | MEMO_WEIGHTS_INFO | 32 |
Every derivation is HKDF-SHA256 with an empty salt unless stated; K_w is salted
by the entry name's ASCII bytes. An entry name is the first 16 bytes of
HMAC-SHA256(K_name, canonical identity), as hex.
This module depends only on the standard library, cryptography and psutil,
so the compute package can use it without importing bitfount.federated.
Module
Functions
generate_host_cache_key
def generate_host_cache_key() ‑> HostCacheKey:Generates a fresh host cache key scoped to the calling process.
Returns A key with a random master and this process's pid-create-time scope.
is_valid_scope
def is_valid_scope(scope: object) ‑> bool:Whether scope is a <pid>-<create time ms> string.
Scopes name directories on disk, so anything else (path separators, ..,
non-ASCII digits, trailing newlines) is refused.
Arguments
scope: The candidate scope.
Returns
True only for a str that fully matches SCOPE_PATTERN.
memo_entry_name
def memo_entry_name(memo_key: bytes, canonical_identity: bytes) ‑> str:Derives the on-disk name of one model memo entry.
The name is keyed, so it reveals nothing about the model it holds to
anyone without memo_key.
Arguments
memo_key: The model-memo purpose key.canonical_identity: The entry's canonical identity bytes.
Returns 32 lowercase hex characters.
Raises
ValueError: Ifmemo_keyis not 32 bytes.
memo_key_id
def memo_key_id(memo_key: bytes) ‑> str:Derives the public id naming the model memo store for memo_key.
Arguments
memo_key: The model-memo purpose key.
Returns 16 lowercase hex characters.
Raises
ValueError: Ifmemo_keyis not 32 bytes.
memo_weights_key
def memo_weights_key(memo_key: bytes, entry_name: str) ‑> bytes:Derives the AES-GCM key sealing one model memo entry's weights.
Arguments
memo_key: The model-memo purpose key.entry_name: The entry's name, as returned bymemo_entry_name.
Returns The entry's 32-byte weights key.
Raises
ValueError: Ifmemo_keyis not 32 bytes.
purpose_key
def purpose_key(master: bytes, purpose: HostCachePurpose) ‑> bytes:Derives one cache's purpose key from the host master key.
Arguments
master: The host master key.purpose: Which cache the key is for.
Returns The 32-byte purpose key.
Raises
ValueError: Ifpurposeis not a known host cache purpose. A non-strpurpose (such as swapped arguments) is not echoed.
Classes
HostCacheKey
class HostCacheKey(master: bytes, scope: str):The host's cache master key and the scope naming its host process.
The master is secret and kept out of repr; the scope is not secret.
Attributes
master: The 32-byte master key.scope:<host pid>-<host create time in ms>.
Raises
ValueError: Ifmasteris not 32 bytes orscopeis not a valid scope.