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
objtostream. RaisesTypeErrorfor an object the codec does not cover andValueErrorfor one it cannot write.
- decode(stream: IO[bytes], /) bytes¶
Returns the object at the head of
stream. RaisesEOFErrorwhen the stream ends inside the object andValueErroron bytes it cannot read.
- class vfhe.util.io.Codec¶
Writes a
Tto a binary stream and reads one back.encodewrites the object’s own bytes and nothing around them, anddecodeconsumes exactly those bytes, so codecs chain on one stream in the order they wrote.- abstractmethod encode(obj: T, stream: IO[bytes], /) None¶
Writes
objtostream. RaisesTypeErrorfor an object the codec does not cover andValueErrorfor one it cannot write.
- abstractmethod decode(stream: IO[bytes], /) T¶
Returns the object at the head of
stream. RaisesEOFErrorwhen the stream ends inside the object andValueErroron bytes it cannot read.
- 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
keyand the value throughvalue.- encode(obj: dict[K, V], stream: IO[bytes], /) None¶
Writes
objtostream. RaisesTypeErrorfor an object the codec does not cover andValueErrorfor one it cannot write.
- decode(stream: IO[bytes], /) dict[K, V]¶
Returns the object at the head of
stream. RaisesEOFErrorwhen the stream ends inside the object andValueErroron bytes it cannot read.
- class vfhe.util.io.IntCodec¶
An int as 8 little-endian bytes, two’s complement.
encoderaisesstruct.errorfor a value outside 64 bits.- encode(obj: int, stream: IO[bytes], /) None¶
Writes
objtostream. RaisesTypeErrorfor an object the codec does not cover andValueErrorfor one it cannot write.
- decode(stream: IO[bytes], /) int¶
Returns the object at the head of
stream. RaisesEOFErrorwhen the stream ends inside the object andValueErroron bytes it cannot read.
- class vfhe.util.io.ListCodec(item: Codec[T])¶
A list of
T: its count, then each item throughitem.- encode(obj: list[T], stream: IO[bytes], /) None¶
Writes
objtostream. RaisesTypeErrorfor an object the codec does not cover andValueErrorfor one it cannot write.
- decode(stream: IO[bytes], /) list[T]¶
Returns the object at the head of
stream. RaisesEOFErrorwhen the stream ends inside the object andValueErroron bytes it cannot read.
- class vfhe.util.io.OptionalCodec(item: Codec[T])¶
Noneor aT: a flag byte, then the value throughitemwhen there is one.- encode(obj: T | None, stream: IO[bytes], /) None¶
Writes
objtostream. RaisesTypeErrorfor an object the codec does not cover andValueErrorfor one it cannot write.
- decode(stream: IO[bytes], /) T | None¶
Returns the object at the head of
stream. RaisesEOFErrorwhen the stream ends inside the object andValueErroron 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.
encoderaisesValueErrorfor a tuple of another length.- encode(obj: tuple, stream: IO[bytes], /) None¶
Writes
objtostream. RaisesTypeErrorfor an object the codec does not cover andValueErrorfor one it cannot write.
- decode(stream: IO[bytes], /) tuple¶
Returns the object at the head of
stream. RaisesEOFErrorwhen the stream ends inside the object andValueErroron 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
messageas itself, its serialized bytes behind their size as a varint, protobuf’s own framing.This is protobuf’s
serialize_length_prefixedandparse_length_prefixedwritten for streams: protobuf’s parser reads the message in onereadand calls a shorter answer corruption, which a socket or a pipe gives on a sound stream.decodereads in as many reads as the stream needs; it raisesEOFErrorat the stream’s end or when the message is cut, andValueErroron malformed bytes or a size pastMAX_SIZE, before reading a byte of it.encodewrites the message in two writes.- encode(obj: M, stream: IO[bytes], /) None¶
Writes
objtostream. RaisesTypeErrorfor an object the codec does not cover andValueErrorfor one it cannot write.
- decode(stream: IO[bytes], /) M¶
Returns the object at the head of
stream. RaisesEOFErrorwhen the stream ends inside the object andValueErroron bytes it cannot read.
- class vfhe.util.io.protobuf.ObjectMessageCodec¶
Object <-> message: a codec whose object is one protobuf
message, in the stream throughMessageBytesCodec.A subclass names its
messageand writesto_messageandfrom_message;encodeanddecodeare those two through the bytes codec. An object holding more than one message’s worth, such as a key and its samples, gets aCodecof its own, which writes its header throughMessageBytesCodecand its parts through their codecs.- abstractmethod to_message(obj: T, /) M¶
Returns the message holding
obj. RaisesTypeErrorfor an object of another type andValueErrorfor one that cannot be written.
- abstractmethod from_message(message: M, /) T¶
Returns the object
messageholds. RaisesValueErrorfor a message this library cannot read.
- encode(obj: T, stream: IO[bytes], /) None¶
Writes
objtostream. RaisesTypeErrorfor an object the codec does not cover andValueErrorfor one it cannot write.
- decode(stream: IO[bytes], /) T¶
Returns the object at the head of
stream. RaisesEOFErrorwhen the stream ends inside the object andValueErroron bytes it cannot read.