mirror of
https://github.com/arnaucube/poulpy.git
synced 2026-02-10 13:16:44 +01:00
wip test glwe encryption
This commit is contained in:
@@ -1,60 +1,38 @@
|
||||
use poulpy_hal::{
|
||||
api::{
|
||||
ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolBytesOf,
|
||||
SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxBigAddInplace, VecZnxBigAddNormal, VecZnxBigAddSmallInplace,
|
||||
VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxCopy, VecZnxDftAlloc, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform,
|
||||
VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace,
|
||||
},
|
||||
layouts::{Backend, Module, ScratchOwned},
|
||||
oep::{
|
||||
ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeScalarZnxImpl, TakeSvpPPolImpl,
|
||||
TakeVecZnxBigImpl, TakeVecZnxDftImpl, TakeVecZnxImpl,
|
||||
ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc,
|
||||
SvpPPolBytesOf, SvpPrepare, VecZnxAddInplace, VecZnxAddNormal, VecZnxBigAddInplace, VecZnxBigAddNormal,
|
||||
VecZnxBigAddSmallInplace, VecZnxBigBytesOf, VecZnxBigNormalize, VecZnxCopy, VecZnxDftAlloc, VecZnxDftApply,
|
||||
VecZnxDftBytesOf, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxNormalize, VecZnxNormalizeInplace,
|
||||
VecZnxNormalizeTmpBytes, VecZnxSub, VecZnxSubInplace,
|
||||
},
|
||||
layouts::{Backend, Module, Scratch, ScratchOwned},
|
||||
source::Source,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
GLWEEncryptSk, GLWESub, ScratchTakeCore,
|
||||
decryption::GLWEDecrypt,
|
||||
encryption::SIGMA,
|
||||
layouts::{
|
||||
GLWE, GLWELayout, GLWEPlaintext, GLWEPlaintextLayout, GLWEPublicKey, GLWESecret, LWEInfos,
|
||||
compressed::{Decompress, GLWECompressed},
|
||||
prepared::{GLWEPublicKeyPrepared, GLWESecretPrepared, PrepareAlloc},
|
||||
GLWE, GLWEAlloc, GLWELayout, GLWEPlaintext, GLWEPlaintextLayout, GLWEPublicKey, GLWESecret, GLWESecretPrepare,
|
||||
GLWESecretPreparedAlloc, LWEInfos,
|
||||
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
|
||||
Module<B>: VecZnxDftBytesOf
|
||||
+ VecZnxBigBytesOf
|
||||
+ VecZnxDftApply<B>
|
||||
+ SvpApplyDftToDftInplace<B>
|
||||
+ VecZnxIdftApplyConsume<B>
|
||||
+ VecZnxBigAddInplace<B>
|
||||
+ VecZnxBigAddSmallInplace<B>
|
||||
+ VecZnxBigNormalize<B>
|
||||
+ VecZnxNormalizeTmpBytes
|
||||
+ SvpPrepare<B>
|
||||
+ SvpPPolBytesOf
|
||||
+ SvpPPolAlloc<B>
|
||||
+ SvpApplyDftToDft<B>
|
||||
+ VecZnxBigAddNormal<B>
|
||||
Module<B>: GLWEAlloc
|
||||
+ GLWEEncryptSk<B>
|
||||
+ GLWEDecrypt<B>
|
||||
+ GLWESecretPreparedAlloc<B>
|
||||
+ GLWESecretPrepare<B>
|
||||
+ VecZnxFillUniform
|
||||
+ VecZnxSubInplace
|
||||
+ VecZnxAddInplace
|
||||
+ VecZnxNormalizeInplace<B>
|
||||
+ VecZnxAddNormal
|
||||
+ VecZnxNormalize<B>
|
||||
+ VecZnxSub,
|
||||
B: Backend
|
||||
+ TakeVecZnxDftImpl<B>
|
||||
+ TakeVecZnxBigImpl<B>
|
||||
+ TakeSvpPPolImpl<B>
|
||||
+ ScratchOwnedAllocImpl<B>
|
||||
+ ScratchOwnedBorrowImpl<B>
|
||||
+ ScratchAvailableImpl<B>
|
||||
+ TakeScalarZnxImpl<B>
|
||||
+ TakeVecZnxImpl<B>,
|
||||
+ GLWESub,
|
||||
ScratchOwned<B>: ScratchOwnedAlloc<B> + ScratchOwnedBorrow<B>,
|
||||
Scratch<B>: ScratchAvailable + ScratchTakeCore<B>,
|
||||
{
|
||||
let base2k: usize = 8;
|
||||
let k_ct: usize = 54;
|
||||
@@ -76,21 +54,22 @@ where
|
||||
k: k_pt.into(),
|
||||
};
|
||||
|
||||
let mut ct: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_infos);
|
||||
let mut pt_want: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&pt_infos);
|
||||
let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&pt_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(module, &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_xe: Source = Source::new([0u8; 32]);
|
||||
let mut source_xa: Source = Source::new([0u8; 32]);
|
||||
|
||||
let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(
|
||||
GLWE::encrypt_sk_tmp_bytes(module, &glwe_infos) | GLWE::decrypt_tmp_bytes(module, &glwe_infos),
|
||||
);
|
||||
let mut scratch: ScratchOwned<B> =
|
||||
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);
|
||||
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);
|
||||
|
||||
@@ -105,7 +84,7 @@ where
|
||||
|
||||
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_want: f64 = SIGMA;
|
||||
@@ -138,15 +117,6 @@ where
|
||||
+ VecZnxNormalize<B>
|
||||
+ VecZnxSub
|
||||
+ VecZnxCopy,
|
||||
B: Backend
|
||||
+ TakeVecZnxDftImpl<B>
|
||||
+ TakeVecZnxBigImpl<B>
|
||||
+ TakeSvpPPolImpl<B>
|
||||
+ ScratchOwnedAllocImpl<B>
|
||||
+ ScratchOwnedBorrowImpl<B>
|
||||
+ ScratchAvailableImpl<B>
|
||||
+ TakeScalarZnxImpl<B>
|
||||
+ TakeVecZnxImpl<B>,
|
||||
{
|
||||
let base2k: usize = 8;
|
||||
let k_ct: usize = 54;
|
||||
@@ -239,15 +209,6 @@ where
|
||||
+ 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 k_ct: usize = 54;
|
||||
@@ -268,9 +229,8 @@ where
|
||||
let mut source_xe: Source = Source::new([1u8; 32]);
|
||||
let mut source_xa: Source = Source::new([0u8; 32]);
|
||||
|
||||
let mut scratch: ScratchOwned<B> = ScratchOwned::alloc(
|
||||
GLWE::decrypt_tmp_bytes(module, &glwe_infos) | GLWE::encrypt_sk_tmp_bytes(module, &glwe_infos),
|
||||
);
|
||||
let mut scratch: ScratchOwned<B> =
|
||||
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);
|
||||
sk.fill_ternary_prob(0.5, &mut source_xs);
|
||||
@@ -316,15 +276,6 @@ where
|
||||
+ VecZnxDftAlloc<B>
|
||||
+ SvpApplyDftToDft<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 k_ct: usize = 54;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
mod gglwe_atk;
|
||||
mod gglwe_ct;
|
||||
mod ggsw_ct;
|
||||
// mod gglwe_atk;
|
||||
// mod gglwe_ct;
|
||||
// mod ggsw_ct;
|
||||
mod glwe_ct;
|
||||
mod glwe_tsk;
|
||||
// mod glwe_tsk;
|
||||
|
||||
pub use gglwe_atk::*;
|
||||
pub use gglwe_ct::*;
|
||||
pub use ggsw_ct::*;
|
||||
// pub use gglwe_atk::*;
|
||||
// pub use gglwe_ct::*;
|
||||
// pub use ggsw_ct::*;
|
||||
pub use glwe_ct::*;
|
||||
pub use glwe_tsk::*;
|
||||
// pub use glwe_tsk::*;
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
pub mod automorphism;
|
||||
// pub mod automorphism;
|
||||
pub mod encryption;
|
||||
pub mod external_product;
|
||||
pub mod keyswitch;
|
||||
// pub mod external_product;
|
||||
// pub mod keyswitch;
|
||||
|
||||
mod conversion;
|
||||
mod packing;
|
||||
mod trace;
|
||||
// mod conversion;
|
||||
// mod packing;
|
||||
// mod trace;
|
||||
|
||||
pub use conversion::*;
|
||||
pub use packing::*;
|
||||
pub use trace::*;
|
||||
// pub use conversion::*;
|
||||
// pub use packing::*;
|
||||
// pub use trace::*;
|
||||
|
||||
Reference in New Issue
Block a user