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