external product tests

This commit is contained in:
Pro7ech
2025-10-20 16:10:49 +02:00
parent 252eda36fe
commit b91918dcaf
7 changed files with 145 additions and 339 deletions

View File

@@ -1,72 +1,34 @@
use poulpy_hal::{
api::{
ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare,
VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace,
VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxCopy, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform,
VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotateInplace, VecZnxSub,
VecZnxSubInplace, VecZnxSubScalarInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd,
VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare,
},
layouts::{Backend, Module, ScalarZnx, ScalarZnxToMut, ScratchOwned, ZnxViewMut},
oep::{
ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeScalarZnxImpl, TakeSvpPPolImpl,
TakeVecZnxBigImpl, TakeVecZnxDftImpl, TakeVecZnxImpl, VecZnxBigAllocBytesImpl, VecZnxDftAllocBytesImpl,
},
api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxRotateInplace},
layouts::{Backend, Module, ScalarZnx, ScalarZnxToMut, Scratch, ScratchOwned, ZnxViewMut},
source::Source,
};
use crate::{
GGLWEExternalProduct, GGLWENoise, GGSWEncryptSk, GLWESwitchingKeyEncryptSk, ScratchTakeCore,
encryption::SIGMA,
layouts::{
GGSW, GGSWCiphertextLayout, GLWESecret, GLWESwitchingKey, GLWESwitchingKeyLayout,
prepared::{GGSWPrepared, GLWESecretPrepared, PrepareAlloc},
GGSW, GGSWLayout, GGSWPrepare, GGSWPreparedAlloc, GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc,
GLWESwitchingKey, GLWESwitchingKeyLayout,
prepared::{GGSWPrepared, GLWESecretPrepared},
},
noise::noise_ggsw_product,
};
#[allow(clippy::too_many_arguments)]
pub fn test_gglwe_switching_key_external_product<B>(module: &Module<B>)
pub fn test_gglwe_switching_key_external_product<BE: Backend>(module: &Module<BE>)
where
Module<B>: VecZnxDftBytesOf
+ VecZnxBigNormalize<B>
+ VecZnxDftApply<B>
+ SvpApplyDftToDftInplace<B>
+ VecZnxIdftApplyConsume<B>
+ VecZnxNormalizeTmpBytes
+ VecZnxFillUniform
+ VecZnxSubInplace
+ VecZnxAddInplace
+ VecZnxNormalizeInplace<B>
+ VecZnxAddNormal
+ VecZnxNormalize<B>
+ VecZnxSub
+ SvpPrepare<B>
+ SvpPPolBytesOf
+ SvpPPolAlloc<B>
+ VecZnxBigBytesOf
+ VecZnxBigAddInplace<B>
+ VecZnxBigAddSmallInplace<B>
+ VecZnxSwitchRing
+ VecZnxAddScalarInplace
+ VecZnxSubScalarInplace
+ VecZnxCopy
+ VmpPMatAlloc<B>
+ VecZnxRotateInplace<B>
+ VmpApplyDftToDftTmpBytes
+ VmpApplyDftToDft<B>
+ VmpApplyDftToDftAdd<B>
+ VmpPrepare<B>,
B: Backend
+ TakeVecZnxDftImpl<B>
+ TakeVecZnxBigImpl<B>
+ ScratchOwnedAllocImpl<B>
+ ScratchOwnedBorrowImpl<B>
+ ScratchAvailableImpl<B>
+ TakeScalarZnxImpl<B>
+ TakeVecZnxImpl<B>
+ VecZnxDftAllocBytesImpl<B>
+ VecZnxBigAllocBytesImpl<B>
+ TakeSvpPPolImpl<B>,
Module<BE>: GGLWEExternalProduct<BE>
+ GGSWEncryptSk<BE>
+ GLWESwitchingKeyEncryptSk<BE>
+ GLWESecretPrepare<BE>
+ GLWESecretPreparedAlloc<BE>
+ VecZnxRotateInplace<BE>
+ GGSWPrepare<BE>
+ GGSWPreparedAlloc<BE>
+ GGLWENoise<BE>,
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
{
let base2k: usize = 12;
let k_in: usize = 60;
@@ -101,7 +63,7 @@ where
rank_out: rank_out.into(),
};
let ggsw_infos: GGSWCiphertextLayout = GGSWCiphertextLayout {
let ggsw_infos: GGSWLayout = GGSWLayout {
n: n.into(),
base2k: base2k.into(),
k: k_ggsw.into(),
@@ -120,14 +82,9 @@ where
let mut source_xe: 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<BE> = ScratchOwned::alloc(
GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &gglwe_in_infos)
| GLWESwitchingKey::external_product_tmp_bytes(
module,
&gglwe_out_infos,
&gglwe_in_infos,
&ggsw_infos,
)
| GLWESwitchingKey::external_product_tmp_bytes(module, &gglwe_out_infos, &gglwe_in_infos, &ggsw_infos)
| GGSW::encrypt_sk_tmp_bytes(module, &ggsw_infos),
);
@@ -142,7 +99,9 @@ where
let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), rank_out.into());
sk_out.fill_ternary_prob(var_xs, &mut source_xs);
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, rank_out.into());
sk_out_prepared.prepare(module, &sk_out);
// gglwe_{s1}(s0) = s0 -> s1
ct_gglwe_in.encrypt_sk(
@@ -163,7 +122,8 @@ where
scratch.borrow(),
);
let ct_rgsw_prepared: GGSWPrepared<Vec<u8>, B> = ct_rgsw.prepare_alloc(module, scratch.borrow());
let mut ct_rgsw_prepared: GGSWPrepared<Vec<u8>, BE> = GGSWPrepared::alloc_from_infos(module, &ct_rgsw);
ct_rgsw_prepared.prepare(module, &ct_rgsw, scratch.borrow());
// gglwe_(m) (x) RGSW_(X^k) = gglwe_(m * X^k)
ct_gglwe_out.external_product(module, &ct_gglwe_in, &ct_rgsw_prepared, scratch.borrow());
@@ -207,48 +167,19 @@ where
}
#[allow(clippy::too_many_arguments)]
pub fn test_gglwe_switching_key_external_product_inplace<B>(module: &Module<B>)
pub fn test_gglwe_switching_key_external_product_inplace<BE: Backend>(module: &Module<BE>)
where
Module<B>: VecZnxDftBytesOf
+ VecZnxBigNormalize<B>
+ VecZnxDftApply<B>
+ SvpApplyDftToDftInplace<B>
+ VecZnxIdftApplyConsume<B>
+ VecZnxNormalizeTmpBytes
+ VecZnxFillUniform
+ VecZnxSubInplace
+ VecZnxAddInplace
+ VecZnxNormalizeInplace<B>
+ VecZnxAddNormal
+ VecZnxNormalize<B>
+ VecZnxSub
+ SvpPrepare<B>
+ SvpPPolBytesOf
+ SvpPPolAlloc<B>
+ VecZnxBigBytesOf
+ VecZnxBigAddInplace<B>
+ VecZnxBigAddSmallInplace<B>
+ VecZnxSwitchRing
+ VecZnxAddScalarInplace
+ VecZnxSubScalarInplace
+ VecZnxCopy
+ VmpPMatAlloc<B>
+ VecZnxRotateInplace<B>
+ VmpApplyDftToDftTmpBytes
+ VmpApplyDftToDft<B>
+ VmpApplyDftToDftAdd<B>
+ VmpPrepare<B>,
B: Backend
+ TakeVecZnxDftImpl<B>
+ TakeVecZnxBigImpl<B>
+ ScratchOwnedAllocImpl<B>
+ ScratchOwnedBorrowImpl<B>
+ ScratchAvailableImpl<B>
+ TakeScalarZnxImpl<B>
+ TakeVecZnxImpl<B>
+ VecZnxDftAllocBytesImpl<B>
+ VecZnxBigAllocBytesImpl<B>
+ TakeSvpPPolImpl<B>,
Module<BE>: GGLWEExternalProduct<BE>
+ GGSWEncryptSk<BE>
+ GLWESwitchingKeyEncryptSk<BE>
+ GLWESecretPrepare<BE>
+ GLWESecretPreparedAlloc<BE>
+ VecZnxRotateInplace<BE>
+ GGSWPrepare<BE>
+ GGSWPreparedAlloc<BE>
+ GGLWENoise<BE>,
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
{
let base2k: usize = 12;
let k_out: usize = 60;
@@ -273,7 +204,7 @@ where
rank_out: rank_out.into(),
};
let ggsw_infos: GGSWCiphertextLayout = GGSWCiphertextLayout {
let ggsw_infos: GGSWLayout = GGSWLayout {
n: n.into(),
base2k: base2k.into(),
k: k_ggsw.into(),
@@ -291,9 +222,9 @@ where
let mut source_xe: 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<BE> = ScratchOwned::alloc(
GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &gglwe_out_infos)
| GLWESwitchingKey::external_product_inplace_tmp_bytes(module, &gglwe_out_infos, &ggsw_infos)
| GLWESwitchingKey::external_product_tmp_bytes(module, &gglwe_out_infos, &gglwe_out_infos, &ggsw_infos)
| GGSW::encrypt_sk_tmp_bytes(module, &ggsw_infos),
);
@@ -308,7 +239,9 @@ where
let mut sk_out: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), rank_out.into());
sk_out.fill_ternary_prob(var_xs, &mut source_xs);
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, rank_out.into());
sk_out_prepared.prepare(module, &sk_out);
// gglwe_{s1}(s0) = s0 -> s1
ct_gglwe.encrypt_sk(
@@ -329,7 +262,8 @@ where
scratch.borrow(),
);
let ct_rgsw_prepared: GGSWPrepared<Vec<u8>, B> = ct_rgsw.prepare_alloc(module, scratch.borrow());
let mut ct_rgsw_prepared: GGSWPrepared<Vec<u8>, BE> = GGSWPrepared::alloc_from_infos(module, &ct_rgsw);
ct_rgsw_prepared.prepare(module, &ct_rgsw, scratch.borrow());
// gglwe_(m) (x) RGSW_(X^k) = gglwe_(m * X^k)
ct_gglwe.external_product_inplace(module, &ct_rgsw_prepared, scratch.borrow());

View File

@@ -1,74 +1,32 @@
use poulpy_hal::{
api::{
ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare,
VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAlloc,
VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxCopy, VecZnxDftAlloc, VecZnxDftApply,
VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalize,
VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotateInplace, VecZnxSub, VecZnxSubInplace, VmpApplyDftToDft,
VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare,
},
layouts::{Backend, Module, ScalarZnx, ScalarZnxToMut, ScratchOwned, ZnxViewMut},
oep::{
ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeScalarZnxImpl, TakeSvpPPolImpl,
TakeVecZnxBigImpl, TakeVecZnxDftImpl, TakeVecZnxImpl, VecZnxBigAllocBytesImpl, VecZnxDftAllocBytesImpl,
},
api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxRotateInplace},
layouts::{Backend, Module, ScalarZnx, ScalarZnxToMut, Scratch, ScratchOwned, ZnxViewMut},
source::Source,
};
use crate::{
GGSWEncryptSk, GGSWExternalProduct, GGSWNoise, ScratchTakeCore,
encryption::SIGMA,
layouts::{
GGSW, GGSWCiphertextLayout, GLWESecret,
prepared::{GGSWPrepared, GLWESecretPrepared, PrepareAlloc},
GGSW, GGSWLayout, GGSWPrepare, GGSWPreparedAlloc, GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc,
prepared::{GGSWPrepared, GLWESecretPrepared},
},
noise::noise_ggsw_product,
};
#[allow(clippy::too_many_arguments)]
pub fn test_ggsw_external_product<B>(module: &Module<B>)
pub fn test_ggsw_external_product<BE: Backend>(module: &Module<BE>)
where
Module<B>: VecZnxDftBytesOf
+ VecZnxBigNormalize<B>
+ VecZnxDftApply<B>
+ SvpApplyDftToDftInplace<B>
+ VecZnxIdftApplyConsume<B>
+ VecZnxNormalizeTmpBytes
+ VecZnxFillUniform
+ VecZnxSubInplace
+ VecZnxAddInplace
+ VecZnxNormalizeInplace<B>
+ VecZnxAddNormal
+ VecZnxNormalize<B>
+ VecZnxSub
+ SvpPrepare<B>
+ SvpPPolBytesOf
+ SvpPPolAlloc<B>
+ VecZnxBigBytesOf
+ VecZnxBigAddInplace<B>
+ VecZnxBigAddSmallInplace<B>
+ VecZnxAddScalarInplace
+ VecZnxCopy
+ VmpPMatAlloc<B>
+ VecZnxRotateInplace<B>
+ VmpApplyDftToDftTmpBytes
+ VmpApplyDftToDft<B>
+ VmpApplyDftToDftAdd<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>,
Module<BE>: GGSWEncryptSk<BE>
+ GGSWExternalProduct<BE>
+ GLWESecretPrepare<BE>
+ GLWESecretPreparedAlloc<BE>
+ GGSWPrepare<BE>
+ GGSWPreparedAlloc<BE>
+ VecZnxRotateInplace<BE>
+ GGSWNoise<BE>,
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
{
let base2k: usize = 12;
let k_in: usize = 60;
@@ -84,7 +42,7 @@ where
let dnum_in: usize = k_in.div_euclid(base2k * di);
let dsize_in: usize = 1;
let ggsw_in_infos: GGSWCiphertextLayout = GGSWCiphertextLayout {
let ggsw_in_infos: GGSWLayout = GGSWLayout {
n: n.into(),
base2k: base2k.into(),
k: k_in.into(),
@@ -93,7 +51,7 @@ where
rank: rank.into(),
};
let ggsw_out_infos: GGSWCiphertextLayout = GGSWCiphertextLayout {
let ggsw_out_infos: GGSWLayout = GGSWLayout {
n: n.into(),
base2k: base2k.into(),
k: k_out.into(),
@@ -102,7 +60,7 @@ where
rank: rank.into(),
};
let ggsw_apply_infos: GGSWCiphertextLayout = GGSWCiphertextLayout {
let ggsw_apply_infos: GGSWLayout = GGSWLayout {
n: n.into(),
base2k: base2k.into(),
k: k_apply.into(),
@@ -127,7 +85,7 @@ where
pt_apply.to_mut().raw_mut()[k] = 1; //X^{k}
let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(
let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(
GGSW::encrypt_sk_tmp_bytes(module, &ggsw_apply_infos)
| GGSW::encrypt_sk_tmp_bytes(module, &ggsw_in_infos)
| GGSW::external_product_tmp_bytes(module, &ggsw_out_infos, &ggsw_in_infos, &ggsw_apply_infos),
@@ -135,7 +93,9 @@ where
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), rank.into());
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);
ggsw_apply.encrypt_sk(
module,
@@ -155,7 +115,8 @@ where
scratch.borrow(),
);
let ct_rhs_prepared: GGSWPrepared<Vec<u8>, B> = ggsw_apply.prepare_alloc(module, scratch.borrow());
let mut ct_rhs_prepared: GGSWPrepared<Vec<u8>, BE> = GGSWPrepared::alloc_from_infos(module, &ggsw_apply);
ct_rhs_prepared.prepare(module, &ggsw_apply, scratch.borrow());
ggsw_out.external_product(module, &ggsw_in, &ct_rhs_prepared, scratch.borrow());
@@ -190,50 +151,18 @@ where
}
#[allow(clippy::too_many_arguments)]
pub fn test_ggsw_external_product_inplace<B>(module: &Module<B>)
pub fn test_ggsw_external_product_inplace<BE: Backend>(module: &Module<BE>)
where
Module<B>: VecZnxDftBytesOf
+ VecZnxBigNormalize<B>
+ VecZnxDftApply<B>
+ SvpApplyDftToDftInplace<B>
+ VecZnxIdftApplyConsume<B>
+ VecZnxNormalizeTmpBytes
+ VecZnxFillUniform
+ VecZnxSubInplace
+ VecZnxAddInplace
+ VecZnxNormalizeInplace<B>
+ VecZnxAddNormal
+ VecZnxNormalize<B>
+ VecZnxSub
+ SvpPrepare<B>
+ SvpPPolBytesOf
+ SvpPPolAlloc<B>
+ VecZnxBigBytesOf
+ VecZnxBigAddInplace<B>
+ VecZnxBigAddSmallInplace<B>
+ VecZnxAddScalarInplace
+ VecZnxCopy
+ VmpPMatAlloc<B>
+ VecZnxRotateInplace<B>
+ VmpApplyDftToDftTmpBytes
+ VmpApplyDftToDft<B>
+ VmpApplyDftToDftAdd<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>,
Module<BE>: GGSWEncryptSk<BE>
+ GGSWExternalProduct<BE>
+ GLWESecretPrepare<BE>
+ GLWESecretPreparedAlloc<BE>
+ GGSWPrepare<BE>
+ GGSWPreparedAlloc<BE>
+ VecZnxRotateInplace<BE>
+ GGSWNoise<BE>,
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
{
let base2k: usize = 12;
let k_out: usize = 60;
@@ -247,7 +176,7 @@ where
let dnum_in: usize = k_out.div_euclid(base2k * di);
let dsize_in: usize = 1;
let ggsw_out_infos: GGSWCiphertextLayout = GGSWCiphertextLayout {
let ggsw_out_infos: GGSWLayout = GGSWLayout {
n: n.into(),
base2k: base2k.into(),
k: k_out.into(),
@@ -256,7 +185,7 @@ where
rank: rank.into(),
};
let ggsw_apply_infos: GGSWCiphertextLayout = GGSWCiphertextLayout {
let ggsw_apply_infos: GGSWLayout = GGSWLayout {
n: n.into(),
base2k: base2k.into(),
k: k_apply.into(),
@@ -281,15 +210,17 @@ where
pt_apply.to_mut().raw_mut()[k] = 1; //X^{k}
let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(
let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(
GGSW::encrypt_sk_tmp_bytes(module, &ggsw_apply_infos)
| GGSW::encrypt_sk_tmp_bytes(module, &ggsw_out_infos)
| GGSW::external_product_inplace_tmp_bytes(module, &ggsw_out_infos, &ggsw_apply_infos),
| GGSW::external_product_tmp_bytes(module, &ggsw_out_infos, &ggsw_out_infos, &ggsw_apply_infos),
);
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), rank.into());
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);
ggsw_apply.encrypt_sk(
module,
@@ -309,7 +240,8 @@ where
scratch.borrow(),
);
let ct_rhs_prepared: GGSWPrepared<Vec<u8>, B> = ggsw_apply.prepare_alloc(module, scratch.borrow());
let mut ct_rhs_prepared: GGSWPrepared<Vec<u8>, BE> = GGSWPrepared::alloc_from_infos(module, &ggsw_apply);
ct_rhs_prepared.prepare(module, &ggsw_apply, scratch.borrow());
ggsw_out.external_product_inplace(module, &ct_rhs_prepared, scratch.borrow());

View File

@@ -1,66 +1,35 @@
use poulpy_hal::{
api::{
ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare,
VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace,
VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume,
VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotateInplace, VecZnxSub, VecZnxSubInplace,
VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare,
},
layouts::{Backend, Module, ScalarZnx, ScratchOwned, ZnxViewMut},
oep::{
ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeScalarZnxImpl, TakeSvpPPolImpl,
TakeVecZnxBigImpl, TakeVecZnxDftImpl, TakeVecZnxImpl,
},
api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxFillUniform, VecZnxRotateInplace},
layouts::{Backend, Module, ScalarZnx, Scratch, ScratchOwned, ZnxViewMut},
source::Source,
};
use crate::{
GGSWEncryptSk, GLWEEncryptSk, GLWEExternalProduct, GLWENoise, ScratchTakeCore,
encryption::SIGMA,
layouts::{
GGSW, GGSWCiphertextLayout, GLWE, GLWELayout, GLWEPlaintext, GLWESecret,
prepared::{GGSWPrepared, GLWESecretPrepared, PrepareAlloc},
GGSW, GGSWLayout, GGSWPrepare, GGSWPreparedAlloc, GLWE, GLWELayout, GLWEPlaintext, GLWESecret, GLWESecretPrepare,
GLWESecretPreparedAlloc,
prepared::{GGSWPrepared, GLWESecretPrepared},
},
noise::noise_ggsw_product,
};
#[allow(clippy::too_many_arguments)]
pub fn test_glwe_external_product<B>(module: &Module<B>)
pub fn test_glwe_external_product<BE: Backend>(module: &Module<BE>)
where
Module<B>: VecZnxDftBytesOf
+ VecZnxBigNormalize<B>
+ VecZnxDftApply<B>
+ SvpApplyDftToDftInplace<B>
+ VecZnxIdftApplyConsume<B>
Module<BE>: GGSWEncryptSk<BE>
+ GGSWPrepare<BE>
+ GGSWPreparedAlloc<BE>
+ VecZnxFillUniform
+ VecZnxSubInplace
+ VecZnxAddInplace
+ VecZnxNormalizeInplace<B>
+ VecZnxAddNormal
+ VecZnxNormalize<B>
+ VecZnxSub
+ SvpPrepare<B>
+ SvpPPolBytesOf
+ SvpPPolAlloc<B>
+ VecZnxBigBytesOf
+ VecZnxBigAddInplace<B>
+ VecZnxBigAddSmallInplace<B>
+ VecZnxNormalizeTmpBytes
+ VecZnxAddScalarInplace
+ VecZnxRotateInplace<B>
+ VmpPMatAlloc<B>
+ VmpPrepare<B>
+ VmpApplyDftToDftTmpBytes
+ VmpApplyDftToDft<B>
+ VmpApplyDftToDftAdd<B>,
B: Backend
+ TakeVecZnxDftImpl<B>
+ TakeVecZnxBigImpl<B>
+ TakeSvpPPolImpl<B>
+ ScratchOwnedAllocImpl<B>
+ ScratchOwnedBorrowImpl<B>
+ ScratchAvailableImpl<B>
+ TakeScalarZnxImpl<B>
+ TakeVecZnxImpl<B>,
+ GLWEExternalProduct<BE>
+ GLWEEncryptSk<BE>
+ GLWENoise<BE>
+ VecZnxRotateInplace<BE>
+ GLWESecretPrepare<BE>
+ GLWESecretPreparedAlloc<BE>,
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
{
let base2k: usize = 12;
let k_in: usize = 45;
@@ -87,7 +56,7 @@ where
rank: rank.into(),
};
let ggsw_apply_infos: GGSWCiphertextLayout = GGSWCiphertextLayout {
let ggsw_apply_infos: GGSWLayout = GGSWLayout {
n: n.into(),
base2k: base2k.into(),
k: k_ggsw.into(),
@@ -115,7 +84,7 @@ where
pt_ggsw.raw_mut()[k] = 1; // X^{k}
let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(
let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(
GGSW::encrypt_sk_tmp_bytes(module, &ggsw_apply_infos)
| GLWE::encrypt_sk_tmp_bytes(module, &glwe_in_infos)
| GLWE::external_product_tmp_bytes(module, &glwe_out_infos, &glwe_in_infos, &ggsw_apply_infos),
@@ -123,7 +92,9 @@ where
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), rank.into());
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);
ggsw_apply.encrypt_sk(
module,
@@ -143,7 +114,8 @@ where
scratch.borrow(),
);
let ct_ggsw_prepared: GGSWPrepared<Vec<u8>, B> = ggsw_apply.prepare_alloc(module, scratch.borrow());
let mut ct_ggsw_prepared: GGSWPrepared<Vec<u8>, BE> = GGSWPrepared::alloc_from_infos(module, &ggsw_apply);
ct_ggsw_prepared.prepare(module, &ggsw_apply, scratch.borrow());
glwe_out.external_product(module, &glwe_in, &ct_ggsw_prepared, scratch.borrow());
@@ -176,43 +148,20 @@ where
}
#[allow(clippy::too_many_arguments)]
pub fn test_glwe_external_product_inplace<B>(module: &Module<B>)
pub fn test_glwe_external_product_inplace<BE: Backend>(module: &Module<BE>)
where
Module<B>: VecZnxDftBytesOf
+ VecZnxBigNormalize<B>
+ VecZnxDftApply<B>
+ SvpApplyDftToDftInplace<B>
+ VecZnxIdftApplyConsume<B>
Module<BE>: GGSWEncryptSk<BE>
+ GGSWPrepare<BE>
+ GGSWPreparedAlloc<BE>
+ VecZnxFillUniform
+ VecZnxSubInplace
+ VecZnxAddInplace
+ VecZnxNormalizeInplace<B>
+ VecZnxAddNormal
+ VecZnxNormalize<B>
+ VecZnxSub
+ SvpPrepare<B>
+ SvpPPolBytesOf
+ SvpPPolAlloc<B>
+ VecZnxBigBytesOf
+ VecZnxBigAddInplace<B>
+ VecZnxBigAddSmallInplace<B>
+ VecZnxNormalizeTmpBytes
+ VecZnxAddScalarInplace
+ VecZnxRotateInplace<B>
+ VmpPMatAlloc<B>
+ VmpPrepare<B>
+ VmpApplyDftToDftTmpBytes
+ VmpApplyDftToDft<B>
+ VmpApplyDftToDftAdd<B>,
B: Backend
+ TakeVecZnxDftImpl<B>
+ TakeVecZnxBigImpl<B>
+ TakeSvpPPolImpl<B>
+ ScratchOwnedAllocImpl<B>
+ ScratchOwnedBorrowImpl<B>
+ ScratchAvailableImpl<B>
+ TakeScalarZnxImpl<B>
+ TakeVecZnxImpl<B>,
+ GLWEExternalProduct<BE>
+ GLWEEncryptSk<BE>
+ GLWENoise<BE>
+ VecZnxRotateInplace<BE>
+ GLWESecretPrepare<BE>
+ GLWESecretPreparedAlloc<BE>,
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
{
let base2k: usize = 12;
let k_out: usize = 60;
@@ -231,7 +180,7 @@ where
rank: rank.into(),
};
let ggsw_apply_infos: GGSWCiphertextLayout = GGSWCiphertextLayout {
let ggsw_apply_infos: GGSWLayout = GGSWLayout {
n: n.into(),
base2k: base2k.into(),
k: k_ggsw.into(),
@@ -258,15 +207,17 @@ where
pt_ggsw.raw_mut()[k] = 1; // X^{k}
let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(
let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(
GGSW::encrypt_sk_tmp_bytes(module, &ggsw_apply_infos)
| GLWE::encrypt_sk_tmp_bytes(module, &glwe_out_infos)
| GLWE::external_product_inplace_tmp_bytes(module, &glwe_out_infos, &ggsw_apply_infos),
| GLWE::external_product_tmp_bytes(module, &glwe_out_infos, &glwe_out_infos, &ggsw_apply_infos),
);
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), rank.into());
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);
ggsw_apply.encrypt_sk(
module,
@@ -286,7 +237,8 @@ where
scratch.borrow(),
);
let ct_ggsw_prepared: GGSWPrepared<Vec<u8>, B> = ggsw_apply.prepare_alloc(module, scratch.borrow());
let mut ct_ggsw_prepared: GGSWPrepared<Vec<u8>, BE> = GGSWPrepared::alloc_from_infos(module, &ggsw_apply);
ct_ggsw_prepared.prepare(module, &ggsw_apply, scratch.borrow());
glwe_out.external_product_inplace(module, &ct_ggsw_prepared, scratch.borrow());

View File

@@ -1,6 +1,6 @@
// pub mod automorphism;
pub mod encryption;
// pub mod external_product;
pub mod external_product;
pub mod keyswitch;
// mod conversion;