use itertools::Itertools; use poulpy_core::{ GLWECopy, GLWEDecrypt, GLWEEncryptSk, GLWEPacking, ScratchTakeCore, layouts::{ GLWE, GLWEInfos, GLWEPlaintextLayout, GLWESecretPreparedToRef, LWEInfos, TorusPrecision, prepared::GLWEAutomorphismKeyPrepared, }, }; use poulpy_hal::{ api::ModuleN, layouts::{Backend, Data, DataMut, DataRef, Scratch}, source::Source, }; use std::{collections::HashMap, marker::PhantomData}; use crate::tfhe::bdd_arithmetic::{FromBits, ToBits, UnsignedInteger}; /// An FHE ciphertext encrypting the bits of an [UnsignedInteger] in compressed format (ideal for decryption/serialization). pub struct FheUintCompressed(pub(crate) GLWE, pub(crate) PhantomData); impl FheUintCompressed { #[allow(dead_code)] fn post_process( &mut self, module: &M, mut tmp_res: Vec>, auto_keys: &HashMap>, scratch: &mut Scratch, ) where ATK: DataRef, M: GLWEPacking + GLWECopy, Scratch: ScratchTakeCore, { // Repacks the GLWE ciphertexts bits let gap: usize = module.n() / T::WORD_SIZE; let log_gap: usize = (usize::BITS - (gap - 1).leading_zeros()) as usize; let mut cts: HashMap> = HashMap::new(); for (i, ct) in tmp_res.iter_mut().enumerate().take(T::WORD_SIZE) { cts.insert(i * gap, ct); } module.glwe_pack(&mut cts, log_gap, auto_keys, scratch); // And copies the repacked ciphertext on the receiver. module.glwe_copy(&mut self.0, cts.remove(&0).unwrap()); } } impl LWEInfos for FheUintCompressed { fn base2k(&self) -> poulpy_core::layouts::Base2K { self.0.base2k() } fn k(&self) -> poulpy_core::layouts::TorusPrecision { self.0.k() } fn n(&self) -> poulpy_core::layouts::Degree { self.0.n() } } impl GLWEInfos for FheUintCompressed { fn rank(&self) -> poulpy_core::layouts::Rank { self.0.rank() } } impl FheUintCompressed { pub fn encrypt_sk( &mut self, module: &M, data: T, sk: &S, source_xa: &mut Source, source_xe: &mut Source, scratch: &mut Scratch, ) where S: GLWESecretPreparedToRef + GLWEInfos, M: ModuleN + GLWEEncryptSk, Scratch: ScratchTakeCore, { #[cfg(debug_assertions)] { assert!(module.n().is_multiple_of(T::WORD_SIZE)); assert_eq!(self.n(), module.n() as u32); assert_eq!(sk.n(), module.n() as u32); } let gap: usize = module.n() / T::WORD_SIZE; let mut data_bits: Vec = vec![0i64; module.n()]; for i in 0..T::WORD_SIZE { data_bits[i * gap] = data.bit(i) as i64 } let pt_infos = GLWEPlaintextLayout { n: self.n(), base2k: self.base2k(), k: 1_usize.into(), }; let (mut pt, scratch_1) = scratch.take_glwe_plaintext(&pt_infos); pt.encode_vec_i64(&data_bits, TorusPrecision(1)); self.0 .encrypt_sk(module, &pt, sk, source_xa, source_xe, scratch_1); } } impl FheUintCompressed { pub fn decrypt(&self, module: &M, sk: &S, scratch: &mut Scratch) -> T where S: GLWESecretPreparedToRef + GLWEInfos, M: GLWEDecrypt, Scratch: ScratchTakeCore, { #[cfg(debug_assertions)] { assert!(module.n().is_multiple_of(T::WORD_SIZE)); assert_eq!(self.n(), module.n() as u32); assert_eq!(sk.n(), module.n() as u32); } let gap: usize = module.n() / T::WORD_SIZE; let pt_infos = GLWEPlaintextLayout { n: self.n(), base2k: self.base2k(), k: 1_usize.into(), }; let (mut pt, scratch_1) = scratch.take_glwe_plaintext(&pt_infos); self.0.decrypt(module, &mut pt, sk, scratch_1); let mut data: Vec = vec![0i64; module.n()]; pt.decode_vec_i64(&mut data, TorusPrecision(1)); let bits: Vec = data.iter().step_by(gap).map(|c| *c as u8).collect_vec(); T::from_bits(&bits) } }