arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/poulpy-core/src/encryption/compressed/glwe_switching_key.rs
poulpy-core/src/encryption/compressed/glwe_switching_key.rsRaw
  1use poulpy_hal::{
  2    api::{ModuleN, ScratchAvailable, ScratchTakeBasic, SvpPrepare, VecZnxSwitchRing},
  3    layouts::{Backend, DataMut, Module, ScalarZnx, Scratch},
  4    source::Source,
  5};
  6
  7use crate::{
  8    GGLWECompressedEncryptSk, ScratchTakeCore,
  9    layouts::{
 10        GGLWECompressedSeedMut, GGLWECompressedToMut, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef,
 11        GLWESwitchingKeyDegreesMut, LWEInfos,
 12        compressed::GLWESwitchingKeyCompressed,
 13        prepared::{GLWESecretPrepared, GLWESecretPreparedFactory},
 14    },
 15};
 16
 17impl GLWESwitchingKeyCompressed<Vec<u8>> {
 18    pub fn encrypt_sk_tmp_bytes<M, A, BE: Backend>(module: &M, infos: &A) -> usize
 19    where
 20        A: GGLWEInfos,
 21        M: GLWESwitchingKeyCompressedEncryptSk<BE>,
 22    {
 23        module.glwe_switching_key_compressed_encrypt_sk_tmp_bytes(infos)
 24    }
 25}
 26
 27impl<D: DataMut> GLWESwitchingKeyCompressed<D> {
 28    #[allow(clippy::too_many_arguments)]
 29    pub fn encrypt_sk<M, S1, S2, BE: Backend>(
 30        &mut self,
 31        module: &M,
 32        sk_in: &S1,
 33        sk_out: &S2,
 34        seed_xa: [u8; 32],
 35        source_xe: &mut Source,
 36        scratch: &mut Scratch<BE>,
 37    ) where
 38        S1: GLWESecretToRef,
 39        S2: GLWESecretToRef,
 40        M: GLWESwitchingKeyCompressedEncryptSk<BE>,
 41    {
 42        module.glwe_switching_key_compressed_encrypt_sk(self, sk_in, sk_out, seed_xa, source_xe, scratch);
 43    }
 44}
 45
 46pub trait GLWESwitchingKeyCompressedEncryptSk<BE: Backend> {
 47    fn glwe_switching_key_compressed_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
 48    where
 49        A: GGLWEInfos;
 50
 51    fn glwe_switching_key_compressed_encrypt_sk<R, S1, S2>(
 52        &self,
 53        res: &mut R,
 54        sk_in: &S1,
 55        sk_out: &S2,
 56        seed_xa: [u8; 32],
 57        source_xe: &mut Source,
 58        scratch: &mut Scratch<BE>,
 59    ) where
 60        R: GGLWECompressedToMut + GGLWECompressedSeedMut + GLWESwitchingKeyDegreesMut + GGLWEInfos,
 61        S1: GLWESecretToRef,
 62        S2: GLWESecretToRef;
 63}
 64
 65impl<BE: Backend> GLWESwitchingKeyCompressedEncryptSk<BE> for Module<BE>
 66where
 67    Self: ModuleN + GGLWECompressedEncryptSk<BE> + GLWESecretPreparedFactory<BE> + VecZnxSwitchRing,
 68    Scratch<BE>: ScratchTakeCore<BE>,
 69{
 70    fn glwe_switching_key_compressed_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
 71    where
 72        A: GGLWEInfos,
 73    {
 74        self.gglwe_compressed_encrypt_sk_tmp_bytes(infos)
 75            .max(ScalarZnx::bytes_of(self.n(), 1))
 76            + ScalarZnx::bytes_of(self.n(), infos.rank_in().into())
 77            + GLWESecretPrepared::bytes_of(self, infos.rank_out())
 78    }
 79
 80    fn glwe_switching_key_compressed_encrypt_sk<R, S1, S2>(
 81        &self,
 82        res: &mut R,
 83        sk_in: &S1,
 84        sk_out: &S2,
 85        seed_xa: [u8; 32],
 86        source_xe: &mut Source,
 87        scratch: &mut Scratch<BE>,
 88    ) where
 89        R: GGLWECompressedToMut + GGLWECompressedSeedMut + GLWESwitchingKeyDegreesMut + GGLWEInfos,
 90        S1: GLWESecretToRef,
 91        S2: GLWESecretToRef,
 92    {
 93        let sk_in: &GLWESecret<&[u8]> = &sk_in.to_ref();
 94        let sk_out: &GLWESecret<&[u8]> = &sk_out.to_ref();
 95
 96        assert!(sk_in.n().0 <= self.n() as u32);
 97        assert!(sk_out.n().0 <= self.n() as u32);
 98        assert!(
 99            scratch.available() >= self.gglwe_compressed_encrypt_sk_tmp_bytes(res),
100            "scratch.available()={} < GLWESwitchingKey::encrypt_sk_tmp_bytes={}",
101            scratch.available(),
102            self.gglwe_compressed_encrypt_sk_tmp_bytes(res)
103        );
104
105        let (mut sk_in_tmp, scratch_1) = scratch.take_scalar_znx(self, sk_in.rank().into());
106        for i in 0..sk_in.rank().into() {
107            self.vec_znx_switch_ring(
108                &mut sk_in_tmp.as_vec_znx_mut(),
109                i,
110                &sk_in.data.as_vec_znx(),
111                i,
112            );
113        }
114
115        let (mut sk_out_tmp, scratch_2) = scratch_1.take_glwe_secret_prepared(self, sk_out.rank());
116        {
117            let (mut tmp, _) = scratch_2.take_scalar_znx(self, 1);
118            for i in 0..sk_out.rank().into() {
119                self.vec_znx_switch_ring(&mut tmp.as_vec_znx_mut(), 0, &sk_out.data.as_vec_znx(), i);
120                self.svp_prepare(&mut sk_out_tmp.data, i, &tmp, 0);
121            }
122        }
123
124        self.gglwe_compressed_encrypt_sk(res, &sk_in_tmp, &sk_out_tmp, seed_xa, source_xe, scratch_2);
125
126        *res.input_degree() = sk_in.n();
127        *res.output_degree() = sk_out.n();
128    }
129}