use std::marker::PhantomData; use poulpy_core::layouts::{Base2K, GLWE, GLWEInfos, GLWEPlaintextLayout, LWEInfos, Rank, TorusPrecision}; use poulpy_core::{TakeGLWEPlaintext, layouts::prepared::GLWESecretPrepared}; use poulpy_hal::api::VecZnxBigBytesOf; #[cfg(test)] use poulpy_hal::api::{ ScratchAvailable, TakeVecZnx, VecZnxAddInplace, VecZnxAddNormal, VecZnxFillUniform, VecZnxNormalize, VecZnxSub, }; #[cfg(test)] use poulpy_hal::source::Source; use poulpy_hal::{ api::{ TakeVecZnxBig, TakeVecZnxDft, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalizeTmpBytes, }, layouts::{Backend, Data, DataMut, DataRef, Module, Scratch}, }; use poulpy_hal::api::{SvpApplyDftToDftInplace, VecZnxNormalizeInplace, VecZnxSubInplace}; use crate::tfhe::bdd_arithmetic::{FromBits, ToBits, UnsignedInteger}; /// An FHE ciphertext encrypting the bits of an [UnsignedInteger]. pub struct FheUintBlocks { pub(crate) blocks: Vec>, pub(crate) _base: u8, pub(crate) _phantom: PhantomData, } impl LWEInfos for FheUintBlocks { fn base2k(&self) -> poulpy_core::layouts::Base2K { self.blocks[0].base2k() } fn k(&self) -> poulpy_core::layouts::TorusPrecision { self.blocks[0].k() } fn n(&self) -> poulpy_core::layouts::RingDegree { self.blocks[0].n() } } impl GLWEInfos for FheUintBlocks { fn rank(&self) -> poulpy_core::layouts::Rank { self.blocks[0].rank() } } impl FheUintBlocks, T> { #[allow(dead_code)] pub(crate) fn alloc(module: &Module, infos: &A) -> Self where A: GLWEInfos, { Self::alloc_with(module, infos.base2k(), infos.k(), infos.rank()) } #[allow(dead_code)] pub(crate) fn alloc_with(module: &Module, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self { Self { blocks: (0..T::WORD_SIZE) .map(|_| GLWE::alloc(module.n().into(), base2k, k, rank)) .collect(), _base: 1, _phantom: PhantomData, } } } impl FheUintBlocks { #[allow(dead_code)] #[cfg(test)] pub(crate) fn encrypt_sk( &mut self, module: &Module, value: T, sk: &GLWESecretPrepared, source_xa: &mut Source, source_xe: &mut Source, scratch: &mut Scratch, ) where S: DataRef, Module: VecZnxDftBytesOf + VecZnxBigNormalize + VecZnxDftApply + SvpApplyDftToDftInplace + VecZnxIdftApplyConsume + VecZnxNormalizeTmpBytes + VecZnxFillUniform + VecZnxSubInplace + VecZnxAddInplace + VecZnxNormalizeInplace + VecZnxAddNormal + VecZnxNormalize + VecZnxSub, Scratch: TakeVecZnxDft + ScratchAvailable + TakeVecZnx + TakeGLWEPlaintext, { use poulpy_core::layouts::GLWEPlaintextLayout; #[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 pt_infos = GLWEPlaintextLayout { n: self.n(), base2k: self.base2k(), k: 1_usize.into(), }; let (mut pt, scratch_1) = scratch.take_glwe_pt(&pt_infos); for i in 0..T::WORD_SIZE { pt.encode_coeff_i64(value.bit(i) as i64, TorusPrecision(1), 0); self.blocks[i].encrypt_sk(&module, &pt, sk, source_xa, source_xe, scratch_1); } } } impl FheUintBlocks { pub fn decrypt( &self, module: &Module, sk: &GLWESecretPrepared, scratch: &mut Scratch, ) -> T where Module: VecZnxDftApply + SvpApplyDftToDftInplace + VecZnxIdftApplyConsume + VecZnxBigAddInplace + VecZnxBigAddSmallInplace + VecZnxBigNormalize, Scratch: TakeVecZnxDft + TakeVecZnxBig + TakeGLWEPlaintext, { #[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 pt_infos = GLWEPlaintextLayout { n: self.n(), base2k: self.base2k(), k: self.k(), }; let (mut pt, scratch_1) = scratch.take_glwe_pt(&pt_infos); let mut bits: Vec = vec![0u8; T::WORD_SIZE]; let base2k: usize = self.base2k().into(); let scale: f64 = 4.0 / ((1 << base2k) as f64); for (i, bit) in bits.iter_mut().enumerate().take(T::WORD_SIZE) { self.blocks[i].decrypt(module, &mut pt, sk, scratch_1); let value: i64 = pt.decode_coeff_i64(base2k.into(), 0); *bit = ((value as f64) * scale).round() as u8; } T::from_bits(&bits) } pub fn noise( &self, module: &Module, sk: &GLWESecretPrepared, want: T, scratch: &mut Scratch, ) -> Vec where Module: VecZnxDftBytesOf + VecZnxBigBytesOf + VecZnxDftApply + SvpApplyDftToDftInplace + VecZnxIdftApplyConsume + VecZnxBigAddInplace + VecZnxBigAddSmallInplace + VecZnxBigNormalize + VecZnxNormalizeTmpBytes + VecZnxSubInplace + VecZnxNormalizeInplace, Scratch: TakeGLWEPlaintext + TakeVecZnxDft + TakeVecZnxBig, { #[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 pt_infos = GLWEPlaintextLayout { n: self.n(), base2k: self.base2k(), k: 1_usize.into(), }; let (mut pt_want, scratch_1) = scratch.take_glwe_pt(&pt_infos); let mut noise: Vec = vec![0f64; T::WORD_SIZE]; for (i, noise_i) in noise.iter_mut().enumerate().take(T::WORD_SIZE) { pt_want.encode_coeff_i64(want.bit(i) as i64, TorusPrecision(2), 0); *noise_i = self.blocks[i].noise(module, sk, &pt_want, scratch_1); } noise } }