conversions tests

This commit is contained in:
Pro7ech
2025-10-20 17:20:34 +02:00
parent 1e2478852e
commit d4c936e04c
9 changed files with 99 additions and 125 deletions

View File

@@ -112,14 +112,13 @@ impl<D: DataMut> LWE<D> {
module.lwe_sample_extract(self, a);
}
pub fn from_glwe<R, A, K, M, BE: Backend>(&self, module: &M, res: &mut R, a: &A, key: &K, scratch: &mut Scratch<BE>)
pub fn from_glwe<A, K, M, BE: Backend>(&mut self, module: &M, a: &A, key: &K, scratch: &mut Scratch<BE>)
where
R: LWEToMut,
A: GLWEToRef,
K: GGLWEPreparedToRef<BE> + GGLWEInfos,
M: LWEFromGLWE<BE>,
Scratch<BE>: ScratchTakeCore<BE>,
{
module.lwe_from_glwe(res, a, key, scratch);
module.lwe_from_glwe(self, a, key, scratch);
}
}

View File

@@ -17,7 +17,7 @@ impl GLWEToLWESwitchingKey<Vec<u8>> {
pub fn encrypt_sk_tmp_bytes<M, A, BE: Backend>(module: &M, infos: &A) -> usize
where
A: GGLWEInfos,
M: GLWEToLWESwitchingKeyEncrypt<BE>,
M: GLWEToLWESwitchingKeyEncryptSk<BE>,
{
module.glwe_to_lwe_switching_key_encrypt_sk_tmp_bytes(infos)
}
@@ -33,7 +33,7 @@ impl<D: DataMut> GLWEToLWESwitchingKey<D> {
source_xe: &mut Source,
scratch: &mut Scratch<BE>,
) where
M: GLWEToLWESwitchingKeyEncrypt<BE>,
M: GLWEToLWESwitchingKeyEncryptSk<BE>,
S1: LWESecretToRef,
S2: GLWESecretToRef,
Scratch<BE>: ScratchTakeCore<BE>,
@@ -42,7 +42,7 @@ impl<D: DataMut> GLWEToLWESwitchingKey<D> {
}
}
pub trait GLWEToLWESwitchingKeyEncrypt<BE: Backend> {
pub trait GLWEToLWESwitchingKeyEncryptSk<BE: Backend> {
fn glwe_to_lwe_switching_key_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
where
A: GGLWEInfos;
@@ -61,7 +61,7 @@ pub trait GLWEToLWESwitchingKeyEncrypt<BE: Backend> {
R: GGLWEToMut;
}
impl<BE: Backend> GLWEToLWESwitchingKeyEncrypt<BE> for Module<BE>
impl<BE: Backend> GLWEToLWESwitchingKeyEncryptSk<BE> for Module<BE>
where
Self: ModuleN
+ GGLWEEncryptSk<BE>

View File

@@ -16,7 +16,7 @@ impl LWEToGLWESwitchingKey<Vec<u8>> {
pub fn encrypt_sk_tmp_bytes<M, A, BE: Backend>(module: &M, infos: &A) -> usize
where
A: GGLWEInfos,
M: LWEToGLWESwitchingKeyEncrypt<BE>,
M: LWEToGLWESwitchingKeyEncryptSk<BE>,
{
module.lwe_to_glwe_switching_key_encrypt_sk_tmp_bytes(infos)
}
@@ -34,14 +34,14 @@ impl<D: DataMut> LWEToGLWESwitchingKey<D> {
) where
S1: LWESecretToRef,
S2: GLWESecretPreparedToRef<BE>,
M: LWEToGLWESwitchingKeyEncrypt<BE>,
M: LWEToGLWESwitchingKeyEncryptSk<BE>,
Scratch<BE>: ScratchTakeCore<BE>,
{
module.lwe_to_glwe_switching_key_encrypt_sk(self, sk_lwe, sk_glwe, source_xa, source_xe, scratch);
}
}
pub trait LWEToGLWESwitchingKeyEncrypt<BE: Backend> {
pub trait LWEToGLWESwitchingKeyEncryptSk<BE: Backend> {
fn lwe_to_glwe_switching_key_encrypt_sk_tmp_bytes<A>(&self, infos: &A) -> usize
where
A: GGLWEInfos;
@@ -60,7 +60,7 @@ pub trait LWEToGLWESwitchingKeyEncrypt<BE: Backend> {
R: GGLWEToMut;
}
impl<BE: Backend> LWEToGLWESwitchingKeyEncrypt<BE> for Module<BE>
impl<BE: Backend> LWEToGLWESwitchingKeyEncryptSk<BE> for Module<BE>
where
Self: ModuleN
+ GGLWEEncryptSk<BE>

View File

@@ -4,7 +4,7 @@ use poulpy_hal::{
};
use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWEInfos, GLWESwitchingKey,
Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyDegrees,
GLWESwitchingKeyDegreesMut, LWEInfos, Rank, TorusPrecision,
};
@@ -217,3 +217,13 @@ impl<D: DataMut> GLWESwitchingKeyDegreesMut for GLWEToLWESwitchingKey<D> {
&mut self.0.output_degree
}
}
impl<D: DataRef> GLWESwitchingKeyDegrees for GLWEToLWESwitchingKey<D> {
fn input_degree(&self) -> &Degree {
&self.0.input_degree
}
fn output_degree(&self) -> &Degree {
&self.0.output_degree
}
}

View File

@@ -6,7 +6,7 @@ use poulpy_hal::{
};
use crate::layouts::{
Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWEInfos, GLWESwitchingKey,
Base2K, Degree, Dnum, Dsize, GGLWE, GGLWEInfos, GGLWEToMut, GGLWEToRef, GLWEInfos, GLWESwitchingKey, GLWESwitchingKeyDegrees,
GLWESwitchingKeyDegreesMut, LWEInfos, Rank, TorusPrecision,
};
@@ -217,3 +217,13 @@ impl<D: DataMut> GLWESwitchingKeyDegreesMut for LWEToGLWESwitchingKey<D> {
&mut self.0.output_degree
}
}
impl<D: DataRef> GLWESwitchingKeyDegrees for LWEToGLWESwitchingKey<D> {
fn input_degree(&self) -> &Degree {
&self.0.input_degree
}
fn output_degree(&self) -> &Degree {
&self.0.output_degree
}
}

View File

@@ -206,3 +206,13 @@ impl<D: DataMut, B: Backend> GLWESwitchingKeyDegreesMut for GLWEToLWESwitchingKe
&mut self.0.output_degree
}
}
impl<D: DataRef, B: Backend> GLWESwitchingKeyDegrees for GLWEToLWESwitchingKeyPrepared<D, B> {
fn input_degree(&self) -> &Degree {
&self.0.input_degree
}
fn output_degree(&self) -> &Degree {
&self.0.output_degree
}
}

View File

@@ -59,8 +59,8 @@ ggsw_automorphism => crate::tests::test_suite::automorphism::test_ggsw_automorph
ggsw_automorphism_inplace => crate::tests::test_suite::automorphism::test_ggsw_automorphism_inplace,
// LWE
lwe_keyswitch => crate::tests::test_suite::keyswitch::test_lwe_keyswitch,
// glwe_to_lwe => crate::tests::test_suite::test_glwe_to_lwe,
// lwe_to_glwe => crate::tests::test_suite::test_lwe_to_glwe,
glwe_to_lwe => crate::tests::test_suite::test_glwe_to_lwe,
lwe_to_glwe => crate::tests::test_suite::test_lwe_to_glwe,
}
);
// #[cfg(test)]

View File

@@ -1,68 +1,33 @@
use poulpy_hal::{
api::{
ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare,
VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxBigAddInplace,
VecZnxBigAddSmallInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxDftApply,
VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace,
VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd,
VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, ZnAddNormal, ZnFillUniform, ZnNormalizeInplace,
},
layouts::{Backend, Module, ScratchOwned, ZnxView},
oep::{
ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeScalarZnxImpl, TakeSvpPPolImpl,
TakeVecZnxBigImpl, TakeVecZnxDftImpl, TakeVecZnxImpl,
},
api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow},
layouts::{Backend, Module, Scratch, ScratchOwned, ZnxView},
source::Source,
};
use crate::layouts::{
Base2K, Degree, Dnum, GLWE, GLWELayout, GLWEPlaintext, GLWESecret, GLWEToLWEKeyLayout, GLWEToLWESwitchingKey, LWE,
LWECiphertextLayout, LWEPlaintext, LWESecret, LWEToGLWESwitchingKey, LWEToGLWESwitchingKeyLayout, Rank, TorusPrecision,
prepared::{GLWESecretPrepared, GLWEToLWESwitchingKeyPrepared, LWEToGLWESwitchingKeyPrepared, PrepareAlloc},
use crate::{
GLWEDecrypt, GLWEEncryptSk, GLWEFromLWE, GLWEToLWESwitchingKeyEncryptSk, LWEDecrypt, LWEEncryptSk,
LWEToGLWESwitchingKeyEncryptSk, ScratchTakeCore,
layouts::{
Base2K, Degree, Dnum, GLWE, GLWELayout, GLWEPlaintext, GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc,
GLWEToLWEKeyLayout, GLWEToLWESwitchingKey, GLWEToLWESwitchingKeyPrepare, GLWEToLWESwitchingKeyPreparedAlloc, LWE,
LWELayout, LWEPlaintext, LWESecret, LWEToGLWESwitchingKey, LWEToGLWESwitchingKeyLayout, LWEToGLWESwitchingKeyPrepare,
LWEToGLWESwitchingKeyPreparedAlloc, Rank, TorusPrecision,
prepared::{GLWESecretPrepared, GLWEToLWESwitchingKeyPrepared, LWEToGLWESwitchingKeyPrepared},
},
};
pub fn test_lwe_to_glwe<B>(module: &Module<B>)
pub fn test_lwe_to_glwe<BE: Backend>(module: &Module<BE>)
where
Module<B>: VecZnxDftBytesOf
+ VecZnxBigNormalize<B>
+ VecZnxDftApply<B>
+ SvpApplyDftToDftInplace<B>
+ VecZnxIdftApplyConsume<B>
+ VecZnxFillUniform
+ VecZnxSubInplace
+ VecZnxAddInplace
+ VecZnxNormalizeInplace<B>
+ VecZnxAddNormal
+ VecZnxNormalize<B>
+ VecZnxSub
+ SvpPrepare<B>
+ SvpPPolBytesOf
+ SvpPPolAlloc<B>
+ VecZnxBigBytesOf
+ VecZnxBigAddInplace<B>
+ VecZnxBigAddSmallInplace<B>
+ VecZnxNormalizeTmpBytes
+ VecZnxAddScalarInplace
+ VmpPMatAlloc<B>
+ VmpPrepare<B>
+ VmpApplyDftToDftTmpBytes
+ VmpApplyDftToDft<B>
+ VmpApplyDftToDftAdd<B>
+ VecZnxBigNormalizeTmpBytes
+ VecZnxSwitchRing
+ VecZnxAutomorphismInplace<B>
+ ZnNormalizeInplace<B>
+ ZnFillUniform
+ ZnAddNormal,
B: Backend
+ TakeVecZnxDftImpl<B>
+ TakeVecZnxBigImpl<B>
+ TakeSvpPPolImpl<B>
+ ScratchOwnedAllocImpl<B>
+ ScratchOwnedBorrowImpl<B>
+ ScratchAvailableImpl<B>
+ TakeScalarZnxImpl<B>
+ TakeVecZnxImpl<B>,
Module<BE>: GLWEFromLWE<BE>
+ LWEToGLWESwitchingKeyEncryptSk<BE>
+ GLWEDecrypt<BE>
+ GLWESecretPrepare<BE>
+ GLWESecretPreparedAlloc<BE>
+ LWEEncryptSk<BE>
+ LWEToGLWESwitchingKeyPreparedAlloc<BE>
+ LWEToGLWESwitchingKeyPrepare<BE>,
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
{
let n_glwe: Degree = Degree(module.n() as u32);
let n_lwe: Degree = Degree(22);
@@ -89,13 +54,13 @@ where
rank,
};
let lwe_infos: LWECiphertextLayout = LWECiphertextLayout {
let lwe_infos: LWELayout = LWELayout {
n: n_lwe,
base2k: Base2K(17),
k: TorusPrecision(34),
};
let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(
let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(
LWEToGLWESwitchingKey::encrypt_sk_tmp_bytes(module, &lwe_to_glwe_infos)
| GLWE::from_lwe_tmp_bytes(module, &glwe_infos, &lwe_infos, &lwe_to_glwe_infos)
| GLWE::decrypt_tmp_bytes(module, &glwe_infos),
@@ -104,7 +69,8 @@ where
let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_infos);
sk_glwe.fill_ternary_prob(0.5, &mut source_xs);
let sk_glwe_prepared: GLWESecretPrepared<Vec<u8>, B> = sk_glwe.prepare_alloc(module, scratch.borrow());
let mut sk_glwe_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc_from_infos(module, &sk_glwe);
sk_glwe_prepared.prepare(module, &sk_glwe);
let mut sk_lwe: LWESecret<Vec<u8>> = LWESecret::alloc(n_lwe);
sk_lwe.fill_ternary_prob(0.5, &mut source_xs);
@@ -122,7 +88,7 @@ where
ksk.encrypt_sk(
module,
&sk_lwe,
&sk_glwe,
&sk_glwe_prepared,
&mut source_xa,
&mut source_xe,
scratch.borrow(),
@@ -130,7 +96,9 @@ where
let mut glwe_ct: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_infos);
let ksk_prepared: LWEToGLWESwitchingKeyPrepared<Vec<u8>, B> = ksk.prepare_alloc(module, scratch.borrow());
let mut ksk_prepared: LWEToGLWESwitchingKeyPrepared<Vec<u8>, BE> =
LWEToGLWESwitchingKeyPrepared::alloc_from_infos(module, &ksk);
ksk_prepared.prepare(module, &ksk, scratch.borrow());
glwe_ct.from_lwe(module, &lwe_ct, &ksk_prepared, scratch.borrow());
@@ -140,46 +108,20 @@ where
assert_eq!(glwe_pt.data.at(0, 0)[0], lwe_pt.data.at(0, 0)[0]);
}
pub fn test_glwe_to_lwe<B>(module: &Module<B>)
pub fn test_glwe_to_lwe<BE: Backend>(module: &Module<BE>)
where
Module<B>: VecZnxDftBytesOf
+ VecZnxBigNormalize<B>
+ VecZnxDftApply<B>
+ SvpApplyDftToDftInplace<B>
+ VecZnxIdftApplyConsume<B>
+ VecZnxFillUniform
+ VecZnxSubInplace
+ VecZnxAddInplace
+ VecZnxNormalizeInplace<B>
+ VecZnxAddNormal
+ VecZnxNormalize<B>
+ VecZnxSub
+ SvpPrepare<B>
+ SvpPPolBytesOf
+ SvpPPolAlloc<B>
+ VecZnxBigBytesOf
+ VecZnxBigAddInplace<B>
+ VecZnxBigAddSmallInplace<B>
+ VecZnxNormalizeTmpBytes
+ VecZnxAddScalarInplace
+ VmpPMatAlloc<B>
+ VmpPrepare<B>
+ VmpApplyDftToDftTmpBytes
+ VmpApplyDftToDft<B>
+ VmpApplyDftToDftAdd<B>
+ VecZnxBigNormalizeTmpBytes
+ VecZnxSwitchRing
+ VecZnxAutomorphismInplace<B>
+ ZnNormalizeInplace<B>,
B: Backend
+ TakeVecZnxDftImpl<B>
+ TakeVecZnxBigImpl<B>
+ TakeSvpPPolImpl<B>
+ ScratchOwnedAllocImpl<B>
+ ScratchOwnedBorrowImpl<B>
+ ScratchAvailableImpl<B>
+ TakeScalarZnxImpl<B>
+ TakeVecZnxImpl<B>,
Module<BE>: GLWEFromLWE<BE>
+ GLWEToLWESwitchingKeyEncryptSk<BE>
+ GLWEEncryptSk<BE>
+ LWEDecrypt<BE>
+ GLWEDecrypt<BE>
+ GLWESecretPrepare<BE>
+ GLWESecretPreparedAlloc<BE>
+ GLWEToLWESwitchingKeyEncryptSk<BE>
+ GLWEToLWESwitchingKeyPrepare<BE>
+ GLWEToLWESwitchingKeyPreparedAlloc<BE>,
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
{
let n_glwe: Degree = Degree(module.n() as u32);
let n_lwe: Degree = Degree(22);
@@ -202,7 +144,7 @@ where
rank,
};
let lwe_infos: LWECiphertextLayout = LWECiphertextLayout {
let lwe_infos: LWELayout = LWELayout {
n: n_lwe,
base2k: Base2K(17),
k: TorusPrecision(34),
@@ -212,7 +154,7 @@ where
let mut source_xa: Source = Source::new([0u8; 32]);
let mut source_xe: Source = Source::new([0u8; 32]);
let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(
let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(
GLWEToLWESwitchingKey::encrypt_sk_tmp_bytes(module, &glwe_to_lwe_infos)
| LWE::from_glwe_tmp_bytes(module, &lwe_infos, &glwe_infos, &glwe_to_lwe_infos)
| GLWE::decrypt_tmp_bytes(module, &glwe_infos),
@@ -221,7 +163,8 @@ where
let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_infos);
sk_glwe.fill_ternary_prob(0.5, &mut source_xs);
let sk_glwe_prepared: GLWESecretPrepared<Vec<u8>, B> = sk_glwe.prepare_alloc(module, scratch.borrow());
let mut sk_glwe_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc_from_infos(module, &sk_glwe);
sk_glwe_prepared.prepare(module, &sk_glwe);
let mut sk_lwe: LWESecret<Vec<u8>> = LWESecret::alloc(n_lwe);
sk_lwe.fill_ternary_prob(0.5, &mut source_xs);
@@ -230,7 +173,7 @@ where
let mut glwe_pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_infos);
glwe_pt.encode_coeff_i64(data, k_lwe_pt, 0);
let mut glwe_ct = GLWE::alloc_from_infos(&glwe_infos);
let mut glwe_ct: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_infos);
glwe_ct.encrypt_sk(
module,
&glwe_pt,
@@ -240,7 +183,7 @@ where
scratch.borrow(),
);
let mut ksk: GLWEToLWESwitchingKey<Vec<u8>> = GLWEToLWESwitchingKey::alloc(&glwe_to_lwe_infos);
let mut ksk: GLWEToLWESwitchingKey<Vec<u8>> = GLWEToLWESwitchingKey::alloc_from_infos(&glwe_to_lwe_infos);
ksk.encrypt_sk(
module,
@@ -253,7 +196,9 @@ where
let mut lwe_ct: LWE<Vec<u8>> = LWE::alloc_from_infos(&lwe_infos);
let ksk_prepared: GLWEToLWESwitchingKeyPrepared<Vec<u8>, B> = ksk.prepare_alloc(module, scratch.borrow());
let mut ksk_prepared: GLWEToLWESwitchingKeyPrepared<Vec<u8>, BE> =
GLWEToLWESwitchingKeyPrepared::alloc_from_infos(module, &ksk);
ksk_prepared.prepare(module, &ksk, scratch.borrow());
lwe_ct.from_glwe(module, &glwe_ct, &ksk_prepared, scratch.borrow());

View File

@@ -3,10 +3,10 @@ pub mod encryption;
pub mod external_product;
pub mod keyswitch;
// mod conversion;
mod conversion;
// mod packing;
// mod trace;
// pub use conversion::*;
pub use conversion::*;
// pub use packing::*;
// pub use trace::*;