arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/poulpy-core/src/layouts/compressed/gglwe_atk.rs
poulpy-core/src/layouts/compressed/gglwe_atk.rsRaw
  1use poulpy_hal::{
  2    layouts::{Backend, Data, DataMut, DataRef, FillUniform, Module, ReaderFrom, WriterTo},
  3    source::Source,
  4};
  5
  6use crate::layouts::{
  7    AutomorphismKey, AutomorphismKeyToMut, Base2K, Degree, Dnum, Dsize, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision,
  8    compressed::{
  9        GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut, GLWESwitchingKeyCompressedToRef, GLWESwitchingKeyDecompress,
 10    },
 11    prepared::{GetAutomorphismGaloisElement, SetAutomorphismGaloisElement},
 12};
 13use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
 14use std::fmt;
 15
 16#[derive(PartialEq, Eq, Clone)]
 17pub struct AutomorphismKeyCompressed<D: Data> {
 18    pub(crate) key: GLWESwitchingKeyCompressed<D>,
 19    pub(crate) p: i64,
 20}
 21
 22impl<D: Data> GetAutomorphismGaloisElement for AutomorphismKeyCompressed<D> {
 23    fn p(&self) -> i64 {
 24        self.p
 25    }
 26}
 27
 28impl<D: Data> LWEInfos for AutomorphismKeyCompressed<D> {
 29    fn n(&self) -> Degree {
 30        self.key.n()
 31    }
 32
 33    fn base2k(&self) -> Base2K {
 34        self.key.base2k()
 35    }
 36
 37    fn k(&self) -> TorusPrecision {
 38        self.key.k()
 39    }
 40
 41    fn size(&self) -> usize {
 42        self.key.size()
 43    }
 44}
 45
 46impl<D: Data> GLWEInfos for AutomorphismKeyCompressed<D> {
 47    fn rank(&self) -> Rank {
 48        self.rank_out()
 49    }
 50}
 51
 52impl<D: Data> GGLWEInfos for AutomorphismKeyCompressed<D> {
 53    fn rank_in(&self) -> Rank {
 54        self.key.rank_in()
 55    }
 56
 57    fn rank_out(&self) -> Rank {
 58        self.key.rank_out()
 59    }
 60
 61    fn dsize(&self) -> Dsize {
 62        self.key.dsize()
 63    }
 64
 65    fn dnum(&self) -> Dnum {
 66        self.key.dnum()
 67    }
 68}
 69
 70impl<D: DataRef> fmt::Debug for AutomorphismKeyCompressed<D> {
 71    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
 72        write!(f, "{self}")
 73    }
 74}
 75
 76impl<D: DataMut> FillUniform for AutomorphismKeyCompressed<D> {
 77    fn fill_uniform(&mut self, log_bound: usize, source: &mut Source) {
 78        self.key.fill_uniform(log_bound, source);
 79    }
 80}
 81
 82impl<D: DataRef> fmt::Display for AutomorphismKeyCompressed<D> {
 83    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 84        write!(f, "(AutomorphismKeyCompressed: p={}) {}", self.p, self.key)
 85    }
 86}
 87
 88impl AutomorphismKeyCompressed<Vec<u8>> {
 89    pub fn alloc_from_infos<A>(infos: &A) -> Self
 90    where
 91        A: GGLWEInfos,
 92    {
 93        Self::alloc(
 94            infos.n(),
 95            infos.base2k(),
 96            infos.k(),
 97            infos.rank(),
 98            infos.dnum(),
 99            infos.dsize(),
100        )
101    }
102
103    pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self {
104        AutomorphismKeyCompressed {
105            key: GLWESwitchingKeyCompressed::alloc(n, base2k, k, rank, rank, dnum, dsize),
106            p: 0,
107        }
108    }
109
110    pub fn bytes_of_from_infos<A>(infos: &A) -> usize
111    where
112        A: GGLWEInfos,
113    {
114        Self::bytes_of(
115            infos.n(),
116            infos.base2k(),
117            infos.k(),
118            infos.rank(),
119            infos.dnum(),
120            infos.dsize(),
121        )
122    }
123
124    pub fn bytes_of(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize {
125        GLWESwitchingKeyCompressed::bytes_of(n, base2k, k, rank, dnum, dsize)
126    }
127}
128
129impl<D: DataMut> ReaderFrom for AutomorphismKeyCompressed<D> {
130    fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> {
131        self.p = reader.read_u64::<LittleEndian>()? as i64;
132        self.key.read_from(reader)
133    }
134}
135
136impl<D: DataRef> WriterTo for AutomorphismKeyCompressed<D> {
137    fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
138        writer.write_u64::<LittleEndian>(self.p as u64)?;
139        self.key.write_to(writer)
140    }
141}
142
143pub trait AutomorphismKeyDecompress
144where
145    Self: GLWESwitchingKeyDecompress,
146{
147    fn decompress_automorphism_key<R, O>(&self, res: &mut R, other: &O)
148    where
149        R: AutomorphismKeyToMut + SetAutomorphismGaloisElement,
150        O: AutomorphismKeyCompressedToRef + GetAutomorphismGaloisElement,
151    {
152        self.decompress_glwe_switching_key(&mut res.to_mut().key, &other.to_ref().key);
153        res.set_p(other.p());
154    }
155}
156
157impl<B: Backend> AutomorphismKeyDecompress for Module<B> where Self: GLWESwitchingKeyDecompress {}
158
159impl<D: DataMut> AutomorphismKey<D>
160where
161    Self: SetAutomorphismGaloisElement,
162{
163    pub fn decompress<O, M>(&mut self, module: &M, other: &O)
164    where
165        O: AutomorphismKeyCompressedToRef + GetAutomorphismGaloisElement,
166        M: AutomorphismKeyDecompress,
167    {
168        module.decompress_automorphism_key(self, other);
169    }
170}
171
172pub trait AutomorphismKeyCompressedToRef {
173    fn to_ref(&self) -> AutomorphismKeyCompressed<&[u8]>;
174}
175
176impl<D: DataRef> AutomorphismKeyCompressedToRef for AutomorphismKeyCompressed<D>
177where
178    GLWESwitchingKeyCompressed<D>: GLWESwitchingKeyCompressedToRef,
179{
180    fn to_ref(&self) -> AutomorphismKeyCompressed<&[u8]> {
181        AutomorphismKeyCompressed {
182            key: self.key.to_ref(),
183            p: self.p,
184        }
185    }
186}
187
188pub trait AutomorphismKeyCompressedToMut {
189    fn to_mut(&mut self) -> AutomorphismKeyCompressed<&mut [u8]>;
190}
191
192impl<D: DataMut> AutomorphismKeyCompressedToMut for AutomorphismKeyCompressed<D>
193where
194    GLWESwitchingKeyCompressed<D>: GLWESwitchingKeyCompressedToMut,
195{
196    fn to_mut(&mut self) -> AutomorphismKeyCompressed<&mut [u8]> {
197        AutomorphismKeyCompressed {
198            p: self.p,
199            key: self.key.to_mut(),
200        }
201    }
202}