arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/poulpy-core/src/encryption/compressed/ggsw_ct.rs
poulpy-core/src/encryption/compressed/ggsw_ct.rsRaw
  1use poulpy_hal::{
  2    api::{ModuleN, VecZnxAddScalarInplace, VecZnxNormalizeInplace},
  3    layouts::{Backend, DataMut, Module, ScalarZnx, ScalarZnxToRef, Scratch, ZnxZero},
  4    source::Source,
  5};
  6
  7use crate::{
  8    ScratchTakeCore,
  9    encryption::{SIGMA, ggsw_ct::GGSWEncryptSk, glwe_ct::GLWEEncryptSkInternal},
 10    layouts::{
 11        GGSWCompressedSeedMut, GGSWInfos, GLWEInfos, LWEInfos,
 12        compressed::{GGSWCompressed, GGSWCompressedToMut},
 13        prepared::{GLWESecretPrepared, GLWESecretPreparedToRef},
 14    },
 15};
 16
 17impl GGSWCompressed<Vec<u8>> {
 18    pub fn encrypt_sk_tmp_bytes<M, A, BE: Backend>(module: &M, infos: &A) -> usize
 19    where
 20        A: GGSWInfos,
 21        M: GGSWCompressedEncryptSk<BE>,
 22    {
 23        module.ggsw_compressed_encrypt_sk_tmp_bytes(infos)
 24    }
 25}
 26
 27impl<DataSelf: DataMut> GGSWCompressed<DataSelf> {
 28    #[allow(clippy::too_many_arguments)]
 29    pub fn encrypt_sk<P, S, M, BE: Backend>(
 30        &mut self,
 31        module: &M,
 32        pt: &P,
 33        sk: &S,
 34        seed_xa: [u8; 32],
 35        source_xe: &mut Source,
 36        scratch: &mut Scratch<BE>,
 37    ) where
 38        P: ScalarZnxToRef,
 39        S: GLWESecretPreparedToRef<BE>,
 40        M: GGSWCompressedEncryptSk<BE>,
 41    {
 42        module.ggsw_compressed_encrypt_sk(self, pt, sk, seed_xa, source_xe, scratch);
 43    }
 44}
 45
 46pub trait GGSWCompressedEncryptSk<BE: Backend> {
 47    fn ggsw_compressed_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
 48    where
 49        A: GGSWInfos;
 50
 51    fn ggsw_compressed_encrypt_sk<R, P, S>(
 52        &self,
 53        res: &mut R,
 54        pt: &P,
 55        sk: &S,
 56        seed_xa: [u8; 32],
 57        source_xe: &mut Source,
 58        scratch: &mut Scratch<BE>,
 59    ) where
 60        R: GGSWCompressedToMut + GGSWCompressedSeedMut,
 61        P: ScalarZnxToRef,
 62        S: GLWESecretPreparedToRef<BE>;
 63}
 64
 65impl<BE: Backend> GGSWCompressedEncryptSk<BE> for Module<BE>
 66where
 67    Self: ModuleN + GLWEEncryptSkInternal<BE> + GGSWEncryptSk<BE> + VecZnxAddScalarInplace + VecZnxNormalizeInplace<BE>,
 68    Scratch<BE>: ScratchTakeCore<BE>,
 69{
 70    fn ggsw_compressed_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
 71    where
 72        A: GGSWInfos,
 73    {
 74        self.ggsw_encrypt_sk_tmp_bytes(infos)
 75    }
 76
 77    fn ggsw_compressed_encrypt_sk<R, P, S>(
 78        &self,
 79        res: &mut R,
 80        pt: &P,
 81        sk: &S,
 82        seed_xa: [u8; 32],
 83        source_xe: &mut Source,
 84        scratch: &mut Scratch<BE>,
 85    ) where
 86        R: GGSWCompressedToMut + GGSWCompressedSeedMut,
 87        P: ScalarZnxToRef,
 88        S: GLWESecretPreparedToRef<BE>,
 89    {
 90        let mut seeds: Vec<[u8; 32]> = vec![[0u8; 32]; res.seed_mut().len()];
 91
 92        {
 93            let res: &mut GGSWCompressed<&mut [u8]> = &mut res.to_mut();
 94            let sk: &GLWESecretPrepared<&[u8], BE> = &sk.to_ref();
 95            let pt: &ScalarZnx<&[u8]> = &pt.to_ref();
 96
 97            #[cfg(debug_assertions)]
 98            {
 99                use poulpy_hal::layouts::ZnxInfos;
100
101                assert_eq!(res.rank(), sk.rank());
102                assert_eq!(res.n(), sk.n());
103                assert_eq!(pt.n() as u32, sk.n());
104            }
105
106            let base2k: usize = res.base2k().into();
107            let rank: usize = res.rank().into();
108            let cols: usize = rank + 1;
109            let dsize: usize = res.dsize().into();
110
111            let (mut tmp_pt, scratch_1) = scratch.take_glwe_pt(self, &res.glwe_layout());
112
113            let mut source = Source::new(seed_xa);
114
115            for row_i in 0..res.dnum().into() {
116                tmp_pt.data.zero();
117
118                // Adds the scalar_znx_pt to the i-th limb of the vec_znx_pt
119                self.vec_znx_add_scalar_inplace(&mut tmp_pt.data, 0, (dsize - 1) + row_i * dsize, pt, 0);
120                self.vec_znx_normalize_inplace(base2k, &mut tmp_pt.data, 0, scratch_1);
121
122                for col_j in 0..rank + 1 {
123                    // rlwe encrypt of vec_znx_pt into vec_znx_ct
124
125                    let (seed, mut source_xa_tmp) = source.branch();
126
127                    seeds[row_i * cols + col_j] = seed;
128
129                    self.glwe_encrypt_sk_internal(
130                        res.base2k().into(),
131                        res.k().into(),
132                        &mut res.at_mut(row_i, col_j).data,
133                        cols,
134                        true,
135                        Some((&tmp_pt, col_j)),
136                        sk,
137                        &mut source_xa_tmp,
138                        source_xe,
139                        SIGMA,
140                        scratch_1,
141                    );
142                }
143            }
144        }
145
146        res.seed_mut().copy_from_slice(&seeds);
147    }
148}