use poulpy_hal::{ layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, ScalarZnxToRef, Scratch, ZnxView, ZnxViewMut}, source::Source, }; use std::marker::PhantomData; use poulpy_core::{ Distribution, GGSWEncryptSk, GetDistribution, ScratchTakeCore, layouts::{GGSW, GGSWInfos, GLWEInfos, GLWESecretPreparedToRef, LWEInfos, LWESecret, LWESecretToRef}, }; use crate::tfhe::blind_rotation::{ BlindRotationKey, BlindRotationKeyEncryptSk, BlindRotationKeyFactory, BlindRotationKeyInfos, CGGI, }; impl BlindRotationKeyFactory for BlindRotationKey { fn blind_rotation_key_alloc(infos: &A) -> BlindRotationKey, CGGI> where A: BlindRotationKeyInfos, { BlindRotationKey { keys: (0..infos.n_lwe().as_usize()) .map(|_| GGSW::alloc_from_infos(infos)) .collect(), dist: Distribution::NONE, _phantom: PhantomData, } } } impl BlindRotationKeyEncryptSk for Module where Self: GGSWEncryptSk, Scratch: ScratchTakeCore, { fn blind_rotation_key_encrypt_sk_tmp_bytes(&self, infos: &A) -> usize { self.ggsw_encrypt_sk_tmp_bytes(infos) } fn blind_rotation_key_encrypt_sk( &self, res: &mut BlindRotationKey, sk_glwe: &S0, sk_lwe: &S1, source_xa: &mut Source, source_xe: &mut Source, scratch: &mut Scratch, ) where D: DataMut, S0: GLWESecretPreparedToRef + GLWEInfos, S1: LWESecretToRef + LWEInfos + GetDistribution, { assert_eq!(res.keys.len() as u32, sk_lwe.n()); assert!(sk_glwe.n() <= self.n() as u32); assert_eq!(sk_glwe.rank(), res.rank()); match sk_lwe.dist() { Distribution::BinaryBlock(_) | Distribution::BinaryFixed(_) | Distribution::BinaryProb(_) | Distribution::ZERO => {} _ => { panic!("invalid GLWESecret distribution: must be BinaryBlock, BinaryFixed or BinaryProb (or ZERO for debugging)") } } { let sk_lwe: &LWESecret<&[u8]> = &sk_lwe.to_ref(); res.dist = sk_lwe.dist(); let mut pt: ScalarZnx> = ScalarZnx::alloc(sk_glwe.n().into(), 1); let sk_ref: ScalarZnx<&[u8]> = sk_lwe.data().to_ref(); for (i, ggsw) in res.keys.iter_mut().enumerate() { pt.at_mut(0, 0)[0] = sk_ref.at(0, 0)[i]; ggsw.encrypt_sk(self, &pt, sk_glwe, source_xa, source_xe, scratch); } } } }