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}; 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)] #[derive(Clone, Copy, Debug)]
pub enum Distribution { pub enum Distribution {
TernaryFixed(usize), // Ternary with fixed Hamming weight TernaryFixed(usize), // Ternary with fixed Hamming weight

View File

@@ -11,7 +11,7 @@ use crate::{
glwe_ct::{GLWEEncryptSk, GLWEEncryptSkInternal}, glwe_ct::{GLWEEncryptSk, GLWEEncryptSkInternal},
}, },
layouts::{ layouts::{
GGLWEInfos, GLWEPlaintextAlloc, LWEInfos, GGLWECompressedSeedMut, GGLWEInfos, GLWEPlaintextAlloc, GLWESecretPrepared, LWEInfos,
compressed::{GGLWECompressed, GGLWECompressedToMut}, compressed::{GGLWECompressed, GGLWECompressedToMut},
prepared::GLWESecretPreparedToRef, prepared::GLWESecretPreparedToRef,
}, },
@@ -60,7 +60,7 @@ pub trait GGLWECompressedEncryptSk<BE: Backend> {
source_xe: &mut Source, source_xe: &mut Source,
scratch: &mut Scratch<BE>, scratch: &mut Scratch<BE>,
) where ) where
R: GGLWECompressedToMut, R: GGLWECompressedToMut + GGLWECompressedSeedMut,
P: ScalarZnxToRef, P: ScalarZnxToRef,
S: GLWESecretPreparedToRef<BE>; S: GLWESecretPreparedToRef<BE>;
} }
@@ -94,14 +94,16 @@ where
source_xe: &mut Source, source_xe: &mut Source,
scratch: &mut Scratch<BE>, scratch: &mut Scratch<BE>,
) where ) where
R: GGLWECompressedToMut, R: GGLWECompressedToMut + GGLWECompressedSeedMut,
P: ScalarZnxToRef, P: ScalarZnxToRef,
S: GLWESecretPreparedToRef<BE>, S: GLWESecretPreparedToRef<BE>,
{
let mut seeds: Vec<[u8; 32]> = vec![[0u8; 32]; res.seed_mut().len()];
{ {
let res: &mut GGLWECompressed<&mut [u8]> = &mut res.to_mut(); let res: &mut GGLWECompressed<&mut [u8]> = &mut res.to_mut();
let pt: &ScalarZnx<&[u8]> = &pt.to_ref(); let pt: &ScalarZnx<&[u8]> = &pt.to_ref();
let sk: &GLWESecretPrepared<&[u8], BE> = &sk.to_ref();
let sk = &sk.to_ref();
assert_eq!( assert_eq!(
res.rank_in(), res.rank_in(),
@@ -144,15 +146,15 @@ where
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); let (mut tmp_pt, scrach_1) = scratch.take_glwe_pt(self, res);
(0..rank_in).for_each(|col_i| { for col_i in 0..rank_in {
(0..dnum).for_each(|d_i| { for d_i in 0..dnum {
// Adds the scalar_znx_pt to the i-th limb of the vec_znx_pt // Adds the scalar_znx_pt to the i-th limb of the vec_znx_pt
tmp_pt.data.zero(); // zeroes for next iteration 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_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); self.vec_znx_normalize_inplace(base2k, &mut tmp_pt.data, 0, scrach_1);
let (seed, mut source_xa_tmp) = source_xa.branch(); let (seed, mut source_xa_tmp) = source_xa.branch();
res.seed[col_i * dnum + d_i] = seed; seeds[col_i * dnum + d_i] = seed;
self.glwe_encrypt_sk_internal( self.glwe_encrypt_sk_internal(
res.base2k().into(), res.base2k().into(),
@@ -167,7 +169,10 @@ where
SIGMA, SIGMA,
scrach_1, scrach_1,
); );
}); }
}); }
}
res.seed_mut().copy_from_slice(&seeds);
} }
} }

View File

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

View File

@@ -8,7 +8,7 @@ use crate::{
ScratchTakeCore, ScratchTakeCore,
encryption::{SIGMA, ggsw_ct::GGSWEncryptSk, glwe_ct::GLWEEncryptSkInternal}, encryption::{SIGMA, ggsw_ct::GGSWEncryptSk, glwe_ct::GLWEEncryptSkInternal},
layouts::{ layouts::{
GGSWInfos, GLWEInfos, LWEInfos, GGSWCompressedSeedMut, GGSWInfos, GLWEInfos, LWEInfos,
compressed::{GGSWCompressed, GGSWCompressedToMut}, compressed::{GGSWCompressed, GGSWCompressedToMut},
prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, prepared::{GLWESecretPrepared, GLWESecretPreparedToRef},
}, },
@@ -57,7 +57,7 @@ pub trait GGSWCompressedEncryptSk<BE: Backend> {
source_xe: &mut Source, source_xe: &mut Source,
scratch: &mut Scratch<BE>, scratch: &mut Scratch<BE>,
) where ) where
R: GGSWCompressedToMut, R: GGSWCompressedToMut + GGSWCompressedSeedMut,
P: ScalarZnxToRef, P: ScalarZnxToRef,
S: GLWESecretPreparedToRef<BE>; S: GLWESecretPreparedToRef<BE>;
} }
@@ -83,9 +83,12 @@ where
source_xe: &mut Source, source_xe: &mut Source,
scratch: &mut Scratch<BE>, scratch: &mut Scratch<BE>,
) where ) where
R: GGSWCompressedToMut, R: GGSWCompressedToMut + GGSWCompressedSeedMut,
P: ScalarZnxToRef, P: ScalarZnxToRef,
S: GLWESecretPreparedToRef<BE>, S: GLWESecretPreparedToRef<BE>,
{
let mut seeds: Vec<[u8; 32]> = vec![[0u8; 32]; res.seed_mut().len()];
{ {
let res: &mut GGSWCompressed<&mut [u8]> = &mut res.to_mut(); let res: &mut GGSWCompressed<&mut [u8]> = &mut res.to_mut();
let sk: &GLWESecretPrepared<&[u8], BE> = &sk.to_ref(); let sk: &GLWESecretPrepared<&[u8], BE> = &sk.to_ref();
@@ -109,8 +112,6 @@ where
let mut source = Source::new(seed_xa); let mut source = Source::new(seed_xa);
res.seed = vec![[0u8; 32]; res.dnum().0 as usize * cols];
for row_i in 0..res.dnum().into() { for row_i in 0..res.dnum().into() {
tmp_pt.data.zero(); tmp_pt.data.zero();
@@ -123,7 +124,7 @@ where
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( self.glwe_encrypt_sk_internal(
res.base2k().into(), res.base2k().into(),
@@ -141,4 +142,7 @@ where
} }
} }
} }
res.seed_mut().copy_from_slice(&seeds);
}
} }

View File

@@ -9,7 +9,7 @@ use crate::{
glwe_ct::{GLWEEncryptSk, GLWEEncryptSkInternal}, glwe_ct::{GLWEEncryptSk, GLWEEncryptSkInternal},
}, },
layouts::{ layouts::{
GLWEInfos, GLWEPlaintextToRef, LWEInfos, GLWECompressedSeedMut, GLWEInfos, GLWEPlaintextToRef, LWEInfos,
compressed::{GLWECompressed, GLWECompressedToMut}, compressed::{GLWECompressed, GLWECompressedToMut},
prepared::GLWESecretPreparedToRef, prepared::GLWESecretPreparedToRef,
}, },
@@ -58,7 +58,7 @@ pub trait GLWECompressedEncryptSk<BE: Backend> {
source_xe: &mut Source, source_xe: &mut Source,
scratch: &mut Scratch<BE>, scratch: &mut Scratch<BE>,
) where ) where
R: GLWECompressedToMut, R: GLWECompressedToMut + GLWECompressedSeedMut,
P: GLWEPlaintextToRef, P: GLWEPlaintextToRef,
S: GLWESecretPreparedToRef<BE>; S: GLWESecretPreparedToRef<BE>;
} }
@@ -83,9 +83,10 @@ where
source_xe: &mut Source, source_xe: &mut Source,
scratch: &mut Scratch<BE>, scratch: &mut Scratch<BE>,
) where ) where
R: GLWECompressedToMut, R: GLWECompressedToMut + GLWECompressedSeedMut,
P: GLWEPlaintextToRef, P: GLWEPlaintextToRef,
S: GLWESecretPreparedToRef<BE>, S: GLWESecretPreparedToRef<BE>,
{
{ {
let res: &mut GLWECompressed<&mut [u8]> = &mut res.to_mut(); let res: &mut GLWECompressed<&mut [u8]> = &mut res.to_mut();
let mut source_xa: Source = Source::new(seed_xa); let mut source_xa: Source = Source::new(seed_xa);
@@ -104,7 +105,8 @@ where
SIGMA, SIGMA,
scratch, scratch,
); );
}
res.seed = seed_xa; res.seed_mut().copy_from_slice(&seed_xa);
} }
} }

View File

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

View File

@@ -510,6 +510,8 @@ where
// ct[i] = uniform (+ pt) // ct[i] = uniform (+ pt)
self.vec_znx_fill_uniform(base2k, ct, col_ct, source_xa); 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); let (mut ci_dft, scratch_3) = scratch_2.take_vec_znx_dft(self, 1, size);
// ci = ct[i] - pt // ci = ct[i] - pt

View File

@@ -5,7 +5,7 @@ use poulpy_hal::{
}; };
use crate::{ use crate::{
Distribution, ScratchTakeCore, Distribution, GetDistribution, GetDistributionMut, ScratchTakeCore,
encryption::glwe_ct::GLWEEncryptSk, encryption::glwe_ct::GLWEEncryptSk,
layouts::{ layouts::{
GLWE, GLWEPublicKey, GLWEPublicKeyToMut, LWEInfos, GLWE, GLWEPublicKey, GLWEPublicKeyToMut, LWEInfos,
@@ -16,7 +16,7 @@ use crate::{
impl<D: DataMut> GLWEPublicKey<D> { 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) pub fn generate<S, M, BE: Backend>(&mut self, module: &M, sk: &S, source_xa: &mut Source, source_xe: &mut Source)
where where
S: GLWESecretPreparedToRef<BE>, S: GLWESecretPreparedToRef<BE> + GetDistribution,
M: GLWEPublicKeyGenerate<BE>, M: GLWEPublicKeyGenerate<BE>,
{ {
module.glwe_public_key_generate(self, sk, source_xa, source_xe); 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> { 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) fn glwe_public_key_generate<R, S>(&self, res: &mut R, sk: &S, source_xa: &mut Source, source_xe: &mut Source)
where where
R: GLWEPublicKeyToMut, R: GLWEPublicKeyToMut + GetDistributionMut,
S: GLWESecretPreparedToRef<BE>; S: GLWESecretPreparedToRef<BE> + GetDistribution;
} }
impl<BE: Backend> GLWEPublicKeyGenerate<BE> for Module<BE> impl<BE: Backend> GLWEPublicKeyGenerate<BE> for Module<BE>
@@ -38,8 +38,9 @@ where
{ {
fn glwe_public_key_generate<R, S>(&self, res: &mut R, sk: &S, source_xa: &mut Source, source_xe: &mut Source) fn glwe_public_key_generate<R, S>(&self, res: &mut R, sk: &S, source_xa: &mut Source, source_xe: &mut Source)
where where
R: GLWEPublicKeyToMut, R: GLWEPublicKeyToMut + GetDistributionMut,
S: GLWESecretPreparedToRef<BE>, S: GLWESecretPreparedToRef<BE> + GetDistribution,
{
{ {
let res: &mut GLWEPublicKey<&mut [u8]> = &mut res.to_mut(); let res: &mut GLWEPublicKey<&mut [u8]> = &mut res.to_mut();
let sk: &GLWESecretPrepared<&[u8], BE> = &sk.to_ref(); let sk: &GLWESecretPrepared<&[u8], BE> = &sk.to_ref();
@@ -57,6 +58,7 @@ where
let mut tmp: GLWE<Vec<u8>> = GLWE::alloc_from_infos(self, 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()); 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(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> { impl<D: Data> LWEInfos for GGLWECompressed<D> {
fn n(&self) -> Degree { fn n(&self) -> Degree {
Degree(self.data.n() as u32) Degree(self.data.n() as u32)

View File

@@ -22,6 +22,26 @@ pub struct GGSWCompressed<D: Data> {
pub(crate) seed: Vec<[u8; 32]>, 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> { impl<D: Data> LWEInfos for GGSWCompressed<D> {
fn n(&self) -> Degree { fn n(&self) -> Degree {
Degree(self.data.n() as u32) Degree(self.data.n() as u32)

View File

@@ -19,6 +19,26 @@ pub struct GLWECompressed<D: Data> {
pub(crate) seed: [u8; 32], 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> { impl<D: Data> LWEInfos for GLWECompressed<D> {
fn base2k(&self) -> Base2K { fn base2k(&self) -> Base2K {
self.base2k self.base2k

View File

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

View File

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

View File

@@ -4,8 +4,9 @@ use poulpy_hal::{
}; };
use crate::{ use crate::{
GetDistribution, GetDistributionMut,
dist::Distribution, 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)] #[derive(PartialEq, Eq)]
@@ -16,13 +17,15 @@ pub struct GLWEPublicKeyPrepared<D: Data, B: Backend> {
pub(crate) dist: Distribution, pub(crate) dist: Distribution,
} }
pub trait SetDist { impl<D: DataRef, BE: Backend> GetDistribution for GLWEPublicKeyPrepared<D, BE> {
fn set_dist(&mut self, dist: Distribution); fn dist(&self) -> &Distribution {
&self.dist
}
} }
impl<D: Data, B: Backend> SetDist for GLWEPublicKeyPrepared<D, B> { impl<D: DataMut, BE: Backend> GetDistributionMut for GLWEPublicKeyPrepared<D, BE> {
fn set_dist(&mut self, dist: Distribution) { fn dist_mut(&mut self) -> &mut Distribution {
self.dist = dist &mut self.dist
} }
} }
@@ -122,8 +125,8 @@ where
{ {
fn prepare_glwe_public_key<R, O>(&self, res: &mut R, other: &O) fn prepare_glwe_public_key<R, O>(&self, res: &mut R, other: &O)
where where
R: GLWEPublicKeyPreparedToMut<B> + SetDist, R: GLWEPublicKeyPreparedToMut<B> + GetDistributionMut,
O: GLWEPublicKeyToRef + GetDist, O: GLWEPublicKeyToRef + GetDistribution,
{ {
{ {
let mut res: GLWEPublicKeyPrepared<&mut [u8], B> = res.to_mut(); 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> { impl<D: DataMut, B: Backend> GLWEPublicKeyPrepared<D, B> {
pub fn prepare<O, M>(&mut self, module: &M, other: &O) pub fn prepare<O, M>(&mut self, module: &M, other: &O)
where where
O: GLWEPublicKeyToRef + GetDist, O: GLWEPublicKeyToRef + GetDistribution,
M: GLWEPublicKeyPrepare<B>, M: GLWEPublicKeyPrepare<B>,
{ {
module.prepare_glwe_public_key(self, other); module.prepare_glwe_public_key(self, other);

View File

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

View File

@@ -14,7 +14,7 @@ backend_test_suite!(
tests = { tests = {
//GLWE Encryption //GLWE Encryption
glwe_encrypt_sk => crate::tests::test_suite::encryption::test_glwe_encrypt_sk, 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_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_encrypt_pk => crate::tests::test_suite::encryption::test_glwe_encrypt_pk,
// GLWE Keyswitch // GLWE Keyswitch

View File

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