arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/core/src/layouts/compressed/lwe_to_glwe_ksk.rs
core/src/layouts/compressed/lwe_to_glwe_ksk.rsRaw
  1use backend::hal::{
  2    api::{FillUniform, Reset},
  3    api::{
  4        SvpApplyInplace, SvpPPolAlloc, SvpPPolAllocBytes, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxBigNormalize,
  5        VecZnxCopy, VecZnxDftAllocBytes, VecZnxDftFromVecZnx, VecZnxDftToVecZnxBigConsume, VecZnxFillUniform, VecZnxNormalize,
  6        VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubABInplace,
  7    },
  8    layouts::{Backend, Data, DataMut, DataRef, MatZnx, Module, ReaderFrom, WriterTo},
  9};
 10
 11use crate::layouts::{
 12    Infos, LWEToGLWESwitchingKey,
 13    compressed::{Decompress, GGLWESwitchingKeyCompressed},
 14};
 15use std::fmt;
 16
 17#[derive(PartialEq, Eq, Clone)]
 18pub struct LWEToGLWESwitchingKeyCompressed<D: Data>(pub(crate) GGLWESwitchingKeyCompressed<D>);
 19
 20impl<D: DataRef> fmt::Debug for LWEToGLWESwitchingKeyCompressed<D> {
 21    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
 22        write!(f, "{}", self)
 23    }
 24}
 25
 26impl<D: DataMut> FillUniform for LWEToGLWESwitchingKeyCompressed<D> {
 27    fn fill_uniform(&mut self, source: &mut sampling::source::Source) {
 28        self.0.fill_uniform(source);
 29    }
 30}
 31
 32impl<D: DataMut> Reset for LWEToGLWESwitchingKeyCompressed<D> {
 33    fn reset(&mut self) {
 34        self.0.reset();
 35    }
 36}
 37
 38impl<D: DataRef> fmt::Display for LWEToGLWESwitchingKeyCompressed<D> {
 39    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
 40        write!(f, "(LWEToGLWESwitchingKeyCompressed) {}", self.0)
 41    }
 42}
 43
 44impl<D: Data> Infos for LWEToGLWESwitchingKeyCompressed<D> {
 45    type Inner = MatZnx<D>;
 46
 47    fn inner(&self) -> &Self::Inner {
 48        self.0.inner()
 49    }
 50
 51    fn basek(&self) -> usize {
 52        self.0.basek()
 53    }
 54
 55    fn k(&self) -> usize {
 56        self.0.k()
 57    }
 58}
 59
 60impl<D: Data> LWEToGLWESwitchingKeyCompressed<D> {
 61    pub fn digits(&self) -> usize {
 62        self.0.digits()
 63    }
 64
 65    pub fn rank(&self) -> usize {
 66        self.0.rank()
 67    }
 68
 69    pub fn rank_in(&self) -> usize {
 70        self.0.rank_in()
 71    }
 72
 73    pub fn rank_out(&self) -> usize {
 74        self.0.rank_out()
 75    }
 76}
 77
 78impl<D: DataMut> ReaderFrom for LWEToGLWESwitchingKeyCompressed<D> {
 79    fn read_from<R: std::io::Read>(&mut self, reader: &mut R) -> std::io::Result<()> {
 80        self.0.read_from(reader)
 81    }
 82}
 83
 84impl<D: DataRef> WriterTo for LWEToGLWESwitchingKeyCompressed<D> {
 85    fn write_to<W: std::io::Write>(&self, writer: &mut W) -> std::io::Result<()> {
 86        self.0.write_to(writer)
 87    }
 88}
 89
 90impl LWEToGLWESwitchingKeyCompressed<Vec<u8>> {
 91    pub fn alloc(n: usize, basek: usize, k: usize, rows: usize, rank_out: usize) -> Self {
 92        Self(GGLWESwitchingKeyCompressed::alloc(
 93            n, basek, k, rows, 1, 1, rank_out,
 94        ))
 95    }
 96
 97    pub fn encrypt_sk_scratch_space<B: Backend>(module: &Module<B>, n: usize, basek: usize, k: usize, rank_out: usize) -> usize
 98    where
 99        Module<B>: VecZnxDftAllocBytes
100            + VecZnxBigNormalize<B>
101            + VecZnxDftFromVecZnx<B>
102            + SvpApplyInplace<B>
103            + VecZnxDftToVecZnxBigConsume<B>
104            + VecZnxNormalizeTmpBytes
105            + VecZnxFillUniform
106            + VecZnxSubABInplace
107            + VecZnxAddInplace
108            + VecZnxNormalizeInplace<B>
109            + VecZnxAddNormal
110            + VecZnxNormalize<B>
111            + VecZnxSub
112            + SvpPrepare<B>
113            + SvpPPolAllocBytes
114            + SvpPPolAlloc<B>,
115    {
116        LWEToGLWESwitchingKey::encrypt_sk_scratch_space(module, n, basek, k, rank_out)
117    }
118}
119
120impl<D: DataMut, DR: DataRef, B: Backend> Decompress<B, LWEToGLWESwitchingKeyCompressed<DR>> for LWEToGLWESwitchingKey<D> {
121    fn decompress(&mut self, module: &Module<B>, other: &LWEToGLWESwitchingKeyCompressed<DR>)
122    where
123        Module<B>: VecZnxCopy + VecZnxFillUniform,
124    {
125        self.0.decompress(module, &other.0);
126    }
127}