mirror of
https://github.com/arnaucube/poulpy.git
synced 2026-02-10 05:06:44 +01:00
Traits reduction, file + structs renaming
This commit is contained in:
62
poulpy-core/src/encryption/glwe_public_key.rs
Normal file
62
poulpy-core/src/encryption/glwe_public_key.rs
Normal file
@@ -0,0 +1,62 @@
|
||||
use poulpy_hal::{
|
||||
api::{ScratchOwnedAlloc, ScratchOwnedBorrow},
|
||||
layouts::{Backend, DataMut, Module, Scratch, ScratchOwned},
|
||||
source::Source,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
Distribution, GLWEEncryptSk, GetDistribution, GetDistributionMut, ScratchTakeCore,
|
||||
layouts::{
|
||||
GLWE, GLWEInfos, GLWEPublicKey, GLWEToMut,
|
||||
prepared::{GLWESecretPrepared, GLWESecretPreparedToRef},
|
||||
},
|
||||
};
|
||||
|
||||
impl<D: DataMut> GLWEPublicKey<D> {
|
||||
pub fn generate<S, M, BE: Backend>(&mut self, module: &M, sk: &S, source_xa: &mut Source, source_xe: &mut Source)
|
||||
where
|
||||
S: GLWESecretPreparedToRef<BE> + GetDistribution,
|
||||
M: GLWEPublicKeyGenerate<BE>,
|
||||
{
|
||||
module.glwe_public_key_generate(self, sk, source_xa, source_xe);
|
||||
}
|
||||
}
|
||||
|
||||
pub trait GLWEPublicKeyGenerate<BE: Backend> {
|
||||
fn glwe_public_key_generate<R, S>(&self, res: &mut R, sk: &S, source_xa: &mut Source, source_xe: &mut Source)
|
||||
where
|
||||
R: GLWEToMut + GetDistributionMut + GLWEInfos,
|
||||
S: GLWESecretPreparedToRef<BE> + GetDistribution;
|
||||
}
|
||||
|
||||
impl<BE: Backend> GLWEPublicKeyGenerate<BE> for Module<BE>
|
||||
where
|
||||
Self: GLWEEncryptSk<BE>,
|
||||
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
|
||||
Scratch<BE>: ScratchTakeCore<BE>,
|
||||
{
|
||||
fn glwe_public_key_generate<R, S>(&self, res: &mut R, sk: &S, source_xa: &mut Source, source_xe: &mut Source)
|
||||
where
|
||||
R: GLWEToMut + GetDistributionMut + GLWEInfos,
|
||||
S: GLWESecretPreparedToRef<BE> + GetDistribution,
|
||||
{
|
||||
{
|
||||
let sk: &GLWESecretPrepared<&[u8], BE> = &sk.to_ref();
|
||||
|
||||
assert_eq!(res.n(), self.n() as u32);
|
||||
assert_eq!(sk.n(), self.n() as u32);
|
||||
|
||||
if sk.dist == Distribution::NONE {
|
||||
panic!("invalid sk: SecretDistribution::NONE")
|
||||
}
|
||||
|
||||
// Its ok to allocate scratch space here since pk is usually generated only once.
|
||||
let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(self.glwe_encrypt_sk_tmp_bytes(res));
|
||||
|
||||
let mut tmp: GLWE<Vec<u8>> = GLWE::alloc_from_infos(res);
|
||||
|
||||
tmp.encrypt_zero_sk(self, sk, source_xa, source_xe, scratch.borrow());
|
||||
}
|
||||
*res.dist_mut() = *sk.dist();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user