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