This commit is contained in:
Pro7ech
2025-10-22 16:43:46 +02:00
parent 5755aea58c
commit cedf7b9c59
26 changed files with 713 additions and 723 deletions

View File

@@ -1,26 +1,17 @@
use std::marker::PhantomData;
use poulpy_core::layouts::{Base2K, GLWE, GLWEInfos, GLWEPlaintextLayout, LWEInfos, Rank, TorusPrecision};
use poulpy_core::{
GLWEDecrypt, GLWENoise,
layouts::{Base2K, GLWE, GLWEInfos, GLWEPlaintextLayout, GLWESecretPreparedToRef, LWEInfos, Rank, TorusPrecision},
};
#[cfg(test)]
use poulpy_core::GLWEEncryptSk;
use poulpy_core::ScratchTakeCore;
use poulpy_core::{layouts::prepared::GLWESecretPrepared};
use poulpy_hal::api::VecZnxBigBytesOf;
use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch};
#[cfg(test)]
use poulpy_hal::api::{
VecZnxAddInplace, VecZnxAddNormal, VecZnxFillUniform, VecZnxNormalize, VecZnxSub,
};
#[cfg(test)]
use poulpy_hal::source::Source;
use poulpy_hal::{
api::{
VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigNormalize, VecZnxDftApply,
VecZnxDftBytesOf, VecZnxIdftApplyConsume, VecZnxNormalizeTmpBytes,
},
layouts::{Backend, Data, DataMut, DataRef, Module, Scratch},
};
use poulpy_hal::api::{SvpApplyDftToDftInplace, VecZnxNormalizeInplace, VecZnxSubInplace};
use crate::tfhe::bdd_arithmetic::{FromBits, ToBits, UnsignedInteger};
@@ -79,25 +70,13 @@ impl<D: DataMut, T: UnsignedInteger + ToBits> FheUintBlocks<D, T> {
&mut self,
module: &Module<BE>,
value: T,
sk: &GLWESecretPrepared<S, BE>,
sk: &S,
source_xa: &mut Source,
source_xe: &mut Source,
scratch: &mut Scratch<BE>,
) where
S: DataRef,
Module<BE>: VecZnxDftBytesOf
+ VecZnxBigNormalize<BE>
+ VecZnxDftApply<BE>
+ SvpApplyDftToDftInplace<BE>
+ VecZnxIdftApplyConsume<BE>
+ VecZnxNormalizeTmpBytes
+ VecZnxFillUniform
+ VecZnxSubInplace
+ VecZnxAddInplace
+ VecZnxNormalizeInplace<BE>
+ VecZnxAddNormal
+ VecZnxNormalize<BE>
+ VecZnxSub,
S: GLWESecretPreparedToRef<BE> + GLWEInfos,
Module<BE>: GLWEEncryptSk<BE>,
Scratch<BE>: ScratchTakeCore<BE>,
{
use poulpy_core::layouts::GLWEPlaintextLayout;
@@ -115,29 +94,20 @@ impl<D: DataMut, T: UnsignedInteger + ToBits> FheUintBlocks<D, T> {
k: 1_usize.into(),
};
let (mut pt, scratch_1) = scratch.take_glwe_pt(&pt_infos);
let (mut pt, scratch_1) = scratch.take_glwe_plaintext(&pt_infos);
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.blocks[i].encrypt_sk(module, &pt, sk, source_xa, source_xe, scratch_1);
}
}
}
impl<D: DataRef, T: UnsignedInteger + FromBits + ToBits> FheUintBlocks<D, T> {
pub fn decrypt<S: DataRef, BE: Backend>(
&self,
module: &Module<BE>,
sk: &GLWESecretPrepared<S, BE>,
scratch: &mut Scratch<BE>,
) -> T
pub fn decrypt<S, BE: Backend>(&self, module: &Module<BE>, sk: &S, scratch: &mut Scratch<BE>) -> T
where
Module<BE>: VecZnxDftApply<BE>
+ SvpApplyDftToDftInplace<BE>
+ VecZnxIdftApplyConsume<BE>
+ VecZnxBigAddInplace<BE>
+ VecZnxBigAddSmallInplace<BE>
+ VecZnxBigNormalize<BE>,
Module<BE>: GLWEDecrypt<BE>,
S: GLWESecretPreparedToRef<BE> + GLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>,
{
#[cfg(debug_assertions)]
@@ -153,7 +123,7 @@ impl<D: DataRef, T: UnsignedInteger + FromBits + ToBits> FheUintBlocks<D, T> {
k: self.k(),
};
let (mut pt, scratch_1) = scratch.take_glwe_pt(&pt_infos);
let (mut pt, scratch_1) = scratch.take_glwe_plaintext(&pt_infos);
let mut bits: Vec<u8> = vec![0u8; T::WORD_SIZE];
@@ -169,25 +139,10 @@ impl<D: DataRef, T: UnsignedInteger + FromBits + ToBits> FheUintBlocks<D, T> {
T::from_bits(&bits)
}
pub fn noise<S: DataRef, BE: Backend>(
&self,
module: &Module<BE>,
sk: &GLWESecretPrepared<S, BE>,
want: T,
scratch: &mut Scratch<BE>,
) -> Vec<f64>
pub fn noise<S, BE: Backend>(&self, module: &Module<BE>, sk: &S, want: T, scratch: &mut Scratch<BE>) -> Vec<f64>
where
Module<BE>: VecZnxDftBytesOf
+ VecZnxBigBytesOf
+ VecZnxDftApply<BE>
+ SvpApplyDftToDftInplace<BE>
+ VecZnxIdftApplyConsume<BE>
+ VecZnxBigAddInplace<BE>
+ VecZnxBigAddSmallInplace<BE>
+ VecZnxBigNormalize<BE>
+ VecZnxNormalizeTmpBytes
+ VecZnxSubInplace
+ VecZnxNormalizeInplace<BE>,
Module<BE>: GLWENoise<BE>,
S: GLWESecretPreparedToRef<BE> + GLWEInfos,
Scratch<BE>: ScratchTakeCore<BE>,
{
#[cfg(debug_assertions)]
@@ -203,7 +158,7 @@ impl<D: DataRef, T: UnsignedInteger + FromBits + ToBits> FheUintBlocks<D, T> {
k: 1_usize.into(),
};
let (mut pt_want, scratch_1) = scratch.take_glwe_pt(&pt_infos);
let (mut pt_want, scratch_1) = scratch.take_glwe_plaintext(&pt_infos);
let mut noise: Vec<f64> = vec![0f64; T::WORD_SIZE];

View File

@@ -0,0 +1,147 @@
use std::marker::PhantomData;
use crate::tfhe::bdd_arithmetic::{BDDKeyPrepared, FheUintBlockDebugPrepare, ToBits};
use crate::tfhe::{
bdd_arithmetic::{FheUintBlocks, UnsignedInteger},
blind_rotation::BlindRotationAlgo,
circuit_bootstrapping::CirtuitBootstrappingExecute,
};
use poulpy_core::GGSWNoise;
#[cfg(test)]
use poulpy_core::layouts::{Base2K, Dnum, Dsize, Rank, TorusPrecision};
use poulpy_core::layouts::{GGSW, GLWESecretPreparedToRef};
use poulpy_core::{
LWEFromGLWE, ScratchTakeCore,
layouts::{GGSWInfos, GGSWPreparedFactory, GLWEInfos, LWE, LWEInfos},
};
#[cfg(test)]
use poulpy_hal::api::ModuleN;
use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch};
pub(crate) struct FheUintBlocksPreparedDebug<D: Data, T: UnsignedInteger> {
pub(crate) blocks: Vec<GGSW<D>>,
pub(crate) _base: u8,
pub(crate) _phantom: PhantomData<T>,
}
#[cfg(test)]
impl<T: UnsignedInteger> FheUintBlocksPreparedDebug<Vec<u8>, T> {
#[allow(dead_code)]
pub(crate) fn alloc<A, M>(module: &M, infos: &A) -> Self
where
M: ModuleN,
A: GGSWInfos,
{
Self::alloc_with(
module,
infos.base2k(),
infos.k(),
infos.dnum(),
infos.dsize(),
infos.rank(),
)
}
#[allow(dead_code)]
pub(crate) fn alloc_with<M>(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> Self
where
M: ModuleN,
{
Self {
blocks: (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> {
fn base2k(&self) -> poulpy_core::layouts::Base2K {
self.blocks[0].base2k()
}
fn k(&self) -> poulpy_core::layouts::TorusPrecision {
self.blocks[0].k()
}
fn n(&self) -> poulpy_core::layouts::Degree {
self.blocks[0].n()
}
}
impl<D: DataRef, T: UnsignedInteger> GLWEInfos for FheUintBlocksPreparedDebug<D, T> {
fn rank(&self) -> poulpy_core::layouts::Rank {
self.blocks[0].rank()
}
}
impl<D: DataRef, T: UnsignedInteger> GGSWInfos for FheUintBlocksPreparedDebug<D, T> {
fn dsize(&self) -> poulpy_core::layouts::Dsize {
self.blocks[0].dsize()
}
fn dnum(&self) -> poulpy_core::layouts::Dnum {
self.blocks[0].dnum()
}
}
impl<D: DataRef, T: UnsignedInteger + ToBits> FheUintBlocksPreparedDebug<D, T> {
pub(crate) fn 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() {
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;
ggsw.print_noise(module, sk, &pt_want);
}
}
}
impl<BRA: BlindRotationAlgo, BE: Backend, T: UnsignedInteger> FheUintBlockDebugPrepare<BRA, T, BE> for Module<BE>
where
Self: LWEFromGLWE<BE> + CirtuitBootstrappingExecute<BRA, BE> + GGSWPreparedFactory<BE>,
Scratch<BE>: ScratchTakeCore<BE>,
{
fn fhe_uint_block_debug_prepare<DM, DR0, DR1>(
&self,
res: &mut FheUintBlocksPreparedDebug<DM, T>,
bits: &FheUintBlocks<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());
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()) {
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> {
pub fn prepare<BRA, M, O, K, BE: Backend>(
&mut self,
module: &M,
other: &FheUintBlocks<O, T>,
key: &BDDKeyPrepared<K, BRA, BE>,
scratch: &mut Scratch<BE>,
) where
BRA: BlindRotationAlgo,
O: DataRef,
K: DataRef,
M: FheUintBlockDebugPrepare<BRA, T, BE>,
Scratch<BE>: ScratchTakeCore<BE>,
{
module.fhe_uint_block_debug_prepare(self, other, key, scratch);
}
}

View File

@@ -1,89 +1,60 @@
use std::marker::PhantomData;
use poulpy_core::layouts::{Base2K, Dnum, Dsize, GGSWInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, prepared::GGSWPrepared};
#[cfg(test)]
use poulpy_core::{
layouts::{prepared::GLWESecretPrepared, GGSW}, ScratchTakeCore,
};
use poulpy_hal::{
api::VmpPMatAlloc,
layouts::{Backend, Data, DataMut, DataRef, Module, Scratch},
use poulpy_core::layouts::{
Base2K, Dnum, Dsize, GGSWInfos, GGSWPreparedFactory, GLWEInfos, LWEInfos, Rank, TorusPrecision, prepared::GGSWPrepared,
};
#[cfg(test)]
use poulpy_core::{GGSWEncryptSk, ScratchTakeCore, layouts::GLWESecretPreparedToRef};
use poulpy_hal::layouts::{Backend, Data, DataRef, Module};
#[cfg(test)]
use poulpy_hal::{
api::{
SvpApplyDftToDftInplace, VecZnxAddInplace, VecZnxAddNormal,
VecZnxAddScalarInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAlloc, VecZnxBigBytesOf,
VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxDftAlloc, VecZnxDftApply, VecZnxDftBytesOf, VecZnxFillUniform,
VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub,
VecZnxSubInplace, VmpPrepare,
},
api::ModuleN,
layouts::{DataMut, Scratch},
source::Source,
};
use crate::tfhe::bdd_arithmetic::{FheUintBlocks, FheUintPrepare, ToBits, UnsignedInteger};
#[cfg(test)]
pub(crate) struct FheUintBlocksPrepDebug<D: Data, T: UnsignedInteger> {
pub(crate) blocks: Vec<GGSW<D>>,
pub(crate) _base: u8,
pub(crate) _phantom: PhantomData<T>,
}
#[cfg(test)]
impl<T: UnsignedInteger> FheUintBlocksPrepDebug<Vec<u8>, T> {
#[allow(dead_code)]
pub(crate) fn alloc<A, BE: Backend>(module: &Module<BE>, infos: &A) -> Self
where
A: GGSWInfos,
{
Self::alloc_with(
module,
infos.base2k(),
infos.k(),
infos.dnum(),
infos.dsize(),
infos.rank(),
)
}
#[allow(dead_code)]
pub(crate) fn alloc_with<BE: Backend>(
module: &Module<BE>,
base2k: Base2K,
k: TorusPrecision,
dnum: Dnum,
dsize: Dsize,
rank: Rank,
) -> Self {
Self {
blocks: (0..T::WORD_SIZE)
.map(|_| GGSW::alloc(module.n().into(), base2k, k, rank, dnum, dsize))
.collect(),
_base: 1,
_phantom: PhantomData,
}
}
}
use crate::tfhe::bdd_arithmetic::ToBits;
use crate::tfhe::bdd_arithmetic::UnsignedInteger;
/// A prepared FHE ciphertext encrypting the bits of an [UnsignedInteger].
pub struct FheUintBlocksPrep<D: Data, B: Backend, T: UnsignedInteger> {
pub struct FheUintBlocksPrepared<D: Data, T: UnsignedInteger, B: Backend> {
pub(crate) blocks: Vec<GGSWPrepared<D, B>>,
pub(crate) _base: u8,
pub(crate) _phantom: PhantomData<T>,
}
impl<T: UnsignedInteger, BE: Backend> FheUintBlocksPrep<Vec<u8>, BE, T>
where
Module<BE>: VmpPMatAlloc<BE>,
impl<T: UnsignedInteger, BE: Backend> FheUintBlocksPreparedFactory<T, BE> for Module<BE> where
Self: Sized + GGSWPreparedFactory<BE>
{
#[allow(dead_code)]
pub(crate) fn alloc<A>(module: &Module<BE>, infos: &A) -> Self
}
pub trait FheUintBlocksPreparedFactory<T: UnsignedInteger, BE: Backend>
where
Self: Sized + GGSWPreparedFactory<BE>,
{
fn alloc_fhe_uint_blocks_prepared(
&self,
base2k: Base2K,
k: TorusPrecision,
dnum: Dnum,
dsize: Dsize,
rank: Rank,
) -> FheUintBlocksPrepared<Vec<u8>, T, BE> {
FheUintBlocksPrepared {
blocks: (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>
where
A: GGSWInfos,
{
Self::alloc_with(
module,
self.alloc_fhe_uint_blocks_prepared(
infos.base2k(),
infos.k(),
infos.dnum(),
@@ -91,129 +62,90 @@ where
infos.rank(),
)
}
}
impl<T: UnsignedInteger, BE: Backend> FheUintBlocksPrepared<Vec<u8>, T, BE> {
#[allow(dead_code)]
pub(crate) 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)
}
#[allow(dead_code)]
pub(crate) fn alloc_with(module: &Module<BE>, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> Self
pub(crate) fn alloc_with<M>(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> Self
where
Module<BE>: VmpPMatAlloc<BE>,
M: FheUintBlocksPreparedFactory<T, BE>,
{
Self {
blocks: (0..T::WORD_SIZE)
.map(|_| GGSWPrepared::alloc(module, base2k, k, dnum, dsize, rank))
.collect(),
_base: 1,
_phantom: PhantomData,
}
module.alloc_fhe_uint_blocks_prepared(base2k, k, dnum, dsize, rank)
}
}
impl<D: DataMut, T: UnsignedInteger + ToBits, BE: Backend> FheUintBlocksPrep<D, BE, T> {
#[allow(dead_code)]
#[cfg(test)]
pub(crate) fn encrypt_sk<S>(
&mut self,
module: &Module<BE>,
#[cfg(test)]
impl<T: UnsignedInteger + ToBits, BE: Backend> FheUintBlocksPreparedEncryptSk<T, BE> for Module<BE> where
Self: Sized + ModuleN + GGSWEncryptSk<BE> + GGSWPreparedFactory<BE>
{
}
#[cfg(test)]
pub trait FheUintBlocksPreparedEncryptSk<T: UnsignedInteger + ToBits, BE: Backend>
where
Self: Sized + ModuleN + GGSWEncryptSk<BE> + GGSWPreparedFactory<BE>,
{
fn fhe_uint_blocks_prepared_encrypt_sk<DM, S>(
&self,
res: &mut FheUintBlocksPrepared<DM, T, BE>,
value: T,
sk: &GLWESecretPrepared<S, BE>,
sk: &S,
source_xa: &mut Source,
source_xe: &mut Source,
scratch: &mut Scratch<BE>,
) where
S: DataRef,
Module<BE>: VecZnxAddScalarInplace
+ VecZnxDftBytesOf
+ VecZnxBigNormalize<BE>
+ VecZnxDftApply<BE>
+ SvpApplyDftToDftInplace<BE>
+ VecZnxIdftApplyConsume<BE>
+ VecZnxNormalizeTmpBytes
+ VecZnxFillUniform
+ VecZnxSubInplace
+ VecZnxAddInplace
+ VecZnxNormalizeInplace<BE>
+ VecZnxAddNormal
+ VecZnxNormalize<BE>
+ VecZnxSub
+ VmpPrepare<BE>,
DM: DataMut,
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);
}
use poulpy_hal::api::ScratchTakeBasic;
let (mut tmp_ggsw, scratch_1) = scratch.take_ggsw(self);
let (mut pt, scratch_2) = scratch_1.take_scalar_znx(module.n(), 1);
assert!(self.n().is_multiple_of(T::WORD_SIZE));
assert_eq!(res.n(), self.n() as u32);
assert_eq!(sk.n(), self.n() as u32);
let (mut tmp_ggsw, scratch_1) = scratch.take_ggsw(res);
let (mut pt, scratch_2) = scratch_1.take_scalar_znx(self.n(), 1);
for i in 0..T::WORD_SIZE {
use poulpy_hal::layouts::ZnxViewMut;
pt.at_mut(0, 0)[0] = value.bit(i) as i64;
tmp_ggsw.encrypt_sk(&module, &pt, sk, source_xa, source_xe, scratch_2);
self.blocks[i].prepare(module, &tmp_ggsw, scratch_2);
tmp_ggsw.encrypt_sk(self, &pt, sk, source_xa, source_xe, scratch_2);
res.blocks[i].prepare(self, &tmp_ggsw, scratch_2);
}
}
/// Prepares [FheUintBits] to [FheUintBitsPrep].
pub fn prepare<BIT, KEY>(&mut self, module: &Module<BE>, bits: &FheUintBlocks<BIT, T>, key: &KEY, scratch: &mut Scratch<BE>)
where
BIT: DataRef,
KEY: FheUintPrepare<BE, FheUintBlocksPrep<D, BE, T>, FheUintBlocks<BIT, T>>,
{
key.prepare(module, self, bits, scratch);
}
}
#[cfg(test)]
impl<D: DataMut, T: UnsignedInteger + ToBits> FheUintBlocksPrepDebug<D, T> {
pub(crate) fn prepare<BIT, KEY, BE: Backend>(
impl<D: DataMut, T: UnsignedInteger + ToBits, BE: Backend> FheUintBlocksPrepared<D, T, BE> {
pub(crate) fn encrypt_sk<M, S>(
&mut self,
module: &Module<BE>,
bits: &FheUintBlocks<BIT, T>,
key: &KEY,
module: &M,
value: T,
sk: &S,
source_xa: &mut Source,
source_xe: &mut Source,
scratch: &mut Scratch<BE>,
) where
BIT: DataRef,
KEY: FheUintPrepare<BE, FheUintBlocksPrepDebug<D, T>, FheUintBlocks<BIT, T>>,
S: GLWESecretPreparedToRef<BE> + GLWEInfos,
M: FheUintBlocksPreparedEncryptSk<T, BE>,
Scratch<BE>: ScratchTakeCore<BE>,
{
key.prepare(module, self, bits, scratch);
module.fhe_uint_blocks_prepared_encrypt_sk(self, value, sk, source_xa, source_xe, scratch);
}
}
#[cfg(test)]
impl<D: DataRef, T: UnsignedInteger + ToBits> FheUintBlocksPrepDebug<D, T> {
#[allow(dead_code)]
pub(crate) fn noise<S: DataRef, BE: Backend>(&self, module: &Module<BE>, sk: &GLWESecretPrepared<S, BE>, want: T)
where
Module<BE>: VecZnxDftBytesOf
+ VecZnxBigBytesOf
+ VecZnxDftApply<BE>
+ SvpApplyDftToDftInplace<BE>
+ VecZnxIdftApplyConsume<BE>
+ VecZnxBigAddInplace<BE>
+ VecZnxBigAddSmallInplace<BE>
+ VecZnxBigNormalize<BE>
+ VecZnxNormalizeTmpBytes
+ VecZnxBigAlloc<BE>
+ VecZnxDftAlloc<BE>
+ VecZnxBigNormalizeTmpBytes
+ VecZnxIdftApplyTmpA<BE>
+ VecZnxAddScalarInplace
+ VecZnxSubInplace,
{
for (i, ggsw) in self.blocks.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;
ggsw.print_noise(module, sk, &pt_want);
}
}
}
impl<D: DataRef, T: UnsignedInteger, B: Backend> LWEInfos for FheUintBlocksPrep<D, B, T> {
impl<D: DataRef, T: UnsignedInteger, B: Backend> LWEInfos for FheUintBlocksPrepared<D, T, B> {
fn base2k(&self) -> poulpy_core::layouts::Base2K {
self.blocks[0].base2k()
}
@@ -227,46 +159,13 @@ impl<D: DataRef, T: UnsignedInteger, B: Backend> LWEInfos for FheUintBlocksPrep<
}
}
impl<D: DataRef, T: UnsignedInteger, B: Backend> GLWEInfos for FheUintBlocksPrep<D, B, T> {
impl<D: DataRef, T: UnsignedInteger, B: Backend> GLWEInfos for FheUintBlocksPrepared<D, T, B> {
fn rank(&self) -> poulpy_core::layouts::Rank {
self.blocks[0].rank()
}
}
impl<D: DataRef, T: UnsignedInteger, B: Backend> GGSWInfos for FheUintBlocksPrep<D, B, T> {
fn dsize(&self) -> poulpy_core::layouts::Dsize {
self.blocks[0].dsize()
}
fn dnum(&self) -> poulpy_core::layouts::Dnum {
self.blocks[0].dnum()
}
}
#[cfg(test)]
impl<D: DataRef, T: UnsignedInteger> LWEInfos for FheUintBlocksPrepDebug<D, T> {
fn base2k(&self) -> poulpy_core::layouts::Base2K {
self.blocks[0].base2k()
}
fn k(&self) -> poulpy_core::layouts::TorusPrecision {
self.blocks[0].k()
}
fn n(&self) -> poulpy_core::layouts::Degree {
self.blocks[0].n()
}
}
#[cfg(test)]
impl<D: DataRef, T: UnsignedInteger> GLWEInfos for FheUintBlocksPrepDebug<D, T> {
fn rank(&self) -> poulpy_core::layouts::Rank {
self.blocks[0].rank()
}
}
#[cfg(test)]
impl<D: DataRef, T: UnsignedInteger> GGSWInfos for FheUintBlocksPrepDebug<D, T> {
impl<D: DataRef, T: UnsignedInteger, B: Backend> GGSWInfos for FheUintBlocksPrepared<D, T, B> {
fn dsize(&self) -> poulpy_core::layouts::Dsize {
self.blocks[0].dsize()
}

View File

@@ -2,6 +2,11 @@ mod block;
mod block_prepared;
mod word;
#[cfg(test)]
mod block_debug;
#[cfg(test)]
pub(crate) use block_debug::*;
pub use block::*;
pub use block_prepared::*;
pub use word::*;

View File

@@ -1,20 +1,14 @@
use itertools::Itertools;
use poulpy_core::{
GLWECopy, GLWEDecrypt, GLWEEncryptSk, GLWEPacking, ScratchTakeCore,
layouts::{
prepared::{GLWEAutomorphismKeyPrepared, GLWESecretPrepared}, GLWEInfos, GLWEPlaintextLayout, LWEInfos, TorusPrecision, GLWE
}, ScratchTakeCore,
GLWE, GLWEInfos, GLWEPlaintextLayout, GLWESecretPreparedToRef, LWEInfos, TorusPrecision,
prepared::GLWEAutomorphismKeyPrepared,
},
};
use poulpy_hal::{
api::{
ScratchAvailable, SvpApplyDftToDftInplace, VecZnxAddInplace, VecZnxAddNormal,
VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace,
VecZnxBigAutomorphismInplace, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxBigSubSmallNegateInplace, VecZnxCopy,
VecZnxDftApply, VecZnxDftBytesOf, VecZnxDftCopy, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA,
VecZnxNegateInplace, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotate, VecZnxRotateInplace,
VecZnxRshInplace, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd,
VmpApplyDftToDftTmpBytes,
},
layouts::{Backend, Data, DataMut, DataRef, Module, Scratch},
api::ModuleN,
layouts::{Backend, Data, DataMut, DataRef, Scratch},
source::Source,
};
use std::{collections::HashMap, marker::PhantomData};
@@ -26,40 +20,15 @@ pub struct FheUintWord<D: Data, T: UnsignedInteger>(pub(crate) GLWE<D>, pub(crat
impl<D: DataMut, T: UnsignedInteger> FheUintWord<D, T> {
#[allow(dead_code)]
fn post_process<ATK, BE: Backend>(
fn post_process<ATK, M, BE: Backend>(
&mut self,
module: &Module<BE>,
module: &M,
mut tmp_res: Vec<GLWE<&mut [u8]>>,
auto_keys: &HashMap<i64, GLWEAutomorphismKeyPrepared<ATK, BE>>,
scratch: &mut Scratch<BE>,
) where
ATK: DataRef,
Module<BE>: VecZnxSub
+ VecZnxCopy
+ VecZnxNegateInplace
+ VecZnxDftBytesOf
+ VecZnxAddInplace
+ VmpApplyDftToDftTmpBytes
+ VecZnxNormalizeTmpBytes
+ VecZnxDftApply<BE>
+ VmpApplyDftToDft<BE>
+ VmpApplyDftToDftAdd<BE>
+ VecZnxIdftApplyConsume<BE>
+ VecZnxBigNormalize<BE>
+ VecZnxNormalize<BE>
+ VecZnxRotateInplace<BE>
+ VecZnxNormalizeInplace<BE>
+ VecZnxSwitchRing
+ VecZnxBigAutomorphismInplace<BE>
+ VecZnxRshInplace<BE>
+ VecZnxDftCopy<BE>
+ VecZnxIdftApplyTmpA<BE>
+ VecZnxSubInplace
+ VecZnxBigNormalizeTmpBytes
+ VecZnxBigAddSmallInplace<BE>
+ VecZnxAutomorphismInplace<BE>
+ VecZnxBigSubSmallNegateInplace<BE>
+ VecZnxRotate,
M: GLWEPacking<BE> + GLWECopy,
Scratch<BE>: ScratchTakeCore<BE>,
{
// Repacks the GLWE ciphertexts bits
@@ -69,10 +38,11 @@ impl<D: DataMut, T: UnsignedInteger> FheUintWord<D, T> {
for (i, ct) in tmp_res.iter_mut().enumerate().take(T::WORD_SIZE) {
cts.insert(i * gap, ct);
}
glwe_packing(module, &mut cts, log_gap, auto_keys, scratch);
module.glwe_pack(&mut cts, log_gap, auto_keys, scratch);
// And copies the repacked ciphertext on the receiver.
self.0.copy(module, cts.remove(&0).unwrap())
module.glwe_copy(&mut self.0, cts.remove(&0).unwrap());
}
}
@@ -97,29 +67,17 @@ impl<D: DataRef, T: UnsignedInteger> GLWEInfos for FheUintWord<D, T> {
}
impl<D: DataMut, T: UnsignedInteger + ToBits> FheUintWord<D, T> {
pub fn encrypt_sk<S: DataRef, BE: Backend>(
pub fn encrypt_sk<S, M, BE: Backend>(
&mut self,
module: &Module<BE>,
module: &M,
data: T,
sk: &GLWESecretPrepared<S, BE>,
sk: &S,
source_xa: &mut Source,
source_xe: &mut Source,
scratch: &mut Scratch<BE>,
) where
Module<BE>: VecZnxAddScalarInplace
+ VecZnxDftBytesOf
+ VecZnxBigNormalize<BE>
+ VecZnxDftApply<BE>
+ SvpApplyDftToDftInplace<BE>
+ VecZnxIdftApplyConsume<BE>
+ VecZnxNormalizeTmpBytes
+ VecZnxFillUniform
+ VecZnxSubInplace
+ VecZnxAddInplace
+ VecZnxNormalizeInplace<BE>
+ VecZnxAddNormal
+ VecZnxNormalize<BE>
+ VecZnxSub,
S: GLWESecretPreparedToRef<BE> + GLWEInfos,
M: ModuleN + GLWEEncryptSk<BE>,
Scratch<BE>: ScratchTakeCore<BE>,
{
#[cfg(debug_assertions)]
@@ -143,7 +101,7 @@ impl<D: DataMut, T: UnsignedInteger + ToBits> FheUintWord<D, T> {
k: 1_usize.into(),
};
let (mut pt, scratch_1) = scratch.take_glwe_pt(&pt_infos);
let (mut pt, scratch_1) = scratch.take_glwe_plaintext(&pt_infos);
pt.encode_vec_i64(&data_bits, TorusPrecision(1));
self.0
@@ -152,19 +110,10 @@ impl<D: DataMut, T: UnsignedInteger + ToBits> FheUintWord<D, T> {
}
impl<D: DataRef, T: UnsignedInteger + FromBits> FheUintWord<D, T> {
pub fn decrypt<S: DataRef, BE: Backend>(
&self,
module: &Module<BE>,
sk: &GLWESecretPrepared<S, BE>,
scratch: &mut Scratch<BE>,
) -> T
pub fn decrypt<S, M, BE: Backend>(&self, module: &M, sk: &S, scratch: &mut Scratch<BE>) -> T
where
Module<BE>: VecZnxDftApply<BE>
+ SvpApplyDftToDftInplace<BE>
+ VecZnxIdftApplyConsume<BE>
+ VecZnxBigAddInplace<BE>
+ VecZnxBigAddSmallInplace<BE>
+ VecZnxBigNormalize<BE>,
S: GLWESecretPreparedToRef<BE> + GLWEInfos,
M: GLWEDecrypt<BE>,
Scratch<BE>: ScratchTakeCore<BE>,
{
#[cfg(debug_assertions)]
@@ -182,7 +131,7 @@ impl<D: DataRef, T: UnsignedInteger + FromBits> FheUintWord<D, T> {
k: 1_usize.into(),
};
let (mut pt, scratch_1) = scratch.take_glwe_pt(&pt_infos);
let (mut pt, scratch_1) = scratch.take_glwe_plaintext(&pt_infos);
self.0.decrypt(module, &mut pt, sk, scratch_1);