poulpy_core/layouts/prepared/
glwe.rs1use poulpy_hal::{
2 api::{VecZnxDftAlloc, VecZnxDftApply, VecZnxDftBytesOf},
3 layouts::{Backend, Data, Module, VecZnxDft, VecZnxDftToBackendMut, VecZnxDftToBackendRef},
4};
5
6use crate::layouts::{Base2K, Degree, GLWEInfos, GLWEToBackendRef, GetDegree, LWEInfos, Rank, TorusPrecision};
7
8#[derive(PartialEq, Eq)]
14pub struct GLWEPrepared<D: Data, B: Backend> {
15 pub(crate) data: VecZnxDft<D, B>,
16 pub(crate) base2k: Base2K,
17}
18
19pub type GLWEPreparedBackendRef<'a, B> = GLWEPrepared<<B as Backend>::BufRef<'a>, B>;
20pub type GLWEPreparedBackendMut<'a, B> = GLWEPrepared<<B as Backend>::BufMut<'a>, B>;
21
22impl<D: Data, B: Backend> LWEInfos for GLWEPrepared<D, B> {
23 fn base2k(&self) -> Base2K {
24 self.base2k
25 }
26
27 fn size(&self) -> usize {
28 self.data.size()
29 }
30
31 fn n(&self) -> Degree {
32 Degree(self.data.n() as u32)
33 }
34}
35
36impl<D: Data, B: Backend> GLWEInfos for GLWEPrepared<D, B> {
37 fn rank(&self) -> Rank {
38 Rank(self.data.cols() as u32 - 1)
39 }
40}
41
42pub trait GLWEPreparedFactory<B: Backend>
44where
45 Self: GetDegree + VecZnxDftAlloc<B> + VecZnxDftBytesOf + VecZnxDftApply<B>,
46{
47 fn glwe_prepared_alloc(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> GLWEPrepared<B::OwnedBuf, B> {
49 GLWEPrepared {
50 data: self.vec_znx_dft_alloc((rank + 1).into(), k.0.div_ceil(base2k.0) as usize),
51 base2k,
52 }
53 }
54
55 fn glwe_prepared_alloc_from_infos<A>(&self, infos: &A) -> GLWEPrepared<B::OwnedBuf, B>
56 where
57 A: GLWEInfos,
58 {
59 self.glwe_prepared_alloc(infos.base2k(), infos.max_k(), infos.rank())
60 }
61
62 fn glwe_prepared_bytes_of(&self, base2k: Base2K, k: TorusPrecision, rank: Rank) -> usize {
63 self.bytes_of_vec_znx_dft((rank + 1).into(), k.0.div_ceil(base2k.0) as usize)
64 }
65
66 fn glwe_prepared_bytes_of_from_infos<A>(&self, infos: &A) -> usize
67 where
68 A: GLWEInfos,
69 {
70 self.glwe_prepared_bytes_of(infos.base2k(), infos.max_k(), infos.rank())
71 }
72
73 fn glwe_prepare<R, O>(&self, res: &mut R, other: &O)
74 where
75 R: GLWEPreparedToBackendMut<B>,
76 O: GLWEToBackendRef<B> + GLWEInfos,
77 {
78 let mut res = res.to_backend_mut();
79 let other = other.to_backend_ref();
80
81 assert_eq!(res.n(), self.ring_degree());
82 assert_eq!(other.n(), self.ring_degree());
83 assert_eq!(res.size(), other.size());
84 assert_eq!(res.max_k(), other.max_k());
85 assert_eq!(res.base2k(), other.base2k());
86
87 for i in 0..(res.rank() + 1).into() {
88 self.vec_znx_dft_apply(1, 0, &mut res.data, i, &other.data, i);
89 }
90 }
91}
92
93impl<B: Backend> GLWEPreparedFactory<B> for Module<B> where Self: VecZnxDftAlloc<B> + VecZnxDftBytesOf + VecZnxDftApply<B> {}
94
95pub trait GLWEPreparedToBackendRef<B: Backend> {
100 fn to_backend_ref(&self) -> GLWEPreparedBackendRef<'_, B>;
101}
102
103impl<B: Backend> GLWEPreparedToBackendRef<B> for GLWEPrepared<B::OwnedBuf, B> {
104 fn to_backend_ref(&self) -> GLWEPreparedBackendRef<'_, B> {
105 GLWEPrepared {
106 data: self.data.to_backend_ref(),
107 base2k: self.base2k,
108 }
109 }
110}
111
112pub trait GLWEPreparedToBackendMut<B: Backend> {
113 fn to_backend_mut(&mut self) -> GLWEPreparedBackendMut<'_, B>;
114}
115
116impl<B: Backend> GLWEPreparedToBackendMut<B> for GLWEPrepared<B::OwnedBuf, B> {
117 fn to_backend_mut(&mut self) -> GLWEPreparedBackendMut<'_, B> {
118 GLWEPrepared {
119 data: self.data.to_backend_mut(),
120 base2k: self.base2k,
121 }
122 }
123}