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

@@ -13,7 +13,7 @@ use poulpy_hal::{
use poulpy_core::{
Distribution, GLWEOperations, TakeGLWECt,
layouts::{GGSWInfos, GLWECiphertext, GLWECiphertextToMut, GLWEInfos, LWECiphertext, LWECiphertextToRef, LWEInfos},
layouts::{GGSWInfos, GLWE, GLWEInfos, GLWEToMut, LWE, LWECiphertextToRef, LWEInfos},
};
use crate::tfhe::blind_rotation::{
@@ -43,14 +43,14 @@ where
if block_size > 1 {
let cols: usize = (brk_infos.rank() + 1).into();
let dnum: usize = brk_infos.dnum().into();
let acc_dft: usize = module.vec_znx_dft_alloc_bytes(cols, dnum) * extension_factor;
let acc_big: usize = module.vec_znx_big_alloc_bytes(1, brk_size);
let vmp_res: usize = module.vec_znx_dft_alloc_bytes(cols, brk_size) * extension_factor;
let vmp_xai: usize = module.vec_znx_dft_alloc_bytes(1, brk_size);
let acc_dft: usize = module.vec_znx_dft_bytes_of(cols, dnum) * extension_factor;
let acc_big: usize = module.vec_znx_big_bytes_of(1, brk_size);
let vmp_res: usize = module.vec_znx_dft_bytes_of(cols, brk_size) * extension_factor;
let vmp_xai: usize = module.vec_znx_dft_bytes_of(1, brk_size);
let acc_dft_add: usize = vmp_res;
let vmp: usize = module.vmp_apply_dft_to_dft_tmp_bytes(brk_size, dnum, dnum, 2, 2, brk_size); // GGSW product: (1 x 2) x (2 x 2)
let acc: usize = if extension_factor > 1 {
VecZnx::alloc_bytes(module.n(), cols, glwe_infos.size()) * extension_factor
VecZnx::bytes_of(module.n(), cols, glwe_infos.size()) * extension_factor
} else {
0
};
@@ -61,8 +61,7 @@ where
+ vmp_xai
+ (vmp | (acc_big + (module.vec_znx_big_normalize_tmp_bytes() | module.vec_znx_idft_apply_tmp_bytes())))
} else {
GLWECiphertext::alloc_bytes(glwe_infos)
+ GLWECiphertext::external_product_inplace_scratch_space(module, glwe_infos, brk_infos)
GLWE::bytes_of(glwe_infos) + GLWE::external_product_inplace_scratch_space(module, glwe_infos, brk_infos)
}
}
@@ -99,8 +98,8 @@ where
fn execute<DR: DataMut, DI: DataRef>(
&self,
module: &Module<B>,
res: &mut GLWECiphertext<DR>,
lwe: &LWECiphertext<DI>,
res: &mut GLWE<DR>,
lwe: &LWE<DI>,
lut: &LookUpTable,
scratch: &mut Scratch<B>,
) {
@@ -121,8 +120,8 @@ where
fn execute_block_binary_extended<DataRes, DataIn, DataBrk, B: Backend>(
module: &Module<B>,
res: &mut GLWECiphertext<DataRes>,
lwe: &LWECiphertext<DataIn>,
res: &mut GLWE<DataRes>,
lwe: &LWE<DataIn>,
lut: &LookUpTable,
brk: &BlindRotationKeyPrepared<DataBrk, CGGI, B>,
scratch: &mut Scratch<B>,
@@ -179,7 +178,7 @@ fn execute_block_binary_extended<DataRes, DataIn, DataBrk, B: Backend>(
}
let mut lwe_2n: Vec<i64> = vec![0i64; (lwe.n() + 1).as_usize()]; // TODO: from scratch space
let lwe_ref: LWECiphertext<&[u8]> = lwe.to_ref();
let lwe_ref: LWE<&[u8]> = lwe.to_ref();
let two_n: usize = 2 * n_glwe;
let two_n_ext: usize = 2 * lut.domain_size();
@@ -288,8 +287,8 @@ fn execute_block_binary_extended<DataRes, DataIn, DataBrk, B: Backend>(
fn execute_block_binary<DataRes, DataIn, DataBrk, B: Backend>(
module: &Module<B>,
res: &mut GLWECiphertext<DataRes>,
lwe: &LWECiphertext<DataIn>,
res: &mut GLWE<DataRes>,
lwe: &LWE<DataIn>,
lut: &LookUpTable,
brk: &BlindRotationKeyPrepared<DataBrk, CGGI, B>,
scratch: &mut Scratch<B>,
@@ -324,8 +323,8 @@ fn execute_block_binary<DataRes, DataIn, DataBrk, B: Backend>(
{
let n_glwe: usize = brk.n_glwe().into();
let mut lwe_2n: Vec<i64> = vec![0i64; (lwe.n() + 1).into()]; // TODO: from scratch space
let mut out_mut: GLWECiphertext<&mut [u8]> = res.to_mut();
let lwe_ref: LWECiphertext<&[u8]> = lwe.to_ref();
let mut out_mut: GLWE<&mut [u8]> = res.to_mut();
let lwe_ref: LWE<&[u8]> = lwe.to_ref();
let two_n: usize = n_glwe << 1;
let base2k: usize = brk.base2k().into();
let dnum: usize = brk.dnum().into();
@@ -410,8 +409,8 @@ fn execute_block_binary<DataRes, DataIn, DataBrk, B: Backend>(
fn execute_standard<DataRes, DataIn, DataBrk, B: Backend>(
module: &Module<B>,
res: &mut GLWECiphertext<DataRes>,
lwe: &LWECiphertext<DataIn>,
res: &mut GLWE<DataRes>,
lwe: &LWE<DataIn>,
lut: &LookUpTable,
brk: &BlindRotationKeyPrepared<DataBrk, CGGI, B>,
scratch: &mut Scratch<B>,
@@ -480,8 +479,8 @@ fn execute_standard<DataRes, DataIn, DataBrk, B: Backend>(
}
let mut lwe_2n: Vec<i64> = vec![0i64; (lwe.n() + 1).into()]; // TODO: from scratch space
let mut out_mut: GLWECiphertext<&mut [u8]> = res.to_mut();
let lwe_ref: LWECiphertext<&[u8]> = lwe.to_ref();
let mut out_mut: GLWE<&mut [u8]> = res.to_mut();
let lwe_ref: LWE<&[u8]> = lwe.to_ref();
mod_switch_2n(
2 * lut.domain_size(),
@@ -519,7 +518,7 @@ fn execute_standard<DataRes, DataIn, DataBrk, B: Backend>(
out_mut.normalize_inplace(module, scratch_1);
}
pub fn mod_switch_2n(n: usize, res: &mut [i64], lwe: &LWECiphertext<&[u8]>, rot_dir: LookUpTableRotationDirection) {
pub fn mod_switch_2n(n: usize, res: &mut [i64], lwe: &LWE<&[u8]>, rot_dir: LookUpTableRotationDirection) {
let base2k: usize = lwe.base2k().into();
let log2n: usize = usize::BITS as usize - (n - 1).leading_zeros() as usize + 1;

View File

@@ -15,7 +15,7 @@ use poulpy_core::{
Distribution,
layouts::{
GGSW, GGSWInfos, LWESecret,
compressed::GGSWCiphertextCompressed,
compressed::GGSWCompressed,
prepared::{GGSWPrepared, GLWESecretPrepared},
},
};
@@ -32,7 +32,7 @@ impl BlindRotationKeyAlloc for BlindRotationKey<Vec<u8>, CGGI> {
{
let mut data: Vec<GGSW<Vec<u8>>> = Vec::with_capacity(infos.n_lwe().into());
for _ in 0..infos.n_lwe().as_usize() {
data.push(GGSW::alloc(infos));
data.push(GGSW::alloc_from_infos(infos));
}
Self {
@@ -137,8 +137,8 @@ impl BlindRotationKeyCompressed<Vec<u8>, CGGI> {
where
A: BlindRotationKeyInfos,
{
let mut data: Vec<GGSWCiphertextCompressed<Vec<u8>>> = Vec::with_capacity(infos.n_lwe().into());
(0..infos.n_lwe().as_usize()).for_each(|_| data.push(GGSWCiphertextCompressed::alloc(infos)));
let mut data: Vec<GGSWCompressed<Vec<u8>>> = Vec::with_capacity(infos.n_lwe().into());
(0..infos.n_lwe().as_usize()).for_each(|_| data.push(GGSWCompressed::alloc_from_infos(infos)));
Self {
keys: data,
dist: Distribution::NONE,
@@ -151,7 +151,7 @@ impl BlindRotationKeyCompressed<Vec<u8>, CGGI> {
A: GGSWInfos,
Module<B>: VecZnxNormalizeTmpBytes + VecZnxDftAllocBytes,
{
GGSWCiphertextCompressed::encrypt_sk_scratch_space(module, infos)
GGSWCompressed::encrypt_sk_scratch_space(module, infos)
}
}

View File

@@ -8,14 +8,14 @@ use std::{fmt, marker::PhantomData};
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use poulpy_core::{
Distribution,
layouts::{Base2K, Degree, Dsize, GGSWInfos, GLWEInfos, LWEInfos, TorusPrecision, compressed::GGSWCiphertextCompressed},
layouts::{Base2K, Degree, Dsize, GGSWInfos, GLWEInfos, LWEInfos, TorusPrecision, compressed::GGSWCompressed},
};
use crate::tfhe::blind_rotation::{BlindRotationAlgo, BlindRotationKeyInfos};
#[derive(Clone)]
pub struct BlindRotationKeyCompressed<D: Data, BRT: BlindRotationAlgo> {
pub(crate) keys: Vec<GGSWCiphertextCompressed<D>>,
pub(crate) keys: Vec<GGSWCompressed<D>>,
pub(crate) dist: Distribution,
pub(crate) _phantom: PhantomData<BRT>,
}

View File

@@ -14,7 +14,7 @@ pub use lut::*;
pub mod tests;
use poulpy_core::layouts::{GLWECiphertext, LWECiphertext};
use poulpy_core::layouts::{GLWE, LWE};
use poulpy_hal::layouts::{Backend, DataMut, DataRef, Module, Scratch};
pub trait BlindRotationAlgo {}
@@ -27,8 +27,8 @@ pub trait BlincRotationExecute<B: Backend> {
fn execute<DR: DataMut, DI: DataRef>(
&self,
module: &Module<B>,
res: &mut GLWECiphertext<DR>,
lwe: &LWECiphertext<DI>,
res: &mut GLWE<DR>,
lwe: &LWE<DI>,
lut: &LookUpTable,
scratch: &mut Scratch<B>,
);

View File

@@ -23,8 +23,7 @@ use crate::tfhe::blind_rotation::{
};
use poulpy_core::layouts::{
GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret, LWECiphertext, LWECiphertextLayout, LWECiphertextToRef,
LWEInfos, LWEPlaintext, LWESecret,
GLWE, GLWELayout, GLWEPlaintext, GLWESecret, LWE, LWECiphertextLayout, LWECiphertextToRef, LWEInfos, LWEPlaintext, LWESecret,
prepared::{GLWESecretPrepared, PrepareAlloc},
};
@@ -111,7 +110,7 @@ where
rank: rank.into(),
};
let glwe_infos: GLWECiphertextLayout = GLWECiphertextLayout {
let glwe_infos: GLWELayout = GLWELayout {
n: n_glwe.into(),
base2k: base2k.into(),
k: k_res.into(),
@@ -128,7 +127,7 @@ where
module, &brk_infos,
));
let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc(&glwe_infos);
let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc_from_infos(&glwe_infos);
sk_glwe.fill_ternary_prob(0.5, &mut source_xs);
let sk_glwe_dft: GLWESecretPrepared<Vec<u8>, B> = sk_glwe.prepare_alloc(module, scratch.borrow());
@@ -154,9 +153,9 @@ where
scratch.borrow(),
);
let mut lwe: LWECiphertext<Vec<u8>> = LWECiphertext::alloc(&lwe_infos);
let mut lwe: LWE<Vec<u8>> = LWE::alloc_from_infos(&lwe_infos);
let mut pt_lwe: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc(&lwe_infos);
let mut pt_lwe: LWEPlaintext<Vec<u8>> = LWEPlaintext::alloc_from_infos(&lwe_infos);
let x: i64 = 15 % (message_modulus as i64);
@@ -175,13 +174,13 @@ where
let mut lut: LookUpTable = LookUpTable::alloc(module, base2k, k_lut, extension_factor);
lut.set(module, &f_vec, log_message_modulus + 1);
let mut res: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&glwe_infos);
let mut res: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_infos);
let brk_prepared: BlindRotationKeyPrepared<Vec<u8>, CGGI, B> = brk.prepare_alloc(module, scratch.borrow());
brk_prepared.execute(module, &mut res, &lwe, &lut, scratch_br.borrow());
let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&glwe_infos);
let mut pt_have: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_infos);
res.decrypt(module, &mut pt_have, &sk_glwe_dft, scratch.borrow());