I am trying to save ciphertexts in a file to reuse for homomorphic computations. However, I obtain large files in size (2.6 GB encrypted file for a 310 kB file). The file consists of only 0s and 1s. I use LWE128_630 parameters to generate a secret key for encryption. Is there a better way to store ciphertexts in reduced size?
I’m guessing you tried to use the VectorLWE::save method.
As this method saves in JSON format (which is a text format) it can take more space than a binary format.
To save your ciphertexts in a binary format you could try to use bincode.
You would add bincode = "1.3.3" the dependencies in your Cargo.toml
To write
let mut file = std::io::BufWriter::new(std::fs::File::create("ciphertexts.bin").unwrap());
bincode::serialize_into(file, &ciphertext).unwrap();
To read
let mut file = std::io::BufReader::new(std::fs::File::open("ciphertexts.bin").unwrap());
let ciphertexts: VectorLWE = bincode::deserialize_from(file).unwrap();
Taking a slightly modified version of the example from the documentation
use std::io::Write;
use concrete::*;
fn main() -> Result<(), CryptoAPIError> {
// generate a secret key and save it
let secret_key = LWESecretKey::new(&LWE128_630);
secret_key.save("my_very_secret_key.json");
// create an encoder
let encoder = Encoder::new(-10., 10., 8, 0)?;
// a list of messages
let messages: Vec<f64> = [-6.276, 4.3, 0.12, -1.1, 7.78].iter().copied().cycle().take(30_000).collect();
// encode and encrypt message vector
let ciphertext = VectorLWE::encode_encrypt(&secret_key, &messages, &encoder)?;
ciphertext.save("ciphertexts.json");
{
let mut file = std::io::BufWriter::new(std::fs::File::create("ciphertexts.bin").unwrap());
bincode::serialize_into(file, &ciphertext).unwrap();
}
{
let mut file = std::io::BufReader::new(std::fs::File::open("ciphertexts.bin").unwrap());
let read_again: VectorLWE = bincode::deserialize_from(file).unwrap();
assert_eq!(read_again, ciphertext);
}
Ok(())
}
ciphertexts.json is 389 MBytes
ciphertexts.bin is 152 MBytes
If that is not what you did, please post a code sample that produces your problem.