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

@@ -3,8 +3,8 @@ use std::hint::black_box;
use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main};
use poulpy_backend::{FFT64Avx, FFT64Ref, FFT64Spqlios};
use poulpy_core::layouts::{
AutomorphismKeyLayout, Dsize, GGSW, GGSWCiphertextLayout, GLWESecret, LWECiphertext, LWECiphertextLayout, LWESecret,
TensorKeyLayout, prepared::PrepareAlloc,
AutomorphismKeyLayout, Dsize, GGSW, GGSWCiphertextLayout, GLWESecret, LWE, LWECiphertextLayout, LWESecret, TensorKeyLayout,
prepared::PrepareAlloc,
};
use poulpy_hal::{
api::{
@@ -202,10 +202,10 @@ where
sk_lwe.fill_binary_block(params.block_size, &mut source_xs);
sk_lwe.fill_zero();
let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc_with(n_glwe, rank);
let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc(n_glwe, rank);
sk_glwe.fill_ternary_prob(0.5, &mut source_xs);
let ct_lwe: LWECiphertext<Vec<u8>> = LWECiphertext::alloc(&params.lwe_infos);
let ct_lwe: LWE<Vec<u8>> = LWE::alloc_from_infos(&params.lwe_infos);
// Circuit bootstrapping evaluation key
let cbt_key: CircuitBootstrappingKey<Vec<u8>, BRA> = CircuitBootstrappingKey::encrypt_sk(
@@ -218,7 +218,7 @@ where
scratch.borrow(),
);
let mut res: GGSW<Vec<u8>> = GGSW::alloc(&params.ggsw_infos);
let mut res: GGSW<Vec<u8>> = GGSW::alloc_from_infos(&params.ggsw_infos);
let cbt_prepared: CircuitBootstrappingKeyPrepared<Vec<u8>, BRA, B> = cbt_key.prepare_alloc(&module, scratch.borrow());
move || {

View File

@@ -1,8 +1,8 @@
use poulpy_core::{
GLWEOperations,
layouts::{
AutomorphismKeyLayout, GGSW, GGSWCiphertextLayout, GLWECiphertext, GLWECiphertextLayout, GLWEPlaintext, GLWESecret,
LWECiphertext, LWECiphertextLayout, LWEInfos, LWEPlaintext, LWESecret, TensorKeyLayout,
AutomorphismKeyLayout, GGSW, GGSWCiphertextLayout, GLWE, GLWELayout, GLWEPlaintext, GLWESecret, LWE, LWECiphertextLayout,
LWEInfos, LWEPlaintext, LWESecret, TensorKeyLayout,
prepared::{GGSWPrepared, GLWESecretPrepared, PrepareAlloc},
},
};
@@ -140,7 +140,7 @@ fn main() {
sk_lwe.fill_zero();
// GLWE secret
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);
// sk_glwe.fill_zero();
@@ -151,7 +151,7 @@ fn main() {
let data: i64 = 1 % (1 << k_lwe_pt);
// LWE plaintext
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());
// LWE plaintext(data * 2^{- (k_lwe_pt - 1)})
pt_lwe.encode_i64(data, (k_lwe_pt + 1).into()); // +1 for padding bit
@@ -167,7 +167,7 @@ fn main() {
println!("pt_lwe: {pt_lwe}");
// LWE ciphertext
let mut ct_lwe: LWECiphertext<Vec<u8>> = LWECiphertext::alloc(&lwe_infos);
let mut ct_lwe: LWE<Vec<u8>> = LWE::alloc_from_infos(&lwe_infos);
// Encrypt LWE Plaintext
ct_lwe.encrypt_sk(&module, &pt_lwe, &sk_lwe, &mut source_xa, &mut source_xe);
@@ -187,7 +187,7 @@ fn main() {
println!("CBT-KGEN: {} ms", now.elapsed().as_millis());
// Output GGSW
let mut res: GGSW<Vec<u8>> = GGSW::alloc(&ggsw_infos);
let mut res: GGSW<Vec<u8>> = GGSW::alloc_from_infos(&ggsw_infos);
// Circuit bootstrapping key prepared (opaque backend dependant write only struct)
let cbt_prepared: CircuitBootstrappingKeyPrepared<Vec<u8>, CGGI, BackendImpl> =
@@ -214,7 +214,7 @@ fn main() {
// Tests RLWE(1) * GGSW(data)
let glwe_infos: GLWECiphertextLayout = GLWECiphertextLayout {
let glwe_infos: GLWELayout = GLWELayout {
n: n_glwe.into(),
base2k: base2k.into(),
k: (k_ggsw_res - base2k).into(),
@@ -222,11 +222,11 @@ fn main() {
};
// GLWE ciphertext modulus
let mut ct_glwe: GLWECiphertext<Vec<u8>> = GLWECiphertext::alloc(&glwe_infos);
let mut ct_glwe: GLWE<Vec<u8>> = GLWE::alloc_from_infos(&glwe_infos);
// Some GLWE plaintext with signed data
let k_glwe_pt: usize = 3;
let mut pt_glwe: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&glwe_infos);
let mut pt_glwe: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_infos);
let mut data_vec: Vec<i64> = vec![0i64; n_glwe];
data_vec
.iter_mut()
@@ -255,7 +255,7 @@ fn main() {
ct_glwe.external_product_inplace(&module, &res_prepared, scratch.borrow());
// Decrypt
let mut pt_res: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc(&glwe_infos);
let mut pt_res: GLWEPlaintext<Vec<u8>> = GLWEPlaintext::alloc_from_infos(&glwe_infos);
ct_glwe.decrypt(&module, &mut pt_res, &sk_glwe_prepared, scratch.borrow());
println!("pt_res: {:?}", &pt_res.data.at(0, 0)[..64]);

View File

@@ -1,6 +1,6 @@
use std::marker::PhantomData;
use poulpy_core::layouts::{Base2K, GLWECiphertext, GLWEInfos, GLWEPlaintextLayout, LWEInfos, Rank, TorusPrecision};
use poulpy_core::layouts::{Base2K, GLWE, GLWEInfos, GLWEPlaintextLayout, LWEInfos, Rank, TorusPrecision};
use poulpy_core::{TakeGLWEPt, layouts::prepared::GLWESecretPrepared};
use poulpy_hal::api::VecZnxBigAllocBytes;
@@ -24,7 +24,7 @@ use crate::tfhe::bdd_arithmetic::{FromBits, ToBits, UnsignedInteger};
/// An FHE ciphertext encrypting the bits of an [UnsignedInteger].
pub struct FheUintBlocks<D: Data, T: UnsignedInteger> {
pub(crate) blocks: Vec<GLWECiphertext<D>>,
pub(crate) blocks: Vec<GLWE<D>>,
pub(crate) _base: u8,
pub(crate) _phantom: PhantomData<T>,
}
@@ -62,7 +62,7 @@ impl<T: UnsignedInteger> FheUintBlocks<Vec<u8>, T> {
pub(crate) fn alloc_with<BE: Backend>(module: &Module<BE>, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self {
Self {
blocks: (0..T::WORD_SIZE)
.map(|_| GLWECiphertext::alloc_with(module.n().into(), base2k, k, rank))
.map(|_| GLWE::alloc(module.n().into(), base2k, k, rank))
.collect(),
_base: 1,
_phantom: PhantomData,

View File

@@ -60,7 +60,7 @@ impl<T: UnsignedInteger> FheUintBlocksPrepDebug<Vec<u8>, T> {
) -> Self {
Self {
blocks: (0..T::WORD_SIZE)
.map(|_| GGSW::alloc_with(module.n().into(), base2k, k, rank, dnum, dsize))
.map(|_| GGSW::alloc(module.n().into(), base2k, k, rank, dnum, dsize))
.collect(),
_base: 1,
_phantom: PhantomData,

View File

@@ -2,7 +2,7 @@ use itertools::Itertools;
use poulpy_core::{
GLWEOperations, TakeGLWECtSlice, TakeGLWEPt, glwe_packing,
layouts::{
GLWECiphertext, GLWEInfos, GLWEPlaintextLayout, LWEInfos, TorusPrecision,
GLWE, GLWEInfos, GLWEPlaintextLayout, LWEInfos, TorusPrecision,
prepared::{AutomorphismKeyPrepared, GLWESecretPrepared},
},
};
@@ -24,14 +24,14 @@ use std::{collections::HashMap, marker::PhantomData};
use crate::tfhe::bdd_arithmetic::{FromBits, ToBits, UnsignedInteger};
/// A FHE ciphertext encrypting a [UnsignedInteger].
pub struct FheUintWord<D: Data, T: UnsignedInteger>(pub(crate) GLWECiphertext<D>, pub(crate) PhantomData<T>);
pub struct FheUintWord<D: Data, T: UnsignedInteger>(pub(crate) GLWE<D>, pub(crate) PhantomData<T>);
impl<D: DataMut, T: UnsignedInteger> FheUintWord<D, T> {
#[allow(dead_code)]
fn post_process<ATK, BE: Backend>(
&mut self,
module: &Module<BE>,
mut tmp_res: Vec<GLWECiphertext<&mut [u8]>>,
mut tmp_res: Vec<GLWE<&mut [u8]>>,
auto_keys: &HashMap<i64, AutomorphismKeyPrepared<ATK, BE>>,
scratch: &mut Scratch<BE>,
) where
@@ -67,7 +67,7 @@ impl<D: DataMut, T: UnsignedInteger> FheUintWord<D, T> {
// Repacks the GLWE ciphertexts bits
let gap: usize = module.n() / T::WORD_SIZE;
let log_gap: usize = (usize::BITS - (gap - 1).leading_zeros()) as usize;
let mut cts: HashMap<usize, &mut GLWECiphertext<&mut [u8]>> = HashMap::new();
let mut cts: HashMap<usize, &mut GLWE<&mut [u8]>> = HashMap::new();
for (i, ct) in tmp_res.iter_mut().enumerate().take(T::WORD_SIZE) {
cts.insert(i * gap, ct);
}

View File

@@ -2,7 +2,7 @@ use itertools::Itertools;
use poulpy_core::{
GLWEExternalProductInplace, GLWEOperations, TakeGLWECtSlice,
layouts::{
GLWECiphertext, GLWECiphertextToMut, LWEInfos,
GLWE, GLWEToMut, LWEInfos,
prepared::{GGSWCiphertextPreparedToRef, GGSWPrepared},
},
};
@@ -38,7 +38,7 @@ where
fn execute<O>(
&self,
module: &Module<BE>,
out: &mut [GLWECiphertext<O>],
out: &mut [GLWE<O>],
inputs: &[&dyn GGSWCiphertextPreparedToRef<BE>],
scratch: &mut Scratch<BE>,
) where
@@ -54,7 +54,7 @@ where
fn execute<O>(
&self,
module: &Module<BE>,
out: &mut [GLWECiphertext<O>],
out: &mut [GLWE<O>],
inputs: &[&dyn GGSWCiphertextPreparedToRef<BE>],
scratch: &mut Scratch<BE>,
) where
@@ -159,14 +159,8 @@ impl Node {
}
pub trait Cmux<BE: Backend> {
fn cmux<O, T, F, S>(
&self,
out: &mut GLWECiphertext<O>,
t: &GLWECiphertext<T>,
f: &GLWECiphertext<F>,
s: &GGSWPrepared<S, BE>,
scratch: &mut Scratch<BE>,
) where
fn cmux<O, T, F, S>(&self, out: &mut GLWE<O>, t: &GLWE<T>, f: &GLWE<F>, s: &GGSWPrepared<S, BE>, scratch: &mut Scratch<BE>)
where
O: DataMut,
T: DataRef,
F: DataRef,
@@ -177,14 +171,8 @@ impl<BE: Backend> Cmux<BE> for Module<BE>
where
Module<BE>: GLWEExternalProductInplace<BE> + VecZnxSub + VecZnxCopy + VecZnxNegateInplace + VecZnxAddInplace,
{
fn cmux<O, T, F, S>(
&self,
out: &mut GLWECiphertext<O>,
t: &GLWECiphertext<T>,
f: &GLWECiphertext<F>,
s: &GGSWPrepared<S, BE>,
scratch: &mut Scratch<BE>,
) where
fn cmux<O, T, F, S>(&self, out: &mut GLWE<O>, t: &GLWE<T>, f: &GLWE<F>, s: &GGSWPrepared<S, BE>, scratch: &mut Scratch<BE>)
where
O: DataMut,
T: DataRef,
F: DataRef,

View File

@@ -11,7 +11,7 @@ use crate::tfhe::{
use poulpy_core::{
TakeGGSW, TakeGLWECt,
layouts::{
GLWESecret, GLWEToLWEKeyLayout, GLWEToLWESwitchingKey, LWECiphertext, LWESecret,
GLWESecret, GLWEToLWEKeyLayout, GLWEToLWESwitchingKey, LWE, LWESecret,
prepared::{GLWEToLWESwitchingKeyPrepared, Prepare, PrepareAlloc},
},
};
@@ -182,7 +182,7 @@ where
{
assert_eq!(out.blocks.len(), bits.blocks.len());
}
let mut lwe: LWECiphertext<Vec<u8>> = LWECiphertext::alloc(&bits.blocks[0]); //TODO: add TakeLWE
let mut lwe: LWE<Vec<u8>> = LWE::alloc(&bits.blocks[0]); //TODO: add TakeLWE
let (mut tmp_ggsw, scratch_1) = scratch.take_ggsw(out);
for (dst, src) in out.blocks.iter_mut().zip(bits.blocks.iter()) {
lwe.from_glwe(module, src, &self.ks, scratch_1);
@@ -231,7 +231,7 @@ where
{
assert_eq!(out.blocks.len(), bits.blocks.len());
}
let mut lwe: LWECiphertext<Vec<u8>> = LWECiphertext::alloc(&bits.blocks[0]); //TODO: add TakeLWE
let mut lwe: LWE<Vec<u8>> = LWE::alloc(&bits.blocks[0]); //TODO: add TakeLWE
for (dst, src) in out.blocks.iter_mut().zip(bits.blocks.iter()) {
lwe.from_glwe(module, src, &self.ks, scratch);
self.cbt

View File

@@ -1,6 +1,6 @@
#[cfg(test)]
use poulpy_core::layouts::{
AutomorphismKeyLayout, Base2K, Degree, Dnum, Dsize, GGSWCiphertextLayout, GLWECiphertextLayout, GLWEToLWEKeyLayout, Rank,
AutomorphismKeyLayout, Base2K, Degree, Dnum, Dsize, GGSWCiphertextLayout, GLWELayout, GLWEToLWEKeyLayout, Rank,
TensorKeyLayout, TorusPrecision,
};
@@ -25,7 +25,7 @@ pub(crate) const TEST_BLOCK_SIZE: u32 = 7;
pub(crate) const TEST_RANK: u32 = 2;
#[cfg(test)]
pub(crate) static TEST_GLWE_INFOS: GLWECiphertextLayout = GLWECiphertextLayout {
pub(crate) static TEST_GLWE_INFOS: GLWELayout = GLWELayout {
n: Degree(TEST_N_GLWE),
base2k: Base2K(TEST_BASE2K),
k: TorusPrecision(TEST_K_GLWE),

View File

@@ -4,7 +4,7 @@ use poulpy_backend::FFT64Ref;
use poulpy_core::{
TakeGGSW, TakeGLWEPt,
layouts::{
GGSWCiphertextLayout, GLWECiphertextLayout, GLWESecret, LWEInfos, LWESecret,
GGSWCiphertextLayout, GLWELayout, GLWESecret, LWEInfos, LWESecret,
prepared::{GLWESecretPrepared, PrepareAlloc},
},
};
@@ -107,7 +107,7 @@ where
BlindRotationKeyPrepared<Vec<u8>, BRA, BE>: BlincRotationExecute<BE>,
BlindRotationKey<Vec<u8>, BRA>: BlindRotationKeyAlloc + BlindRotationKeyEncryptSk<BE>,
{
let glwe_infos: GLWECiphertextLayout = TEST_GLWE_INFOS;
let glwe_infos: GLWELayout = TEST_GLWE_INFOS;
let ggsw_infos: GGSWCiphertextLayout = TEST_GGSW_INFOS;
let n_glwe: usize = glwe_infos.n().into();
@@ -120,7 +120,7 @@ where
let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(1 << 22);
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_prep: GLWESecretPrepared<Vec<u8>, BE> = sk_glwe.prepare_alloc(&module, scratch.borrow());

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());

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.