Add tests to BDD

This commit is contained in:
Pro7ech
2025-10-23 10:11:12 +02:00
parent 9067de8d96
commit 06795e9547
25 changed files with 1080 additions and 116 deletions

View File

@@ -5,12 +5,9 @@ use poulpy_core::{
layouts::{Base2K, GLWE, GLWEInfos, GLWEPlaintextLayout, GLWESecretPreparedToRef, LWEInfos, Rank, TorusPrecision},
};
#[cfg(test)]
use poulpy_core::GLWEEncryptSk;
use poulpy_core::ScratchTakeCore;
use poulpy_hal::layouts::{Backend, Data, DataMut, DataRef, Module, Scratch, ZnxZero};
#[cfg(test)]
#[cfg(test)]
use poulpy_hal::source::Source;
use crate::tfhe::bdd_arithmetic::{FromBits, ToBits, UnsignedInteger};
@@ -43,16 +40,14 @@ impl<D: DataRef, T: UnsignedInteger> GLWEInfos for FheUintBlocks<D, T> {
}
impl<T: UnsignedInteger> FheUintBlocks<Vec<u8>, T> {
#[allow(dead_code)]
pub(crate) fn alloc<A, BE: Backend>(module: &Module<BE>, infos: &A) -> Self
pub fn alloc_from_infos<A, BE: Backend>(module: &Module<BE>, infos: &A) -> Self
where
A: GLWEInfos,
{
Self::alloc_with(module, infos.base2k(), infos.k(), infos.rank())
Self::alloc(module, infos.base2k(), infos.k(), infos.rank())
}
#[allow(dead_code)]
pub(crate) fn alloc_with<BE: Backend>(module: &Module<BE>, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self {
pub fn alloc<BE: Backend>(module: &Module<BE>, base2k: Base2K, k: TorusPrecision, rank: Rank) -> Self {
Self {
blocks: (0..T::WORD_SIZE)
.map(|_| GLWE::alloc(module.n().into(), base2k, k, rank))
@@ -64,9 +59,7 @@ impl<T: UnsignedInteger> FheUintBlocks<Vec<u8>, T> {
}
impl<D: DataMut, T: UnsignedInteger + ToBits> FheUintBlocks<D, T> {
#[allow(dead_code)]
#[cfg(test)]
pub(crate) fn encrypt_sk<S, BE: Backend>(
pub fn encrypt_sk<S, BE: Backend>(
&mut self,
module: &Module<BE>,
value: T,

View File

@@ -7,32 +7,30 @@ use crate::tfhe::{
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 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
pub fn alloc_from_infos<A, M>(module: &M, infos: &A) -> Self
where
M: ModuleN,
A: GGSWInfos,
{
Self::alloc_with(
Self::alloc(
module,
infos.base2k(),
infos.k(),
@@ -42,8 +40,7 @@ impl<T: UnsignedInteger> FheUintBlocksPreparedDebug<Vec<u8>, T> {
)
}
#[allow(dead_code)]
pub(crate) fn alloc_with<M>(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> Self
pub fn alloc<M>(module: &M, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> Self
where
M: ModuleN,
{
@@ -88,8 +85,7 @@ impl<D: DataRef, T: UnsignedInteger> GGSWInfos for FheUintBlocksPreparedDebug<D,
}
impl<D: DataRef, T: UnsignedInteger + ToBits> FheUintBlocksPreparedDebug<D, T> {
#[allow(dead_code)]
pub(crate) fn noise<S, M, BE: Backend>(&self, module: &M, sk: &S, want: T)
pub fn print_noise<S, M, BE: Backend>(&self, module: &M, sk: &S, want: T)
where
S: GLWESecretPreparedToRef<BE>,
M: GGSWNoise<BE>,
@@ -101,6 +97,20 @@ impl<D: DataRef, T: UnsignedInteger + ToBits> FheUintBlocksPreparedDebug<D, T> {
ggsw.print_noise(module, sk, &pt_want);
}
}
pub fn assert_noise<S, M, F, BE: Backend>(&self, module: &M, sk: &S, want: T, max_noise: &F)
where
S: GLWESecretPreparedToRef<BE>,
M: GGSWNoise<BE>,
F: Fn(usize) -> f64,
{
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.assert_noise(module, sk, &pt_want, max_noise);
}
}
}
impl<BRA: BlindRotationAlgo, BE: Backend, T: UnsignedInteger> FheUintBlockDebugPrepare<BRA, T, BE> for Module<BE>
@@ -130,7 +140,6 @@ where
}
impl<D: DataMut, T: UnsignedInteger> FheUintBlocksPreparedDebug<D, T> {
#[allow(dead_code)]
pub fn prepare<BRA, M, O, K, BE: Backend>(
&mut self,
module: &M,

View File

@@ -3,17 +3,16 @@ use std::marker::PhantomData;
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::ModuleN,
layouts::{DataMut, Scratch},
source::Source,
};
#[cfg(test)]
use crate::tfhe::bdd_arithmetic::ToBits;
use crate::tfhe::bdd_arithmetic::UnsignedInteger;
@@ -83,13 +82,11 @@ impl<T: UnsignedInteger, BE: Backend> FheUintBlocksPrepared<Vec<u8>, T, 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>,
@@ -126,7 +123,6 @@ where
}
}
#[cfg(test)]
impl<D: DataMut, T: UnsignedInteger + ToBits, BE: Backend> FheUintBlocksPrepared<D, T, BE> {
pub(crate) fn encrypt_sk<M, S>(
&mut self,

View File

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