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