Skip to main content

poulpy_core/delegates/
decryption.rs

1use poulpy_hal::layouts::{Backend, Data, HostBackend, Module, ScratchArena};
2
3use crate::{
4    api::{GLWEDecrypt, GLWETensorDecrypt, LWEDecrypt},
5    layouts::{
6        GLWEInfos, GLWEPlaintext, GLWESecretPrepared, GLWESecretTensorPrepared, GLWETensor, GLWEToBackendMut, GLWEToBackendRef,
7        LWEInfos, LWEPlaintextToBackendMut, LWESecretToBackendRef, LWEToBackendRef, SetLWEInfos,
8        prepared::{GLWESecretPreparedToBackendRef, GLWESecretTensorPreparedToBackendRef},
9    },
10    oep::DecryptionImpl,
11};
12
13macro_rules! impl_decryption_delegate {
14    ($trait:ty, $($body:item),+ $(,)?) => {
15        impl<BE> $trait for Module<BE>
16        where
17            BE: Backend + HostBackend + DecryptionImpl<BE>,
18        {
19            $($body)+
20        }
21    };
22}
23
24impl_decryption_delegate!(
25    GLWEDecrypt<BE>,
26    fn glwe_decrypt_tmp_bytes<A>(&self, infos: &A) -> usize
27    where
28        A: GLWEInfos,
29    {
30        BE::glwe_decrypt_tmp_bytes(self, infos)
31    },
32    fn glwe_decrypt<R, P, S>(&self, res: &R, pt: &mut P, sk: &S, scratch: &mut ScratchArena<'_, BE>)
33    where
34        R: GLWEToBackendRef<BE> + GLWEInfos,
35        P: GLWEToBackendMut<BE> + GLWEInfos + SetLWEInfos,
36        S: GLWESecretPreparedToBackendRef<BE> + GLWEInfos,
37    {
38        BE::glwe_decrypt(self, res, pt, sk, scratch)
39    }
40);
41
42impl_decryption_delegate!(
43    LWEDecrypt<BE>,
44    fn lwe_decrypt<R, P, S>(&self, res: &R, pt: &mut P, sk: &S, scratch: &mut ScratchArena<'_, BE>)
45    where
46        R: LWEToBackendRef<BE> + LWEInfos,
47        P: LWEPlaintextToBackendMut<BE> + SetLWEInfos + LWEInfos,
48        S: LWESecretToBackendRef<BE> + LWEInfos,
49    {
50        BE::lwe_decrypt(self, res, pt, sk, scratch)
51    },
52    fn lwe_decrypt_tmp_bytes<A>(&self, infos: &A) -> usize
53    where
54        A: LWEInfos,
55    {
56        BE::lwe_decrypt_tmp_bytes(self, infos)
57    }
58);
59
60impl_decryption_delegate!(
61    GLWETensorDecrypt<BE>,
62    fn glwe_tensor_decrypt_tmp_bytes<A>(&self, infos: &A) -> usize
63    where
64        A: GLWEInfos,
65    {
66        BE::glwe_tensor_decrypt_tmp_bytes(self, infos)
67    },
68    fn glwe_tensor_decrypt<R: Data, P: Data, S0: Data, S1: Data>(
69        &self,
70        res: &GLWETensor<R>,
71        pt: &mut GLWEPlaintext<P>,
72        sk: &GLWESecretPrepared<S0, BE>,
73        sk_tensor: &GLWESecretTensorPrepared<S1, BE>,
74        scratch: &mut ScratchArena<'_, BE>,
75    ) where
76        GLWETensor<R>: GLWEToBackendRef<BE> + GLWEInfos,
77        GLWEPlaintext<P>: GLWEToBackendMut<BE> + GLWEInfos + SetLWEInfos,
78        GLWESecretPrepared<S0, BE>: GLWESecretPreparedToBackendRef<BE> + GLWEInfos,
79        GLWESecretTensorPrepared<S1, BE>: GLWESecretTensorPreparedToBackendRef<BE> + GLWEInfos,
80    {
81        BE::glwe_tensor_decrypt(self, res, pt, sk, sk_tensor, scratch)
82    }
83);