arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rs
poulpy-core/src/layouts/prepared/glwe_to_lwe_ksk.rsRaw
  1use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch};
  2
  3use crate::layouts::{
  4    Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, GLWEToLWESwitchingKeyToRef, LWEInfos, Rank, TorusPrecision,
  5    prepared::{
  6        GLWESwitchingKeyPrepare, GLWESwitchingKeyPrepared, GLWESwitchingKeyPreparedAlloc, GLWESwitchingKeyPreparedToMut,
  7        GLWESwitchingKeyPreparedToRef,
  8    },
  9};
 10
 11#[derive(PartialEq, Eq)]
 12pub struct GLWEToLWESwitchingKeyPrepared<D: Data, B: Backend>(pub(crate) GLWESwitchingKeyPrepared<D, B>);
 13
 14impl<D: Data, B: Backend> LWEInfos for GLWEToLWESwitchingKeyPrepared<D, B> {
 15    fn base2k(&self) -> Base2K {
 16        self.0.base2k()
 17    }
 18
 19    fn k(&self) -> TorusPrecision {
 20        self.0.k()
 21    }
 22
 23    fn n(&self) -> Degree {
 24        self.0.n()
 25    }
 26
 27    fn size(&self) -> usize {
 28        self.0.size()
 29    }
 30}
 31
 32impl<D: Data, B: Backend> GLWEInfos for GLWEToLWESwitchingKeyPrepared<D, B> {
 33    fn rank(&self) -> Rank {
 34        self.rank_out()
 35    }
 36}
 37
 38impl<D: Data, B: Backend> GGLWEInfos for GLWEToLWESwitchingKeyPrepared<D, B> {
 39    fn rank_in(&self) -> Rank {
 40        self.0.rank_in()
 41    }
 42
 43    fn dsize(&self) -> Dsize {
 44        self.0.dsize()
 45    }
 46
 47    fn rank_out(&self) -> Rank {
 48        self.0.rank_out()
 49    }
 50
 51    fn dnum(&self) -> Dnum {
 52        self.0.dnum()
 53    }
 54}
 55
 56pub trait GLWEToLWESwitchingKeyPreparedAlloc<B: Backend>
 57where
 58    Self: GLWESwitchingKeyPreparedAlloc<B>,
 59{
 60    fn glwe_to_lwe_switching_key_prepared_alloc(
 61        &self,
 62        base2k: Base2K,
 63        k: TorusPrecision,
 64        rank_in: Rank,
 65        dnum: Dnum,
 66    ) -> GLWEToLWESwitchingKeyPrepared<Vec<u8>, B> {
 67        GLWEToLWESwitchingKeyPrepared(self.alloc_glwe_switching_key_prepared(base2k, k, rank_in, Rank(1), dnum, Dsize(1)))
 68    }
 69    fn glwe_to_lwe_switching_key_prepared_alloc_from_infos<A>(&self, infos: &A) -> GLWEToLWESwitchingKeyPrepared<Vec<u8>, B>
 70    where
 71        A: GGLWEInfos,
 72    {
 73        debug_assert_eq!(
 74            infos.rank_out().0,
 75            1,
 76            "rank_out > 1 is not supported for GLWEToLWESwitchingKeyPrepared"
 77        );
 78        debug_assert_eq!(
 79            infos.dsize().0,
 80            1,
 81            "dsize > 1 is not supported for GLWEToLWESwitchingKeyPrepared"
 82        );
 83        self.glwe_to_lwe_switching_key_prepared_alloc(infos.base2k(), infos.k(), infos.rank_in(), infos.dnum())
 84    }
 85
 86    fn glwe_to_lwe_switching_key_prepared_bytes_of(&self, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize {
 87        self.bytes_of_glwe_switching_key_prepared(base2k, k, rank_in, Rank(1), dnum, Dsize(1))
 88    }
 89
 90    fn glwe_to_lwe_switching_key_prepared_bytes_of_from_infos<A>(&self, infos: &A) -> usize
 91    where
 92        A: GGLWEInfos,
 93    {
 94        debug_assert_eq!(
 95            infos.rank_out().0,
 96            1,
 97            "rank_out > 1 is not supported for GLWEToLWESwitchingKeyPrepared"
 98        );
 99        debug_assert_eq!(
100            infos.dsize().0,
101            1,
102            "dsize > 1 is not supported for GLWEToLWESwitchingKeyPrepared"
103        );
104        self.glwe_to_lwe_switching_key_prepared_bytes_of(infos.base2k(), infos.k(), infos.rank_in(), infos.dnum())
105    }
106}
107
108impl<B: Backend> GLWEToLWESwitchingKeyPreparedAlloc<B> for Module<B> where Module<B>: GLWESwitchingKeyPreparedAlloc<B> {}
109
110impl<B: Backend> GLWEToLWESwitchingKeyPrepared<Vec<u8>, B>
111where
112    Module<B>: GLWEToLWESwitchingKeyPreparedAlloc<B>,
113{
114    pub fn alloc_from_infos<A>(module: &Module<B>, infos: &A) -> Self
115    where
116        A: GGLWEInfos,
117    {
118        module.glwe_to_lwe_switching_key_prepared_alloc_from_infos(infos)
119    }
120
121    pub fn alloc(module: &Module<B>, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self {
122        module.glwe_to_lwe_switching_key_prepared_alloc(base2k, k, rank_in, dnum)
123    }
124
125    pub fn bytes_of_from_infos<A>(module: &Module<B>, infos: &A) -> usize
126    where
127        A: GGLWEInfos,
128    {
129        module.glwe_to_lwe_switching_key_prepared_bytes_of_from_infos(infos)
130    }
131
132    pub fn bytes_of(module: &Module<B>, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize {
133        module.glwe_to_lwe_switching_key_prepared_bytes_of(base2k, k, rank_in, dnum)
134    }
135}
136
137pub trait GLWEToLWESwitchingKeyPrepare<B: Backend>
138where
139    Self: GLWESwitchingKeyPrepare<B>,
140{
141    fn glwe_to_lwe_switching_key_prepare_tmp_bytes<A>(&self, infos: &A)
142    where
143        A: GGLWEInfos,
144    {
145        self.prepare_glwe_switching_key_tmp_bytes(infos);
146    }
147
148    fn glwe_to_lwe_switching_key_prepare<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
149    where
150        R: GLWEToLWESwitchingKeyPreparedToMut<B>,
151        O: GLWEToLWESwitchingKeyToRef,
152    {
153        self.prepare_glwe_switching(&mut res.to_mut().0, &other.to_ref().0, scratch);
154    }
155}
156
157impl<B: Backend> GLWEToLWESwitchingKeyPrepare<B> for Module<B> where Self: GLWESwitchingKeyPrepare<B> {}
158
159impl<B: Backend> GLWEToLWESwitchingKeyPrepared<Vec<u8>, B> {
160    pub fn prepare_tmp_bytes<A>(&self, module: &Module<B>, infos: &A)
161    where
162        A: GGLWEInfos,
163        Module<B>: GLWEToLWESwitchingKeyPrepare<B>,
164    {
165        module.glwe_to_lwe_switching_key_prepare_tmp_bytes(infos);
166    }
167}
168
169impl<D: DataMut, B: Backend> GLWEToLWESwitchingKeyPrepared<D, B> {
170    fn prepare<O>(&mut self, module: &Module<B>, other: &O, scratch: &mut Scratch<B>)
171    where
172        O: GLWEToLWESwitchingKeyToRef,
173        Module<B>: GLWEToLWESwitchingKeyPrepare<B>,
174    {
175        module.glwe_to_lwe_switching_key_prepare(self, other, scratch);
176    }
177}
178
179pub trait GLWEToLWESwitchingKeyPreparedToRef<B: Backend> {
180    fn to_ref(&self) -> GLWEToLWESwitchingKeyPrepared<&[u8], B>;
181}
182
183impl<D: DataRef, B: Backend> GLWEToLWESwitchingKeyPreparedToRef<B> for GLWEToLWESwitchingKeyPrepared<D, B>
184where
185    GLWESwitchingKeyPrepared<D, B>: GLWESwitchingKeyPreparedToRef<B>,
186{
187    fn to_ref(&self) -> GLWEToLWESwitchingKeyPrepared<&[u8], B> {
188        GLWEToLWESwitchingKeyPrepared(self.0.to_ref())
189    }
190}
191
192pub trait GLWEToLWESwitchingKeyPreparedToMut<B: Backend> {
193    fn to_mut(&mut self) -> GLWEToLWESwitchingKeyPrepared<&mut [u8], B>;
194}
195
196impl<D: DataMut, B: Backend> GLWEToLWESwitchingKeyPreparedToMut<B> for GLWEToLWESwitchingKeyPrepared<D, B>
197where
198    GLWESwitchingKeyPrepared<D, B>: GLWESwitchingKeyPreparedToMut<B>,
199{
200    fn to_mut(&mut self) -> GLWEToLWESwitchingKeyPrepared<&mut [u8], B> {
201        GLWEToLWESwitchingKeyPrepared(self.0.to_mut())
202    }
203}