Skip to main content

poulpy_ckks/layouts/
alloc.rs

1use poulpy_core::layouts::{
2    Base2K, Degree, GLWEInfos, GLWEPlaintextLayout, GetDegree, LWEInfos, ModuleCoreAlloc, Rank, TorusPrecision,
3};
4use poulpy_hal::layouts::{Backend, Module};
5
6use crate::{CKKSInfos, CKKSMeta};
7
8use super::{CKKSCiphertext, CKKSPlaintext};
9
10pub trait CKKSModuleAlloc<BE: Backend>: ModuleCoreAlloc<OwnedBuf = BE::OwnedBuf> {
11    fn ckks_ciphertext_alloc_from_infos<A>(&self, infos: &A) -> CKKSCiphertext<BE::OwnedBuf>
12    where
13        A: GLWEInfos;
14
15    fn ckks_ciphertext_alloc(&self, base2k: Base2K, k: TorusPrecision) -> CKKSCiphertext<BE::OwnedBuf>;
16
17    fn ckks_pt_vec_alloc_from_infos<A>(&self, infos: &A) -> CKKSPlaintext<BE::OwnedBuf>
18    where
19        A: GLWEInfos + CKKSInfos;
20
21    fn ckks_plaintext_alloc_from_infos<A>(&self, infos: &A) -> CKKSPlaintext<BE::OwnedBuf>
22    where
23        A: LWEInfos + CKKSInfos;
24
25    fn ckks_plaintext_alloc(&self, n: Degree, base2k: Base2K, meta: CKKSMeta) -> CKKSPlaintext<BE::OwnedBuf>;
26
27    fn ckks_pt_coeffs_alloc(&self, coeff_count: usize, base2k: Base2K, meta: CKKSMeta) -> CKKSPlaintext<BE::OwnedBuf> {
28        self.ckks_plaintext_alloc(coeff_count.into(), base2k, meta)
29    }
30
31    fn ckks_pt_vec_alloc(&self, base2k: Base2K, meta: CKKSMeta) -> CKKSPlaintext<BE::OwnedBuf>;
32}
33
34impl<BE: Backend> CKKSModuleAlloc<BE> for Module<BE>
35where
36    Module<BE>: ModuleCoreAlloc<OwnedBuf = BE::OwnedBuf>,
37{
38    fn ckks_ciphertext_alloc_from_infos<A>(&self, infos: &A) -> CKKSCiphertext<BE::OwnedBuf>
39    where
40        A: GLWEInfos,
41    {
42        CKKSCiphertext::from_inner(self.glwe_alloc_from_infos(infos), CKKSMeta::default())
43    }
44
45    fn ckks_ciphertext_alloc(&self, base2k: Base2K, k: TorusPrecision) -> CKKSCiphertext<BE::OwnedBuf> {
46        CKKSCiphertext::from_inner(self.glwe_alloc(base2k, k, Rank(1)), CKKSMeta::default())
47    }
48
49    fn ckks_pt_vec_alloc_from_infos<A>(&self, infos: &A) -> CKKSPlaintext<BE::OwnedBuf>
50    where
51        A: GLWEInfos + CKKSInfos,
52    {
53        self.ckks_plaintext_alloc_from_infos(infos)
54    }
55
56    fn ckks_plaintext_alloc_from_infos<A>(&self, infos: &A) -> CKKSPlaintext<BE::OwnedBuf>
57    where
58        A: LWEInfos + CKKSInfos,
59    {
60        self.ckks_plaintext_alloc(infos.n(), infos.base2k(), infos.meta())
61    }
62
63    fn ckks_plaintext_alloc(&self, n: Degree, base2k: Base2K, meta: CKKSMeta) -> CKKSPlaintext<BE::OwnedBuf> {
64        CKKSPlaintext::from_inner(
65            self.glwe_plaintext_alloc_from_infos(&GLWEPlaintextLayout {
66                n,
67                base2k,
68                k: meta.min_k(base2k),
69            }),
70            meta,
71        )
72    }
73
74    fn ckks_pt_vec_alloc(&self, base2k: Base2K, meta: CKKSMeta) -> CKKSPlaintext<BE::OwnedBuf> {
75        self.ckks_plaintext_alloc(self.ring_degree(), base2k, meta)
76    }
77}