poulpy_core/layouts/prepared/
lwe_ksk.rs

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