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

@@ -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);
}