arnaucube
arnaucube/poulpyPublic Self-hosted repository
poulpy/poulpy-core/src/layouts/prepared/gglwe_atk.rs
poulpy-core/src/layouts/prepared/gglwe_atk.rsRaw
  1use poulpy_backend::hal::{
  2    api::{VmpPMatAlloc, VmpPMatAllocBytes, VmpPrepare},
  3    layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, VmpPMat},
  4};
  5
  6use crate::layouts::{
  7    GGLWEAutomorphismKey, Infos,
  8    prepared::{GGLWESwitchingKeyPrepared, Prepare, PrepareAlloc},
  9};
 10
 11#[derive(PartialEq, Eq)]
 12pub struct GGLWEAutomorphismKeyPrepared<D: Data, B: Backend> {
 13    pub(crate) key: GGLWESwitchingKeyPrepared<D, B>,
 14    pub(crate) p: i64,
 15}
 16
 17impl<B: Backend> GGLWEAutomorphismKeyPrepared<Vec<u8>, B> {
 18    pub fn alloc(module: &Module<B>, n: usize, basek: usize, k: usize, rows: usize, digits: usize, rank: usize) -> Self
 19    where
 20        Module<B>: VmpPMatAlloc<B>,
 21    {
 22        GGLWEAutomorphismKeyPrepared::<Vec<u8>, B> {
 23            key: GGLWESwitchingKeyPrepared::alloc(module, n, basek, k, rows, digits, rank, rank),
 24            p: 0,
 25        }
 26    }
 27
 28    pub fn bytes_of(module: &Module<B>, n: usize, basek: usize, k: usize, rows: usize, digits: usize, rank: usize) -> usize
 29    where
 30        Module<B>: VmpPMatAllocBytes,
 31    {
 32        GGLWESwitchingKeyPrepared::bytes_of(module, n, basek, k, rows, digits, rank, rank)
 33    }
 34}
 35
 36impl<D: Data, B: Backend> Infos for GGLWEAutomorphismKeyPrepared<D, B> {
 37    type Inner = VmpPMat<D, B>;
 38
 39    fn inner(&self) -> &Self::Inner {
 40        self.key.inner()
 41    }
 42
 43    fn basek(&self) -> usize {
 44        self.key.basek()
 45    }
 46
 47    fn k(&self) -> usize {
 48        self.key.k()
 49    }
 50}
 51
 52impl<D: Data, B: Backend> GGLWEAutomorphismKeyPrepared<D, B> {
 53    pub fn p(&self) -> i64 {
 54        self.p
 55    }
 56
 57    pub fn digits(&self) -> usize {
 58        self.key.digits()
 59    }
 60
 61    pub fn rank(&self) -> usize {
 62        self.key.rank()
 63    }
 64
 65    pub fn rank_in(&self) -> usize {
 66        self.key.rank_in()
 67    }
 68
 69    pub fn rank_out(&self) -> usize {
 70        self.key.rank_out()
 71    }
 72}
 73
 74impl<D: DataMut, DR: DataRef, B: Backend> Prepare<B, GGLWEAutomorphismKey<DR>> for GGLWEAutomorphismKeyPrepared<D, B>
 75where
 76    Module<B>: VmpPrepare<B>,
 77{
 78    fn prepare(&mut self, module: &Module<B>, other: &GGLWEAutomorphismKey<DR>, scratch: &mut Scratch<B>) {
 79        self.key.prepare(module, &other.key, scratch);
 80        self.p = other.p;
 81    }
 82}
 83
 84impl<D: DataRef, B: Backend> PrepareAlloc<B, GGLWEAutomorphismKeyPrepared<Vec<u8>, B>> for GGLWEAutomorphismKey<D>
 85where
 86    Module<B>: VmpPMatAlloc<B> + VmpPrepare<B>,
 87{
 88    fn prepare_alloc(&self, module: &Module<B>, scratch: &mut Scratch<B>) -> GGLWEAutomorphismKeyPrepared<Vec<u8>, B> {
 89        let mut atk_prepared: GGLWEAutomorphismKeyPrepared<Vec<u8>, B> = GGLWEAutomorphismKeyPrepared::alloc(
 90            module,
 91            self.n(),
 92            self.basek(),
 93            self.k(),
 94            self.rows(),
 95            self.digits(),
 96            self.rank(),
 97        );
 98        atk_prepared.prepare(module, self, scratch);
 99        atk_prepared
100    }
101}