encrypted_record_format
Typed, segmented, authenticated binary format for decoded scan records.
A record is a list of row dictionaries (one per scan) whose values are plain scalars, lists of scalars and numpy arrays. It is stored as a 32-byte clear prefix followed by AES-256-GCM segments:
prefix (little-endian):
magic "BFWC" | u8 version | 3 zero bytes | 8-byte nonce prefix
u32 header_len | u32 n_payload_segments | u64 payload_len
segment 0 AES-GCM(header)
segments 1..n AES-GCM(array payload, SEGMENT_BYTES each, the last may be shorter)
The header is a typed binary structure (never JSON or pickle): a row count, the rows as (name, tagged value) pairs, then an array table. Array bytes live only in the payload, C-contiguous at increasing offsets with no padding.
Every segment's nonce is the nonce prefix followed by its big-endian index, and its associated data is the whole prefix, the caller's binding, the index and a final flag. A record moved under another binding, truncated, extended or reordered therefore fails authentication.
Decoding is bounded: the prefix is validated before anything is read or allocated, the header is authenticated before it is parsed, every length is checked against the bytes that remain, and the payload buffer is allocated only once the header has been accepted. No array is returned before every segment has authenticated.
Module
Functions
prepare_record
def prepare_record( records: Sequence[Mapping[str, Any]],) ‑> PreparedRecord:Validate records and build the plaintext header and array list.
Nothing is written or encrypted here, so an uncacheable record is refused before any output exists.
Raises
UncacheableRecordError: If any field name or value is of a type, dtype, timezone or range the format does not store, or the header would exceedMAX_HEADER_BYTES.
read_record
def read_record( source: BinaryIO, *, key: bytes, binding: bytes,) ‑> list[dict[str, typing.Any]]:Read, authenticate and decode one record.
Arguments
source: A readable binary stream positioned at the start of the record.key: The 32-byte AES-256-GCM key.binding: The value the record was written with.
Returns The rows, with arrays as writable, C-contiguous views of one buffer.
Raises
ValueError: Ifkeyis not 32 bytes.MalformedRecordError: If the record is structurally invalid or truncated.cryptography.exceptions.InvalidTag: If any segment fails authentication (wrong key or binding, tampering, reordering).
record_binding
def record_binding(cache_id: bytes, canonical_key: bytes) ‑> bytes:Derive the binding that ties a record to its cache and its key.
write_record
def write_record( prepared: PreparedRecord, out: BinaryIO, *, key: bytes, binding: bytes,) ‑> int:Encrypt prepared and stream it to out.
Arguments
prepared: The record fromprepare_record.out: A writable binary stream.key: The 32-byte AES-256-GCM key.binding: The value tying the ciphertext to its cache and key.
Returns The number of bytes written.
Raises
ValueError: Ifkeyis not 32 bytes, or the arrays no longer add up to the prepared payload length.
Classes
MalformedRecordError
class MalformedRecordError(*args, **kwargs):Encoded record bytes are structurally invalid.
PreparedRecord
class PreparedRecord(header: bytes, arrays: tuple[np.ndarray, ...], payload_len: int):A record validated and encoded up to, but not including, encryption.
Attributes
header: The plaintext header, already validated.arrays: The arrays in array-table order, as given (not copied).payload_len: The total array bytes.
Variables
-
arrays : tuple[numpy.ndarray, ...]
encoded_size : int- Total byteswrite_recordwrites for this record.
-
header : bytes
n_payload_segments : int- Number of payload segments; 0 when there is no payload.
-
payload_len : int
UncacheableRecordError
class UncacheableRecordError(type_name: str):A record holds a value the format does not store.
Attributes
type_name: The name of the offending value's type.