update BDD ciphertext types + API for GLWEToLWE

This commit is contained in:
Pro7ech
2025-10-29 00:59:45 +01:00
parent 37c76b6420
commit c761d2cae0
23 changed files with 576 additions and 618 deletions

View File

@@ -1,9 +1,9 @@
use itertools::Itertools;
use poulpy_core::{
GLWECopy, GLWEDecrypt, GLWEEncryptSk, GLWEPacking, ScratchTakeCore,
GLWECopy, GLWEDecrypt, GLWEEncryptSk, GLWEPacking, GLWERotate, LWEFromGLWE, ScratchTakeCore,
layouts::{
GLWE, GLWEInfos, GLWEPlaintextLayout, GLWESecretPreparedToRef, LWEInfos, TorusPrecision,
prepared::GLWEAutomorphismKeyPrepared,
Base2K, Degree, GGLWEInfos, GGLWEPreparedToRef, GLWE, GLWEInfos, GLWEPlaintextLayout, GLWESecretPreparedToRef, GLWEToRef,
LWEInfos, LWEToMut, Rank, TorusPrecision, prepared::GLWEAutomorphismKeyPrepared,
},
};
use poulpy_hal::{
@@ -15,58 +15,49 @@ use std::{collections::HashMap, marker::PhantomData};
use crate::tfhe::bdd_arithmetic::{FromBits, ToBits, UnsignedInteger};
/// An FHE ciphertext encrypting the bits of an [UnsignedInteger] in compressed format (ideal for decryption/serialization).
pub struct FheUintCompressed<D: Data, T: UnsignedInteger>(pub(crate) GLWE<D>, pub(crate) PhantomData<T>);
/// An FHE ciphertext encrypting the bits of an [UnsignedInteger].
pub struct FheUint<D: Data, T: UnsignedInteger> {
pub(crate) bits: GLWE<D>,
pub(crate) _phantom: PhantomData<T>,
}
impl<D: DataMut, T: UnsignedInteger> FheUintCompressed<D, T> {
#[allow(dead_code)]
fn post_process<ATK, M, BE: Backend>(
&mut self,
module: &M,
mut tmp_res: Vec<GLWE<&mut [u8]>>,
auto_keys: &HashMap<i64, GLWEAutomorphismKeyPrepared<ATK, BE>>,
scratch: &mut Scratch<BE>,
) where
ATK: DataRef,
M: GLWEPacking<BE> + GLWECopy,
Scratch<BE>: ScratchTakeCore<BE>,
impl<T: UnsignedInteger> FheUint<Vec<u8>, T> {
pub fn alloc_from_infos<A>(infos: &A) -> Self
where
A: GLWEInfos,
{
// 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 GLWE<&mut [u8]>> = HashMap::new();
for (i, ct) in tmp_res.iter_mut().enumerate().take(T::WORD_SIZE) {
cts.insert(i * gap, ct);
Self::alloc(infos.n(), infos.base2k(), infos.k(), infos.rank())
}
pub fn alloc(n: Degree, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self {
Self {
bits: GLWE::alloc(n, base2k, k, rank),
_phantom: PhantomData,
}
module.glwe_pack(&mut cts, log_gap, auto_keys, scratch);
// And copies the repacked ciphertext on the receiver.
module.glwe_copy(&mut self.0, cts.remove(&0).unwrap());
}
}
impl<D: DataRef, T: UnsignedInteger> LWEInfos for FheUintCompressed<D, T> {
impl<D: DataRef, T: UnsignedInteger> LWEInfos for FheUint<D, T> {
fn base2k(&self) -> poulpy_core::layouts::Base2K {
self.0.base2k()
self.bits.base2k()
}
fn k(&self) -> poulpy_core::layouts::TorusPrecision {
self.0.k()
self.bits.k()
}
fn n(&self) -> poulpy_core::layouts::Degree {
self.0.n()
self.bits.n()
}
}
impl<D: DataRef, T: UnsignedInteger> GLWEInfos for FheUintCompressed<D, T> {
impl<D: DataRef, T: UnsignedInteger> GLWEInfos for FheUint<D, T> {
fn rank(&self) -> poulpy_core::layouts::Rank {
self.0.rank()
self.bits.rank()
}
}
impl<D: DataMut, T: UnsignedInteger + ToBits> FheUintCompressed<D, T> {
impl<D: DataMut, T: UnsignedInteger + ToBits> FheUint<D, T> {
pub fn encrypt_sk<S, M, BE: Backend>(
&mut self,
module: &M,
@@ -103,13 +94,13 @@ impl<D: DataMut, T: UnsignedInteger + ToBits> FheUintCompressed<D, T> {
let (mut pt, scratch_1) = scratch.take_glwe_plaintext(&pt_infos);
pt.encode_vec_i64(&data_bits, TorusPrecision(1));
self.0
pt.encode_vec_i64(&data_bits, TorusPrecision(2));
self.bits
.encrypt_sk(module, &pt, sk, source_xa, source_xe, scratch_1);
}
}
impl<D: DataRef, T: UnsignedInteger + FromBits> FheUintCompressed<D, T> {
impl<D: DataRef, T: UnsignedInteger + FromBits> FheUint<D, T> {
pub fn decrypt<S, M, BE: Backend>(&self, module: &M, sk: &S, scratch: &mut Scratch<BE>) -> T
where
S: GLWESecretPreparedToRef<BE> + GLWEInfos,
@@ -133,13 +124,61 @@ impl<D: DataRef, T: UnsignedInteger + FromBits> FheUintCompressed<D, T> {
let (mut pt, scratch_1) = scratch.take_glwe_plaintext(&pt_infos);
self.0.decrypt(module, &mut pt, sk, scratch_1);
self.bits.decrypt(module, &mut pt, sk, scratch_1);
let mut data: Vec<i64> = vec![0i64; module.n()];
pt.decode_vec_i64(&mut data, TorusPrecision(1));
pt.decode_vec_i64(&mut data, TorusPrecision(2));
let bits: Vec<u8> = data.iter().step_by(gap).map(|c| *c as u8).collect_vec();
T::from_bits(&bits)
}
}
impl<D: DataMut, T: UnsignedInteger> FheUint<D, T> {
#[allow(dead_code)]
pub(crate) fn pack<D1, ATK, M, BE: Backend>(
&mut self,
module: &M,
mut tmp_res: Vec<GLWE<D1>>,
auto_keys: &HashMap<i64, GLWEAutomorphismKeyPrepared<ATK, BE>>,
scratch: &mut Scratch<BE>,
) where
D1: DataMut,
ATK: DataRef,
M: GLWEPacking<BE> + GLWECopy,
Scratch<BE>: ScratchTakeCore<BE>,
{
// 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 GLWE<D1>> = HashMap::new();
for (i, ct) in tmp_res.iter_mut().enumerate().take(T::WORD_SIZE) {
cts.insert(i * gap, ct);
}
module.glwe_pack(&mut cts, log_gap, auto_keys, scratch);
// And copies the repacked ciphertext on the receiver.
module.glwe_copy(&mut self.bits, cts.remove(&0).unwrap());
}
}
impl<D: DataRef, T: UnsignedInteger> FheUint<D, T> {
pub fn get_bit<L, K, M, BE: Backend>(&self, module: &M, bit: usize, res: &mut L, ks: &K, scratch: &mut Scratch<BE>)
where
L: LWEToMut,
K: GGLWEPreparedToRef<BE> + GGLWEInfos,
M: LWEFromGLWE<BE> + GLWERotate<BE>,
Scratch<BE>: ScratchTakeCore<BE>,
{
let gap: usize = module.n() / T::WORD_SIZE;
res.to_mut().from_glwe(module, self, bit * gap, ks, scratch);
}
}
impl<D: DataRef, T: UnsignedInteger> GLWEToRef for FheUint<D, T> {
fn to_ref(&self) -> GLWE<&[u8]> {
self.bits.to_ref()
}
}

View File

@@ -1,10 +1,8 @@
use std::marker::PhantomData;
use crate::tfhe::bdd_arithmetic::{BddKeyPrepared, FheUintBlockDebugPrepare, ToBits};
use crate::tfhe::bdd_arithmetic::{BDDKeyPrepared, FheUint, FheUintBlockDebugPrepare, ToBits};
use crate::tfhe::{
bdd_arithmetic::{FheUint, UnsignedInteger},
blind_rotation::BlindRotationAlgo,
circuit_bootstrapping::CirtuitBootstrappingExecute,
bdd_arithmetic::UnsignedInteger, blind_rotation::BlindRotationAlgo, circuit_bootstrapping::CirtuitBootstrappingExecute,
};
use poulpy_core::GGSWNoise;
@@ -113,25 +111,23 @@ impl<D: DataRef, T: UnsignedInteger + ToBits> FheUintPreparedDebug<D, T> {
impl<BRA: BlindRotationAlgo, BE: Backend, T: UnsignedInteger> FheUintBlockDebugPrepare<BRA, T, BE> for Module<BE>
where
Self: LWEFromGLWE<BE> + CirtuitBootstrappingExecute<BRA, BE> + GGSWPreparedFactory<BE>,
Self: ModuleN + LWEFromGLWE<BE> + CirtuitBootstrappingExecute<BRA, BE> + GGSWPreparedFactory<BE>,
Scratch<BE>: ScratchTakeCore<BE>,
{
fn fhe_uint_debug_prepare<DM, DR0, DR1>(
&self,
res: &mut FheUintPreparedDebug<DM, T>,
bits: &FheUint<DR0, T>,
key: &BddKeyPrepared<DR1, BRA, BE>,
key: &BDDKeyPrepared<DR1, BRA, BE>,
scratch: &mut Scratch<BE>,
) where
DM: DataMut,
DR0: DataRef,
DR1: DataRef,
{
assert_eq!(res.bits.len(), bits.bits.len());
let mut lwe: LWE<Vec<u8>> = LWE::alloc_from_infos(&bits.bits[0]); //TODO: add TakeLWE
for (dst, src) in res.bits.iter_mut().zip(bits.bits.iter()) {
lwe.from_glwe(self, src, &key.ks, scratch);
let mut lwe: LWE<Vec<u8>> = LWE::alloc_from_infos(bits); //TODO: add TakeLWE
for (bit, dst) in res.bits.iter_mut().enumerate() {
bits.get_bit(self, bit, &mut lwe, &key.ks, scratch);
key.cbt.execute_to_constant(self, dst, &lwe, 1, 1, scratch);
}
}
@@ -142,7 +138,7 @@ impl<D: DataMut, T: UnsignedInteger> FheUintPreparedDebug<D, T> {
&mut self,
module: &M,
other: &FheUint<O, T>,
key: &BddKeyPrepared<K, BRA, BE>,
key: &BDDKeyPrepared<K, BRA, BE>,
scratch: &mut Scratch<BE>,
) where
BRA: BlindRotationAlgo,

View File

@@ -1,176 +0,0 @@
use std::marker::PhantomData;
use poulpy_core::{
GLWEDecrypt, GLWENoise,
layouts::{Base2K, GLWE, GLWEInfos, GLWEPlaintextLayout, GLWESecretPreparedToRef, LWEInfos, Rank, TorusPrecision},
};
use poulpy_core::GLWEEncryptSk;
use poulpy_core::ScratchTakeCore;
use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, ZnxZero};
use poulpy_hal::source::Source;
use crate::tfhe::bdd_arithmetic::{FromBits, ToBits, UnsignedInteger};
/// An FHE ciphertext encrypting the bits of an [UnsignedInteger].
pub struct FheUint<D: Data, T: UnsignedInteger> {
pub(crate) bits: Vec<GLWE<D>>,
pub(crate) _phantom: PhantomData<T>,
}
impl<D: DataRef, T: UnsignedInteger> LWEInfos for FheUint<D, T> {
fn base2k(&self) -> poulpy_core::layouts::Base2K {
self.bits[0].base2k()
}
fn k(&self) -> poulpy_core::layouts::TorusPrecision {
self.bits[0].k()
}
fn n(&self) -> poulpy_core::layouts::Degree {
self.bits[0].n()
}
}
impl<D: DataRef, T: UnsignedInteger> GLWEInfos for FheUint<D, T> {
fn rank(&self) -> poulpy_core::layouts::Rank {
self.bits[0].rank()
}
}
impl<D: Data, T: UnsignedInteger> FheUint<D, T> {
pub fn new(bits: Vec<GLWE<D>>) -> Self {
assert_eq!(bits.len(), T::WORD_SIZE);
Self {
bits,
_phantom: PhantomData,
}
}
}
impl<T: UnsignedInteger> FheUint<Vec<u8>, T> {
pub fn alloc_from_infos<A, BE: Backend>(module: &Module<BE>, infos: &A) -> Self
where
A: GLWEInfos,
{
Self::alloc(module, infos.base2k(), infos.k(), infos.rank())
}
pub fn alloc<BE: Backend>(module: &Module<BE>, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self {
Self {
bits: (0..T::WORD_SIZE)
.map(|_| GLWE::alloc(module.n().into(), base2k, k, rank))
.collect(),
_phantom: PhantomData,
}
}
}
impl<D: DataMut, T: UnsignedInteger + ToBits> FheUint<D, T> {
pub fn encrypt_sk<S, BE: Backend>(
&mut self,
module: &Module<BE>,
value: T,
sk: &S,
source_xa: &mut Source,
source_xe: &mut Source,
scratch: &mut Scratch<BE>,
) where
S: GLWESecretPreparedToRef<BE> + GLWEInfos,
Module<BE>: GLWEEncryptSk<BE>,
Scratch<BE>: ScratchTakeCore<BE>,
{
use poulpy_core::layouts::GLWEPlaintextLayout;
use poulpy_hal::layouts::ZnxZero;
#[cfg(debug_assertions)]
{
assert!(module.n().is_multiple_of(T::WORD_SIZE));
assert_eq!(self.n(), module.n() as u32);
assert_eq!(sk.n(), module.n() as u32);
}
let pt_infos = GLWEPlaintextLayout {
n: self.n(),
base2k: self.base2k(),
k: 1_usize.into(),
};
let (mut pt, scratch_1) = scratch.take_glwe_plaintext(&pt_infos);
pt.data.zero();
for i in 0..T::WORD_SIZE {
pt.encode_coeff_i64(value.bit(i) as i64, TorusPrecision(2), 0);
self.bits[i].encrypt_sk(module, &pt, sk, source_xa, source_xe, scratch_1);
}
}
}
impl<D: DataRef, T: UnsignedInteger + FromBits + ToBits> FheUint<D, T> {
pub fn decrypt<S, BE: Backend>(&self, module: &Module<BE>, sk: &S, scratch: &mut Scratch<BE>) -> T
where
Module<BE>: GLWEDecrypt<BE>,
S: GLWESecretPreparedToRef<BE> + GLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>,
{
#[cfg(debug_assertions)]
{
assert!(module.n().is_multiple_of(T::WORD_SIZE));
assert_eq!(self.n(), module.n() as u32);
assert_eq!(sk.n(), module.n() as u32);
}
let pt_infos = GLWEPlaintextLayout {
n: self.n(),
base2k: self.base2k(),
k: self.k(),
};
let (mut pt, scratch_1) = scratch.take_glwe_plaintext(&pt_infos);
let mut bits: Vec<u8> = vec![0u8; T::WORD_SIZE];
let base2k: usize = self.base2k().into();
let scale: f64 = 4.0 / ((1 << base2k) as f64);
for (i, bit) in bits.iter_mut().enumerate().take(T::WORD_SIZE) {
self.bits[i].decrypt(module, &mut pt, sk, scratch_1);
let value: i64 = pt.decode_coeff_i64(base2k.into(), 0);
*bit = ((value as f64) * scale).round() as u8;
}
T::from_bits(&bits)
}
pub fn noise<S, BE: Backend>(&self, module: &Module<BE>, sk: &S, want: T, scratch: &mut Scratch<BE>) -> Vec<f64>
where
Module<BE>: GLWENoise<BE>,
S: GLWESecretPreparedToRef<BE> + GLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>,
{
#[cfg(debug_assertions)]
{
assert!(module.n().is_multiple_of(T::WORD_SIZE));
assert_eq!(self.n(), module.n() as u32);
assert_eq!(sk.n(), module.n() as u32);
}
let pt_infos = GLWEPlaintextLayout {
n: self.n(),
base2k: self.base2k(),
k: 1_usize.into(),
};
let (mut pt_want, scratch_1) = scratch.take_glwe_plaintext(&pt_infos);
pt_want.data.zero();
let mut noise: Vec<f64> = vec![0f64; T::WORD_SIZE];
for (i, noise_i) in noise.iter_mut().enumerate().take(T::WORD_SIZE) {
pt_want.encode_coeff_i64(want.bit(i) as i64, TorusPrecision(2), 0);
*noise_i = self.bits[i].noise(module, sk, &pt_want, scratch_1);
}
noise
}
}

View File

@@ -1,11 +1,9 @@
mod fhe_uint_compressed;
mod fhe_uint;
mod fhe_uint_prepared;
mod fheuint;
mod fhe_uint_prepared_debug;
pub use fhe_uint_prepared_debug::*;
pub use fhe_uint_compressed::*;
pub use fhe_uint::*;
pub use fhe_uint_prepared::*;
pub use fheuint::*;