mirror of
https://github.com/arnaucube/poulpy.git
synced 2026-02-10 13:16:44 +01:00
Code organisation for glwe
This commit is contained in:
75
core/src/glwe/public_key.rs
Normal file
75
core/src/glwe/public_key.rs
Normal file
@@ -0,0 +1,75 @@
|
||||
use backend::{Backend, FFT64, Module, ScratchOwned, VecZnxDft};
|
||||
use sampling::source::Source;
|
||||
|
||||
use crate::{FourierGLWECiphertext, GLWESecret, Infos, keys::SecretDistribution};
|
||||
|
||||
pub struct GLWEPublicKey<D, B: Backend> {
|
||||
pub(crate) data: FourierGLWECiphertext<D, B>,
|
||||
pub(crate) dist: SecretDistribution,
|
||||
}
|
||||
|
||||
impl<B: Backend> GLWEPublicKey<Vec<u8>, B> {
|
||||
pub fn alloc(module: &Module<B>, basek: usize, k: usize, rank: usize) -> Self {
|
||||
Self {
|
||||
data: FourierGLWECiphertext::alloc(module, basek, k, rank),
|
||||
dist: SecretDistribution::NONE,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn bytes_of(module: &Module<B>, basek: usize, k: usize, rank: usize) -> usize {
|
||||
FourierGLWECiphertext::<Vec<u8>, B>::bytes_of(module, basek, k, rank)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, B: Backend> Infos for GLWEPublicKey<T, B> {
|
||||
type Inner = VecZnxDft<T, B>;
|
||||
|
||||
fn inner(&self) -> &Self::Inner {
|
||||
&self.data.data
|
||||
}
|
||||
|
||||
fn basek(&self) -> usize {
|
||||
self.data.basek
|
||||
}
|
||||
|
||||
fn k(&self) -> usize {
|
||||
self.data.k
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, B: Backend> GLWEPublicKey<T, B> {
|
||||
pub fn rank(&self) -> usize {
|
||||
self.cols() - 1
|
||||
}
|
||||
}
|
||||
|
||||
impl<C: AsRef<[u8]> + AsMut<[u8]>> GLWEPublicKey<C, FFT64> {
|
||||
pub fn generate_from_sk<S: AsRef<[u8]>>(
|
||||
&mut self,
|
||||
module: &Module<FFT64>,
|
||||
sk: &GLWESecret<S, FFT64>,
|
||||
source_xa: &mut Source,
|
||||
source_xe: &mut Source,
|
||||
sigma: f64,
|
||||
) {
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
match sk.dist {
|
||||
SecretDistribution::NONE => panic!("invalid sk: SecretDistribution::NONE"),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
// Its ok to allocate scratch space here since pk is usually generated only once.
|
||||
let mut scratch: ScratchOwned = ScratchOwned::new(FourierGLWECiphertext::encrypt_sk_scratch_space(
|
||||
module,
|
||||
self.basek(),
|
||||
self.k(),
|
||||
self.rank(),
|
||||
));
|
||||
|
||||
self.data
|
||||
.encrypt_zero_sk(module, sk, source_xa, source_xe, sigma, scratch.borrow());
|
||||
self.dist = sk.dist;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user