wip test glwe encryption

This commit is contained in:
Jean-Philippe Bossuat
2025-10-18 13:12:01 +02:00
parent a84ad06086
commit ca3997a4c9
11 changed files with 82 additions and 110 deletions

View File

@@ -15,7 +15,7 @@ impl GLWE<Vec<u8>> {
pub fn decrypt_tmp_bytes<A, M, BE: Backend>(module: &M, a_infos: &A) -> usize pub fn decrypt_tmp_bytes<A, M, BE: Backend>(module: &M, a_infos: &A) -> usize
where where
A: GLWEInfos, A: GLWEInfos,
M: GLWEDecryption<BE>, M: GLWEDecrypt<BE>,
{ {
module.glwe_decrypt_tmp_bytes(a_infos) module.glwe_decrypt_tmp_bytes(a_infos)
} }
@@ -26,14 +26,14 @@ impl<DataSelf: DataMut> GLWE<DataSelf> {
where where
P: GLWEPlaintextToMut, P: GLWEPlaintextToMut,
S: GLWESecretPreparedToRef<BE>, S: GLWESecretPreparedToRef<BE>,
M: GLWEDecryption<BE>, M: GLWEDecrypt<BE>,
Scratch<BE>: ScratchTakeBasic, Scratch<BE>: ScratchTakeBasic,
{ {
module.glwe_decrypt(self, pt, sk, scratch); module.glwe_decrypt(self, pt, sk, scratch);
} }
} }
pub trait GLWEDecryption<BE: Backend> pub trait GLWEDecrypt<BE: Backend>
where where
Self: Sized Self: Sized
+ ModuleN + ModuleN
@@ -110,7 +110,7 @@ where
} }
} }
impl<BE: Backend> GLWEDecryption<BE> for Module<BE> where impl<BE: Backend> GLWEDecrypt<BE> for Module<BE> where
Self: ModuleN Self: ModuleN
+ VecZnxDftBytesOf + VecZnxDftBytesOf
+ VecZnxNormalizeTmpBytes + VecZnxNormalizeTmpBytes

View File

@@ -4,3 +4,10 @@ mod gglwe_ksk;
mod gglwe_tsk; mod gglwe_tsk;
mod ggsw_ct; mod ggsw_ct;
mod glwe_ct; mod glwe_ct;
pub use gglwe_atk::*;
pub use gglwe_ct::*;
pub use gglwe_ksk::*;
pub use gglwe_tsk::*;
pub use ggsw_ct::*;
pub use glwe_ct::*;

View File

@@ -11,5 +11,18 @@ mod lwe_ct;
mod lwe_ksk; mod lwe_ksk;
mod lwe_to_glwe_ksk; mod lwe_to_glwe_ksk;
pub use compressed::*;
pub use gglwe_atk::*;
pub use gglwe_ct::*;
pub use gglwe_ksk::*;
pub use gglwe_tsk::*;
pub use ggsw_ct::*;
pub use glwe_ct::*;
pub use glwe_pk::*;
pub use glwe_to_lwe_ksk::*;
pub use lwe_ct::*;
pub use lwe_ksk::*;
pub use lwe_to_glwe_ksk::*;
pub const SIGMA: f64 = 3.2; pub const SIGMA: f64 = 3.2;
pub(crate) const SIGMA_BOUND: f64 = 6.0 * SIGMA; pub(crate) const SIGMA_BOUND: f64 = 6.0 * SIGMA;

View File

@@ -17,6 +17,7 @@ pub mod layouts;
pub use automorphism::*; pub use automorphism::*;
pub use conversion::*; pub use conversion::*;
pub use dist::*; pub use dist::*;
pub use encryption::*;
pub use external_product::*; pub use external_product::*;
pub use glwe_packing::*; pub use glwe_packing::*;
pub use keyswitching::*; pub use keyswitching::*;

View File

@@ -4,7 +4,7 @@ use poulpy_hal::{
oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, VecZnxSubScalarInplaceImpl}, oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, VecZnxSubScalarInplaceImpl},
}; };
use crate::decryption::GLWEDecryption; use crate::decryption::GLWEDecrypt;
use crate::layouts::{ use crate::layouts::{
GGLWE, GGLWEInfos, GGLWEToRef, GLWEPlaintext, LWEInfos, GGLWE, GGLWEInfos, GGLWEToRef, GLWEPlaintext, LWEInfos,
prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, prepared::{GLWESecretPrepared, GLWESecretPreparedToRef},
@@ -90,7 +90,7 @@ pub trait GGLWENoise<BE: Backend> {
impl<BE: Backend> GGLWENoise<BE> for Module<BE> impl<BE: Backend> GGLWENoise<BE> for Module<BE>
where where
Module<BE>: GLWEDecryption<BE>, Module<BE>: GLWEDecrypt<BE>,
Scratch<BE>: ScratchTakeBasic Scratch<BE>: ScratchTakeBasic
+ ScratchOwnedAllocImpl<BE> + ScratchOwnedAllocImpl<BE>
+ ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrowImpl<BE>

View File

@@ -9,7 +9,7 @@ use poulpy_hal::{
oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl}, oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl},
}; };
use crate::decryption::GLWEDecryption; use crate::decryption::GLWEDecrypt;
use crate::layouts::prepared::GLWESecretPreparedToRef; use crate::layouts::prepared::GLWESecretPreparedToRef;
use crate::layouts::{GGSW, GGSWInfos, GGSWToRef, GLWEInfos, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared}; use crate::layouts::{GGSW, GGSWInfos, GGSWToRef, GLWEInfos, GLWEPlaintext, LWEInfos, prepared::GLWESecretPrepared};
@@ -79,7 +79,7 @@ where
+ VecZnxIdftApplyTmpA<BE> + VecZnxIdftApplyTmpA<BE>
+ VecZnxAddScalarInplace + VecZnxAddScalarInplace
+ VecZnxSubInplace + VecZnxSubInplace
+ GLWEDecryption<BE>, + GLWEDecrypt<BE>,
Scratch<BE>: ScratchTakeBasic, Scratch<BE>: ScratchTakeBasic,
{ {
fn ggsw_assert_noise<R, S, P, F>(&self, res: &R, sk_prepared: &S, pt_want: &P, max_noise: F) fn ggsw_assert_noise<R, S, P, F>(&self, res: &R, sk_prepared: &S, pt_want: &P, max_noise: F)

View File

@@ -9,7 +9,7 @@ use poulpy_hal::{
}; };
use crate::{ use crate::{
decryption::GLWEDecryption, decryption::GLWEDecrypt,
layouts::{ layouts::{
GLWE, GLWEPlaintext, GLWEPlaintextToRef, GLWEToRef, LWEInfos, GLWE, GLWEPlaintext, GLWEPlaintextToRef, GLWEToRef, LWEInfos,
prepared::{GLWESecretPrepared, GLWESecretPreparedToRef}, prepared::{GLWESecretPrepared, GLWESecretPreparedToRef},
@@ -124,7 +124,7 @@ where
+ VecZnxNormalizeTmpBytes + VecZnxNormalizeTmpBytes
+ VecZnxSubInplace + VecZnxSubInplace
+ VecZnxNormalizeInplace<BE> + VecZnxNormalizeInplace<BE>
+ GLWEDecryption<BE>, + GLWEDecrypt<BE>,
Scratch<BE>: ScratchTakeBasic Scratch<BE>: ScratchTakeBasic
+ ScratchOwnedAllocImpl<BE> + ScratchOwnedAllocImpl<BE>
+ ScratchOwnedBorrowImpl<BE> + ScratchOwnedBorrowImpl<BE>

View File

@@ -1,4 +1,4 @@
// pub mod test_suite; pub mod test_suite;
#[cfg(test)] #[cfg(test)]
mod serialization; mod serialization;

View File

@@ -1,60 +1,38 @@
use poulpy_hal::{ use poulpy_hal::{
api::{ api::{
ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc,
SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxBigAddInplace, VecZnxBigAddNormal, VecZnxBigAddSmallInplace, SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxBigAddInplace, VecZnxBigAddNormal,
VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxCopy, VecZnxDftAlloc, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxBigAddSmallInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxCopy, VecZnxDftAlloc, VecZnxDftApply,
VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace,
}, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace,
layouts::{Backend, Module, ScratchOwned},
oep::{
ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeScalarZnxImpl, TakeSvpPPolImpl,
TakeVecZnxBigImpl, TakeVecZnxDftImpl, TakeVecZnxImpl,
}, },
layouts::{Backend, Module, Scratch, ScratchOwned},
source::Source, source::Source,
}; };
use crate::{ use crate::{
GLWEEncryptSk, GLWESub, ScratchTakeCore,
decryption::GLWEDecrypt,
encryption::SIGMA, encryption::SIGMA,
layouts::{ layouts::{
GLWE, GLWELayout, GLWEPlaintext, GLWEPlaintextLayout, GLWEPublicKey, GLWESecret, LWEInfos, GLWE, GLWEAlloc, GLWELayout, GLWEPlaintext, GLWEPlaintextLayout, GLWEPublicKey, GLWESecret, GLWESecretPrepare,
compressed::{Decompress, GLWECompressed}, GLWESecretPreparedAlloc, LWEInfos,
prepared::{GLWEPublicKeyPrepared, GLWESecretPrepared, PrepareAlloc}, compressed::GLWECompressed,
prepared::{GLWEPublicKeyPrepared, GLWESecretPrepared},
}, },
operations::GLWEOperations,
}; };
pub fn test_glwe_encrypt_sk<B>(module: &Module<B>) pub fn test_glwe_encrypt_sk<B: Backend>(module: &Module<B>)
where where
Module<B>: VecZnxDftBytesOf Module<B>: GLWEAlloc
+ VecZnxBigBytesOf + GLWEEncryptSk<B>
+ VecZnxDftApply<B> + GLWEDecrypt<B>
+ SvpApplyDftToDftInplace<B> + GLWESecretPreparedAlloc<B>
+ VecZnxIdftApplyConsume<B> + GLWESecretPrepare<B>
+ VecZnxBigAddInplace<B>
+ VecZnxBigAddSmallInplace<B>
+ VecZnxBigNormalize<B>
+ VecZnxNormalizeTmpBytes
+ SvpPrepare<B>
+ SvpPPolBytesOf
+ SvpPPolAlloc<B>
+ SvpApplyDftToDft<B>
+ VecZnxBigAddNormal<B>
+ VecZnxFillUniform + VecZnxFillUniform
+ VecZnxSubInplace + GLWESub,
+ VecZnxAddInplace ScratchOwned<B>: ScratchOwnedAlloc<B> + ScratchOwnedBorrow<B>,
+ VecZnxNormalizeInplace<B> Scratch<B>: ScratchAvailable + ScratchTakeCore<B>,
+ VecZnxAddNormal
+ VecZnxNormalize<B>
+ VecZnxSub,
B: Backend
+ TakeVecZnxDftImpl<B>
+ TakeVecZnxBigImpl<B>
+ TakeSvpPPolImpl<B>
+ ScratchOwnedAllocImpl<B>
+ ScratchOwnedBorrowImpl<B>
+ ScratchAvailableImpl<B>
+ TakeScalarZnxImpl<B>
+ TakeVecZnxImpl<B>,
{ {
let base2k: usize = 8; let base2k: usize = 8;
let k_ct: usize = 54; let k_ct: usize = 54;
@@ -76,21 +54,22 @@ where
k: k_pt.into(), k: k_pt.into(),
}; };
let mut ct: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_infos); let mut ct: GLWE<Vec<u8>> = GLWE::alloc_from_infos(module, &glwe_infos);
let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&pt_infos); let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(module, &pt_infos);
let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&pt_infos); let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(module, &pt_infos);
let mut source_xs: Source = Source::new([0u8; 32]); let mut source_xs: Source = Source::new([0u8; 32]);
let mut source_xe: Source = Source::new([0u8; 32]); let mut source_xe: Source = Source::new([0u8; 32]);
let mut source_xa: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]);
let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( let mut scratch: ScratchOwned<B> =
GLWE::encrypt_sk_tmp_bytes(module, &glwe_infos) | GLWE::decrypt_tmp_bytes(module, &glwe_infos), ScratchOwned::alloc(GLWE::encrypt_sk_tmp_bytes(module, &glwe_infos) | GLWE::decrypt_tmp_bytes(module, &glwe_infos));
);
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_infos); let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(module, &glwe_infos);
sk.fill_ternary_prob(0.5, &mut source_xs); sk.fill_ternary_prob(0.5, &mut source_xs);
let sk_prepared: GLWESecretPrepared<Vec<u8>, B> = sk.prepare_alloc(module, scratch.borrow()); let mut sk_prepared: GLWESecretPrepared<Vec<u8>, B> = GLWESecretPrepared::alloc(module, rank.into());
sk_prepared.prepare(module, &sk);
module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa); module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa);
@@ -105,7 +84,7 @@ where
ct.decrypt(module, &mut pt_have, &sk_prepared, scratch.borrow()); ct.decrypt(module, &mut pt_have, &sk_prepared, scratch.borrow());
pt_want.sub_inplace_ab(module, &pt_have); module.glwe_sub_inplace(&mut pt_want, &pt_have);
let noise_have: f64 = pt_want.data.std(base2k, 0) * (ct.k().as_u32() as f64).exp2(); let noise_have: f64 = pt_want.data.std(base2k, 0) * (ct.k().as_u32() as f64).exp2();
let noise_want: f64 = SIGMA; let noise_want: f64 = SIGMA;
@@ -138,15 +117,6 @@ where
+ VecZnxNormalize<B> + VecZnxNormalize<B>
+ VecZnxSub + VecZnxSub
+ VecZnxCopy, + VecZnxCopy,
B: Backend
+ TakeVecZnxDftImpl<B>
+ TakeVecZnxBigImpl<B>
+ TakeSvpPPolImpl<B>
+ ScratchOwnedAllocImpl<B>
+ ScratchOwnedBorrowImpl<B>
+ ScratchAvailableImpl<B>
+ TakeScalarZnxImpl<B>
+ TakeVecZnxImpl<B>,
{ {
let base2k: usize = 8; let base2k: usize = 8;
let k_ct: usize = 54; let k_ct: usize = 54;
@@ -239,15 +209,6 @@ where
+ VecZnxAddNormal + VecZnxAddNormal
+ VecZnxNormalize<B> + VecZnxNormalize<B>
+ VecZnxSub, + VecZnxSub,
B: Backend
+ TakeVecZnxDftImpl<B>
+ TakeVecZnxBigImpl<B>
+ TakeSvpPPolImpl<B>
+ ScratchOwnedAllocImpl<B>
+ ScratchOwnedBorrowImpl<B>
+ ScratchAvailableImpl<B>
+ TakeScalarZnxImpl<B>
+ TakeVecZnxImpl<B>,
{ {
let base2k: usize = 8; let base2k: usize = 8;
let k_ct: usize = 54; let k_ct: usize = 54;
@@ -268,9 +229,8 @@ where
let mut source_xe: Source = Source::new([1u8; 32]); let mut source_xe: Source = Source::new([1u8; 32]);
let mut source_xa: Source = Source::new([0u8; 32]); let mut source_xa: Source = Source::new([0u8; 32]);
let mut scratch: ScratchOwned<B> = ScratchOwned::alloc( let mut scratch: ScratchOwned<B> =
GLWE::decrypt_tmp_bytes(module, &glwe_infos) | GLWE::encrypt_sk_tmp_bytes(module, &glwe_infos), ScratchOwned::alloc(GLWE::decrypt_tmp_bytes(module, &glwe_infos) | GLWE::encrypt_sk_tmp_bytes(module, &glwe_infos));
);
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_infos); let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_infos);
sk.fill_ternary_prob(0.5, &mut source_xs); sk.fill_ternary_prob(0.5, &mut source_xs);
@@ -316,15 +276,6 @@ where
+ VecZnxDftAlloc<B> + VecZnxDftAlloc<B>
+ SvpApplyDftToDft<B> + SvpApplyDftToDft<B>
+ VecZnxBigAddNormal<B>, + VecZnxBigAddNormal<B>,
B: Backend
+ TakeVecZnxDftImpl<B>
+ TakeVecZnxBigImpl<B>
+ TakeSvpPPolImpl<B>
+ ScratchOwnedAllocImpl<B>
+ ScratchOwnedBorrowImpl<B>
+ ScratchAvailableImpl<B>
+ TakeScalarZnxImpl<B>
+ TakeVecZnxImpl<B>,
{ {
let base2k: usize = 8; let base2k: usize = 8;
let k_ct: usize = 54; let k_ct: usize = 54;

View File

@@ -1,11 +1,11 @@
mod gglwe_atk; // mod gglwe_atk;
mod gglwe_ct; // mod gglwe_ct;
mod ggsw_ct; // mod ggsw_ct;
mod glwe_ct; mod glwe_ct;
mod glwe_tsk; // mod glwe_tsk;
pub use gglwe_atk::*; // pub use gglwe_atk::*;
pub use gglwe_ct::*; // pub use gglwe_ct::*;
pub use ggsw_ct::*; // pub use ggsw_ct::*;
pub use glwe_ct::*; pub use glwe_ct::*;
pub use glwe_tsk::*; // pub use glwe_tsk::*;

View File

@@ -1,12 +1,12 @@
pub mod automorphism; // pub mod automorphism;
pub mod encryption; pub mod encryption;
pub mod external_product; // pub mod external_product;
pub mod keyswitch; // pub mod keyswitch;
mod conversion; // mod conversion;
mod packing; // mod packing;
mod trace; // mod trace;
pub use conversion::*; // pub use conversion::*;
pub use packing::*; // pub use packing::*;
pub use trace::*; // pub use trace::*;