automorphism tests

This commit is contained in:
Pro7ech
2025-10-20 16:31:25 +02:00
parent b91918dcaf
commit 1e2478852e
10 changed files with 154 additions and 354 deletions

View File

@@ -1,71 +1,35 @@
use poulpy_hal::{
api::{
ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare,
VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxAutomorphismInplace,
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,
},
api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAutomorphism, VecZnxSubScalarInplace},
layouts::{Backend, GaloisElement, Module, Scratch, ScratchOwned},
source::Source,
};
use crate::{
AutomorphismKeyAutomorphism, AutomorphismKeyEncryptSk, GLWEDecrypt, ScratchTakeCore,
encryption::SIGMA,
layouts::{
AutomorphismKey, AutomorphismKeyLayout, GGLWEInfos, GLWEPlaintext, GLWESecret,
prepared::{AutomorphismKeyPrepared, GLWESecretPrepared, Prepare, PrepareAlloc},
AutomorphismKey, AutomorphismKeyLayout, AutomorphismKeyPrepare, AutomorphismKeyPreparedAlloc, GGLWEInfos, GLWEPlaintext,
GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc,
prepared::{AutomorphismKeyPrepared, GLWESecretPrepared},
},
noise::log2_std_noise_gglwe_product,
};
#[allow(clippy::too_many_arguments)]
pub fn test_gglwe_automorphism_key_automorphism<B>(module: &Module<B>)
pub fn test_gglwe_automorphism_key_automorphism<BE: Backend>(module: &Module<BE>)
where
Module<B>: VecZnxDftBytesOf
+ VmpApplyDftToDftTmpBytes
+ VecZnxBigNormalizeTmpBytes
+ VmpApplyDftToDft<B>
+ VmpApplyDftToDftAdd<B>
+ VecZnxDftApply<B>
+ VecZnxIdftApplyConsume<B>
+ VecZnxBigAddSmallInplace<B>
+ VecZnxBigNormalize<B>
Module<BE>: AutomorphismKeyEncryptSk<BE>
+ AutomorphismKeyPreparedAlloc<BE>
+ AutomorphismKeyPrepare<BE>
+ AutomorphismKeyAutomorphism<BE>
+ VecZnxAutomorphism
+ VecZnxAutomorphismInplace<B>
+ SvpPPolBytesOf
+ VecZnxDftBytesOf
+ VecZnxNormalizeTmpBytes
+ VmpPMatAlloc<B>
+ VmpPrepare<B>
+ SvpPrepare<B>
+ SvpApplyDftToDftInplace<B>
+ VecZnxAddScalarInplace
+ VecZnxFillUniform
+ VecZnxSubInplace
+ VecZnxAddInplace
+ VecZnxNormalizeInplace<B>
+ VecZnxAddNormal
+ VecZnxNormalize<B>
+ VecZnxSub
+ VecZnxSwitchRing
+ SvpPPolAlloc<B>
+ VecZnxBigAddInplace<B>
+ VecZnxSubScalarInplace,
B: Backend
+ ScratchOwnedAllocImpl<B>
+ ScratchOwnedBorrowImpl<B>
+ ScratchAvailableImpl<B>
+ TakeScalarZnxImpl<B>
+ TakeVecZnxDftImpl<B>
+ TakeVecZnxImpl<B>
+ TakeSvpPPolImpl<B>
+ TakeVecZnxBigImpl<B>,
+ GaloisElement
+ VecZnxSubScalarInplace
+ GLWESecretPrepare<BE>
+ GLWESecretPreparedAlloc<BE>
+ GLWEDecrypt<BE>,
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
{
let base2k: usize = 12;
let k_in: usize = 60;
@@ -119,7 +83,7 @@ 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(
AutomorphismKey::encrypt_sk_tmp_bytes(module, &auto_key_in_infos)
| AutomorphismKey::encrypt_sk_tmp_bytes(module, &auto_key_apply_infos)
| AutomorphismKey::automorphism_tmp_bytes(
@@ -153,7 +117,7 @@ where
scratch.borrow(),
);
let mut auto_key_apply_prepared: AutomorphismKeyPrepared<Vec<u8>, B> =
let mut auto_key_apply_prepared: AutomorphismKeyPrepared<Vec<u8>, BE> =
AutomorphismKeyPrepared::alloc_from_infos(module, &auto_key_apply_infos);
auto_key_apply_prepared.prepare(module, &auto_key_apply, scratch.borrow());
@@ -180,7 +144,8 @@ where
);
}
let sk_auto_dft: GLWESecretPrepared<Vec<u8>, B> = sk_auto.prepare_alloc(module, scratch.borrow());
let mut sk_auto_dft: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc_from_infos(module, &sk_auto);
sk_auto_dft.prepare(module, &sk_auto);
(0..auto_key_out.rank_in().into()).for_each(|col_i| {
(0..auto_key_out.dnum().into()).for_each(|row_i| {
@@ -222,61 +187,20 @@ where
}
#[allow(clippy::too_many_arguments)]
pub fn test_gglwe_automorphism_key_automorphism_inplace<B>(module: &Module<B>)
pub fn test_gglwe_automorphism_key_automorphism_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>
+ VecZnxDftBytesOf
+ VmpApplyDftToDftTmpBytes
+ VecZnxBigNormalizeTmpBytes
+ VmpApplyDftToDftTmpBytes
+ VmpApplyDftToDft<B>
+ VmpApplyDftToDftAdd<B>
+ VecZnxDftApply<B>
+ VecZnxIdftApplyConsume<B>
+ VecZnxBigAddSmallInplace<B>
+ VecZnxBigNormalize<B>
Module<BE>: AutomorphismKeyEncryptSk<BE>
+ AutomorphismKeyPreparedAlloc<BE>
+ AutomorphismKeyPrepare<BE>
+ AutomorphismKeyAutomorphism<BE>
+ VecZnxAutomorphism
+ VecZnxSwitchRing
+ VecZnxAddScalarInplace
+ VecZnxAutomorphism
+ VecZnxAutomorphismInplace<B>
+ VecZnxDftBytesOf
+ VecZnxBigBytesOf
+ VecZnxDftApply<B>
+ SvpApplyDftToDftInplace<B>
+ VecZnxIdftApplyConsume<B>
+ VecZnxBigAddInplace<B>
+ VecZnxBigAddSmallInplace<B>
+ VecZnxBigNormalize<B>
+ VecZnxNormalizeTmpBytes
+ GaloisElement
+ VecZnxSubScalarInplace
+ VecZnxCopy
+ VmpPMatAlloc<B>
+ VmpPrepare<B>,
B: Backend
+ ScratchOwnedAllocImpl<B>
+ ScratchOwnedBorrowImpl<B>
+ ScratchAvailableImpl<B>
+ TakeScalarZnxImpl<B>
+ TakeVecZnxDftImpl<B>
+ TakeVecZnxImpl<B>
+ TakeSvpPPolImpl<B>
+ TakeVecZnxBigImpl<B>,
+ GLWESecretPrepare<BE>
+ GLWESecretPreparedAlloc<BE>
+ GLWEDecrypt<BE>,
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
{
let base2k: usize = 12;
let k_in: usize = 60;
@@ -318,10 +242,10 @@ 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(
AutomorphismKey::encrypt_sk_tmp_bytes(module, &auto_key)
| AutomorphismKey::encrypt_sk_tmp_bytes(module, &auto_key_apply)
| AutomorphismKey::automorphism_inplace_tmp_bytes(module, &auto_key, &auto_key_apply),
| AutomorphismKey::automorphism_tmp_bytes(module, &auto_key, &auto_key, &auto_key_apply),
);
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&auto_key);
@@ -347,7 +271,7 @@ where
scratch.borrow(),
);
let mut auto_key_apply_prepared: AutomorphismKeyPrepared<Vec<u8>, B> =
let mut auto_key_apply_prepared: AutomorphismKeyPrepared<Vec<u8>, BE> =
AutomorphismKeyPrepared::alloc_from_infos(module, &auto_key_apply_layout);
auto_key_apply_prepared.prepare(module, &auto_key_apply, scratch.borrow());
@@ -370,7 +294,8 @@ where
);
}
let sk_auto_dft: GLWESecretPrepared<Vec<u8>, B> = sk_auto.prepare_alloc(module, scratch.borrow());
let mut sk_auto_dft: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc_from_infos(module, &sk_auto);
sk_auto_dft.prepare(module, &sk_auto);
(0..auto_key.rank_in().into()).for_each(|col_i| {
(0..auto_key.dnum().into()).for_each(|row_i| {

View File

@@ -1,79 +1,36 @@
use poulpy_hal::{
api::{
ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf,
SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxAutomorphismInplace,
VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAlloc, VecZnxBigBytesOf, VecZnxBigNormalize,
VecZnxBigNormalizeTmpBytes, VecZnxCopy, VecZnxDftAddInplace, VecZnxDftAlloc, VecZnxDftApply, VecZnxDftBytesOf,
VecZnxDftCopy, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalize, VecZnxNormalizeInplace,
VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd,
VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare,
},
layouts::{Backend, Module, ScalarZnx, ScratchOwned},
oep::{
ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeScalarZnxImpl, TakeSvpPPolImpl,
TakeVecZnxBigImpl, TakeVecZnxDftImpl, TakeVecZnxImpl, VecZnxBigAllocBytesImpl, VecZnxDftAllocBytesImpl,
},
api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAutomorphismInplace},
layouts::{Backend, Module, ScalarZnx, Scratch, ScratchOwned},
source::Source,
};
use crate::{
AutomorphismKeyEncryptSk, GGSWAutomorphism, GGSWEncryptSk, GGSWNoise, ScratchTakeCore, TensorKeyEncryptSk,
encryption::SIGMA,
layouts::{
AutomorphismKey, GGSW, GGSWCiphertextLayout, GLWESecret, TensorKey, TensorKeyLayout,
prepared::{AutomorphismKeyPrepared, GLWESecretPrepared, Prepare, PrepareAlloc, TensorKeyPrepared},
AutomorphismKey, AutomorphismKeyPrepare, AutomorphismKeyPreparedAlloc, GGSW, GGSWLayout, GLWESecret, GLWESecretPrepare,
GLWESecretPreparedAlloc, TensorKey, TensorKeyLayout, TensorKeyPrepare, TensorKeyPreparedAlloc,
prepared::{AutomorphismKeyPrepared, GLWESecretPrepared, TensorKeyPrepared},
},
noise::noise_ggsw_keyswitch,
};
pub fn test_ggsw_automorphism<B>(module: &Module<B>)
pub fn test_ggsw_automorphism<BE: Backend>(module: &Module<BE>)
where
Module<B>: VecZnxDftBytesOf
+ VecZnxBigBytesOf
+ VecZnxDftApply<B>
+ SvpApplyDftToDftInplace<B>
+ VecZnxIdftApplyConsume<B>
+ VecZnxBigAddInplace<B>
+ VecZnxBigAddSmallInplace<B>
+ VecZnxBigNormalize<B>
+ VecZnxNormalizeTmpBytes
+ VecZnxBigAlloc<B>
+ VecZnxDftAlloc<B>
+ VecZnxBigNormalizeTmpBytes
+ VecZnxIdftApplyTmpA<B>
+ SvpPrepare<B>
+ SvpPPolBytesOf
+ SvpPPolAlloc<B>
+ VecZnxAddScalarInplace
+ VecZnxCopy
+ VecZnxSubInplace
+ VmpPMatAlloc<B>
+ VmpPrepare<B>
+ VmpApplyDftToDftTmpBytes
+ VmpApplyDftToDft<B>
+ VmpApplyDftToDftAdd<B>
+ VecZnxDftCopy<B>
+ VecZnxDftAddInplace<B>
+ VecZnxFillUniform
+ VecZnxAddInplace
+ VecZnxNormalizeInplace<B>
+ VecZnxAddNormal
+ VecZnxNormalize<B>
+ VecZnxSub
+ SvpApplyDftToDft<B>
+ VecZnxSwitchRing
+ VecZnxAutomorphismInplace<B>
+ VecZnxAutomorphism,
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>
+ AutomorphismKeyEncryptSk<BE>
+ AutomorphismKeyPreparedAlloc<BE>
+ AutomorphismKeyPrepare<BE>
+ GGSWAutomorphism<BE>
+ TensorKeyPrepare<BE>
+ TensorKeyPreparedAlloc<BE>
+ TensorKeyEncryptSk<BE>
+ GLWESecretPrepare<BE>
+ GLWESecretPreparedAlloc<BE>
+ VecZnxAutomorphismInplace<BE>
+ GGSWNoise<BE>,
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
{
let base2k: usize = 12;
let k_in: usize = 54;
@@ -92,7 +49,7 @@ where
let dsize_in: usize = 1;
let ggsw_in_layout: GGSWCiphertextLayout = GGSWCiphertextLayout {
let ggsw_in_layout: GGSWLayout = GGSWLayout {
n: n.into(),
base2k: base2k.into(),
k: k_in.into(),
@@ -101,7 +58,7 @@ where
rank: rank.into(),
};
let ggsw_out_layout: GGSWCiphertextLayout = GGSWCiphertextLayout {
let ggsw_out_layout: GGSWLayout = GGSWLayout {
n: n.into(),
base2k: base2k.into(),
k: k_out.into(),
@@ -138,7 +95,7 @@ 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(
GGSW::encrypt_sk_tmp_bytes(module, &ct_in)
| AutomorphismKey::encrypt_sk_tmp_bytes(module, &auto_key)
| TensorKey::encrypt_sk_tmp_bytes(module, &tensor_key)
@@ -149,7 +106,9 @@ where
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&ct_out);
sk.fill_ternary_prob(var_xs, &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_from_infos(module, &sk);
sk_prepared.prepare(module, &sk);
auto_key.encrypt_sk(
module,
@@ -178,11 +137,12 @@ where
scratch.borrow(),
);
let mut auto_key_prepared: AutomorphismKeyPrepared<Vec<u8>, B> =
let mut auto_key_prepared: AutomorphismKeyPrepared<Vec<u8>, BE> =
AutomorphismKeyPrepared::alloc_from_infos(module, &auto_key_layout);
auto_key_prepared.prepare(module, &auto_key, scratch.borrow());
let mut tsk_prepared: TensorKeyPrepared<Vec<u8>, B> = TensorKeyPrepared::alloc_from_infos(module, &tensor_key_layout);
let mut tsk_prepared: TensorKeyPrepared<Vec<u8>, BE> =
TensorKeyPrepared::alloc_from_infos(module, &tensor_key_layout);
tsk_prepared.prepare(module, &tensor_key, scratch.borrow());
ct_out.automorphism(
@@ -217,56 +177,22 @@ where
}
#[allow(clippy::too_many_arguments)]
pub fn test_ggsw_automorphism_inplace<B>(module: &Module<B>)
pub fn test_ggsw_automorphism_inplace<BE: Backend>(module: &Module<BE>)
where
Module<B>: VecZnxDftBytesOf
+ VecZnxBigBytesOf
+ VecZnxDftApply<B>
+ SvpApplyDftToDftInplace<B>
+ VecZnxIdftApplyConsume<B>
+ VecZnxBigAddInplace<B>
+ VecZnxBigAddSmallInplace<B>
+ VecZnxBigNormalize<B>
+ VecZnxNormalizeTmpBytes
+ VecZnxBigAlloc<B>
+ VecZnxDftAlloc<B>
+ VecZnxBigNormalizeTmpBytes
+ VecZnxIdftApplyTmpA<B>
+ SvpPrepare<B>
+ SvpPPolBytesOf
+ SvpPPolAlloc<B>
+ VecZnxAddScalarInplace
+ VecZnxCopy
+ VecZnxSubInplace
+ VmpPMatAlloc<B>
+ VmpPrepare<B>
+ VmpApplyDftToDftTmpBytes
+ VmpApplyDftToDft<B>
+ VmpApplyDftToDftAdd<B>
+ VecZnxBigAddSmallInplace<B>
+ VecZnxDftCopy<B>
+ VecZnxDftAddInplace<B>
+ VecZnxAddInplace
+ VecZnxNormalizeInplace<B>
+ VecZnxAddNormal
+ VecZnxNormalize<B>
+ VecZnxSub
+ VecZnxFillUniform
+ SvpApplyDftToDft<B>
+ VecZnxSwitchRing
+ VecZnxAutomorphismInplace<B>
+ VecZnxAutomorphism,
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>
+ AutomorphismKeyEncryptSk<BE>
+ AutomorphismKeyPreparedAlloc<BE>
+ AutomorphismKeyPrepare<BE>
+ GGSWAutomorphism<BE>
+ TensorKeyPrepare<BE>
+ TensorKeyPreparedAlloc<BE>
+ TensorKeyEncryptSk<BE>
+ GLWESecretPrepare<BE>
+ GLWESecretPreparedAlloc<BE>
+ VecZnxAutomorphismInplace<BE>
+ GGSWNoise<BE>,
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
{
let base2k: usize = 12;
let k_out: usize = 54;
@@ -282,7 +208,7 @@ where
let dnum_in: usize = k_out.div_euclid(base2k * di);
let dsize_in: usize = 1;
let ggsw_out_layout: GGSWCiphertextLayout = GGSWCiphertextLayout {
let ggsw_out_layout: GGSWLayout = GGSWLayout {
n: n.into(),
base2k: base2k.into(),
k: k_out.into(),
@@ -318,18 +244,20 @@ 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(
GGSW::encrypt_sk_tmp_bytes(module, &ct)
| AutomorphismKey::encrypt_sk_tmp_bytes(module, &auto_key)
| TensorKey::encrypt_sk_tmp_bytes(module, &tensor_key)
| GGSW::automorphism_inplace_tmp_bytes(module, &ct, &auto_key, &tensor_key),
| GGSW::automorphism_tmp_bytes(module, &ct, &ct, &auto_key, &tensor_key),
);
let var_xs: f64 = 0.5;
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&ct);
sk.fill_ternary_prob(var_xs, &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_from_infos(module, &sk);
sk_prepared.prepare(module, &sk);
auto_key.encrypt_sk(
module,
@@ -358,11 +286,12 @@ where
scratch.borrow(),
);
let mut auto_key_prepared: AutomorphismKeyPrepared<Vec<u8>, B> =
let mut auto_key_prepared: AutomorphismKeyPrepared<Vec<u8>, BE> =
AutomorphismKeyPrepared::alloc_from_infos(module, &auto_key_layout);
auto_key_prepared.prepare(module, &auto_key, scratch.borrow());
let mut tsk_prepared: TensorKeyPrepared<Vec<u8>, B> = TensorKeyPrepared::alloc_from_infos(module, &tensor_key_layout);
let mut tsk_prepared: TensorKeyPrepared<Vec<u8>, BE> =
TensorKeyPrepared::alloc_from_infos(module, &tensor_key_layout);
tsk_prepared.prepare(module, &tensor_key, scratch.borrow());
ct.automorphism_inplace(module, &auto_key_prepared, &tsk_prepared, scratch.borrow());

View File

@@ -1,69 +1,35 @@
use poulpy_hal::{
api::{
ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf, SvpPrepare,
VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxAutomorphismInplace,
VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes,
VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace,
VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd,
VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare,
},
layouts::{Backend, Module, ScratchOwned},
oep::{
ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeScalarZnxImpl, TakeSvpPPolImpl,
TakeVecZnxBigImpl, TakeVecZnxDftImpl, TakeVecZnxImpl,
},
api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxAutomorphismInplace, VecZnxFillUniform},
layouts::{Backend, Module, Scratch, ScratchOwned},
source::Source,
};
use crate::{
AutomorphismKeyEncryptSk, GLWEAutomorphism, GLWEDecrypt, GLWEEncryptSk, GLWENoise, ScratchTakeCore,
encryption::SIGMA,
layouts::{
AutomorphismKey, AutomorphismKeyLayout, GLWE, GLWELayout, GLWEPlaintext, GLWESecret,
prepared::{AutomorphismKeyPrepared, GLWESecretPrepared, Prepare, PrepareAlloc},
AutomorphismKey, AutomorphismKeyLayout, AutomorphismKeyPrepare, AutomorphismKeyPreparedAlloc, GLWE, GLWELayout,
GLWEPlaintext, GLWESecret, GLWESecretPrepare, GLWESecretPreparedAlloc,
prepared::{AutomorphismKeyPrepared, GLWESecretPrepared},
},
noise::log2_std_noise_gglwe_product,
};
pub fn test_glwe_automorphism<B>(module: &Module<B>)
pub fn test_glwe_automorphism<BE: Backend>(module: &Module<BE>)
where
Module<B>: VecZnxDftBytesOf
+ VecZnxBigNormalize<B>
+ VecZnxDftApply<B>
+ SvpApplyDftToDftInplace<B>
+ VecZnxIdftApplyConsume<B>
+ VecZnxNormalizeTmpBytes
Module<BE>: GLWEEncryptSk<BE>
+ GLWESecretPrepare<BE>
+ GLWESecretPreparedAlloc<BE>
+ VecZnxFillUniform
+ VecZnxSubInplace
+ VecZnxAddInplace
+ VecZnxNormalizeInplace<B>
+ VecZnxAddNormal
+ VecZnxNormalize<B>
+ VecZnxSub
+ SvpPrepare<B>
+ SvpPPolBytesOf
+ SvpPPolAlloc<B>
+ VecZnxBigBytesOf
+ VecZnxBigAddInplace<B>
+ VecZnxBigAddSmallInplace<B>
+ VmpApplyDftToDftTmpBytes
+ VecZnxBigNormalizeTmpBytes
+ VmpApplyDftToDft<B>
+ VmpApplyDftToDftAdd<B>
+ VecZnxAutomorphism
+ VecZnxSwitchRing
+ VecZnxAddScalarInplace
+ VecZnxAutomorphismInplace<B>
+ VmpPMatAlloc<B>
+ VmpPrepare<B>,
B: Backend
+ TakeVecZnxDftImpl<B>
+ TakeVecZnxBigImpl<B>
+ TakeSvpPPolImpl<B>
+ ScratchOwnedAllocImpl<B>
+ ScratchOwnedBorrowImpl<B>
+ ScratchAvailableImpl<B>
+ TakeScalarZnxImpl<B>
+ TakeVecZnxImpl<B>,
+ GLWEDecrypt<BE>
+ GLWEAutomorphism<BE>
+ AutomorphismKeyEncryptSk<BE>
+ AutomorphismKeyPrepare<BE>
+ AutomorphismKeyPreparedAlloc<BE>
+ GLWENoise<BE>
+ VecZnxAutomorphismInplace<BE>,
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
{
let base2k: usize = 12;
let k_in: usize = 60;
@@ -111,7 +77,7 @@ where
module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa);
let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(
let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(
AutomorphismKey::encrypt_sk_tmp_bytes(module, &autokey)
| GLWE::decrypt_tmp_bytes(module, &ct_out)
| GLWE::encrypt_sk_tmp_bytes(module, &ct_in)
@@ -120,7 +86,9 @@ where
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&ct_out);
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_from_infos(module, &sk);
sk_prepared.prepare(module, &sk);
autokey.encrypt_sk(
module,
@@ -140,7 +108,7 @@ where
scratch.borrow(),
);
let mut autokey_prepared: AutomorphismKeyPrepared<Vec<u8>, B> =
let mut autokey_prepared: AutomorphismKeyPrepared<Vec<u8>, BE> =
AutomorphismKeyPrepared::alloc_from_infos(module, &autokey_infos);
autokey_prepared.prepare(module, &autokey, scratch.borrow());
@@ -167,46 +135,21 @@ where
}
#[allow(clippy::too_many_arguments)]
pub fn test_glwe_automorphism_inplace<B>(module: &Module<B>)
pub fn test_glwe_automorphism_inplace<BE: Backend>(module: &Module<BE>)
where
Module<B>: VecZnxDftBytesOf
+ VecZnxBigNormalize<B>
+ VecZnxDftApply<B>
+ SvpApplyDftToDftInplace<B>
+ VecZnxIdftApplyConsume<B>
+ VecZnxNormalizeTmpBytes
Module<BE>: GLWEEncryptSk<BE>
+ GLWESecretPrepare<BE>
+ GLWESecretPreparedAlloc<BE>
+ VecZnxFillUniform
+ VecZnxSubInplace
+ VecZnxAddInplace
+ VecZnxNormalizeInplace<B>
+ VecZnxAddNormal
+ VecZnxNormalize<B>
+ VecZnxSub
+ SvpPrepare<B>
+ SvpPPolBytesOf
+ SvpPPolAlloc<B>
+ VecZnxBigBytesOf
+ VecZnxBigAddInplace<B>
+ VecZnxBigAddSmallInplace<B>
+ VmpApplyDftToDftTmpBytes
+ VecZnxBigNormalizeTmpBytes
+ VmpApplyDftToDft<B>
+ VmpApplyDftToDftAdd<B>
+ VecZnxAutomorphism
+ VecZnxSwitchRing
+ VecZnxAddScalarInplace
+ VecZnxAutomorphismInplace<B>
+ VmpPMatAlloc<B>
+ VmpPrepare<B>,
B: Backend
+ TakeVecZnxDftImpl<B>
+ TakeVecZnxBigImpl<B>
+ TakeSvpPPolImpl<B>
+ ScratchOwnedAllocImpl<B>
+ ScratchOwnedBorrowImpl<B>
+ ScratchAvailableImpl<B>
+ TakeScalarZnxImpl<B>
+ TakeVecZnxImpl<B>,
+ GLWEDecrypt<BE>
+ GLWEAutomorphism<BE>
+ AutomorphismKeyEncryptSk<BE>
+ AutomorphismKeyPrepare<BE>
+ AutomorphismKeyPreparedAlloc<BE>
+ GLWENoise<BE>
+ VecZnxAutomorphismInplace<BE>,
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
{
let base2k: usize = 12;
let k_out: usize = 60;
@@ -245,16 +188,18 @@ where
module.vec_znx_fill_uniform(base2k, &mut pt_want.data, 0, &mut source_xa);
let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(
let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(
AutomorphismKey::encrypt_sk_tmp_bytes(module, &autokey)
| GLWE::decrypt_tmp_bytes(module, &ct)
| GLWE::encrypt_sk_tmp_bytes(module, &ct)
| GLWE::automorphism_inplace_tmp_bytes(module, &ct, &autokey),
| GLWE::automorphism_tmp_bytes(module, &ct, &ct, &autokey),
);
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&ct);
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_from_infos(module, &sk);
sk_prepared.prepare(module, &sk);
autokey.encrypt_sk(
module,
@@ -274,7 +219,7 @@ where
scratch.borrow(),
);
let mut autokey_prepared: AutomorphismKeyPrepared<Vec<u8>, B> =
let mut autokey_prepared: AutomorphismKeyPrepared<Vec<u8>, BE> =
AutomorphismKeyPrepared::alloc_from_infos(module, &autokey);
autokey_prepared.prepare(module, &autokey, scratch.borrow());

View File

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