vfhe.util.io

Serialization of vfhe objects: the type class Codec and the codecs of plain values.

A codec writes an object to a binary stream and reads one back, consuming exactly the bytes it wrote, so codecs chain on one stream and compose: a key’s codec writes its header, then every sample through the sample codec it was built with, and a key of any size streams one sample at a time. The caller names the whole structure once, as scodec does. A codec of an object that others refer to, such as a ring or a scheme, is built with the caller’s own objects and hands them back where the stream’s parameters match; built with none, it rebuilds them from the stream.

>>> from vfhe.mlwe.codec import KeySwitchKeyCodec, MLWESchemeCodec, SampleCodec
>>> keys = KeySwitchKeyCodec(SampleCodec(MLWESchemeCodec(scheme), packing="tight"))
>>> with open("rlk.vfhe", "wb") as f:
...     keys.encode(rlk, f)
>>> with open("rlk.vfhe", "rb") as f:
...     rlk = keys.decode_strict(f)

A stream is a binary file, a buffer or a buffered socket. A decoder raises EOFError when the stream ends inside an object and ValueError on bytes it cannot read; Codec.decode_strict also refuses bytes after the object. vfhe.util.io.protobuf is the format the vfhe codecs use: a protobuf message per object, length-prefixed in the stream.

class vfhe.util.io.BytesCodec

Bytes behind their count.

encode(obj: bytes, stream: IO[bytes], /) → None

Writes obj to stream. Raises TypeError for an object the codec does not cover and ValueError for one it cannot write.

decode(stream: IO[bytes], /) → bytes

Returns the object at the head of stream. Raises EOFError when the stream ends inside the object and ValueError on bytes it cannot read.

class vfhe.util.io.Codec

Writes a T to a binary stream and reads one back.

encode writes the object’s own bytes and nothing around them, and decode consumes exactly those bytes, so codecs chain on one stream in the order they wrote.

abstractmethod encode(obj: T, stream: IO[bytes], /) → None

Writes obj to stream. Raises TypeError for an object the codec does not cover and ValueError for one it cannot write.

abstractmethod decode(stream: IO[bytes], /) → T

Returns the object at the head of stream. Raises EOFError when the stream ends inside the object and ValueError on bytes it cannot read.

decode_strict(stream: IO[bytes], /) → T

decode, then checks that the stream has ended: for a whole file or buffer. Raises ValueError when bytes follow the object. On a socket the check waits for the peer to close, so use decode there.

class vfhe.util.io.DictCodec(key: Codec[K], value: Codec[V])

A dict: its count, then each pair in the dict’s order, the key through key and the value through value.

encode(obj: dict[K, V], stream: IO[bytes], /) → None

Writes obj to stream. Raises TypeError for an object the codec does not cover and ValueError for one it cannot write.

decode(stream: IO[bytes], /) → dict[K, V]

Returns the object at the head of stream. Raises EOFError when the stream ends inside the object and ValueError on bytes it cannot read.

class vfhe.util.io.IntCodec

An int as 8 little-endian bytes, two’s complement. encode raises struct.error for a value outside 64 bits.

encode(obj: int, stream: IO[bytes], /) → None

Writes obj to stream. Raises TypeError for an object the codec does not cover and ValueError for one it cannot write.

decode(stream: IO[bytes], /) → int

Returns the object at the head of stream. Raises EOFError when the stream ends inside the object and ValueError on bytes it cannot read.

class vfhe.util.io.ListCodec(item: Codec[T])

A list of T: its count, then each item through item.

encode(obj: list[T], stream: IO[bytes], /) → None

Writes obj to stream. Raises TypeError for an object the codec does not cover and ValueError for one it cannot write.

decode(stream: IO[bytes], /) → list[T]

Returns the object at the head of stream. Raises EOFError when the stream ends inside the object and ValueError on bytes it cannot read.

class vfhe.util.io.OptionalCodec(item: Codec[T])

None or a T: a flag byte, then the value through item when there is one.

encode(obj: T | None, stream: IO[bytes], /) → None

Writes obj to stream. Raises TypeError for an object the codec does not cover and ValueError for one it cannot write.

decode(stream: IO[bytes], /) → T | None

Returns the object at the head of stream. Raises EOFError when the stream ends inside the object and ValueError on bytes it cannot read.

class vfhe.util.io.TupleCodec(*items: Codec[Any])

A tuple: each position through its own codec, in order, with no count. encode raises ValueError for a tuple of another length.

encode(obj: tuple, stream: IO[bytes], /) → None

Writes obj to stream. Raises TypeError for an object the codec does not cover and ValueError for one it cannot write.

decode(stream: IO[bytes], /) → tuple

Returns the object at the head of stream. Raises EOFError when the stream ends inside the object and ValueError on bytes it cannot read.

Protobuf in a stream, object <-> message <-> bytes: ObjectMessageCodec maps an object to one message of a vfhe.<module>.io.v1 schema under proto/, and MessageBytesCodec puts a message in the stream, length-prefixed.

class vfhe.util.io.protobuf.MessageBytesCodec(message: type[M])

Message <-> bytes: the codec of a protobuf message as itself, its serialized bytes behind their size as a varint, protobuf’s own framing.

This is protobuf’s serialize_length_prefixed and parse_length_prefixed written for streams: protobuf’s parser reads the message in one read and calls a shorter answer corruption, which a socket or a pipe gives on a sound stream. decode reads in as many reads as the stream needs; it raises EOFError at the stream’s end or when the message is cut, and ValueError on malformed bytes or a size past MAX_SIZE, before reading a byte of it. encode writes the message in two writes.

encode(obj: M, stream: IO[bytes], /) → None

Writes obj to stream. Raises TypeError for an object the codec does not cover and ValueError for one it cannot write.

decode(stream: IO[bytes], /) → M

Returns the object at the head of stream. Raises EOFError when the stream ends inside the object and ValueError on bytes it cannot read.

class vfhe.util.io.protobuf.ObjectMessageCodec

Object <-> message: a codec whose object is one protobuf message, in the stream through MessageBytesCodec.

A subclass names its message and writes to_message and from_message; encode and decode are those two through the bytes codec. An object holding more than one message’s worth, such as a key and its samples, gets a Codec of its own, which writes its header through MessageBytesCodec and its parts through their codecs.

message: type[M]

a subclass sets it to its M.

Type:

The message class decode parses

abstractmethod to_message(obj: T, /) → M

Returns the message holding obj. Raises TypeError for an object of another type and ValueError for one that cannot be written.

abstractmethod from_message(message: M, /) → T

Returns the object message holds. Raises ValueError for a message this library cannot read.

encode(obj: T, stream: IO[bytes], /) → None

Writes obj to stream. Raises TypeError for an object the codec does not cover and ValueError for one it cannot write.

decode(stream: IO[bytes], /) → T

Returns the object at the head of stream. Raises EOFError when the stream ends inside the object and ValueError on bytes it cannot read.