mirror of
https://github.com/arnaucube/poulpy.git
synced 2026-02-10 05:06:44 +01:00
fixed encryption
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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,14 +94,16 @@ where
|
||||
source_xe: &mut Source,
|
||||
scratch: &mut Scratch<BE>,
|
||||
) where
|
||||
R: GGLWECompressedToMut,
|
||||
R: GGLWECompressedToMut + GGLWECompressedSeedMut,
|
||||
P: ScalarZnxToRef,
|
||||
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 pt: &ScalarZnx<&[u8]> = &pt.to_ref();
|
||||
|
||||
let sk = &sk.to_ref();
|
||||
let sk: &GLWESecretPrepared<&[u8], BE> = &sk.to_ref();
|
||||
|
||||
assert_eq!(
|
||||
res.rank_in(),
|
||||
@@ -144,15 +146,15 @@ where
|
||||
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| {
|
||||
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;
|
||||
seeds[col_i * dnum + d_i] = seed;
|
||||
|
||||
self.glwe_encrypt_sk_internal(
|
||||
res.base2k().into(),
|
||||
@@ -167,7 +169,10 @@ where
|
||||
SIGMA,
|
||||
scrach_1,
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
res.seed_mut().copy_from_slice(&seeds);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
@@ -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,9 +83,12 @@ where
|
||||
source_xe: &mut Source,
|
||||
scratch: &mut Scratch<BE>,
|
||||
) where
|
||||
R: GGSWCompressedToMut,
|
||||
R: GGSWCompressedToMut + GGSWCompressedSeedMut,
|
||||
P: ScalarZnxToRef,
|
||||
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 sk: &GLWESecretPrepared<&[u8], BE> = &sk.to_ref();
|
||||
@@ -109,8 +112,6 @@ where
|
||||
|
||||
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() {
|
||||
tmp_pt.data.zero();
|
||||
|
||||
@@ -123,7 +124,7 @@ where
|
||||
|
||||
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(),
|
||||
@@ -141,4 +142,7 @@ where
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
res.seed_mut().copy_from_slice(&seeds);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,9 +83,10 @@ 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);
|
||||
@@ -104,7 +105,8 @@ where
|
||||
SIGMA,
|
||||
scratch,
|
||||
);
|
||||
}
|
||||
|
||||
res.seed = seed_xa;
|
||||
res.seed_mut().copy_from_slice(&seed_xa);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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,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)
|
||||
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();
|
||||
@@ -57,6 +58,7 @@ where
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user