arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/core/src/ggsw_ciphertext.rs
core/src/ggsw_ciphertext.rsRaw
  1use backend::{
  2    Backend, FFT64, MatZnxDft, MatZnxDftAlloc, MatZnxDftOps, MatZnxDftScratch, Module, ScalarZnx, Scratch, VecZnxAlloc,
  3    VecZnxBigAlloc, VecZnxBigOps, VecZnxBigScratch, VecZnxDft, VecZnxDftAlloc, VecZnxDftOps, VecZnxOps, VecZnxToMut, ZnxInfos,
  4    ZnxZero,
  5};
  6use sampling::source::Source;
  7
  8use crate::{
  9    ScratchCore,
 10    automorphism::AutomorphismKey,
 11    elem::{GetRow, Infos, SetRow},
 12    glwe_ciphertext::GLWECiphertext,
 13    glwe_ciphertext_fourier::GLWECiphertextFourier,
 14    keys::SecretKeyFourier,
 15    keyswitch_key::GLWESwitchingKey,
 16    tensor_key::TensorKey,
 17    utils::derive_size,
 18};
 19
 20pub struct GGSWCiphertext<C, B: Backend> {
 21    pub data: MatZnxDft<C, B>,
 22    pub basek: usize,
 23    pub k: usize,
 24}
 25
 26impl<B: Backend> GGSWCiphertext<Vec<u8>, B> {
 27    pub fn alloc(module: &Module<B>, basek: usize, k: usize, rows: usize, rank: usize) -> Self {
 28        Self {
 29            data: module.new_mat_znx_dft(rows, rank + 1, rank + 1, derive_size(basek, k)),
 30            basek: basek,
 31            k: k,
 32        }
 33    }
 34
 35    pub fn bytes_of(module: &Module<FFT64>, basek: usize, k: usize, rows: usize, rank: usize) -> usize {
 36        module.bytes_of_mat_znx_dft(rows, rank + 1, rank + 1, derive_size(basek, k))
 37    }
 38}
 39
 40impl<T, B: Backend> Infos for GGSWCiphertext<T, B> {
 41    type Inner = MatZnxDft<T, B>;
 42
 43    fn inner(&self) -> &Self::Inner {
 44        &self.data
 45    }
 46
 47    fn basek(&self) -> usize {
 48        self.basek
 49    }
 50
 51    fn k(&self) -> usize {
 52        self.k
 53    }
 54}
 55
 56impl<T, B: Backend> GGSWCiphertext<T, B> {
 57    pub fn rank(&self) -> usize {
 58        self.data.cols_out() - 1
 59    }
 60}
 61
 62impl GGSWCiphertext<Vec<u8>, FFT64> {
 63    pub fn encrypt_sk_scratch_space(module: &Module<FFT64>, rank: usize, size: usize) -> usize {
 64        GLWECiphertext::encrypt_sk_scratch_space(module, size)
 65            + module.bytes_of_vec_znx(rank + 1, size)
 66            + module.bytes_of_vec_znx(1, size)
 67            + module.bytes_of_vec_znx_dft(rank + 1, size)
 68    }
 69
 70    pub(crate) fn expand_row_scratch_space(
 71        module: &Module<FFT64>,
 72        self_size: usize,
 73        tensor_key_size: usize,
 74        rank: usize,
 75    ) -> usize {
 76        let tmp_dft_i: usize = module.bytes_of_vec_znx_dft(rank + 1, tensor_key_size);
 77        let tmp_dft_col_data: usize = module.bytes_of_vec_znx_dft(1, self_size);
 78        let vmp: usize =
 79            tmp_dft_col_data + module.vmp_apply_tmp_bytes(self_size, self_size, self_size, rank, rank, tensor_key_size);
 80        let tmp_idft: usize = module.bytes_of_vec_znx_big(1, tensor_key_size);
 81        let norm: usize = module.vec_znx_big_normalize_tmp_bytes();
 82        tmp_dft_i + ((tmp_dft_col_data + vmp) | (tmp_idft + norm))
 83    }
 84
 85    pub(crate) fn keyswitch_internal_col0_scratch_space(
 86        module: &Module<FFT64>,
 87        out_size: usize,
 88        in_size: usize,
 89        ksk_size: usize,
 90        rank: usize,
 91    ) -> usize {
 92        GLWECiphertext::keyswitch_from_fourier_scratch_space(module, out_size, rank, in_size, rank, ksk_size)
 93            + module.bytes_of_vec_znx_dft(rank + 1, in_size)
 94    }
 95
 96    pub fn keyswitch_scratch_space(
 97        module: &Module<FFT64>,
 98        out_size: usize,
 99        in_size: usize,
100        ksk_size: usize,
101        tensor_key_size: usize,
102        rank: usize,
103    ) -> usize {
104        let res_znx: usize = module.bytes_of_vec_znx(rank + 1, out_size);
105        let ci_dft: usize = module.bytes_of_vec_znx_dft(rank + 1, out_size);
106        let ks: usize = GGSWCiphertext::keyswitch_internal_col0_scratch_space(module, out_size, in_size, ksk_size, rank);
107        let expand_rows: usize = GGSWCiphertext::expand_row_scratch_space(module, out_size, tensor_key_size, rank);
108        let res_dft: usize = module.bytes_of_vec_znx_dft(rank + 1, out_size);
109        res_znx + ci_dft + (ks | expand_rows | res_dft)
110    }
111
112    pub fn keyswitch_inplace_scratch_space(
113        module: &Module<FFT64>,
114        out_size: usize,
115        ksk_size: usize,
116        tensor_key_size: usize,
117        rank: usize,
118    ) -> usize {
119        GGSWCiphertext::keyswitch_scratch_space(module, out_size, out_size, ksk_size, tensor_key_size, rank)
120    }
121
122    pub fn automorphism_scratch_space(
123        module: &Module<FFT64>,
124        out_size: usize,
125        in_size: usize,
126        auto_key_size: usize,
127        tensor_key_size: usize,
128        rank: usize,
129    ) -> usize {
130        let cols: usize = rank + 1;
131        let res: usize = module.bytes_of_vec_znx(cols, out_size);
132        let res_dft: usize = module.bytes_of_vec_znx_dft(cols, out_size);
133        let ci_dft: usize = module.bytes_of_vec_znx_dft(cols, out_size);
134        let ks_internal: usize =
135            GGSWCiphertext::keyswitch_internal_col0_scratch_space(module, out_size, in_size, auto_key_size, rank);
136        let expand: usize = GGSWCiphertext::expand_row_scratch_space(module, out_size, tensor_key_size, rank);
137        res + ci_dft + (ks_internal | expand | res_dft)
138    }
139
140    pub fn automorphism_inplace_scratch_space(
141        module: &Module<FFT64>,
142        out_size: usize,
143        auto_key_size: usize,
144        tensor_key_size: usize,
145        rank: usize,
146    ) -> usize {
147        GGSWCiphertext::automorphism_scratch_space(
148            module,
149            out_size,
150            out_size,
151            auto_key_size,
152            tensor_key_size,
153            rank,
154        )
155    }
156
157    pub fn external_product_scratch_space(
158        module: &Module<FFT64>,
159        out_size: usize,
160        in_size: usize,
161        ggsw_size: usize,
162        rank: usize,
163    ) -> usize {
164        let tmp_in: usize = module.bytes_of_vec_znx_dft(rank + 1, in_size);
165        let tmp_out: usize = module.bytes_of_vec_znx_dft(rank + 1, out_size);
166        let ggsw: usize = GLWECiphertextFourier::external_product_scratch_space(module, out_size, in_size, ggsw_size, rank);
167        tmp_in + tmp_out + ggsw
168    }
169
170    pub fn external_product_inplace_scratch_space(
171        module: &Module<FFT64>,
172        out_size: usize,
173        ggsw_size: usize,
174        rank: usize,
175    ) -> usize {
176        let tmp: usize = module.bytes_of_vec_znx_dft(rank + 1, out_size);
177        let ggsw: usize = GLWECiphertextFourier::external_product_inplace_scratch_space(module, out_size, ggsw_size, rank);
178        tmp + ggsw
179    }
180}
181
182impl<DataSelf: AsMut<[u8]> + AsRef<[u8]>> GGSWCiphertext<DataSelf, FFT64> {
183    pub fn encrypt_sk<DataPt: AsRef<[u8]>, DataSk: AsRef<[u8]>>(
184        &mut self,
185        module: &Module<FFT64>,
186        pt: &ScalarZnx<DataPt>,
187        sk_dft: &SecretKeyFourier<DataSk, FFT64>,
188        source_xa: &mut Source,
189        source_xe: &mut Source,
190        sigma: f64,
191        scratch: &mut Scratch,
192    ) {
193        #[cfg(debug_assertions)]
194        {
195            assert_eq!(self.rank(), sk_dft.rank());
196            assert_eq!(self.n(), module.n());
197            assert_eq!(pt.n(), module.n());
198            assert_eq!(sk_dft.n(), module.n());
199        }
200
201        let basek: usize = self.basek();
202        let k: usize = self.k();
203        let rank: usize = self.rank();
204
205        let (mut tmp_pt, scratch1) = scratch.tmp_glwe_pt(module, basek, k);
206        let (mut tmp_ct, scratch2) = scratch1.tmp_glwe_ct(module, basek, k, rank);
207
208        (0..self.rows()).for_each(|row_i| {
209            tmp_pt.data.zero();
210
211            // Adds the scalar_znx_pt to the i-th limb of the vec_znx_pt
212            module.vec_znx_add_scalar_inplace(&mut tmp_pt.data, 0, row_i, pt, 0);
213            module.vec_znx_normalize_inplace(basek, &mut tmp_pt.data, 0, scratch2);
214
215            (0..rank + 1).for_each(|col_j| {
216                // rlwe encrypt of vec_znx_pt into vec_znx_ct
217
218                tmp_ct.encrypt_sk_private(
219                    module,
220                    Some((&tmp_pt, col_j)),
221                    sk_dft,
222                    source_xa,
223                    source_xe,
224                    sigma,
225                    scratch2,
226                );
227
228                // Switch vec_znx_ct into DFT domain
229                {
230                    let (mut tmp_ct_dft, _) = scratch2.tmp_glwe_fourier(module, basek, k, rank);
231                    tmp_ct.dft(module, &mut tmp_ct_dft);
232                    self.set_row(module, row_i, col_j, &tmp_ct_dft);
233                }
234            });
235        });
236    }
237
238    pub(crate) fn expand_row<R, DataCi: AsRef<[u8]>, DataTsk: AsRef<[u8]>>(
239        &mut self,
240        module: &Module<FFT64>,
241        col_j: usize,
242        res: &mut R,
243        ci_dft: &VecZnxDft<DataCi, FFT64>,
244        tsk: &TensorKey<DataTsk, FFT64>,
245        scratch: &mut Scratch,
246    ) where
247        R: VecZnxToMut,
248    {
249        let cols: usize = self.rank() + 1;
250
251        assert!(scratch.available() >= GGSWCiphertext::expand_row_scratch_space(module, self.size(), tsk.size(), tsk.rank()));
252
253        // Example for rank 3:
254        //
255        // Note: M is a vector (m, Bm, B^2m, B^3m, ...), so each column is
256        // actually composed of that many rows and we focus on a specific row here
257        // implicitely given ci_dft.
258        //
259        // # Input
260        //
261        // col 0: (-(a0s0 + a1s1 + a2s2) + M[i], a0    , a1    , a2    )
262        // col 1: (0, 0, 0, 0)
263        // col 2: (0, 0, 0, 0)
264        // col 3: (0, 0, 0, 0)
265        //
266        // # Output
267        //
268        // col 0: (-(a0s0 + a1s1 + a2s2) + M[i], a0       , a1       , a2       )
269        // col 1: (-(b0s0 + b1s1 + b2s2)       , b0 + M[i], b1       , b2       )
270        // col 2: (-(c0s0 + c1s1 + c2s2)       , c0       , c1 + M[i], c2       )
271        // col 3: (-(d0s0 + d1s1 + d2s2)       , d0       , d1       , d2 + M[i])
272
273        let (mut tmp_dft_i, scratch1) = scratch.tmp_vec_znx_dft(module, cols, tsk.size());
274        {
275            let (mut tmp_dft_col_data, scratch2) = scratch1.tmp_vec_znx_dft(module, 1, self.size());
276
277            // Performs a key-switch for each combination of s[i]*s[j], i.e. for a0, a1, a2
278            //
279            // # Example for col=1
280            //
281            // a0 * (-(f0s0 + f1s1 + f1s2) + s0^2, f0, f1, f2) = (-(a0f0s0 + a0f1s1 + a0f1s2) + a0s0^2, a0f0, a0f1, a0f2)
282            // +
283            // a1 * (-(g0s0 + g1s1 + g1s2) + s0s1, g0, g1, g2) = (-(a1g0s0 + a1g1s1 + a1g1s2) + a1s0s1, a1g0, a1g1, a1g2)
284            // +
285            // a2 * (-(h0s0 + h1s1 + h1s2) + s0s2, h0, h1, h2) = (-(a2h0s0 + a2h1s1 + a2h1s2) + a2s0s2, a2h0, a2h1, a2h2)
286            // =
287            // (-(x0s0 + x1s1 + x2s2) + s0(a0s0 + a1s1 + a2s2), x0, x1, x2)
288            (1..cols).for_each(|col_i| {
289                // Extracts a[i] and multipies with Enc(s[i]s[j])
290                tmp_dft_col_data.extract_column(0, ci_dft, col_i);
291
292                if col_i == 1 {
293                    module.vmp_apply(
294                        &mut tmp_dft_i,
295                        &tmp_dft_col_data,
296                        &tsk.at(col_i - 1, col_j - 1).0.data, // Selects Enc(s[i]s[j])
297                        scratch2,
298                    );
299                } else {
300                    module.vmp_apply_add(
301                        &mut tmp_dft_i,
302                        &tmp_dft_col_data,
303                        &tsk.at(col_i - 1, col_j - 1).0.data, // Selects Enc(s[i]s[j])
304                        scratch2,
305                    );
306                }
307            });
308        }
309
310        // Adds -(sum a[i] * s[i]) + m)  on the i-th column of tmp_idft_i
311        //
312        // (-(x0s0 + x1s1 + x2s2) + a0s0s0 + a1s0s1 + a2s0s2, x0, x1, x2)
313        // +
314        // (0, -(a0s0 + a1s1 + a2s2) + M[i], 0, 0)
315        // =
316        // (-(x0s0 + x1s1 + x2s2) + s0(a0s0 + a1s1 + a2s2), x0 -(a0s0 + a1s1 + a2s2) + M[i], x1, x2)
317        // =
318        // (-(x0s0 + x1s1 + x2s2), x0 + M[i], x1, x2)
319        module.vec_znx_dft_add_inplace(&mut tmp_dft_i, col_j, ci_dft, 0);
320        let (mut tmp_idft, scratch2) = scratch1.tmp_vec_znx_big(module, 1, tsk.size());
321        (0..cols).for_each(|i| {
322            module.vec_znx_idft_tmp_a(&mut tmp_idft, 0, &mut tmp_dft_i, i);
323            module.vec_znx_big_normalize(self.basek(), res, i, &tmp_idft, 0, scratch2);
324        });
325    }
326
327    pub fn keyswitch<DataLhs: AsRef<[u8]>, DataKsk: AsRef<[u8]>, DataTsk: AsRef<[u8]>>(
328        &mut self,
329        module: &Module<FFT64>,
330        lhs: &GGSWCiphertext<DataLhs, FFT64>,
331        ksk: &GLWESwitchingKey<DataKsk, FFT64>,
332        tsk: &TensorKey<DataTsk, FFT64>,
333        scratch: &mut Scratch,
334    ) {
335        let rank: usize = self.rank();
336        let cols: usize = rank + 1;
337        let basek: usize = self.basek();
338
339        let (mut tmp_res, scratch1) = scratch.tmp_glwe_ct(module, basek, self.k(), rank);
340        let (mut ci_dft, scratch2) = scratch1.tmp_vec_znx_dft(module, cols, lhs.size());
341
342        // Keyswitch the j-th row of the col 0
343        (0..lhs.rows()).for_each(|row_i| {
344            // Key-switch column 0, i.e.
345            // col 0: (-(a0s0 + a1s1 + a2s2) + M[i], a0, a1, a2) -> (-(a0s0' + a1s1' + a2s2') + M[i], a0, a1, a2)
346            lhs.keyswitch_internal_col0(module, row_i, &mut tmp_res, ksk, scratch2);
347
348            // Isolates DFT(a[i])
349            (0..cols).for_each(|col_i| {
350                module.vec_znx_dft(&mut ci_dft, col_i, &tmp_res.data, col_i);
351            });
352
353            module.vmp_prepare_row(&mut self.data, row_i, 0, &ci_dft);
354
355            // Generates
356            //
357            // col 1: (-(b0s0' + b1s1' + b2s2')    , b0 + M[i], b1       , b2       )
358            // col 2: (-(c0s0' + c1s1' + c2s2')    , c0       , c1 + M[i], c2       )
359            // col 3: (-(d0s0' + d1s1' + d2s2')    , d0       , d1       , d2 + M[i])
360            (1..cols).for_each(|col_j| {
361                self.expand_row(module, col_j, &mut tmp_res.data, &ci_dft, tsk, scratch2);
362                let (mut tmp_res_dft, _) = scratch2.tmp_glwe_fourier(module, basek, self.k(), rank);
363                tmp_res.dft(module, &mut tmp_res_dft);
364                self.set_row(module, row_i, col_j, &tmp_res_dft);
365            });
366        })
367    }
368
369    pub fn keyswitch_inplace<DataKsk: AsRef<[u8]>, DataTsk: AsRef<[u8]>>(
370        &mut self,
371        module: &Module<FFT64>,
372        ksk: &GLWESwitchingKey<DataKsk, FFT64>,
373        tsk: &TensorKey<DataTsk, FFT64>,
374        scratch: &mut Scratch,
375    ) {
376        unsafe {
377            let self_ptr: *mut GGSWCiphertext<DataSelf, FFT64> = self as *mut GGSWCiphertext<DataSelf, FFT64>;
378            self.keyswitch(module, &*self_ptr, ksk, tsk, scratch);
379        }
380    }
381
382    pub fn automorphism<DataLhs: AsRef<[u8]>, DataAk: AsRef<[u8]>, DataTsk: AsRef<[u8]>>(
383        &mut self,
384        module: &Module<FFT64>,
385        lhs: &GGSWCiphertext<DataLhs, FFT64>,
386        auto_key: &AutomorphismKey<DataAk, FFT64>,
387        tensor_key: &TensorKey<DataTsk, FFT64>,
388        scratch: &mut Scratch,
389    ) {
390        #[cfg(debug_assertions)]
391        {
392            assert_eq!(
393                self.rank(),
394                lhs.rank(),
395                "ggsw_out rank: {} != ggsw_in rank: {}",
396                self.rank(),
397                lhs.rank()
398            );
399            assert_eq!(
400                self.rank(),
401                auto_key.rank(),
402                "ggsw_in rank: {} != auto_key rank: {}",
403                self.rank(),
404                auto_key.rank()
405            );
406            assert_eq!(
407                self.rank(),
408                tensor_key.rank(),
409                "ggsw_in rank: {} != tensor_key rank: {}",
410                self.rank(),
411                tensor_key.rank()
412            );
413            assert!(
414                scratch.available()
415                    >= GGSWCiphertext::automorphism_scratch_space(
416                        module,
417                        self.size(),
418                        lhs.size(),
419                        auto_key.size(),
420                        tensor_key.size(),
421                        self.rank()
422                    )
423            )
424        };
425
426        let rank: usize = self.rank();
427        let cols: usize = rank + 1;
428        let basek: usize = self.basek();
429
430        let (mut tmp_res, scratch1) = scratch.tmp_glwe_ct(module, basek, self.k(), rank);
431        let (mut ci_dft, scratch2) = scratch1.tmp_vec_znx_dft(module, cols, lhs.size());
432
433        // Keyswitch the j-th row of the col 0
434        (0..lhs.rows()).for_each(|row_i| {
435            // Key-switch column 0, i.e.
436            // col 0: (-(a0s0 + a1s1 + a2s2) + M[i], a0, a1, a2) -> (-(a0pi^-1(s0) + a1pi^-1(s1) + a2pi^-1(s2)) + M[i], a0, a1, a2)
437            lhs.keyswitch_internal_col0(module, row_i, &mut tmp_res, &auto_key.key, scratch2);
438
439            // Isolates DFT(AUTO(a[i]))
440            (0..cols).for_each(|col_i| {
441                // (-(a0pi^-1(s0) + a1pi^-1(s1) + a2pi^-1(s2)) + M[i], a0, a1, a2) -> (-(a0s0 + a1s1 + a2s2) + pi(M[i]), a0, a1, a2)
442                module.vec_znx_automorphism_inplace(auto_key.p(), &mut tmp_res.data, col_i);
443                module.vec_znx_dft(&mut ci_dft, col_i, &tmp_res.data, col_i);
444            });
445
446            module.vmp_prepare_row(&mut self.data, row_i, 0, &ci_dft);
447
448            // Generates
449            //
450            // col 1: (-(b0s0 + b1s1 + b2s2)    , b0 + pi(M[i]), b1           , b2           )
451            // col 2: (-(c0s0 + c1s1 + c2s2)    , c0           , c1 + pi(M[i]), c2           )
452            // col 3: (-(d0s0 + d1s1 + d2s2)    , d0           , d1           , d2 + pi(M[i]))
453            (1..cols).for_each(|col_j| {
454                self.expand_row(
455                    module,
456                    col_j,
457                    &mut tmp_res.data,
458                    &ci_dft,
459                    tensor_key,
460                    scratch2,
461                );
462                let (mut tmp_res_dft, _) = scratch2.tmp_glwe_fourier(module, basek, self.k(), rank);
463                tmp_res.dft(module, &mut tmp_res_dft);
464                self.set_row(module, row_i, col_j, &tmp_res_dft);
465            });
466        })
467    }
468
469    pub fn automorphism_inplace<DataKsk: AsRef<[u8]>, DataTsk: AsRef<[u8]>>(
470        &mut self,
471        module: &Module<FFT64>,
472        auto_key: &AutomorphismKey<DataKsk, FFT64>,
473        tensor_key: &TensorKey<DataTsk, FFT64>,
474        scratch: &mut Scratch,
475    ) {
476        unsafe {
477            let self_ptr: *mut GGSWCiphertext<DataSelf, FFT64> = self as *mut GGSWCiphertext<DataSelf, FFT64>;
478            self.automorphism(module, &*self_ptr, auto_key, tensor_key, scratch);
479        }
480    }
481
482    pub fn external_product<DataLhs: AsRef<[u8]>, DataRhs: AsRef<[u8]>>(
483        &mut self,
484        module: &Module<FFT64>,
485        lhs: &GGSWCiphertext<DataLhs, FFT64>,
486        rhs: &GGSWCiphertext<DataRhs, FFT64>,
487        scratch: &mut Scratch,
488    ) {
489        #[cfg(debug_assertions)]
490        {
491            assert_eq!(
492                self.rank(),
493                lhs.rank(),
494                "ggsw_out rank: {} != ggsw_in rank: {}",
495                self.rank(),
496                lhs.rank()
497            );
498            assert_eq!(
499                self.rank(),
500                rhs.rank(),
501                "ggsw_in rank: {} != ggsw_apply rank: {}",
502                self.rank(),
503                rhs.rank()
504            );
505        }
506
507        let (mut tmp_ct_in, scratch1) = scratch.tmp_glwe_fourier(module, lhs.basek(), lhs.k(), lhs.rank());
508        let (mut tmp_ct_out, scratch2) = scratch1.tmp_glwe_fourier(module, self.basek(), self.k(), self.rank());
509
510        (0..self.rank() + 1).for_each(|col_i| {
511            (0..self.rows()).for_each(|row_j| {
512                lhs.get_row(module, row_j, col_i, &mut tmp_ct_in);
513                tmp_ct_out.external_product(module, &tmp_ct_in, rhs, scratch2);
514                self.set_row(module, row_j, col_i, &tmp_ct_out);
515            });
516        });
517
518        tmp_ct_out.data.zero();
519
520        (self.rows().min(lhs.rows())..self.rows()).for_each(|row_i| {
521            (0..self.rank() + 1).for_each(|col_j| {
522                self.set_row(module, row_i, col_j, &tmp_ct_out);
523            });
524        });
525    }
526
527    pub fn external_product_inplace<DataRhs: AsRef<[u8]>>(
528        &mut self,
529        module: &Module<FFT64>,
530        rhs: &GGSWCiphertext<DataRhs, FFT64>,
531        scratch: &mut Scratch,
532    ) {
533        #[cfg(debug_assertions)]
534        {
535            assert_eq!(
536                self.rank(),
537                rhs.rank(),
538                "ggsw_out rank: {} != ggsw_apply: {}",
539                self.rank(),
540                rhs.rank()
541            );
542        }
543
544        let (mut tmp_ct, scratch1) = scratch.tmp_glwe_fourier(module, self.basek(), self.k(), self.rank());
545
546        (0..self.rank() + 1).for_each(|col_i| {
547            (0..self.rows()).for_each(|row_j| {
548                self.get_row(module, row_j, col_i, &mut tmp_ct);
549                tmp_ct.external_product_inplace(module, rhs, scratch1);
550                self.set_row(module, row_j, col_i, &tmp_ct);
551            });
552        });
553    }
554}
555
556impl<DataSelf: AsRef<[u8]>> GGSWCiphertext<DataSelf, FFT64> {
557    pub(crate) fn keyswitch_internal_col0<DataRes: AsMut<[u8]> + AsRef<[u8]>, DataKsk: AsRef<[u8]>>(
558        &self,
559        module: &Module<FFT64>,
560        row_i: usize,
561        res: &mut GLWECiphertext<DataRes>,
562        ksk: &GLWESwitchingKey<DataKsk, FFT64>,
563        scratch: &mut Scratch,
564    ) {
565        #[cfg(debug_assertions)]
566        {
567            assert_eq!(self.rank(), ksk.rank());
568            assert_eq!(res.rank(), ksk.rank());
569            assert!(
570                scratch.available()
571                    >= GGSWCiphertext::keyswitch_internal_col0_scratch_space(
572                        module,
573                        res.size(),
574                        self.size(),
575                        ksk.size(),
576                        ksk.rank()
577                    )
578            )
579        }
580        let (mut tmp_dft_dft, scratch1) = scratch.tmp_glwe_fourier(module, self.basek(), self.k(), self.rank());
581        self.get_row(module, row_i, 0, &mut tmp_dft_dft);
582        res.keyswitch_from_fourier(module, &tmp_dft_dft, ksk, scratch1);
583    }
584}
585
586impl<DataSelf: AsRef<[u8]>> GetRow<FFT64> for GGSWCiphertext<DataSelf, FFT64> {
587    fn get_row<R: AsMut<[u8]> + AsRef<[u8]>>(
588        &self,
589        module: &Module<FFT64>,
590        row_i: usize,
591        col_j: usize,
592        res: &mut GLWECiphertextFourier<R, FFT64>,
593    ) {
594        module.vmp_extract_row(&mut res.data, &self.data, row_i, col_j);
595    }
596}
597
598impl<DataSelf: AsMut<[u8]> + AsRef<[u8]>> SetRow<FFT64> for GGSWCiphertext<DataSelf, FFT64> {
599    fn set_row<R: AsRef<[u8]>>(
600        &mut self,
601        module: &Module<FFT64>,
602        row_i: usize,
603        col_j: usize,
604        a: &GLWECiphertextFourier<R, FFT64>,
605    ) {
606        module.vmp_prepare_row(&mut self.data, row_i, col_j, &a.data);
607    }
608}