arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/poulpy-core/src/encryption/gglwe_ct.rs
poulpy-core/src/encryption/gglwe_ct.rsRaw
  1use poulpy_hal::{
  2    api::{
  3        ScratchAvailable, TakeVecZnx, TakeVecZnxDft, VecZnxAddScalarInplace, VecZnxDftBytesOf, VecZnxNormalizeInplace,
  4        VecZnxNormalizeTmpBytes,
  5    },
  6    layouts::{Backend, DataMut, DataRef, Module, ScalarZnx, ScalarZnxToRef, Scratch, ZnxZero},
  7    source::Source,
  8};
  9
 10use crate::{
 11    TakeGLWEPlaintext,
 12    encryption::glwe_ct::GLWEEncryptSk,
 13    layouts::{
 14        GGLWE, GGLWEInfos, GGLWEToMut, GLWE, GLWEPlaintext, LWEInfos,
 15        prepared::{GLWESecretPrepared, GLWESecretPreparedToRef},
 16    },
 17};
 18
 19impl GGLWE<Vec<u8>> {
 20    pub fn encrypt_sk_scratch_space<B: Backend, A>(module: &Module<B>, infos: &A) -> usize
 21    where
 22        A: GGLWEInfos,
 23        Module<B>: VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxNormalizeTmpBytes,
 24    {
 25        GLWE::encrypt_sk_scratch_space(module, &infos.glwe_layout())
 26            + (GLWEPlaintext::bytes_of(&infos.glwe_layout()) | module.vec_znx_normalize_tmp_bytes())
 27    }
 28
 29    pub fn encrypt_pk_scratch_space<B: Backend, A>(_module: &Module<B>, _infos: &A) -> usize
 30    where
 31        A: GGLWEInfos,
 32    {
 33        unimplemented!()
 34    }
 35}
 36
 37pub trait GGLWEEncryptSk<B: Backend> {
 38    fn gglwe_encrypt_sk<R, P, S>(
 39        &self,
 40        res: &mut R,
 41        pt: &P,
 42        sk: &S,
 43        source_xa: &mut Source,
 44        source_xe: &mut Source,
 45        scratch: &mut Scratch<B>,
 46    ) where
 47        R: GGLWEToMut,
 48        P: ScalarZnxToRef,
 49        S: GLWESecretPreparedToRef<B>;
 50}
 51
 52impl<B: Backend> GGLWEEncryptSk<B> for Module<B>
 53where
 54    Module<B>: GLWEEncryptSk<B> + VecZnxNormalizeTmpBytes + VecZnxDftBytesOf + VecZnxAddScalarInplace + VecZnxNormalizeInplace<B>,
 55    Scratch<B>: TakeVecZnxDft<B> + ScratchAvailable + TakeVecZnx,
 56{
 57    fn gglwe_encrypt_sk<R, P, S>(
 58        &self,
 59        res: &mut R,
 60        pt: &P,
 61        sk: &S,
 62        source_xa: &mut Source,
 63        source_xe: &mut Source,
 64        scratch: &mut Scratch<B>,
 65    ) where
 66        R: GGLWEToMut,
 67        P: ScalarZnxToRef,
 68        S: GLWESecretPreparedToRef<B>,
 69    {
 70        let res: &mut GGLWE<&mut [u8]> = &mut res.to_mut();
 71        let pt: &ScalarZnx<&[u8]> = &pt.to_ref();
 72
 73        #[cfg(debug_assertions)]
 74        {
 75            use poulpy_hal::layouts::ZnxInfos;
 76            let sk: GLWESecretPrepared<&[u8], B> = sk.to_ref();
 77
 78            assert_eq!(
 79                res.rank_in(),
 80                pt.cols() as u32,
 81                "res.rank_in(): {} != pt.cols(): {}",
 82                res.rank_in(),
 83                pt.cols()
 84            );
 85            assert_eq!(
 86                res.rank_out(),
 87                sk.rank(),
 88                "res.rank_out(): {} != sk.rank(): {}",
 89                res.rank_out(),
 90                sk.rank()
 91            );
 92            assert_eq!(res.n(), sk.n());
 93            assert_eq!(pt.n() as u32, sk.n());
 94            assert!(
 95                scratch.available() >= GGLWE::encrypt_sk_scratch_space(self, res),
 96                "scratch.available: {} < GGLWECiphertext::encrypt_sk_scratch_space(self, res.rank()={}, res.size()={}): {}",
 97                scratch.available(),
 98                res.rank_out(),
 99                res.size(),
100                GGLWE::encrypt_sk_scratch_space(self, res)
101            );
102            assert!(
103                res.dnum().0 * res.dsize().0 * res.base2k().0 <= res.k().0,
104                "res.dnum() : {} * res.dsize() : {} * res.base2k() : {} = {} >= res.k() = {}",
105                res.dnum(),
106                res.dsize(),
107                res.base2k(),
108                res.dnum().0 * res.dsize().0 * res.base2k().0,
109                res.k()
110            );
111        }
112
113        let dnum: usize = res.dnum().into();
114        let dsize: usize = res.dsize().into();
115        let base2k: usize = res.base2k().into();
116        let rank_in: usize = res.rank_in().into();
117
118        let (mut tmp_pt, scrach_1) = scratch.take_glwe_pt(res);
119        // For each input column (i.e. rank) produces a GGLWE ciphertext of rank_out+1 columns
120        //
121        // Example for ksk rank 2 to rank 3:
122        //
123        // (-(a0*s0 + a1*s1 + a2*s2) + s0', a0, a1, a2)
124        // (-(b0*s0 + b1*s1 + b2*s2) + s0', b0, b1, b2)
125        //
126        // Example ksk rank 2 to rank 1
127        //
128        // (-(a*s) + s0, a)
129        // (-(b*s) + s1, b)
130
131        for col_i in 0..rank_in {
132            for row_i in 0..dnum {
133                // Adds the scalar_znx_pt to the i-th limb of the vec_znx_pt
134                tmp_pt.data.zero(); // zeroes for next iteration
135                self.vec_znx_add_scalar_inplace(&mut tmp_pt.data, 0, (dsize - 1) + row_i * dsize, pt, col_i);
136                self.vec_znx_normalize_inplace(base2k, &mut tmp_pt.data, 0, scrach_1);
137                self.glwe_encrypt_sk(
138                    &mut res.at_mut(row_i, col_i),
139                    &tmp_pt,
140                    sk,
141                    source_xa,
142                    source_xe,
143                    scrach_1,
144                );
145            }
146        }
147    }
148}
149
150impl<DataSelf: DataMut> GGLWE<DataSelf> {
151    #[allow(clippy::too_many_arguments)]
152    pub fn encrypt_sk<DataPt: DataRef, DataSk: DataRef, B: Backend>(
153        &mut self,
154        module: &Module<B>,
155        pt: &ScalarZnx<DataPt>,
156        sk: &GLWESecretPrepared<DataSk, B>,
157        source_xa: &mut Source,
158        source_xe: &mut Source,
159        scratch: &mut Scratch<B>,
160    ) where
161        Module<B>: GGLWEEncryptSk<B>,
162    {
163        module.gglwe_encrypt_sk(self, pt, sk, source_xa, source_xe, scratch);
164    }
165}