This commit is contained in:
Pro7ech
2025-10-13 12:14:11 +02:00
parent 662e533eac
commit cf377ff243
94 changed files with 1892 additions and 1235 deletions

View File

@@ -9,18 +9,19 @@ use crate::{
encryption::compressed::gglwe_ksk::GGLWEKeyCompressedEncryptSk,
layouts::{
GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos,
compressed::{GGLWEAutomorphismKeyCompressed, GGLWEAutomorphismKeyCompressedToMut, GGLWEKeyCompressed},
compressed::{AutomorphismKeyCompressed, AutomorphismKeyCompressedToMut, GLWESwitchingKeyCompressed},
},
};
impl GGLWEAutomorphismKeyCompressed<Vec<u8>> {
impl AutomorphismKeyCompressed<Vec<u8>> {
pub fn encrypt_sk_scratch_space<B: Backend, A>(module: &Module<B>, infos: &A) -> usize
where
A: GGLWEInfos,
Module<B>: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes + SvpPPolAllocBytes,
{
assert_eq!(module.n() as u32, infos.n());
GGLWEKeyCompressed::encrypt_sk_scratch_space(module, infos) + GLWESecret::alloc_bytes_with(infos.n(), infos.rank_out())
GLWESwitchingKeyCompressed::encrypt_sk_scratch_space(module, infos)
+ GLWESecret::alloc_bytes_with(infos.n(), infos.rank_out())
}
}
@@ -34,7 +35,7 @@ pub trait GGLWEAutomorphismKeyCompressedEncryptSk<B: Backend> {
source_xe: &mut Source,
scratch: &mut Scratch<B>,
) where
R: GGLWEAutomorphismKeyCompressedToMut,
R: AutomorphismKeyCompressedToMut,
S: GLWESecretToRef;
}
@@ -53,10 +54,10 @@ where
source_xe: &mut Source,
scratch: &mut Scratch<B>,
) where
R: GGLWEAutomorphismKeyCompressedToMut,
R: AutomorphismKeyCompressedToMut,
S: GLWESecretToRef,
{
let res: &mut GGLWEAutomorphismKeyCompressed<&mut [u8]> = &mut res.to_mut();
let res: &mut AutomorphismKeyCompressed<&mut [u8]> = &mut res.to_mut();
let sk: &GLWESecret<&[u8]> = &sk.to_ref();
#[cfg(debug_assertions)]
@@ -65,10 +66,10 @@ where
assert_eq!(res.rank_out(), res.rank_in());
assert_eq!(sk.rank(), res.rank_out());
assert!(
scratch.available() >= GGLWEAutomorphismKeyCompressed::encrypt_sk_scratch_space(self, res),
scratch.available() >= AutomorphismKeyCompressed::encrypt_sk_scratch_space(self, res),
"scratch.available(): {} < AutomorphismKey::encrypt_sk_scratch_space: {}",
scratch.available(),
GGLWEAutomorphismKeyCompressed::encrypt_sk_scratch_space(self, res)
AutomorphismKeyCompressed::encrypt_sk_scratch_space(self, res)
)
}
@@ -92,7 +93,7 @@ where
}
}
impl<DataSelf: DataMut> GGLWEAutomorphismKeyCompressed<DataSelf> {
impl<DataSelf: DataMut> AutomorphismKeyCompressed<DataSelf> {
#[allow(clippy::too_many_arguments)]
pub fn encrypt_sk<DataSk: DataRef, B: Backend>(
&mut self,

View File

@@ -11,7 +11,7 @@ use crate::{
TakeGLWEPt,
encryption::{SIGMA, glwe_ct::GLWEEncryptSkInternal},
layouts::{
GGLWECiphertext, GGLWEInfos, LWEInfos,
GGLWE, GGLWEInfos, LWEInfos,
compressed::{GGLWECiphertextCompressed, GGLWECiphertextCompressedToMut},
prepared::{GLWESecretPrepared, GLWESecretPreparedToRef},
},
@@ -40,7 +40,7 @@ impl GGLWECiphertextCompressed<Vec<u8>> {
A: GGLWEInfos,
Module<B>: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes,
{
GGLWECiphertext::encrypt_sk_scratch_space(module, infos)
GGLWE::encrypt_sk_scratch_space(module, infos)
}
}

View File

@@ -11,25 +11,25 @@ use crate::{
TakeGLWESecretPrepared,
encryption::compressed::gglwe_ct::GGLWECompressedEncryptSk,
layouts::{
Degree, GGLWECiphertext, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos,
compressed::{GGLWEKeyCompressed, GGLWEKeyCompressedToMut},
Degree, GGLWE, GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos,
compressed::{GLWESwitchingKeyCompressed, GLWESwitchingKeyCompressedToMut},
prepared::GLWESecretPrepared,
},
};
impl GGLWEKeyCompressed<Vec<u8>> {
impl GLWESwitchingKeyCompressed<Vec<u8>> {
pub fn encrypt_sk_scratch_space<B: Backend, A>(module: &Module<B>, infos: &A) -> usize
where
A: GGLWEInfos,
Module<B>: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes + SvpPPolAllocBytes,
{
(GGLWECiphertext::encrypt_sk_scratch_space(module, infos) | ScalarZnx::alloc_bytes(module.n(), 1))
(GGLWE::encrypt_sk_scratch_space(module, infos) | ScalarZnx::alloc_bytes(module.n(), 1))
+ ScalarZnx::alloc_bytes(module.n(), infos.rank_in().into())
+ GLWESecretPrepared::alloc_bytes_with(module, infos.rank_out())
}
}
impl<DataSelf: DataMut> GGLWEKeyCompressed<DataSelf> {
impl<DataSelf: DataMut> GLWESwitchingKeyCompressed<DataSelf> {
#[allow(clippy::too_many_arguments)]
pub fn encrypt_sk<DataSkIn: DataRef, DataSkOut: DataRef, B: Backend>(
&mut self,
@@ -56,7 +56,7 @@ pub trait GGLWEKeyCompressedEncryptSk<B: Backend> {
source_xe: &mut Source,
scratch: &mut Scratch<B>,
) where
R: GGLWEKeyCompressedToMut,
R: GLWESwitchingKeyCompressedToMut,
SI: GLWESecretToRef,
SO: GLWESecretToRef;
}
@@ -80,25 +80,25 @@ where
source_xe: &mut Source,
scratch: &mut Scratch<B>,
) where
R: GGLWEKeyCompressedToMut,
R: GLWESwitchingKeyCompressedToMut,
SI: GLWESecretToRef,
SO: GLWESecretToRef,
{
let res: &mut GGLWEKeyCompressed<&mut [u8]> = &mut res.to_mut();
let res: &mut GLWESwitchingKeyCompressed<&mut [u8]> = &mut res.to_mut();
let sk_in: &GLWESecret<&[u8]> = &sk_in.to_ref();
let sk_out: &GLWESecret<&[u8]> = &sk_out.to_ref();
#[cfg(debug_assertions)]
{
use crate::layouts::GGLWESwitchingKey;
use crate::layouts::GLWESwitchingKey;
assert!(sk_in.n().0 <= self.n() as u32);
assert!(sk_out.n().0 <= self.n() as u32);
assert!(
scratch.available() >= GGLWESwitchingKey::encrypt_sk_scratch_space(self, res),
scratch.available() >= GLWESwitchingKey::encrypt_sk_scratch_space(self, res),
"scratch.available()={} < GLWESwitchingKey::encrypt_sk_scratch_space={}",
scratch.available(),
GGLWESwitchingKey::encrypt_sk_scratch_space(self, res)
GLWESwitchingKey::encrypt_sk_scratch_space(self, res)
)
}

View File

@@ -11,20 +11,19 @@ use crate::{
TakeGLWESecret, TakeGLWESecretPrepared,
encryption::compressed::gglwe_ksk::GGLWEKeyCompressedEncryptSk,
layouts::{
GGLWEInfos, GGLWETensorKey, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, Rank,
compressed::{GGLWETensorKeyCompressed, GGLWETensorKeyCompressedToMut},
prepared::Prepare,
GGLWEInfos, GLWEInfos, GLWESecret, GLWESecretToRef, LWEInfos, Rank, TensorKey,
compressed::{TensorKeyCompressed, TensorKeyCompressedToMut},
},
};
impl GGLWETensorKeyCompressed<Vec<u8>> {
impl TensorKeyCompressed<Vec<u8>> {
pub fn encrypt_sk_scratch_space<B: Backend, A>(module: &Module<B>, infos: &A) -> usize
where
A: GGLWEInfos,
Module<B>:
SvpPPolAllocBytes + VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes + VecZnxNormalizeTmpBytes + VecZnxBigAllocBytes,
{
GGLWETensorKey::encrypt_sk_scratch_space(module, infos)
TensorKey::encrypt_sk_scratch_space(module, infos)
}
}
@@ -37,7 +36,7 @@ pub trait GGLWETensorKeyCompressedEncryptSk<B: Backend> {
source_xe: &mut Source,
scratch: &mut Scratch<B>,
) where
R: GGLWETensorKeyCompressedToMut,
R: TensorKeyCompressedToMut,
S: GLWESecretToRef;
}
@@ -59,10 +58,10 @@ where
source_xe: &mut Source,
scratch: &mut Scratch<B>,
) where
R: GGLWETensorKeyCompressedToMut,
R: TensorKeyCompressedToMut,
S: GLWESecretToRef,
{
let res: &mut GGLWETensorKeyCompressed<&mut [u8]> = &mut res.to_mut();
let res: &mut TensorKeyCompressed<&mut [u8]> = &mut res.to_mut();
let sk: &GLWESecret<&[u8]> = &sk.to_ref();
#[cfg(debug_assertions)]
@@ -119,7 +118,7 @@ where
}
}
impl<DataSelf: DataMut> GGLWETensorKeyCompressed<DataSelf> {
impl<DataSelf: DataMut> TensorKeyCompressed<DataSelf> {
pub fn encrypt_sk<DataSk: DataRef, B: Backend>(
&mut self,
module: &Module<B>,

View File

@@ -8,7 +8,7 @@ use crate::{
TakeGLWEPt,
encryption::{SIGMA, glwe_ct::GLWEEncryptSkInternal},
layouts::{
GGSWCiphertext, GGSWInfos, GLWEInfos, LWEInfos,
GGSW, GGSWInfos, GLWEInfos, LWEInfos,
compressed::{GGSWCiphertextCompressed, GGSWCiphertextCompressedToMut},
prepared::{GLWESecretPrepared, GLWESecretPreparedToRef},
},
@@ -20,7 +20,7 @@ impl GGSWCiphertextCompressed<Vec<u8>> {
A: GGSWInfos,
Module<B>: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes,
{
GGSWCiphertext::encrypt_sk_scratch_space(module, infos)
GGSW::encrypt_sk_scratch_space(module, infos)
}
}

View File

@@ -1,17 +1,15 @@
use poulpy_hal::{
api::{
ScratchAvailable, SvpApplyDftToDftInplace, TakeVecZnx, TakeVecZnxDft, VecZnxAddInplace, VecZnxAddNormal,
VecZnxBigNormalize, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize,
VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace,
},
api::{VecZnxDftAllocBytes, VecZnxNormalizeTmpBytes},
layouts::{Backend, DataMut, DataRef, Module, Scratch},
source::Source,
};
use crate::{
encryption::{SIGMA, glwe_ct::glwe_encrypt_sk_internal},
encryption::{SIGMA, glwe_ct::GLWEEncryptSkInternal},
layouts::{
GLWECiphertext, GLWEInfos, GLWEPlaintext, LWEInfos, compressed::GLWECiphertextCompressed, prepared::GLWESecretPrepared,
GLWECiphertext, GLWEInfos, GLWEPlaintext, GLWEPlaintextToRef, LWEInfos,
compressed::{GLWECiphertextCompressed, GLWECiphertextCompressedToMut},
prepared::{GLWESecretPrepared, GLWESecretPreparedToRef},
},
};
@@ -25,6 +23,60 @@ impl GLWECiphertextCompressed<Vec<u8>> {
}
}
pub trait GLWECompressedEncryptSk<B: Backend> {
fn glwe_compressed_encrypt_sk<R, P, S>(
&self,
res: &mut R,
pt: &P,
sk: &S,
seed_xa: [u8; 32],
source_xe: &mut Source,
scratch: &mut Scratch<B>,
) where
R: GLWECiphertextCompressedToMut,
P: GLWEPlaintextToRef,
S: GLWESecretPreparedToRef<B>;
}
impl<B: Backend> GLWECompressedEncryptSk<B> for Module<B>
where
Module<B>: GLWEEncryptSkInternal<B>,
{
fn glwe_compressed_encrypt_sk<R, P, S>(
&self,
res: &mut R,
pt: &P,
sk: &S,
seed_xa: [u8; 32],
source_xe: &mut Source,
scratch: &mut Scratch<B>,
) where
R: GLWECiphertextCompressedToMut,
P: GLWEPlaintextToRef,
S: GLWESecretPreparedToRef<B>,
{
let res: &mut GLWECiphertextCompressed<&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,
);
res.seed = seed_xa;
}
}
impl<D: DataMut> GLWECiphertextCompressed<D> {
#[allow(clippy::too_many_arguments)]
pub fn encrypt_sk<DataPt: DataRef, DataSk: DataRef, B: Backend>(
@@ -36,65 +88,8 @@ impl<D: DataMut> GLWECiphertextCompressed<D> {
source_xe: &mut Source,
scratch: &mut Scratch<B>,
) where
Module<B>: VecZnxDftAllocBytes
+ VecZnxBigNormalize<B>
+ VecZnxDftApply<B>
+ SvpApplyDftToDftInplace<B>
+ VecZnxIdftApplyConsume<B>
+ VecZnxNormalizeTmpBytes
+ VecZnxFillUniform
+ VecZnxSubInplace
+ VecZnxAddInplace
+ VecZnxNormalizeInplace<B>
+ VecZnxAddNormal
+ VecZnxNormalize<B>
+ VecZnxSub,
Scratch<B>: TakeVecZnxDft<B> + ScratchAvailable + TakeVecZnx,
Module<B>: GLWECompressedEncryptSk<B>,
{
self.encrypt_sk_internal(module, Some((pt, 0)), sk, seed_xa, source_xe, scratch);
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn encrypt_sk_internal<DataPt: DataRef, DataSk: DataRef, B: Backend>(
&mut self,
module: &Module<B>,
pt: Option<(&GLWEPlaintext<DataPt>, usize)>,
sk: &GLWESecretPrepared<DataSk, B>,
seed_xa: [u8; 32],
source_xe: &mut Source,
scratch: &mut Scratch<B>,
) where
Module<B>: VecZnxDftAllocBytes
+ VecZnxBigNormalize<B>
+ VecZnxDftApply<B>
+ SvpApplyDftToDftInplace<B>
+ VecZnxIdftApplyConsume<B>
+ VecZnxNormalizeTmpBytes
+ VecZnxFillUniform
+ VecZnxSubInplace
+ VecZnxAddInplace
+ VecZnxNormalizeInplace<B>
+ VecZnxAddNormal
+ VecZnxNormalize<B>
+ VecZnxSub,
Scratch<B>: TakeVecZnxDft<B> + ScratchAvailable + TakeVecZnx,
{
let mut source_xa = Source::new(seed_xa);
let cols: usize = (self.rank() + 1).into();
glwe_encrypt_sk_internal(
module,
self.base2k().into(),
self.k().into(),
&mut self.data,
cols,
true,
pt,
sk,
&mut source_xa,
source_xe,
SIGMA,
scratch,
);
self.seed = seed_xa;
module.glwe_compressed_encrypt_sk(self, pt, sk, seed_xa, source_xe, scratch);
}
}