poulpy/poulpy-core/src/automorphism/ggsw_ct.rs
poulpy-core/src/automorphism/ggsw_ct.rsRaw
1use poulpy_hal::{
2 api::ScratchAvailable,
3 layouts::{Backend, DataMut, Module, Scratch},
4};
5
6use crate::{
7 GGSWExpandRows, ScratchTakeCore,
8 automorphism::glwe_ct::GLWEAutomorphism,
9 layouts::{
10 GGLWEInfos, GGLWEPreparedToRef, GGLWEToGGSWKeyPrepared, GGLWEToGGSWKeyPreparedToRef, GGSW, GGSWInfos, GGSWToMut,
11 GGSWToRef, GetGaloisElement,
12 },
13};
14
15impl GGSW<Vec<u8>> {
16 pub fn automorphism_tmp_bytes<R, A, K, T, M, BE: Backend>(
17 module: &M,
18 res_infos: &R,
19 a_infos: &A,
20 key_infos: &K,
21 tsk_infos: &T,
22 ) -> usize
23 where
24 R: GGSWInfos,
25 A: GGSWInfos,
26 K: GGLWEInfos,
27 T: GGLWEInfos,
28 M: GGSWAutomorphism<BE>,
29 {
30 module.ggsw_automorphism_tmp_bytes(res_infos, a_infos, key_infos, tsk_infos)
31 }
32}
33
34impl<D: DataMut> GGSW<D> {
35 pub fn automorphism<A, K, T, M, BE: Backend>(&mut self, module: &M, a: &A, key: &K, tsk: &T, scratch: &mut Scratch<BE>)
36 where
37 A: GGSWToRef,
38 K: GetGaloisElement + GGLWEPreparedToRef<BE> + GGLWEInfos,
39 T: GGLWEToGGSWKeyPreparedToRef<BE>,
40 Scratch<BE>: ScratchTakeCore<BE>,
41 M: GGSWAutomorphism<BE>,
42 {
43 module.ggsw_automorphism(self, a, key, tsk, scratch);
44 }
45
46 pub fn automorphism_inplace<K, T, M, BE: Backend>(&mut self, module: &M, key: &K, tsk: &T, scratch: &mut Scratch<BE>)
47 where
48 K: GetGaloisElement + GGLWEPreparedToRef<BE> + GGLWEInfos,
49 T: GGLWEToGGSWKeyPreparedToRef<BE>,
50 Scratch<BE>: ScratchTakeCore<BE>,
51 M: GGSWAutomorphism<BE>,
52 {
53 module.ggsw_automorphism_inplace(self, key, tsk, scratch);
54 }
55}
56
57impl<BE: Backend> GGSWAutomorphism<BE> for Module<BE> where Self: GLWEAutomorphism<BE> + GGSWExpandRows<BE> {}
58
59pub trait GGSWAutomorphism<BE: Backend>
60where
61 Self: GLWEAutomorphism<BE> + GGSWExpandRows<BE>,
62{
63 fn ggsw_automorphism_tmp_bytes<R, A, K, T>(&self, res_infos: &R, a_infos: &A, key_infos: &K, tsk_infos: &T) -> usize
64 where
65 R: GGSWInfos,
66 A: GGSWInfos,
67 K: GGLWEInfos,
68 T: GGLWEInfos,
69 {
70 self.glwe_automorphism_tmp_bytes(res_infos, a_infos, key_infos)
71 .max(self.ggsw_expand_rows_tmp_bytes(res_infos, tsk_infos))
72 }
73
74 fn ggsw_automorphism<R, A, K, T>(&self, res: &mut R, a: &A, key: &K, tsk: &T, scratch: &mut Scratch<BE>)
75 where
76 R: GGSWToMut,
77 A: GGSWToRef,
78 K: GetGaloisElement + GGLWEPreparedToRef<BE> + GGLWEInfos,
79 T: GGLWEToGGSWKeyPreparedToRef<BE>,
80 Scratch<BE>: ScratchTakeCore<BE>,
81 {
82 let res: &mut GGSW<&mut [u8]> = &mut res.to_mut();
83 let a: &GGSW<&[u8]> = &a.to_ref();
84 let tsk: &GGLWEToGGSWKeyPrepared<&[u8], BE> = &tsk.to_ref();
85
86 assert_eq!(res.dsize(), a.dsize());
87 assert!(res.dnum() <= a.dnum());
88 assert!(scratch.available() >= self.ggsw_automorphism_tmp_bytes(res, a, key, tsk));
89
90 // Keyswitch the j-th row of the col 0
91 for row in 0..res.dnum().as_usize() {
92 // Key-switch column 0, i.e.
93 // col 0: (-(a0s0 + a1s1 + a2s2) + M[i], a0, a1, a2) -> (-(a0pi^-1(s0) + a1pi^-1(s1) + a2pi^-1(s2)) + M[i], a0, a1, a2)
94 self.glwe_automorphism(&mut res.at_mut(row, 0), &a.at(row, 0), key, scratch);
95 }
96
97 self.ggsw_expand_row(res, tsk, scratch);
98 }
99
100 fn ggsw_automorphism_inplace<R, K, T>(&self, res: &mut R, key: &K, tsk: &T, scratch: &mut Scratch<BE>)
101 where
102 R: GGSWToMut,
103 K: GetGaloisElement + GGLWEPreparedToRef<BE> + GGLWEInfos,
104 T: GGLWEToGGSWKeyPreparedToRef<BE>,
105 Scratch<BE>: ScratchTakeCore<BE>,
106 {
107 let res: &mut GGSW<&mut [u8]> = &mut res.to_mut();
108 let tsk: &GGLWEToGGSWKeyPrepared<&[u8], BE> = &tsk.to_ref();
109
110 // Keyswitch the j-th row of the col 0
111 for row in 0..res.dnum().as_usize() {
112 // Key-switch column 0, i.e.
113 // col 0: (-(a0s0 + a1s1 + a2s2) + M[i], a0, a1, a2) -> (-(a0pi^-1(s0) + a1pi^-1(s1) + a2pi^-1(s2)) + M[i], a0, a1, a2)
114 self.glwe_automorphism_inplace(&mut res.at_mut(row, 0), key, scratch);
115 }
116
117 self.ggsw_expand_row(res, tsk, scratch);
118 }
119}
120
121impl<DataSelf: DataMut> GGSW<DataSelf> {}