arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/poulpy-core/src/layouts/compressed/lwe_to_glwe_ksk.rs
poulpy-core/src/layouts/compressed/lwe_to_glwe_ksk.rsRaw
  1use poulpy_hal::{
  2    api::{VecZnxCopy, VecZnxFillUniform},
  3    layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo},
  4    source::Source,
  5};
  6
  7use crate::layouts::{
  8    Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, LWEToGLWESwitchingKey, Rank, TorusPrecision,
  9    compressed::{Decompress, GLWESwitchingKeyCompressed},
 10};
 11use std::fmt;
 12
 13#[derive(PartialEq, Eq, Clone)]
 14pub struct LWEToGLWESwitchingKeyCompressed<D: Data>(pub(crate) GLWESwitchingKeyCompressed<D>);
 15
 16impl<D: Data> LWEInfos for LWEToGLWESwitchingKeyCompressed<D> {
 17    fn n(&self) -> Degree {
 18        self.0.n()
 19    }
 20
 21    fn base2k(&self) -> Base2K {
 22        self.0.base2k()
 23    }
 24
 25    fn k(&self) -> TorusPrecision {
 26        self.0.k()
 27    }
 28    fn size(&self) -> usize {
 29        self.0.size()
 30    }
 31}
 32impl<D: Data> GLWEInfos for LWEToGLWESwitchingKeyCompressed<D> {
 33    fn rank(&self) -> Rank {
 34        self.rank_out()
 35    }
 36}
 37
 38impl<D: Data> GGLWEInfos for LWEToGLWESwitchingKeyCompressed<D> {
 39    fn dsize(&self) -> Dsize {
 40        self.0.dsize()
 41    }
 42
 43    fn rank_in(&self) -> Rank {
 44        self.0.rank_in()
 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
 56impl<D: DataRef> fmt::Debug for LWEToGLWESwitchingKeyCompressed<D> {
 57    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
 58        write!(f, "{self}")
 59    }
 60}
 61
 62impl<D: DataMut> FillUniform for LWEToGLWESwitchingKeyCompressed<D> {
 63    fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) {
 64        self.0.fill_uniform(log_bound, source);
 65    }
 66}
 67
 68impl<D: DataRef> fmt::Display for LWEToGLWESwitchingKeyCompressed<D> {
 69    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 70        write!(f, "(LWEToGLWESwitchingKeyCompressed) {}", self.0)
 71    }
 72}
 73
 74impl<D: DataMut> ReaderFrom for LWEToGLWESwitchingKeyCompressed<D> {
 75    fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> {
 76        self.0.read_from(reader)
 77    }
 78}
 79
 80impl<D: DataRef> WriterTo for LWEToGLWESwitchingKeyCompressed<D> {
 81    fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
 82        self.0.write_to(writer)
 83    }
 84}
 85
 86impl LWEToGLWESwitchingKeyCompressed<Vec<u8>> {
 87    pub fn alloc<A>(infos: &A) -> Self
 88    where
 89        A: GGLWEInfos,
 90    {
 91        debug_assert_eq!(
 92            infos.dsize().0,
 93            1,
 94            "dsize > 1 is not supported for LWEToGLWESwitchingKeyCompressed"
 95        );
 96        debug_assert_eq!(
 97            infos.rank_in().0,
 98            1,
 99            "rank_in > 1 is not supported for LWEToGLWESwitchingKeyCompressed"
100        );
101        Self(GLWESwitchingKeyCompressed::alloc(infos))
102    }
103
104    pub fn alloc_with(n: Degree, base2k: Base2K, k: TorusPrecision, rank_out: Rank, dnum: Dnum) -> Self {
105        Self(GLWESwitchingKeyCompressed::alloc_with(
106            n,
107            base2k,
108            k,
109            Rank(1),
110            rank_out,
111            dnum,
112            Dsize(1),
113        ))
114    }
115
116    pub fn alloc_bytes<A>(infos: &A) -> usize
117    where
118        A: GGLWEInfos,
119    {
120        debug_assert_eq!(
121            infos.rank_in().0,
122            1,
123            "rank_in > 1 is not supported for LWEToGLWESwitchingKey"
124        );
125        debug_assert_eq!(
126            infos.dsize().0,
127            1,
128            "dsize > 1 is not supported for LWEToGLWESwitchingKey"
129        );
130        GLWESwitchingKeyCompressed::alloc_bytes(infos)
131    }
132
133    pub fn alloc_bytes_with(n: Degree, base2k: Base2K, k: TorusPrecision, dnum: Dnum) -> usize {
134        GLWESwitchingKeyCompressed::alloc_bytes_with(n, base2k, k, Rank(1), dnum, Dsize(1))
135    }
136}
137
138impl<D: DataMut, DR: DataRef, B: Backend> Decompress<B, LWEToGLWESwitchingKeyCompressed<DR>> for LWEToGLWESwitchingKey<D>
139where
140    Module<B>: VecZnxFillUniform + VecZnxCopy,
141{
142    fn decompress(&mut self, module: &Module<B>, other: &LWEToGLWESwitchingKeyCompressed<DR>) {
143        self.0.decompress(module, &other.0);
144    }
145}