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, Dnum, Dsize, GGLWEAutomorphismKey, GGLWEInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision,
8 prepared::{GGLWESwitchingKeyPrepared, Prepare, PrepareAlloc, PrepareScratchSpace},
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> GGLWEInfos 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 dsize(&self) -> Dsize {
57 self.key.dsize()
58 }
59
60 fn dnum(&self) -> Dnum {
61 self.key.dnum()
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: GGLWEInfos,
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, rank: Rank, dnum: Dnum, dsize: Dsize) -> Self
83 where
84 Module<B>: VmpPMatAlloc<B>,
85 {
86 GGLWEAutomorphismKeyPrepared {
87 key: GGLWESwitchingKeyPrepared::alloc_with(module, base2k, k, rank, rank, dnum, dsize),
88 p: 0,
89 }
90 }
91
92 pub fn alloc_bytes<A>(module: &Module<B>, infos: &A) -> usize
93 where
94 A: GGLWEInfos,
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(module: &Module<B>, base2k: Base2K, k: TorusPrecision, rank: Rank, dnum: Dnum, dsize: Dsize) -> usize
106 where
107 Module<B>: VmpPMatAllocBytes,
108 {
109 GGLWESwitchingKeyPrepared::alloc_bytes_with(module, base2k, k, rank, rank, dnum, dsize)
110 }
111}
112
113impl<DR: DataRef, B: Backend, A: GGLWEInfos> PrepareScratchSpace<B, A> for GGLWEAutomorphismKeyPrepared<DR, B>
114where
115 GGLWESwitchingKeyPrepared<DR, B>: PrepareScratchSpace<B, A>,
116{
117 fn prepare_scratch_space(module: &Module<B>, infos: &A) -> usize {
118 GGLWESwitchingKeyPrepared::<DR, B>::prepare_scratch_space(module, infos)
119 }
120}
121
122impl<D: DataMut, DR: DataRef, B: Backend> Prepare<B, GGLWEAutomorphismKey<DR>> for GGLWEAutomorphismKeyPrepared<D, B>
123where
124 Module<B>: VmpPrepare<B>,
125{
126 fn prepare(&mut self, module: &Module<B>, other: &GGLWEAutomorphismKey<DR>, scratch: &mut Scratch<B>) {
127 self.key.prepare(module, &other.key, scratch);
128 self.p = other.p;
129 }
130}
131
132impl<D: DataRef, B: Backend> PrepareAlloc<B, GGLWEAutomorphismKeyPrepared<Vec<u8>, B>> for GGLWEAutomorphismKey<D>
133where
134 Module<B>: VmpPMatAlloc<B> + VmpPrepare<B>,
135{
136 fn prepare_alloc(&self, module: &Module<B>, scratch: &mut Scratch<B>) -> GGLWEAutomorphismKeyPrepared<Vec<u8>, B> {
137 let mut atk_prepared: GGLWEAutomorphismKeyPrepared<Vec<u8>, B> = GGLWEAutomorphismKeyPrepared::alloc(module, self);
138 atk_prepared.prepare(module, self, scratch);
139 atk_prepared
140 }
141}