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