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