arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/poulpy-core/src/external_product/ggsw.rs
poulpy-core/src/external_product/ggsw.rsRaw
  1use poulpy_hal::{
  2    api::ScratchAvailable,
  3    layouts::{Backend, DataMut, Module, Scratch, ZnxZero},
  4};
  5
  6use crate::{
  7    GLWEExternalProduct, ScratchTakeCore,
  8    layouts::{
  9        GGSW, GGSWInfos, GGSWToMut, GGSWToRef, GLWEInfos, LWEInfos,
 10        prepared::{GGSWPrepared, GGSWPreparedToRef},
 11    },
 12};
 13
 14pub trait GGSWExternalProduct<BE: Backend>
 15where
 16    Self: GLWEExternalProduct<BE>,
 17{
 18    fn ggsw_external_product_tmp_bytes<R, A, B>(&self, res_infos: &R, a_infos: &A, b_infos: &B) -> usize
 19    where
 20        R: GGSWInfos,
 21        A: GGSWInfos,
 22        B: GGSWInfos,
 23    {
 24        self.glwe_external_product_tmp_bytes(res_infos, a_infos, b_infos)
 25    }
 26
 27    fn ggsw_external_product<R, A, B>(&self, res: &mut R, a: &A, b: &B, scratch: &mut Scratch<BE>)
 28    where
 29        R: GGSWToMut,
 30        A: GGSWToRef,
 31        B: GGSWPreparedToRef<BE>,
 32        Scratch<BE>: ScratchTakeCore<BE>,
 33    {
 34        let res: &mut GGSW<&mut [u8]> = &mut res.to_mut();
 35        let a: &GGSW<&[u8]> = &a.to_ref();
 36        let b: &GGSWPrepared<&[u8], BE> = &b.to_ref();
 37
 38        assert_eq!(
 39            res.rank(),
 40            a.rank(),
 41            "res rank: {} != a rank: {}",
 42            res.rank(),
 43            a.rank()
 44        );
 45        assert_eq!(
 46            res.rank(),
 47            b.rank(),
 48            "res rank: {} != b rank: {}",
 49            res.rank(),
 50            b.rank()
 51        );
 52
 53        assert!(scratch.available() >= self.ggsw_external_product_tmp_bytes(res, a, b));
 54
 55        let min_dnum: usize = res.dnum().min(a.dnum()).into();
 56
 57        for row in 0..min_dnum {
 58            for col in 0..(res.rank() + 1).into() {
 59                self.glwe_external_product(&mut res.at_mut(row, col), &a.at(row, col), b, scratch);
 60            }
 61        }
 62
 63        for row in min_dnum..res.dnum().into() {
 64            for col in 0..(res.rank() + 1).into() {
 65                res.at_mut(row, col).data.zero();
 66            }
 67        }
 68    }
 69
 70    fn ggsw_external_product_inplace<R, A>(&self, res: &mut R, a: &A, scratch: &mut Scratch<BE>)
 71    where
 72        R: GGSWToMut,
 73        A: GGSWPreparedToRef<BE>,
 74        Scratch<BE>: ScratchTakeCore<BE>,
 75    {
 76        let res: &mut GGSW<&mut [u8]> = &mut res.to_mut();
 77        let a: &GGSWPrepared<&[u8], BE> = &a.to_ref();
 78
 79        assert_eq!(res.n(), self.n() as u32);
 80        assert_eq!(a.n(), self.n() as u32);
 81        assert_eq!(
 82            res.rank(),
 83            a.rank(),
 84            "res rank: {} != a rank: {}",
 85            res.rank(),
 86            a.rank()
 87        );
 88
 89        for row in 0..res.dnum().into() {
 90            for col in 0..(res.rank() + 1).into() {
 91                self.glwe_external_product_inplace(&mut res.at_mut(row, col), a, scratch);
 92            }
 93        }
 94    }
 95}
 96
 97impl<BE: Backend> GGSWExternalProduct<BE> for Module<BE> where Self: GLWEExternalProduct<BE> {}
 98
 99impl GGSW<Vec<u8>> {
100    pub fn external_product_tmp_bytes<R, A, B, M, BE: Backend>(module: &M, res_infos: &R, a_infos: &A, b_infos: &B) -> usize
101    where
102        R: GGSWInfos,
103        A: GGSWInfos,
104        B: GGSWInfos,
105        M: GGSWExternalProduct<BE>,
106    {
107        module.ggsw_external_product_tmp_bytes(res_infos, a_infos, b_infos)
108    }
109}
110
111impl<DataSelf: DataMut> GGSW<DataSelf> {
112    pub fn external_product<A, B, M, BE: Backend>(&mut self, module: &M, a: &A, b: &B, scratch: &mut Scratch<BE>)
113    where
114        M: GGSWExternalProduct<BE>,
115        A: GGSWToRef,
116        B: GGSWPreparedToRef<BE>,
117        Scratch<BE>: ScratchTakeCore<BE>,
118    {
119        module.ggsw_external_product(self, a, b, scratch);
120    }
121
122    pub fn external_product_inplace<A, M, BE: Backend>(&mut self, module: &M, a: &A, scratch: &mut Scratch<BE>)
123    where
124        M: GGSWExternalProduct<BE>,
125        A: GGSWPreparedToRef<BE>,
126        Scratch<BE>: ScratchTakeCore<BE>,
127    {
128        module.ggsw_external_product_inplace(self, a, scratch);
129    }
130}