Update FheUint ciphertext naming + circuit evaluation based on GetGGSWBit

This commit is contained in:
Pro7ech
2025-10-28 15:43:30 +01:00
parent a2aecfd380
commit 8c1cc354e3
23 changed files with 265 additions and 282 deletions

View File

@@ -15,10 +15,10 @@ 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) GLWE<D>, pub(crate) PhantomData<T>);
/// 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>);
impl<D: DataMut, T: UnsignedInteger> FheUintWord<D, T> {
impl<D: DataMut, T: UnsignedInteger> FheUintCompressed<D, T> {
#[allow(dead_code)]
fn post_process<ATK, M, BE: Backend>(
&mut self,
@@ -46,7 +46,7 @@ impl<D: DataMut, T: UnsignedInteger> FheUintWord<D, T> {
}
}
impl<D: DataRef, T: UnsignedInteger> LWEInfos for FheUintWord<D, T> {
impl<D: DataRef, T: UnsignedInteger> LWEInfos for FheUintCompressed<D, T> {
fn base2k(&self) -> poulpy_core::layouts::Base2K {
self.0.base2k()
}
@@ -60,13 +60,13 @@ impl<D: DataRef, T: UnsignedInteger> LWEInfos for FheUintWord<D, T> {
}
}
impl<D: DataRef, T: UnsignedInteger> GLWEInfos for FheUintWord<D, T> {
impl<D: DataRef, T: UnsignedInteger> GLWEInfos for FheUintCompressed<D, T> {
fn rank(&self) -> poulpy_core::layouts::Rank {
self.0.rank()
}
}
impl<D: DataMut, T: UnsignedInteger + ToBits> FheUintWord<D, T> {
impl<D: DataMut, T: UnsignedInteger + ToBits> FheUintCompressed<D, T> {
pub fn encrypt_sk<S, M, BE: Backend>(
&mut self,
module: &M,
@@ -109,7 +109,7 @@ impl<D: DataMut, T: UnsignedInteger + ToBits> FheUintWord<D, T> {
}
}
impl<D: DataRef, T: UnsignedInteger + FromBits> FheUintWord<D, T> {
impl<D: DataRef, T: UnsignedInteger + FromBits> FheUintCompressed<D, T> {
pub fn decrypt<S, M, BE: Backend>(&self, module: &M, sk: &S, scratch: &mut Scratch<BE>) -> T
where
S: GLWESecretPreparedToRef<BE> + GLWEInfos,

View File

@@ -18,31 +18,24 @@ use crate::tfhe::bdd_arithmetic::ToBits;
use crate::tfhe::bdd_arithmetic::UnsignedInteger;
/// A prepared FHE ciphertext encrypting the bits of an [UnsignedInteger].
pub struct FheUintBlocksPrepared<D: Data, T: UnsignedInteger, B: Backend> {
pub(crate) blocks: Vec<GGSWPrepared<D, B>>,
pub(crate) _base: u8,
pub struct FheUintPrepared<D: Data, T: UnsignedInteger, B: Backend> {
pub(crate) bits: Vec<GGSWPrepared<D, B>>,
pub(crate) _phantom: PhantomData<T>,
}
impl<D: Data, T: UnsignedInteger, B: Backend> FheUintBlocksPrepared<D, T, B> {
pub fn blocks(&self) -> &Vec<GGSWPrepared<D, B>> {
&self.blocks
}
}
impl<T: UnsignedInteger, BE: Backend> FheUintBlocksPreparedFactory<T, BE> for Module<BE> where
Self: Sized + GGSWPreparedFactory<BE>
{
}
pub trait GetGGSWBit<T: UnsignedInteger, BE: Backend> {
pub trait GetGGSWBit<BE: Backend> {
fn get_bit(&self, bit: usize) -> GGSWPrepared<&[u8], BE>;
}
impl<D: DataRef, T: UnsignedInteger, BE: Backend> GetGGSWBit<T, BE> for FheUintBlocksPrepared<D, T, BE> {
impl<D: DataRef, T: UnsignedInteger, BE: Backend> GetGGSWBit<BE> for FheUintPrepared<D, T, BE> {
fn get_bit(&self, bit: usize) -> GGSWPrepared<&[u8], BE> {
assert!(bit <= self.blocks.len());
self.blocks[bit].to_ref()
assert!(bit <= self.bits.len());
self.bits[bit].to_ref()
}
}
@@ -50,10 +43,10 @@ pub trait GetGGSWBitMut<T: UnsignedInteger, BE: Backend> {
fn get_bit(&mut self, bit: usize) -> GGSWPrepared<&mut [u8], BE>;
}
impl<D: DataMut, T: UnsignedInteger, BE: Backend> GetGGSWBitMut<T, BE> for FheUintBlocksPrepared<D, T, BE> {
impl<D: DataMut, T: UnsignedInteger, BE: Backend> GetGGSWBitMut<T, BE> for FheUintPrepared<D, T, BE> {
fn get_bit(&mut self, bit: usize) -> GGSWPrepared<&mut [u8], BE> {
assert!(bit <= self.blocks.len());
self.blocks[bit].to_mut()
assert!(bit <= self.bits.len());
self.bits[bit].to_mut()
}
}
@@ -61,28 +54,27 @@ pub trait FheUintBlocksPreparedFactory<T: UnsignedInteger, BE: Backend>
where
Self: Sized + GGSWPreparedFactory<BE>,
{
fn alloc_fhe_uint_blocks_prepared(
fn alloc_fhe_uint_prepared(
&self,
base2k: Base2K,
k: TorusPrecision,
dnum: Dnum,
dsize: Dsize,
rank: Rank,
) -> FheUintBlocksPrepared<Vec<u8>, T, BE> {
FheUintBlocksPrepared {
blocks: (0..T::WORD_SIZE)
) -> FheUintPrepared<Vec<u8>, T, BE> {
FheUintPrepared {
bits: (0..T::WORD_SIZE)
.map(|_| GGSWPrepared::alloc(self, base2k, k, dnum, dsize, rank))
.collect(),
_base: 1,
_phantom: PhantomData,
}
}
fn alloc_fhe_uint_blocks_prepared_from_infos<A>(&self, infos: &A) -> FheUintBlocksPrepared<Vec<u8>, T, BE>
fn alloc_fhe_uint_prepared_from_infos<A>(&self, infos: &A) -> FheUintPrepared<Vec<u8>, T, BE>
where
A: GGSWInfos,
{
self.alloc_fhe_uint_blocks_prepared(
self.alloc_fhe_uint_prepared(
infos.base2k(),
infos.k(),
infos.dnum(),
@@ -92,20 +84,20 @@ where
}
}
impl<T: UnsignedInteger, BE: Backend> FheUintBlocksPrepared<Vec<u8>, T, BE> {
impl<T: UnsignedInteger, BE: Backend> FheUintPrepared<Vec<u8>, T, BE> {
pub fn alloc<A, M>(module: &M, infos: &A) -> Self
where
A: GGSWInfos,
M: FheUintBlocksPreparedFactory<T, BE>,
{
module.alloc_fhe_uint_blocks_prepared_from_infos(infos)
module.alloc_fhe_uint_prepared_from_infos(infos)
}
pub fn alloc_with<M>(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> Self
where
M: FheUintBlocksPreparedFactory<T, BE>,
{
module.alloc_fhe_uint_blocks_prepared(base2k, k, dnum, dsize, rank)
module.alloc_fhe_uint_prepared(base2k, k, dnum, dsize, rank)
}
}
@@ -118,9 +110,9 @@ pub trait FheUintBlocksPreparedEncryptSk<T: UnsignedInteger + ToBits, BE: Backen
where
Self: Sized + ModuleN + GGSWEncryptSk<BE> + GGSWPreparedFactory<BE>,
{
fn fhe_uint_blocks_prepared_encrypt_sk<DM, S>(
fn fhe_uint_prepared_encrypt_sk<DM, S>(
&self,
res: &mut FheUintBlocksPrepared<DM, T, BE>,
res: &mut FheUintPrepared<DM, T, BE>,
value: T,
sk: &S,
source_xa: &mut Source,
@@ -145,12 +137,12 @@ where
use poulpy_hal::layouts::ZnxViewMut;
pt.at_mut(0, 0)[0] = value.bit(i) as i64;
tmp_ggsw.encrypt_sk(self, &pt, sk, source_xa, source_xe, scratch_2);
res.blocks[i].prepare(self, &tmp_ggsw, scratch_2);
res.bits[i].prepare(self, &tmp_ggsw, scratch_2);
}
}
}
impl<D: DataMut, T: UnsignedInteger + ToBits, BE: Backend> FheUintBlocksPrepared<D, T, BE> {
impl<D: DataMut, T: UnsignedInteger + ToBits, BE: Backend> FheUintPrepared<D, T, BE> {
pub fn encrypt_sk<M, S>(
&mut self,
module: &M,
@@ -164,36 +156,36 @@ impl<D: DataMut, T: UnsignedInteger + ToBits, BE: Backend> FheUintBlocksPrepared
M: FheUintBlocksPreparedEncryptSk<T, BE>,
Scratch<BE>: ScratchTakeCore<BE>,
{
module.fhe_uint_blocks_prepared_encrypt_sk(self, value, sk, source_xa, source_xe, scratch);
module.fhe_uint_prepared_encrypt_sk(self, value, sk, source_xa, source_xe, scratch);
}
}
impl<D: DataRef, T: UnsignedInteger, B: Backend> LWEInfos for FheUintBlocksPrepared<D, T, B> {
impl<D: DataRef, T: UnsignedInteger, B: Backend> LWEInfos for FheUintPrepared<D, T, B> {
fn base2k(&self) -> poulpy_core::layouts::Base2K {
self.blocks[0].base2k()
self.bits[0].base2k()
}
fn k(&self) -> poulpy_core::layouts::TorusPrecision {
self.blocks[0].k()
self.bits[0].k()
}
fn n(&self) -> poulpy_core::layouts::Degree {
self.blocks[0].n()
self.bits[0].n()
}
}
impl<D: DataRef, T: UnsignedInteger, B: Backend> GLWEInfos for FheUintBlocksPrepared<D, T, B> {
impl<D: DataRef, T: UnsignedInteger, B: Backend> GLWEInfos for FheUintPrepared<D, T, B> {
fn rank(&self) -> poulpy_core::layouts::Rank {
self.blocks[0].rank()
self.bits[0].rank()
}
}
impl<D: DataRef, T: UnsignedInteger, B: Backend> GGSWInfos for FheUintBlocksPrepared<D, T, B> {
impl<D: DataRef, T: UnsignedInteger, B: Backend> GGSWInfos for FheUintPrepared<D, T, B> {
fn dsize(&self) -> poulpy_core::layouts::Dsize {
self.blocks[0].dsize()
self.bits[0].dsize()
}
fn dnum(&self) -> poulpy_core::layouts::Dnum {
self.blocks[0].dnum()
self.bits[0].dnum()
}
}

View File

@@ -1,8 +1,8 @@
use std::marker::PhantomData;
use crate::tfhe::bdd_arithmetic::{BDDKeyPrepared, FheUintBlockDebugPrepare, ToBits};
use crate::tfhe::bdd_arithmetic::{BddKeyPrepared, FheUintBlockDebugPrepare, ToBits};
use crate::tfhe::{
bdd_arithmetic::{FheUintBlocks, UnsignedInteger},
bdd_arithmetic::{FheUint, UnsignedInteger},
blind_rotation::BlindRotationAlgo,
circuit_bootstrapping::CirtuitBootstrappingExecute,
};
@@ -18,13 +18,12 @@ use poulpy_core::{
use poulpy_hal::api::ModuleN;
use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch};
pub struct FheUintBlocksPreparedDebug<D: Data, T: UnsignedInteger> {
pub(crate) blocks: Vec<GGSW<D>>,
pub(crate) _base: u8,
pub struct FheUintPreparedDebug<D: Data, T: UnsignedInteger> {
pub(crate) bits: Vec<GGSW<D>>,
pub(crate) _phantom: PhantomData<T>,
}
impl<T: UnsignedInteger> FheUintBlocksPreparedDebug<Vec<u8>, T> {
impl<T: UnsignedInteger> FheUintPreparedDebug<Vec<u8>, T> {
pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self
where
M: ModuleN,
@@ -45,52 +44,51 @@ impl<T: UnsignedInteger> FheUintBlocksPreparedDebug<Vec<u8>, T> {
M: ModuleN,
{
Self {
blocks: (0..T::WORD_SIZE)
bits: (0..T::WORD_SIZE)
.map(|_| GGSW::alloc(module.n().into(), base2k, k, rank, dnum, dsize))
.collect(),
_base: 1,
_phantom: PhantomData,
}
}
}
impl<D: DataRef, T: UnsignedInteger> LWEInfos for FheUintBlocksPreparedDebug<D, T> {
impl<D: DataRef, T: UnsignedInteger> LWEInfos for FheUintPreparedDebug<D, T> {
fn base2k(&self) -> poulpy_core::layouts::Base2K {
self.blocks[0].base2k()
self.bits[0].base2k()
}
fn k(&self) -> poulpy_core::layouts::TorusPrecision {
self.blocks[0].k()
self.bits[0].k()
}
fn n(&self) -> poulpy_core::layouts::Degree {
self.blocks[0].n()
self.bits[0].n()
}
}
impl<D: DataRef, T: UnsignedInteger> GLWEInfos for FheUintBlocksPreparedDebug<D, T> {
impl<D: DataRef, T: UnsignedInteger> GLWEInfos for FheUintPreparedDebug<D, T> {
fn rank(&self) -> poulpy_core::layouts::Rank {
self.blocks[0].rank()
self.bits[0].rank()
}
}
impl<D: DataRef, T: UnsignedInteger> GGSWInfos for FheUintBlocksPreparedDebug<D, T> {
impl<D: DataRef, T: UnsignedInteger> GGSWInfos for FheUintPreparedDebug<D, T> {
fn dsize(&self) -> poulpy_core::layouts::Dsize {
self.blocks[0].dsize()
self.bits[0].dsize()
}
fn dnum(&self) -> poulpy_core::layouts::Dnum {
self.blocks[0].dnum()
self.bits[0].dnum()
}
}
impl<D: DataRef, T: UnsignedInteger + ToBits> FheUintBlocksPreparedDebug<D, T> {
impl<D: DataRef, T: UnsignedInteger + ToBits> FheUintPreparedDebug<D, T> {
pub fn print_noise<S, M, BE: Backend>(&self, module: &M, sk: &S, want: T)
where
S: GLWESecretPreparedToRef<BE>,
M: GGSWNoise<BE>,
{
for (i, ggsw) in self.blocks.iter().enumerate() {
for (i, ggsw) in self.bits.iter().enumerate() {
use poulpy_hal::layouts::{ScalarZnx, ZnxViewMut};
let mut pt_want = ScalarZnx::alloc(self.n().into(), 1);
pt_want.at_mut(0, 0)[0] = want.bit(i) as i64;
@@ -104,7 +102,7 @@ impl<D: DataRef, T: UnsignedInteger + ToBits> FheUintBlocksPreparedDebug<D, T> {
M: GGSWNoise<BE>,
F: Fn(usize) -> f64,
{
for (i, ggsw) in self.blocks.iter().enumerate() {
for (i, ggsw) in self.bits.iter().enumerate() {
use poulpy_hal::layouts::{ScalarZnx, ZnxViewMut};
let mut pt_want = ScalarZnx::alloc(self.n().into(), 1);
pt_want.at_mut(0, 0)[0] = want.bit(i) as i64;
@@ -118,33 +116,33 @@ where
Self: LWEFromGLWE<BE> + CirtuitBootstrappingExecute<BRA, BE> + GGSWPreparedFactory<BE>,
Scratch<BE>: ScratchTakeCore<BE>,
{
fn fhe_uint_block_debug_prepare<DM, DR0, DR1>(
fn fhe_uint_debug_prepare<DM, DR0, DR1>(
&self,
res: &mut FheUintBlocksPreparedDebug<DM, T>,
bits: &FheUintBlocks<DR0, T>,
key: &BDDKeyPrepared<DR1, BRA, BE>,
res: &mut FheUintPreparedDebug<DM, T>,
bits: &FheUint<DR0, T>,
key: &BddKeyPrepared<DR1, BRA, BE>,
scratch: &mut Scratch<BE>,
) where
DM: DataMut,
DR0: DataRef,
DR1: DataRef,
{
assert_eq!(res.blocks.len(), bits.blocks.len());
assert_eq!(res.bits.len(), bits.bits.len());
let mut lwe: LWE<Vec<u8>> = LWE::alloc_from_infos(&bits.blocks[0]); //TODO: add TakeLWE
for (dst, src) in res.blocks.iter_mut().zip(bits.blocks.iter()) {
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);
key.cbt.execute_to_constant(self, dst, &lwe, 1, 1, scratch);
}
}
}
impl<D: DataMut, T: UnsignedInteger> FheUintBlocksPreparedDebug<D, T> {
impl<D: DataMut, T: UnsignedInteger> FheUintPreparedDebug<D, T> {
pub fn prepare<BRA, M, O, K, BE: Backend>(
&mut self,
module: &M,
other: &FheUintBlocks<O, T>,
key: &BDDKeyPrepared<K, BRA, BE>,
other: &FheUint<O, T>,
key: &BddKeyPrepared<K, BRA, BE>,
scratch: &mut Scratch<BE>,
) where
BRA: BlindRotationAlgo,
@@ -153,6 +151,6 @@ impl<D: DataMut, T: UnsignedInteger> FheUintBlocksPreparedDebug<D, T> {
M: FheUintBlockDebugPrepare<BRA, T, BE>,
Scratch<BE>: ScratchTakeCore<BE>,
{
module.fhe_uint_block_debug_prepare(self, other, key, scratch);
module.fhe_uint_debug_prepare(self, other, key, scratch);
}
}

View File

@@ -13,50 +13,42 @@ use poulpy_hal::source::Source;
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<GLWE<D>>,
pub(crate) _base: u8,
pub struct FheUint<D: Data, T: UnsignedInteger> {
pub(crate) bits: Vec<GLWE<D>>,
pub(crate) _phantom: PhantomData<T>,
}
impl<D: DataRef, T: UnsignedInteger> FheUintBlocks<D, T> {
pub fn blocks(&self) -> &Vec<GLWE<D>> {
&self.blocks
}
}
impl<D: DataRef, T: UnsignedInteger> LWEInfos for FheUintBlocks<D, T> {
impl<D: DataRef, T: UnsignedInteger> LWEInfos for FheUint<D, T> {
fn base2k(&self) -> poulpy_core::layouts::Base2K {
self.blocks[0].base2k()
self.bits[0].base2k()
}
fn k(&self) -> poulpy_core::layouts::TorusPrecision {
self.blocks[0].k()
self.bits[0].k()
}
fn n(&self) -> poulpy_core::layouts::Degree {
self.blocks[0].n()
self.bits[0].n()
}
}
impl<D: DataRef, T: UnsignedInteger> GLWEInfos for FheUintBlocks<D, T> {
impl<D: DataRef, T: UnsignedInteger> GLWEInfos for FheUint<D, T> {
fn rank(&self) -> poulpy_core::layouts::Rank {
self.blocks[0].rank()
self.bits[0].rank()
}
}
impl<D: Data, T: UnsignedInteger> FheUintBlocks<D, T> {
pub fn new(blocks: Vec<GLWE<D>>) -> Self {
assert_eq!(blocks.len(), T::WORD_SIZE);
impl<D: Data, T: UnsignedInteger> FheUint<D, T> {
pub fn new(bits: Vec<GLWE<D>>) -> Self {
assert_eq!(bits.len(), T::WORD_SIZE);
Self {
blocks,
_base: 1,
bits,
_phantom: PhantomData,
}
}
}
impl<T: UnsignedInteger> FheUintBlocks<Vec<u8>, T> {
impl<T: UnsignedInteger> FheUint<Vec<u8>, T> {
pub fn alloc_from_infos<A, BE: Backend>(module: &Module<BE>, infos: &A) -> Self
where
A: GLWEInfos,
@@ -66,16 +58,15 @@ impl<T: UnsignedInteger> FheUintBlocks<Vec<u8>, T> {
pub fn alloc<BE: Backend>(module: &Module<BE>, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self {
Self {
blocks: (0..T::WORD_SIZE)
bits: (0..T::WORD_SIZE)
.map(|_| GLWE::alloc(module.n().into(), base2k, k, rank))
.collect(),
_base: 1,
_phantom: PhantomData,
}
}
}
impl<D: DataMut, T: UnsignedInteger + ToBits> FheUintBlocks<D, T> {
impl<D: DataMut, T: UnsignedInteger + ToBits> FheUint<D, T> {
pub fn encrypt_sk<S, BE: Backend>(
&mut self,
module: &Module<BE>,
@@ -110,12 +101,12 @@ impl<D: DataMut, T: UnsignedInteger + ToBits> FheUintBlocks<D, T> {
for i in 0..T::WORD_SIZE {
pt.encode_coeff_i64(value.bit(i) as i64, TorusPrecision(2), 0);
self.blocks[i].encrypt_sk(module, &pt, sk, source_xa, source_xe, scratch_1);
self.bits[i].encrypt_sk(module, &pt, sk, source_xa, source_xe, scratch_1);
}
}
}
impl<D: DataRef, T: UnsignedInteger + FromBits + ToBits> FheUintBlocks<D, T> {
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>,
@@ -143,7 +134,7 @@ impl<D: DataRef, T: UnsignedInteger + FromBits + ToBits> FheUintBlocks<D, T> {
let scale: f64 = 4.0 / ((1 << base2k) as f64);
for (i, bit) in bits.iter_mut().enumerate().take(T::WORD_SIZE) {
self.blocks[i].decrypt(module, &mut pt, sk, scratch_1);
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;
}
@@ -177,7 +168,7 @@ impl<D: DataRef, T: UnsignedInteger + FromBits + ToBits> FheUintBlocks<D, T> {
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.blocks[i].noise(module, sk, &pt_want, scratch_1);
*noise_i = self.bits[i].noise(module, sk, &pt_want, scratch_1);
}
noise

View File

@@ -1,11 +1,11 @@
mod block;
mod block_prepared;
mod word;
mod fhe_uint_compressed;
mod fhe_uint_prepared;
mod fheuint;
mod block_debug;
mod fhe_uint_prepared_debug;
pub use block_debug::*;
pub use fhe_uint_prepared_debug::*;
pub use block::*;
pub use block_prepared::*;
pub use word::*;
pub use fhe_uint_compressed::*;
pub use fhe_uint_prepared::*;
pub use fheuint::*;