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.glwe_switching_key_prepared_alloc(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_alloc_bytes(
 87        &self,
 88        base2k: Base2K,
 89        k: TorusPrecision,
 90        rank_in: Rank,
 91        dnum: Dnum,
 92    ) -> usize {
 93        self.glwe_switching_key_prepared_alloc_bytes(base2k, k, rank_in, Rank(1), dnum, Dsize(1))
 94    }
 95
 96    fn glwe_to_lwe_switching_key_prepared_alloc_bytes_from_infos<A>(&self, infos: &A) -> usize
 97    where
 98        A: GGLWEInfos,
 99    {
100        debug_assert_eq!(
101            infos.rank_out().0,
102            1,
103            "rank_out > 1 is not supported for GLWEToLWESwitchingKeyPrepared"
104        );
105        debug_assert_eq!(
106            infos.dsize().0,
107            1,
108            "dsize > 1 is not supported for GLWEToLWESwitchingKeyPrepared"
109        );
110        self.glwe_to_lwe_switching_key_prepared_alloc_bytes(infos.base2k(), infos.k(), infos.rank_in(), infos.dnum())
111    }
112}
113
114impl<B: Backend> GLWEToLWESwitchingKeyPreparedAlloc<B> for Module<B> where Module<B>: GLWESwitchingKeyPreparedAlloc<B> {}
115
116impl<B: Backend> GLWEToLWESwitchingKeyPrepared<Vec<u8>, B>
117where
118    Module<B>: GLWEToLWESwitchingKeyPreparedAlloc<B>,
119{
120    pub fn alloc_from_infos<A>(module: &Module<B>, infos: &A) -> Self
121    where
122        A: GGLWEInfos,
123    {
124        module.glwe_to_lwe_switching_key_prepared_alloc_from_infos(infos)
125    }
126
127    pub fn alloc(module: &Module<B>, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> Self {
128        module.glwe_to_lwe_switching_key_prepared_alloc(base2k, k, rank_in, dnum)
129    }
130
131    pub fn alloc_bytes_from_infos<A>(module: &Module<B>, infos: &A) -> usize
132    where
133        A: GGLWEInfos,
134    {
135        module.glwe_to_lwe_switching_key_prepared_alloc_bytes_from_infos(infos)
136    }
137
138    pub fn alloc_bytes_with(module: &Module<B>, base2k: Base2K, k: TorusPrecision, rank_in: Rank, dnum: Dnum) -> usize {
139        module.glwe_to_lwe_switching_key_prepared_alloc_bytes(base2k, k, rank_in, dnum)
140    }
141}
142
143pub trait GLWEToLWESwitchingKeyPrepare<B: Backend>
144where
145    Self: GLWESwitchingKeyPrepare<B>,
146{
147    fn glwe_to_lwe_switching_key_prepare_tmp_bytes<A>(&self, infos: &A)
148    where
149        A: GGLWEInfos,
150    {
151        self.glwe_switching_key_prepare_tmp_bytes(infos);
152    }
153
154    fn glwe_to_lwe_switching_key_prepare<R, O>(&self, res: &mut R, other: &O, scratch: &mut Scratch<B>)
155    where
156        R: GLWEToLWESwitchingKeyPreparedToMut<B>,
157        O: GLWEToLWESwitchingKeyToRef,
158    {
159        self.glwe_switching_prepare(&mut res.to_mut().0, &other.to_ref().0, scratch);
160    }
161}
162
163impl<B: Backend> GLWEToLWESwitchingKeyPrepare<B> for Module<B> where Self: GLWESwitchingKeyPrepare<B> {}
164
165impl<B: Backend> GLWEToLWESwitchingKeyPrepared<Vec<u8>, B> {
166    pub fn prepare_tmp_bytes<A>(&self, module: &Module<B>, infos: &A)
167    where
168        A: GGLWEInfos,
169        Module<B>: GLWEToLWESwitchingKeyPrepare<B>,
170    {
171        module.glwe_to_lwe_switching_key_prepare_tmp_bytes(infos);
172    }
173}
174
175impl<D: DataMut, B: Backend> GLWEToLWESwitchingKeyPrepared<D, B> {
176    fn prepare<O>(&mut self, module: &Module<B>, other: &O, scratch: &mut Scratch<B>)
177    where
178        O: GLWEToLWESwitchingKeyToRef,
179        Module<B>: GLWEToLWESwitchingKeyPrepare<B>,
180    {
181        module.glwe_to_lwe_switching_key_prepare(self, other, scratch);
182    }
183}
184
185pub trait GLWEToLWESwitchingKeyPreparedToRef<B: Backend> {
186    fn to_ref(&self) -> GLWEToLWESwitchingKeyPrepared<&[u8], B>;
187}
188
189impl<D: DataRef, B: Backend> GLWEToLWESwitchingKeyPreparedToRef<B> for GLWEToLWESwitchingKeyPrepared<D, B>
190where
191    GLWESwitchingKeyPrepared<D, B>: GLWESwitchingKeyPreparedToRef<B>,
192{
193    fn to_ref(&self) -> GLWEToLWESwitchingKeyPrepared<&[u8], B> {
194        GLWEToLWESwitchingKeyPrepared(self.0.to_ref())
195    }
196}
197
198pub trait GLWEToLWESwitchingKeyPreparedToMut<B: Backend> {
199    fn to_mut(&mut self) -> GLWEToLWESwitchingKeyPrepared<&mut [u8], B>;
200}
201
202impl<D: DataMut, B: Backend> GLWEToLWESwitchingKeyPreparedToMut<B> for GLWEToLWESwitchingKeyPrepared<D, B>
203where
204    GLWESwitchingKeyPrepared<D, B>: GLWESwitchingKeyPreparedToMut<B>,
205{
206    fn to_mut(&mut self) -> GLWEToLWESwitchingKeyPrepared<&mut [u8], B> {
207        GLWEToLWESwitchingKeyPrepared(self.0.to_mut())
208    }
209}