arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/poulpy-core/src/encryption/compressed/gglwe_ksk.rs
poulpy-core/src/encryption/compressed/gglwe_ksk.rsRaw
  1use poulpy_hal::{
  2    api::{
  3        ScratchAvailable, SvpPPolAllocBytes, SvpPrepare, TakeScalarZnx, VecZnxDftAllocBytes, VecZnxNormalizeTmpBytes,
  4        VecZnxSwitchRing,
  5    },
  6    layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, Scratch},
  7    source::Source,
  8};
  9
 10use crate::{
 11    TakeGLWESecretPrepared,
 12    encryption::compressed::gglwe_ct::GGLWECompressedEncryptSk,
 13    layouts::{
 14        Degree, GGLWECiphertext, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos,
 15        compressed::{GGLWEKeyCompressed, GGLWEKeyCompressedToMut},
 16        prepared::GLWESecretPrepared,
 17    },
 18};
 19
 20impl GGLWEKeyCompressed<Vec<u8>> {
 21    pub fn encrypt_sk_scratch_space<B: Backend, A>(module: &Module<B>, infos: &A) -> usize
 22    where
 23        A: GGLWEInfos,
 24        Module<B>: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes + SvpPPolAllocBytes,
 25    {
 26        (GGLWECiphertext::encrypt_sk_scratch_space(module, infos) | ScalarZnx::alloc_bytes(module.n(), 1))
 27            + ScalarZnx::alloc_bytes(module.n(), infos.rank_in().into())
 28            + GLWESecretPrepared::alloc_bytes_with(module, infos.rank_out())
 29    }
 30}
 31
 32impl<DataSelf: DataMut> GGLWEKeyCompressed<DataSelf> {
 33    #[allow(clippy::too_many_arguments)]
 34    pub fn encrypt_sk<DataSkIn: DataRef, DataSkOut: DataRef, B: Backend>(
 35        &mut self,
 36        module: &Module<B>,
 37        sk_in: &GLWESecret<DataSkIn>,
 38        sk_out: &GLWESecret<DataSkOut>,
 39        seed_xa: [u8; 32],
 40        source_xe: &mut Source,
 41        scratch: &mut Scratch<B>,
 42    ) where
 43        Module<B>: GGLWEKeyCompressedEncryptSk<B>,
 44    {
 45        module.gglwe_key_compressed_encrypt_sk(self, sk_in, sk_out, seed_xa, source_xe, scratch);
 46    }
 47}
 48
 49pub trait GGLWEKeyCompressedEncryptSk<B: Backend> {
 50    fn gglwe_key_compressed_encrypt_sk<R, SI, SO>(
 51        &self,
 52        res: &mut R,
 53        sk_in: &SI,
 54        sk_out: &SO,
 55        seed_xa: [u8; 32],
 56        source_xe: &mut Source,
 57        scratch: &mut Scratch<B>,
 58    ) where
 59        R: GGLWEKeyCompressedToMut,
 60        SI: GLWESecretToRef,
 61        SO: GLWESecretToRef;
 62}
 63
 64impl<B: Backend> GGLWEKeyCompressedEncryptSk<B> for Module<B>
 65where
 66    Module<B>: GGLWECompressedEncryptSk<B>
 67        + SvpPPolAllocBytes
 68        + VecZnxNormalizeTmpBytes
 69        + VecZnxDftAllocBytes
 70        + VecZnxSwitchRing
 71        + SvpPrepare<B>,
 72    Scratch<B>: ScratchAvailable + TakeScalarZnx + TakeGLWESecretPrepared<B>,
 73{
 74    fn gglwe_key_compressed_encrypt_sk<R, SI, SO>(
 75        &self,
 76        res: &mut R,
 77        sk_in: &SI,
 78        sk_out: &SO,
 79        seed_xa: [u8; 32],
 80        source_xe: &mut Source,
 81        scratch: &mut Scratch<B>,
 82    ) where
 83        R: GGLWEKeyCompressedToMut,
 84        SI: GLWESecretToRef,
 85        SO: GLWESecretToRef,
 86    {
 87        let res: &mut GGLWEKeyCompressed<&mut [u8]> = &mut res.to_mut();
 88        let sk_in: &GLWESecret<&[u8]> = &sk_in.to_ref();
 89        let sk_out: &GLWESecret<&[u8]> = &sk_out.to_ref();
 90
 91        #[cfg(debug_assertions)]
 92        {
 93            use crate::layouts::GGLWESwitchingKey;
 94
 95            assert!(sk_in.n().0 <= self.n() as u32);
 96            assert!(sk_out.n().0 <= self.n() as u32);
 97            assert!(
 98                scratch.available() >= GGLWESwitchingKey::encrypt_sk_scratch_space(self, res),
 99                "scratch.available()={} < GLWESwitchingKey::encrypt_sk_scratch_space={}",
100                scratch.available(),
101                GGLWESwitchingKey::encrypt_sk_scratch_space(self, res)
102            )
103        }
104
105        let n: usize = sk_in.n().max(sk_out.n()).into();
106
107        let (mut sk_in_tmp, scratch_1) = scratch.take_scalar_znx(n, sk_in.rank().into());
108        (0..sk_in.rank().into()).for_each(|i| {
109            self.vec_znx_switch_ring(
110                &mut sk_in_tmp.as_vec_znx_mut(),
111                i,
112                &sk_in.data.as_vec_znx(),
113                i,
114            );
115        });
116
117        let (mut sk_out_tmp, scratch_2) = scratch_1.take_glwe_secret_prepared(Degree(n as u32), sk_out.rank());
118        {
119            let (mut tmp, _) = scratch_2.take_scalar_znx(n, 1);
120            (0..sk_out.rank().into()).for_each(|i| {
121                self.vec_znx_switch_ring(&mut tmp.as_vec_znx_mut(), 0, &sk_out.data.as_vec_znx(), i);
122                self.svp_prepare(&mut sk_out_tmp.data, i, &tmp, 0);
123            });
124        }
125
126        self.gglwe_compressed_encrypt_sk(
127            &mut res.key,
128            &sk_in_tmp,
129            &sk_out_tmp,
130            seed_xa,
131            source_xe,
132            scratch_2,
133        );
134        res.sk_in_n = sk_in.n().into();
135        res.sk_out_n = sk_out.n().into();
136    }
137}