arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/poulpy-schemes/src/tfhe/bdd_arithmetic/bdd_rotation.rs
poulpy-schemes/src/tfhe/bdd_arithmetic/bdd_rotation.rsRaw
 1use poulpy_core::{
 2    GLWECopy, GLWERotate, ScratchTakeCore,
 3    layouts::{GLWE, GLWEToMut},
 4};
 5use poulpy_hal::layouts::{Backend, Scratch};
 6
 7use crate::tfhe::bdd_arithmetic::{Cmux, GetGGSWBit, UnsignedInteger};
 8
 9pub trait BDDRotation<T: UnsignedInteger, BE: Backend>
10where
11    Self: GLWECopy + GLWERotate<BE> + Cmux<BE>,
12    Scratch<BE>: ScratchTakeCore<BE>,
13{
14    /// Homomorphic multiplication of res by X^{k[bit_start..bit_start + bit_size] * bit_step}.
15    fn bdd_rotate<R, K, D>(
16        &self,
17        res: &mut R,
18        k: K,
19        bit_start: usize,
20        bit_size: usize,
21        bit_step: usize,
22        scratch: &mut Scratch<BE>,
23    ) where
24        R: GLWEToMut,
25        K: GetGGSWBit<T, BE>,
26        Scratch<BE>: ScratchTakeCore<BE>,
27    {
28        let res: &mut GLWE<&mut [u8]> = &mut res.to_mut();
29
30        let (mut tmp_res, scratch_1) = scratch.take_glwe(res);
31
32        self.glwe_copy(&mut tmp_res, res);
33
34        for i in 1..bit_size {
35            // res' = res * X^2^(i * bit_step)
36            self.glwe_rotate(1 << (i + bit_step), &mut tmp_res, res);
37
38            // res = (res - res') * GGSW(b[i]) + res'
39            self.cmux_inplace(res, &tmp_res, &k.get_bit(i + bit_start), scratch_1);
40        }
41    }
42}