fixed encryption

This commit is contained in:
Pro7ech
2025-10-19 18:11:04 +02:00
parent d6e9805a8f
commit a706b759d6
17 changed files with 289 additions and 196 deletions

View File

@@ -1,5 +1,13 @@
use std::io::{Read, Result, Write};
pub trait GetDistribution {
fn dist(&self) -> &Distribution;
}
pub trait GetDistributionMut {
fn dist_mut(&mut self) -> &mut Distribution;
}
#[derive(Clone, Copy, Debug)]
pub enum Distribution {
TernaryFixed(usize), // Ternary with fixed Hamming weight

View File

@@ -11,7 +11,7 @@ use crate::{
glwe_ct::{GLWEEncryptSk, GLWEEncryptSkInternal},
},
layouts::{
GGLWEInfos, GLWEPlaintextAlloc, LWEInfos,
GGLWECompressedSeedMut, GGLWEInfos, GLWEPlaintextAlloc, GLWESecretPrepared, LWEInfos,
compressed::{GGLWECompressed, GGLWECompressedToMut},
prepared::GLWESecretPreparedToRef,
},
@@ -60,7 +60,7 @@ pub trait GGLWECompressedEncryptSk<BE: Backend> {
source_xe: &mut Source,
scratch: &mut Scratch<BE>,
) where
R: GGLWECompressedToMut,
R: GGLWECompressedToMut + GGLWECompressedSeedMut,
P: ScalarZnxToRef,
S: GLWESecretPreparedToRef<BE>;
}
@@ -94,80 +94,85 @@ where
source_xe: &mut Source,
scratch: &mut Scratch<BE>,
) where
R: GGLWECompressedToMut,
R: GGLWECompressedToMut + GGLWECompressedSeedMut,
P: ScalarZnxToRef,
S: GLWESecretPreparedToRef<BE>,
{
let res: &mut GGLWECompressed<&mut [u8]> = &mut res.to_mut();
let pt: &ScalarZnx<&[u8]> = &pt.to_ref();
let mut seeds: Vec<[u8; 32]> = vec![[0u8; 32]; res.seed_mut().len()];
let sk = &sk.to_ref();
{
let res: &mut GGLWECompressed<&mut [u8]> = &mut res.to_mut();
let pt: &ScalarZnx<&[u8]> = &pt.to_ref();
let sk: &GLWESecretPrepared<&[u8], BE> = &sk.to_ref();
assert_eq!(
res.rank_in(),
pt.cols() as u32,
"res.rank_in(): {} != pt.cols(): {}",
res.rank_in(),
pt.cols()
);
assert_eq!(
res.rank_out(),
sk.rank(),
"res.rank_out(): {} != sk.rank(): {}",
res.rank_out(),
sk.rank()
);
assert_eq!(res.n(), sk.n());
assert_eq!(pt.n() as u32, sk.n());
assert!(
scratch.available() >= GGLWECompressed::encrypt_sk_tmp_bytes(self, res),
"scratch.available: {} < GGLWECiphertext::encrypt_sk_tmp_bytes: {}",
scratch.available(),
GGLWECompressed::encrypt_sk_tmp_bytes(self, res)
);
assert!(
res.dnum().0 * res.dsize().0 * res.base2k().0 <= res.k().0,
"res.dnum() : {} * res.dsize() : {} * res.base2k() : {} = {} >= res.k() = {}",
res.dnum(),
res.dsize(),
res.base2k(),
res.dnum().0 * res.dsize().0 * res.base2k().0,
res.k()
);
assert_eq!(
res.rank_in(),
pt.cols() as u32,
"res.rank_in(): {} != pt.cols(): {}",
res.rank_in(),
pt.cols()
);
assert_eq!(
res.rank_out(),
sk.rank(),
"res.rank_out(): {} != sk.rank(): {}",
res.rank_out(),
sk.rank()
);
assert_eq!(res.n(), sk.n());
assert_eq!(pt.n() as u32, sk.n());
assert!(
scratch.available() >= GGLWECompressed::encrypt_sk_tmp_bytes(self, res),
"scratch.available: {} < GGLWECiphertext::encrypt_sk_tmp_bytes: {}",
scratch.available(),
GGLWECompressed::encrypt_sk_tmp_bytes(self, res)
);
assert!(
res.dnum().0 * res.dsize().0 * res.base2k().0 <= res.k().0,
"res.dnum() : {} * res.dsize() : {} * res.base2k() : {} = {} >= res.k() = {}",
res.dnum(),
res.dsize(),
res.base2k(),
res.dnum().0 * res.dsize().0 * res.base2k().0,
res.k()
);
let dnum: usize = res.dnum().into();
let dsize: usize = res.dsize().into();
let base2k: usize = res.base2k().into();
let rank_in: usize = res.rank_in().into();
let cols: usize = (res.rank_out() + 1).into();
let dnum: usize = res.dnum().into();
let dsize: usize = res.dsize().into();
let base2k: usize = res.base2k().into();
let rank_in: usize = res.rank_in().into();
let cols: usize = (res.rank_out() + 1).into();
let mut source_xa = Source::new(seed);
let mut source_xa = Source::new(seed);
let (mut tmp_pt, scrach_1) = scratch.take_glwe_pt(self, res);
(0..rank_in).for_each(|col_i| {
(0..dnum).for_each(|d_i| {
// Adds the scalar_znx_pt to the i-th limb of the vec_znx_pt
tmp_pt.data.zero(); // zeroes for next iteration
self.vec_znx_add_scalar_inplace(&mut tmp_pt.data, 0, (dsize - 1) + d_i * dsize, pt, col_i);
self.vec_znx_normalize_inplace(base2k, &mut tmp_pt.data, 0, scrach_1);
let (mut tmp_pt, scrach_1) = scratch.take_glwe_pt(self, res);
for col_i in 0..rank_in {
for d_i in 0..dnum {
// Adds the scalar_znx_pt to the i-th limb of the vec_znx_pt
tmp_pt.data.zero(); // zeroes for next iteration
self.vec_znx_add_scalar_inplace(&mut tmp_pt.data, 0, (dsize - 1) + d_i * dsize, pt, col_i);
self.vec_znx_normalize_inplace(base2k, &mut tmp_pt.data, 0, scrach_1);
let (seed, mut source_xa_tmp) = source_xa.branch();
res.seed[col_i * dnum + d_i] = seed;
let (seed, mut source_xa_tmp) = source_xa.branch();
seeds[col_i * dnum + d_i] = seed;
self.glwe_encrypt_sk_internal(
res.base2k().into(),
res.k().into(),
&mut res.at_mut(d_i, col_i).data,
cols,
true,
Some((&tmp_pt, 0)),
sk,
&mut source_xa_tmp,
source_xe,
SIGMA,
scrach_1,
);
});
});
self.glwe_encrypt_sk_internal(
res.base2k().into(),
res.k().into(),
&mut res.at_mut(d_i, col_i).data,
cols,
true,
Some((&tmp_pt, 0)),
sk,
&mut source_xa_tmp,
source_xe,
SIGMA,
scrach_1,
);
}
}
}
res.seed_mut().copy_from_slice(&seeds);
}
}

View File

@@ -9,11 +9,10 @@ use poulpy_hal::{
};
use crate::{
ScratchTakeCore,
encryption::compressed::gglwe_ksk::GLWESwitchingKeyCompressedEncryptSk,
encryption::gglwe_tsk::TensorKeyEncryptSk,
GetDistribution, ScratchTakeCore,
encryption::{compressed::gglwe_ksk::GLWESwitchingKeyCompressedEncryptSk, gglwe_tsk::TensorKeyEncryptSk},
layouts::{
GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GetDist, LWEInfos, Rank,
GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, Rank,
compressed::{TensorKeyCompressed, TensorKeyCompressedToMut},
},
};
@@ -37,7 +36,7 @@ impl<DataSelf: DataMut> TensorKeyCompressed<DataSelf> {
source_xe: &mut Source,
scratch: &mut Scratch<BE>,
) where
S: GLWESecretToRef + GetDist,
S: GLWESecretToRef + GetDistribution,
M: GGLWETensorKeyCompressedEncryptSk<BE>,
{
module.gglwe_tensor_key_encrypt_sk(self, sk, seed_xa, source_xe, scratch);
@@ -58,7 +57,7 @@ pub trait GGLWETensorKeyCompressedEncryptSk<BE: Backend> {
scratch: &mut Scratch<BE>,
) where
R: TensorKeyCompressedToMut,
S: GLWESecretToRef + GetDist;
S: GLWESecretToRef + GetDistribution;
}
impl<BE: Backend> GGLWETensorKeyCompressedEncryptSk<BE> for Module<BE>
@@ -95,7 +94,7 @@ where
scratch: &mut Scratch<BE>,
) where
R: TensorKeyCompressedToMut,
S: GLWESecretToRef + GetDist,
S: GLWESecretToRef + GetDistribution,
{
let res: &mut TensorKeyCompressed<&mut [u8]> = &mut res.to_mut();

View File

@@ -8,7 +8,7 @@ use crate::{
ScratchTakeCore,
encryption::{SIGMA, ggsw_ct::GGSWEncryptSk, glwe_ct::GLWEEncryptSkInternal},
layouts::{
GGSWInfos, GLWEInfos, LWEInfos,
GGSWCompressedSeedMut, GGSWInfos, GLWEInfos, LWEInfos,
compressed::{GGSWCompressed, GGSWCompressedToMut},
prepared::{GLWESecretPrepared, GLWESecretPreparedToRef},
},
@@ -57,7 +57,7 @@ pub trait GGSWCompressedEncryptSk<BE: Backend> {
source_xe: &mut Source,
scratch: &mut Scratch<BE>,
) where
R: GGSWCompressedToMut,
R: GGSWCompressedToMut + GGSWCompressedSeedMut,
P: ScalarZnxToRef,
S: GLWESecretPreparedToRef<BE>;
}
@@ -83,62 +83,66 @@ where
source_xe: &mut Source,
scratch: &mut Scratch<BE>,
) where
R: GGSWCompressedToMut,
R: GGSWCompressedToMut + GGSWCompressedSeedMut,
P: ScalarZnxToRef,
S: GLWESecretPreparedToRef<BE>,
{
let res: &mut GGSWCompressed<&mut [u8]> = &mut res.to_mut();
let sk: &GLWESecretPrepared<&[u8], BE> = &sk.to_ref();
let pt: &ScalarZnx<&[u8]> = &pt.to_ref();
let mut seeds: Vec<[u8; 32]> = vec![[0u8; 32]; res.seed_mut().len()];
#[cfg(debug_assertions)]
{
use poulpy_hal::layouts::ZnxInfos;
let res: &mut GGSWCompressed<&mut [u8]> = &mut res.to_mut();
let sk: &GLWESecretPrepared<&[u8], BE> = &sk.to_ref();
let pt: &ScalarZnx<&[u8]> = &pt.to_ref();
assert_eq!(res.rank(), sk.rank());
assert_eq!(res.n(), sk.n());
assert_eq!(pt.n() as u32, sk.n());
}
#[cfg(debug_assertions)]
{
use poulpy_hal::layouts::ZnxInfos;
let base2k: usize = res.base2k().into();
let rank: usize = res.rank().into();
let cols: usize = rank + 1;
let dsize: usize = res.dsize().into();
assert_eq!(res.rank(), sk.rank());
assert_eq!(res.n(), sk.n());
assert_eq!(pt.n() as u32, sk.n());
}
let (mut tmp_pt, scratch_1) = scratch.take_glwe_pt(self, &res.glwe_layout());
let base2k: usize = res.base2k().into();
let rank: usize = res.rank().into();
let cols: usize = rank + 1;
let dsize: usize = res.dsize().into();
let mut source = Source::new(seed_xa);
let (mut tmp_pt, scratch_1) = scratch.take_glwe_pt(self, &res.glwe_layout());
res.seed = vec![[0u8; 32]; res.dnum().0 as usize * cols];
let mut source = Source::new(seed_xa);
for row_i in 0..res.dnum().into() {
tmp_pt.data.zero();
for row_i in 0..res.dnum().into() {
tmp_pt.data.zero();
// Adds the scalar_znx_pt to the i-th limb of the vec_znx_pt
self.vec_znx_add_scalar_inplace(&mut tmp_pt.data, 0, (dsize - 1) + row_i * dsize, pt, 0);
self.vec_znx_normalize_inplace(base2k, &mut tmp_pt.data, 0, scratch_1);
// Adds the scalar_znx_pt to the i-th limb of the vec_znx_pt
self.vec_znx_add_scalar_inplace(&mut tmp_pt.data, 0, (dsize - 1) + row_i * dsize, pt, 0);
self.vec_znx_normalize_inplace(base2k, &mut tmp_pt.data, 0, scratch_1);
for col_j in 0..rank + 1 {
// rlwe encrypt of vec_znx_pt into vec_znx_ct
for col_j in 0..rank + 1 {
// rlwe encrypt of vec_znx_pt into vec_znx_ct
let (seed, mut source_xa_tmp) = source.branch();
let (seed, mut source_xa_tmp) = source.branch();
res.seed[row_i * cols + col_j] = seed;
seeds[row_i * cols + col_j] = seed;
self.glwe_encrypt_sk_internal(
res.base2k().into(),
res.k().into(),
&mut res.at_mut(row_i, col_j).data,
cols,
true,
Some((&tmp_pt, col_j)),
sk,
&mut source_xa_tmp,
source_xe,
SIGMA,
scratch_1,
);
self.glwe_encrypt_sk_internal(
res.base2k().into(),
res.k().into(),
&mut res.at_mut(row_i, col_j).data,
cols,
true,
Some((&tmp_pt, col_j)),
sk,
&mut source_xa_tmp,
source_xe,
SIGMA,
scratch_1,
);
}
}
}
res.seed_mut().copy_from_slice(&seeds);
}
}

View File

@@ -9,7 +9,7 @@ use crate::{
glwe_ct::{GLWEEncryptSk, GLWEEncryptSkInternal},
},
layouts::{
GLWEInfos, GLWEPlaintextToRef, LWEInfos,
GLWECompressedSeedMut, GLWEInfos, GLWEPlaintextToRef, LWEInfos,
compressed::{GLWECompressed, GLWECompressedToMut},
prepared::GLWESecretPreparedToRef,
},
@@ -58,7 +58,7 @@ pub trait GLWECompressedEncryptSk<BE: Backend> {
source_xe: &mut Source,
scratch: &mut Scratch<BE>,
) where
R: GLWECompressedToMut,
R: GLWECompressedToMut + GLWECompressedSeedMut,
P: GLWEPlaintextToRef,
S: GLWESecretPreparedToRef<BE>;
}
@@ -83,28 +83,30 @@ where
source_xe: &mut Source,
scratch: &mut Scratch<BE>,
) where
R: GLWECompressedToMut,
R: GLWECompressedToMut + GLWECompressedSeedMut,
P: GLWEPlaintextToRef,
S: GLWESecretPreparedToRef<BE>,
{
let res: &mut GLWECompressed<&mut [u8]> = &mut res.to_mut();
let mut source_xa: Source = Source::new(seed_xa);
let cols: usize = (res.rank() + 1).into();
{
let res: &mut GLWECompressed<&mut [u8]> = &mut res.to_mut();
let mut source_xa: Source = Source::new(seed_xa);
let cols: usize = (res.rank() + 1).into();
self.glwe_encrypt_sk_internal(
res.base2k().into(),
res.k().into(),
&mut res.data,
cols,
true,
Some((pt, 0)),
sk,
&mut source_xa,
source_xe,
SIGMA,
scratch,
);
self.glwe_encrypt_sk_internal(
res.base2k().into(),
res.k().into(),
&mut res.data,
cols,
true,
Some((pt, 0)),
sk,
&mut source_xa,
source_xe,
SIGMA,
scratch,
);
}
res.seed = seed_xa;
res.seed_mut().copy_from_slice(&seed_xa);
}
}

View File

@@ -8,10 +8,10 @@ use poulpy_hal::{
};
use crate::{
ScratchTakeCore,
GetDistribution, ScratchTakeCore,
encryption::gglwe_ksk::GLWESwitchingKeyEncryptSk,
layouts::{
GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, GetDist, LWEInfos, Rank, TensorKey, TensorKeyToMut,
GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, GLWESwitchingKey, LWEInfos, Rank, TensorKey, TensorKeyToMut,
prepared::{GLWESecretPrepare, GLWESecretPrepared, GLWESecretPreparedAlloc},
},
};
@@ -36,7 +36,7 @@ impl<DataSelf: DataMut> TensorKey<DataSelf> {
scratch: &mut Scratch<BE>,
) where
M: TensorKeyEncryptSk<BE>,
S: GLWESecretToRef + GetDist,
S: GLWESecretToRef + GetDistribution,
Scratch<BE>: ScratchTakeCore<BE>,
{
module.tensor_key_encrypt_sk(self, sk, source_xa, source_xe, scratch);
@@ -57,7 +57,7 @@ pub trait TensorKeyEncryptSk<BE: Backend> {
scratch: &mut Scratch<BE>,
) where
R: TensorKeyToMut,
S: GLWESecretToRef + GetDist;
S: GLWESecretToRef + GetDistribution;
}
impl<BE: Backend> TensorKeyEncryptSk<BE> for Module<BE>
@@ -95,7 +95,7 @@ where
scratch: &mut Scratch<BE>,
) where
R: TensorKeyToMut,
S: GLWESecretToRef + GetDist,
S: GLWESecretToRef + GetDistribution,
{
let res: &mut TensorKey<&mut [u8]> = &mut res.to_mut();

View File

@@ -510,6 +510,8 @@ where
// ct[i] = uniform (+ pt)
self.vec_znx_fill_uniform(base2k, ct, col_ct, source_xa);
println!("vec_znx_fill_uniform: {}", ct);
let (mut ci_dft, scratch_3) = scratch_2.take_vec_znx_dft(self, 1, size);
// ci = ct[i] - pt

View File

@@ -5,7 +5,7 @@ use poulpy_hal::{
};
use crate::{
Distribution, ScratchTakeCore,
Distribution, GetDistribution, GetDistributionMut, ScratchTakeCore,
encryption::glwe_ct::GLWEEncryptSk,
layouts::{
GLWE, GLWEPublicKey, GLWEPublicKeyToMut, LWEInfos,
@@ -16,7 +16,7 @@ use crate::{
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>,
S: GLWESecretPreparedToRef<BE> + GetDistribution,
M: GLWEPublicKeyGenerate<BE>,
{
module.glwe_public_key_generate(self, sk, source_xa, source_xe);
@@ -26,8 +26,8 @@ impl<D: DataMut> GLWEPublicKey<D> {
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: GLWEPublicKeyToMut,
S: GLWESecretPreparedToRef<BE>;
R: GLWEPublicKeyToMut + GetDistributionMut,
S: GLWESecretPreparedToRef<BE> + GetDistribution;
}
impl<BE: Backend> GLWEPublicKeyGenerate<BE> for Module<BE>
@@ -38,25 +38,27 @@ where
{
fn glwe_public_key_generate<R, S>(&self, res: &mut R, sk: &S, source_xa: &mut Source, source_xe: &mut Source)
where
R: GLWEPublicKeyToMut,
S: GLWESecretPreparedToRef<BE>,
R: GLWEPublicKeyToMut + GetDistributionMut,
S: GLWESecretPreparedToRef<BE> + GetDistribution,
{
let res: &mut GLWEPublicKey<&mut [u8]> = &mut res.to_mut();
let sk: &GLWESecretPrepared<&[u8], BE> = &sk.to_ref();
{
let res: &mut GLWEPublicKey<&mut [u8]> = &mut res.to_mut();
let sk: &GLWESecretPrepared<&[u8], BE> = &sk.to_ref();
assert_eq!(res.n(), self.n() as u32);
assert_eq!(sk.n(), self.n() as u32);
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")
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(self, res);
tmp.encrypt_zero_sk(self, sk, source_xa, source_xe, scratch.borrow());
}
// 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(self, res);
tmp.encrypt_zero_sk(self, sk, source_xa, source_xe, scratch.borrow());
res.dist = sk.dist;
*res.dist_mut() = *sk.dist();
}
}

View File

@@ -23,6 +23,25 @@ pub struct GGLWECompressed<D: Data> {
pub(crate) seed: Vec<[u8; 32]>,
}
pub trait GGLWECompressedSeedMut {
fn seed_mut(&mut self) -> &mut Vec<[u8; 32]>;
}
impl<D: DataMut> GGLWECompressedSeedMut for GGLWECompressed<D> {
fn seed_mut(&mut self) -> &mut Vec<[u8; 32]> {
&mut self.seed
}
}
pub trait GGLWECompressedSeed {
fn seed(&self) -> &Vec<[u8; 32]>;
}
impl<D: DataRef> GGLWECompressedSeed for GGLWECompressed<D> {
fn seed(&self) -> &Vec<[u8; 32]> {
&self.seed
}
}
impl<D: Data> LWEInfos for GGLWECompressed<D> {
fn n(&self) -> Degree {
Degree(self.data.n() as u32)

View File

@@ -22,6 +22,26 @@ pub struct GGSWCompressed<D: Data> {
pub(crate) seed: Vec<[u8; 32]>,
}
pub trait GGSWCompressedSeedMut {
fn seed_mut(&mut self) -> &mut Vec<[u8; 32]>;
}
impl<D: DataMut> GGSWCompressedSeedMut for GGSWCompressed<D> {
fn seed_mut(&mut self) -> &mut Vec<[u8; 32]> {
&mut self.seed
}
}
pub trait GGSWCompressedSeed {
fn seed(&self) -> &Vec<[u8; 32]>;
}
impl<D: DataRef> GGSWCompressedSeed for GGSWCompressed<D> {
fn seed(&self) -> &Vec<[u8; 32]> {
&self.seed
}
}
impl<D: Data> LWEInfos for GGSWCompressed<D> {
fn n(&self) -> Degree {
Degree(self.data.n() as u32)

View File

@@ -19,6 +19,26 @@ pub struct GLWECompressed<D: Data> {
pub(crate) seed: [u8; 32],
}
pub trait GLWECompressedSeedMut {
fn seed_mut(&mut self) -> &mut [u8; 32];
}
impl<D: DataMut> GLWECompressedSeedMut for GLWECompressed<D> {
fn seed_mut(&mut self) -> &mut [u8; 32] {
&mut self.seed
}
}
pub trait GLWECompressedSeed {
fn seed(&self) -> &[u8; 32];
}
impl<D: DataRef> GLWECompressedSeed for GLWECompressed<D> {
fn seed(&self) -> &[u8; 32] {
&self.seed
}
}
impl<D: Data> LWEInfos for GLWECompressed<D> {
fn base2k(&self) -> Base2K {
self.base2k

View File

@@ -3,6 +3,7 @@ use poulpy_hal::layouts::{
};
use crate::{
GetDistribution, GetDistributionMut,
dist::Distribution,
layouts::{Base2K, Degree, GLWEInfos, GetDegree, LWEInfos, Rank, TorusPrecision},
};
@@ -16,6 +17,18 @@ pub struct GLWEPublicKey<D: Data> {
pub(crate) dist: Distribution,
}
impl<D: DataMut> GetDistributionMut for GLWEPublicKey<D> {
fn dist_mut(&mut self) -> &mut Distribution {
&mut self.dist
}
}
impl<D: DataRef> GetDistribution for GLWEPublicKey<D> {
fn dist(&self) -> &Distribution {
&self.dist
}
}
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
pub struct GLWEPublicKeyLayout {
pub n: Degree,
@@ -24,16 +37,6 @@ pub struct GLWEPublicKeyLayout {
pub rank: Rank,
}
pub trait GetDist {
fn get_dist(&self) -> Distribution;
}
impl<D: DataRef> GetDist for GLWEPublicKey<D> {
fn get_dist(&self) -> Distribution {
self.dist
}
}
impl<D: Data> LWEInfos for GLWEPublicKey<D> {
fn base2k(&self) -> Base2K {
self.base2k

View File

@@ -7,8 +7,9 @@ use poulpy_hal::{
};
use crate::{
GetDistribution,
dist::Distribution,
layouts::{Base2K, Degree, GLWEInfos, GetDegree, GetDist, LWEInfos, Rank, TorusPrecision},
layouts::{Base2K, Degree, GLWEInfos, GetDegree, LWEInfos, Rank, TorusPrecision},
};
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
@@ -64,9 +65,9 @@ impl<D: Data> LWEInfos for GLWESecret<D> {
}
}
impl<D: Data> GetDist for GLWESecret<D> {
fn get_dist(&self) -> Distribution {
self.dist
impl<D: Data> GetDistribution for GLWESecret<D> {
fn dist(&self) -> &Distribution {
&self.dist
}
}

View File

@@ -4,8 +4,9 @@ use poulpy_hal::{
};
use crate::{
GetDistribution, GetDistributionMut,
dist::Distribution,
layouts::{Base2K, Degree, GLWEInfos, GLWEPublicKey, GLWEPublicKeyToRef, GetDegree, GetDist, LWEInfos, Rank, TorusPrecision},
layouts::{Base2K, Degree, GLWEInfos, GLWEPublicKey, GLWEPublicKeyToRef, GetDegree, LWEInfos, Rank, TorusPrecision},
};
#[derive(PartialEq, Eq)]
@@ -16,13 +17,15 @@ pub struct GLWEPublicKeyPrepared<D: Data, B: Backend> {
pub(crate) dist: Distribution,
}
pub trait SetDist {
fn set_dist(&mut self, dist: Distribution);
impl<D: DataRef, BE: Backend> GetDistribution for GLWEPublicKeyPrepared<D, BE> {
fn dist(&self) -> &Distribution {
&self.dist
}
}
impl<D: Data, B: Backend> SetDist for GLWEPublicKeyPrepared<D, B> {
fn set_dist(&mut self, dist: Distribution) {
self.dist = dist
impl<D: DataMut, BE: Backend> GetDistributionMut for GLWEPublicKeyPrepared<D, BE> {
fn dist_mut(&mut self) -> &mut Distribution {
&mut self.dist
}
}
@@ -122,8 +125,8 @@ where
{
fn prepare_glwe_public_key<R, O>(&self, res: &mut R, other: &O)
where
R: GLWEPublicKeyPreparedToMut<B> + SetDist,
O: GLWEPublicKeyToRef + GetDist,
R: GLWEPublicKeyPreparedToMut<B> + GetDistributionMut,
O: GLWEPublicKeyToRef + GetDistribution,
{
{
let mut res: GLWEPublicKeyPrepared<&mut [u8], B> = res.to_mut();
@@ -140,7 +143,7 @@ where
}
}
res.set_dist(other.get_dist());
*res.dist_mut() = *other.dist();
}
}
@@ -149,7 +152,7 @@ impl<B: Backend> GLWEPublicKeyPrepare<B> for Module<B> where Self: GetDegree + V
impl<D: DataMut, B: Backend> GLWEPublicKeyPrepared<D, B> {
pub fn prepare<O, M>(&mut self, module: &M, other: &O)
where
O: GLWEPublicKeyToRef + GetDist,
O: GLWEPublicKeyToRef + GetDistribution,
M: GLWEPublicKeyPrepare<B>,
{
module.prepare_glwe_public_key(self, other);

View File

@@ -4,11 +4,9 @@ use poulpy_hal::{
};
use crate::{
GetDistribution, GetDistributionMut,
dist::Distribution,
layouts::{
Base2K, Degree, GLWEInfos, GLWESecret, GLWESecretToRef, GetDegree, GetDist, LWEInfos, Rank, TorusPrecision,
prepared::SetDist,
},
layouts::{Base2K, Degree, GLWEInfos, GLWESecret, GLWESecretToRef, GetDegree, LWEInfos, Rank, TorusPrecision},
};
pub struct GLWESecretPrepared<D: Data, B: Backend> {
@@ -16,9 +14,15 @@ pub struct GLWESecretPrepared<D: Data, B: Backend> {
pub(crate) dist: Distribution,
}
impl<D: DataRef, B: Backend> SetDist for GLWESecretPrepared<D, B> {
fn set_dist(&mut self, dist: Distribution) {
self.dist = dist
impl<D: DataRef, BE: Backend> GetDistribution for GLWESecretPrepared<D, BE> {
fn dist(&self) -> &Distribution {
&self.dist
}
}
impl<D: DataMut, BE: Backend> GetDistributionMut for GLWESecretPrepared<D, BE> {
fn dist_mut(&mut self) -> &mut Distribution {
&mut self.dist
}
}
@@ -125,8 +129,8 @@ where
{
fn prepare_glwe_secret<R, O>(&self, res: &mut R, other: &O)
where
R: GLWESecretPreparedToMut<B> + SetDist,
O: GLWESecretToRef + GetDist,
R: GLWESecretPreparedToMut<B> + GetDistributionMut,
O: GLWESecretToRef + GetDistribution,
{
{
let mut res: GLWESecretPrepared<&mut [u8], _> = res.to_mut();
@@ -137,7 +141,7 @@ where
}
}
res.set_dist(other.get_dist());
*res.dist_mut() = *other.dist();
}
}
@@ -147,7 +151,7 @@ impl<D: DataMut, B: Backend> GLWESecretPrepared<D, B> {
pub fn prepare<M, O>(&mut self, module: &M, other: &O)
where
M: GLWESecretPrepare<B>,
O: GLWESecretToRef + GetDist,
O: GLWESecretToRef + GetDistribution,
{
module.prepare_glwe_secret(self, other);
}

View File

@@ -14,7 +14,7 @@ backend_test_suite!(
tests = {
//GLWE Encryption
glwe_encrypt_sk => crate::tests::test_suite::encryption::test_glwe_encrypt_sk,
lwe_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_glwe_compressed_encrypt_sk,
glwe_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_glwe_compressed_encrypt_sk,
glwe_encrypt_zero_sk => crate::tests::test_suite::encryption::test_glwe_encrypt_zero_sk,
glwe_encrypt_pk => crate::tests::test_suite::encryption::test_glwe_encrypt_pk,
// GLWE Keyswitch

View File

@@ -104,6 +104,7 @@ where
let k_pt: usize = 30;
for rank in 1_usize..3 {
println!("rank: {}", rank);
let n: usize = module.n();
let glwe_infos: GLWELayout = GLWELayout {