use backend::{ Backend, FFT64, Module, ScalarZnx, ScalarZnxAlloc, ScalarZnxDft, ScalarZnxDftAlloc, ScalarZnxDftOps, ScratchOwned, VecZnxDft, ZnxInfos, ZnxZero, }; use sampling::source::Source; use crate::{GLWECiphertextFourier, Infos}; #[derive(Clone, Copy, Debug)] pub(crate) enum SecretDistribution { TernaryFixed(usize), // Ternary with fixed Hamming weight TernaryProb(f64), // Ternary with probabilistic Hamming weight ZERO, // Debug mod NONE, } pub struct GLWESecret { pub(crate) data: ScalarZnx, pub(crate) data_fourier: ScalarZnxDft, pub(crate) dist: SecretDistribution, } impl GLWESecret, B> { pub fn alloc(module: &Module, rank: usize) -> Self { Self { data: module.new_scalar_znx(rank), data_fourier: module.new_scalar_znx_dft(rank), dist: SecretDistribution::NONE, } } pub fn bytes_of(module: &Module, rank: usize) -> usize { module.bytes_of_scalar_znx(rank) + module.bytes_of_scalar_znx_dft(rank) } } impl GLWESecret { pub fn n(&self) -> usize { self.data.n() } pub fn log_n(&self) -> usize { self.data.log_n() } pub fn rank(&self) -> usize { self.data.cols() } } impl + AsRef<[u8]>> GLWESecret { pub fn fill_ternary_prob(&mut self, module: &Module, prob: f64, source: &mut Source) { (0..self.rank()).for_each(|i| { self.data.fill_ternary_prob(i, prob, source); }); self.prep_fourier(module); self.dist = SecretDistribution::TernaryProb(prob); } pub fn fill_ternary_hw(&mut self, module: &Module, hw: usize, source: &mut Source) { (0..self.rank()).for_each(|i| { self.data.fill_ternary_hw(i, hw, source); }); self.prep_fourier(module); self.dist = SecretDistribution::TernaryFixed(hw); } pub fn fill_zero(&mut self) { self.data.zero(); self.dist = SecretDistribution::ZERO; } pub(crate) fn prep_fourier(&mut self, module: &Module) { (0..self.rank()).for_each(|i| { module.svp_prepare(&mut self.data_fourier, i, &self.data, i); }); } } pub struct GLWEPublicKey { pub(crate) data: GLWECiphertextFourier, pub(crate) dist: SecretDistribution, } impl GLWEPublicKey, B> { pub fn alloc(module: &Module, basek: usize, k: usize, rank: usize) -> Self { Self { data: GLWECiphertextFourier::alloc(module, basek, k, rank), dist: SecretDistribution::NONE, } } pub fn bytes_of(module: &Module, basek: usize, k: usize, rank: usize) -> usize { GLWECiphertextFourier::, B>::bytes_of(module, basek, k, rank) } } impl Infos for GLWEPublicKey { type Inner = VecZnxDft; fn inner(&self) -> &Self::Inner { &self.data.data } fn basek(&self) -> usize { self.data.basek } fn k(&self) -> usize { self.data.k } } impl GLWEPublicKey { pub fn rank(&self) -> usize { self.cols() - 1 } } impl + AsMut<[u8]>> GLWEPublicKey { pub fn generate_from_sk>( &mut self, module: &Module, sk: &GLWESecret, source_xa: &mut Source, source_xe: &mut Source, sigma: f64, ) { #[cfg(debug_assertions)] { match sk.dist { SecretDistribution::NONE => panic!("invalid sk: SecretDistribution::NONE"), _ => {} } } // Its ok to allocate scratch space here since pk is usually generated only once. let mut scratch: ScratchOwned = ScratchOwned::new(GLWECiphertextFourier::encrypt_sk_scratch_space( module, self.basek(), self.k(), self.rank(), )); self.data .encrypt_zero_sk(module, sk, source_xa, source_xe, sigma, scratch.borrow()); self.dist = sk.dist; } }