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_hal::{
  2    api::{VmpPMatAlloc, VmpPMatAllocBytes, VmpPrepare},
  3    layouts::{Backend, Data, DataMut, DataRef, Module, Scratch},
  4};
  5
  6use crate::layouts::{
  7    Base2K, Degree, Digits, GGLWEAutomorphismKey, GGLWELayoutInfos, GLWEInfos, LWEInfos, Rank, Rows, TorusPrecision,
  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<D: Data, B: Backend> GGLWEAutomorphismKeyPrepared<D, B> {
 18    pub fn p(&self) -> i64 {
 19        self.p
 20    }
 21}
 22
 23impl<D: Data, B: Backend> LWEInfos for GGLWEAutomorphismKeyPrepared<D, B> {
 24    fn n(&self) -> Degree {
 25        self.key.n()
 26    }
 27
 28    fn base2k(&self) -> Base2K {
 29        self.key.base2k()
 30    }
 31
 32    fn k(&self) -> TorusPrecision {
 33        self.key.k()
 34    }
 35
 36    fn size(&self) -> usize {
 37        self.key.size()
 38    }
 39}
 40
 41impl<D: Data, B: Backend> GLWEInfos for GGLWEAutomorphismKeyPrepared<D, B> {
 42    fn rank(&self) -> Rank {
 43        self.rank_out()
 44    }
 45}
 46
 47impl<D: Data, B: Backend> GGLWELayoutInfos for GGLWEAutomorphismKeyPrepared<D, B> {
 48    fn rank_in(&self) -> Rank {
 49        self.key.rank_in()
 50    }
 51
 52    fn rank_out(&self) -> Rank {
 53        self.key.rank_out()
 54    }
 55
 56    fn digits(&self) -> Digits {
 57        self.key.digits()
 58    }
 59
 60    fn rows(&self) -> Rows {
 61        self.key.rows()
 62    }
 63}
 64
 65impl<B: Backend> GGLWEAutomorphismKeyPrepared<Vec<u8>, B> {
 66    pub fn alloc<A>(module: &Module<B>, infos: &A) -> Self
 67    where
 68        A: GGLWELayoutInfos,
 69        Module<B>: VmpPMatAlloc<B>,
 70    {
 71        assert_eq!(
 72            infos.rank_in(),
 73            infos.rank_out(),
 74            "rank_in != rank_out is not supported for GGLWEAutomorphismKeyPrepared"
 75        );
 76        GGLWEAutomorphismKeyPrepared::<Vec<u8>, B> {
 77            key: GGLWESwitchingKeyPrepared::alloc(module, infos),
 78            p: 0,
 79        }
 80    }
 81
 82    pub fn alloc_with(module: &Module<B>, base2k: Base2K, k: TorusPrecision, rows: Rows, digits: Digits, rank: Rank) -> Self
 83    where
 84        Module<B>: VmpPMatAlloc<B>,
 85    {
 86        GGLWEAutomorphismKeyPrepared {
 87            key: GGLWESwitchingKeyPrepared::alloc_with(module, base2k, k, rows, digits, rank, rank),
 88            p: 0,
 89        }
 90    }
 91
 92    pub fn alloc_bytes<A>(module: &Module<B>, infos: &A) -> usize
 93    where
 94        A: GGLWELayoutInfos,
 95        Module<B>: VmpPMatAllocBytes,
 96    {
 97        assert_eq!(
 98            infos.rank_in(),
 99            infos.rank_out(),
100            "rank_in != rank_out is not supported for GGLWEAutomorphismKeyPrepared"
101        );
102        GGLWESwitchingKeyPrepared::alloc_bytes(module, infos)
103    }
104
105    pub fn alloc_bytes_with(
106        module: &Module<B>,
107        base2k: Base2K,
108        k: TorusPrecision,
109        rows: Rows,
110        digits: Digits,
111        rank: Rank,
112    ) -> usize
113    where
114        Module<B>: VmpPMatAllocBytes,
115    {
116        GGLWESwitchingKeyPrepared::alloc_bytes_with(module, base2k, k, rows, digits, rank, rank)
117    }
118}
119
120impl<D: DataMut, DR: DataRef, B: Backend> Prepare<B, GGLWEAutomorphismKey<DR>> for GGLWEAutomorphismKeyPrepared<D, B>
121where
122    Module<B>: VmpPrepare<B>,
123{
124    fn prepare(&mut self, module: &Module<B>, other: &GGLWEAutomorphismKey<DR>, scratch: &mut Scratch<B>) {
125        self.key.prepare(module, &other.key, scratch);
126        self.p = other.p;
127    }
128}
129
130impl<D: DataRef, B: Backend> PrepareAlloc<B, GGLWEAutomorphismKeyPrepared<Vec<u8>, B>> for GGLWEAutomorphismKey<D>
131where
132    Module<B>: VmpPMatAlloc<B> + VmpPrepare<B>,
133{
134    fn prepare_alloc(&self, module: &Module<B>, scratch: &mut Scratch<B>) -> GGLWEAutomorphismKeyPrepared<Vec<u8>, B> {
135        let mut atk_prepared: GGLWEAutomorphismKeyPrepared<Vec<u8>, B> = GGLWEAutomorphismKeyPrepared::alloc(module, self);
136        atk_prepared.prepare(module, self, scratch);
137        atk_prepared
138    }
139}