arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/poulpy-hal/src/api/convolution.rs
poulpy-hal/src/api/convolution.rsRaw
  1use crate::{
  2    api::{
  3        ModuleN, ScratchTakeBasic, SvpApplyDftToDft, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, VecZnxDftAddScaledInplace,
  4        VecZnxDftBytesOf, VecZnxDftZero,
  5    },
  6    layouts::{Backend, Module, Scratch, VecZnxDftToMut, VecZnxDftToRef, VecZnxToRef, ZnxInfos},
  7};
  8
  9impl<BE: Backend> BivariateTensoring<BE> for Module<BE>
 10where
 11    Self: BivariateConvolution<BE>,
 12    Scratch<BE>: ScratchTakeBasic,
 13{
 14}
 15
 16pub trait BivariateTensoring<BE: Backend>
 17where
 18    Self: BivariateConvolution<BE>,
 19    Scratch<BE>: ScratchTakeBasic,
 20{
 21    fn bivariate_tensoring<R, A, B>(&self, k: i64, res: &mut R, a: &A, b: &B, scratch: &mut Scratch<BE>)
 22    where
 23        R: VecZnxDftToMut<BE>,
 24        A: VecZnxToRef,
 25        B: VecZnxDftToRef<BE>,
 26    {
 27        let res: &mut crate::layouts::VecZnxDft<&mut [u8], BE> = &mut res.to_mut();
 28        let a: &crate::layouts::VecZnx<&[u8]> = &a.to_ref();
 29        let b: &crate::layouts::VecZnxDft<&[u8], BE> = &b.to_ref();
 30
 31        let res_cols: usize = res.cols();
 32        let a_cols: usize = a.cols();
 33        let b_cols: usize = b.cols();
 34
 35        assert!(res_cols >= a_cols + b_cols - 1);
 36
 37        for res_col in 0..res_cols {
 38            self.vec_znx_dft_zero(res, res_col);
 39        }
 40
 41        for a_col in 0..a_cols {
 42            for b_col in 0..b_cols {
 43                self.bivariate_convolution_add(k, res, a_col + b_col, a, a_col, b, b_col, scratch);
 44            }
 45        }
 46    }
 47}
 48
 49impl<BE: Backend> BivariateConvolution<BE> for Module<BE>
 50where
 51    Self: Sized
 52        + ModuleN
 53        + SvpPPolAlloc<BE>
 54        + SvpApplyDftToDft<BE>
 55        + SvpPrepare<BE>
 56        + SvpPPolBytesOf
 57        + VecZnxDftBytesOf
 58        + VecZnxDftAddScaledInplace<BE>
 59        + VecZnxDftZero<BE>,
 60    Scratch<BE>: ScratchTakeBasic,
 61{
 62}
 63
 64pub trait BivariateConvolution<BE: Backend>
 65where
 66    Self: Sized
 67        + ModuleN
 68        + SvpPPolAlloc<BE>
 69        + SvpApplyDftToDft<BE>
 70        + SvpPrepare<BE>
 71        + SvpPPolBytesOf
 72        + VecZnxDftBytesOf
 73        + VecZnxDftAddScaledInplace<BE>
 74        + VecZnxDftZero<BE>,
 75    Scratch<BE>: ScratchTakeBasic,
 76{
 77    fn convolution_tmp_bytes(&self, b_size: usize) -> usize {
 78        self.bytes_of_svp_ppol(1) + self.bytes_of_vec_znx_dft(1, b_size)
 79    }
 80
 81    /// Evaluates a bivariate convolution over Z[X, Y] / (X^N + 1) where Y = 2^-K over the
 82    /// selected columsn and stores the result on the selected column, scaled by 2^{k * Base2K}
 83    ///
 84    /// # Example
 85    /// a = [a00, a10, a20, a30] = (a00 * 2^-K + a01 * 2^-2K) + (a10 * 2^-K + a11 * 2^-2K) * X ...
 86    ///     [a01, a11, a21, a31]
 87    ///
 88    /// b = [b00, b10, b20, b30] = (b00 * 2^-K + b01 * 2^-2K) + (b10 * 2^-K + b11 * 2^-2K) * X ...
 89    ///     [b01, b11, b21, b31]
 90    ///
 91    /// If k = 0:
 92    /// res = [  0,   0,   0,   0] = (r01 * 2^-2K + r02 * 2^-3K + r03 * 2^-4K + r04 * 2^-5K) + ...
 93    ///       [r01, r11, r21, r31]
 94    ///       [r02, r12, r22, r32]
 95    ///       [r03, r13, r23, r33]
 96    ///       [r04, r14, r24, r34]
 97    ///
 98    /// If k = 1:
 99    /// res = [r01, r11, r21, r31] = (r01 * 2^-K + r02 * 2^-2K + r03 * 2^-3K + r04 * 2^-4K + r05 * 2^-5K) + ...
100    ///       [r02, r12, r22, r32]
101    ///       [r03, r13, r23, r33]
102    ///       [r04, r14, r24, r34]
103    ///       [r05, r15, r25, r35]
104    ///
105    /// If k = -1:
106    /// res = [  0,   0,   0,   0] = (r01 * 2^-3K + r02 * 2^-4K + r03 * 2^-5K) + ...
107    ///       [  0,   0,   0,   0]
108    ///       [r01, r11, r21, r31]
109    ///       [r02, r12, r22, r32]
110    ///       [r03, r13, r23, r33]
111    ///
112    /// If res.size() < a.size() + b.size() + 1 + k, result is truncated accordingly in the Y dimension.
113    fn bivariate_convolution_add<R, A, B>(
114        &self,
115        k: i64,
116        res: &mut R,
117        res_col: usize,
118        a: &A,
119        a_col: usize,
120        b: &B,
121        b_col: usize,
122        scratch: &mut Scratch<BE>,
123    ) where
124        R: VecZnxDftToMut<BE>,
125        A: VecZnxToRef,
126        B: VecZnxDftToRef<BE>,
127    {
128        let res: &mut crate::layouts::VecZnxDft<&mut [u8], BE> = &mut res.to_mut();
129        let a: &crate::layouts::VecZnx<&[u8]> = &a.to_ref();
130        let b: &crate::layouts::VecZnxDft<&[u8], BE> = &b.to_ref();
131
132        let (mut ppol, scratch_1) = scratch.take_svp_ppol(self, 1);
133        let (mut res_tmp, _) = scratch_1.take_vec_znx_dft(self, 1, b.size());
134
135        for a_limb in 0..a.size() {
136            self.svp_prepare(&mut ppol, 0, &a.as_scalar_znx_ref(a_col, a_limb), 0);
137            self.svp_apply_dft_to_dft(&mut res_tmp, 0, &ppol, 0, b, b_col);
138            self.vec_znx_dft_add_scaled_inplace(res, res_col, &res_tmp, 0, -(1 + a_limb as i64) + k);
139        }
140    }
141
142    fn bivariate_convolution<R, A, B>(
143        &self,
144        k: i64,
145        res: &mut R,
146        res_col: usize,
147        a: &A,
148        a_col: usize,
149        b: &B,
150        b_col: usize,
151        scratch: &mut Scratch<BE>,
152    ) where
153        R: VecZnxDftToMut<BE>,
154        A: VecZnxToRef,
155        B: VecZnxDftToRef<BE>,
156    {
157        self.vec_znx_dft_zero(res, res_col);
158        self.bivariate_convolution_add(k, res, res_col, a, a_col, b, b_col, scratch);
159    }
160}