refactored secret key

This commit is contained in:
Jean-Philippe Bossuat
2025-05-30 15:50:01 +02:00
parent 82810a87c8
commit de4a5e9bfc
24 changed files with 358 additions and 680 deletions

View File

@@ -2,11 +2,7 @@ use backend::{FFT64, Module, ScalarZnxOps, ScratchOwned, Stats, VecZnxOps};
use sampling::source::Source;
use crate::{
automorphism::AutomorphismKey,
elem::{GetRow, Infos},
glwe_ciphertext_fourier::GLWECiphertextFourier,
glwe_plaintext::GLWEPlaintext,
keys::{SecretKey, SecretKeyFourier},
AutomorphismKey, GLWECiphertextFourier, GLWEPlaintext, GLWESecret, GetRow, Infos,
test_fft64::gglwe::log2_std_noise_gglwe_product,
};
@@ -44,11 +40,8 @@ fn test_automorphism(p0: i64, p1: i64, log_n: usize, basek: usize, k_ksk: usize,
| AutomorphismKey::automorphism_scratch_space(&module, basek, k_ksk, k_ksk, k_ksk, rank),
);
let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
sk_dft.dft(&module, &sk);
let mut sk: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank);
sk.fill_ternary_prob(&module, 0.5, &mut source_xs);
// gglwe_{s1}(s0) = s0 -> s1
auto_key_in.generate_from_sk(
@@ -78,7 +71,7 @@ fn test_automorphism(p0: i64, p1: i64, log_n: usize, basek: usize, k_ksk: usize,
let mut ct_glwe_dft: GLWECiphertextFourier<Vec<u8>, FFT64> = GLWECiphertextFourier::alloc(&module, basek, k_ksk, rank);
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, k_ksk);
let mut sk_auto: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
let mut sk_auto: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank);
sk_auto.fill_zero(); // Necessary to avoid panic of unfilled sk
(0..rank).for_each(|i| {
module.scalar_znx_automorphism(
@@ -90,14 +83,13 @@ fn test_automorphism(p0: i64, p1: i64, log_n: usize, basek: usize, k_ksk: usize,
);
});
let mut sk_auto_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
sk_auto_dft.dft(&module, &sk_auto);
sk_auto.prep_fourier(&module);
(0..auto_key_out.rank_in()).for_each(|col_i| {
(0..auto_key_out.rows()).for_each(|row_i| {
auto_key_out.get_row(&module, row_i, col_i, &mut ct_glwe_dft);
ct_glwe_dft.decrypt(&module, &mut pt, &sk_auto_dft, scratch.borrow());
ct_glwe_dft.decrypt(&module, &mut pt, &sk_auto, scratch.borrow());
module.vec_znx_sub_scalar_inplace(&mut pt.data, 0, row_i, &sk.data, col_i);
let noise_have: f64 = pt.data.std(0, basek).log2();
@@ -141,11 +133,8 @@ fn test_automorphism_inplace(p0: i64, p1: i64, log_n: usize, basek: usize, k_ksk
| AutomorphismKey::automorphism_inplace_scratch_space(&module, basek, k_ksk, k_ksk, rank),
);
let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
sk_dft.dft(&module, &sk);
let mut sk: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank);
sk.fill_ternary_prob(&module, 0.5, &mut source_xs);
// gglwe_{s1}(s0) = s0 -> s1
auto_key.generate_from_sk(
@@ -175,7 +164,7 @@ fn test_automorphism_inplace(p0: i64, p1: i64, log_n: usize, basek: usize, k_ksk
let mut ct_glwe_dft: GLWECiphertextFourier<Vec<u8>, FFT64> = GLWECiphertextFourier::alloc(&module, basek, k_ksk, rank);
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, k_ksk);
let mut sk_auto: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
let mut sk_auto: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank);
sk_auto.fill_zero(); // Necessary to avoid panic of unfilled sk
(0..rank).for_each(|i| {
module.scalar_znx_automorphism(
@@ -187,14 +176,13 @@ fn test_automorphism_inplace(p0: i64, p1: i64, log_n: usize, basek: usize, k_ksk
);
});
let mut sk_auto_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
sk_auto_dft.dft(&module, &sk_auto);
sk_auto.prep_fourier(&module);
(0..auto_key.rank_in()).for_each(|col_i| {
(0..auto_key.rows()).for_each(|row_i| {
auto_key.get_row(&module, row_i, col_i, &mut ct_glwe_dft);
ct_glwe_dft.decrypt(&module, &mut pt, &sk_auto_dft, scratch.borrow());
ct_glwe_dft.decrypt(&module, &mut pt, &sk_auto, scratch.borrow());
module.vec_znx_sub_scalar_inplace(&mut pt.data, 0, row_i, &sk.data, col_i);
let noise_have: f64 = pt.data.std(0, basek).log2();

View File

@@ -2,12 +2,7 @@ use backend::{FFT64, Module, ScalarZnx, ScalarZnxAlloc, ScalarZnxToMut, ScratchO
use sampling::source::Source;
use crate::{
elem::{GetRow, Infos},
ggsw_ciphertext::GGSWCiphertext,
glwe_ciphertext_fourier::GLWECiphertextFourier,
glwe_plaintext::GLWEPlaintext,
keys::{SecretKey, SecretKeyFourier},
keyswitch_key::GLWESwitchingKey,
GGSWCiphertext, GLWECiphertextFourier, GLWEPlaintext, GLWESecret, GLWESwitchingKey, GetRow, Infos,
test_fft64::ggsw::noise_ggsw_product,
};
@@ -88,22 +83,16 @@ fn test_encrypt_sk(log_n: usize, basek: usize, k_ksk: usize, sigma: f64, rank_in
| GLWECiphertextFourier::decrypt_scratch_space(&module, basek, k_ksk),
);
let mut sk_in: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank_in);
sk_in.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_in: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank_in);
sk_in.fill_ternary_prob(&module, 0.5, &mut source_xs);
let mut sk_in_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank_in);
sk_in_dft.dft(&module, &sk_in);
let mut sk_out: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank_out);
sk_out.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_out_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank_out);
sk_out_dft.dft(&module, &sk_out);
let mut sk_out: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank_out);
sk_out.fill_ternary_prob(&module, 0.5, &mut source_xs);
ksk.generate_from_sk(
&module,
&sk_in,
&sk_out_dft,
&sk_out,
&mut source_xa,
&mut source_xe,
sigma,
@@ -116,7 +105,7 @@ fn test_encrypt_sk(log_n: usize, basek: usize, k_ksk: usize, sigma: f64, rank_in
(0..ksk.rank_in()).for_each(|col_i| {
(0..ksk.rows()).for_each(|row_i| {
ksk.get_row(&module, row_i, col_i, &mut ct_glwe_fourier);
ct_glwe_fourier.decrypt(&module, &mut pt, &sk_out_dft, scratch.borrow());
ct_glwe_fourier.decrypt(&module, &mut pt, &sk_out, scratch.borrow());
module.vec_znx_sub_scalar_inplace(&mut pt.data, 0, row_i, &sk_in.data, col_i);
let std_pt: f64 = pt.data.std(0, basek) * (k_ksk as f64).exp2();
assert!((sigma - std_pt).abs() <= 0.2, "{} {}", sigma, std_pt);
@@ -161,29 +150,20 @@ fn test_key_switch(
),
);
let mut sk0: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank_in_s0s1);
sk0.fill_ternary_prob(0.5, &mut source_xs);
let mut sk0: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank_in_s0s1);
sk0.fill_ternary_prob(&module, 0.5, &mut source_xs);
let mut sk0_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank_in_s0s1);
sk0_dft.dft(&module, &sk0);
let mut sk1: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank_out_s0s1);
sk1.fill_ternary_prob(&module, 0.5, &mut source_xs);
let mut sk1: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank_out_s0s1);
sk1.fill_ternary_prob(0.5, &mut source_xs);
let mut sk1_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank_out_s0s1);
sk1_dft.dft(&module, &sk1);
let mut sk2: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank_out_s1s2);
sk2.fill_ternary_prob(0.5, &mut source_xs);
let mut sk2_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank_out_s1s2);
sk2_dft.dft(&module, &sk2);
let mut sk2: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank_out_s1s2);
sk2.fill_ternary_prob(&module, 0.5, &mut source_xs);
// gglwe_{s1}(s0) = s0 -> s1
ct_gglwe_s0s1.generate_from_sk(
&module,
&sk0,
&sk1_dft,
&sk1,
&mut source_xa,
&mut source_xe,
sigma,
@@ -194,7 +174,7 @@ fn test_key_switch(
ct_gglwe_s1s2.generate_from_sk(
&module,
&sk1,
&sk2_dft,
&sk2,
&mut source_xa,
&mut source_xe,
sigma,
@@ -211,7 +191,7 @@ fn test_key_switch(
(0..ct_gglwe_s0s2.rank_in()).for_each(|col_i| {
(0..ct_gglwe_s0s2.rows()).for_each(|row_i| {
ct_gglwe_s0s2.get_row(&module, row_i, col_i, &mut ct_glwe_dft);
ct_glwe_dft.decrypt(&module, &mut pt, &sk2_dft, scratch.borrow());
ct_glwe_dft.decrypt(&module, &mut pt, &sk2, scratch.borrow());
module.vec_znx_sub_scalar_inplace(&mut pt.data, 0, row_i, &sk0.data, col_i);
let noise_have: f64 = pt.data.std(0, basek).log2();
@@ -263,29 +243,20 @@ fn test_key_switch_inplace(log_n: usize, basek: usize, k_ksk: usize, sigma: f64,
),
);
let mut sk0: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank_in_s0s1);
sk0.fill_ternary_prob(0.5, &mut source_xs);
let mut sk0: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank_in_s0s1);
sk0.fill_ternary_prob(&module, 0.5, &mut source_xs);
let mut sk0_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank_in_s0s1);
sk0_dft.dft(&module, &sk0);
let mut sk1: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank_out_s0s1);
sk1.fill_ternary_prob(&module, 0.5, &mut source_xs);
let mut sk1: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank_out_s0s1);
sk1.fill_ternary_prob(0.5, &mut source_xs);
let mut sk1_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank_out_s0s1);
sk1_dft.dft(&module, &sk1);
let mut sk2: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank_out_s0s1);
sk2.fill_ternary_prob(0.5, &mut source_xs);
let mut sk2_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank_out_s0s1);
sk2_dft.dft(&module, &sk2);
let mut sk2: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank_out_s0s1);
sk2.fill_ternary_prob(&module, 0.5, &mut source_xs);
// gglwe_{s1}(s0) = s0 -> s1
ct_gglwe_s0s1.generate_from_sk(
&module,
&sk0,
&sk1_dft,
&sk1,
&mut source_xa,
&mut source_xe,
sigma,
@@ -296,7 +267,7 @@ fn test_key_switch_inplace(log_n: usize, basek: usize, k_ksk: usize, sigma: f64,
ct_gglwe_s1s2.generate_from_sk(
&module,
&sk1,
&sk2_dft,
&sk2,
&mut source_xa,
&mut source_xe,
sigma,
@@ -315,7 +286,7 @@ fn test_key_switch_inplace(log_n: usize, basek: usize, k_ksk: usize, sigma: f64,
(0..ct_gglwe_s0s2.rank_in()).for_each(|col_i| {
(0..ct_gglwe_s0s2.rows()).for_each(|row_i| {
ct_gglwe_s0s2.get_row(&module, row_i, col_i, &mut ct_glwe_dft);
ct_glwe_dft.decrypt(&module, &mut pt, &sk2_dft, scratch.borrow());
ct_glwe_dft.decrypt(&module, &mut pt, &sk2, scratch.borrow());
module.vec_znx_sub_scalar_inplace(&mut pt.data, 0, row_i, &sk0.data, col_i);
let noise_have: f64 = pt.data.std(0, basek).log2();
@@ -375,23 +346,17 @@ fn test_external_product(log_n: usize, basek: usize, k: usize, sigma: f64, rank_
pt_rgsw.to_mut().raw_mut()[r] = 1; // X^{r}
let mut sk_in: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank_in);
sk_in.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_in: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank_in);
sk_in.fill_ternary_prob(&module, 0.5, &mut source_xs);
let mut sk_in_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank_in);
sk_in_dft.dft(&module, &sk_in);
let mut sk_out: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank_out);
sk_out.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_out_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank_out);
sk_out_dft.dft(&module, &sk_out);
let mut sk_out: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank_out);
sk_out.fill_ternary_prob(&module, 0.5, &mut source_xs);
// gglwe_{s1}(s0) = s0 -> s1
ct_gglwe_in.generate_from_sk(
&module,
&sk_in,
&sk_out_dft,
&sk_out,
&mut source_xa,
&mut source_xe,
sigma,
@@ -401,7 +366,7 @@ fn test_external_product(log_n: usize, basek: usize, k: usize, sigma: f64, rank_
ct_rgsw.encrypt_sk(
&module,
&pt_rgsw,
&sk_out_dft,
&sk_out,
&mut source_xa,
&mut source_xe,
sigma,
@@ -435,7 +400,7 @@ fn test_external_product(log_n: usize, basek: usize, k: usize, sigma: f64, rank_
(0..rank_in).for_each(|col_i| {
(0..ct_gglwe_out.rows()).for_each(|row_i| {
ct_gglwe_out.get_row(&module, row_i, col_i, &mut ct_glwe_dft);
ct_glwe_dft.decrypt(&module, &mut pt, &sk_out_dft, scratch.borrow());
ct_glwe_dft.decrypt(&module, &mut pt, &sk_out, scratch.borrow());
module.vec_znx_sub_scalar_inplace(&mut pt.data, 0, row_i, &sk_in.data, col_i);
let noise_have: f64 = pt.data.std(0, basek).log2();
@@ -496,23 +461,17 @@ fn test_external_product_inplace(log_n: usize, basek: usize, k: usize, sigma: f6
pt_rgsw.to_mut().raw_mut()[r] = 1; // X^{r}
let mut sk_in: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank_in);
sk_in.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_in: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank_in);
sk_in.fill_ternary_prob(&module, 0.5, &mut source_xs);
let mut sk_in_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank_in);
sk_in_dft.dft(&module, &sk_in);
let mut sk_out: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank_out);
sk_out.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_out_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank_out);
sk_out_dft.dft(&module, &sk_out);
let mut sk_out: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank_out);
sk_out.fill_ternary_prob(&module, 0.5, &mut source_xs);
// gglwe_{s1}(s0) = s0 -> s1
ct_gglwe.generate_from_sk(
&module,
&sk_in,
&sk_out_dft,
&sk_out,
&mut source_xa,
&mut source_xe,
sigma,
@@ -522,7 +481,7 @@ fn test_external_product_inplace(log_n: usize, basek: usize, k: usize, sigma: f6
ct_rgsw.encrypt_sk(
&module,
&pt_rgsw,
&sk_out_dft,
&sk_out,
&mut source_xa,
&mut source_xe,
sigma,
@@ -542,7 +501,7 @@ fn test_external_product_inplace(log_n: usize, basek: usize, k: usize, sigma: f6
(0..rank_in).for_each(|col_i| {
(0..ct_gglwe.rows()).for_each(|row_i| {
ct_gglwe.get_row(&module, row_i, col_i, &mut ct_glwe_dft);
ct_glwe_dft.decrypt(&module, &mut pt, &sk_out_dft, scratch.borrow());
ct_glwe_dft.decrypt(&module, &mut pt, &sk_out, scratch.borrow());
module.vec_znx_sub_scalar_inplace(&mut pt.data, 0, row_i, &sk_in.data, col_i);
let noise_have: f64 = pt.data.std(0, basek).log2();

View File

@@ -5,14 +5,8 @@ use backend::{
use sampling::source::Source;
use crate::{
GGSWCiphertext, GLWECiphertextFourier, GLWEPlaintext, GLWESecret, GLWESwitchingKey, GetRow, Infos, TensorKey,
automorphism::AutomorphismKey,
elem::{GetRow, Infos},
ggsw_ciphertext::GGSWCiphertext,
glwe_ciphertext_fourier::GLWECiphertextFourier,
glwe_plaintext::GLWEPlaintext,
keys::{SecretKey, SecretKeyFourier},
keyswitch_key::GLWESwitchingKey,
tensor_key::TensorKey,
};
use super::gglwe::var_noise_gglwe_product;
@@ -94,16 +88,13 @@ fn test_encrypt_sk(log_n: usize, basek: usize, k: usize, sigma: f64, rank: usize
| GLWECiphertextFourier::decrypt_scratch_space(&module, basek, k),
);
let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
sk_dft.dft(&module, &sk);
let mut sk: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank);
sk.fill_ternary_prob(&module, 0.5, &mut source_xs);
ct.encrypt_sk(
&module,
&pt_scalar,
&sk_dft,
&sk,
&mut source_xa,
&mut source_xe,
sigma,
@@ -121,14 +112,14 @@ fn test_encrypt_sk(log_n: usize, basek: usize, k: usize, sigma: f64, rank: usize
// mul with sk[col_j-1]
if col_j > 0 {
module.vec_znx_dft(&mut pt_dft, 0, &pt_want.data, 0);
module.svp_apply_inplace(&mut pt_dft, 0, &sk_dft.data, col_j - 1);
module.svp_apply_inplace(&mut pt_dft, 0, &sk.data_fourier, col_j - 1);
module.vec_znx_idft_tmp_a(&mut pt_big, 0, &mut pt_dft, 0);
module.vec_znx_big_normalize(basek, &mut pt_want.data, 0, &pt_big, 0, scratch.borrow());
}
ct.get_row(&module, row_i, col_j, &mut ct_glwe_fourier);
ct_glwe_fourier.decrypt(&module, &mut pt_have, &sk_dft, scratch.borrow());
ct_glwe_fourier.decrypt(&module, &mut pt_have, &sk, scratch.borrow());
module.vec_znx_sub_ab_inplace(&mut pt_have.data, 0, &pt_want.data, 0);
@@ -174,22 +165,16 @@ fn test_keyswitch(log_n: usize, basek: usize, k: usize, rank: usize, sigma: f64)
let var_xs: f64 = 0.5;
let mut sk_in: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
sk_in.fill_ternary_prob(var_xs, &mut source_xs);
let mut sk_in: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank);
sk_in.fill_ternary_prob(&module, var_xs, &mut source_xs);
let mut sk_in_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
sk_in_dft.dft(&module, &sk_in);
let mut sk_out: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
sk_out.fill_ternary_prob(var_xs, &mut source_xs);
let mut sk_out_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
sk_out_dft.dft(&module, &sk_out);
let mut sk_out: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank);
sk_out.fill_ternary_prob(&module, var_xs, &mut source_xs);
ksk.generate_from_sk(
&module,
&sk_in,
&sk_out_dft,
&sk_out,
&mut source_xa,
&mut source_xe,
sigma,
@@ -197,7 +182,7 @@ fn test_keyswitch(log_n: usize, basek: usize, k: usize, rank: usize, sigma: f64)
);
tsk.generate_from_sk(
&module,
&sk_out_dft,
&sk_out,
&mut source_xa,
&mut source_xe,
sigma,
@@ -209,7 +194,7 @@ fn test_keyswitch(log_n: usize, basek: usize, k: usize, rank: usize, sigma: f64)
ct_in.encrypt_sk(
&module,
&pt_scalar,
&sk_in_dft,
&sk_in,
&mut source_xa,
&mut source_xe,
sigma,
@@ -229,14 +214,14 @@ fn test_keyswitch(log_n: usize, basek: usize, k: usize, rank: usize, sigma: f64)
// mul with sk[col_j-1]
if col_j > 0 {
module.vec_znx_dft(&mut pt_dft, 0, &pt_want.data, 0);
module.svp_apply_inplace(&mut pt_dft, 0, &sk_out_dft.data, col_j - 1);
module.svp_apply_inplace(&mut pt_dft, 0, &sk_out.data_fourier, col_j - 1);
module.vec_znx_idft_tmp_a(&mut pt_big, 0, &mut pt_dft, 0);
module.vec_znx_big_normalize(basek, &mut pt_want.data, 0, &pt_big, 0, scratch.borrow());
}
ct_out.get_row(&module, row_i, col_j, &mut ct_glwe_fourier);
ct_glwe_fourier.decrypt(&module, &mut pt_have, &sk_out_dft, scratch.borrow());
ct_glwe_fourier.decrypt(&module, &mut pt_have, &sk_out, scratch.borrow());
module.vec_znx_sub_ab_inplace(&mut pt_have.data, 0, &pt_want.data, 0);
@@ -293,22 +278,16 @@ fn test_keyswitch_inplace(log_n: usize, basek: usize, k: usize, rank: usize, sig
let var_xs: f64 = 0.5;
let mut sk_in: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
sk_in.fill_ternary_prob(var_xs, &mut source_xs);
let mut sk_in: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank);
sk_in.fill_ternary_prob(&module, var_xs, &mut source_xs);
let mut sk_in_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
sk_in_dft.dft(&module, &sk_in);
let mut sk_out: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
sk_out.fill_ternary_prob(var_xs, &mut source_xs);
let mut sk_out_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
sk_out_dft.dft(&module, &sk_out);
let mut sk_out: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank);
sk_out.fill_ternary_prob(&module, var_xs, &mut source_xs);
ksk.generate_from_sk(
&module,
&sk_in,
&sk_out_dft,
&sk_out,
&mut source_xa,
&mut source_xe,
sigma,
@@ -316,7 +295,7 @@ fn test_keyswitch_inplace(log_n: usize, basek: usize, k: usize, rank: usize, sig
);
tsk.generate_from_sk(
&module,
&sk_out_dft,
&sk_out,
&mut source_xa,
&mut source_xe,
sigma,
@@ -328,7 +307,7 @@ fn test_keyswitch_inplace(log_n: usize, basek: usize, k: usize, rank: usize, sig
ct.encrypt_sk(
&module,
&pt_scalar,
&sk_in_dft,
&sk_in,
&mut source_xa,
&mut source_xe,
sigma,
@@ -348,14 +327,14 @@ fn test_keyswitch_inplace(log_n: usize, basek: usize, k: usize, rank: usize, sig
// mul with sk[col_j-1]
if col_j > 0 {
module.vec_znx_dft(&mut pt_dft, 0, &pt_want.data, 0);
module.svp_apply_inplace(&mut pt_dft, 0, &sk_out_dft.data, col_j - 1);
module.svp_apply_inplace(&mut pt_dft, 0, &sk_out.data_fourier, col_j - 1);
module.vec_znx_idft_tmp_a(&mut pt_big, 0, &mut pt_dft, 0);
module.vec_znx_big_normalize(basek, &mut pt_want.data, 0, &pt_big, 0, scratch.borrow());
}
ct.get_row(&module, row_i, col_j, &mut ct_glwe_fourier);
ct_glwe_fourier.decrypt(&module, &mut pt_have, &sk_out_dft, scratch.borrow());
ct_glwe_fourier.decrypt(&module, &mut pt_have, &sk_out, scratch.borrow());
module.vec_znx_sub_ab_inplace(&mut pt_have.data, 0, &pt_want.data, 0);
@@ -470,11 +449,8 @@ fn test_automorphism(p: i64, log_n: usize, basek: usize, k: usize, rank: usize,
let var_xs: f64 = 0.5;
let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
sk.fill_ternary_prob(var_xs, &mut source_xs);
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
sk_dft.dft(&module, &sk);
let mut sk: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank);
sk.fill_ternary_prob(&module, var_xs, &mut source_xs);
auto_key.generate_from_sk(
&module,
@@ -487,7 +463,7 @@ fn test_automorphism(p: i64, log_n: usize, basek: usize, k: usize, rank: usize,
);
tensor_key.generate_from_sk(
&module,
&sk_dft,
&sk,
&mut source_xa,
&mut source_xe,
sigma,
@@ -499,7 +475,7 @@ fn test_automorphism(p: i64, log_n: usize, basek: usize, k: usize, rank: usize,
ct_in.encrypt_sk(
&module,
&pt_scalar,
&sk_dft,
&sk,
&mut source_xa,
&mut source_xe,
sigma,
@@ -521,14 +497,14 @@ fn test_automorphism(p: i64, log_n: usize, basek: usize, k: usize, rank: usize,
// mul with sk[col_j-1]
if col_j > 0 {
module.vec_znx_dft(&mut pt_dft, 0, &pt_want.data, 0);
module.svp_apply_inplace(&mut pt_dft, 0, &sk_dft.data, col_j - 1);
module.svp_apply_inplace(&mut pt_dft, 0, &sk.data_fourier, col_j - 1);
module.vec_znx_idft_tmp_a(&mut pt_big, 0, &mut pt_dft, 0);
module.vec_znx_big_normalize(basek, &mut pt_want.data, 0, &pt_big, 0, scratch.borrow());
}
ct_out.get_row(&module, row_i, col_j, &mut ct_glwe_fourier);
ct_glwe_fourier.decrypt(&module, &mut pt_have, &sk_dft, scratch.borrow());
ct_glwe_fourier.decrypt(&module, &mut pt_have, &sk, scratch.borrow());
module.vec_znx_sub_ab_inplace(&mut pt_have.data, 0, &pt_want.data, 0);
@@ -583,11 +559,8 @@ fn test_automorphism_inplace(p: i64, log_n: usize, basek: usize, k: usize, rank:
let var_xs: f64 = 0.5;
let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
sk.fill_ternary_prob(var_xs, &mut source_xs);
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
sk_dft.dft(&module, &sk);
let mut sk: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank);
sk.fill_ternary_prob(&module, var_xs, &mut source_xs);
auto_key.generate_from_sk(
&module,
@@ -600,7 +573,7 @@ fn test_automorphism_inplace(p: i64, log_n: usize, basek: usize, k: usize, rank:
);
tensor_key.generate_from_sk(
&module,
&sk_dft,
&sk,
&mut source_xa,
&mut source_xe,
sigma,
@@ -612,7 +585,7 @@ fn test_automorphism_inplace(p: i64, log_n: usize, basek: usize, k: usize, rank:
ct.encrypt_sk(
&module,
&pt_scalar,
&sk_dft,
&sk,
&mut source_xa,
&mut source_xe,
sigma,
@@ -634,14 +607,14 @@ fn test_automorphism_inplace(p: i64, log_n: usize, basek: usize, k: usize, rank:
// mul with sk[col_j-1]
if col_j > 0 {
module.vec_znx_dft(&mut pt_dft, 0, &pt_want.data, 0);
module.svp_apply_inplace(&mut pt_dft, 0, &sk_dft.data, col_j - 1);
module.svp_apply_inplace(&mut pt_dft, 0, &sk.data_fourier, col_j - 1);
module.vec_znx_idft_tmp_a(&mut pt_big, 0, &mut pt_dft, 0);
module.vec_znx_big_normalize(basek, &mut pt_want.data, 0, &pt_big, 0, scratch.borrow());
}
ct.get_row(&module, row_i, col_j, &mut ct_glwe_fourier);
ct_glwe_fourier.decrypt(&module, &mut pt_have, &sk_dft, scratch.borrow());
ct_glwe_fourier.decrypt(&module, &mut pt_have, &sk, scratch.borrow());
module.vec_znx_sub_ab_inplace(&mut pt_have.data, 0, &pt_want.data, 0);
@@ -705,16 +678,13 @@ fn test_external_product(log_n: usize, basek: usize, k_ggsw: usize, rank: usize,
),
);
let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
sk_dft.dft(&module, &sk);
let mut sk: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank);
sk.fill_ternary_prob(&module, 0.5, &mut source_xs);
ct_ggsw_rhs.encrypt_sk(
&module,
&pt_ggsw_rhs,
&sk_dft,
&sk,
&mut source_xa,
&mut source_xe,
sigma,
@@ -724,7 +694,7 @@ fn test_external_product(log_n: usize, basek: usize, k_ggsw: usize, rank: usize,
ct_ggsw_lhs_in.encrypt_sk(
&module,
&pt_ggsw_lhs,
&sk_dft,
&sk,
&mut source_xa,
&mut source_xe,
sigma,
@@ -747,13 +717,13 @@ fn test_external_product(log_n: usize, basek: usize, k_ggsw: usize, rank: usize,
if col_j > 0 {
module.vec_znx_dft(&mut pt_dft, 0, &pt_want.data, 0);
module.svp_apply_inplace(&mut pt_dft, 0, &sk_dft.data, col_j - 1);
module.svp_apply_inplace(&mut pt_dft, 0, &sk.data_fourier, col_j - 1);
module.vec_znx_idft_tmp_a(&mut pt_big, 0, &mut pt_dft, 0);
module.vec_znx_big_normalize(basek, &mut pt_want.data, 0, &pt_big, 0, scratch.borrow());
}
ct_ggsw_lhs_out.get_row(&module, row_i, col_j, &mut ct_glwe_fourier);
ct_glwe_fourier.decrypt(&module, &mut pt, &sk_dft, scratch.borrow());
ct_glwe_fourier.decrypt(&module, &mut pt, &sk, scratch.borrow());
module.vec_znx_sub_ab_inplace(&mut pt.data, 0, &pt_want.data, 0);
@@ -818,16 +788,13 @@ fn test_external_product_inplace(log_n: usize, basek: usize, k_ggsw: usize, rank
| GGSWCiphertext::external_product_inplace_scratch_space(&module, basek, ct_ggsw_lhs.k(), ct_ggsw_rhs.k(), rank),
);
let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
sk_dft.dft(&module, &sk);
let mut sk: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank);
sk.fill_ternary_prob(&module, 0.5, &mut source_xs);
ct_ggsw_rhs.encrypt_sk(
&module,
&pt_ggsw_rhs,
&sk_dft,
&sk,
&mut source_xa,
&mut source_xe,
sigma,
@@ -837,7 +804,7 @@ fn test_external_product_inplace(log_n: usize, basek: usize, k_ggsw: usize, rank
ct_ggsw_lhs.encrypt_sk(
&module,
&pt_ggsw_lhs,
&sk_dft,
&sk,
&mut source_xa,
&mut source_xe,
sigma,
@@ -860,13 +827,13 @@ fn test_external_product_inplace(log_n: usize, basek: usize, k_ggsw: usize, rank
if col_j > 0 {
module.vec_znx_dft(&mut pt_dft, 0, &pt_want.data, 0);
module.svp_apply_inplace(&mut pt_dft, 0, &sk_dft.data, col_j - 1);
module.svp_apply_inplace(&mut pt_dft, 0, &sk.data_fourier, col_j - 1);
module.vec_znx_idft_tmp_a(&mut pt_big, 0, &mut pt_dft, 0);
module.vec_znx_big_normalize(basek, &mut pt_want.data, 0, &pt_big, 0, scratch.borrow());
}
ct_ggsw_lhs.get_row(&module, row_i, col_j, &mut ct_glwe_fourier);
ct_glwe_fourier.decrypt(&module, &mut pt, &sk_dft, scratch.borrow());
ct_glwe_fourier.decrypt(&module, &mut pt, &sk, scratch.borrow());
module.vec_znx_sub_ab_inplace(&mut pt.data, 0, &pt_want.data, 0);

View File

@@ -6,13 +6,8 @@ use itertools::izip;
use sampling::source::Source;
use crate::{
GGSWCiphertext, GLWECiphertext, GLWECiphertextFourier, GLWEPlaintext, GLWEPublicKey, GLWESecret, Infos,
automorphism::AutomorphismKey,
elem::Infos,
ggsw_ciphertext::GGSWCiphertext,
glwe_ciphertext::GLWECiphertext,
glwe_ciphertext_fourier::GLWECiphertextFourier,
glwe_plaintext::GLWEPlaintext,
keys::{GLWEPublicKey, SecretKey, SecretKeyFourier},
keyswitch_key::GLWESwitchingKey,
test_fft64::{gglwe::log2_std_noise_gglwe_product, ggsw::noise_ggsw_product},
};
@@ -106,11 +101,8 @@ fn test_encrypt_sk(log_n: usize, basek: usize, ct_k: usize, k_pt: usize, sigma:
| GLWECiphertext::decrypt_scratch_space(&module, basek, ct.k()),
);
let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
sk_dft.dft(&module, &sk);
let mut sk: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank);
sk.fill_ternary_prob(&module, 0.5, &mut source_xs);
let mut data_want: Vec<i64> = vec![0i64; module.n()];
@@ -123,7 +115,7 @@ fn test_encrypt_sk(log_n: usize, basek: usize, ct_k: usize, k_pt: usize, sigma:
ct.encrypt_sk(
&module,
&pt,
&sk_dft,
&sk,
&mut source_xa,
&mut source_xe,
sigma,
@@ -132,7 +124,7 @@ fn test_encrypt_sk(log_n: usize, basek: usize, ct_k: usize, k_pt: usize, sigma:
pt.data.zero();
ct.decrypt(&module, &mut pt, &sk_dft, scratch.borrow());
ct.decrypt(&module, &mut pt, &sk, scratch.borrow());
let mut data_have: Vec<i64> = vec![0i64; module.n()];
@@ -161,10 +153,8 @@ fn test_encrypt_zero_sk(log_n: usize, basek: usize, ct_k: usize, sigma: f64, ran
let mut source_xe: Source = Source::new([1u8; 32]);
let mut source_xa: Source = Source::new([0u8; 32]);
let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
sk_dft.dft(&module, &sk);
let mut sk: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank);
sk.fill_ternary_prob(&module, 0.5, &mut source_xs);
let mut ct_dft: GLWECiphertextFourier<Vec<u8>, FFT64> = GLWECiphertextFourier::alloc(&module, basek, ct_k, rank);
@@ -175,13 +165,13 @@ fn test_encrypt_zero_sk(log_n: usize, basek: usize, ct_k: usize, sigma: f64, ran
ct_dft.encrypt_zero_sk(
&module,
&sk_dft,
&sk,
&mut source_xa,
&mut source_xe,
sigma,
scratch.borrow(),
);
ct_dft.decrypt(&module, &mut pt, &sk_dft, scratch.borrow());
ct_dft.decrypt(&module, &mut pt, &sk, scratch.borrow());
assert!((sigma - pt.data.std(0, basek) * (ct_k as f64).exp2()) <= 0.2);
}
@@ -197,13 +187,11 @@ fn test_encrypt_pk(log_n: usize, basek: usize, ct_k: usize, k_pk: usize, sigma:
let mut source_xa: Source = Source::new([0u8; 32]);
let mut source_xu: Source = Source::new([0u8; 32]);
let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
sk_dft.dft(&module, &sk);
let mut sk: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank);
sk.fill_ternary_prob(&module, 0.5, &mut source_xs);
let mut pk: GLWEPublicKey<Vec<u8>, FFT64> = GLWEPublicKey::alloc(&module, basek, k_pk, rank);
pk.generate_from_sk(&module, &sk_dft, &mut source_xa, &mut source_xe, sigma);
pk.generate_from_sk(&module, &sk, &mut source_xa, &mut source_xe, sigma);
let mut scratch: ScratchOwned = ScratchOwned::new(
GLWECiphertext::encrypt_sk_scratch_space(&module, basek, ct.k())
@@ -231,7 +219,7 @@ fn test_encrypt_pk(log_n: usize, basek: usize, ct_k: usize, k_pk: usize, sigma:
let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, ct_k);
ct.decrypt(&module, &mut pt_have, &sk_dft, scratch.borrow());
ct.decrypt(&module, &mut pt_have, &sk, scratch.borrow());
module.vec_znx_sub_ab_inplace(&mut pt_want.data, 0, &pt_have.data, 0);
@@ -289,22 +277,16 @@ fn test_keyswitch(
),
);
let mut sk_in: SecretKey<Vec<u8>> = SecretKey::alloc(&module, in_rank);
sk_in.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_in: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, in_rank);
sk_in.fill_ternary_prob(&module, 0.5, &mut source_xs);
let mut sk_in_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, in_rank);
sk_in_dft.dft(&module, &sk_in);
let mut sk_out: SecretKey<Vec<u8>> = SecretKey::alloc(&module, out_rank);
sk_out.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_out_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, out_rank);
sk_out_dft.dft(&module, &sk_out);
let mut sk_out: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, out_rank);
sk_out.fill_ternary_prob(&module, 0.5, &mut source_xs);
ksk.generate_from_sk(
&module,
&sk_in,
&sk_out_dft,
&sk_out,
&mut source_xa,
&mut source_xe,
sigma,
@@ -314,7 +296,7 @@ fn test_keyswitch(
ct_in.encrypt_sk(
&module,
&pt_want,
&sk_in_dft,
&sk_in,
&mut source_xa,
&mut source_xe,
sigma,
@@ -323,7 +305,7 @@ fn test_keyswitch(
ct_out.keyswitch(&module, &ct_in, &ksk, scratch.borrow());
ct_out.decrypt(&module, &mut pt_have, &sk_out_dft, scratch.borrow());
ct_out.decrypt(&module, &mut pt_have, &sk_out, scratch.borrow());
module.vec_znx_sub_ab_inplace(&mut pt_have.data, 0, &pt_want.data, 0);
@@ -374,22 +356,16 @@ fn test_keyswitch_inplace(log_n: usize, basek: usize, k_ksk: usize, ct_k: usize,
| GLWECiphertext::keyswitch_inplace_scratch_space(&module, basek, ct_glwe.k(), rank, ct_grlwe.k()),
);
let mut sk0: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
sk0.fill_ternary_prob(0.5, &mut source_xs);
let mut sk0: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank);
sk0.fill_ternary_prob(&module, 0.5, &mut source_xs);
let mut sk0_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
sk0_dft.dft(&module, &sk0);
let mut sk1: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
sk1.fill_ternary_prob(0.5, &mut source_xs);
let mut sk1_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
sk1_dft.dft(&module, &sk1);
let mut sk1: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank);
sk1.fill_ternary_prob(&module, 0.5, &mut source_xs);
ct_grlwe.generate_from_sk(
&module,
&sk0,
&sk1_dft,
&sk1,
&mut source_xa,
&mut source_xe,
sigma,
@@ -399,7 +375,7 @@ fn test_keyswitch_inplace(log_n: usize, basek: usize, k_ksk: usize, ct_k: usize,
ct_glwe.encrypt_sk(
&module,
&pt_want,
&sk0_dft,
&sk0,
&mut source_xa,
&mut source_xe,
sigma,
@@ -408,7 +384,7 @@ fn test_keyswitch_inplace(log_n: usize, basek: usize, k_ksk: usize, ct_k: usize,
ct_glwe.keyswitch_inplace(&module, &ct_grlwe, scratch.borrow());
ct_glwe.decrypt(&module, &mut pt_have, &sk1_dft, scratch.borrow());
ct_glwe.decrypt(&module, &mut pt_have, &sk1, scratch.borrow());
module.vec_znx_sub_ab_inplace(&mut pt_have.data, 0, &pt_want.data, 0);
@@ -462,17 +438,14 @@ fn test_automorphism(
.fill_uniform(basek, 0, pt_want.size(), &mut source_xa);
let mut scratch: ScratchOwned = ScratchOwned::new(
GLWESwitchingKey::encrypt_sk_scratch_space(&module, basek, autokey.k(), rank)
AutomorphismKey::generate_from_sk_scratch_space(&module, basek, autokey.k(), rank)
| GLWECiphertext::decrypt_scratch_space(&module, basek, ct_out.k())
| GLWECiphertext::encrypt_sk_scratch_space(&module, basek, ct_in.k())
| GLWECiphertext::automorphism_scratch_space(&module, basek, ct_out.k(), ct_in.k(), autokey.k(), rank),
);
let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
sk_dft.dft(&module, &sk);
let mut sk: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank);
sk.fill_ternary_prob(&module, 0.5, &mut source_xs);
autokey.generate_from_sk(
&module,
@@ -487,7 +460,7 @@ fn test_automorphism(
ct_in.encrypt_sk(
&module,
&pt_want,
&sk_dft,
&sk,
&mut source_xa,
&mut source_xe,
sigma,
@@ -495,7 +468,7 @@ fn test_automorphism(
);
ct_out.automorphism(&module, &ct_in, &autokey, scratch.borrow());
ct_out.decrypt(&module, &mut pt_have, &sk_dft, scratch.borrow());
ct_out.decrypt(&module, &mut pt_have, &sk, scratch.borrow());
module.vec_znx_automorphism_inplace(p, &mut pt_want.data, 0);
module.vec_znx_sub_ab_inplace(&mut pt_have.data, 0, &pt_want.data, 0);
module.vec_znx_normalize_inplace(basek, &mut pt_have.data, 0, scratch.borrow());
@@ -544,17 +517,14 @@ fn test_automorphism_inplace(log_n: usize, basek: usize, p: i64, k_autokey: usiz
.fill_uniform(basek, 0, pt_want.size(), &mut source_xa);
let mut scratch: ScratchOwned = ScratchOwned::new(
GLWESwitchingKey::encrypt_sk_scratch_space(&module, basek, autokey.k(), rank)
AutomorphismKey::generate_from_sk_scratch_space(&module, basek, autokey.k(), rank)
| GLWECiphertext::decrypt_scratch_space(&module, basek, ct.k())
| GLWECiphertext::encrypt_sk_scratch_space(&module, basek, ct.k())
| GLWECiphertext::automorphism_inplace_scratch_space(&module, basek, ct.k(), autokey.k(), rank),
);
let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
sk_dft.dft(&module, &sk);
let mut sk: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank);
sk.fill_ternary_prob(&module, 0.5, &mut source_xs);
autokey.generate_from_sk(
&module,
@@ -569,7 +539,7 @@ fn test_automorphism_inplace(log_n: usize, basek: usize, p: i64, k_autokey: usiz
ct.encrypt_sk(
&module,
&pt_want,
&sk_dft,
&sk,
&mut source_xa,
&mut source_xe,
sigma,
@@ -577,7 +547,7 @@ fn test_automorphism_inplace(log_n: usize, basek: usize, p: i64, k_autokey: usiz
);
ct.automorphism_inplace(&module, &autokey, scratch.borrow());
ct.decrypt(&module, &mut pt_have, &sk_dft, scratch.borrow());
ct.decrypt(&module, &mut pt_have, &sk, scratch.borrow());
module.vec_znx_automorphism_inplace(p, &mut pt_want.data, 0);
module.vec_znx_sub_ab_inplace(&mut pt_have.data, 0, &pt_want.data, 0);
module.vec_znx_normalize_inplace(basek, &mut pt_have.data, 0, scratch.borrow());
@@ -645,16 +615,13 @@ fn test_external_product(log_n: usize, basek: usize, k_ggsw: usize, ct_k_in: usi
),
);
let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
sk_dft.dft(&module, &sk);
let mut sk: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank);
sk.fill_ternary_prob(&module, 0.5, &mut source_xs);
ct_ggsw.encrypt_sk(
&module,
&pt_rgsw,
&sk_dft,
&sk,
&mut source_xa,
&mut source_xe,
sigma,
@@ -664,7 +631,7 @@ fn test_external_product(log_n: usize, basek: usize, k_ggsw: usize, ct_k_in: usi
ct_glwe_in.encrypt_sk(
&module,
&pt_want,
&sk_dft,
&sk,
&mut source_xa,
&mut source_xe,
sigma,
@@ -673,7 +640,7 @@ fn test_external_product(log_n: usize, basek: usize, k_ggsw: usize, ct_k_in: usi
ct_glwe_out.external_product(&module, &ct_glwe_in, &ct_ggsw, scratch.borrow());
ct_glwe_out.decrypt(&module, &mut pt_have, &sk_dft, scratch.borrow());
ct_glwe_out.decrypt(&module, &mut pt_have, &sk, scratch.borrow());
module.vec_znx_rotate_inplace(k as i64, &mut pt_want.data, 0);
@@ -742,16 +709,13 @@ fn test_external_product_inplace(log_n: usize, basek: usize, k_ggsw: usize, ct_k
| GLWECiphertext::external_product_inplace_scratch_space(&module, basek, ct_glwe.k(), ct_ggsw.k(), rank),
);
let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
sk_dft.dft(&module, &sk);
let mut sk: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank);
sk.fill_ternary_prob(&module, 0.5, &mut source_xs);
ct_ggsw.encrypt_sk(
&module,
&pt_rgsw,
&sk_dft,
&sk,
&mut source_xa,
&mut source_xe,
sigma,
@@ -761,7 +725,7 @@ fn test_external_product_inplace(log_n: usize, basek: usize, k_ggsw: usize, ct_k
ct_glwe.encrypt_sk(
&module,
&pt_want,
&sk_dft,
&sk,
&mut source_xa,
&mut source_xe,
sigma,
@@ -770,7 +734,7 @@ fn test_external_product_inplace(log_n: usize, basek: usize, k_ggsw: usize, ct_k
ct_glwe.external_product_inplace(&module, &ct_ggsw, scratch.borrow());
ct_glwe.decrypt(&module, &mut pt_have, &sk_dft, scratch.borrow());
ct_glwe.decrypt(&module, &mut pt_have, &sk, scratch.borrow());
module.vec_znx_rotate_inplace(k as i64, &mut pt_want.data, 0);

View File

@@ -1,12 +1,5 @@
use crate::{
GLWEOps,
elem::Infos,
ggsw_ciphertext::GGSWCiphertext,
glwe_ciphertext::GLWECiphertext,
glwe_ciphertext_fourier::GLWECiphertextFourier,
glwe_plaintext::GLWEPlaintext,
keys::{SecretKey, SecretKeyFourier},
keyswitch_key::GLWESwitchingKey,
GGSWCiphertext, GLWECiphertext, GLWECiphertextFourier, GLWEOps, GLWEPlaintext, GLWESecret, GLWESwitchingKey, Infos,
test_fft64::{gglwe::log2_std_noise_gglwe_product, ggsw::noise_ggsw_product},
};
use backend::{FFT64, FillUniform, Module, ScalarZnx, ScalarZnxAlloc, ScratchOwned, Stats, VecZnxOps, ZnxViewMut};
@@ -94,22 +87,16 @@ fn test_keyswitch(
),
);
let mut sk_in: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank_in);
sk_in.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_in: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank_in);
sk_in.fill_ternary_prob(&module, 0.5, &mut source_xs);
let mut sk_in_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank_in);
sk_in_dft.dft(&module, &sk_in);
let mut sk_out: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank_out);
sk_out.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_out_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank_out);
sk_out_dft.dft(&module, &sk_out);
let mut sk_out: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank_out);
sk_out.fill_ternary_prob(&module, 0.5, &mut source_xs);
ksk.generate_from_sk(
&module,
&sk_in,
&sk_out_dft,
&sk_out,
&mut source_xa,
&mut source_xe,
sigma,
@@ -119,7 +106,7 @@ fn test_keyswitch(
ct_glwe_in.encrypt_sk(
&module,
&pt_want,
&sk_in_dft,
&sk_in,
&mut source_xa,
&mut source_xe,
sigma,
@@ -130,7 +117,7 @@ fn test_keyswitch(
ct_glwe_dft_out.keyswitch(&module, &ct_glwe_dft_in, &ksk, scratch.borrow());
ct_glwe_dft_out.idft(&module, &mut ct_glwe_out, scratch.borrow());
ct_glwe_out.decrypt(&module, &mut pt_have, &sk_out_dft, scratch.borrow());
ct_glwe_out.decrypt(&module, &mut pt_have, &sk_out, scratch.borrow());
module.vec_znx_sub_ab_inplace(&mut pt_have.data, 0, &pt_want.data, 0);
@@ -182,22 +169,16 @@ fn test_keyswitch_inplace(log_n: usize, basek: usize, k_ksk: usize, k_ct: usize,
| GLWECiphertextFourier::keyswitch_inplace_scratch_space(&module, basek, ct_rlwe_dft.k(), ksk.k(), rank),
);
let mut sk_in: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
sk_in.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_in: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank);
sk_in.fill_ternary_prob(&module, 0.5, &mut source_xs);
let mut sk_in_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
sk_in_dft.dft(&module, &sk_in);
let mut sk_out: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
sk_out.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_out_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
sk_out_dft.dft(&module, &sk_out);
let mut sk_out: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank);
sk_out.fill_ternary_prob(&module, 0.5, &mut source_xs);
ksk.generate_from_sk(
&module,
&sk_in,
&sk_out_dft,
&sk_out,
&mut source_xa,
&mut source_xe,
sigma,
@@ -207,7 +188,7 @@ fn test_keyswitch_inplace(log_n: usize, basek: usize, k_ksk: usize, k_ct: usize,
ct_glwe.encrypt_sk(
&module,
&pt_want,
&sk_in_dft,
&sk_in,
&mut source_xa,
&mut source_xe,
sigma,
@@ -218,7 +199,7 @@ fn test_keyswitch_inplace(log_n: usize, basek: usize, k_ksk: usize, k_ct: usize,
ct_rlwe_dft.keyswitch_inplace(&module, &ksk, scratch.borrow());
ct_rlwe_dft.idft(&module, &mut ct_glwe, scratch.borrow());
ct_glwe.decrypt(&module, &mut pt_have, &sk_out_dft, scratch.borrow());
ct_glwe.decrypt(&module, &mut pt_have, &sk_out, scratch.borrow());
module.vec_znx_sub_ab_inplace(&mut pt_have.data, 0, &pt_want.data, 0);
@@ -281,16 +262,13 @@ fn test_external_product(log_n: usize, basek: usize, k_ggsw: usize, k_ct_in: usi
| GLWECiphertextFourier::external_product_scratch_space(&module, basek, ct_out.k(), ct_in.k(), ct_ggsw.k(), rank),
);
let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
sk_dft.dft(&module, &sk);
let mut sk: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank);
sk.fill_ternary_prob(&module, 0.5, &mut source_xs);
ct_ggsw.encrypt_sk(
&module,
&pt_rgsw,
&sk_dft,
&sk,
&mut source_xa,
&mut source_xe,
sigma,
@@ -300,7 +278,7 @@ fn test_external_product(log_n: usize, basek: usize, k_ggsw: usize, k_ct_in: usi
ct_in.encrypt_sk(
&module,
&pt_want,
&sk_dft,
&sk,
&mut source_xa,
&mut source_xe,
sigma,
@@ -311,7 +289,7 @@ fn test_external_product(log_n: usize, basek: usize, k_ggsw: usize, k_ct_in: usi
ct_out_dft.external_product(&module, &ct_in_dft, &ct_ggsw, scratch.borrow());
ct_out_dft.idft(&module, &mut ct_out, scratch.borrow());
ct_out.decrypt(&module, &mut pt_have, &sk_dft, scratch.borrow());
ct_out.decrypt(&module, &mut pt_have, &sk, scratch.borrow());
pt_want.rotate_inplace(&module, k);
pt_have.sub_inplace_ab(&module, &pt_want);
@@ -381,16 +359,13 @@ fn test_external_product_inplace(log_n: usize, basek: usize, k_ggsw: usize, k_ct
| GLWECiphertextFourier::external_product_inplace_scratch_space(&module, basek, ct.k(), ct_ggsw.k(), rank),
);
let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
sk_dft.dft(&module, &sk);
let mut sk: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank);
sk.fill_ternary_prob(&module, 0.5, &mut source_xs);
ct_ggsw.encrypt_sk(
&module,
&pt_rgsw,
&sk_dft,
&sk,
&mut source_xa,
&mut source_xe,
sigma,
@@ -400,7 +375,7 @@ fn test_external_product_inplace(log_n: usize, basek: usize, k_ggsw: usize, k_ct
ct.encrypt_sk(
&module,
&pt_want,
&sk_dft,
&sk,
&mut source_xa,
&mut source_xe,
sigma,
@@ -411,7 +386,7 @@ fn test_external_product_inplace(log_n: usize, basek: usize, k_ggsw: usize, k_ct
ct_rlwe_dft.external_product_inplace(&module, &ct_ggsw, scratch.borrow());
ct_rlwe_dft.idft(&module, &mut ct, scratch.borrow());
ct.decrypt(&module, &mut pt_have, &sk_dft, scratch.borrow());
ct.decrypt(&module, &mut pt_have, &sk, scratch.borrow());
pt_want.rotate_inplace(&module, k);
pt_have.sub_inplace_ab(&module, &pt_want);

View File

@@ -1,11 +1,4 @@
use crate::{
automorphism::AutomorphismKey,
glwe_ciphertext::GLWECiphertext,
glwe_ops::GLWEOps,
glwe_packing::StreamPacker,
glwe_plaintext::GLWEPlaintext,
keys::{SecretKey, SecretKeyFourier},
};
use crate::{AutomorphismKey, GLWECiphertext, GLWEOps, GLWEPlaintext, GLWESecret, StreamPacker};
use std::collections::HashMap;
use backend::{Encoding, FFT64, Module, ScratchOwned, Stats};
@@ -35,11 +28,8 @@ fn packing() {
| StreamPacker::scratch_space(&module, basek, ct_k, atk_k, rank),
);
let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
sk_dft.dft(&module, &sk);
let mut sk: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank);
sk.fill_ternary_prob(&module, 0.5, &mut source_xs);
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, ct_k);
let mut data: Vec<i64> = vec![0i64; module.n()];
@@ -74,7 +64,7 @@ fn packing() {
ct.encrypt_sk(
&module,
&pt,
&sk_dft,
&sk,
&mut source_xa,
&mut source_xe,
sigma,
@@ -87,7 +77,7 @@ fn packing() {
ct.encrypt_sk(
&module,
&pt,
&sk_dft,
&sk,
&mut source_xa,
&mut source_xe,
sigma,
@@ -123,7 +113,7 @@ fn packing() {
});
pt_want.data.encode_vec_i64(0, basek, pt_k, &data, 32);
res_i.decrypt(&module, &mut pt, &sk_dft, scratch.borrow());
res_i.decrypt(&module, &mut pt, &sk, scratch.borrow());
if i & 1 == 0 {
pt.sub_inplace_ab(&module, &pt_want);

View File

@@ -1,15 +1,7 @@
use backend::{
FFT64, Module, ScalarZnx, ScalarZnxDft, ScalarZnxDftAlloc, ScalarZnxDftOps, ScratchOwned, Stats, VecZnxDftOps, VecZnxOps,
};
use backend::{FFT64, Module, ScalarZnxDftOps, ScratchOwned, Stats, VecZnxOps};
use sampling::source::Source;
use crate::{
elem::{GetRow, Infos},
glwe_ciphertext_fourier::GLWECiphertextFourier,
glwe_plaintext::GLWEPlaintext,
keys::{SecretKey, SecretKeyFourier},
tensor_key::TensorKey,
};
use crate::{GLWECiphertextFourier, GLWEPlaintext, GLWESecret, GetRow, Infos, TensorKey};
#[test]
fn encrypt_sk() {
@@ -37,15 +29,12 @@ fn test_encrypt_sk(log_n: usize, basek: usize, k: usize, sigma: f64, rank: usize
rank,
));
let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
sk_dft.dft(&module, &sk);
let mut sk: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank);
sk.fill_ternary_prob(&module, 0.5, &mut source_xs);
tensor_key.generate_from_sk(
&module,
&sk_dft,
&sk,
&mut source_xa,
&mut source_xe,
sigma,
@@ -55,22 +44,26 @@ fn test_encrypt_sk(log_n: usize, basek: usize, k: usize, sigma: f64, rank: usize
let mut ct_glwe_fourier: GLWECiphertextFourier<Vec<u8>, FFT64> = GLWECiphertextFourier::alloc(&module, basek, k, rank);
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&module, basek, k);
let mut sk_ij = GLWESecret::alloc(&module, 1);
(0..rank).for_each(|i| {
(0..rank).for_each(|j| {
let mut sk_ij_dft: ScalarZnxDft<Vec<u8>, FFT64> = module.new_scalar_znx_dft(1);
module.svp_apply(&mut sk_ij_dft, 0, &sk_dft.data, i, &sk_dft.data, j);
let sk_ij: ScalarZnx<Vec<u8>> = module
.vec_znx_idft_consume(sk_ij_dft.as_vec_znx_dft())
.to_vec_znx_small()
.to_scalar_znx();
module.svp_apply(
&mut sk_ij.data_fourier,
0,
&sk.data_fourier,
i,
&sk.data_fourier,
j,
);
module.svp_idft(&mut sk_ij.data, 0, &sk_ij.data_fourier, 0, scratch.borrow());
(0..tensor_key.rank_in()).for_each(|col_i| {
(0..tensor_key.rows()).for_each(|row_i| {
tensor_key
.at(i, j)
.get_row(&module, row_i, col_i, &mut ct_glwe_fourier);
ct_glwe_fourier.decrypt(&module, &mut pt, &sk_dft, scratch.borrow());
module.vec_znx_sub_scalar_inplace(&mut pt.data, 0, row_i, &sk_ij, col_i);
ct_glwe_fourier.decrypt(&module, &mut pt, &sk, scratch.borrow());
module.vec_znx_sub_scalar_inplace(&mut pt.data, 0, row_i, &sk_ij.data, col_i);
let std_pt: f64 = pt.data.std(0, basek) * (k as f64).exp2();
assert!((sigma - std_pt).abs() <= 0.2, "{} {}", sigma, std_pt);
});

View File

@@ -3,14 +3,7 @@ use std::collections::HashMap;
use backend::{FFT64, FillUniform, Module, ScratchOwned, Stats, VecZnxOps, ZnxView, ZnxViewMut};
use sampling::source::Source;
use crate::{
automorphism::AutomorphismKey,
elem::Infos,
glwe_ciphertext::GLWECiphertext,
glwe_plaintext::GLWEPlaintext,
keys::{SecretKey, SecretKeyFourier},
test_fft64::gglwe::var_noise_gglwe_product,
};
use crate::{AutomorphismKey, GLWECiphertext, GLWEPlaintext, GLWESecret, Infos, test_fft64::gglwe::var_noise_gglwe_product};
#[test]
fn trace_inplace() {
@@ -42,11 +35,8 @@ fn test_trace_inplace(log_n: usize, basek: usize, k: usize, sigma: f64, rank: us
| GLWECiphertext::trace_inplace_scratch_space(&module, basek, ct.k(), k_autokey, rank),
);
let mut sk: SecretKey<Vec<u8>> = SecretKey::alloc(&module, rank);
sk.fill_ternary_prob(0.5, &mut source_xs);
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::alloc(&module, rank);
sk_dft.dft(&module, &sk);
let mut sk: GLWESecret<Vec<u8>, FFT64> = GLWESecret::alloc(&module, rank);
sk.fill_ternary_prob(&&module, 0.5, &mut source_xs);
let mut data_want: Vec<i64> = vec![0i64; module.n()];
@@ -61,7 +51,7 @@ fn test_trace_inplace(log_n: usize, basek: usize, k: usize, sigma: f64, rank: us
ct.encrypt_sk(
&module,
&pt_have,
&sk_dft,
&sk,
&mut source_xa,
&mut source_xe,
sigma,
@@ -89,7 +79,7 @@ fn test_trace_inplace(log_n: usize, basek: usize, k: usize, sigma: f64, rank: us
(0..pt_want.size()).for_each(|i| pt_want.data.at_mut(0, i)[0] = pt_have.data.at(0, i)[0]);
ct.decrypt(&module, &mut pt_have, &sk_dft, scratch.borrow());
ct.decrypt(&module, &mut pt_have, &sk, scratch.borrow());
module.vec_znx_sub_ab_inplace(&mut pt_want.data, 0, &pt_have.data, 0);
module.vec_znx_normalize_inplace(basek, &mut pt_want.data, 0, scratch.borrow());