use poulpy_core::{ GLWECopy, GLWEDecrypt, GLWEEncryptSk, GLWEPacking, GLWERotate, LWEFromGLWE, ScratchTakeCore, layouts::{ Base2K, Degree, GGLWEInfos, GGLWEPreparedToRef, GLWE, GLWEInfos, GLWEPlaintextLayout, GLWESecretPreparedToRef, GLWEToMut, GLWEToRef, LWEInfos, LWEToMut, Rank, TorusPrecision, }, }; use poulpy_hal::{ api::ModuleLogN, layouts::{Backend, Data, DataMut, DataRef, Scratch}, source::Source, }; use std::{collections::HashMap, marker::PhantomData}; use crate::tfhe::{ bdd_arithmetic::{BDDKeyPrepared, FromBits, ToBits, UnsignedInteger}, blind_rotation::BlindRotationAlgo, }; /// An FHE ciphertext encrypting the bits of an [UnsignedInteger]. pub struct FheUint { pub(crate) bits: GLWE, pub(crate) _phantom: PhantomData, } impl FheUint, T> { pub fn alloc_from_infos(infos: &A) -> Self where A: GLWEInfos, { Self::alloc(infos.n(), infos.base2k(), infos.k(), infos.rank()) } pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self { Self { bits: GLWE::alloc(n, base2k, k, rank), _phantom: PhantomData, } } } impl LWEInfos for FheUint { fn base2k(&self) -> poulpy_core::layouts::Base2K { self.bits.base2k() } fn k(&self) -> poulpy_core::layouts::TorusPrecision { self.bits.k() } fn n(&self) -> poulpy_core::layouts::Degree { self.bits.n() } } impl GLWEInfos for FheUint { fn rank(&self) -> poulpy_core::layouts::Rank { self.bits.rank() } } impl FheUint { 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: ModuleLogN + GLWEEncryptSk, Scratch: ScratchTakeCore, { #[cfg(debug_assertions)] { assert!(module.n().is_multiple_of(T::BITS as usize)); assert_eq!(self.n(), module.n() as u32); assert_eq!(sk.n(), module.n() as u32); } let mut data_bits: Vec = vec![0i64; module.n()]; let log_gap: usize = module.log_n() - T::LOG_BITS as usize; // Interleaves bytes for i in 0..T::BITS as usize { data_bits[T::bit_index(i) << log_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(2)); self.bits .encrypt_sk(module, &pt, sk, source_xa, source_xe, scratch_1); } } impl FheUint { pub fn decrypt(&self, module: &M, sk: &S, scratch: &mut Scratch) -> T where S: GLWESecretPreparedToRef + GLWEInfos, M: ModuleLogN + GLWEDecrypt, Scratch: ScratchTakeCore, { #[cfg(debug_assertions)] { assert!(module.n().is_multiple_of(T::BITS as usize)); assert_eq!(self.n(), module.n() as u32); assert_eq!(sk.n(), module.n() as u32); } 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.bits.decrypt(module, &mut pt, sk, scratch_1); let mut data_bits: Vec = vec![0i64; module.n()]; pt.decode_vec_i64(&mut data_bits, TorusPrecision(2)); let mut bits: Vec = vec![0u8; T::BITS as usize]; let log_gap: usize = module.log_n() - T::LOG_BITS as usize; // Retrives from interleaved bytes for i in 0..T::BITS as usize { bits[i] = data_bits[T::bit_index(i) << log_gap] as u8 } T::from_bits(&bits) } } impl FheUint { /// Packs Vec into [FheUint]. pub fn pack( &mut self, module: &M, mut bits: Vec, key: &BDDKeyPrepared, scratch: &mut Scratch, ) where G: GLWEToMut + GLWEToRef + GLWEInfos, D1: DataRef, M: ModuleLogN + GLWEPacking + GLWECopy, Scratch: ScratchTakeCore, { // Repacks the GLWE ciphertexts bits let log_gap: usize = module.log_n() - T::LOG_BITS as usize; let mut cts: HashMap = HashMap::new(); for (i, ct) in bits.iter_mut().enumerate().take(T::BITS as usize) { cts.insert(T::bit_index(i) << log_gap, ct); } module.glwe_pack(&mut cts, log_gap, &key.cbt.atk, scratch); // And copies the repacked ciphertext on the receiver. module.glwe_copy(&mut self.bits, cts.remove(&0).unwrap()); } // pub fn copy_byte( // &mut self, // module: &M, // byte_self: usize, // byte_a: usize, // a: &FheUint, // keys: &BDDKeyPrepared, // scratch: &mut Scratch, // ) where // D0: DataRef, // D1: DataRef, // BRA: BlindRotationAlgo, // M:ModuleLogN + GLWERotate + GLWETrace + GLWESub + GLWEAdd, // Scratch: ScratchTakeBDD, // { // let (mut tmp_fhe_uint_byte, scratch_1) = scratch.take_fhe_uint(a); // // // let log_gap: usize = module.log_n() - T::LOG_BITS as usize; // module.glwe_rotate(-((T::bit_index(byte_a << 3) << log_gap) as i64), tmp_fhe_uint_byte, self); // module.glwe_trace_inplace(&mut tmp_fhe_uint_byte, module.log_n() - 3, module.log_n(),&keys.cbt.atk, scratch); // // let log_gap: usize = module.log_n() - T::LOG_BITS as usize; // let rot: i64 = (T::bit_index(byte_self << 3) << log_gap) as i64; // // Move starting byte index to first coefficient // module.glwe_rotate_inplace(-rot, &mut self.bits, scratch); // // Stores this byte (everything else zeroed) into tmp_trace // let (mut tmp_trace, scratch_1) = scratch.take_glwe(self); // module.glwe_trace( // &mut tmp_trace, // module.log_n() - 3, // module.log_n(), // self, // &keys.cbt.atk, // scratch_1, // ); // // Subtracts the byte // module.glwe_sub_inplace(&mut self.bits, &tmp_trace); // // module.glwe_add_inplace(&mut self.bits, &tmp_fhe_uint_byte); // // Moves back into the original position // module.glwe_rotate_inplace(-rot, &mut self.bits, scratch); // // } } impl GLWEToMut for FheUint { fn to_mut(&mut self) -> GLWE<&mut [u8]> { self.bits.to_mut() } } pub trait ScratchTakeBDD where Self: ScratchTakeCore, { fn take_fhe_uint(&mut self, infos: &A) -> (FheUint<&mut [u8], T>, &mut Self) where A: GLWEInfos, { let (glwe, scratch) = self.take_glwe(infos); ( FheUint { bits: glwe, _phantom: PhantomData, }, scratch, ) } } impl ScratchTakeBDD for Scratch where Self: ScratchTakeCore {} impl FheUint { pub fn get_bit(&self, module: &M, bit: usize, res: &mut R, ks: &K, scratch: &mut Scratch) where R: LWEToMut, K: GGLWEPreparedToRef + GGLWEInfos, M: ModuleLogN + LWEFromGLWE + GLWERotate, Scratch: ScratchTakeCore, { let log_gap: usize = module.log_n() - T::LOG_BITS as usize; res.to_mut() .from_glwe(module, self, T::bit_index(bit) << log_gap, ks, scratch); } } impl GLWEToRef for FheUint { fn to_ref(&self) -> GLWE<&[u8]> { self.bits.to_ref() } }