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, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxDftBytesOf, VecZnxNormalizeTmpBytes, VecZnxSwitchRing},
  3    layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, Scratch},
  4    source::Source,
  5};
  6
  7use crate::{
  8    ScratchTakeCore,
  9    encryption::compressed::gglwe_ct::GGLWECompressedEncryptSk,
 10    layouts::{
 11        GGLWE, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, RingDegree,
 12        compressed::{GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut},
 13        prepared::GLWESecretPrepared,
 14    },
 15};
 16
 17impl GLWESwitchingKeyCompressed<Vec<u8>> {
 18    pub fn encrypt_sk_tmp_bytes<B: Backend, A>(module: &Module<B>, infos: &A) -> usize
 19    where
 20        A: GGLWEInfos,
 21        Module<B>: ModuleN + SvpPPolAlloc<B> + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes + SvpPPolBytesOf,
 22    {
 23        (GGLWE::encrypt_sk_tmp_bytes(module, infos) | ScalarZnx::bytes_of(module.n(), 1))
 24            + ScalarZnx::bytes_of(module.n(), infos.rank_in().into())
 25            + GLWESecretPrepared::bytes_of(module, infos.rank_out())
 26    }
 27}
 28
 29impl<DataSelf: DataMut> GLWESwitchingKeyCompressed<DataSelf> {
 30    #[allow(clippy::too_many_arguments)]
 31    pub fn encrypt_sk<DataSkIn: DataRef, DataSkOut: DataRef, B: Backend>(
 32        &mut self,
 33        module: &Module<B>,
 34        sk_in: &GLWESecret<DataSkIn>,
 35        sk_out: &GLWESecret<DataSkOut>,
 36        seed_xa: [u8; 32],
 37        source_xe: &mut Source,
 38        scratch: &mut Scratch<B>,
 39    ) where
 40        Module<B>: GGLWEKeyCompressedEncryptSk<B>,
 41    {
 42        module.gglwe_key_compressed_encrypt_sk(self, sk_in, sk_out, seed_xa, source_xe, scratch);
 43    }
 44}
 45
 46pub trait GGLWEKeyCompressedEncryptSk<B: Backend> {
 47    fn gglwe_key_compressed_encrypt_sk<R, SI, SO>(
 48        &self,
 49        res: &mut R,
 50        sk_in: &SI,
 51        sk_out: &SO,
 52        seed_xa: [u8; 32],
 53        source_xe: &mut Source,
 54        scratch: &mut Scratch<B>,
 55    ) where
 56        R: GLWESwitchingKeyCompressedToMut,
 57        SI: GLWESecretToRef,
 58        SO: GLWESecretToRef;
 59}
 60
 61impl<B: Backend> GGLWEKeyCompressedEncryptSk<B> for Module<B>
 62where
 63    Module<B>: ModuleN
 64        + GGLWECompressedEncryptSk<B>
 65        + SvpPPolBytesOf
 66        + VecZnxNormalizeTmpBytes
 67        + VecZnxDftBytesOf
 68        + VecZnxSwitchRing
 69        + SvpPrepare<B>
 70        + SvpPPolAlloc<B>,
 71    Scratch<B>: ScratchAvailable + ScratchTakeBasic + ScratchTakeCore<B>,
 72{
 73    fn gglwe_key_compressed_encrypt_sk<R, SI, SO>(
 74        &self,
 75        res: &mut R,
 76        sk_in: &SI,
 77        sk_out: &SO,
 78        seed_xa: [u8; 32],
 79        source_xe: &mut Source,
 80        scratch: &mut Scratch<B>,
 81    ) where
 82        R: GLWESwitchingKeyCompressedToMut,
 83        SI: GLWESecretToRef,
 84        SO: GLWESecretToRef,
 85    {
 86        let res: &mut GLWESwitchingKeyCompressed<&mut [u8]> = &mut res.to_mut();
 87        let sk_in: &GLWESecret<&[u8]> = &sk_in.to_ref();
 88        let sk_out: &GLWESecret<&[u8]> = &sk_out.to_ref();
 89
 90        #[cfg(debug_assertions)]
 91        {
 92            use crate::layouts::GLWESwitchingKey;
 93
 94            assert!(sk_in.n().0 <= self.n() as u32);
 95            assert!(sk_out.n().0 <= self.n() as u32);
 96            assert!(
 97                scratch.available() >= GLWESwitchingKey::encrypt_sk_tmp_bytes(self, res),
 98                "scratch.available()={} < GLWESwitchingKey::encrypt_sk_tmp_bytes={}",
 99                scratch.available(),
100                GLWESwitchingKey::encrypt_sk_tmp_bytes(self, res)
101            )
102        }
103
104        let n: usize = sk_in.n().max(sk_out.n()).into();
105
106        let (mut sk_in_tmp, scratch_1) = scratch.take_scalar_znx(self, sk_in.rank().into());
107        (0..sk_in.rank().into()).for_each(|i| {
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            (0..sk_out.rank().into()).for_each(|i| {
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(
126            &mut res.key,
127            &sk_in_tmp,
128            &sk_out_tmp,
129            seed_xa,
130            source_xe,
131            scratch_2,
132        );
133        res.sk_in_n = sk_in.n().into();
134        res.sk_out_n = sk_out.n().into();
135    }
136}