Janmajayamall / bfv

MIT License
26 stars 5 forks source link

Add Serialization/Deserialization for all keys #3

Open Janmajayamall opened 1 year ago

Janmajayamall commented 1 year ago

Add serialization/deserialization for Poly, Ciphertext, KeySwitchingKey, RelinerizationKey, GaloisKey.

Since all keys depend on Poly and poly only consists of coefficients (ie Array2) and representation, the only important functions are converting vector of u64s (ie each row in Array2) to bytes and back.

I have already implemented functions to convert vector of u64s assumed to be in range [0, modulus) to bytes and back. Check convert_to_bytes and convert_from_bytes.

I have also added a rough way to convert Poly to proto::Poly here.

Conversion of proto::Poly to bytes is behaving as expected. For example, a Poly with modulus Q of size 500 bits (ie moduli chain = [50;10]) and degree 2^15 should take 500*(2^15)/8 = 2048000 bytes (ignoring representation value). I can confirm that converting such a Poly to proto::Poly and encoding converted value in bytes equals to 2048052 bytes, which is only a difference of 52 bytes.

Janmajayamall commented 1 year ago

Commit 5bf8e16da939a4aef0a8e469242f3359cbbd4cc8 implements serialization/deserialization for HybridKeySwitchingKey, EvaluationKey, RelinearizationKey, GaloisKey.

I checked that serialized objects are of expected size in bytes by first estimating the sizes by hand and then converting the types to respective serialized types and then to bytes. Eveything looks correct.

Janmajayamall commented 1 year ago

I seem to have made a mistake by allowing Poly to be serialized in Evaluation representation. Since we plan to support mutiple NTT backends, Evaluation representation of same polynomial can be different on each. This can lead to incorrectness if one serializes a polynomial in Evaluation form using native NTT backend and deserializes it using hexl NTT backend, since the polynomial would not remain the same.

To avoid this, I think it is ok to forbid serliization of polynomial in Evaluation form.

Janmajayamall commented 1 year ago

Commit db97066665ea9d37ff09ed7dc8c4578d6684457d adds support for seeded Ciphertexts and makes necessary changes to forbid seriaization of Poly in Evalaution form

Janmajayamall commented 1 year ago

I have been contemplating to switch serialization to a feature. The obvious benefit of this is to allow users not in need of serialization to not download and install protobuf compiler. Plus, I don't see any downisdes of this.