gglwe atk key

This commit is contained in:
Rasoul Akhavan Mahdavi
2025-10-19 22:57:00 -04:00
parent 1ba54b68c7
commit d68c0a2baf
5 changed files with 59 additions and 110 deletions

View File

@@ -20,6 +20,12 @@ pub struct AutomorphismKeyCompressed<D: Data> {
pub(crate) p: i64, pub(crate) p: i64,
} }
impl<D: Data> GetAutomorphismGaloisElement for AutomorphismKeyCompressed<D> {
fn p(&self) -> i64 {
self.p
}
}
impl<D: Data> LWEInfos for AutomorphismKeyCompressed<D> { impl<D: Data> LWEInfos for AutomorphismKeyCompressed<D> {
fn n(&self) -> Degree { fn n(&self) -> Degree {
self.key.n() self.key.n()
@@ -37,6 +43,7 @@ impl<D: Data> LWEInfos for AutomorphismKeyCompressed<D> {
self.key.size() self.key.size()
} }
} }
impl<D: Data> GLWEInfos for AutomorphismKeyCompressed<D> { impl<D: Data> GLWEInfos for AutomorphismKeyCompressed<D> {
fn rank(&self) -> Rank { fn rank(&self) -> Rank {
self.rank_out() self.rank_out()
@@ -199,7 +206,7 @@ where
} }
} }
impl<B: Backend> AutomorphismKeyDecompress for Module<B> where Self: AutomorphismKeyDecompress {} impl<B: Backend> AutomorphismKeyDecompress for Module<B> where Self: GLWESwitchingKeyDecompress {}
impl<D: DataMut> AutomorphismKey<D> impl<D: DataMut> AutomorphismKey<D>
where where

View File

@@ -3,8 +3,8 @@ mod ggsw_ct;
mod glwe_ct; mod glwe_ct;
pub use gglwe_ct::*; pub use gglwe_ct::*;
pub use ggsw_ct::*; // pub use ggsw_ct::*;
pub use glwe_ct::*; // pub use glwe_ct::*;
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
#[allow(dead_code)] #[allow(dead_code)]

View File

@@ -32,8 +32,8 @@ backend_test_suite!(
// GGLWE Encryption // GGLWE Encryption
gglwe_switching_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_switching_key_encrypt_sk, gglwe_switching_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_switching_key_encrypt_sk,
gglwe_switching_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_switching_key_compressed_encrypt_sk, gglwe_switching_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_switching_key_compressed_encrypt_sk,
// gglwe_automorphisk_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_automorphisk_key_encrypt_sk, gglwe_automorphism_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_automorphism_key_encrypt_sk,
// gglwe_automorphisk_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_automorphisk_key_compressed_encrypt_sk, gglwe_automorphism_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_automorphism_key_compressed_encrypt_sk,
// gglwe_tensor_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_tensor_key_encrypt_sk, // gglwe_tensor_key_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_tensor_key_encrypt_sk,
// gglwe_tensor_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_tensor_key_compressed_encrypt_sk, // gglwe_tensor_key_compressed_encrypt_sk => crate::tests::test_suite::encryption::test_gglwe_tensor_key_compressed_encrypt_sk,
// GGLWE Keyswitching // GGLWE Keyswitching

View File

@@ -1,71 +1,38 @@
use poulpy_hal::{ use poulpy_hal::{
api::{ api::{
ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow,
VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxAutomorphismInplace, VecZnxAutomorphism,VecZnxFillUniform,
VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes,
VecZnxCopy, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize,
VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSubScalarInplace, VecZnxSwitchRing,
VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare,
},
layouts::{Backend, Module, ScratchOwned},
oep::{
ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeScalarZnxImpl, TakeSvpPPolImpl,
TakeVecZnxBigImpl, TakeVecZnxDftImpl, TakeVecZnxImpl,
}, },
layouts::{Backend, Module, Scratch, ScratchOwned, GaloisElement},
source::Source, source::Source,
}; };
use crate::{ use crate::{
encryption::SIGMA, encryption::SIGMA,
layouts::{ layouts::{
AutomorphismKey, AutomorphismKeyLayout, GLWEInfos, GLWESecret, compressed::AutomorphismKeyCompressed, prepared::GLWESecretPrepared,
compressed::{AutomorphismKeyCompressed, Decompress}, AutomorphismKey, AutomorphismKeyDecompress, AutomorphismKeyLayout, GLWEInfos,
prepared::{GLWESecretPrepared, PrepareAlloc}, GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, GLWESwitchingKeyAlloc, GLWESwitchingKeyDecompress
}, },
noise::GGLWENoise,
GGLWEEncryptSk, GGLWEKeyswitch, GLWESwitchingKeyCompressedEncryptSk, GLWESwitchingKeyEncryptSk, ScratchTakeCore
}; };
pub fn test_gglwe_automorphisk_key_encrypt_sk<B>(module: &Module<B>) pub fn test_gglwe_automorphism_key_encrypt_sk<BE: Backend>(module: &Module<BE>)
where where
Module<B>: VecZnxDftBytesOf Module<BE>: GGLWEEncryptSk<BE>
+ VecZnxBigNormalize<B> + GGLWEKeyswitch<BE>
+ VecZnxDftApply<B> + GLWESecretPreparedAlloc<BE>
+ SvpApplyDftToDftInplace<B> + GLWESecretPrepare<BE>
+ VecZnxIdftApplyConsume<B> + GLWESwitchingKeyAlloc
+ VecZnxNormalizeTmpBytes + GLWESwitchingKeyEncryptSk<BE>
+ GLWESwitchingKeyCompressedEncryptSk<BE>
+ GLWESwitchingKeyDecompress
+ GGLWENoise<BE>
+ VecZnxFillUniform + VecZnxFillUniform
+ VecZnxSubInplace + VecZnxAutomorphism,
+ VecZnxAddInplace ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
+ VecZnxNormalizeInplace<B> Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
+ VecZnxAddNormal
+ VecZnxNormalize<B>
+ VecZnxSub
+ SvpPrepare<B>
+ SvpPPolBytesOf
+ SvpPPolAlloc<B>
+ VmpApplyDftToDftTmpBytes
+ VecZnxBigNormalizeTmpBytes
+ VmpApplyDftToDft<B>
+ VmpApplyDftToDftAdd<B>
+ VecZnxBigAddSmallInplace<B>
+ VecZnxAutomorphism
+ VecZnxSwitchRing
+ VecZnxAddScalarInplace
+ VecZnxAutomorphismInplace<B>
+ VecZnxBigBytesOf
+ VecZnxBigAddInplace<B>
+ VecZnxSubScalarInplace
+ VecZnxCopy
+ VmpPMatAlloc<B>
+ VmpPrepare<B>,
B: Backend
+ ScratchOwnedAllocImpl<B>
+ ScratchOwnedBorrowImpl<B>
+ ScratchAvailableImpl<B>
+ TakeScalarZnxImpl<B>
+ TakeVecZnxDftImpl<B>
+ TakeVecZnxImpl<B>
+ TakeSvpPPolImpl<B>
+ TakeVecZnxBigImpl<B>,
{ {
let base2k: usize = 12; let base2k: usize = 12;
let k_ksk: usize = 60; let k_ksk: usize = 60;
@@ -84,17 +51,17 @@ where
rank: rank.into(), rank: rank.into(),
}; };
let mut atk: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc_from_infos(&atk_infos); let mut atk: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc_from_infos(module, &atk_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(AutomorphismKey::encrypt_sk_tmp_bytes( let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(AutomorphismKey::encrypt_sk_tmp_bytes(
module, &atk_infos, module, &atk_infos,
)); ));
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&atk_infos); let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(module, &atk_infos);
sk.fill_ternary_prob(0.5, &mut source_xs); sk.fill_ternary_prob(0.5, &mut source_xs);
let p = -5; let p = -5;
@@ -118,7 +85,8 @@ where
i, i,
); );
}); });
let sk_out_prepared: GLWESecretPrepared<Vec<u8>, B> = sk_out.prepare_alloc(module, scratch.borrow()); let mut sk_out_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(module, sk_out.rank().into());
sk_out_prepared.prepare(module, &sk_out);
atk.key atk.key
.key .key
@@ -127,48 +95,21 @@ where
} }
} }
pub fn test_gglwe_automorphisk_key_compressed_encrypt_sk<B>(module: &Module<B>) pub fn test_gglwe_automorphism_key_compressed_encrypt_sk<BE: Backend>(module: &Module<BE>)
where where
Module<B>: VecZnxDftBytesOf Module<BE>: GGLWEEncryptSk<BE>
+ VecZnxBigNormalize<B> + GGLWEKeyswitch<BE>
+ VecZnxDftApply<B> + GLWESecretPreparedAlloc<BE>
+ SvpApplyDftToDftInplace<B> + GLWESecretPrepare<BE>
+ VecZnxIdftApplyConsume<B> + GLWESwitchingKeyAlloc
+ VecZnxNormalizeTmpBytes + GLWESwitchingKeyEncryptSk<BE>
+ VecZnxFillUniform + GLWESwitchingKeyCompressedEncryptSk<BE>
+ VecZnxSubInplace + AutomorphismKeyDecompress
+ VecZnxAddInplace
+ VecZnxNormalizeInplace<B>
+ VecZnxAddNormal
+ VecZnxNormalize<B>
+ VecZnxSub
+ SvpPrepare<B>
+ SvpPPolBytesOf
+ SvpPPolAlloc<B>
+ VmpApplyDftToDftTmpBytes
+ VecZnxBigNormalizeTmpBytes
+ VmpApplyDftToDft<B>
+ VmpApplyDftToDftAdd<B>
+ VecZnxBigAddSmallInplace<B>
+ VecZnxAutomorphism + VecZnxAutomorphism
+ VecZnxSwitchRing + VecZnxFillUniform
+ VecZnxAddScalarInplace + GGLWENoise<BE>,
+ VecZnxAutomorphismInplace<B> ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
+ VecZnxBigBytesOf Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
+ VecZnxBigAddInplace<B>
+ VecZnxSubScalarInplace
+ VecZnxCopy
+ VmpPMatAlloc<B>
+ VmpPrepare<B>,
B: Backend
+ ScratchOwnedAllocImpl<B>
+ ScratchOwnedBorrowImpl<B>
+ ScratchAvailableImpl<B>
+ TakeScalarZnxImpl<B>
+ TakeVecZnxDftImpl<B>
+ TakeVecZnxImpl<B>
+ TakeSvpPPolImpl<B>
+ TakeVecZnxBigImpl<B>,
{ {
let base2k: usize = 12; let base2k: usize = 12;
let k_ksk: usize = 60; let k_ksk: usize = 60;
@@ -187,16 +128,16 @@ where
rank: rank.into(), rank: rank.into(),
}; };
let mut atk_compressed: AutomorphismKeyCompressed<Vec<u8>> = AutomorphismKeyCompressed::alloc_from_infos(&atk_infos); let mut atk_compressed: AutomorphismKeyCompressed<Vec<u8>> = AutomorphismKeyCompressed::alloc_from_infos(module, &atk_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 scratch: ScratchOwned<B> = ScratchOwned::alloc(AutomorphismKey::encrypt_sk_tmp_bytes( let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(AutomorphismKey::encrypt_sk_tmp_bytes(
module, &atk_infos, module, &atk_infos,
)); ));
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&atk_infos); let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(module, &atk_infos);
sk.fill_ternary_prob(0.5, &mut source_xs); sk.fill_ternary_prob(0.5, &mut source_xs);
let p = -5; let p = -5;
@@ -215,9 +156,10 @@ where
i, i,
); );
}); });
let sk_out_prepared = sk_out.prepare_alloc(module, scratch.borrow()); let mut sk_out_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(module, sk_out.rank().into());
sk_out_prepared.prepare(module, &sk_out);
let mut atk: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc_from_infos(&atk_infos); let mut atk: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc_from_infos(module, &atk_infos);
atk.decompress(module, &atk_compressed); atk.decompress(module, &atk_compressed);
atk.key atk.key

View File

@@ -1,10 +1,10 @@
// 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::*;