poulpy_core/encryption/
glwe_pk.rs

1use poulpy_hal::{
2    api::{
3        ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, VecZnxAddInplace, VecZnxAddNormal, VecZnxBigNormalize,
4        VecZnxDftAllocBytes, VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace,
5        VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace,
6    },
7    layouts::{Backend, DataMut, DataRef, Module, ScratchOwned},
8    oep::{ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeVecZnxDftImpl, TakeVecZnxImpl},
9    source::Source,
10};
11
12use crate::layouts::{GLWECiphertext, GLWEPublicKey, prepared::GLWESecretPrepared};
13
14impl<D: DataMut> GLWEPublicKey<D> {
15    pub fn generate_from_sk<S: DataRef, B>(
16        &mut self,
17        module: &Module<B>,
18        sk: &GLWESecretPrepared<S, B>,
19        source_xa: &mut Source,
20        source_xe: &mut Source,
21    ) where
22        Module<B>:,
23        Module<B>: VecZnxDftAllocBytes
24            + VecZnxBigNormalize<B>
25            + VecZnxDftApply<B>
26            + SvpApplyDftToDftInplace<B>
27            + VecZnxIdftApplyConsume<B>
28            + VecZnxNormalizeTmpBytes
29            + VecZnxFillUniform
30            + VecZnxSubInplace
31            + VecZnxAddInplace
32            + VecZnxNormalizeInplace<B>
33            + VecZnxAddNormal
34            + VecZnxNormalize<B>
35            + VecZnxSub,
36        B: Backend
37            + ScratchOwnedAllocImpl<B>
38            + ScratchOwnedBorrowImpl<B>
39            + TakeVecZnxDftImpl<B>
40            + ScratchAvailableImpl<B>
41            + TakeVecZnxImpl<B>,
42    {
43        #[cfg(debug_assertions)]
44        {
45            use crate::{Distribution, layouts::LWEInfos};
46
47            assert_eq!(self.n(), sk.n());
48
49            if sk.dist == Distribution::NONE {
50                panic!("invalid sk: SecretDistribution::NONE")
51            }
52        }
53
54        // Its ok to allocate scratch space here since pk is usually generated only once.
55        let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(GLWECiphertext::encrypt_sk_scratch_space(module, self));
56
57        let mut tmp: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(self);
58        tmp.encrypt_zero_sk(module, sk, source_xa, source_xe, scratch.borrow());
59        self.dist = sk.dist;
60    }
61}