This commit is contained in:
Pro7ech
2025-10-14 18:46:25 +02:00
parent 0533cdff8a
commit 72dca47cbe
153 changed files with 3099 additions and 1956 deletions

View File

@@ -20,7 +20,7 @@ use poulpy_core::{
};
use poulpy_core::glwe_packing;
use poulpy_core::layouts::{GGSW, GLWECiphertext, LWECiphertext, prepared::AutomorphismKeyPrepared};
use poulpy_core::layouts::{GGSW, GLWE, LWE, prepared::AutomorphismKeyPrepared};
use crate::tfhe::{
blind_rotation::{
@@ -75,7 +75,7 @@ where
&self,
module: &Module<B>,
res: &mut GGSW<DM>,
lwe: &LWECiphertext<DR>,
lwe: &LWE<DR>,
log_domain: usize,
extension_factor: usize,
scratch: &mut Scratch<B>,
@@ -98,7 +98,7 @@ where
module: &Module<B>,
log_gap_out: usize,
res: &mut GGSW<DM>,
lwe: &LWECiphertext<DR>,
lwe: &LWE<DR>,
log_domain: usize,
extension_factor: usize,
scratch: &mut Scratch<B>,
@@ -123,7 +123,7 @@ pub fn circuit_bootstrap_core<DRes, DLwe, DBrk, BRA: BlindRotationAlgo, B>(
module: &Module<B>,
log_gap_out: usize,
res: &mut GGSW<DRes>,
lwe: &LWECiphertext<DLwe>,
lwe: &LWE<DLwe>,
log_domain: usize,
extension_factor: usize,
key: &CircuitBootstrappingKeyPrepared<DBrk, BRA, B>,
@@ -233,7 +233,7 @@ pub fn circuit_bootstrap_core<DRes, DLwe, DBrk, BRA: BlindRotationAlgo, B>(
let log_gap_in: usize = (usize::BITS - (gap * alpha - 1).leading_zeros()) as _;
(0..dnum).for_each(|i| {
let mut tmp_glwe: GLWECiphertext<&mut [u8]> = tmp_gglwe.at_mut(i, 0);
let mut tmp_glwe: GLWE<&mut [u8]> = tmp_gglwe.at_mut(i, 0);
if to_exponent {
// Isolates i-th LUT and moves coefficients according to requested gap.
@@ -263,8 +263,8 @@ pub fn circuit_bootstrap_core<DRes, DLwe, DBrk, BRA: BlindRotationAlgo, B>(
#[allow(clippy::too_many_arguments)]
fn post_process<DataRes, DataA, B: Backend>(
module: &Module<B>,
res: &mut GLWECiphertext<DataRes>,
a: &GLWECiphertext<DataA>,
res: &mut GLWE<DataRes>,
a: &GLWE<DataA>,
log_gap_in: usize,
log_gap_out: usize,
log_domain: usize,
@@ -303,7 +303,7 @@ fn post_process<DataRes, DataA, B: Backend>(
{
let log_n: usize = module.log_n();
let mut cts: HashMap<usize, &mut GLWECiphertext<Vec<u8>>> = HashMap::new();
let mut cts: HashMap<usize, &mut GLWE<Vec<u8>>> = HashMap::new();
// First partial trace, vanishes all coefficients which are not multiples of gap_in
// [1, 1, 1, 1, 0, 0, 0, ..., 0, 0, -1, -1, -1, -1] -> [1, 0, 0, 0, 0, 0, 0, ..., 0, 0, 0, 0, 0, 0]
@@ -322,7 +322,7 @@ fn post_process<DataRes, DataA, B: Backend>(
let steps: usize = 1 << log_domain;
// TODO: from Scratch
let mut cts_vec: Vec<GLWECiphertext<Vec<u8>>> = Vec::new();
let mut cts_vec: Vec<GLWE<Vec<u8>>> = Vec::new();
for i in 0..steps {
if i != 0 {
@@ -336,7 +336,7 @@ fn post_process<DataRes, DataA, B: Backend>(
}
glwe_packing(module, &mut cts, log_gap_out, auto_keys, scratch);
let packed: &mut GLWECiphertext<Vec<u8>> = cts.remove(&0).unwrap();
let packed: &mut GLWE<Vec<u8>> = cts.remove(&0).unwrap();
res.trace(
module,
log_n - log_gap_out,

View File

@@ -1,6 +1,6 @@
use poulpy_core::layouts::{
AutomorphismKey, AutomorphismKeyLayout, GGLWEInfos, GGSWInfos, GLWECiphertext, GLWEInfos, GLWESecret, LWEInfos, LWESecret,
TensorKey, TensorKeyLayout,
AutomorphismKey, AutomorphismKeyLayout, GGLWEInfos, GGSWInfos, GLWE, GLWEInfos, GLWESecret, LWEInfos, LWESecret, TensorKey,
TensorKeyLayout,
prepared::{AutomorphismKeyPrepared, GLWESecretPrepared, PrepareAlloc, TensorKeyPrepared},
};
use std::collections::HashMap;
@@ -122,9 +122,9 @@ where
let trk_infos: TensorKeyLayout = cbt_infos.tsk_infos();
let mut auto_keys: HashMap<i64, AutomorphismKey<Vec<u8>>> = HashMap::new();
let gal_els: Vec<i64> = GLWECiphertext::trace_galois_elements(module);
let gal_els: Vec<i64> = GLWE::trace_galois_elements(module);
gal_els.iter().for_each(|gal_el| {
let mut key: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc(&atk_infos);
let mut key: AutomorphismKey<Vec<u8>> = AutomorphismKey::alloc_from_infos(&atk_infos);
key.encrypt_sk(module, *gal_el, sk_glwe, source_xa, source_xe, scratch);
auto_keys.insert(*gal_el, key);
});
@@ -141,7 +141,7 @@ where
scratch,
);
let mut tsk: TensorKey<Vec<u8>> = TensorKey::alloc(&trk_infos);
let mut tsk: TensorKey<Vec<u8>> = TensorKey::alloc_from_infos(&trk_infos);
tsk.encrypt_sk(module, sk_glwe, source_xa, source_xe, scratch);
Self {

View File

@@ -5,7 +5,7 @@ pub mod tests;
pub use circuit::*;
pub use key::*;
use poulpy_core::layouts::{GGSW, LWECiphertext};
use poulpy_core::layouts::{GGSW, LWE};
use poulpy_hal::layouts::{Backend, DataMut, DataRef, Module, Scratch};
@@ -14,7 +14,7 @@ pub trait CirtuitBootstrappingExecute<B: Backend> {
&self,
module: &Module<B>,
res: &mut GGSW<DM>,
lwe: &LWECiphertext<DR>,
lwe: &LWE<DR>,
log_domain: usize,
extension_factor: usize,
scratch: &mut Scratch<B>,
@@ -26,7 +26,7 @@ pub trait CirtuitBootstrappingExecute<B: Backend> {
module: &Module<B>,
log_gap_out: usize,
res: &mut GGSW<DM>,
lwe: &LWECiphertext<DR>,
lwe: &LWE<DR>,
log_domain: usize,
extension_factor: usize,
scratch: &mut Scratch<B>,

View File

@@ -36,7 +36,7 @@ use poulpy_core::layouts::{
};
use poulpy_core::layouts::{
GGSW, GLWECiphertext, GLWEPlaintext, GLWESecret, LWECiphertext, LWEPlaintext, LWESecret,
GGSW, GLWE, GLWEPlaintext, GLWESecret, LWE, LWEPlaintext, LWESecret,
prepared::{GGSWPrepared, GLWESecretPrepared},
};
@@ -179,19 +179,19 @@ where
let mut sk_lwe: LWESecret<Vec<u8>> = LWESecret::alloc(n_lwe.into());
sk_lwe.fill_binary_block(block_size, &mut source_xs);
let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc_with(n_glwe.into(), rank.into());
let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc(n_glwe.into(), rank.into());
sk_glwe.fill_ternary_prob(0.5, &mut source_xs);
let sk_glwe_prepared: GLWESecretPrepared<Vec<u8>, B> = sk_glwe.prepare_alloc(module, scratch.borrow());
let data: i64 = 1;
let mut pt_lwe: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc_with(base2k.into(), k_lwe_pt.into());
let mut pt_lwe: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc(base2k.into(), k_lwe_pt.into());
pt_lwe.encode_i64(data, (k_lwe_pt + 1).into());
println!("pt_lwe: {pt_lwe}");
let mut ct_lwe: LWECiphertext<Vec<u8>> = LWECiphertext::alloc(&lwe_infos);
let mut ct_lwe: LWE<Vec<u8>> = LWE::alloc_from_infos(&lwe_infos);
ct_lwe.encrypt_sk(module, &pt_lwe, &sk_lwe, &mut source_xa, &mut source_xe);
let now: Instant = Instant::now();
@@ -206,7 +206,7 @@ where
);
println!("CBT-KGEN: {} ms", now.elapsed().as_millis());
let mut res: GGSW<Vec<u8>> = GGSW::alloc(&ggsw_infos);
let mut res: GGSW<Vec<u8>> = GGSW::alloc_from_infos(&ggsw_infos);
let log_gap_out = 1;
@@ -236,8 +236,8 @@ where
res.print_noise(module, &sk_glwe_prepared, &pt_ggsw);
let mut ct_glwe: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&ggsw_infos);
let mut pt_glwe: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&ggsw_infos);
let mut ct_glwe: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&ggsw_infos);
let mut pt_glwe: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&ggsw_infos);
pt_glwe.data.at_mut(0, 0)[0] = 1 << (base2k - 2);
ct_glwe.encrypt_sk(
@@ -253,7 +253,7 @@ where
ct_glwe.external_product_inplace(module, &res_prepared, scratch.borrow());
let mut pt_res: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&ggsw_infos);
let mut pt_res: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&ggsw_infos);
ct_glwe.decrypt(module, &mut pt_res, &sk_glwe_prepared, scratch.borrow());
// Parameters are set such that the first limb should be noiseless.
@@ -401,19 +401,19 @@ where
let mut sk_lwe: LWESecret<Vec<u8>> = LWESecret::alloc(n_lwe.into());
sk_lwe.fill_binary_block(block_size, &mut source_xs);
let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc_with(n_glwe.into(), rank.into());
let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc(n_glwe.into(), rank.into());
sk_glwe.fill_ternary_prob(0.5, &mut source_xs);
let sk_glwe_prepared: GLWESecretPrepared<Vec<u8>, B> = sk_glwe.prepare_alloc(module, scratch.borrow());
let data: i64 = 1;
let mut pt_lwe: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc_with(base2k.into(), k_lwe_pt.into());
let mut pt_lwe: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc(base2k.into(), k_lwe_pt.into());
pt_lwe.encode_i64(data, (k_lwe_pt + 1).into());
println!("pt_lwe: {pt_lwe}");
let mut ct_lwe: LWECiphertext<Vec<u8>> = LWECiphertext::alloc(&lwe_infos);
let mut ct_lwe: LWE<Vec<u8>> = LWE::alloc_from_infos(&lwe_infos);
ct_lwe.encrypt_sk(module, &pt_lwe, &sk_lwe, &mut source_xa, &mut source_xe);
let now: Instant = Instant::now();
@@ -428,7 +428,7 @@ where
);
println!("CBT-KGEN: {} ms", now.elapsed().as_millis());
let mut res: GGSW<Vec<u8>> = GGSW::alloc(&ggsw_infos);
let mut res: GGSW<Vec<u8>> = GGSW::alloc_from_infos(&ggsw_infos);
let cbt_prepared: CircuitBootstrappingKeyPrepared<Vec<u8>, BRA, B> = cbt_key.prepare_alloc(module, scratch.borrow());
@@ -449,8 +449,8 @@ where
res.print_noise(module, &sk_glwe_prepared, &pt_ggsw);
let mut ct_glwe: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&ggsw_infos);
let mut pt_glwe: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&ggsw_infos);
let mut ct_glwe: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&ggsw_infos);
let mut pt_glwe: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&ggsw_infos);
pt_glwe.data.at_mut(0, 0)[0] = 1 << (base2k - k_lwe_pt - 1);
ct_glwe.encrypt_sk(
@@ -466,7 +466,7 @@ where
ct_glwe.external_product_inplace(module, &res_prepared, scratch.borrow());
let mut pt_res: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&ggsw_infos);
let mut pt_res: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&ggsw_infos);
ct_glwe.decrypt(module, &mut pt_res, &sk_glwe_prepared, scratch.borrow());
// Parameters are set such that the first limb should be noiseless.