mirror of
https://github.com/arnaucube/poulpy.git
synced 2026-02-10 13:16:44 +01:00
refactored secret key
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user