mirror of
https://github.com/arnaucube/poulpy.git
synced 2026-02-10 05:06:44 +01:00
Distinguish between gglwe_to_ggsw key and tensor_key + update key repreentation
This commit is contained in:
@@ -5,12 +5,12 @@ use poulpy_hal::{
|
||||
};
|
||||
|
||||
use crate::{
|
||||
GGSWAutomorphism, GGSWEncryptSk, GGSWNoise, GLWEAutomorphismKeyEncryptSk, GLWETensorKeyEncryptSk, ScratchTakeCore,
|
||||
GGLWEToGGSWKeyEncryptSk, GGSWAutomorphism, GGSWEncryptSk, GGSWNoise, GLWEAutomorphismKeyEncryptSk, ScratchTakeCore,
|
||||
encryption::SIGMA,
|
||||
layouts::{
|
||||
GGSW, GGSWLayout, GLWEAutomorphismKey, GLWEAutomorphismKeyPreparedFactory, GLWESecret, GLWESecretPreparedFactory,
|
||||
GLWETensorKey, GLWETensorKeyLayout, GLWETensorKeyPreparedFactory,
|
||||
prepared::{GLWEAutomorphismKeyPrepared, GLWESecretPrepared, GLWETensorKeyPrepared},
|
||||
GGLWEToGGSWKey, GGLWEToGGSWKeyLayout, GGLWEToGGSWKeyPreparedFactory, GGSW, GGSWLayout, GLWEAutomorphismKey,
|
||||
GLWEAutomorphismKeyPreparedFactory, GLWESecret, GLWESecretPreparedFactory,
|
||||
prepared::{GGLWEToGGSWKeyPrepared, GLWEAutomorphismKeyPrepared, GLWESecretPrepared},
|
||||
},
|
||||
noise::noise_ggsw_keyswitch,
|
||||
};
|
||||
@@ -21,8 +21,8 @@ where
|
||||
+ GLWEAutomorphismKeyEncryptSk<BE>
|
||||
+ GLWEAutomorphismKeyPreparedFactory<BE>
|
||||
+ GGSWAutomorphism<BE>
|
||||
+ GLWETensorKeyPreparedFactory<BE>
|
||||
+ GLWETensorKeyEncryptSk<BE>
|
||||
+ GGLWEToGGSWKeyPreparedFactory<BE>
|
||||
+ GGLWEToGGSWKeyEncryptSk<BE>
|
||||
+ GLWESecretPreparedFactory<BE>
|
||||
+ VecZnxAutomorphismInplace<BE>
|
||||
+ GGSWNoise<BE>,
|
||||
@@ -64,7 +64,7 @@ where
|
||||
rank: rank.into(),
|
||||
};
|
||||
|
||||
let tensor_key_layout: GLWETensorKeyLayout = GLWETensorKeyLayout {
|
||||
let tsk_layout: GGLWEToGGSWKeyLayout = GGLWEToGGSWKeyLayout {
|
||||
n: n.into(),
|
||||
base2k: base2k.into(),
|
||||
k: k_tsk.into(),
|
||||
@@ -73,7 +73,7 @@ where
|
||||
rank: rank.into(),
|
||||
};
|
||||
|
||||
let auto_key_layout: GLWETensorKeyLayout = GLWETensorKeyLayout {
|
||||
let auto_key_layout: GGLWEToGGSWKeyLayout = GGLWEToGGSWKeyLayout {
|
||||
n: n.into(),
|
||||
base2k: base2k.into(),
|
||||
k: k_ksk.into(),
|
||||
@@ -84,7 +84,7 @@ where
|
||||
|
||||
let mut ct_in: GGSW<Vec<u8>> = GGSW::alloc_from_infos(&ggsw_in_layout);
|
||||
let mut ct_out: GGSW<Vec<u8>> = GGSW::alloc_from_infos(&ggsw_out_layout);
|
||||
let mut tensor_key: GLWETensorKey<Vec<u8>> = GLWETensorKey::alloc_from_infos(&tensor_key_layout);
|
||||
let mut tsk: GGLWEToGGSWKey<Vec<u8>> = GGLWEToGGSWKey::alloc_from_infos(&tsk_layout);
|
||||
let mut auto_key: GLWEAutomorphismKey<Vec<u8>> = GLWEAutomorphismKey::alloc_from_infos(&auto_key_layout);
|
||||
let mut pt_scalar: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n, 1);
|
||||
|
||||
@@ -95,8 +95,8 @@ where
|
||||
let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(
|
||||
GGSW::encrypt_sk_tmp_bytes(module, &ct_in)
|
||||
| GLWEAutomorphismKey::encrypt_sk_tmp_bytes(module, &auto_key)
|
||||
| GLWETensorKey::encrypt_sk_tmp_bytes(module, &tensor_key)
|
||||
| GGSW::automorphism_tmp_bytes(module, &ct_out, &ct_in, &auto_key, &tensor_key),
|
||||
| GGLWEToGGSWKey::encrypt_sk_tmp_bytes(module, &tsk)
|
||||
| GGSW::automorphism_tmp_bytes(module, &ct_out, &ct_in, &auto_key, &tsk),
|
||||
);
|
||||
|
||||
let var_xs: f64 = 0.5;
|
||||
@@ -115,7 +115,7 @@ where
|
||||
&mut source_xe,
|
||||
scratch.borrow(),
|
||||
);
|
||||
tensor_key.encrypt_sk(
|
||||
tsk.encrypt_sk(
|
||||
module,
|
||||
&sk,
|
||||
&mut source_xa,
|
||||
@@ -138,9 +138,8 @@ where
|
||||
GLWEAutomorphismKeyPrepared::alloc_from_infos(module, &auto_key_layout);
|
||||
auto_key_prepared.prepare(module, &auto_key, scratch.borrow());
|
||||
|
||||
let mut tsk_prepared: GLWETensorKeyPrepared<Vec<u8>, BE> =
|
||||
GLWETensorKeyPrepared::alloc_from_infos(module, &tensor_key_layout);
|
||||
tsk_prepared.prepare(module, &tensor_key, scratch.borrow());
|
||||
let mut tsk_prepared: GGLWEToGGSWKeyPrepared<Vec<u8>, BE> = GGLWEToGGSWKeyPrepared::alloc_from_infos(module, &tsk);
|
||||
tsk_prepared.prepare(module, &tsk, scratch.borrow());
|
||||
|
||||
ct_out.automorphism(
|
||||
module,
|
||||
@@ -180,8 +179,8 @@ where
|
||||
+ GLWEAutomorphismKeyEncryptSk<BE>
|
||||
+ GLWEAutomorphismKeyPreparedFactory<BE>
|
||||
+ GGSWAutomorphism<BE>
|
||||
+ GLWETensorKeyPreparedFactory<BE>
|
||||
+ GLWETensorKeyEncryptSk<BE>
|
||||
+ GGLWEToGGSWKeyPreparedFactory<BE>
|
||||
+ GGLWEToGGSWKeyEncryptSk<BE>
|
||||
+ GLWESecretPreparedFactory<BE>
|
||||
+ VecZnxAutomorphismInplace<BE>
|
||||
+ GGSWNoise<BE>,
|
||||
@@ -211,7 +210,7 @@ where
|
||||
rank: rank.into(),
|
||||
};
|
||||
|
||||
let tensor_key_layout: GLWETensorKeyLayout = GLWETensorKeyLayout {
|
||||
let tsk_layout: GGLWEToGGSWKeyLayout = GGLWEToGGSWKeyLayout {
|
||||
n: n.into(),
|
||||
base2k: base2k.into(),
|
||||
k: k_tsk.into(),
|
||||
@@ -220,7 +219,7 @@ where
|
||||
rank: rank.into(),
|
||||
};
|
||||
|
||||
let auto_key_layout: GLWETensorKeyLayout = GLWETensorKeyLayout {
|
||||
let auto_key_layout: GGLWEToGGSWKeyLayout = GGLWEToGGSWKeyLayout {
|
||||
n: n.into(),
|
||||
base2k: base2k.into(),
|
||||
k: k_ksk.into(),
|
||||
@@ -230,7 +229,7 @@ where
|
||||
};
|
||||
|
||||
let mut ct: GGSW<Vec<u8>> = GGSW::alloc_from_infos(&ggsw_out_layout);
|
||||
let mut tensor_key: GLWETensorKey<Vec<u8>> = GLWETensorKey::alloc_from_infos(&tensor_key_layout);
|
||||
let mut tsk: GGLWEToGGSWKey<Vec<u8>> = GGLWEToGGSWKey::alloc_from_infos(&tsk_layout);
|
||||
let mut auto_key: GLWEAutomorphismKey<Vec<u8>> = GLWEAutomorphismKey::alloc_from_infos(&auto_key_layout);
|
||||
let mut pt_scalar: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n, 1);
|
||||
|
||||
@@ -241,8 +240,8 @@ where
|
||||
let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(
|
||||
GGSW::encrypt_sk_tmp_bytes(module, &ct)
|
||||
| GLWEAutomorphismKey::encrypt_sk_tmp_bytes(module, &auto_key)
|
||||
| GLWETensorKey::encrypt_sk_tmp_bytes(module, &tensor_key)
|
||||
| GGSW::automorphism_tmp_bytes(module, &ct, &ct, &auto_key, &tensor_key),
|
||||
| GGLWEToGGSWKey::encrypt_sk_tmp_bytes(module, &tsk)
|
||||
| GGSW::automorphism_tmp_bytes(module, &ct, &ct, &auto_key, &tsk),
|
||||
);
|
||||
|
||||
let var_xs: f64 = 0.5;
|
||||
@@ -261,7 +260,7 @@ where
|
||||
&mut source_xe,
|
||||
scratch.borrow(),
|
||||
);
|
||||
tensor_key.encrypt_sk(
|
||||
tsk.encrypt_sk(
|
||||
module,
|
||||
&sk,
|
||||
&mut source_xa,
|
||||
@@ -284,9 +283,8 @@ where
|
||||
GLWEAutomorphismKeyPrepared::alloc_from_infos(module, &auto_key_layout);
|
||||
auto_key_prepared.prepare(module, &auto_key, scratch.borrow());
|
||||
|
||||
let mut tsk_prepared: GLWETensorKeyPrepared<Vec<u8>, BE> =
|
||||
GLWETensorKeyPrepared::alloc_from_infos(module, &tensor_key_layout);
|
||||
tsk_prepared.prepare(module, &tensor_key, scratch.borrow());
|
||||
let mut tsk_prepared: GGLWEToGGSWKeyPrepared<Vec<u8>, BE> = GGLWEToGGSWKeyPrepared::alloc_from_infos(module, &tsk);
|
||||
tsk_prepared.prepare(module, &tsk, scratch.borrow());
|
||||
|
||||
ct.automorphism_inplace(module, &auto_key_prepared, &tsk_prepared, scratch.borrow());
|
||||
|
||||
|
||||
@@ -8,10 +8,10 @@ use crate::{
|
||||
GLWEDecrypt, GLWEEncryptSk, GLWEFromLWE, GLWEToLWESwitchingKeyEncryptSk, LWEDecrypt, LWEEncryptSk,
|
||||
LWEToGLWESwitchingKeyEncryptSk, ScratchTakeCore,
|
||||
layouts::{
|
||||
Base2K, Degree, Dnum, GLWE, GLWELayout, GLWEPlaintext, GLWESecret, GLWESecretPreparedFactory, GLWEToLWEKeyLayout,
|
||||
GLWEToLWESwitchingKey, GLWEToLWESwitchingKeyPreparedFactory, LWE, LWELayout, LWEPlaintext, LWESecret,
|
||||
LWEToGLWESwitchingKey, LWEToGLWESwitchingKeyLayout, LWEToGLWESwitchingKeyPreparedFactory, Rank, TorusPrecision,
|
||||
prepared::{GLWESecretPrepared, GLWEToLWESwitchingKeyPrepared, LWEToGLWESwitchingKeyPrepared},
|
||||
Base2K, Degree, Dnum, GLWE, GLWELayout, GLWEPlaintext, GLWESecret, GLWESecretPreparedFactory, GLWEToLWEKey,
|
||||
GLWEToLWEKeyLayout, GLWEToLWEKeyPrepared, GLWEToLWEKeyPreparedFactory, LWE, LWELayout, LWEPlaintext, LWESecret,
|
||||
LWEToGLWEKey, LWEToGLWEKeyLayout, LWEToGLWEKeyPrepared, LWEToGLWEKeyPreparedFactory, Rank, TorusPrecision,
|
||||
prepared::GLWESecretPrepared,
|
||||
},
|
||||
};
|
||||
|
||||
@@ -22,7 +22,7 @@ where
|
||||
+ GLWEDecrypt<BE>
|
||||
+ GLWESecretPreparedFactory<BE>
|
||||
+ LWEEncryptSk<BE>
|
||||
+ LWEToGLWESwitchingKeyPreparedFactory<BE>,
|
||||
+ LWEToGLWEKeyPreparedFactory<BE>,
|
||||
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
|
||||
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
|
||||
{
|
||||
@@ -36,7 +36,7 @@ where
|
||||
let mut source_xa: Source = Source::new([0u8; 32]);
|
||||
let mut source_xe: Source = Source::new([0u8; 32]);
|
||||
|
||||
let lwe_to_glwe_infos: LWEToGLWESwitchingKeyLayout = LWEToGLWESwitchingKeyLayout {
|
||||
let lwe_to_glwe_infos: LWEToGLWEKeyLayout = LWEToGLWEKeyLayout {
|
||||
n: n_glwe,
|
||||
base2k: Base2K(17),
|
||||
k: TorusPrecision(51),
|
||||
@@ -58,7 +58,7 @@ where
|
||||
};
|
||||
|
||||
let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(
|
||||
LWEToGLWESwitchingKey::encrypt_sk_tmp_bytes(module, &lwe_to_glwe_infos)
|
||||
LWEToGLWEKey::encrypt_sk_tmp_bytes(module, &lwe_to_glwe_infos)
|
||||
| GLWE::from_lwe_tmp_bytes(module, &glwe_infos, &lwe_infos, &lwe_to_glwe_infos)
|
||||
| GLWE::decrypt_tmp_bytes(module, &glwe_infos),
|
||||
);
|
||||
@@ -80,7 +80,7 @@ where
|
||||
let mut lwe_ct: LWE<Vec<u8>> = LWE::alloc_from_infos(&lwe_infos);
|
||||
lwe_ct.encrypt_sk(module, &lwe_pt, &sk_lwe, &mut source_xa, &mut source_xe);
|
||||
|
||||
let mut ksk: LWEToGLWESwitchingKey<Vec<u8>> = LWEToGLWESwitchingKey::alloc_from_infos(&lwe_to_glwe_infos);
|
||||
let mut ksk: LWEToGLWEKey<Vec<u8>> = LWEToGLWEKey::alloc_from_infos(&lwe_to_glwe_infos);
|
||||
|
||||
ksk.encrypt_sk(
|
||||
module,
|
||||
@@ -93,8 +93,7 @@ where
|
||||
|
||||
let mut glwe_ct: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_infos);
|
||||
|
||||
let mut ksk_prepared: LWEToGLWESwitchingKeyPrepared<Vec<u8>, BE> =
|
||||
LWEToGLWESwitchingKeyPrepared::alloc_from_infos(module, &ksk);
|
||||
let mut ksk_prepared: LWEToGLWEKeyPrepared<Vec<u8>, BE> = LWEToGLWEKeyPrepared::alloc_from_infos(module, &ksk);
|
||||
ksk_prepared.prepare(module, &ksk, scratch.borrow());
|
||||
|
||||
glwe_ct.from_lwe(module, &lwe_ct, &ksk_prepared, scratch.borrow());
|
||||
@@ -114,7 +113,7 @@ where
|
||||
+ GLWEDecrypt<BE>
|
||||
+ GLWESecretPreparedFactory<BE>
|
||||
+ GLWEToLWESwitchingKeyEncryptSk<BE>
|
||||
+ GLWEToLWESwitchingKeyPreparedFactory<BE>,
|
||||
+ GLWEToLWEKeyPreparedFactory<BE>,
|
||||
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
|
||||
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
|
||||
{
|
||||
@@ -150,7 +149,7 @@ where
|
||||
let mut source_xe: Source = Source::new([0u8; 32]);
|
||||
|
||||
let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(
|
||||
GLWEToLWESwitchingKey::encrypt_sk_tmp_bytes(module, &glwe_to_lwe_infos)
|
||||
GLWEToLWEKey::encrypt_sk_tmp_bytes(module, &glwe_to_lwe_infos)
|
||||
| LWE::from_glwe_tmp_bytes(module, &lwe_infos, &glwe_infos, &glwe_to_lwe_infos)
|
||||
| GLWE::decrypt_tmp_bytes(module, &glwe_infos),
|
||||
);
|
||||
@@ -178,7 +177,7 @@ where
|
||||
scratch.borrow(),
|
||||
);
|
||||
|
||||
let mut ksk: GLWEToLWESwitchingKey<Vec<u8>> = GLWEToLWESwitchingKey::alloc_from_infos(&glwe_to_lwe_infos);
|
||||
let mut ksk: GLWEToLWEKey<Vec<u8>> = GLWEToLWEKey::alloc_from_infos(&glwe_to_lwe_infos);
|
||||
|
||||
ksk.encrypt_sk(
|
||||
module,
|
||||
@@ -191,8 +190,7 @@ where
|
||||
|
||||
let mut lwe_ct: LWE<Vec<u8>> = LWE::alloc_from_infos(&lwe_infos);
|
||||
|
||||
let mut ksk_prepared: GLWEToLWESwitchingKeyPrepared<Vec<u8>, BE> =
|
||||
GLWEToLWESwitchingKeyPrepared::alloc_from_infos(module, &ksk);
|
||||
let mut ksk_prepared: GLWEToLWEKeyPrepared<Vec<u8>, BE> = GLWEToLWEKeyPrepared::alloc_from_infos(module, &ksk);
|
||||
ksk_prepared.prepare(module, &ksk, scratch.borrow());
|
||||
|
||||
lwe_ct.from_glwe(module, &glwe_ct, &ksk_prepared, scratch.borrow());
|
||||
|
||||
144
poulpy-core/src/tests/test_suite/encryption/gglwe_to_ggsw_key.rs
Normal file
144
poulpy-core/src/tests/test_suite/encryption/gglwe_to_ggsw_key.rs
Normal file
@@ -0,0 +1,144 @@
|
||||
use poulpy_hal::{
|
||||
api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, VecZnxCopy},
|
||||
layouts::{Backend, Module, ScalarZnx, Scratch, ScratchOwned},
|
||||
source::Source,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
GGLWENoise, GGLWEToGGSWKeyCompressedEncryptSk, GGLWEToGGSWKeyEncryptSk, ScratchTakeCore,
|
||||
decryption::GLWEDecrypt,
|
||||
encryption::SIGMA,
|
||||
layouts::{
|
||||
Dsize, GGLWEDecompress, GGLWEToGGSWKey, GGLWEToGGSWKeyCompressed, GGLWEToGGSWKeyDecompress, GGLWEToGGSWKeyLayout,
|
||||
GLWESecret, GLWESecretPreparedFactory, GLWESecretTensor, GLWESecretTensorFactory, LWEInfos, prepared::GLWESecretPrepared,
|
||||
},
|
||||
};
|
||||
|
||||
pub fn test_gglwe_to_ggsw_key_encrypt_sk<BE: Backend>(module: &Module<BE>)
|
||||
where
|
||||
Module<BE>: GGLWEToGGSWKeyEncryptSk<BE>
|
||||
+ GLWESecretTensorFactory<BE>
|
||||
+ GLWESecretPreparedFactory<BE>
|
||||
+ GLWEDecrypt<BE>
|
||||
+ GGLWENoise<BE>
|
||||
+ VecZnxCopy,
|
||||
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
|
||||
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
|
||||
{
|
||||
let base2k: usize = 8;
|
||||
let k: usize = 54;
|
||||
|
||||
for rank in 2_usize..3 {
|
||||
let n: usize = module.n();
|
||||
let dnum: usize = k / base2k;
|
||||
|
||||
let key_infos: GGLWEToGGSWKeyLayout = GGLWEToGGSWKeyLayout {
|
||||
n: n.into(),
|
||||
base2k: base2k.into(),
|
||||
k: k.into(),
|
||||
dnum: dnum.into(),
|
||||
dsize: Dsize(1),
|
||||
rank: rank.into(),
|
||||
};
|
||||
|
||||
let mut key: GGLWEToGGSWKey<Vec<u8>> = GGLWEToGGSWKey::alloc_from_infos(&key_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<BE> = ScratchOwned::alloc(GGLWEToGGSWKey::encrypt_sk_tmp_bytes(module, &key_infos));
|
||||
|
||||
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&key_infos);
|
||||
sk.fill_ternary_prob(0.5, &mut source_xs);
|
||||
let mut sk_prepared: GLWESecretPrepared<Vec<u8>, BE> = GLWESecretPrepared::alloc(module, rank.into());
|
||||
sk_prepared.prepare(module, &sk);
|
||||
|
||||
key.encrypt_sk(
|
||||
module,
|
||||
&sk,
|
||||
&mut source_xa,
|
||||
&mut source_xe,
|
||||
scratch.borrow(),
|
||||
);
|
||||
|
||||
let mut sk_tensor: GLWESecretTensor<Vec<u8>> = GLWESecretTensor::alloc_from_infos(&sk);
|
||||
sk_tensor.prepare(module, &sk, scratch.borrow());
|
||||
|
||||
let max_noise = SIGMA.log2() + 0.5 - (key.k().as_u32() as f64);
|
||||
|
||||
let mut pt_want: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(module.n(), rank);
|
||||
|
||||
for i in 0..rank {
|
||||
for j in 0..rank {
|
||||
module.vec_znx_copy(
|
||||
&mut pt_want.as_vec_znx_mut(),
|
||||
j,
|
||||
&sk_tensor.at(i, j).as_vec_znx(),
|
||||
0,
|
||||
);
|
||||
}
|
||||
|
||||
println!("pt_want: {}", pt_want.as_vec_znx());
|
||||
|
||||
module.gglwe_assert_noise(key.at(i), &sk_prepared, &pt_want, max_noise);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn test_gglwe_to_ggsw_compressed_encrypt_sk<BE: Backend>(module: &Module<BE>)
|
||||
where
|
||||
Module<BE>: GGLWEToGGSWKeyCompressedEncryptSk<BE>
|
||||
+ GLWESecretPreparedFactory<BE>
|
||||
+ GLWEDecrypt<BE>
|
||||
+ GLWESecretTensorFactory<BE>
|
||||
+ GGLWENoise<BE>
|
||||
+ GGLWEDecompress
|
||||
+ GGLWEToGGSWKeyDecompress,
|
||||
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
|
||||
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
|
||||
{
|
||||
let base2k = 8;
|
||||
let k = 54;
|
||||
for rank in 1_usize..3 {
|
||||
let n: usize = module.n();
|
||||
let dnum: usize = k / base2k;
|
||||
|
||||
let key_infos: GGLWEToGGSWKeyLayout = GGLWEToGGSWKeyLayout {
|
||||
n: n.into(),
|
||||
base2k: base2k.into(),
|
||||
k: k.into(),
|
||||
dnum: dnum.into(),
|
||||
dsize: Dsize(1),
|
||||
rank: rank.into(),
|
||||
};
|
||||
|
||||
let mut key_compressed: GGLWEToGGSWKeyCompressed<Vec<u8>> = GGLWEToGGSWKeyCompressed::alloc_from_infos(&key_infos);
|
||||
|
||||
let mut source_xs: Source = Source::new([0u8; 32]);
|
||||
let mut source_xe: Source = Source::new([0u8; 32]);
|
||||
|
||||
let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(GGLWEToGGSWKeyCompressed::encrypt_sk_tmp_bytes(
|
||||
module, &key_infos,
|
||||
));
|
||||
|
||||
let mut sk: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&key_infos);
|
||||
sk.fill_ternary_prob(0.5, &mut source_xs);
|
||||
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];
|
||||
|
||||
key_compressed.encrypt_sk(module, &sk, seed_xa, &mut source_xe, scratch.borrow());
|
||||
|
||||
let mut key: GGLWEToGGSWKey<Vec<u8>> = GGLWEToGGSWKey::alloc_from_infos(&key_infos);
|
||||
key.decompress(module, &key_compressed);
|
||||
|
||||
let mut sk_tensor: GLWESecretTensor<Vec<u8>> = GLWESecretTensor::alloc_from_infos(&sk);
|
||||
sk_tensor.prepare(module, &sk, scratch.borrow());
|
||||
|
||||
for i in 0..rank {
|
||||
module.gglwe_assert_noise(key.at(i), &sk_prepared, &sk_tensor.data, SIGMA + 0.5);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,20 +1,16 @@
|
||||
use poulpy_hal::{
|
||||
api::{
|
||||
ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDft, VecZnxBigAlloc, VecZnxBigNormalize,
|
||||
VecZnxCopy, VecZnxDftAlloc, VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyTmpA, VecZnxSubScalarInplace,
|
||||
VecZnxSwitchRing,
|
||||
},
|
||||
layouts::{Backend, Module, Scratch, ScratchOwned, VecZnxBig, VecZnxDft},
|
||||
api::{ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow},
|
||||
layouts::{Backend, Module, Scratch, ScratchOwned},
|
||||
source::Source,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
GLWETensorKeyCompressedEncryptSk, GLWETensorKeyEncryptSk, ScratchTakeCore,
|
||||
GGLWENoise, GLWETensorKeyCompressedEncryptSk, GLWETensorKeyEncryptSk, ScratchTakeCore,
|
||||
decryption::GLWEDecrypt,
|
||||
encryption::SIGMA,
|
||||
layouts::{
|
||||
Dsize, GLWEPlaintext, GLWESecret, GLWESecretPreparedFactory, GLWETensorKey, GLWETensorKeyCompressed, GLWETensorKeyLayout,
|
||||
prepared::GLWESecretPrepared,
|
||||
Dsize, GGLWEDecompress, GLWESecret, GLWESecretPreparedFactory, GLWESecretTensor, GLWESecretTensorFactory, GLWETensorKey,
|
||||
GLWETensorKeyCompressed, GLWETensorKeyLayout, prepared::GLWESecretPrepared,
|
||||
},
|
||||
};
|
||||
|
||||
@@ -23,20 +19,15 @@ where
|
||||
Module<BE>: GLWETensorKeyEncryptSk<BE>
|
||||
+ GLWESecretPreparedFactory<BE>
|
||||
+ GLWEDecrypt<BE>
|
||||
+ VecZnxDftAlloc<BE>
|
||||
+ VecZnxBigAlloc<BE>
|
||||
+ VecZnxDftApply<BE>
|
||||
+ SvpApplyDftToDft<BE>
|
||||
+ VecZnxIdftApplyTmpA<BE>
|
||||
+ VecZnxBigNormalize<BE>
|
||||
+ VecZnxSubScalarInplace,
|
||||
+ GLWESecretTensorFactory<BE>
|
||||
+ GGLWENoise<BE>,
|
||||
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
|
||||
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
|
||||
{
|
||||
let base2k: usize = 8;
|
||||
let k: usize = 54;
|
||||
|
||||
for rank in 1_usize..3 {
|
||||
for rank in 2_usize..3 {
|
||||
let n: usize = module.n();
|
||||
let dnum: usize = k / base2k;
|
||||
|
||||
@@ -73,42 +64,10 @@ where
|
||||
scratch.borrow(),
|
||||
);
|
||||
|
||||
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&tensor_key_infos);
|
||||
let mut sk_tensor: GLWESecretTensor<Vec<u8>> = GLWESecretTensor::alloc_from_infos(&sk);
|
||||
sk_tensor.prepare(module, &sk, scratch.borrow());
|
||||
|
||||
let mut sk_ij_dft: VecZnxDft<Vec<u8>, BE> = module.vec_znx_dft_alloc(1, 1);
|
||||
let mut sk_ij_big: VecZnxBig<Vec<u8>, BE> = module.vec_znx_big_alloc(1, 1);
|
||||
let mut sk_ij: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), 1_u32.into());
|
||||
let mut sk_dft: VecZnxDft<Vec<u8>, BE> = module.vec_znx_dft_alloc(rank, 1);
|
||||
|
||||
for i in 0..rank {
|
||||
module.vec_znx_dft_apply(1, 0, &mut sk_dft, i, &sk.data.as_vec_znx(), i);
|
||||
}
|
||||
|
||||
for i in 0..rank {
|
||||
for j in 0..rank {
|
||||
module.svp_apply_dft_to_dft(&mut sk_ij_dft, 0, &sk_prepared.data, j, &sk_dft, i);
|
||||
module.vec_znx_idft_apply_tmpa(&mut sk_ij_big, 0, &mut sk_ij_dft, 0);
|
||||
module.vec_znx_big_normalize(
|
||||
base2k,
|
||||
&mut sk_ij.data.as_vec_znx_mut(),
|
||||
0,
|
||||
base2k,
|
||||
&sk_ij_big,
|
||||
0,
|
||||
scratch.borrow(),
|
||||
);
|
||||
for row_i in 0..dnum {
|
||||
let ct = tensor_key.at(i, j).at(row_i, 0);
|
||||
|
||||
ct.decrypt(module, &mut pt, &sk_prepared, scratch.borrow());
|
||||
|
||||
module.vec_znx_sub_scalar_inplace(&mut pt.data, 0, row_i, &sk_ij.data, 0);
|
||||
|
||||
let std_pt: f64 = pt.data.std(base2k, 0) * (k as f64).exp2();
|
||||
assert!((SIGMA - std_pt).abs() <= 0.5, "{SIGMA} {std_pt}");
|
||||
}
|
||||
}
|
||||
}
|
||||
module.gglwe_assert_noise(&tensor_key, &sk_prepared, &sk_tensor.data, SIGMA + 0.5);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -118,15 +77,9 @@ where
|
||||
+ GLWESecretPreparedFactory<BE>
|
||||
+ GLWETensorKeyCompressedEncryptSk<BE>
|
||||
+ GLWEDecrypt<BE>
|
||||
+ VecZnxDftAlloc<BE>
|
||||
+ VecZnxBigAlloc<BE>
|
||||
+ VecZnxDftApply<BE>
|
||||
+ SvpApplyDftToDft<BE>
|
||||
+ VecZnxIdftApplyTmpA<BE>
|
||||
+ VecZnxSubScalarInplace
|
||||
+ VecZnxFillUniform
|
||||
+ VecZnxCopy
|
||||
+ VecZnxSwitchRing,
|
||||
+ GLWESecretTensorFactory<BE>
|
||||
+ GGLWENoise<BE>
|
||||
+ GGLWEDecompress,
|
||||
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
|
||||
Scratch<BE>: ScratchAvailable + ScratchTakeCore<BE>,
|
||||
{
|
||||
@@ -168,42 +121,9 @@ where
|
||||
let mut tensor_key: GLWETensorKey<Vec<u8>> = GLWETensorKey::alloc_from_infos(&tensor_key_infos);
|
||||
tensor_key.decompress(module, &tensor_key_compressed);
|
||||
|
||||
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&tensor_key_infos);
|
||||
let mut sk_tensor: GLWESecretTensor<Vec<u8>> = GLWESecretTensor::alloc_from_infos(&sk);
|
||||
sk_tensor.prepare(module, &sk, scratch.borrow());
|
||||
|
||||
let mut sk_ij_dft: VecZnxDft<Vec<u8>, BE> = module.vec_znx_dft_alloc(1, 1);
|
||||
let mut sk_ij_big: VecZnxBig<Vec<u8>, BE> = module.vec_znx_big_alloc(1, 1);
|
||||
let mut sk_ij: GLWESecret<Vec<u8>> = GLWESecret::alloc(n.into(), 1_u32.into());
|
||||
let mut sk_dft: VecZnxDft<Vec<u8>, BE> = module.vec_znx_dft_alloc(rank, 1);
|
||||
|
||||
for i in 0..rank {
|
||||
module.vec_znx_dft_apply(1, 0, &mut sk_dft, i, &sk.data.as_vec_znx(), i);
|
||||
}
|
||||
|
||||
for i in 0..rank {
|
||||
for j in 0..rank {
|
||||
module.svp_apply_dft_to_dft(&mut sk_ij_dft, 0, &sk_prepared.data, j, &sk_dft, i);
|
||||
module.vec_znx_idft_apply_tmpa(&mut sk_ij_big, 0, &mut sk_ij_dft, 0);
|
||||
module.vec_znx_big_normalize(
|
||||
base2k,
|
||||
&mut sk_ij.data.as_vec_znx_mut(),
|
||||
0,
|
||||
base2k,
|
||||
&sk_ij_big,
|
||||
0,
|
||||
scratch.borrow(),
|
||||
);
|
||||
for row_i in 0..dnum {
|
||||
tensor_key
|
||||
.at(i, j)
|
||||
.at(row_i, 0)
|
||||
.decrypt(module, &mut pt, &sk_prepared, scratch.borrow());
|
||||
|
||||
module.vec_znx_sub_scalar_inplace(&mut pt.data, 0, row_i, &sk_ij.data, 0);
|
||||
|
||||
let std_pt: f64 = pt.data.std(base2k, 0) * (k as f64).exp2();
|
||||
assert!((SIGMA - std_pt).abs() <= 0.5, "{SIGMA} {std_pt}");
|
||||
}
|
||||
}
|
||||
}
|
||||
module.gglwe_assert_noise(&tensor_key, &sk_prepared, &sk_tensor.data, SIGMA + 0.5);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
mod gglwe_atk;
|
||||
mod gglwe_ct;
|
||||
mod gglwe_to_ggsw_key;
|
||||
mod ggsw_ct;
|
||||
mod glwe_ct;
|
||||
mod glwe_tsk;
|
||||
|
||||
pub use gglwe_atk::*;
|
||||
pub use gglwe_ct::*;
|
||||
pub use gglwe_to_ggsw_key::*;
|
||||
pub use ggsw_ct::*;
|
||||
pub use glwe_ct::*;
|
||||
pub use glwe_tsk::*;
|
||||
|
||||
@@ -5,12 +5,13 @@ use poulpy_hal::{
|
||||
};
|
||||
|
||||
use crate::{
|
||||
GGSWEncryptSk, GGSWKeyswitch, GGSWNoise, GLWESwitchingKeyEncryptSk, GLWETensorKeyEncryptSk, ScratchTakeCore,
|
||||
GGLWEToGGSWKeyEncryptSk, GGSWEncryptSk, GGSWKeyswitch, GGSWNoise, GLWESwitchingKeyEncryptSk, ScratchTakeCore,
|
||||
encryption::SIGMA,
|
||||
layouts::{
|
||||
GGSW, GGSWLayout, GLWESecret, GLWESecretPreparedFactory, GLWESwitchingKey, GLWESwitchingKeyLayout,
|
||||
GLWESwitchingKeyPreparedFactory, GLWETensorKey, GLWETensorKeyLayout, GLWETensorKeyPreparedFactory,
|
||||
prepared::{GLWESecretPrepared, GLWESwitchingKeyPrepared, GLWETensorKeyPrepared},
|
||||
GGLWEToGGSWKey, GGLWEToGGSWKeyPrepared, GGLWEToGGSWKeyPreparedFactory, GGSW, GGSWLayout, GLWESecret,
|
||||
GLWESecretPreparedFactory, GLWESwitchingKey, GLWESwitchingKeyLayout, GLWESwitchingKeyPreparedFactory,
|
||||
GLWETensorKeyLayout,
|
||||
prepared::{GLWESecretPrepared, GLWESwitchingKeyPrepared},
|
||||
},
|
||||
noise::noise_ggsw_keyswitch,
|
||||
};
|
||||
@@ -20,10 +21,10 @@ pub fn test_ggsw_keyswitch<BE: Backend>(module: &Module<BE>)
|
||||
where
|
||||
Module<BE>: GGSWEncryptSk<BE>
|
||||
+ GLWESwitchingKeyEncryptSk<BE>
|
||||
+ GLWETensorKeyEncryptSk<BE>
|
||||
+ GGLWEToGGSWKeyEncryptSk<BE>
|
||||
+ GGSWKeyswitch<BE>
|
||||
+ GLWESecretPreparedFactory<BE>
|
||||
+ GLWETensorKeyPreparedFactory<BE>
|
||||
+ GGLWEToGGSWKeyPreparedFactory<BE>
|
||||
+ GLWESwitchingKeyPreparedFactory<BE>
|
||||
+ GGSWNoise<BE>,
|
||||
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
|
||||
@@ -82,7 +83,7 @@ where
|
||||
|
||||
let mut ggsw_in: GGSW<Vec<u8>> = GGSW::alloc_from_infos(&ggsw_in_infos);
|
||||
let mut ggsw_out: GGSW<Vec<u8>> = GGSW::alloc_from_infos(&ggsw_out_infos);
|
||||
let mut tsk: GLWETensorKey<Vec<u8>> = GLWETensorKey::alloc_from_infos(&tsk_infos);
|
||||
let mut tsk: GGLWEToGGSWKey<Vec<u8>> = GGLWEToGGSWKey::alloc_from_infos(&tsk_infos);
|
||||
let mut ksk: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc_from_infos(&ksk_apply_infos);
|
||||
let mut pt_scalar: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n, 1);
|
||||
|
||||
@@ -93,7 +94,7 @@ where
|
||||
let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(
|
||||
GGSW::encrypt_sk_tmp_bytes(module, &ggsw_in_infos)
|
||||
| GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &ksk_apply_infos)
|
||||
| GLWETensorKey::encrypt_sk_tmp_bytes(module, &tsk_infos)
|
||||
| GGLWEToGGSWKey::encrypt_sk_tmp_bytes(module, &tsk_infos)
|
||||
| GGSW::keyswitch_tmp_bytes(
|
||||
module,
|
||||
&ggsw_out_infos,
|
||||
@@ -148,7 +149,7 @@ where
|
||||
GLWESwitchingKeyPrepared::alloc_from_infos(module, &ksk);
|
||||
ksk_prepared.prepare(module, &ksk, scratch.borrow());
|
||||
|
||||
let mut tsk_prepared: GLWETensorKeyPrepared<Vec<u8>, BE> = GLWETensorKeyPrepared::alloc_from_infos(module, &tsk);
|
||||
let mut tsk_prepared: GGLWEToGGSWKeyPrepared<Vec<u8>, BE> = GGLWEToGGSWKeyPrepared::alloc_from_infos(module, &tsk);
|
||||
tsk_prepared.prepare(module, &tsk, scratch.borrow());
|
||||
|
||||
ggsw_out.keyswitch(
|
||||
@@ -185,10 +186,10 @@ pub fn test_ggsw_keyswitch_inplace<BE: Backend>(module: &Module<BE>)
|
||||
where
|
||||
Module<BE>: GGSWEncryptSk<BE>
|
||||
+ GLWESwitchingKeyEncryptSk<BE>
|
||||
+ GLWETensorKeyEncryptSk<BE>
|
||||
+ GGLWEToGGSWKeyEncryptSk<BE>
|
||||
+ GGSWKeyswitch<BE>
|
||||
+ GLWESecretPreparedFactory<BE>
|
||||
+ GLWETensorKeyPreparedFactory<BE>
|
||||
+ GGLWEToGGSWKeyPreparedFactory<BE>
|
||||
+ GLWESwitchingKeyPreparedFactory<BE>
|
||||
+ GGSWNoise<BE>,
|
||||
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
|
||||
@@ -236,7 +237,7 @@ where
|
||||
};
|
||||
|
||||
let mut ggsw_out: GGSW<Vec<u8>> = GGSW::alloc_from_infos(&ggsw_out_infos);
|
||||
let mut tsk: GLWETensorKey<Vec<u8>> = GLWETensorKey::alloc_from_infos(&tsk_infos);
|
||||
let mut tsk: GGLWEToGGSWKey<Vec<u8>> = GGLWEToGGSWKey::alloc_from_infos(&tsk_infos);
|
||||
let mut ksk: GLWESwitchingKey<Vec<u8>> = GLWESwitchingKey::alloc_from_infos(&ksk_apply_infos);
|
||||
let mut pt_scalar: ScalarZnx<Vec<u8>> = ScalarZnx::alloc(n, 1);
|
||||
|
||||
@@ -247,7 +248,7 @@ where
|
||||
let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(
|
||||
GGSW::encrypt_sk_tmp_bytes(module, &ggsw_out_infos)
|
||||
| GLWESwitchingKey::encrypt_sk_tmp_bytes(module, &ksk_apply_infos)
|
||||
| GLWETensorKey::encrypt_sk_tmp_bytes(module, &tsk_infos)
|
||||
| GGLWEToGGSWKey::encrypt_sk_tmp_bytes(module, &tsk_infos)
|
||||
| GGSW::keyswitch_tmp_bytes(
|
||||
module,
|
||||
&ggsw_out_infos,
|
||||
@@ -302,7 +303,7 @@ where
|
||||
GLWESwitchingKeyPrepared::alloc_from_infos(module, &ksk);
|
||||
ksk_prepared.prepare(module, &ksk, scratch.borrow());
|
||||
|
||||
let mut tsk_prepared: GLWETensorKeyPrepared<Vec<u8>, BE> = GLWETensorKeyPrepared::alloc_from_infos(module, &tsk);
|
||||
let mut tsk_prepared: GGLWEToGGSWKeyPrepared<Vec<u8>, BE> = GGLWEToGGSWKeyPrepared::alloc_from_infos(module, &tsk);
|
||||
tsk_prepared.prepare(module, &tsk, scratch.borrow());
|
||||
|
||||
ggsw_out.keyswitch_inplace(module, &ksk_prepared, &tsk_prepared, scratch.borrow());
|
||||
|
||||
@@ -7,7 +7,7 @@ use poulpy_hal::{
|
||||
};
|
||||
|
||||
use crate::{
|
||||
GLWEAutomorphismKeyEncryptSk, GLWEDecrypt, GLWEEncryptSk, GLWEPacker, GLWEPacking, GLWERotate, GLWESub, ScratchTakeCore,
|
||||
GLWEAutomorphismKeyEncryptSk, GLWEDecrypt, GLWEEncryptSk, GLWEPacker, GLWEPackerOps, GLWERotate, GLWESub, ScratchTakeCore,
|
||||
layouts::{
|
||||
GLWE, GLWEAutomorphismKey, GLWEAutomorphismKeyLayout, GLWEAutomorphismKeyPreparedFactory, GLWELayout, GLWEPlaintext,
|
||||
GLWESecret, GLWESecretPreparedFactory,
|
||||
@@ -20,7 +20,7 @@ where
|
||||
Module<BE>: GLWEEncryptSk<BE>
|
||||
+ GLWEAutomorphismKeyEncryptSk<BE>
|
||||
+ GLWEAutomorphismKeyPreparedFactory<BE>
|
||||
+ GLWEPacking<BE>
|
||||
+ GLWEPackerOps<BE>
|
||||
+ GLWESecretPreparedFactory<BE>
|
||||
+ GLWESub
|
||||
+ GLWEDecrypt<BE>
|
||||
|
||||
Reference in New Issue
Block a user