arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/core/src/blind_rotation/key.rs
core/src/blind_rotation/key.rsRaw
 1use backend::{Backend, FFT64, Module, ScalarZnx, ScalarZnxAlloc, ScalarZnxToRef, Scratch, ZnxView, ZnxViewMut};
 2use sampling::source::Source;
 3
 4use crate::{AutomorphismKey, GGSWCiphertext, GLWESecret, LWESecret, SecretDistribution};
 5
 6pub struct BlindRotationKeyCGGI<B: Backend> {
 7    pub(crate) data: Vec<GGSWCiphertext<Vec<u8>, B>>,
 8    pub(crate) dist: SecretDistribution,
 9}
10
11pub struct BlindRotationKeyFHEW<B: Backend> {
12    pub(crate) data: Vec<GGSWCiphertext<Vec<u8>, B>>,
13    pub(crate) auto: Vec<AutomorphismKey<Vec<u8>, B>>,
14}
15
16impl BlindRotationKeyCGGI<FFT64> {
17    pub fn allocate(module: &Module<FFT64>, lwe_degree: usize, basek: usize, k: usize, rows: usize, rank: usize) -> Self {
18        let mut data: Vec<GGSWCiphertext<Vec<u8>, FFT64>> = Vec::with_capacity(lwe_degree);
19        (0..lwe_degree).for_each(|_| data.push(GGSWCiphertext::alloc(module, basek, k, rows, 1, rank)));
20        Self {
21            data,
22            dist: SecretDistribution::NONE,
23        }
24    }
25
26    pub fn generate_from_sk<DataSkGLWE, DataSkLWE>(
27        &mut self,
28        module: &Module<FFT64>,
29        sk_glwe: &GLWESecret<DataSkGLWE, FFT64>,
30        sk_lwe: &LWESecret<DataSkLWE>,
31        source_xa: &mut Source,
32        source_xe: &mut Source,
33        sigma: f64,
34        scratch: &mut Scratch,
35    ) where
36        DataSkGLWE: AsRef<[u8]>,
37        DataSkLWE: AsRef<[u8]>,
38    {
39        #[cfg(debug_assertions)]
40        {
41            assert_eq!(self.data.len(), sk_lwe.n());
42            assert_eq!(sk_glwe.n(), module.n());
43            assert_eq!(sk_glwe.rank(), self.data[0].rank());
44            match sk_lwe.dist {
45                SecretDistribution::BinaryBlock(_) | SecretDistribution::BinaryFixed(_) | SecretDistribution::BinaryProb(_) => {}
46                _ => panic!("invalid GLWESecret distribution: must be BinaryBlock, BinaryFixed or BinaryProb"),
47            }
48        }
49
50        self.dist = sk_lwe.dist;
51
52        let mut pt: ScalarZnx<Vec<u8>> = module.new_scalar_znx(1);
53        let sk_ref: ScalarZnx<&[u8]> = sk_lwe.data.to_ref();
54
55        self.data.iter_mut().enumerate().for_each(|(i, ggsw)| {
56            pt.at_mut(0, 0)[0] = sk_ref.at(0, 0)[i];
57            ggsw.encrypt_sk(module, &pt, sk_glwe, source_xa, source_xe, sigma, scratch);
58        })
59    }
60}