arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/poulpy-core/src/encryption/gglwe_ksk.rs
poulpy-core/src/encryption/gglwe_ksk.rsRaw
  1use poulpy_hal::{
  2    api::{
  3        ScratchAvailable, SvpApplyDftToDftInplace, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal,
  4        VecZnxAddScalarInplace, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume,
  5        VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing,
  6    },
  7    layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, Scratch},
  8    source::Source,
  9};
 10
 11use crate::layouts::{
 12    GGLWE, GGLWEInfos, GLWEInfos, GLWESecret, GLWESwitchingKey, LWEInfos, RingDegree, prepared::GLWESecretPrepared,
 13};
 14
 15impl GLWESwitchingKey<Vec<u8>> {
 16    pub fn encrypt_sk_tmp_bytes<B: Backend, A>(module: &Module<B>, infos: &A) -> usize
 17    where
 18        A: GGLWEInfos,
 19        Module<B>: SvpPPolBytesOf + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes,
 20    {
 21        (GGLWE::encrypt_sk_tmp_bytes(module, infos) | ScalarZnx::bytes_of(module.n(), 1))
 22            + ScalarZnx::bytes_of(module.n(), infos.rank_in().into())
 23            + GLWESecretPrepared::bytes_of_from_infos(module, &infos.glwe_layout())
 24    }
 25
 26    pub fn encrypt_pk_tmp_bytes<B: Backend, A>(module: &Module<B>, _infos: &A) -> usize
 27    where
 28        A: GGLWEInfos,
 29    {
 30        GGLWE::encrypt_pk_tmp_bytes(module, _infos)
 31    }
 32}
 33
 34impl<DataSelf: DataMut> GLWESwitchingKey<DataSelf> {
 35    #[allow(clippy::too_many_arguments)]
 36    pub fn encrypt_sk<DataSkIn: DataRef, DataSkOut: DataRef, B: Backend>(
 37        &mut self,
 38        module: &Module<B>,
 39        sk_in: &GLWESecret<DataSkIn>,
 40        sk_out: &GLWESecret<DataSkOut>,
 41        source_xa: &mut Source,
 42        source_xe: &mut Source,
 43        scratch: &mut Scratch<B>,
 44    ) where
 45        Module<B>: VecZnxAddScalarInplace
 46            + VecZnxDftBytesOf
 47            + VecZnxBigNormalize<B>
 48            + VecZnxDftApply<B>
 49            + SvpApplyDftToDftInplace<B>
 50            + VecZnxIdftApplyConsume<B>
 51            + VecZnxNormalizeTmpBytes
 52            + VecZnxFillUniform
 53            + VecZnxSubInplace
 54            + VecZnxAddInplace
 55            + VecZnxNormalizeInplace<B>
 56            + VecZnxAddNormal
 57            + VecZnxNormalize<B>
 58            + VecZnxSub
 59            + SvpPrepare<B>
 60            + VecZnxSwitchRing
 61            + SvpPPolBytesOf,
 62        Scratch<B>: ScratchAvailable,
 63    {
 64        #[cfg(debug_assertions)]
 65        {
 66            assert!(sk_in.n().0 <= module.n() as u32);
 67            assert!(sk_out.n().0 <= module.n() as u32);
 68            assert!(
 69                scratch.available() >= GLWESwitchingKey::encrypt_sk_tmp_bytes(module, self),
 70                "scratch.available()={} < GLWESwitchingKey::encrypt_sk_tmp_bytes={}",
 71                scratch.available(),
 72                GLWESwitchingKey::encrypt_sk_tmp_bytes(module, self)
 73            )
 74        }
 75
 76        let n: usize = sk_in.n().max(sk_out.n()).into();
 77
 78        let (mut sk_in_tmp, scratch_1) = scratch.take_scalar_znx(n, sk_in.rank().into());
 79        (0..sk_in.rank().into()).for_each(|i| {
 80            module.vec_znx_switch_ring(
 81                &mut sk_in_tmp.as_vec_znx_mut(),
 82                i,
 83                &sk_in.data.as_vec_znx(),
 84                i,
 85            );
 86        });
 87
 88        let (mut sk_out_tmp, scratch_2) = scratch_1.take_glwe_secret_prepared(RingDegree(n as u32), sk_out.rank());
 89        {
 90            let (mut tmp, _) = scratch_2.take_scalar_znx(n, 1);
 91            (0..sk_out.rank().into()).for_each(|i| {
 92                module.vec_znx_switch_ring(&mut tmp.as_vec_znx_mut(), 0, &sk_out.data.as_vec_znx(), i);
 93                module.svp_prepare(&mut sk_out_tmp.data, i, &tmp, 0);
 94            });
 95        }
 96
 97        self.key.encrypt_sk(
 98            module,
 99            &sk_in_tmp,
100            &sk_out_tmp,
101            source_xa,
102            source_xe,
103            scratch_2,
104        );
105        self.sk_in_n = sk_in.n().into();
106        self.sk_out_n = sk_out.n().into();
107    }
108}