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

@@ -3,72 +3,44 @@ use poulpy_core::layouts::prepared::GGSWPreparedToRef;
use poulpy_hal::layouts::{Backend, DataMut, DataRef, Module, Scratch};
use crate::tfhe::bdd_arithmetic::{
BitCircuitInfo, Circuit, CircuitExecute, FheUintBlocks, FheUintBlocksPrep, UnsignedInteger, circuits,
ExecuteBDDCircuit, FheUintBlocks, FheUintBlocksPrepared, GetBitCircuitInfo, UnsignedInteger, circuits,
};
/// Operations Z x Z -> Z
pub(crate) struct Circuits2WTo1W<C: BitCircuitInfo + 'static, const WORD_SIZE: usize>(pub &'static Circuit<C, WORD_SIZE>);
impl<T: UnsignedInteger, BE: Backend> ExecuteBDDCircuit2WTo1W<T, BE> for Module<BE> where Self: Sized + ExecuteBDDCircuit<T, BE> {}
pub trait EvalBDD2WTo1W<BE: Backend, T: UnsignedInteger> {
fn eval_bdd_2w_to_1w<R, A, B>(
&self,
module: &Module<BE>,
out: &mut FheUintBlocks<R, T>,
a: &FheUintBlocksPrep<A, BE, T>,
b: &FheUintBlocksPrep<B, BE, T>,
scratch: &mut Scratch<BE>,
) where
R: DataMut,
A: DataRef,
B: DataRef;
}
impl<C: BitCircuitInfo + 'static, const WORD_SIZE: usize, BE: Backend, T: UnsignedInteger> EvalBDD2WTo1W<BE, T>
for Circuits2WTo1W<C, WORD_SIZE>
pub trait ExecuteBDDCircuit2WTo1W<T: UnsignedInteger, BE: Backend>
where
Circuit<C, WORD_SIZE>: CircuitExecute<BE, T>,
Self: Sized + ExecuteBDDCircuit<T, BE>,
{
fn eval_bdd_2w_to_1w<R, A, B>(
/// Operations Z x Z -> Z
fn execute_bdd_circuit_2w_to_1w<R, C, A, B>(
&self,
module: &Module<BE>,
out: &mut FheUintBlocks<R, T>,
a: &FheUintBlocksPrep<A, BE, T>,
b: &FheUintBlocksPrep<B, BE, T>,
circuit: &C,
a: &FheUintBlocksPrepared<A, T, BE>,
b: &FheUintBlocksPrepared<B, T, BE>,
scratch: &mut Scratch<BE>,
) where
C: GetBitCircuitInfo<T>,
R: DataMut,
A: DataRef,
B: DataRef,
{
eval_bdd_2w_to_1w(module, self.0, out, a, b, scratch);
}
}
pub fn eval_bdd_2w_to_1w<R: DataMut, A: DataRef, B: DataRef, T: UnsignedInteger, C: CircuitExecute<BE, T>, BE: Backend>(
module: &Module<BE>,
circuit: &C,
out: &mut FheUintBlocks<R, T>,
a: &FheUintBlocksPrep<A, BE, T>,
b: &FheUintBlocksPrep<B, BE, T>,
scratch: &mut Scratch<BE>,
) {
#[cfg(debug_assertions)]
{
assert_eq!(out.blocks.len(), T::WORD_SIZE);
assert_eq!(b.blocks.len(), T::WORD_SIZE);
assert_eq!(b.blocks.len(), T::WORD_SIZE);
// Collects inputs into a single array
let inputs: Vec<&dyn GGSWPreparedToRef<BE>> = a
.blocks
.iter()
.map(|x| x as &dyn GGSWPreparedToRef<BE>)
.chain(b.blocks.iter().map(|x| x as &dyn GGSWPreparedToRef<BE>))
.collect_vec();
// Evaluates out[i] = circuit[i](a, b)
self.execute_bdd_circuit(&mut out.blocks, &inputs, circuit, scratch);
}
// Collects inputs into a single array
let inputs: Vec<&dyn GGSWPreparedToRef<BE>> = a
.blocks
.iter()
.map(|x| x as &dyn GGSWPreparedToRef<BE>)
.chain(b.blocks.iter().map(|x| x as &dyn GGSWPreparedToRef<BE>))
.collect_vec();
// Evaluates out[i] = circuit[i](a, b)
circuit.execute(module, &mut out.blocks, &inputs, scratch);
}
#[macro_export]
@@ -76,13 +48,14 @@ macro_rules! define_bdd_2w_to_1w_trait {
($(#[$meta:meta])* $vis:vis $trait_name:ident, $method_name:ident) => {
$(#[$meta])*
$vis trait $trait_name<T: UnsignedInteger, BE: Backend> {
fn $method_name<A, B>(
fn $method_name<A, M, B>(
&mut self,
module: &Module<BE>,
a: &FheUintBlocksPrep<A, BE, T>,
b: &FheUintBlocksPrep<B, BE, T>,
module: &M,
a: &FheUintBlocksPrepared<A, T, BE>,
b: &FheUintBlocksPrepared<B, T, BE>,
scratch: &mut Scratch<BE>,
) where
M: ExecuteBDDCircuit2WTo1W<T, BE>,
A: DataRef,
B: DataRef;
}
@@ -92,23 +65,19 @@ macro_rules! define_bdd_2w_to_1w_trait {
#[macro_export]
macro_rules! impl_bdd_2w_to_1w_trait {
($trait_name:ident, $method_name:ident, $ty:ty, $n:literal, $circuit_ty:ty, $output_circuits:path) => {
impl<D: DataMut, BE: Backend> $trait_name<$ty, BE> for FheUintBlocks<D, $ty>
where
Circuits2WTo1W<$circuit_ty, $n>: EvalBDD2WTo1W<BE, $ty>,
{
fn $method_name<A, B>(
impl<D: DataMut, BE: Backend> $trait_name<$ty, BE> for FheUintBlocks<D, $ty> {
fn $method_name<A, M, B>(
&mut self,
module: &Module<BE>,
a: &FheUintBlocksPrep<A, BE, $ty>,
b: &FheUintBlocksPrep<B, BE, $ty>,
module: &M,
a: &FheUintBlocksPrepared<A, $ty, BE>,
b: &FheUintBlocksPrepared<B, $ty, BE>,
scratch: &mut Scratch<BE>,
) where
M: ExecuteBDDCircuit2WTo1W<$ty, BE>,
A: DataRef,
B: DataRef,
{
const OP: Circuits2WTo1W<$circuit_ty, $n> = Circuits2WTo1W::<$circuit_ty, $n>(&$output_circuits);
OP.eval_bdd_2w_to_1w(module, self, a, b, scratch);
module.execute_bdd_circuit_2w_to_1w(self, &$output_circuits, a, b, scratch)
}
}
};