arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/poulpy-core/src/layouts/prepared/ggsw.rs
poulpy-core/src/layouts/prepared/ggsw.rsRaw
  1use poulpy_hal::{
  2    api::{VmpPMatAlloc, VmpPMatBytesOf, VmpPrepare, VmpPrepareTmpBytes, VmpZero},
  3    layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, VmpPMat, VmpPMatToMut, VmpPMatToRef, ZnxInfos},
  4};
  5
  6use crate::layouts::{
  7    Base2K, Degree, Dnum, Dsize, GGSW, GGSWInfos, GGSWToRef, GLWEInfos, GetDegree, LWEInfos, Rank, TorusPrecision,
  8};
  9
 10#[derive(PartialEq, Eq)]
 11pub struct GGSWPrepared<D: Data, B: Backend> {
 12    pub(crate) data: VmpPMat<D, B>,
 13    pub(crate) k: TorusPrecision,
 14    pub(crate) base2k: Base2K,
 15    pub(crate) dsize: Dsize,
 16}
 17
 18impl<D: Data, B: Backend> LWEInfos for GGSWPrepared<D, B> {
 19    fn n(&self) -> Degree {
 20        Degree(self.data.n() as u32)
 21    }
 22
 23    fn base2k(&self) -> Base2K {
 24        self.base2k
 25    }
 26
 27    fn k(&self) -> TorusPrecision {
 28        self.k
 29    }
 30
 31    fn size(&self) -> usize {
 32        self.data.size()
 33    }
 34}
 35
 36impl<D: Data, B: Backend> GLWEInfos for GGSWPrepared<D, B> {
 37    fn rank(&self) -> Rank {
 38        Rank(self.data.cols_out() as u32 - 1)
 39    }
 40}
 41
 42impl<D: Data, B: Backend> GGSWInfos for GGSWPrepared<D, B> {
 43    fn dsize(&self) -> Dsize {
 44        self.dsize
 45    }
 46
 47    fn dnum(&self) -> Dnum {
 48        Dnum(self.data.rows() as u32)
 49    }
 50}
 51
 52pub trait GGSWPreparedFactory<B: Backend>
 53where
 54    Self: GetDegree + VmpPMatAlloc<B> + VmpPMatBytesOf + VmpPrepareTmpBytes + VmpPrepare<B> + VmpZero<B>,
 55{
 56    fn alloc_ggsw_prepared(
 57        &self,
 58        base2k: Base2K,
 59        k: TorusPrecision,
 60        dnum: Dnum,
 61        dsize: Dsize,
 62        rank: Rank,
 63    ) -> GGSWPrepared<Vec<u8>, B> {
 64        let size: usize = k.0.div_ceil(base2k.0) as usize;
 65        debug_assert!(
 66            size as u32 > dsize.0,
 67            "invalid ggsw: ceil(k/base2k): {size} <= dsize: {}",
 68            dsize.0
 69        );
 70
 71        assert!(
 72            dnum.0 * dsize.0 <= size as u32,
 73            "invalid ggsw: dnum: {} * dsize:{} > ceil(k/base2k): {size}",
 74            dnum.0,
 75            dsize.0,
 76        );
 77
 78        GGSWPrepared {
 79            data: self.vmp_pmat_alloc(
 80                dnum.into(),
 81                (rank + 1).into(),
 82                (rank + 1).into(),
 83                k.0.div_ceil(base2k.0) as usize,
 84            ),
 85            k,
 86            base2k,
 87            dsize,
 88        }
 89    }
 90
 91    fn alloc_ggsw_prepared_from_infos<A>(&self, infos: &A) -> GGSWPrepared<Vec<u8>, B>
 92    where
 93        A: GGSWInfos,
 94    {
 95        assert_eq!(self.ring_degree(), infos.n());
 96        self.alloc_ggsw_prepared(infos.base2k(), infos.k(), infos.dnum(), infos.dsize(), infos.rank())
 97    }
 98
 99    fn bytes_of_ggsw_prepared(&self, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> usize {
100        let size: usize = k.0.div_ceil(base2k.0) as usize;
101        debug_assert!(
102            size as u32 > dsize.0,
103            "invalid ggsw: ceil(k/base2k): {size} <= dsize: {}",
104            dsize.0
105        );
106
107        assert!(
108            dnum.0 * dsize.0 <= size as u32,
109            "invalid ggsw: dnum: {} * dsize:{} > ceil(k/base2k): {size}",
110            dnum.0,
111            dsize.0,
112        );
113
114        self.bytes_of_vmp_pmat(dnum.into(), (rank + 1).into(), (rank + 1).into(), size)
115    }
116
117    fn bytes_of_ggsw_prepared_from_infos<A>(&self, infos: &A) -> usize
118    where
119        A: GGSWInfos,
120    {
121        assert_eq!(self.ring_degree(), infos.n());
122        self.bytes_of_ggsw_prepared(infos.base2k(), infos.k(), infos.dnum(), infos.dsize(), infos.rank())
123    }
124
125    fn ggsw_prepare_tmp_bytes<A>(&self, infos: &A) -> usize
126    where
127        A: GGSWInfos,
128    {
129        assert_eq!(self.ring_degree(), infos.n());
130        self.vmp_prepare_tmp_bytes(
131            infos.dnum().into(),
132            (infos.rank() + 1).into(),
133            (infos.rank() + 1).into(),
134            infos.size(),
135        )
136    }
137    fn ggsw_prepare<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
138    where
139        R: GGSWPreparedToMut<B>,
140        O: GGSWToRef,
141    {
142        let mut res: GGSWPrepared<&mut [u8], B> = res.to_mut();
143        let other: GGSW<&[u8]> = other.to_ref();
144        assert_eq!(res.n(), self.ring_degree());
145        assert_eq!(other.n(), self.ring_degree());
146        assert_eq!(res.k, other.k);
147        assert_eq!(res.base2k, other.base2k);
148        assert_eq!(res.dsize, other.dsize);
149        self.vmp_prepare(&mut res.data, &other.data, scratch);
150    }
151}
152
153impl<B: Backend> GGSWPreparedFactory<B> for Module<B> where
154    Self: GetDegree + VmpPMatAlloc<B> + VmpPMatBytesOf + VmpPrepareTmpBytes + VmpPrepare<B> + VmpZero<B>
155{
156}
157
158impl<B: Backend> GGSWPrepared<Vec<u8>, B> {
159    pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self
160    where
161        A: GGSWInfos,
162        M: GGSWPreparedFactory<B>,
163    {
164        module.alloc_ggsw_prepared_from_infos(infos)
165    }
166
167    pub fn alloc<M>(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> Self
168    where
169        M: GGSWPreparedFactory<B>,
170    {
171        module.alloc_ggsw_prepared(base2k, k, dnum, dsize, rank)
172    }
173
174    pub fn bytes_of_from_infos<A, M>(module: &M, infos: &A) -> usize
175    where
176        A: GGSWInfos,
177        M: GGSWPreparedFactory<B>,
178    {
179        module.bytes_of_ggsw_prepared_from_infos(infos)
180    }
181
182    pub fn bytes_of<M>(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> usize
183    where
184        M: GGSWPreparedFactory<B>,
185    {
186        module.bytes_of_ggsw_prepared(base2k, k, dnum, dsize, rank)
187    }
188}
189
190impl<D: DataRef, B: Backend> GGSWPrepared<D, B> {
191    pub fn data(&self) -> &VmpPMat<D, B> {
192        &self.data
193    }
194}
195
196impl<B: Backend> GGSWPrepared<Vec<u8>, B> {
197    pub fn prepare_tmp_bytes<A, M>(&self, module: &M, infos: &A) -> usize
198    where
199        A: GGSWInfos,
200        M: GGSWPreparedFactory<B>,
201    {
202        module.ggsw_prepare_tmp_bytes(infos)
203    }
204}
205
206impl<D: DataMut, B: Backend> GGSWPrepared<D, B> {
207    pub fn prepare<O, M>(&mut self, module: &M, other: &O, scratch: &mut Scratch<B>)
208    where
209        O: GGSWToRef,
210        M: GGSWPreparedFactory<B>,
211    {
212        module.ggsw_prepare(self, other, scratch);
213    }
214
215    pub fn zero<M>(&mut self, module: &M)
216    where
217        M: GGSWPreparedFactory<B>,
218    {
219        module.vmp_zero(&mut self.data);
220    }
221}
222
223pub trait GGSWPreparedToMut<B: Backend> {
224    fn to_mut(&mut self) -> GGSWPrepared<&mut [u8], B>;
225}
226
227impl<D: DataMut, B: Backend> GGSWPreparedToMut<B> for GGSWPrepared<D, B> {
228    fn to_mut(&mut self) -> GGSWPrepared<&mut [u8], B> {
229        GGSWPrepared {
230            base2k: self.base2k,
231            k: self.k,
232            dsize: self.dsize,
233            data: self.data.to_mut(),
234        }
235    }
236}
237
238pub trait GGSWPreparedToRef<B: Backend> {
239    fn to_ref(&self) -> GGSWPrepared<&[u8], B>;
240}
241
242impl<D: DataRef, B: Backend> GGSWPreparedToRef<B> for GGSWPrepared<D, B> {
243    fn to_ref(&self) -> GGSWPrepared<&[u8], B> {
244        GGSWPrepared {
245            base2k: self.base2k,
246            k: self.k,
247            dsize: self.dsize,
248            data: self.data.to_ref(),
249        }
250    }
251}