mirror of
https://github.com/arnaucube/poulpy.git
synced 2026-02-10 13:16:44 +01:00
Add cross-basek normalization (#90)
* added cross_basek_normalization * updated method signatures to take layouts * fixed cross-base normalization fix #91 fix #93
This commit is contained in:
committed by
GitHub
parent
4da790ea6a
commit
37e13b965c
@@ -6,14 +6,15 @@ use poulpy_hal::{
|
||||
layouts::{Backend, DataMut, DataRef, DataViewMut, Module, Scratch},
|
||||
};
|
||||
|
||||
use crate::layouts::{GLWECiphertext, GLWEPlaintext, Infos, prepared::GLWESecretPrepared};
|
||||
use crate::layouts::{GLWECiphertext, GLWEInfos, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared};
|
||||
|
||||
impl GLWECiphertext<Vec<u8>> {
|
||||
pub fn decrypt_scratch_space<B: Backend>(module: &Module<B>, basek: usize, k: usize) -> usize
|
||||
pub fn decrypt_scratch_space<B: Backend, A>(module: &Module<B>, infos: &A) -> usize
|
||||
where
|
||||
A: GLWEInfos,
|
||||
Module<B>: VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes,
|
||||
{
|
||||
let size: usize = k.div_ceil(basek);
|
||||
let size: usize = infos.size();
|
||||
(module.vec_znx_normalize_tmp_bytes() | module.vec_znx_dft_alloc_bytes(1, size)) + module.vec_znx_dft_alloc_bytes(1, size)
|
||||
}
|
||||
}
|
||||
@@ -41,15 +42,15 @@ impl<DataSelf: DataRef> GLWECiphertext<DataSelf> {
|
||||
assert_eq!(pt.n(), sk.n());
|
||||
}
|
||||
|
||||
let cols: usize = self.rank() + 1;
|
||||
let cols: usize = (self.rank() + 1).into();
|
||||
|
||||
let (mut c0_big, scratch_1) = scratch.take_vec_znx_big(self.n(), 1, self.size()); // TODO optimize size when pt << ct
|
||||
let (mut c0_big, scratch_1) = scratch.take_vec_znx_big(self.n().into(), 1, self.size()); // TODO optimize size when pt << ct
|
||||
c0_big.data_mut().fill(0);
|
||||
|
||||
{
|
||||
(1..cols).for_each(|i| {
|
||||
// ci_dft = DFT(a[i]) * DFT(s[i])
|
||||
let (mut ci_dft, _) = scratch_1.take_vec_znx_dft(self.n(), 1, self.size()); // TODO optimize size when pt << ct
|
||||
let (mut ci_dft, _) = scratch_1.take_vec_znx_dft(self.n().into(), 1, self.size()); // TODO optimize size when pt << ct
|
||||
module.vec_znx_dft_apply(1, 0, &mut ci_dft, 0, &self.data, i);
|
||||
module.svp_apply_dft_to_dft_inplace(&mut ci_dft, 0, &sk.data, i - 1);
|
||||
let ci_big = module.vec_znx_idft_apply_consume(ci_dft);
|
||||
@@ -63,9 +64,17 @@ impl<DataSelf: DataRef> GLWECiphertext<DataSelf> {
|
||||
module.vec_znx_big_add_small_inplace(&mut c0_big, 0, &self.data, 0);
|
||||
|
||||
// pt = norm(BIG(m + e))
|
||||
module.vec_znx_big_normalize(self.basek(), &mut pt.data, 0, &c0_big, 0, scratch_1);
|
||||
module.vec_znx_big_normalize(
|
||||
self.base2k().into(),
|
||||
&mut pt.data,
|
||||
0,
|
||||
self.base2k().into(),
|
||||
&c0_big,
|
||||
0,
|
||||
scratch_1,
|
||||
);
|
||||
|
||||
pt.basek = self.basek();
|
||||
pt.base2k = self.base2k();
|
||||
pt.k = pt.k().min(self.k());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ use poulpy_hal::{
|
||||
oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl},
|
||||
};
|
||||
|
||||
use crate::layouts::{Infos, LWECiphertext, LWEPlaintext, LWESecret, SetMetaData};
|
||||
use crate::layouts::{LWECiphertext, LWEInfos, LWEPlaintext, LWESecret};
|
||||
|
||||
impl<DataSelf> LWECiphertext<DataSelf>
|
||||
where
|
||||
@@ -31,13 +31,13 @@ where
|
||||
.sum::<i64>();
|
||||
});
|
||||
module.zn_normalize_inplace(
|
||||
pt.n(),
|
||||
self.basek(),
|
||||
1,
|
||||
self.base2k().into(),
|
||||
&mut pt.data,
|
||||
0,
|
||||
ScratchOwned::alloc(size_of::<i64>()).borrow(),
|
||||
);
|
||||
pt.set_basek(self.basek());
|
||||
pt.set_k(self.k().min(pt.size() * self.basek()));
|
||||
pt.base2k = self.base2k();
|
||||
pt.k = crate::layouts::TorusPrecision(self.k().0.min(pt.size() as u32 * self.base2k().0));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user