|
|
|
@@ -1,4 +1,4 @@
|
|
|
|
use base2k::{FFT64, Module, ScalarZnx, ScalarZnxAlloc, ScratchOwned, Stats, VecZnxOps, ZnxViewMut};
|
|
|
|
use base2k::{FFT64, Module, ScalarZnx, ScalarZnxAlloc, ScalarZnxToMut, ScratchOwned, Stats, VecZnxOps, ZnxViewMut};
|
|
|
|
use sampling::source::Source;
|
|
|
|
use sampling::source::Source;
|
|
|
|
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
use crate::{
|
|
|
|
@@ -8,6 +8,7 @@ use crate::{
|
|
|
|
glwe_plaintext::GLWEPlaintext,
|
|
|
|
glwe_plaintext::GLWEPlaintext,
|
|
|
|
keys::{SecretKey, SecretKeyFourier},
|
|
|
|
keys::{SecretKey, SecretKeyFourier},
|
|
|
|
keyswitch_key::GLWESwitchingKey,
|
|
|
|
keyswitch_key::GLWESwitchingKey,
|
|
|
|
|
|
|
|
test_fft64::ggsw::noise_ggsw_product,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[test]
|
|
|
|
@@ -15,7 +16,58 @@ fn encrypt_sk() {
|
|
|
|
(1..4).for_each(|rank_in| {
|
|
|
|
(1..4).for_each(|rank_in| {
|
|
|
|
(1..4).for_each(|rank_out| {
|
|
|
|
(1..4).for_each(|rank_out| {
|
|
|
|
println!("test encrypt_sk rank_in rank_out: {} {}", rank_in, rank_out);
|
|
|
|
println!("test encrypt_sk rank_in rank_out: {} {}", rank_in, rank_out);
|
|
|
|
test_encrypt_sk(11, 8, 54, 3.2, rank_in, rank_out);
|
|
|
|
test_encrypt_sk(12, 8, 54, 3.2, rank_in, rank_out);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
|
|
|
fn key_switch() {
|
|
|
|
|
|
|
|
(1..4).for_each(|rank_in_s0s1| {
|
|
|
|
|
|
|
|
(1..4).for_each(|rank_out_s0s1| {
|
|
|
|
|
|
|
|
(1..4).for_each(|rank_out_s1s2| {
|
|
|
|
|
|
|
|
println!(
|
|
|
|
|
|
|
|
"test key_switch : ({},{},{})",
|
|
|
|
|
|
|
|
rank_in_s0s1, rank_out_s0s1, rank_out_s1s2
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
test_key_switch(12, 15, 60, 3.2, rank_in_s0s1, rank_out_s0s1, rank_out_s1s2);
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
|
|
|
fn key_switch_inplace() {
|
|
|
|
|
|
|
|
(1..4).for_each(|rank_in_s0s1| {
|
|
|
|
|
|
|
|
(1..4).for_each(|rank_out_s0s1| {
|
|
|
|
|
|
|
|
println!(
|
|
|
|
|
|
|
|
"test key_switch_inplace : ({},{})",
|
|
|
|
|
|
|
|
rank_in_s0s1, rank_out_s0s1
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
test_key_switch_inplace(12, 15, 60, 3.2, rank_in_s0s1, rank_out_s0s1);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
|
|
|
fn external_product() {
|
|
|
|
|
|
|
|
(1..4).for_each(|rank_in| {
|
|
|
|
|
|
|
|
(1..4).for_each(|rank_out| {
|
|
|
|
|
|
|
|
println!("test external_product rank: {} {}", rank_in, rank_out);
|
|
|
|
|
|
|
|
test_external_product(12, 12, 60, 3.2, rank_in, rank_out);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
|
|
|
fn external_product_inplace() {
|
|
|
|
|
|
|
|
(1..4).for_each(|rank_in| {
|
|
|
|
|
|
|
|
(1..4).for_each(|rank_out| {
|
|
|
|
|
|
|
|
println!(
|
|
|
|
|
|
|
|
"test external_product_inplace rank: {} {}",
|
|
|
|
|
|
|
|
rank_in, rank_out
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
test_external_product_inplace(12, 12, 60, 3.2, rank_in, rank_out);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@@ -58,12 +110,12 @@ fn test_encrypt_sk(log_n: usize, basek: usize, k_ksk: usize, sigma: f64, rank_in
|
|
|
|
scratch.borrow(),
|
|
|
|
scratch.borrow(),
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
let mut ct_gglwe_fourier: GLWECiphertextFourier<Vec<u8>, FFT64> = GLWECiphertextFourier::new(&module, basek, k_ksk, rank_out);
|
|
|
|
let mut ct_glwe_fourier: GLWECiphertextFourier<Vec<u8>, FFT64> = GLWECiphertextFourier::new(&module, basek, k_ksk, rank_out);
|
|
|
|
|
|
|
|
|
|
|
|
(0..ksk.rank_in()).for_each(|col_i| {
|
|
|
|
(0..ksk.rank_in()).for_each(|col_i| {
|
|
|
|
(0..ksk.rows()).for_each(|row_i| {
|
|
|
|
(0..ksk.rows()).for_each(|row_i| {
|
|
|
|
ksk.get_row(&module, row_i, 0, &mut ct_gglwe_fourier);
|
|
|
|
ksk.get_row(&module, row_i, col_i, &mut ct_glwe_fourier);
|
|
|
|
ct_gglwe_fourier.decrypt(&module, &mut pt, &sk_out_dft, scratch.borrow());
|
|
|
|
ct_glwe_fourier.decrypt(&module, &mut pt, &sk_out_dft, scratch.borrow());
|
|
|
|
module.vec_znx_sub_scalar_inplace(&mut pt, 0, row_i, &sk_in, col_i);
|
|
|
|
module.vec_znx_sub_scalar_inplace(&mut pt, 0, row_i, &sk_in, col_i);
|
|
|
|
let std_pt: f64 = pt.data.std(0, basek) * (k_ksk as f64).exp2();
|
|
|
|
let std_pt: f64 = pt.data.std(0, basek) * (k_ksk as f64).exp2();
|
|
|
|
assert!((sigma - std_pt).abs() <= 0.2, "{} {}", sigma, std_pt);
|
|
|
|
assert!((sigma - std_pt).abs() <= 0.2, "{} {}", sigma, std_pt);
|
|
|
|
@@ -71,61 +123,64 @@ fn test_encrypt_sk(log_n: usize, basek: usize, k_ksk: usize, sigma: f64, rank_in
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_key_switch(
|
|
|
|
fn keyswitch() {
|
|
|
|
log_n: usize,
|
|
|
|
let module: Module<FFT64> = Module::<FFT64>::new(2048);
|
|
|
|
basek: usize,
|
|
|
|
let basek: usize = 12;
|
|
|
|
k_ksk: usize,
|
|
|
|
let log_k_grlwe: usize = 60;
|
|
|
|
sigma: f64,
|
|
|
|
let rows: usize = (log_k_grlwe + basek - 1) / basek;
|
|
|
|
rank_in_s0s1: usize,
|
|
|
|
|
|
|
|
rank_out_s0s1: usize,
|
|
|
|
|
|
|
|
rank_out_s1s2: usize,
|
|
|
|
|
|
|
|
) {
|
|
|
|
|
|
|
|
let module: Module<FFT64> = Module::<FFT64>::new(1 << log_n);
|
|
|
|
|
|
|
|
let rows = (k_ksk + basek - 1) / basek;
|
|
|
|
|
|
|
|
|
|
|
|
let rank: usize = 1;
|
|
|
|
let mut ct_gglwe_s0s1: GLWESwitchingKey<Vec<u8>, FFT64> =
|
|
|
|
|
|
|
|
GLWESwitchingKey::new(&module, basek, k_ksk, rows, rank_in_s0s1, rank_out_s0s1);
|
|
|
|
let sigma: f64 = 3.2;
|
|
|
|
let mut ct_gglwe_s1s2: GLWESwitchingKey<Vec<u8>, FFT64> =
|
|
|
|
|
|
|
|
GLWESwitchingKey::new(&module, basek, k_ksk, rows, rank_out_s0s1, rank_out_s1s2);
|
|
|
|
let mut ct_grlwe_s0s1: GLWESwitchingKey<Vec<u8>, FFT64> =
|
|
|
|
let mut ct_gglwe_s0s2: GLWESwitchingKey<Vec<u8>, FFT64> =
|
|
|
|
GLWESwitchingKey::new(&module, basek, log_k_grlwe, rows, rank, rank);
|
|
|
|
GLWESwitchingKey::new(&module, basek, k_ksk, rows, rank_in_s0s1, rank_out_s1s2);
|
|
|
|
let mut ct_grlwe_s1s2: GLWESwitchingKey<Vec<u8>, FFT64> =
|
|
|
|
|
|
|
|
GLWESwitchingKey::new(&module, basek, log_k_grlwe, rows, rank, rank);
|
|
|
|
|
|
|
|
let mut ct_grlwe_s0s2: GLWESwitchingKey<Vec<u8>, FFT64> =
|
|
|
|
|
|
|
|
GLWESwitchingKey::new(&module, basek, log_k_grlwe, rows, rank, rank);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let mut source_xs: Source = Source::new([0u8; 32]);
|
|
|
|
let mut source_xs: Source = Source::new([0u8; 32]);
|
|
|
|
let mut source_xe: 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 source_xa: Source = Source::new([0u8; 32]);
|
|
|
|
|
|
|
|
|
|
|
|
let mut scratch: ScratchOwned = ScratchOwned::new(
|
|
|
|
let mut scratch: ScratchOwned = ScratchOwned::new(
|
|
|
|
GLWESwitchingKey::encrypt_sk_scratch_space(&module, rank, ct_grlwe_s0s1.size())
|
|
|
|
GLWESwitchingKey::encrypt_sk_scratch_space(&module, rank_in_s0s1 | rank_out_s0s1, ct_gglwe_s0s1.size())
|
|
|
|
| GLWECiphertextFourier::decrypt_scratch_space(&module, ct_grlwe_s0s2.size())
|
|
|
|
| GLWECiphertextFourier::decrypt_scratch_space(&module, ct_gglwe_s0s2.size())
|
|
|
|
| GLWESwitchingKey::keyswitch_scratch_space(
|
|
|
|
| GLWESwitchingKey::keyswitch_scratch_space(
|
|
|
|
&module,
|
|
|
|
&module,
|
|
|
|
ct_grlwe_s0s2.size(),
|
|
|
|
ct_gglwe_s0s2.size(),
|
|
|
|
ct_grlwe_s0s1.size(),
|
|
|
|
ct_gglwe_s0s2.rank(),
|
|
|
|
ct_grlwe_s1s2.size(),
|
|
|
|
ct_gglwe_s0s1.size(),
|
|
|
|
|
|
|
|
ct_gglwe_s0s1.rank(),
|
|
|
|
|
|
|
|
ct_gglwe_s1s2.size(),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
let mut sk0: SecretKey<Vec<u8>> = SecretKey::new(&module, rank);
|
|
|
|
let mut sk0: SecretKey<Vec<u8>> = SecretKey::new(&module, rank_in_s0s1);
|
|
|
|
sk0.fill_ternary_prob(0.5, &mut source_xs);
|
|
|
|
sk0.fill_ternary_prob(0.5, &mut source_xs);
|
|
|
|
|
|
|
|
|
|
|
|
let mut sk0_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank);
|
|
|
|
let mut sk0_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank_in_s0s1);
|
|
|
|
sk0_dft.dft(&module, &sk0);
|
|
|
|
sk0_dft.dft(&module, &sk0);
|
|
|
|
|
|
|
|
|
|
|
|
let mut sk1: SecretKey<Vec<u8>> = SecretKey::new(&module, rank);
|
|
|
|
let mut sk1: SecretKey<Vec<u8>> = SecretKey::new(&module, rank_out_s0s1);
|
|
|
|
sk1.fill_ternary_prob(0.5, &mut source_xs);
|
|
|
|
sk1.fill_ternary_prob(0.5, &mut source_xs);
|
|
|
|
|
|
|
|
|
|
|
|
let mut sk1_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank);
|
|
|
|
let mut sk1_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank_out_s0s1);
|
|
|
|
sk1_dft.dft(&module, &sk1);
|
|
|
|
sk1_dft.dft(&module, &sk1);
|
|
|
|
|
|
|
|
|
|
|
|
let mut sk2: SecretKey<Vec<u8>> = SecretKey::new(&module, rank);
|
|
|
|
let mut sk2: SecretKey<Vec<u8>> = SecretKey::new(&module, rank_out_s1s2);
|
|
|
|
sk2.fill_ternary_prob(0.5, &mut source_xs);
|
|
|
|
sk2.fill_ternary_prob(0.5, &mut source_xs);
|
|
|
|
|
|
|
|
|
|
|
|
let mut sk2_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank);
|
|
|
|
let mut sk2_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank_out_s1s2);
|
|
|
|
sk2_dft.dft(&module, &sk2);
|
|
|
|
sk2_dft.dft(&module, &sk2);
|
|
|
|
|
|
|
|
|
|
|
|
// GRLWE_{s1}(s0) = s0 -> s1
|
|
|
|
// gglwe_{s1}(s0) = s0 -> s1
|
|
|
|
ct_grlwe_s0s1.encrypt_sk(
|
|
|
|
ct_gglwe_s0s1.encrypt_sk(
|
|
|
|
&module,
|
|
|
|
&module,
|
|
|
|
&sk0.data,
|
|
|
|
&sk0,
|
|
|
|
&sk1_dft,
|
|
|
|
&sk1_dft,
|
|
|
|
&mut source_xa,
|
|
|
|
&mut source_xa,
|
|
|
|
&mut source_xe,
|
|
|
|
&mut source_xe,
|
|
|
|
@@ -133,10 +188,10 @@ fn keyswitch() {
|
|
|
|
scratch.borrow(),
|
|
|
|
scratch.borrow(),
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
// GRLWE_{s2}(s1) -> s1 -> s2
|
|
|
|
// gglwe_{s2}(s1) -> s1 -> s2
|
|
|
|
ct_grlwe_s1s2.encrypt_sk(
|
|
|
|
ct_gglwe_s1s2.encrypt_sk(
|
|
|
|
&module,
|
|
|
|
&module,
|
|
|
|
&sk1.data,
|
|
|
|
&sk1,
|
|
|
|
&sk2_dft,
|
|
|
|
&sk2_dft,
|
|
|
|
&mut source_xa,
|
|
|
|
&mut source_xa,
|
|
|
|
&mut source_xe,
|
|
|
|
&mut source_xe,
|
|
|
|
@@ -144,89 +199,88 @@ fn keyswitch() {
|
|
|
|
scratch.borrow(),
|
|
|
|
scratch.borrow(),
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
// GRLWE_{s1}(s0) (x) GRLWE_{s2}(s1) = GRLWE_{s2}(s0)
|
|
|
|
// gglwe_{s1}(s0) (x) gglwe_{s2}(s1) = gglwe_{s2}(s0)
|
|
|
|
ct_grlwe_s0s2.keyswitch(&module, &ct_grlwe_s0s1, &ct_grlwe_s1s2, scratch.borrow());
|
|
|
|
ct_gglwe_s0s2.keyswitch(&module, &ct_gglwe_s0s1, &ct_gglwe_s1s2, scratch.borrow());
|
|
|
|
|
|
|
|
|
|
|
|
let mut ct_rlwe_dft_s0s2: GLWECiphertextFourier<Vec<u8>, FFT64> =
|
|
|
|
let mut ct_glwe_dft: GLWECiphertextFourier<Vec<u8>, FFT64> = GLWECiphertextFourier::new(&module, basek, k_ksk, rank_out_s1s2);
|
|
|
|
GLWECiphertextFourier::new(&module, basek, log_k_grlwe, rank);
|
|
|
|
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, basek, k_ksk);
|
|
|
|
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, basek, log_k_grlwe);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(0..ct_grlwe_s0s2.rows()).for_each(|row_i| {
|
|
|
|
(0..ct_gglwe_s0s2.rank_in()).for_each(|col_i| {
|
|
|
|
ct_grlwe_s0s2.get_row(&module, row_i, 0, &mut ct_rlwe_dft_s0s2);
|
|
|
|
(0..ct_gglwe_s0s2.rows()).for_each(|row_i| {
|
|
|
|
ct_rlwe_dft_s0s2.decrypt(&module, &mut pt, &sk2_dft, scratch.borrow());
|
|
|
|
ct_gglwe_s0s2.get_row(&module, row_i, col_i, &mut ct_glwe_dft);
|
|
|
|
module.vec_znx_sub_scalar_inplace(&mut pt, 0, row_i, &sk0, 0);
|
|
|
|
ct_glwe_dft.decrypt(&module, &mut pt, &sk2_dft, scratch.borrow());
|
|
|
|
|
|
|
|
module.vec_znx_sub_scalar_inplace(&mut pt, 0, row_i, &sk0, col_i);
|
|
|
|
|
|
|
|
|
|
|
|
let noise_have: f64 = pt.data.std(0, basek).log2();
|
|
|
|
let noise_have: f64 = pt.data.std(0, basek).log2();
|
|
|
|
let noise_want: f64 = noise_grlwe_rlwe_product(
|
|
|
|
let noise_want: f64 = noise_gglwe_product(
|
|
|
|
module.n() as f64,
|
|
|
|
module.n() as f64,
|
|
|
|
basek,
|
|
|
|
basek,
|
|
|
|
0.5,
|
|
|
|
0.5,
|
|
|
|
0.5,
|
|
|
|
0.5,
|
|
|
|
0f64,
|
|
|
|
0f64,
|
|
|
|
sigma * sigma,
|
|
|
|
sigma * sigma,
|
|
|
|
0f64,
|
|
|
|
0f64,
|
|
|
|
log_k_grlwe,
|
|
|
|
rank_out_s0s1 as f64,
|
|
|
|
log_k_grlwe,
|
|
|
|
k_ksk,
|
|
|
|
);
|
|
|
|
k_ksk,
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
assert!(
|
|
|
|
assert!(
|
|
|
|
(noise_have - noise_want).abs() <= 0.1,
|
|
|
|
(noise_have - noise_want).abs() <= 0.1,
|
|
|
|
"{} {}",
|
|
|
|
"{} {}",
|
|
|
|
noise_have,
|
|
|
|
noise_have,
|
|
|
|
noise_want
|
|
|
|
noise_want
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_key_switch_inplace(log_n: usize, basek: usize, k_ksk: usize, sigma: f64, rank_in_s0s1: usize, rank_out_s0s1: usize) {
|
|
|
|
fn keyswitch_inplace() {
|
|
|
|
let module: Module<FFT64> = Module::<FFT64>::new(1 << log_n);
|
|
|
|
let module: Module<FFT64> = Module::<FFT64>::new(2048);
|
|
|
|
let rows: usize = (k_ksk + basek - 1) / basek;
|
|
|
|
let basek: usize = 12;
|
|
|
|
|
|
|
|
let log_k_grlwe: usize = 60;
|
|
|
|
|
|
|
|
let rows: usize = (log_k_grlwe + basek - 1) / basek;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let rank: usize = 1;
|
|
|
|
let mut ct_gglwe_s0s1: GLWESwitchingKey<Vec<u8>, FFT64> =
|
|
|
|
let rank_out: usize = 1;
|
|
|
|
GLWESwitchingKey::new(&module, basek, k_ksk, rows, rank_in_s0s1, rank_out_s0s1);
|
|
|
|
|
|
|
|
let mut ct_gglwe_s1s2: GLWESwitchingKey<Vec<u8>, FFT64> =
|
|
|
|
let sigma: f64 = 3.2;
|
|
|
|
GLWESwitchingKey::new(&module, basek, k_ksk, rows, rank_out_s0s1, rank_out_s0s1);
|
|
|
|
|
|
|
|
|
|
|
|
let mut ct_grlwe_s0s1: GLWESwitchingKey<Vec<u8>, FFT64> =
|
|
|
|
|
|
|
|
GLWESwitchingKey::new(&module, basek, log_k_grlwe, rows, rank, rank_out);
|
|
|
|
|
|
|
|
let mut ct_grlwe_s1s2: GLWESwitchingKey<Vec<u8>, FFT64> =
|
|
|
|
|
|
|
|
GLWESwitchingKey::new(&module, basek, log_k_grlwe, rows, rank, rank_out);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let mut source_xs: Source = Source::new([0u8; 32]);
|
|
|
|
let mut source_xs: Source = Source::new([0u8; 32]);
|
|
|
|
let mut source_xe: 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 source_xa: Source = Source::new([0u8; 32]);
|
|
|
|
|
|
|
|
|
|
|
|
let mut scratch: ScratchOwned = ScratchOwned::new(
|
|
|
|
let mut scratch: ScratchOwned = ScratchOwned::new(
|
|
|
|
GLWESwitchingKey::encrypt_sk_scratch_space(&module, rank, ct_grlwe_s0s1.size())
|
|
|
|
GLWESwitchingKey::encrypt_sk_scratch_space(&module, rank_out_s0s1, ct_gglwe_s0s1.size())
|
|
|
|
| GLWECiphertextFourier::decrypt_scratch_space(&module, ct_grlwe_s0s1.size())
|
|
|
|
| GLWECiphertextFourier::decrypt_scratch_space(&module, ct_gglwe_s0s1.size())
|
|
|
|
| GLWESwitchingKey::keyswitch_inplace_scratch_space(&module, ct_grlwe_s0s1.size(), ct_grlwe_s1s2.size()),
|
|
|
|
| GLWESwitchingKey::keyswitch_inplace_scratch_space(
|
|
|
|
|
|
|
|
&module,
|
|
|
|
|
|
|
|
ct_gglwe_s0s1.size(),
|
|
|
|
|
|
|
|
ct_gglwe_s0s1.rank(),
|
|
|
|
|
|
|
|
ct_gglwe_s1s2.size(),
|
|
|
|
|
|
|
|
),
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
let mut sk0: SecretKey<Vec<u8>> = SecretKey::new(&module, rank);
|
|
|
|
let mut sk0: SecretKey<Vec<u8>> = SecretKey::new(&module, rank_in_s0s1);
|
|
|
|
sk0.fill_ternary_prob(0.5, &mut source_xs);
|
|
|
|
sk0.fill_ternary_prob(0.5, &mut source_xs);
|
|
|
|
|
|
|
|
|
|
|
|
let mut sk0_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank);
|
|
|
|
let mut sk0_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank_in_s0s1);
|
|
|
|
sk0_dft.dft(&module, &sk0);
|
|
|
|
sk0_dft.dft(&module, &sk0);
|
|
|
|
|
|
|
|
|
|
|
|
let mut sk1: SecretKey<Vec<u8>> = SecretKey::new(&module, rank);
|
|
|
|
let mut sk1: SecretKey<Vec<u8>> = SecretKey::new(&module, rank_out_s0s1);
|
|
|
|
sk1.fill_ternary_prob(0.5, &mut source_xs);
|
|
|
|
sk1.fill_ternary_prob(0.5, &mut source_xs);
|
|
|
|
|
|
|
|
|
|
|
|
let mut sk1_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank);
|
|
|
|
let mut sk1_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank_out_s0s1);
|
|
|
|
sk1_dft.dft(&module, &sk1);
|
|
|
|
sk1_dft.dft(&module, &sk1);
|
|
|
|
|
|
|
|
|
|
|
|
let mut sk2: SecretKey<Vec<u8>> = SecretKey::new(&module, rank);
|
|
|
|
let mut sk2: SecretKey<Vec<u8>> = SecretKey::new(&module, rank_out_s0s1);
|
|
|
|
sk2.fill_ternary_prob(0.5, &mut source_xs);
|
|
|
|
sk2.fill_ternary_prob(0.5, &mut source_xs);
|
|
|
|
|
|
|
|
|
|
|
|
let mut sk2_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank);
|
|
|
|
let mut sk2_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank_out_s0s1);
|
|
|
|
sk2_dft.dft(&module, &sk2);
|
|
|
|
sk2_dft.dft(&module, &sk2);
|
|
|
|
|
|
|
|
|
|
|
|
// GRLWE_{s1}(s0) = s0 -> s1
|
|
|
|
// gglwe_{s1}(s0) = s0 -> s1
|
|
|
|
ct_grlwe_s0s1.encrypt_sk(
|
|
|
|
ct_gglwe_s0s1.encrypt_sk(
|
|
|
|
&module,
|
|
|
|
&module,
|
|
|
|
&sk0.data,
|
|
|
|
&sk0,
|
|
|
|
&sk1_dft,
|
|
|
|
&sk1_dft,
|
|
|
|
&mut source_xa,
|
|
|
|
&mut source_xa,
|
|
|
|
&mut source_xe,
|
|
|
|
&mut source_xe,
|
|
|
|
@@ -234,10 +288,10 @@ fn keyswitch_inplace() {
|
|
|
|
scratch.borrow(),
|
|
|
|
scratch.borrow(),
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
// GRLWE_{s2}(s1) -> s1 -> s2
|
|
|
|
// gglwe_{s2}(s1) -> s1 -> s2
|
|
|
|
ct_grlwe_s1s2.encrypt_sk(
|
|
|
|
ct_gglwe_s1s2.encrypt_sk(
|
|
|
|
&module,
|
|
|
|
&module,
|
|
|
|
&sk1.data,
|
|
|
|
&sk1,
|
|
|
|
&sk2_dft,
|
|
|
|
&sk2_dft,
|
|
|
|
&mut source_xa,
|
|
|
|
&mut source_xa,
|
|
|
|
&mut source_xe,
|
|
|
|
&mut source_xe,
|
|
|
|
@@ -245,96 +299,93 @@ fn keyswitch_inplace() {
|
|
|
|
scratch.borrow(),
|
|
|
|
scratch.borrow(),
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
// GRLWE_{s1}(s0) (x) GRLWE_{s2}(s1) = GRLWE_{s2}(s0)
|
|
|
|
// gglwe_{s1}(s0) (x) gglwe_{s2}(s1) = gglwe_{s2}(s0)
|
|
|
|
ct_grlwe_s0s1.keyswitch_inplace(&module, &ct_grlwe_s1s2, scratch.borrow());
|
|
|
|
ct_gglwe_s0s1.keyswitch_inplace(&module, &ct_gglwe_s1s2, scratch.borrow());
|
|
|
|
|
|
|
|
|
|
|
|
let ct_grlwe_s0s2: GLWESwitchingKey<Vec<u8>, FFT64> = ct_grlwe_s0s1;
|
|
|
|
let ct_gglwe_s0s2: GLWESwitchingKey<Vec<u8>, FFT64> = ct_gglwe_s0s1;
|
|
|
|
|
|
|
|
|
|
|
|
let mut ct_rlwe_dft_s0s2: GLWECiphertextFourier<Vec<u8>, FFT64> =
|
|
|
|
let mut ct_glwe_dft: GLWECiphertextFourier<Vec<u8>, FFT64> = GLWECiphertextFourier::new(&module, basek, k_ksk, rank_out_s0s1);
|
|
|
|
GLWECiphertextFourier::new(&module, basek, log_k_grlwe, rank);
|
|
|
|
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, basek, k_ksk);
|
|
|
|
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, basek, log_k_grlwe);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(0..ct_grlwe_s0s2.rows()).for_each(|row_i| {
|
|
|
|
(0..ct_gglwe_s0s2.rank_in()).for_each(|col_i| {
|
|
|
|
ct_grlwe_s0s2.get_row(&module, row_i, 0, &mut ct_rlwe_dft_s0s2);
|
|
|
|
(0..ct_gglwe_s0s2.rows()).for_each(|row_i| {
|
|
|
|
ct_rlwe_dft_s0s2.decrypt(&module, &mut pt, &sk2_dft, scratch.borrow());
|
|
|
|
ct_gglwe_s0s2.get_row(&module, row_i, col_i, &mut ct_glwe_dft);
|
|
|
|
module.vec_znx_sub_scalar_inplace(&mut pt, 0, row_i, &sk0, 0);
|
|
|
|
ct_glwe_dft.decrypt(&module, &mut pt, &sk2_dft, scratch.borrow());
|
|
|
|
|
|
|
|
module.vec_znx_sub_scalar_inplace(&mut pt, 0, row_i, &sk0, col_i);
|
|
|
|
|
|
|
|
|
|
|
|
let noise_have: f64 = pt.data.std(0, basek).log2();
|
|
|
|
let noise_have: f64 = pt.data.std(0, basek).log2();
|
|
|
|
let noise_want: f64 = noise_grlwe_rlwe_product(
|
|
|
|
let noise_want: f64 = noise_gglwe_product(
|
|
|
|
module.n() as f64,
|
|
|
|
module.n() as f64,
|
|
|
|
basek,
|
|
|
|
basek,
|
|
|
|
0.5,
|
|
|
|
0.5,
|
|
|
|
0.5,
|
|
|
|
0.5,
|
|
|
|
0f64,
|
|
|
|
0f64,
|
|
|
|
sigma * sigma,
|
|
|
|
sigma * sigma,
|
|
|
|
0f64,
|
|
|
|
0f64,
|
|
|
|
log_k_grlwe,
|
|
|
|
rank_out_s0s1 as f64,
|
|
|
|
log_k_grlwe,
|
|
|
|
k_ksk,
|
|
|
|
);
|
|
|
|
k_ksk,
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
assert!(
|
|
|
|
assert!(
|
|
|
|
(noise_have - noise_want).abs() <= 0.1,
|
|
|
|
(noise_have - noise_want).abs() <= 0.1,
|
|
|
|
"{} {}",
|
|
|
|
"{} {}",
|
|
|
|
noise_have,
|
|
|
|
noise_have,
|
|
|
|
noise_want
|
|
|
|
noise_want
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_external_product(log_n: usize, basek: usize, k: usize, sigma: f64, rank_in: usize, rank_out: usize) {
|
|
|
|
fn external_product() {
|
|
|
|
let module: Module<FFT64> = Module::<FFT64>::new(1 << log_n);
|
|
|
|
let module: Module<FFT64> = Module::<FFT64>::new(2048);
|
|
|
|
|
|
|
|
let basek: usize = 12;
|
|
|
|
|
|
|
|
let log_k_grlwe: usize = 60;
|
|
|
|
|
|
|
|
let rows: usize = (log_k_grlwe + basek - 1) / basek;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let rank: usize = 1;
|
|
|
|
let rows: usize = (k + basek - 1) / basek;
|
|
|
|
let rank_out: usize = 1;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let sigma: f64 = 3.2;
|
|
|
|
let mut ct_gglwe_in: GLWESwitchingKey<Vec<u8>, FFT64> = GLWESwitchingKey::new(&module, basek, k, rows, rank_in, rank_out);
|
|
|
|
|
|
|
|
let mut ct_gglwe_out: GLWESwitchingKey<Vec<u8>, FFT64> = GLWESwitchingKey::new(&module, basek, k, rows, rank_in, rank_out);
|
|
|
|
let mut ct_grlwe_in: GLWESwitchingKey<Vec<u8>, FFT64> =
|
|
|
|
let mut ct_rgsw: GGSWCiphertext<Vec<u8>, FFT64> = GGSWCiphertext::new(&module, basek, k, rows, rank_out);
|
|
|
|
GLWESwitchingKey::new(&module, basek, log_k_grlwe, rows, rank, rank_out);
|
|
|
|
|
|
|
|
let mut ct_grlwe_out: GLWESwitchingKey<Vec<u8>, FFT64> =
|
|
|
|
|
|
|
|
GLWESwitchingKey::new(&module, basek, log_k_grlwe, rows, rank, rank_out);
|
|
|
|
|
|
|
|
let mut ct_rgsw: GGSWCiphertext<Vec<u8>, FFT64> = GGSWCiphertext::new(&module, basek, log_k_grlwe, rows, rank);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let mut pt_rgsw: ScalarZnx<Vec<u8>> = module.new_scalar_znx(1);
|
|
|
|
let mut pt_rgsw: ScalarZnx<Vec<u8>> = module.new_scalar_znx(1);
|
|
|
|
let mut pt_grlwe: ScalarZnx<Vec<u8>> = module.new_scalar_znx(1);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let mut source_xs: Source = Source::new([0u8; 32]);
|
|
|
|
let mut source_xs: Source = Source::new([0u8; 32]);
|
|
|
|
let mut source_xe: 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 source_xa: Source = Source::new([0u8; 32]);
|
|
|
|
|
|
|
|
|
|
|
|
let mut scratch: ScratchOwned = ScratchOwned::new(
|
|
|
|
let mut scratch: ScratchOwned = ScratchOwned::new(
|
|
|
|
GLWESwitchingKey::encrypt_sk_scratch_space(&module, rank, ct_grlwe_in.size())
|
|
|
|
GLWESwitchingKey::encrypt_sk_scratch_space(&module, rank_out, ct_gglwe_in.size())
|
|
|
|
| GLWECiphertextFourier::decrypt_scratch_space(&module, ct_grlwe_out.size())
|
|
|
|
| GLWECiphertextFourier::decrypt_scratch_space(&module, ct_gglwe_out.size())
|
|
|
|
| GLWESwitchingKey::external_product_scratch_space(
|
|
|
|
| GLWESwitchingKey::external_product_scratch_space(
|
|
|
|
&module,
|
|
|
|
&module,
|
|
|
|
ct_grlwe_out.size(),
|
|
|
|
ct_gglwe_out.size(),
|
|
|
|
ct_grlwe_in.size(),
|
|
|
|
ct_gglwe_in.size(),
|
|
|
|
ct_rgsw.size(),
|
|
|
|
ct_rgsw.size(),
|
|
|
|
|
|
|
|
rank_out,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
| GGSWCiphertext::encrypt_sk_scratch_space(&module, rank, ct_rgsw.size()),
|
|
|
|
| GGSWCiphertext::encrypt_sk_scratch_space(&module, rank_out, ct_rgsw.size()),
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
let k: usize = 1;
|
|
|
|
let r: usize = 1;
|
|
|
|
|
|
|
|
|
|
|
|
pt_rgsw.raw_mut()[k] = 1; // X^{k}
|
|
|
|
pt_rgsw.to_mut().raw_mut()[r] = 1; // X^{r}
|
|
|
|
|
|
|
|
|
|
|
|
pt_grlwe.fill_ternary_prob(0, 0.5, &mut source_xs);
|
|
|
|
let mut sk_in: SecretKey<Vec<u8>> = SecretKey::new(&module, rank_in);
|
|
|
|
|
|
|
|
sk_in.fill_ternary_prob(0.5, &mut source_xs);
|
|
|
|
|
|
|
|
|
|
|
|
let mut sk: SecretKey<Vec<u8>> = SecretKey::new(&module, rank);
|
|
|
|
let mut sk_in_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank_in);
|
|
|
|
sk.fill_ternary_prob(0.5, &mut source_xs);
|
|
|
|
sk_in_dft.dft(&module, &sk_in);
|
|
|
|
|
|
|
|
|
|
|
|
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank);
|
|
|
|
let mut sk_out: SecretKey<Vec<u8>> = SecretKey::new(&module, rank_out);
|
|
|
|
sk_dft.dft(&module, &sk);
|
|
|
|
sk_out.fill_ternary_prob(0.5, &mut source_xs);
|
|
|
|
|
|
|
|
|
|
|
|
// GRLWE_{s1}(s0) = s0 -> s1
|
|
|
|
let mut sk_out_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank_out);
|
|
|
|
ct_grlwe_in.encrypt_sk(
|
|
|
|
sk_out_dft.dft(&module, &sk_out);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// gglwe_{s1}(s0) = s0 -> s1
|
|
|
|
|
|
|
|
ct_gglwe_in.encrypt_sk(
|
|
|
|
&module,
|
|
|
|
&module,
|
|
|
|
&pt_grlwe,
|
|
|
|
&sk_in,
|
|
|
|
&sk_dft,
|
|
|
|
&sk_out_dft,
|
|
|
|
&mut source_xa,
|
|
|
|
&mut source_xa,
|
|
|
|
&mut source_xe,
|
|
|
|
&mut source_xe,
|
|
|
|
sigma,
|
|
|
|
sigma,
|
|
|
|
@@ -344,104 +395,104 @@ fn external_product() {
|
|
|
|
ct_rgsw.encrypt_sk(
|
|
|
|
ct_rgsw.encrypt_sk(
|
|
|
|
&module,
|
|
|
|
&module,
|
|
|
|
&pt_rgsw,
|
|
|
|
&pt_rgsw,
|
|
|
|
&sk_dft,
|
|
|
|
&sk_out_dft,
|
|
|
|
&mut source_xa,
|
|
|
|
&mut source_xa,
|
|
|
|
&mut source_xe,
|
|
|
|
&mut source_xe,
|
|
|
|
sigma,
|
|
|
|
sigma,
|
|
|
|
scratch.borrow(),
|
|
|
|
scratch.borrow(),
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
// GRLWE_(m) (x) RGSW_(X^k) = GRLWE_(m * X^k)
|
|
|
|
// gglwe_(m) (x) RGSW_(X^k) = gglwe_(m * X^k)
|
|
|
|
ct_grlwe_out.external_product(&module, &ct_grlwe_in, &ct_rgsw, scratch.borrow());
|
|
|
|
ct_gglwe_out.external_product(&module, &ct_gglwe_in, &ct_rgsw, scratch.borrow());
|
|
|
|
|
|
|
|
|
|
|
|
let mut ct_rlwe_dft_s0s2: GLWECiphertextFourier<Vec<u8>, FFT64> =
|
|
|
|
let mut ct_glwe_dft: GLWECiphertextFourier<Vec<u8>, FFT64> = GLWECiphertextFourier::new(&module, basek, k, rank_out);
|
|
|
|
GLWECiphertextFourier::new(&module, basek, log_k_grlwe, rank);
|
|
|
|
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, basek, k);
|
|
|
|
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, basek, log_k_grlwe);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
module.vec_znx_rotate_inplace(k as i64, &mut pt_grlwe, 0);
|
|
|
|
(0..rank_in).for_each(|i| {
|
|
|
|
|
|
|
|
module.vec_znx_rotate_inplace(r as i64, &mut sk_in.data, i); // * X^{r}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
(0..ct_grlwe_out.rows()).for_each(|row_i| {
|
|
|
|
(0..rank_in).for_each(|col_i| {
|
|
|
|
ct_grlwe_out.get_row(&module, row_i, 0, &mut ct_rlwe_dft_s0s2);
|
|
|
|
(0..ct_gglwe_out.rows()).for_each(|row_i| {
|
|
|
|
ct_rlwe_dft_s0s2.decrypt(&module, &mut pt, &sk_dft, scratch.borrow());
|
|
|
|
ct_gglwe_out.get_row(&module, row_i, col_i, &mut ct_glwe_dft);
|
|
|
|
module.vec_znx_sub_scalar_inplace(&mut pt, 0, row_i, &pt_grlwe, 0);
|
|
|
|
ct_glwe_dft.decrypt(&module, &mut pt, &sk_out_dft, scratch.borrow());
|
|
|
|
|
|
|
|
module.vec_znx_sub_scalar_inplace(&mut pt, 0, row_i, &sk_in, col_i);
|
|
|
|
|
|
|
|
|
|
|
|
let noise_have: f64 = pt.data.std(0, basek).log2();
|
|
|
|
let noise_have: f64 = pt.data.std(0, basek).log2();
|
|
|
|
|
|
|
|
|
|
|
|
let var_gct_err_lhs: f64 = sigma * sigma;
|
|
|
|
let var_gct_err_lhs: f64 = sigma * sigma;
|
|
|
|
let var_gct_err_rhs: f64 = 0f64;
|
|
|
|
let var_gct_err_rhs: f64 = 0f64;
|
|
|
|
|
|
|
|
|
|
|
|
let var_msg: f64 = 1f64 / module.n() as f64; // X^{k}
|
|
|
|
let var_msg: f64 = 1f64 / module.n() as f64; // X^{k}
|
|
|
|
let var_a0_err: f64 = sigma * sigma;
|
|
|
|
let var_a0_err: f64 = sigma * sigma;
|
|
|
|
let var_a1_err: f64 = 1f64 / 12f64;
|
|
|
|
let var_a1_err: f64 = 1f64 / 12f64;
|
|
|
|
|
|
|
|
|
|
|
|
let noise_want: f64 = noise_rgsw_product(
|
|
|
|
let noise_want: f64 = noise_ggsw_product(
|
|
|
|
module.n() as f64,
|
|
|
|
module.n() as f64,
|
|
|
|
basek,
|
|
|
|
basek,
|
|
|
|
0.5,
|
|
|
|
0.5,
|
|
|
|
var_msg,
|
|
|
|
var_msg,
|
|
|
|
var_a0_err,
|
|
|
|
var_a0_err,
|
|
|
|
var_a1_err,
|
|
|
|
var_a1_err,
|
|
|
|
var_gct_err_lhs,
|
|
|
|
var_gct_err_lhs,
|
|
|
|
var_gct_err_rhs,
|
|
|
|
var_gct_err_rhs,
|
|
|
|
log_k_grlwe,
|
|
|
|
rank_out as f64,
|
|
|
|
log_k_grlwe,
|
|
|
|
k,
|
|
|
|
);
|
|
|
|
k,
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
assert!(
|
|
|
|
assert!(
|
|
|
|
(noise_have - noise_want).abs() <= 0.1,
|
|
|
|
(noise_have - noise_want).abs() <= 0.1,
|
|
|
|
"{} {}",
|
|
|
|
"{} {}",
|
|
|
|
noise_have,
|
|
|
|
noise_have,
|
|
|
|
noise_want
|
|
|
|
noise_want
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_external_product_inplace(log_n: usize, basek: usize, k: usize, sigma: f64, rank_in: usize, rank_out: usize) {
|
|
|
|
fn external_product_inplace() {
|
|
|
|
let module: Module<FFT64> = Module::<FFT64>::new(1 << log_n);
|
|
|
|
let module: Module<FFT64> = Module::<FFT64>::new(2048);
|
|
|
|
|
|
|
|
let basek: usize = 12;
|
|
|
|
|
|
|
|
let log_k_grlwe: usize = 60;
|
|
|
|
|
|
|
|
let rows: usize = (log_k_grlwe + basek - 1) / basek;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let rank: usize = 1;
|
|
|
|
let rows: usize = (k + basek - 1) / basek;
|
|
|
|
let rank_out: usize = 1;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let sigma: f64 = 3.2;
|
|
|
|
let mut ct_gglwe: GLWESwitchingKey<Vec<u8>, FFT64> = GLWESwitchingKey::new(&module, basek, k, rows, rank_in, rank_out);
|
|
|
|
|
|
|
|
let mut ct_rgsw: GGSWCiphertext<Vec<u8>, FFT64> = GGSWCiphertext::new(&module, basek, k, rows, rank_out);
|
|
|
|
let mut ct_grlwe: GLWESwitchingKey<Vec<u8>, FFT64> = GLWESwitchingKey::new(&module, basek, log_k_grlwe, rows, rank, rank_out);
|
|
|
|
|
|
|
|
let mut ct_rgsw: GGSWCiphertext<Vec<u8>, FFT64> = GGSWCiphertext::new(&module, basek, log_k_grlwe, rows, rank);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let mut pt_rgsw: ScalarZnx<Vec<u8>> = module.new_scalar_znx(1);
|
|
|
|
let mut pt_rgsw: ScalarZnx<Vec<u8>> = module.new_scalar_znx(1);
|
|
|
|
let mut pt_grlwe: ScalarZnx<Vec<u8>> = module.new_scalar_znx(1);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let mut source_xs: Source = Source::new([0u8; 32]);
|
|
|
|
let mut source_xs: Source = Source::new([0u8; 32]);
|
|
|
|
let mut source_xe: 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 source_xa: Source = Source::new([0u8; 32]);
|
|
|
|
|
|
|
|
|
|
|
|
let mut scratch: ScratchOwned = ScratchOwned::new(
|
|
|
|
let mut scratch: ScratchOwned = ScratchOwned::new(
|
|
|
|
GLWESwitchingKey::encrypt_sk_scratch_space(&module, rank, ct_grlwe.size())
|
|
|
|
GLWESwitchingKey::encrypt_sk_scratch_space(&module, rank_out, ct_gglwe.size())
|
|
|
|
| GLWECiphertextFourier::decrypt_scratch_space(&module, ct_grlwe.size())
|
|
|
|
| GLWECiphertextFourier::decrypt_scratch_space(&module, ct_gglwe.size())
|
|
|
|
| GLWESwitchingKey::external_product_inplace_scratch_space(&module, ct_grlwe.size(), ct_rgsw.size())
|
|
|
|
| GLWESwitchingKey::external_product_inplace_scratch_space(&module, ct_gglwe.size(), ct_rgsw.size(), rank_out)
|
|
|
|
| GGSWCiphertext::encrypt_sk_scratch_space(&module, rank, ct_rgsw.size()),
|
|
|
|
| GGSWCiphertext::encrypt_sk_scratch_space(&module, rank_out, ct_rgsw.size()),
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
let k: usize = 1;
|
|
|
|
let r: usize = 1;
|
|
|
|
|
|
|
|
|
|
|
|
pt_rgsw.raw_mut()[k] = 1; // X^{k}
|
|
|
|
pt_rgsw.to_mut().raw_mut()[r] = 1; // X^{r}
|
|
|
|
|
|
|
|
|
|
|
|
pt_grlwe.fill_ternary_prob(0, 0.5, &mut source_xs);
|
|
|
|
let mut sk_in: SecretKey<Vec<u8>> = SecretKey::new(&module, rank_in);
|
|
|
|
|
|
|
|
sk_in.fill_ternary_prob(0.5, &mut source_xs);
|
|
|
|
|
|
|
|
|
|
|
|
let mut sk: SecretKey<Vec<u8>> = SecretKey::new(&module, rank);
|
|
|
|
let mut sk_in_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank_in);
|
|
|
|
sk.fill_ternary_prob(0.5, &mut source_xs);
|
|
|
|
sk_in_dft.dft(&module, &sk_in);
|
|
|
|
|
|
|
|
|
|
|
|
let mut sk_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank);
|
|
|
|
let mut sk_out: SecretKey<Vec<u8>> = SecretKey::new(&module, rank_out);
|
|
|
|
sk_dft.dft(&module, &sk);
|
|
|
|
sk_out.fill_ternary_prob(0.5, &mut source_xs);
|
|
|
|
|
|
|
|
|
|
|
|
// GRLWE_{s1}(s0) = s0 -> s1
|
|
|
|
let mut sk_out_dft: SecretKeyFourier<Vec<u8>, FFT64> = SecretKeyFourier::new(&module, rank_out);
|
|
|
|
ct_grlwe.encrypt_sk(
|
|
|
|
sk_out_dft.dft(&module, &sk_out);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// gglwe_{s1}(s0) = s0 -> s1
|
|
|
|
|
|
|
|
ct_gglwe.encrypt_sk(
|
|
|
|
&module,
|
|
|
|
&module,
|
|
|
|
&pt_grlwe,
|
|
|
|
&sk_in,
|
|
|
|
&sk_dft,
|
|
|
|
&sk_out_dft,
|
|
|
|
&mut source_xa,
|
|
|
|
&mut source_xa,
|
|
|
|
&mut source_xe,
|
|
|
|
&mut source_xe,
|
|
|
|
sigma,
|
|
|
|
sigma,
|
|
|
|
@@ -451,57 +502,62 @@ fn external_product_inplace() {
|
|
|
|
ct_rgsw.encrypt_sk(
|
|
|
|
ct_rgsw.encrypt_sk(
|
|
|
|
&module,
|
|
|
|
&module,
|
|
|
|
&pt_rgsw,
|
|
|
|
&pt_rgsw,
|
|
|
|
&sk_dft,
|
|
|
|
&sk_out_dft,
|
|
|
|
&mut source_xa,
|
|
|
|
&mut source_xa,
|
|
|
|
&mut source_xe,
|
|
|
|
&mut source_xe,
|
|
|
|
sigma,
|
|
|
|
sigma,
|
|
|
|
scratch.borrow(),
|
|
|
|
scratch.borrow(),
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
// GRLWE_(m) (x) RGSW_(X^k) = GRLWE_(m * X^k)
|
|
|
|
// gglwe_(m) (x) RGSW_(X^k) = gglwe_(m * X^k)
|
|
|
|
ct_grlwe.external_product_inplace(&module, &ct_rgsw, scratch.borrow());
|
|
|
|
ct_gglwe.external_product_inplace(&module, &ct_rgsw, scratch.borrow());
|
|
|
|
|
|
|
|
|
|
|
|
let mut ct_rlwe_dft_s0s2: GLWECiphertextFourier<Vec<u8>, FFT64> =
|
|
|
|
let mut ct_glwe_dft: GLWECiphertextFourier<Vec<u8>, FFT64> = GLWECiphertextFourier::new(&module, basek, k, rank_out);
|
|
|
|
GLWECiphertextFourier::new(&module, basek, log_k_grlwe, rank);
|
|
|
|
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, basek, k);
|
|
|
|
let mut pt: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::new(&module, basek, log_k_grlwe);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
module.vec_znx_rotate_inplace(k as i64, &mut pt_grlwe, 0);
|
|
|
|
(0..rank_in).for_each(|i| {
|
|
|
|
|
|
|
|
module.vec_znx_rotate_inplace(r as i64, &mut sk_in.data, i); // * X^{r}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
(0..ct_grlwe.rows()).for_each(|row_i| {
|
|
|
|
(0..rank_in).for_each(|col_i| {
|
|
|
|
ct_grlwe.get_row(&module, row_i, 0, &mut ct_rlwe_dft_s0s2);
|
|
|
|
(0..ct_gglwe.rows()).for_each(|row_i| {
|
|
|
|
ct_rlwe_dft_s0s2.decrypt(&module, &mut pt, &sk_dft, scratch.borrow());
|
|
|
|
ct_gglwe.get_row(&module, row_i, col_i, &mut ct_glwe_dft);
|
|
|
|
module.vec_znx_sub_scalar_inplace(&mut pt, 0, row_i, &pt_grlwe, 0);
|
|
|
|
ct_glwe_dft.decrypt(&module, &mut pt, &sk_out_dft, scratch.borrow());
|
|
|
|
|
|
|
|
module.vec_znx_sub_scalar_inplace(&mut pt, 0, row_i, &sk_in, col_i);
|
|
|
|
|
|
|
|
|
|
|
|
let noise_have: f64 = pt.data.std(0, basek).log2();
|
|
|
|
let noise_have: f64 = pt.data.std(0, basek).log2();
|
|
|
|
|
|
|
|
|
|
|
|
let var_gct_err_lhs: f64 = sigma * sigma;
|
|
|
|
let var_gct_err_lhs: f64 = sigma * sigma;
|
|
|
|
let var_gct_err_rhs: f64 = 0f64;
|
|
|
|
let var_gct_err_rhs: f64 = 0f64;
|
|
|
|
|
|
|
|
|
|
|
|
let var_msg: f64 = 1f64 / module.n() as f64; // X^{k}
|
|
|
|
let var_msg: f64 = 1f64 / module.n() as f64; // X^{k}
|
|
|
|
let var_a0_err: f64 = sigma * sigma;
|
|
|
|
let var_a0_err: f64 = sigma * sigma;
|
|
|
|
let var_a1_err: f64 = 1f64 / 12f64;
|
|
|
|
let var_a1_err: f64 = 1f64 / 12f64;
|
|
|
|
|
|
|
|
|
|
|
|
let noise_want: f64 = noise_rgsw_product(
|
|
|
|
let noise_want: f64 = noise_ggsw_product(
|
|
|
|
module.n() as f64,
|
|
|
|
module.n() as f64,
|
|
|
|
basek,
|
|
|
|
basek,
|
|
|
|
0.5,
|
|
|
|
0.5,
|
|
|
|
var_msg,
|
|
|
|
var_msg,
|
|
|
|
var_a0_err,
|
|
|
|
var_a0_err,
|
|
|
|
var_a1_err,
|
|
|
|
var_a1_err,
|
|
|
|
var_gct_err_lhs,
|
|
|
|
var_gct_err_lhs,
|
|
|
|
var_gct_err_rhs,
|
|
|
|
var_gct_err_rhs,
|
|
|
|
log_k_grlwe,
|
|
|
|
rank_out as f64,
|
|
|
|
log_k_grlwe,
|
|
|
|
k,
|
|
|
|
);
|
|
|
|
k,
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
assert!(
|
|
|
|
assert!(
|
|
|
|
(noise_have - noise_want).abs() <= 0.1,
|
|
|
|
(noise_have - noise_want).abs() <= 0.1,
|
|
|
|
"{} {}",
|
|
|
|
"{} {}",
|
|
|
|
noise_have,
|
|
|
|
noise_have,
|
|
|
|
noise_want
|
|
|
|
noise_want
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub(crate) fn noise_gglwe_product(
|
|
|
|
pub(crate) fn noise_gglwe_product(
|
|
|
|
n: f64,
|
|
|
|
n: f64,
|
|
|
|
basek: usize,
|
|
|
|
basek: usize,
|
|
|
|
|