arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/core/src/encryption/gglwe_ksk.rs
core/src/encryption/gglwe_ksk.rsRaw
  1use backend::hal::{
  2    api::{
  3        ScratchAvailable, SvpApplyInplace, SvpPPolAllocBytes, SvpPrepare, TakeScalarZnx, TakeVecZnx, TakeVecZnxDft,
  4        VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigNormalize, VecZnxDftAllocBytes, VecZnxDftFromVecZnx,
  5        VecZnxDftToVecZnxBigConsume, VecZnxFillUniform, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes,
  6        VecZnxSub, VecZnxSubABInplace, VecZnxSwithcDegree,
  7    },
  8    layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, Scratch},
  9};
 10use sampling::source::Source;
 11
 12use crate::{
 13    TakeGLWESecretPrepared,
 14    layouts::{GGLWECiphertext, GGLWESwitchingKey, GLWESecret, prepared::GLWESecretPrepared},
 15};
 16
 17impl GGLWESwitchingKey<Vec<u8>> {
 18    pub fn encrypt_sk_scratch_space<B: Backend>(
 19        module: &Module<B>,
 20        n: usize,
 21        basek: usize,
 22        k: usize,
 23        rank_in: usize,
 24        rank_out: usize,
 25    ) -> usize
 26    where
 27        Module<B>: SvpPPolAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes,
 28    {
 29        (GGLWECiphertext::encrypt_sk_scratch_space(module, n, basek, k) | ScalarZnx::alloc_bytes(n, 1))
 30            + ScalarZnx::alloc_bytes(n, rank_in)
 31            + GLWESecretPrepared::bytes_of(module, n, rank_out)
 32    }
 33
 34    pub fn encrypt_pk_scratch_space<B: Backend>(
 35        module: &Module<B>,
 36        _n: usize,
 37        _basek: usize,
 38        _k: usize,
 39        _rank_in: usize,
 40        _rank_out: usize,
 41    ) -> usize {
 42        GGLWECiphertext::encrypt_pk_scratch_space(module, _n, _basek, _k, _rank_out)
 43    }
 44}
 45
 46impl<DataSelf: DataMut> GGLWESwitchingKey<DataSelf> {
 47    pub fn encrypt_sk<DataSkIn: DataRef, DataSkOut: DataRef, B: Backend>(
 48        &mut self,
 49        module: &Module<B>,
 50        sk_in: &GLWESecret<DataSkIn>,
 51        sk_out: &GLWESecret<DataSkOut>,
 52        source_xa: &mut Source,
 53        source_xe: &mut Source,
 54        sigma: f64,
 55        scratch: &mut Scratch<B>,
 56    ) where
 57        Module<B>: VecZnxAddScalarInplace
 58            + VecZnxDftAllocBytes
 59            + VecZnxBigNormalize<B>
 60            + VecZnxDftFromVecZnx<B>
 61            + SvpApplyInplace<B>
 62            + VecZnxDftToVecZnxBigConsume<B>
 63            + VecZnxNormalizeTmpBytes
 64            + VecZnxFillUniform
 65            + VecZnxSubABInplace
 66            + VecZnxAddInplace
 67            + VecZnxNormalizeInplace<B>
 68            + VecZnxAddNormal
 69            + VecZnxNormalize<B>
 70            + VecZnxSub
 71            + SvpPrepare<B>
 72            + VecZnxSwithcDegree
 73            + SvpPPolAllocBytes,
 74        Scratch<B>: TakeVecZnxDft<B> + ScratchAvailable + TakeVecZnx + TakeScalarZnx + TakeGLWESecretPrepared<B>,
 75    {
 76        #[cfg(debug_assertions)]
 77        {
 78            use crate::layouts::Infos;
 79
 80            assert!(sk_in.n() <= module.n());
 81            assert!(sk_out.n() <= module.n());
 82            assert!(
 83                scratch.available()
 84                    >= GGLWESwitchingKey::encrypt_sk_scratch_space(
 85                        module,
 86                        sk_out.n(),
 87                        self.basek(),
 88                        self.k(),
 89                        self.rank_in(),
 90                        self.rank_out()
 91                    ),
 92                "scratch.available()={} < GLWESwitchingKey::encrypt_sk_scratch_space={}",
 93                scratch.available(),
 94                GGLWESwitchingKey::encrypt_sk_scratch_space(
 95                    module,
 96                    sk_out.n(),
 97                    self.basek(),
 98                    self.k(),
 99                    self.rank_in(),
100                    self.rank_out()
101                )
102            )
103        }
104
105        let n: usize = sk_in.n().max(sk_out.n());
106
107        let (mut sk_in_tmp, scratch1) = scratch.take_scalar_znx(n, sk_in.rank());
108        (0..sk_in.rank()).for_each(|i| {
109            module.vec_znx_switch_degree(
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, scratch2) = scratch1.take_glwe_secret_prepared(n, sk_out.rank());
118        {
119            let (mut tmp, _) = scratch2.take_scalar_znx(n, 1);
120            (0..sk_out.rank()).for_each(|i| {
121                module.vec_znx_switch_degree(&mut tmp.as_vec_znx_mut(), 0, &sk_out.data.as_vec_znx(), i);
122                module.svp_prepare(&mut sk_out_tmp.data, i, &tmp, 0);
123            });
124        }
125
126        self.key.encrypt_sk(
127            module,
128            &sk_in_tmp,
129            &sk_out_tmp,
130            source_xa,
131            source_xe,
132            sigma,
133            scratch2,
134        );
135        self.sk_in_n = sk_in.n();
136        self.sk_out_n = sk_out.n();
137    }
138}