ggsw encrypt

This commit is contained in:
Pro7ech
2025-10-20 10:39:00 +02:00
parent 53bc78f421
commit 8dc4946736
4 changed files with 29 additions and 70 deletions

View File

@@ -21,6 +21,7 @@ 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::*;
pub use noise::*;
pub use encryption::SIGMA; pub use encryption::SIGMA;

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

@@ -1,32 +1,23 @@
use poulpy_hal::{ use poulpy_hal::{
api::{ api::{ScratchOwnedAlloc, ScratchOwnedBorrow},
ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare, layouts::{Backend, Module, ScalarZnx, Scratch, ScratchOwned},
VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAlloc,
VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxCopy, VecZnxDftAlloc, VecZnxDftApply,
VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalize,
VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VmpPMatAlloc, VmpPrepare,
},
layouts::{Backend, Module, ScalarZnx, ScratchOwned},
oep::{
ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeScalarZnxImpl, TakeSvpPPolImpl,
TakeVecZnxBigImpl, TakeVecZnxDftImpl, TakeVecZnxImpl, VecZnxBigAllocBytesImpl, VecZnxDftAllocBytesImpl,
},
source::Source, source::Source,
}; };
use crate::{ use crate::{
GGSWCompressedEncryptSk, GGSWEncryptSk, GGSWNoise, ScratchTakeCore,
encryption::SIGMA, encryption::SIGMA,
layouts::{ layouts::{
GGSW, GGSWCiphertextLayout, GLWESecret, GGSW, GGSWDecompress, GGSWLayout, GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc, compressed::GGSWCompressed,
compressed::{Decompress, GGSWCompressed}, prepared::GLWESecretPrepared,
prepared::{GLWESecretPrepared, PrepareAlloc},
}, },
}; };
pub fn test_ggsw_encrypt_sk<B: Backend>(module: &Module<B>) pub fn test_ggsw_encrypt_sk<BE: Backend>(module: &Module<BE>)
where where
ScratchOwned<B>: ScratchOwnedAlloc<B>, ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
Module<B>: SvpPrepare<B>, Scratch<BE>: ScratchTakeCore<BE>,
Module<BE>: GGSWEncryptSk<BE> + GLWESecretPreparedAlloc<BE> + GLWESecretPrepare<BE> + GGSWNoise<BE>,
{ {
let base2k: usize = 12; let base2k: usize = 12;
let k: usize = 54; let k: usize = 54;
@@ -36,7 +27,7 @@ where
let n: usize = module.n(); let n: usize = module.n();
let dnum: usize = (k - di * base2k) / (di * base2k); let dnum: usize = (k - di * base2k) / (di * base2k);
let ggsw_infos: GGSWCiphertextLayout = GGSWCiphertextLayout { let ggsw_infos: GGSWLayout = GGSWLayout {
n: n.into(), n: n.into(),
base2k: base2k.into(), base2k: base2k.into(),
k: k.into(), k: k.into(),
@@ -55,11 +46,13 @@ where
pt_scalar.fill_ternary_hw(0, n, &mut source_xs); pt_scalar.fill_ternary_hw(0, n, &mut source_xs);
let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(GGSW::encrypt_sk_tmp_bytes(module, &ggsw_infos)); let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(GGSW::encrypt_sk_tmp_bytes(module, &ggsw_infos));
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&ggsw_infos); let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&ggsw_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>, BE> = GLWESecretPrepared::alloc(module, rank.into());
sk_prepared.prepare(module, &sk);
ct.encrypt_sk( ct.encrypt_sk(
module, module,
@@ -77,46 +70,12 @@ where
} }
} }
pub fn test_ggsw_compressed_encrypt_sk<B>(module: &Module<B>) pub fn test_ggsw_compressed_encrypt_sk<BE: Backend>(module: &Module<BE>)
where where
Module<B>: VecZnxDftBytesOf ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
+ VecZnxBigNormalize<B> Scratch<BE>: ScratchTakeCore<BE>,
+ VecZnxDftApply<B> Module<BE>:
+ SvpApplyDftToDftInplace<B> GGSWCompressedEncryptSk<BE> + GLWESecretPreparedAlloc<BE> + GLWESecretPrepare<BE> + GGSWNoise<BE> + GGSWDecompress,
+ VecZnxIdftApplyConsume<B>
+ VecZnxNormalizeTmpBytes
+ VecZnxFillUniform
+ VecZnxSubInplace
+ VecZnxAddInplace
+ VecZnxNormalizeInplace<B>
+ VecZnxAddNormal
+ VecZnxNormalize<B>
+ VecZnxSub
+ SvpPrepare<B>
+ SvpPPolBytesOf
+ SvpPPolAlloc<B>
+ VecZnxBigAddSmallInplace<B>
+ VecZnxAddScalarInplace
+ VecZnxBigBytesOf
+ VecZnxBigAddInplace<B>
+ VecZnxCopy
+ VmpPMatAlloc<B>
+ VmpPrepare<B>
+ VecZnxBigAlloc<B>
+ VecZnxDftAlloc<B>
+ VecZnxBigNormalizeTmpBytes
+ VecZnxIdftApplyTmpA<B>,
B: Backend
+ TakeVecZnxDftImpl<B>
+ TakeVecZnxBigImpl<B>
+ ScratchOwnedAllocImpl<B>
+ ScratchOwnedBorrowImpl<B>
+ ScratchAvailableImpl<B>
+ TakeScalarZnxImpl<B>
+ TakeVecZnxImpl<B>
+ VecZnxDftAllocBytesImpl<B>
+ VecZnxBigAllocBytesImpl<B>
+ TakeSvpPPolImpl<B>,
{ {
let base2k: usize = 12; let base2k: usize = 12;
let k: usize = 54; let k: usize = 54;
@@ -126,7 +85,7 @@ where
let n: usize = module.n(); let n: usize = module.n();
let dnum: usize = (k - di * base2k) / (di * base2k); let dnum: usize = (k - di * base2k) / (di * base2k);
let ggsw_infos: GGSWCiphertextLayout = GGSWCiphertextLayout { let ggsw_infos: GGSWLayout = GGSWLayout {
n: n.into(), n: n.into(),
base2k: base2k.into(), base2k: base2k.into(),
k: k.into(), k: k.into(),
@@ -144,14 +103,13 @@ where
pt_scalar.fill_ternary_hw(0, n, &mut source_xs); pt_scalar.fill_ternary_hw(0, n, &mut source_xs);
let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(GGSWCompressed::encrypt_sk_tmp_bytes( let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(GGSWCompressed::encrypt_sk_tmp_bytes(module, &ggsw_infos));
module,
&ggsw_infos,
));
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&ggsw_infos); let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&ggsw_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>, BE> = GLWESecretPrepared::alloc(module, rank.into());
sk_prepared.prepare(module, &sk);
let seed_xa: [u8; 32] = [1u8; 32]; let seed_xa: [u8; 32] = [1u8; 32];

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::*;