use itertools::Itertools; use poulpy_core::{ GLWEOperations, TakeGLWECtSlice, TakeGLWEPt, glwe_packing, layouts::{ GLWE, GLWEInfos, GLWEPlaintextLayout, LWEInfos, TorusPrecision, prepared::{AutomorphismKeyPrepared, GLWESecretPrepared}, }, }; use poulpy_hal::{ api::{ ScratchAvailable, SvpApplyDftToDftInplace, TakeVecZnx, TakeVecZnxBig, TakeVecZnxDft, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAutomorphismInplace, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxBigSubSmallNegateInplace, VecZnxCopy, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxDftCopy, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNegateInplace, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotate, VecZnxRotateInplace, VecZnxRshInplace, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, }, layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}, source::Source, }; use std::{collections::HashMap, marker::PhantomData}; use crate::tfhe::bdd_arithmetic::{FromBits, ToBits, UnsignedInteger}; /// A FHE ciphertext encrypting a [UnsignedInteger]. pub struct FheUintWord(pub(crate) GLWE, pub(crate) PhantomData); impl FheUintWord { #[allow(dead_code)] fn post_process( &mut self, module: &Module, mut tmp_res: Vec>, auto_keys: &HashMap>, scratch: &mut Scratch, ) where ATK: DataRef, Module: VecZnxSub + VecZnxCopy + VecZnxNegateInplace + VecZnxDftAllocBytes + VecZnxAddInplace + VmpApplyDftToDftTmpBytes + VecZnxNormalizeTmpBytes + VecZnxDftApply + VmpApplyDftToDft + VmpApplyDftToDftAdd + VecZnxIdftApplyConsume + VecZnxBigNormalize + VecZnxNormalize + VecZnxRotateInplace + VecZnxNormalizeInplace + VecZnxSwitchRing + VecZnxBigAutomorphismInplace + VecZnxRshInplace + VecZnxDftCopy + VecZnxIdftApplyTmpA + VecZnxSubInplace + VecZnxBigNormalizeTmpBytes + VecZnxBigAddSmallInplace + VecZnxAutomorphismInplace + VecZnxBigSubSmallNegateInplace + VecZnxRotate, Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx + TakeGLWECtSlice, { // 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); } glwe_packing(module, &mut cts, log_gap, auto_keys, scratch); // And copies the repacked ciphertext on the receiver. self.0.copy(module, cts.remove(&0).unwrap()) } } impl LWEInfos for FheUintWord { 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 FheUintWord { fn rank(&self) -> poulpy_core::layouts::Rank { self.0.rank() } } impl FheUintWord { pub fn encrypt_sk( &mut self, module: &Module, data: T, sk: &GLWESecretPrepared, source_xa: &mut Source, source_xe: &mut Source, scratch: &mut Scratch, ) where Module: VecZnxAddScalarInplace + VecZnxDftAllocBytes + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace + VecZnxIdftApplyConsume + VecZnxNormalizeTmpBytes + VecZnxFillUniform + VecZnxSubInplace + VecZnxAddInplace + VecZnxNormalizeInplace + VecZnxAddNormal + VecZnxNormalize + VecZnxSub, Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx + TakeGLWEPt, { #[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_pt(&pt_infos); pt.encode_vec_i64(&data_bits, TorusPrecision(1)); self.0 .encrypt_sk(module, &pt, sk, source_xa, source_xe, scratch_1); } } impl FheUintWord { pub fn decrypt( &self, module: &Module, sk: &GLWESecretPrepared, scratch: &mut Scratch, ) -> T where Module: VecZnxDftApply + SvpApplyDftToDftInplace + VecZnxIdftApplyConsume + VecZnxBigAddInplace + VecZnxBigAddSmallInplace + VecZnxBigNormalize, Scratch: TakeVecZnxDft + TakeVecZnxBig + TakeGLWEPt, { #[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_pt(&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) } }