arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/poulpy-core/src/layouts/glwe_pt.rs
poulpy-core/src/layouts/glwe_pt.rsRaw
  1use std::fmt;
  2
  3use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, VecZnx, VecZnxToMut, VecZnxToRef, ZnxInfos};
  4
  5use crate::layouts::{
  6    Base2K, Degree, GLWE, GLWEInfos, GLWEToMut, GLWEToRef, GetDegree, LWEInfos, Rank, SetGLWEInfos, TorusPrecision,
  7};
  8
  9#[derive(PartialEq, Eq, Copy, Clone, Debug)]
 10pub struct GLWEPlaintextLayout {
 11    pub n: Degree,
 12    pub base2k: Base2K,
 13    pub k: TorusPrecision,
 14}
 15
 16impl LWEInfos for GLWEPlaintextLayout {
 17    fn base2k(&self) -> Base2K {
 18        self.base2k
 19    }
 20
 21    fn k(&self) -> TorusPrecision {
 22        self.k
 23    }
 24
 25    fn n(&self) -> Degree {
 26        self.n
 27    }
 28}
 29
 30impl GLWEInfos for GLWEPlaintextLayout {
 31    fn rank(&self) -> Rank {
 32        Rank(0)
 33    }
 34}
 35
 36pub struct GLWEPlaintext<D: Data> {
 37    pub data: VecZnx<D>,
 38    pub base2k: Base2K,
 39    pub k: TorusPrecision,
 40}
 41
 42impl<D: DataMut> SetGLWEInfos for GLWEPlaintext<D> {
 43    fn set_base2k(&mut self, base2k: Base2K) {
 44        self.base2k = base2k
 45    }
 46
 47    fn set_k(&mut self, k: TorusPrecision) {
 48        self.k = k
 49    }
 50}
 51
 52impl<D: Data> LWEInfos for GLWEPlaintext<D> {
 53    fn base2k(&self) -> Base2K {
 54        self.base2k
 55    }
 56
 57    fn k(&self) -> TorusPrecision {
 58        self.k
 59    }
 60
 61    fn size(&self) -> usize {
 62        self.data.size()
 63    }
 64
 65    fn n(&self) -> Degree {
 66        Degree(self.data.n() as u32)
 67    }
 68}
 69
 70impl<D: Data> GLWEInfos for GLWEPlaintext<D> {
 71    fn rank(&self) -> Rank {
 72        Rank(self.data.cols() as u32 - 1)
 73    }
 74}
 75
 76impl<D: DataRef> fmt::Display for GLWEPlaintext<D> {
 77    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 78        write!(
 79            f,
 80            "GLWEPlaintext: base2k={} k={}: {}",
 81            self.base2k().0,
 82            self.k().0,
 83            self.data
 84        )
 85    }
 86}
 87
 88pub trait GLWEPlaintextAlloc
 89where
 90    Self: GetDegree,
 91{
 92    fn alloc_glwe_plaintext(&self, base2k: Base2K, k: TorusPrecision) -> GLWEPlaintext<Vec<u8>> {
 93        GLWEPlaintext {
 94            data: VecZnx::alloc(self.n().into(), 1, k.0.div_ceil(base2k.0) as usize),
 95            base2k,
 96            k,
 97        }
 98    }
 99
100    fn alloc_glwe_plaintext_from_infos<A>(&self, infos: &A) -> GLWEPlaintext<Vec<u8>>
101    where
102        A: GLWEInfos,
103    {
104        self.alloc_glwe_plaintext(infos.base2k(), infos.k())
105    }
106
107    fn bytes_of_glwe_plaintext(&self, base2k: Base2K, k: TorusPrecision) -> usize {
108        VecZnx::bytes_of(self.n().into(), 1, k.0.div_ceil(base2k.0) as usize)
109    }
110
111    fn bytes_of_glwe_plaintext_from_infos<A>(&self, infos: &A) -> usize
112    where
113        A: GLWEInfos,
114    {
115        self.bytes_of_glwe_plaintext(infos.base2k(), infos.k())
116    }
117}
118
119impl GLWEPlaintext<Vec<u8>> {
120    pub fn alloc_from_infos<A, B: Backend>(module: Module<B>, infos: &A) -> Self
121    where
122        A: GLWEInfos,
123        Module<B>: GLWEPlaintextAlloc,
124    {
125        module.alloc_glwe_plaintext_from_infos(infos)
126    }
127
128    pub fn alloc<B: Backend>(module: Module<B>, base2k: Base2K, k: TorusPrecision) -> Self
129    where
130        Module<B>: GLWEPlaintextAlloc,
131    {
132        module.alloc_glwe_plaintext(base2k, k)
133    }
134
135    pub fn bytes_of_from_infos<A, B: Backend>(module: Module<B>, infos: &A) -> usize
136    where
137        A: GLWEInfos,
138        Module<B>: GLWEPlaintextAlloc,
139    {
140        module.bytes_of_glwe_plaintext_from_infos(infos)
141    }
142
143    pub fn bytes_of<B: Backend>(module: Module<B>, base2k: Base2K, k: TorusPrecision) -> usize
144    where
145        Module<B>: GLWEPlaintextAlloc,
146    {
147        module.bytes_of_glwe_plaintext(base2k, k)
148    }
149}
150
151impl<D: DataRef> GLWEToRef for GLWEPlaintext<D> {
152    fn to_ref(&self) -> GLWE<&[u8]> {
153        GLWE {
154            k: self.k,
155            base2k: self.base2k,
156            data: self.data.to_ref(),
157        }
158    }
159}
160
161impl<D: DataMut> GLWEToMut for GLWEPlaintext<D> {
162    fn to_mut(&mut self) -> GLWE<&mut [u8]> {
163        GLWE {
164            k: self.k,
165            base2k: self.base2k,
166            data: self.data.to_mut(),
167        }
168    }
169}
170
171pub trait GLWEPlaintextToRef {
172    fn to_ref(&self) -> GLWEPlaintext<&[u8]>;
173}
174
175impl<D: DataRef> GLWEPlaintextToRef for GLWEPlaintext<D> {
176    fn to_ref(&self) -> GLWEPlaintext<&[u8]> {
177        GLWEPlaintext {
178            data: self.data.to_ref(),
179            base2k: self.base2k,
180            k: self.k,
181        }
182    }
183}
184
185pub trait GLWEPlaintextToMut {
186    fn to_ref(&mut self) -> GLWEPlaintext<&mut [u8]>;
187}
188
189impl<D: DataMut> GLWEPlaintextToMut for GLWEPlaintext<D> {
190    fn to_ref(&mut self) -> GLWEPlaintext<&mut [u8]> {
191        GLWEPlaintext {
192            base2k: self.base2k,
193            k: self.k,
194            data: self.data.to_mut(),
195        }
196    }
197}