mirror of
https://github.com/arnaucube/poulpy.git
synced 2026-02-10 13:16:44 +01:00
Add BDD Arithmetic (#98)
* Added some circuit, evaluation + some layouts * Refactor + memory reduction * Rows -> Dnum, Digits -> Dsize * fix #96 + glwe_packing (indirectly CBT) * clippy
This commit is contained in:
committed by
GitHub
parent
37e13b965c
commit
6357a05509
220
poulpy-schemes/src/tfhe/bdd_arithmetic/bdd_2w_to_1w.rs
Normal file
220
poulpy-schemes/src/tfhe/bdd_arithmetic/bdd_2w_to_1w.rs
Normal file
@@ -0,0 +1,220 @@
|
||||
use itertools::Itertools;
|
||||
use poulpy_core::layouts::prepared::GGSWCiphertextPreparedToRef;
|
||||
use poulpy_hal::layouts::{Backend, DataMut, DataRef, Module, Scratch};
|
||||
|
||||
use crate::tfhe::bdd_arithmetic::{
|
||||
BitCircuitInfo, Circuit, CircuitExecute, FheUintBlocks, FheUintBlocksPrep, UnsignedInteger, circuits,
|
||||
};
|
||||
|
||||
/// Operations Z x Z -> Z
|
||||
pub(crate) struct Circuits2WTo1W<C: BitCircuitInfo + 'static, const WORD_SIZE: usize>(pub &'static Circuit<C, WORD_SIZE>);
|
||||
|
||||
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>
|
||||
where
|
||||
Circuit<C, WORD_SIZE>: CircuitExecute<BE, T>,
|
||||
{
|
||||
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,
|
||||
{
|
||||
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 GGSWCiphertextPreparedToRef<BE>> = a
|
||||
.blocks
|
||||
.iter()
|
||||
.map(|x| x as &dyn GGSWCiphertextPreparedToRef<BE>)
|
||||
.chain(
|
||||
b.blocks
|
||||
.iter()
|
||||
.map(|x| x as &dyn GGSWCiphertextPreparedToRef<BE>),
|
||||
)
|
||||
.collect_vec();
|
||||
|
||||
// Evaluates out[i] = circuit[i](a, b)
|
||||
circuit.execute(module, &mut out.blocks, &inputs, scratch);
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
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>(
|
||||
&mut self,
|
||||
module: &Module<BE>,
|
||||
a: &FheUintBlocksPrep<A, BE, T>,
|
||||
b: &FheUintBlocksPrep<B, BE, T>,
|
||||
scratch: &mut Scratch<BE>,
|
||||
) where
|
||||
A: DataRef,
|
||||
B: DataRef;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[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>(
|
||||
&mut self,
|
||||
module: &Module<BE>,
|
||||
a: &FheUintBlocksPrep<A, BE, $ty>,
|
||||
b: &FheUintBlocksPrep<B, BE, $ty>,
|
||||
scratch: &mut Scratch<BE>,
|
||||
) where
|
||||
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);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
define_bdd_2w_to_1w_trait!(pub Add, add);
|
||||
define_bdd_2w_to_1w_trait!(pub Sub, sub);
|
||||
define_bdd_2w_to_1w_trait!(pub Sll, sll);
|
||||
define_bdd_2w_to_1w_trait!(pub Sra, sra);
|
||||
define_bdd_2w_to_1w_trait!(pub Srl, srl);
|
||||
define_bdd_2w_to_1w_trait!(pub Slt, slt);
|
||||
define_bdd_2w_to_1w_trait!(pub Sltu, sltu);
|
||||
define_bdd_2w_to_1w_trait!(pub Or, or);
|
||||
define_bdd_2w_to_1w_trait!(pub And, and);
|
||||
define_bdd_2w_to_1w_trait!(pub Xor, xor);
|
||||
|
||||
impl_bdd_2w_to_1w_trait!(
|
||||
Add,
|
||||
add,
|
||||
u32,
|
||||
32,
|
||||
circuits::u32::add_codegen::AnyBitCircuit,
|
||||
circuits::u32::add_codegen::OUTPUT_CIRCUITS
|
||||
);
|
||||
|
||||
impl_bdd_2w_to_1w_trait!(
|
||||
Sub,
|
||||
sub,
|
||||
u32,
|
||||
32,
|
||||
circuits::u32::sub_codegen::AnyBitCircuit,
|
||||
circuits::u32::sub_codegen::OUTPUT_CIRCUITS
|
||||
);
|
||||
|
||||
impl_bdd_2w_to_1w_trait!(
|
||||
Sll,
|
||||
sll,
|
||||
u32,
|
||||
32,
|
||||
circuits::u32::sll_codegen::AnyBitCircuit,
|
||||
circuits::u32::sll_codegen::OUTPUT_CIRCUITS
|
||||
);
|
||||
|
||||
impl_bdd_2w_to_1w_trait!(
|
||||
Sra,
|
||||
sra,
|
||||
u32,
|
||||
32,
|
||||
circuits::u32::sra_codegen::AnyBitCircuit,
|
||||
circuits::u32::sra_codegen::OUTPUT_CIRCUITS
|
||||
);
|
||||
|
||||
impl_bdd_2w_to_1w_trait!(
|
||||
Srl,
|
||||
srl,
|
||||
u32,
|
||||
32,
|
||||
circuits::u32::srl_codegen::AnyBitCircuit,
|
||||
circuits::u32::srl_codegen::OUTPUT_CIRCUITS
|
||||
);
|
||||
|
||||
impl_bdd_2w_to_1w_trait!(
|
||||
Slt,
|
||||
slt,
|
||||
u32,
|
||||
1,
|
||||
circuits::u32::slt_codegen::AnyBitCircuit,
|
||||
circuits::u32::slt_codegen::OUTPUT_CIRCUITS
|
||||
);
|
||||
|
||||
impl_bdd_2w_to_1w_trait!(
|
||||
Sltu,
|
||||
sltu,
|
||||
u32,
|
||||
1,
|
||||
circuits::u32::sltu_codegen::AnyBitCircuit,
|
||||
circuits::u32::sltu_codegen::OUTPUT_CIRCUITS
|
||||
);
|
||||
|
||||
impl_bdd_2w_to_1w_trait!(
|
||||
And,
|
||||
and,
|
||||
u32,
|
||||
32,
|
||||
circuits::u32::and_codegen::AnyBitCircuit,
|
||||
circuits::u32::and_codegen::OUTPUT_CIRCUITS
|
||||
);
|
||||
|
||||
impl_bdd_2w_to_1w_trait!(
|
||||
Or,
|
||||
or,
|
||||
u32,
|
||||
1,
|
||||
circuits::u32::or_codegen::AnyBitCircuit,
|
||||
circuits::u32::or_codegen::OUTPUT_CIRCUITS
|
||||
);
|
||||
|
||||
impl_bdd_2w_to_1w_trait!(
|
||||
Xor,
|
||||
xor,
|
||||
u32,
|
||||
1,
|
||||
circuits::u32::xor_codegen::AnyBitCircuit,
|
||||
circuits::u32::xor_codegen::OUTPUT_CIRCUITS
|
||||
);
|
||||
215
poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block.rs
Normal file
215
poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/block.rs
Normal file
@@ -0,0 +1,215 @@
|
||||
use std::marker::PhantomData;
|
||||
|
||||
use poulpy_core::layouts::{Base2K, GLWECiphertext, GLWEInfos, GLWEPlaintextLayout, LWEInfos, Rank, TorusPrecision};
|
||||
|
||||
use poulpy_core::{TakeGLWEPt, layouts::prepared::GLWESecretPrepared};
|
||||
use poulpy_hal::api::VecZnxBigAllocBytes;
|
||||
#[cfg(test)]
|
||||
use poulpy_hal::api::{
|
||||
ScratchAvailable, TakeVecZnx, VecZnxAddInplace, VecZnxAddNormal, VecZnxFillUniform, VecZnxNormalize, VecZnxSub,
|
||||
};
|
||||
#[cfg(test)]
|
||||
use poulpy_hal::source::Source;
|
||||
use poulpy_hal::{
|
||||
api::{
|
||||
TakeVecZnxBig, TakeVecZnxDft, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigNormalize, VecZnxDftAllocBytes,
|
||||
VecZnxDftApply, VecZnxIdftApplyConsume, VecZnxNormalizeTmpBytes,
|
||||
},
|
||||
layouts::{Backend, Data, DataMut, DataRef, Module, Scratch},
|
||||
};
|
||||
|
||||
use poulpy_hal::api::{SvpApplyDftToDftInplace, VecZnxNormalizeInplace, VecZnxSubInplace};
|
||||
|
||||
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<GLWECiphertext<D>>,
|
||||
pub(crate) _base: u8,
|
||||
pub(crate) _phantom: PhantomData<T>,
|
||||
}
|
||||
|
||||
impl<D: DataRef, T: UnsignedInteger> LWEInfos for FheUintBlocks<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 FheUintBlocks<D, T> {
|
||||
fn rank(&self) -> poulpy_core::layouts::Rank {
|
||||
self.blocks[0].rank()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: UnsignedInteger> FheUintBlocks<Vec<u8>, T> {
|
||||
#[allow(dead_code)]
|
||||
pub(crate) fn alloc<A, BE: Backend>(module: &Module<BE>, infos: &A) -> Self
|
||||
where
|
||||
A: GLWEInfos,
|
||||
{
|
||||
Self::alloc_with(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 {
|
||||
Self {
|
||||
blocks: (0..T::WORD_SIZE)
|
||||
.map(|_| GLWECiphertext::alloc_with(module.n().into(), base2k, k, rank))
|
||||
.collect(),
|
||||
_base: 1,
|
||||
_phantom: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: DataMut, T: UnsignedInteger + ToBits> FheUintBlocks<D, T> {
|
||||
#[allow(dead_code)]
|
||||
#[cfg(test)]
|
||||
pub(crate) fn encrypt_sk<S, BE: Backend>(
|
||||
&mut self,
|
||||
module: &Module<BE>,
|
||||
value: T,
|
||||
sk: &GLWESecretPrepared<S, BE>,
|
||||
source_xa: &mut Source,
|
||||
source_xe: &mut Source,
|
||||
scratch: &mut Scratch<BE>,
|
||||
) where
|
||||
S: DataRef,
|
||||
Module<BE>: VecZnxDftAllocBytes
|
||||
+ VecZnxBigNormalize<BE>
|
||||
+ VecZnxDftApply<BE>
|
||||
+ SvpApplyDftToDftInplace<BE>
|
||||
+ VecZnxIdftApplyConsume<BE>
|
||||
+ VecZnxNormalizeTmpBytes
|
||||
+ VecZnxFillUniform
|
||||
+ VecZnxSubInplace
|
||||
+ VecZnxAddInplace
|
||||
+ VecZnxNormalizeInplace<BE>
|
||||
+ VecZnxAddNormal
|
||||
+ VecZnxNormalize<BE>
|
||||
+ VecZnxSub,
|
||||
Scratch<BE>: TakeVecZnxDft<BE> + ScratchAvailable + TakeVecZnx + TakeGLWEPt<BE>,
|
||||
{
|
||||
use poulpy_core::layouts::GLWEPlaintextLayout;
|
||||
|
||||
#[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_pt(&pt_infos);
|
||||
|
||||
for i in 0..T::WORD_SIZE {
|
||||
pt.encode_coeff_i64(value.bit(i) as i64, TorusPrecision(1), 0);
|
||||
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
|
||||
where
|
||||
Module<BE>: VecZnxDftApply<BE>
|
||||
+ SvpApplyDftToDftInplace<BE>
|
||||
+ VecZnxIdftApplyConsume<BE>
|
||||
+ VecZnxBigAddInplace<BE>
|
||||
+ VecZnxBigAddSmallInplace<BE>
|
||||
+ VecZnxBigNormalize<BE>,
|
||||
Scratch<BE>: TakeVecZnxDft<BE> + TakeVecZnxBig<BE> + TakeGLWEPt<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_pt(&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.blocks[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: DataRef, BE: Backend>(
|
||||
&self,
|
||||
module: &Module<BE>,
|
||||
sk: &GLWESecretPrepared<S, BE>,
|
||||
want: T,
|
||||
scratch: &mut Scratch<BE>,
|
||||
) -> Vec<f64>
|
||||
where
|
||||
Module<BE>: VecZnxDftAllocBytes
|
||||
+ VecZnxBigAllocBytes
|
||||
+ VecZnxDftApply<BE>
|
||||
+ SvpApplyDftToDftInplace<BE>
|
||||
+ VecZnxIdftApplyConsume<BE>
|
||||
+ VecZnxBigAddInplace<BE>
|
||||
+ VecZnxBigAddSmallInplace<BE>
|
||||
+ VecZnxBigNormalize<BE>
|
||||
+ VecZnxNormalizeTmpBytes
|
||||
+ VecZnxSubInplace
|
||||
+ VecZnxNormalizeInplace<BE>,
|
||||
Scratch<BE>: TakeGLWEPt<BE> + TakeVecZnxDft<BE> + TakeVecZnxBig<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_pt(&pt_infos);
|
||||
|
||||
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.blocks[i].noise(module, sk, &pt_want, scratch_1);
|
||||
}
|
||||
|
||||
noise
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,282 @@
|
||||
use std::marker::PhantomData;
|
||||
|
||||
use poulpy_core::layouts::{
|
||||
Base2K, Dnum, Dsize, GGSWInfos, GLWEInfos, LWEInfos, Rank, TorusPrecision, prepared::GGSWCiphertextPrepared,
|
||||
};
|
||||
#[cfg(test)]
|
||||
use poulpy_core::{
|
||||
TakeGGSW,
|
||||
layouts::{GGSWCiphertext, prepared::GLWESecretPrepared},
|
||||
};
|
||||
use poulpy_hal::{
|
||||
api::VmpPMatAlloc,
|
||||
layouts::{Backend, Data, DataMut, DataRef, Module, Scratch},
|
||||
};
|
||||
#[cfg(test)]
|
||||
use poulpy_hal::{
|
||||
api::{
|
||||
ScratchAvailable, SvpApplyDftToDftInplace, TakeScalarZnx, TakeVecZnx, TakeVecZnxDft, VecZnxAddInplace, VecZnxAddNormal,
|
||||
VecZnxAddScalarInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAlloc, VecZnxBigAllocBytes,
|
||||
VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxDftAlloc, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxFillUniform,
|
||||
VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxSub,
|
||||
VecZnxSubInplace, VmpPrepare,
|
||||
},
|
||||
oep::{ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeVecZnxBigImpl, TakeVecZnxDftImpl},
|
||||
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<GGSWCiphertext<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(|_| GGSWCiphertext::alloc_with(module.n().into(), base2k, k, rank, dnum, dsize))
|
||||
.collect(),
|
||||
_base: 1,
|
||||
_phantom: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A prepared FHE ciphertext encrypting the bits of an [UnsignedInteger].
|
||||
pub struct FheUintBlocksPrep<D: Data, B: Backend, T: UnsignedInteger> {
|
||||
pub(crate) blocks: Vec<GGSWCiphertextPrepared<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>,
|
||||
{
|
||||
#[allow(dead_code)]
|
||||
pub(crate) fn alloc<A>(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(module: &Module<BE>, base2k: Base2K, k: TorusPrecision, dnum: Dnum, dsize: Dsize, rank: Rank) -> Self
|
||||
where
|
||||
Module<BE>: VmpPMatAlloc<BE>,
|
||||
{
|
||||
Self {
|
||||
blocks: (0..T::WORD_SIZE)
|
||||
.map(|_| GGSWCiphertextPrepared::alloc_with(module, base2k, k, dnum, dsize, rank))
|
||||
.collect(),
|
||||
_base: 1,
|
||||
_phantom: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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>,
|
||||
value: T,
|
||||
sk: &GLWESecretPrepared<S, BE>,
|
||||
source_xa: &mut Source,
|
||||
source_xe: &mut Source,
|
||||
scratch: &mut Scratch<BE>,
|
||||
) where
|
||||
S: DataRef,
|
||||
Module<BE>: VecZnxAddScalarInplace
|
||||
+ VecZnxDftAllocBytes
|
||||
+ VecZnxBigNormalize<BE>
|
||||
+ VecZnxDftApply<BE>
|
||||
+ SvpApplyDftToDftInplace<BE>
|
||||
+ VecZnxIdftApplyConsume<BE>
|
||||
+ VecZnxNormalizeTmpBytes
|
||||
+ VecZnxFillUniform
|
||||
+ VecZnxSubInplace
|
||||
+ VecZnxAddInplace
|
||||
+ VecZnxNormalizeInplace<BE>
|
||||
+ VecZnxAddNormal
|
||||
+ VecZnxNormalize<BE>
|
||||
+ VecZnxSub
|
||||
+ VmpPrepare<BE>,
|
||||
Scratch<BE>: TakeVecZnxDft<BE> + ScratchAvailable + TakeVecZnx + TakeGGSW + TakeScalarZnx,
|
||||
{
|
||||
#[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 (mut tmp_ggsw, scratch_1) = scratch.take_ggsw(self);
|
||||
let (mut pt, scratch_2) = scratch_1.take_scalar_znx(module.n(), 1);
|
||||
|
||||
for i in 0..T::WORD_SIZE {
|
||||
use poulpy_core::layouts::prepared::Prepare;
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
/// 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>(
|
||||
&mut self,
|
||||
module: &Module<BE>,
|
||||
bits: &FheUintBlocks<BIT, T>,
|
||||
key: &KEY,
|
||||
scratch: &mut Scratch<BE>,
|
||||
) where
|
||||
BIT: DataRef,
|
||||
KEY: FheUintPrepare<BE, FheUintBlocksPrepDebug<D, T>, FheUintBlocks<BIT, T>>,
|
||||
{
|
||||
key.prepare(module, self, bits, 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>: VecZnxDftAllocBytes
|
||||
+ VecZnxBigAllocBytes
|
||||
+ VecZnxDftApply<BE>
|
||||
+ SvpApplyDftToDftInplace<BE>
|
||||
+ VecZnxIdftApplyConsume<BE>
|
||||
+ VecZnxBigAddInplace<BE>
|
||||
+ VecZnxBigAddSmallInplace<BE>
|
||||
+ VecZnxBigNormalize<BE>
|
||||
+ VecZnxNormalizeTmpBytes
|
||||
+ VecZnxBigAlloc<BE>
|
||||
+ VecZnxDftAlloc<BE>
|
||||
+ VecZnxBigNormalizeTmpBytes
|
||||
+ VecZnxIdftApplyTmpA<BE>
|
||||
+ VecZnxAddScalarInplace
|
||||
+ VecZnxSubInplace,
|
||||
BE: Backend + TakeVecZnxDftImpl<BE> + TakeVecZnxBigImpl<BE> + ScratchOwnedAllocImpl<BE> + ScratchOwnedBorrowImpl<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<D: DataRef, T: UnsignedInteger, B: Backend> LWEInfos for FheUintBlocksPrep<D, B, 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, B: Backend> GLWEInfos for FheUintBlocksPrep<D, B, T> {
|
||||
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> {
|
||||
fn dsize(&self) -> poulpy_core::layouts::Dsize {
|
||||
self.blocks[0].dsize()
|
||||
}
|
||||
|
||||
fn dnum(&self) -> poulpy_core::layouts::Dnum {
|
||||
self.blocks[0].dnum()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
mod block;
|
||||
mod block_prepared;
|
||||
mod word;
|
||||
|
||||
pub use block::*;
|
||||
pub use block_prepared::*;
|
||||
pub use word::*;
|
||||
198
poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/word.rs
Normal file
198
poulpy-schemes/src/tfhe/bdd_arithmetic/ciphertexts/word.rs
Normal file
@@ -0,0 +1,198 @@
|
||||
use itertools::Itertools;
|
||||
use poulpy_core::{
|
||||
GLWEOperations, TakeGLWECtSlice, TakeGLWEPt, glwe_packing,
|
||||
layouts::{
|
||||
GLWECiphertext, GLWEInfos, GLWEPlaintextLayout, LWEInfos, TorusPrecision,
|
||||
prepared::{GGLWEAutomorphismKeyPrepared, GLWESecretPrepared},
|
||||
},
|
||||
};
|
||||
use poulpy_hal::{
|
||||
api::{
|
||||
ScratchAvailable, SvpApplyDftToDftInplace, TakeVecZnx, TakeVecZnxBig, TakeVecZnxDft, VecZnxAddInplace, VecZnxAddNormal,
|
||||
VecZnxAddScalarInplace, VecZnxAutomorphismInplace, VecZnxBigAddInplace, VecZnxBigAddSmallInplace,
|
||||
VecZnxBigAutomorphismInplace, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxBigSubSmallNegateInplace, VecZnxCopy,
|
||||
VecZnxDftAllocBytes, VecZnxDftApply, VecZnxDftCopy, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA,
|
||||
VecZnxNegateInplace, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotate, VecZnxRotateInplace,
|
||||
VecZnxRshInplace, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing, VmpApplyDftToDft, VmpApplyDftToDftAdd,
|
||||
VmpApplyDftToDftTmpBytes,
|
||||
},
|
||||
layouts::{Backend, Data, DataMut, DataRef, Module, Scratch},
|
||||
source::Source,
|
||||
};
|
||||
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) GLWECiphertext<D>, pub(crate) PhantomData<T>);
|
||||
|
||||
impl<D: DataMut, T: UnsignedInteger> FheUintWord<D, T> {
|
||||
#[allow(dead_code)]
|
||||
fn post_process<ATK, BE: Backend>(
|
||||
&mut self,
|
||||
module: &Module<BE>,
|
||||
mut tmp_res: Vec<GLWECiphertext<&mut [u8]>>,
|
||||
auto_keys: &HashMap<i64, GGLWEAutomorphismKeyPrepared<ATK, BE>>,
|
||||
scratch: &mut Scratch<BE>,
|
||||
) where
|
||||
ATK: DataRef,
|
||||
Module<BE>: VecZnxSub
|
||||
+ VecZnxCopy
|
||||
+ VecZnxNegateInplace
|
||||
+ VecZnxDftAllocBytes
|
||||
+ 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,
|
||||
Scratch<BE>: TakeVecZnxDft<BE> + ScratchAvailable + TakeVecZnx + TakeGLWECtSlice,
|
||||
{
|
||||
// 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 GLWECiphertext<&mut [u8]>> = HashMap::new();
|
||||
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);
|
||||
|
||||
// And copies the repacked ciphertext on the receiver.
|
||||
self.0.copy(module, cts.remove(&0).unwrap())
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: DataRef, T: UnsignedInteger> LWEInfos for FheUintWord<D, T> {
|
||||
fn base2k(&self) -> poulpy_core::layouts::Base2K {
|
||||
self.0.base2k()
|
||||
}
|
||||
|
||||
fn k(&self) -> poulpy_core::layouts::TorusPrecision {
|
||||
self.0.k()
|
||||
}
|
||||
|
||||
fn n(&self) -> poulpy_core::layouts::Degree {
|
||||
self.0.n()
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: DataRef, T: UnsignedInteger> GLWEInfos for FheUintWord<D, T> {
|
||||
fn rank(&self) -> poulpy_core::layouts::Rank {
|
||||
self.0.rank()
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: DataMut, T: UnsignedInteger + ToBits> FheUintWord<D, T> {
|
||||
pub fn encrypt_sk<S: DataRef, BE: Backend>(
|
||||
&mut self,
|
||||
module: &Module<BE>,
|
||||
data: T,
|
||||
sk: &GLWESecretPrepared<S, BE>,
|
||||
source_xa: &mut Source,
|
||||
source_xe: &mut Source,
|
||||
scratch: &mut Scratch<BE>,
|
||||
) where
|
||||
Module<BE>: VecZnxAddScalarInplace
|
||||
+ VecZnxDftAllocBytes
|
||||
+ VecZnxBigNormalize<BE>
|
||||
+ VecZnxDftApply<BE>
|
||||
+ SvpApplyDftToDftInplace<BE>
|
||||
+ VecZnxIdftApplyConsume<BE>
|
||||
+ VecZnxNormalizeTmpBytes
|
||||
+ VecZnxFillUniform
|
||||
+ VecZnxSubInplace
|
||||
+ VecZnxAddInplace
|
||||
+ VecZnxNormalizeInplace<BE>
|
||||
+ VecZnxAddNormal
|
||||
+ VecZnxNormalize<BE>
|
||||
+ VecZnxSub,
|
||||
Scratch<BE>: TakeVecZnxDft<BE> + ScratchAvailable + TakeVecZnx + TakeGLWEPt<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 gap: usize = module.n() / T::WORD_SIZE;
|
||||
|
||||
let mut data_bits: Vec<i64> = vec![0i64; module.n()];
|
||||
|
||||
for i in 0..T::WORD_SIZE {
|
||||
data_bits[i * gap] = data.bit(i) as i64
|
||||
}
|
||||
|
||||
let pt_infos = GLWEPlaintextLayout {
|
||||
n: self.n(),
|
||||
base2k: self.base2k(),
|
||||
k: 1_usize.into(),
|
||||
};
|
||||
|
||||
let (mut pt, scratch_1) = scratch.take_glwe_pt(&pt_infos);
|
||||
|
||||
pt.encode_vec_i64(&data_bits, TorusPrecision(1));
|
||||
self.0
|
||||
.encrypt_sk(module, &pt, sk, source_xa, source_xe, scratch_1);
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
where
|
||||
Module<BE>: VecZnxDftApply<BE>
|
||||
+ SvpApplyDftToDftInplace<BE>
|
||||
+ VecZnxIdftApplyConsume<BE>
|
||||
+ VecZnxBigAddInplace<BE>
|
||||
+ VecZnxBigAddSmallInplace<BE>
|
||||
+ VecZnxBigNormalize<BE>,
|
||||
Scratch<BE>: TakeVecZnxDft<BE> + TakeVecZnxBig<BE> + TakeGLWEPt<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 gap: usize = module.n() / T::WORD_SIZE;
|
||||
|
||||
let pt_infos = GLWEPlaintextLayout {
|
||||
n: self.n(),
|
||||
base2k: self.base2k(),
|
||||
k: 1_usize.into(),
|
||||
};
|
||||
|
||||
let (mut pt, scratch_1) = scratch.take_glwe_pt(&pt_infos);
|
||||
|
||||
self.0.decrypt(module, &mut pt, sk, scratch_1);
|
||||
|
||||
let mut data: Vec<i64> = vec![0i64; module.n()];
|
||||
|
||||
pt.decode_vec_i64(&mut data, TorusPrecision(1));
|
||||
|
||||
let bits: Vec<u8> = data.iter().step_by(gap).map(|c| *c as u8).collect_vec();
|
||||
T::from_bits(&bits)
|
||||
}
|
||||
}
|
||||
1
poulpy-schemes/src/tfhe/bdd_arithmetic/circuits/mod.rs
Normal file
1
poulpy-schemes/src/tfhe/bdd_arithmetic/circuits/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod u32;
|
||||
3020
poulpy-schemes/src/tfhe/bdd_arithmetic/circuits/u32/add_codegen.rs
Normal file
3020
poulpy-schemes/src/tfhe/bdd_arithmetic/circuits/u32/add_codegen.rs
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,465 @@
|
||||
use crate::tfhe::bdd_arithmetic::{BitCircuit, BitCircuitInfo, Circuit, GetBitCircuitInfo, Node};
|
||||
pub(crate) enum AnyBitCircuit {
|
||||
B0(BitCircuit<3, 2>),
|
||||
B1(BitCircuit<3, 2>),
|
||||
B2(BitCircuit<3, 2>),
|
||||
B3(BitCircuit<3, 2>),
|
||||
B4(BitCircuit<3, 2>),
|
||||
B5(BitCircuit<3, 2>),
|
||||
B6(BitCircuit<3, 2>),
|
||||
B7(BitCircuit<3, 2>),
|
||||
B8(BitCircuit<3, 2>),
|
||||
B9(BitCircuit<3, 2>),
|
||||
B10(BitCircuit<3, 2>),
|
||||
B11(BitCircuit<3, 2>),
|
||||
B12(BitCircuit<3, 2>),
|
||||
B13(BitCircuit<3, 2>),
|
||||
B14(BitCircuit<3, 2>),
|
||||
B15(BitCircuit<3, 2>),
|
||||
B16(BitCircuit<3, 2>),
|
||||
B17(BitCircuit<3, 2>),
|
||||
B18(BitCircuit<3, 2>),
|
||||
B19(BitCircuit<3, 2>),
|
||||
B20(BitCircuit<3, 2>),
|
||||
B21(BitCircuit<3, 2>),
|
||||
B22(BitCircuit<3, 2>),
|
||||
B23(BitCircuit<3, 2>),
|
||||
B24(BitCircuit<3, 2>),
|
||||
B25(BitCircuit<3, 2>),
|
||||
B26(BitCircuit<3, 2>),
|
||||
B27(BitCircuit<3, 2>),
|
||||
B28(BitCircuit<3, 2>),
|
||||
B29(BitCircuit<3, 2>),
|
||||
B30(BitCircuit<3, 2>),
|
||||
B31(BitCircuit<3, 2>),
|
||||
}
|
||||
|
||||
impl BitCircuitInfo for AnyBitCircuit {
|
||||
fn info(&self) -> (&[Node], &[usize], usize) {
|
||||
match self {
|
||||
AnyBitCircuit::B0(bit_circuit) => (
|
||||
bit_circuit.nodes.as_ref(),
|
||||
bit_circuit.levels.as_ref(),
|
||||
bit_circuit.max_inter_state,
|
||||
),
|
||||
AnyBitCircuit::B1(bit_circuit) => (
|
||||
bit_circuit.nodes.as_ref(),
|
||||
bit_circuit.levels.as_ref(),
|
||||
bit_circuit.max_inter_state,
|
||||
),
|
||||
AnyBitCircuit::B2(bit_circuit) => (
|
||||
bit_circuit.nodes.as_ref(),
|
||||
bit_circuit.levels.as_ref(),
|
||||
bit_circuit.max_inter_state,
|
||||
),
|
||||
AnyBitCircuit::B3(bit_circuit) => (
|
||||
bit_circuit.nodes.as_ref(),
|
||||
bit_circuit.levels.as_ref(),
|
||||
bit_circuit.max_inter_state,
|
||||
),
|
||||
AnyBitCircuit::B4(bit_circuit) => (
|
||||
bit_circuit.nodes.as_ref(),
|
||||
bit_circuit.levels.as_ref(),
|
||||
bit_circuit.max_inter_state,
|
||||
),
|
||||
AnyBitCircuit::B5(bit_circuit) => (
|
||||
bit_circuit.nodes.as_ref(),
|
||||
bit_circuit.levels.as_ref(),
|
||||
bit_circuit.max_inter_state,
|
||||
),
|
||||
AnyBitCircuit::B6(bit_circuit) => (
|
||||
bit_circuit.nodes.as_ref(),
|
||||
bit_circuit.levels.as_ref(),
|
||||
bit_circuit.max_inter_state,
|
||||
),
|
||||
AnyBitCircuit::B7(bit_circuit) => (
|
||||
bit_circuit.nodes.as_ref(),
|
||||
bit_circuit.levels.as_ref(),
|
||||
bit_circuit.max_inter_state,
|
||||
),
|
||||
AnyBitCircuit::B8(bit_circuit) => (
|
||||
bit_circuit.nodes.as_ref(),
|
||||
bit_circuit.levels.as_ref(),
|
||||
bit_circuit.max_inter_state,
|
||||
),
|
||||
AnyBitCircuit::B9(bit_circuit) => (
|
||||
bit_circuit.nodes.as_ref(),
|
||||
bit_circuit.levels.as_ref(),
|
||||
bit_circuit.max_inter_state,
|
||||
),
|
||||
AnyBitCircuit::B10(bit_circuit) => (
|
||||
bit_circuit.nodes.as_ref(),
|
||||
bit_circuit.levels.as_ref(),
|
||||
bit_circuit.max_inter_state,
|
||||
),
|
||||
AnyBitCircuit::B11(bit_circuit) => (
|
||||
bit_circuit.nodes.as_ref(),
|
||||
bit_circuit.levels.as_ref(),
|
||||
bit_circuit.max_inter_state,
|
||||
),
|
||||
AnyBitCircuit::B12(bit_circuit) => (
|
||||
bit_circuit.nodes.as_ref(),
|
||||
bit_circuit.levels.as_ref(),
|
||||
bit_circuit.max_inter_state,
|
||||
),
|
||||
AnyBitCircuit::B13(bit_circuit) => (
|
||||
bit_circuit.nodes.as_ref(),
|
||||
bit_circuit.levels.as_ref(),
|
||||
bit_circuit.max_inter_state,
|
||||
),
|
||||
AnyBitCircuit::B14(bit_circuit) => (
|
||||
bit_circuit.nodes.as_ref(),
|
||||
bit_circuit.levels.as_ref(),
|
||||
bit_circuit.max_inter_state,
|
||||
),
|
||||
AnyBitCircuit::B15(bit_circuit) => (
|
||||
bit_circuit.nodes.as_ref(),
|
||||
bit_circuit.levels.as_ref(),
|
||||
bit_circuit.max_inter_state,
|
||||
),
|
||||
AnyBitCircuit::B16(bit_circuit) => (
|
||||
bit_circuit.nodes.as_ref(),
|
||||
bit_circuit.levels.as_ref(),
|
||||
bit_circuit.max_inter_state,
|
||||
),
|
||||
AnyBitCircuit::B17(bit_circuit) => (
|
||||
bit_circuit.nodes.as_ref(),
|
||||
bit_circuit.levels.as_ref(),
|
||||
bit_circuit.max_inter_state,
|
||||
),
|
||||
AnyBitCircuit::B18(bit_circuit) => (
|
||||
bit_circuit.nodes.as_ref(),
|
||||
bit_circuit.levels.as_ref(),
|
||||
bit_circuit.max_inter_state,
|
||||
),
|
||||
AnyBitCircuit::B19(bit_circuit) => (
|
||||
bit_circuit.nodes.as_ref(),
|
||||
bit_circuit.levels.as_ref(),
|
||||
bit_circuit.max_inter_state,
|
||||
),
|
||||
AnyBitCircuit::B20(bit_circuit) => (
|
||||
bit_circuit.nodes.as_ref(),
|
||||
bit_circuit.levels.as_ref(),
|
||||
bit_circuit.max_inter_state,
|
||||
),
|
||||
AnyBitCircuit::B21(bit_circuit) => (
|
||||
bit_circuit.nodes.as_ref(),
|
||||
bit_circuit.levels.as_ref(),
|
||||
bit_circuit.max_inter_state,
|
||||
),
|
||||
AnyBitCircuit::B22(bit_circuit) => (
|
||||
bit_circuit.nodes.as_ref(),
|
||||
bit_circuit.levels.as_ref(),
|
||||
bit_circuit.max_inter_state,
|
||||
),
|
||||
AnyBitCircuit::B23(bit_circuit) => (
|
||||
bit_circuit.nodes.as_ref(),
|
||||
bit_circuit.levels.as_ref(),
|
||||
bit_circuit.max_inter_state,
|
||||
),
|
||||
AnyBitCircuit::B24(bit_circuit) => (
|
||||
bit_circuit.nodes.as_ref(),
|
||||
bit_circuit.levels.as_ref(),
|
||||
bit_circuit.max_inter_state,
|
||||
),
|
||||
AnyBitCircuit::B25(bit_circuit) => (
|
||||
bit_circuit.nodes.as_ref(),
|
||||
bit_circuit.levels.as_ref(),
|
||||
bit_circuit.max_inter_state,
|
||||
),
|
||||
AnyBitCircuit::B26(bit_circuit) => (
|
||||
bit_circuit.nodes.as_ref(),
|
||||
bit_circuit.levels.as_ref(),
|
||||
bit_circuit.max_inter_state,
|
||||
),
|
||||
AnyBitCircuit::B27(bit_circuit) => (
|
||||
bit_circuit.nodes.as_ref(),
|
||||
bit_circuit.levels.as_ref(),
|
||||
bit_circuit.max_inter_state,
|
||||
),
|
||||
AnyBitCircuit::B28(bit_circuit) => (
|
||||
bit_circuit.nodes.as_ref(),
|
||||
bit_circuit.levels.as_ref(),
|
||||
bit_circuit.max_inter_state,
|
||||
),
|
||||
AnyBitCircuit::B29(bit_circuit) => (
|
||||
bit_circuit.nodes.as_ref(),
|
||||
bit_circuit.levels.as_ref(),
|
||||
bit_circuit.max_inter_state,
|
||||
),
|
||||
AnyBitCircuit::B30(bit_circuit) => (
|
||||
bit_circuit.nodes.as_ref(),
|
||||
bit_circuit.levels.as_ref(),
|
||||
bit_circuit.max_inter_state,
|
||||
),
|
||||
AnyBitCircuit::B31(bit_circuit) => (
|
||||
bit_circuit.nodes.as_ref(),
|
||||
bit_circuit.levels.as_ref(),
|
||||
bit_circuit.max_inter_state,
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl GetBitCircuitInfo<u32> for Circuit<AnyBitCircuit, 32usize> {
|
||||
fn input_size(&self) -> usize {
|
||||
2 * u32::BITS as usize
|
||||
}
|
||||
fn output_size(&self) -> usize {
|
||||
u32::BITS as usize
|
||||
}
|
||||
fn get_circuit(&self, bit: usize) -> (&[Node], &[usize], usize) {
|
||||
self.0[bit].info()
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) static OUTPUT_CIRCUITS: Circuit<AnyBitCircuit, 32usize> = Circuit([
|
||||
AnyBitCircuit::B0(BitCircuit::new(
|
||||
[Node::new(0, 0, 0), Node::new(32, 1, 0), Node::new(0, 1, 0)],
|
||||
[0, 2],
|
||||
2,
|
||||
)),
|
||||
AnyBitCircuit::B1(BitCircuit::new(
|
||||
[Node::new(1, 0, 0), Node::new(33, 1, 0), Node::new(1, 1, 0)],
|
||||
[0, 2],
|
||||
2,
|
||||
)),
|
||||
AnyBitCircuit::B2(BitCircuit::new(
|
||||
[Node::new(2, 0, 0), Node::new(34, 1, 0), Node::new(2, 1, 0)],
|
||||
[0, 2],
|
||||
2,
|
||||
)),
|
||||
AnyBitCircuit::B3(BitCircuit::new(
|
||||
[Node::new(3, 0, 0), Node::new(35, 1, 0), Node::new(3, 1, 0)],
|
||||
[0, 2],
|
||||
2,
|
||||
)),
|
||||
AnyBitCircuit::B4(BitCircuit::new(
|
||||
[Node::new(4, 0, 0), Node::new(36, 1, 0), Node::new(4, 1, 0)],
|
||||
[0, 2],
|
||||
2,
|
||||
)),
|
||||
AnyBitCircuit::B5(BitCircuit::new(
|
||||
[Node::new(5, 0, 0), Node::new(37, 1, 0), Node::new(5, 1, 0)],
|
||||
[0, 2],
|
||||
2,
|
||||
)),
|
||||
AnyBitCircuit::B6(BitCircuit::new(
|
||||
[Node::new(6, 0, 0), Node::new(38, 1, 0), Node::new(6, 1, 0)],
|
||||
[0, 2],
|
||||
2,
|
||||
)),
|
||||
AnyBitCircuit::B7(BitCircuit::new(
|
||||
[Node::new(7, 0, 0), Node::new(39, 1, 0), Node::new(7, 1, 0)],
|
||||
[0, 2],
|
||||
2,
|
||||
)),
|
||||
AnyBitCircuit::B8(BitCircuit::new(
|
||||
[Node::new(8, 0, 0), Node::new(40, 1, 0), Node::new(8, 1, 0)],
|
||||
[0, 2],
|
||||
2,
|
||||
)),
|
||||
AnyBitCircuit::B9(BitCircuit::new(
|
||||
[Node::new(9, 0, 0), Node::new(41, 1, 0), Node::new(9, 1, 0)],
|
||||
[0, 2],
|
||||
2,
|
||||
)),
|
||||
AnyBitCircuit::B10(BitCircuit::new(
|
||||
[
|
||||
Node::new(10, 0, 0),
|
||||
Node::new(42, 1, 0),
|
||||
Node::new(10, 1, 0),
|
||||
],
|
||||
[0, 2],
|
||||
2,
|
||||
)),
|
||||
AnyBitCircuit::B11(BitCircuit::new(
|
||||
[
|
||||
Node::new(11, 0, 0),
|
||||
Node::new(43, 1, 0),
|
||||
Node::new(11, 1, 0),
|
||||
],
|
||||
[0, 2],
|
||||
2,
|
||||
)),
|
||||
AnyBitCircuit::B12(BitCircuit::new(
|
||||
[
|
||||
Node::new(12, 0, 0),
|
||||
Node::new(44, 1, 0),
|
||||
Node::new(12, 1, 0),
|
||||
],
|
||||
[0, 2],
|
||||
2,
|
||||
)),
|
||||
AnyBitCircuit::B13(BitCircuit::new(
|
||||
[
|
||||
Node::new(13, 0, 0),
|
||||
Node::new(45, 1, 0),
|
||||
Node::new(13, 1, 0),
|
||||
],
|
||||
[0, 2],
|
||||
2,
|
||||
)),
|
||||
AnyBitCircuit::B14(BitCircuit::new(
|
||||
[
|
||||
Node::new(14, 0, 0),
|
||||
Node::new(46, 1, 0),
|
||||
Node::new(14, 1, 0),
|
||||
],
|
||||
[0, 2],
|
||||
2,
|
||||
)),
|
||||
AnyBitCircuit::B15(BitCircuit::new(
|
||||
[
|
||||
Node::new(15, 0, 0),
|
||||
Node::new(47, 1, 0),
|
||||
Node::new(15, 1, 0),
|
||||
],
|
||||
[0, 2],
|
||||
2,
|
||||
)),
|
||||
AnyBitCircuit::B16(BitCircuit::new(
|
||||
[
|
||||
Node::new(16, 0, 0),
|
||||
Node::new(48, 1, 0),
|
||||
Node::new(16, 1, 0),
|
||||
],
|
||||
[0, 2],
|
||||
2,
|
||||
)),
|
||||
AnyBitCircuit::B17(BitCircuit::new(
|
||||
[
|
||||
Node::new(17, 0, 0),
|
||||
Node::new(49, 1, 0),
|
||||
Node::new(17, 1, 0),
|
||||
],
|
||||
[0, 2],
|
||||
2,
|
||||
)),
|
||||
AnyBitCircuit::B18(BitCircuit::new(
|
||||
[
|
||||
Node::new(18, 0, 0),
|
||||
Node::new(50, 1, 0),
|
||||
Node::new(18, 1, 0),
|
||||
],
|
||||
[0, 2],
|
||||
2,
|
||||
)),
|
||||
AnyBitCircuit::B19(BitCircuit::new(
|
||||
[
|
||||
Node::new(19, 0, 0),
|
||||
Node::new(51, 1, 0),
|
||||
Node::new(19, 1, 0),
|
||||
],
|
||||
[0, 2],
|
||||
2,
|
||||
)),
|
||||
AnyBitCircuit::B20(BitCircuit::new(
|
||||
[
|
||||
Node::new(20, 0, 0),
|
||||
Node::new(52, 1, 0),
|
||||
Node::new(20, 1, 0),
|
||||
],
|
||||
[0, 2],
|
||||
2,
|
||||
)),
|
||||
AnyBitCircuit::B21(BitCircuit::new(
|
||||
[
|
||||
Node::new(21, 0, 0),
|
||||
Node::new(53, 1, 0),
|
||||
Node::new(21, 1, 0),
|
||||
],
|
||||
[0, 2],
|
||||
2,
|
||||
)),
|
||||
AnyBitCircuit::B22(BitCircuit::new(
|
||||
[
|
||||
Node::new(22, 0, 0),
|
||||
Node::new(54, 1, 0),
|
||||
Node::new(22, 1, 0),
|
||||
],
|
||||
[0, 2],
|
||||
2,
|
||||
)),
|
||||
AnyBitCircuit::B23(BitCircuit::new(
|
||||
[
|
||||
Node::new(23, 0, 0),
|
||||
Node::new(55, 1, 0),
|
||||
Node::new(23, 1, 0),
|
||||
],
|
||||
[0, 2],
|
||||
2,
|
||||
)),
|
||||
AnyBitCircuit::B24(BitCircuit::new(
|
||||
[
|
||||
Node::new(24, 0, 0),
|
||||
Node::new(56, 1, 0),
|
||||
Node::new(24, 1, 0),
|
||||
],
|
||||
[0, 2],
|
||||
2,
|
||||
)),
|
||||
AnyBitCircuit::B25(BitCircuit::new(
|
||||
[
|
||||
Node::new(25, 0, 0),
|
||||
Node::new(57, 1, 0),
|
||||
Node::new(25, 1, 0),
|
||||
],
|
||||
[0, 2],
|
||||
2,
|
||||
)),
|
||||
AnyBitCircuit::B26(BitCircuit::new(
|
||||
[
|
||||
Node::new(26, 0, 0),
|
||||
Node::new(58, 1, 0),
|
||||
Node::new(26, 1, 0),
|
||||
],
|
||||
[0, 2],
|
||||
2,
|
||||
)),
|
||||
AnyBitCircuit::B27(BitCircuit::new(
|
||||
[
|
||||
Node::new(27, 0, 0),
|
||||
Node::new(59, 1, 0),
|
||||
Node::new(27, 1, 0),
|
||||
],
|
||||
[0, 2],
|
||||
2,
|
||||
)),
|
||||
AnyBitCircuit::B28(BitCircuit::new(
|
||||
[
|
||||
Node::new(28, 0, 0),
|
||||
Node::new(60, 1, 0),
|
||||
Node::new(28, 1, 0),
|
||||
],
|
||||
[0, 2],
|
||||
2,
|
||||
)),
|
||||
AnyBitCircuit::B29(BitCircuit::new(
|
||||
[
|
||||
Node::new(29, 0, 0),
|
||||
Node::new(61, 1, 0),
|
||||
Node::new(29, 1, 0),
|
||||
],
|
||||
[0, 2],
|
||||
2,
|
||||
)),
|
||||
AnyBitCircuit::B30(BitCircuit::new(
|
||||
[
|
||||
Node::new(30, 0, 0),
|
||||
Node::new(62, 1, 0),
|
||||
Node::new(30, 1, 0),
|
||||
],
|
||||
[0, 2],
|
||||
2,
|
||||
)),
|
||||
AnyBitCircuit::B31(BitCircuit::new(
|
||||
[
|
||||
Node::new(31, 0, 0),
|
||||
Node::new(63, 1, 0),
|
||||
Node::new(31, 1, 0),
|
||||
],
|
||||
[0, 2],
|
||||
2,
|
||||
)),
|
||||
]);
|
||||
10
poulpy-schemes/src/tfhe/bdd_arithmetic/circuits/u32/mod.rs
Normal file
10
poulpy-schemes/src/tfhe/bdd_arithmetic/circuits/u32/mod.rs
Normal file
@@ -0,0 +1,10 @@
|
||||
pub(crate) mod add_codegen;
|
||||
pub(crate) mod and_codegen;
|
||||
pub(crate) mod or_codegen;
|
||||
pub(crate) mod sll_codegen;
|
||||
pub(crate) mod slt_codegen;
|
||||
pub(crate) mod sltu_codegen;
|
||||
pub(crate) mod sra_codegen;
|
||||
pub(crate) mod srl_codegen;
|
||||
pub(crate) mod sub_codegen;
|
||||
pub(crate) mod xor_codegen;
|
||||
@@ -0,0 +1,34 @@
|
||||
use crate::tfhe::bdd_arithmetic::{BitCircuit, BitCircuitInfo, Circuit, GetBitCircuitInfo, Node};
|
||||
pub(crate) enum AnyBitCircuit {
|
||||
B0(BitCircuit<3, 2>),
|
||||
}
|
||||
|
||||
impl BitCircuitInfo for AnyBitCircuit {
|
||||
fn info(&self) -> (&[Node], &[usize], usize) {
|
||||
match self {
|
||||
AnyBitCircuit::B0(bit_circuit) => (
|
||||
bit_circuit.nodes.as_ref(),
|
||||
bit_circuit.levels.as_ref(),
|
||||
bit_circuit.max_inter_state,
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl GetBitCircuitInfo<u32> for Circuit<AnyBitCircuit, 1usize> {
|
||||
fn input_size(&self) -> usize {
|
||||
2 * u32::BITS as usize
|
||||
}
|
||||
fn output_size(&self) -> usize {
|
||||
u32::BITS as usize
|
||||
}
|
||||
fn get_circuit(&self, _bit: usize) -> (&[Node], &[usize], usize) {
|
||||
self.0[0].info()
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) static OUTPUT_CIRCUITS: Circuit<AnyBitCircuit, 1usize> = Circuit([AnyBitCircuit::B0(BitCircuit::new(
|
||||
[Node::new(0, 0, 0), Node::new(1, 1, 0), Node::new(0, 1, 1)],
|
||||
[0, 2],
|
||||
2,
|
||||
))]);
|
||||
1830
poulpy-schemes/src/tfhe/bdd_arithmetic/circuits/u32/sll_codegen.rs
Normal file
1830
poulpy-schemes/src/tfhe/bdd_arithmetic/circuits/u32/sll_codegen.rs
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,257 @@
|
||||
use crate::tfhe::bdd_arithmetic::{BitCircuit, BitCircuitInfo, Circuit, GetBitCircuitInfo, Node};
|
||||
pub(crate) enum AnyBitCircuit {
|
||||
B0(BitCircuit<219, 64>),
|
||||
}
|
||||
impl BitCircuitInfo for AnyBitCircuit {
|
||||
fn info(&self) -> (&[Node], &[usize], usize) {
|
||||
match self {
|
||||
AnyBitCircuit::B0(bit_circuit) => (
|
||||
bit_circuit.nodes.as_ref(),
|
||||
bit_circuit.levels.as_ref(),
|
||||
bit_circuit.max_inter_state,
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl GetBitCircuitInfo<u32> for Circuit<AnyBitCircuit, 1usize> {
|
||||
fn input_size(&self) -> usize {
|
||||
2 * u32::BITS as usize
|
||||
}
|
||||
fn output_size(&self) -> usize {
|
||||
1
|
||||
}
|
||||
fn get_circuit(&self, bit: usize) -> (&[Node], &[usize], usize) {
|
||||
self.0[bit].info()
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) static OUTPUT_CIRCUITS: Circuit<AnyBitCircuit, 1usize> = Circuit([AnyBitCircuit::B0(BitCircuit::new(
|
||||
[
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(32, 1, 0),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(0, 0, 2),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(33, 1, 2),
|
||||
Node::new(33, 2, 0),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(1, 3, 2),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(34, 1, 2),
|
||||
Node::new(34, 2, 0),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(2, 3, 2),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(35, 2, 0),
|
||||
Node::new(35, 1, 2),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(3, 2, 3),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(36, 1, 2),
|
||||
Node::new(36, 2, 0),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(4, 3, 2),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(37, 1, 2),
|
||||
Node::new(37, 2, 0),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(5, 3, 2),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(38, 2, 0),
|
||||
Node::new(38, 1, 2),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(6, 2, 3),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(39, 2, 0),
|
||||
Node::new(39, 1, 2),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(7, 2, 3),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(40, 1, 2),
|
||||
Node::new(40, 2, 0),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(8, 3, 2),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(41, 1, 2),
|
||||
Node::new(41, 2, 0),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(9, 3, 2),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(42, 2, 0),
|
||||
Node::new(42, 1, 2),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(10, 2, 3),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(43, 2, 0),
|
||||
Node::new(43, 1, 2),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(11, 2, 3),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(44, 1, 2),
|
||||
Node::new(44, 2, 0),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(12, 3, 2),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(45, 1, 2),
|
||||
Node::new(45, 2, 0),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(13, 3, 2),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(46, 2, 0),
|
||||
Node::new(46, 1, 2),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(14, 2, 3),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(47, 1, 2),
|
||||
Node::new(47, 2, 0),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(15, 3, 2),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(48, 2, 0),
|
||||
Node::new(48, 1, 2),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(16, 2, 3),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(49, 1, 2),
|
||||
Node::new(49, 2, 0),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(17, 3, 2),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(50, 2, 0),
|
||||
Node::new(50, 1, 2),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(18, 2, 3),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(51, 2, 0),
|
||||
Node::new(51, 1, 2),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(19, 2, 3),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(52, 1, 2),
|
||||
Node::new(52, 2, 0),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(20, 3, 2),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(53, 2, 0),
|
||||
Node::new(53, 1, 2),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(21, 2, 3),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(54, 1, 2),
|
||||
Node::new(54, 2, 0),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(22, 3, 2),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(55, 1, 2),
|
||||
Node::new(55, 2, 0),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(23, 3, 2),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(56, 2, 0),
|
||||
Node::new(56, 1, 2),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(24, 2, 3),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(57, 1, 2),
|
||||
Node::new(57, 2, 0),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(25, 3, 2),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(58, 2, 0),
|
||||
Node::new(58, 1, 2),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(26, 2, 3),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(59, 2, 0),
|
||||
Node::new(59, 1, 2),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(27, 2, 3),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(60, 2, 0),
|
||||
Node::new(60, 1, 2),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(28, 2, 3),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(61, 1, 2),
|
||||
Node::new(61, 2, 0),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(29, 3, 2),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(62, 1, 2),
|
||||
Node::new(62, 2, 0),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(30, 3, 2),
|
||||
Node::new(63, 2, 1),
|
||||
Node::new(63, 0, 2),
|
||||
Node::new(31, 0, 1),
|
||||
],
|
||||
[
|
||||
0, 3, 6, 10, 13, 17, 20, 24, 27, 31, 34, 38, 41, 45, 48, 52, 55, 59, 62, 66, 69, 73, 76, 80, 83, 87, 90, 94, 97, 101,
|
||||
104, 108, 111, 115, 118, 122, 125, 129, 132, 136, 139, 143, 146, 150, 153, 157, 160, 164, 167, 171, 174, 178, 181, 185,
|
||||
188, 192, 195, 199, 202, 206, 209, 213, 216, 218,
|
||||
],
|
||||
4,
|
||||
))]);
|
||||
@@ -0,0 +1,257 @@
|
||||
use crate::tfhe::bdd_arithmetic::{BitCircuit, BitCircuitInfo, Circuit, GetBitCircuitInfo, Node};
|
||||
pub(crate) enum AnyBitCircuit {
|
||||
B0(BitCircuit<219, 64>),
|
||||
}
|
||||
impl BitCircuitInfo for AnyBitCircuit {
|
||||
fn info(&self) -> (&[Node], &[usize], usize) {
|
||||
match self {
|
||||
AnyBitCircuit::B0(bit_circuit) => (
|
||||
bit_circuit.nodes.as_ref(),
|
||||
bit_circuit.levels.as_ref(),
|
||||
bit_circuit.max_inter_state,
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl GetBitCircuitInfo<u32> for Circuit<AnyBitCircuit, 1usize> {
|
||||
fn input_size(&self) -> usize {
|
||||
2 * u32::BITS as usize
|
||||
}
|
||||
fn output_size(&self) -> usize {
|
||||
1
|
||||
}
|
||||
fn get_circuit(&self, bit: usize) -> (&[Node], &[usize], usize) {
|
||||
self.0[bit].info()
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) static OUTPUT_CIRCUITS: Circuit<AnyBitCircuit, 1usize> = Circuit([AnyBitCircuit::B0(BitCircuit::new(
|
||||
[
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(32, 1, 0),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(0, 0, 2),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(33, 2, 0),
|
||||
Node::new(33, 1, 2),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(1, 2, 3),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(34, 2, 0),
|
||||
Node::new(34, 1, 2),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(2, 2, 3),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(35, 1, 2),
|
||||
Node::new(35, 2, 0),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(3, 3, 2),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(36, 2, 0),
|
||||
Node::new(36, 1, 2),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(4, 2, 3),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(37, 2, 0),
|
||||
Node::new(37, 1, 2),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(5, 2, 3),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(38, 1, 2),
|
||||
Node::new(38, 2, 0),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(6, 3, 2),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(39, 2, 0),
|
||||
Node::new(39, 1, 2),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(7, 2, 3),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(40, 1, 2),
|
||||
Node::new(40, 2, 0),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(8, 3, 2),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(41, 1, 2),
|
||||
Node::new(41, 2, 0),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(9, 3, 2),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(42, 1, 2),
|
||||
Node::new(42, 2, 0),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(10, 3, 2),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(43, 2, 0),
|
||||
Node::new(43, 1, 2),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(11, 2, 3),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(44, 1, 2),
|
||||
Node::new(44, 2, 0),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(12, 3, 2),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(45, 2, 0),
|
||||
Node::new(45, 1, 2),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(13, 2, 3),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(46, 1, 2),
|
||||
Node::new(46, 2, 0),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(14, 3, 2),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(47, 2, 0),
|
||||
Node::new(47, 1, 2),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(15, 2, 3),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(48, 2, 0),
|
||||
Node::new(48, 1, 2),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(16, 2, 3),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(49, 1, 2),
|
||||
Node::new(49, 2, 0),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(17, 3, 2),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(50, 1, 2),
|
||||
Node::new(50, 2, 0),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(18, 3, 2),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(51, 1, 2),
|
||||
Node::new(51, 2, 0),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(19, 3, 2),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(52, 2, 0),
|
||||
Node::new(52, 1, 2),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(20, 2, 3),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(53, 2, 0),
|
||||
Node::new(53, 1, 2),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(21, 2, 3),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(54, 2, 0),
|
||||
Node::new(54, 1, 2),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(22, 2, 3),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(55, 1, 2),
|
||||
Node::new(55, 2, 0),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(23, 3, 2),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(56, 2, 0),
|
||||
Node::new(56, 1, 2),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(24, 2, 3),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(57, 1, 2),
|
||||
Node::new(57, 2, 0),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(25, 3, 2),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(58, 2, 0),
|
||||
Node::new(58, 1, 2),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(26, 2, 3),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(59, 2, 0),
|
||||
Node::new(59, 1, 2),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(27, 2, 3),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(60, 2, 0),
|
||||
Node::new(60, 1, 2),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(28, 2, 3),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(61, 2, 0),
|
||||
Node::new(61, 1, 2),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(29, 2, 3),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(62, 2, 0),
|
||||
Node::new(62, 1, 2),
|
||||
Node::new(0, 0, 0),
|
||||
Node::new(0, 1, 1),
|
||||
Node::new(30, 2, 3),
|
||||
Node::new(63, 2, 0),
|
||||
Node::new(63, 1, 2),
|
||||
Node::new(31, 0, 1),
|
||||
],
|
||||
[
|
||||
0, 3, 6, 10, 13, 17, 20, 24, 27, 31, 34, 38, 41, 45, 48, 52, 55, 59, 62, 66, 69, 73, 76, 80, 83, 87, 90, 94, 97, 101,
|
||||
104, 108, 111, 115, 118, 122, 125, 129, 132, 136, 139, 143, 146, 150, 153, 157, 160, 164, 167, 171, 174, 178, 181, 185,
|
||||
188, 192, 195, 199, 202, 206, 209, 213, 216, 218,
|
||||
],
|
||||
4,
|
||||
))]);
|
||||
1762
poulpy-schemes/src/tfhe/bdd_arithmetic/circuits/u32/sra_codegen.rs
Normal file
1762
poulpy-schemes/src/tfhe/bdd_arithmetic/circuits/u32/sra_codegen.rs
Normal file
File diff suppressed because it is too large
Load Diff
1830
poulpy-schemes/src/tfhe/bdd_arithmetic/circuits/u32/srl_codegen.rs
Normal file
1830
poulpy-schemes/src/tfhe/bdd_arithmetic/circuits/u32/srl_codegen.rs
Normal file
File diff suppressed because it is too large
Load Diff
3019
poulpy-schemes/src/tfhe/bdd_arithmetic/circuits/u32/sub_codegen.rs
Normal file
3019
poulpy-schemes/src/tfhe/bdd_arithmetic/circuits/u32/sub_codegen.rs
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,34 @@
|
||||
use crate::tfhe::bdd_arithmetic::{BitCircuit, BitCircuitInfo, Circuit, GetBitCircuitInfo, Node};
|
||||
pub(crate) enum AnyBitCircuit {
|
||||
B0(BitCircuit<3, 2>),
|
||||
}
|
||||
|
||||
impl BitCircuitInfo for AnyBitCircuit {
|
||||
fn info(&self) -> (&[Node], &[usize], usize) {
|
||||
match self {
|
||||
AnyBitCircuit::B0(bit_circuit) => (
|
||||
bit_circuit.nodes.as_ref(),
|
||||
bit_circuit.levels.as_ref(),
|
||||
bit_circuit.max_inter_state,
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl GetBitCircuitInfo<u32> for Circuit<AnyBitCircuit, 1usize> {
|
||||
fn input_size(&self) -> usize {
|
||||
2 * u32::BITS as usize
|
||||
}
|
||||
fn output_size(&self) -> usize {
|
||||
u32::BITS as usize
|
||||
}
|
||||
fn get_circuit(&self, _bit: usize) -> (&[Node], &[usize], usize) {
|
||||
self.0[0].info()
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) static OUTPUT_CIRCUITS: Circuit<AnyBitCircuit, 1usize> = Circuit([AnyBitCircuit::B0(BitCircuit::new(
|
||||
[Node::new(1, 1, 0), Node::new(1, 0, 1), Node::new(0, 1, 0)],
|
||||
[0, 2],
|
||||
2,
|
||||
))]);
|
||||
198
poulpy-schemes/src/tfhe/bdd_arithmetic/eval.rs
Normal file
198
poulpy-schemes/src/tfhe/bdd_arithmetic/eval.rs
Normal file
@@ -0,0 +1,198 @@
|
||||
use itertools::Itertools;
|
||||
use poulpy_core::{
|
||||
GLWEExternalProductInplace, GLWEOperations, TakeGLWECtSlice,
|
||||
layouts::{
|
||||
GLWECiphertext, GLWECiphertextToMut, LWEInfos,
|
||||
prepared::{GGSWCiphertextPrepared, GGSWCiphertextPreparedToRef},
|
||||
},
|
||||
};
|
||||
use poulpy_hal::{
|
||||
api::{VecZnxAddInplace, VecZnxCopy, VecZnxNegateInplace, VecZnxSub},
|
||||
layouts::{Backend, DataMut, DataRef, Module, Scratch, ZnxZero},
|
||||
};
|
||||
|
||||
use crate::tfhe::bdd_arithmetic::UnsignedInteger;
|
||||
|
||||
pub trait BitCircuitInfo {
|
||||
fn info(&self) -> (&[Node], &[usize], usize);
|
||||
}
|
||||
|
||||
pub trait GetBitCircuitInfo<T: UnsignedInteger> {
|
||||
fn input_size(&self) -> usize;
|
||||
fn output_size(&self) -> usize;
|
||||
fn get_circuit(&self, bit: usize) -> (&[Node], &[usize], usize);
|
||||
}
|
||||
|
||||
pub(crate) struct BitCircuit<const N: usize, const K: usize> {
|
||||
pub(crate) nodes: [Node; N],
|
||||
pub(crate) levels: [usize; K],
|
||||
pub(crate) max_inter_state: usize,
|
||||
}
|
||||
|
||||
pub struct Circuit<C: BitCircuitInfo, const N: usize>(pub [C; N]);
|
||||
|
||||
pub trait CircuitExecute<BE: Backend, T: UnsignedInteger>
|
||||
where
|
||||
Self: GetBitCircuitInfo<T>,
|
||||
{
|
||||
fn execute<O>(
|
||||
&self,
|
||||
module: &Module<BE>,
|
||||
out: &mut [GLWECiphertext<O>],
|
||||
inputs: &[&dyn GGSWCiphertextPreparedToRef<BE>],
|
||||
scratch: &mut Scratch<BE>,
|
||||
) where
|
||||
O: DataMut;
|
||||
}
|
||||
|
||||
impl<C: BitCircuitInfo, const N: usize, T: UnsignedInteger, BE: Backend> CircuitExecute<BE, T> for Circuit<C, N>
|
||||
where
|
||||
Self: GetBitCircuitInfo<T>,
|
||||
Module<BE>: Cmux<BE> + VecZnxCopy,
|
||||
Scratch<BE>: TakeGLWECtSlice,
|
||||
{
|
||||
fn execute<O>(
|
||||
&self,
|
||||
module: &Module<BE>,
|
||||
out: &mut [GLWECiphertext<O>],
|
||||
inputs: &[&dyn GGSWCiphertextPreparedToRef<BE>],
|
||||
scratch: &mut Scratch<BE>,
|
||||
) where
|
||||
O: DataMut,
|
||||
{
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
assert_eq!(inputs.len(), self.input_size());
|
||||
assert!(out.len() >= self.output_size());
|
||||
}
|
||||
|
||||
for (i, out_i) in out.iter_mut().enumerate().take(self.output_size()) {
|
||||
let (nodes, levels, max_inter_state) = self.get_circuit(i);
|
||||
|
||||
let (mut level, scratch_1) = scratch.take_glwe_ct_slice(max_inter_state * 2, out_i);
|
||||
|
||||
level.iter_mut().for_each(|ct| ct.data_mut().zero());
|
||||
|
||||
// TODO: implement API on GLWE
|
||||
level[1]
|
||||
.data_mut()
|
||||
.encode_coeff_i64(out_i.base2k().into(), 0, 2, 0, 1);
|
||||
|
||||
let mut level_ref = level.iter_mut().collect_vec();
|
||||
let (mut prev_level, mut next_level) = level_ref.split_at_mut(max_inter_state);
|
||||
|
||||
for i in 0..levels.len() - 1 {
|
||||
let start: usize = levels[i];
|
||||
let end: usize = levels[i + 1];
|
||||
|
||||
let nodes_lvl: &[Node] = &nodes[start..end];
|
||||
|
||||
for (j, node) in nodes_lvl.iter().enumerate() {
|
||||
if node.low_index == node.high_index {
|
||||
next_level[j].copy(module, prev_level[node.low_index]);
|
||||
} else {
|
||||
module.cmux(
|
||||
next_level[j],
|
||||
prev_level[node.high_index],
|
||||
prev_level[node.low_index],
|
||||
&inputs[node.input_index].to_ref(),
|
||||
scratch_1,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
(prev_level, next_level) = (next_level, prev_level);
|
||||
}
|
||||
|
||||
// handle last output
|
||||
// there's always only 1 node at last level
|
||||
let node: &Node = nodes.last().unwrap();
|
||||
module.cmux(
|
||||
out_i,
|
||||
prev_level[node.high_index],
|
||||
prev_level[node.low_index],
|
||||
&inputs[node.input_index].to_ref(),
|
||||
scratch_1,
|
||||
);
|
||||
}
|
||||
|
||||
for out_i in out.iter_mut().skip(self.output_size()) {
|
||||
out_i.data_mut().zero();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<const N: usize, const K: usize> BitCircuit<N, K> {
|
||||
pub(crate) const fn new(nodes: [Node; N], levels: [usize; K], max_inter_state: usize) -> Self {
|
||||
Self {
|
||||
nodes,
|
||||
levels,
|
||||
max_inter_state,
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<const N: usize, const K: usize> BitCircuitInfo for BitCircuit<N, K> {
|
||||
fn info(&self) -> (&[Node], &[usize], usize) {
|
||||
(
|
||||
self.nodes.as_ref(),
|
||||
self.levels.as_ref(),
|
||||
self.max_inter_state,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Node {
|
||||
input_index: usize,
|
||||
high_index: usize,
|
||||
low_index: usize,
|
||||
}
|
||||
|
||||
impl Node {
|
||||
pub(crate) const fn new(input_index: usize, high_index: usize, low_index: usize) -> Self {
|
||||
Self {
|
||||
input_index,
|
||||
high_index,
|
||||
low_index,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Cmux<BE: Backend> {
|
||||
fn cmux<O, T, F, S>(
|
||||
&self,
|
||||
out: &mut GLWECiphertext<O>,
|
||||
t: &GLWECiphertext<T>,
|
||||
f: &GLWECiphertext<F>,
|
||||
s: &GGSWCiphertextPrepared<S, BE>,
|
||||
scratch: &mut Scratch<BE>,
|
||||
) where
|
||||
O: DataMut,
|
||||
T: DataRef,
|
||||
F: DataRef,
|
||||
S: DataRef;
|
||||
}
|
||||
|
||||
impl<BE: Backend> Cmux<BE> for Module<BE>
|
||||
where
|
||||
Module<BE>: GLWEExternalProductInplace<BE> + VecZnxSub + VecZnxCopy + VecZnxNegateInplace + VecZnxAddInplace,
|
||||
{
|
||||
fn cmux<O, T, F, S>(
|
||||
&self,
|
||||
out: &mut GLWECiphertext<O>,
|
||||
t: &GLWECiphertext<T>,
|
||||
f: &GLWECiphertext<F>,
|
||||
s: &GGSWCiphertextPrepared<S, BE>,
|
||||
scratch: &mut Scratch<BE>,
|
||||
) where
|
||||
O: DataMut,
|
||||
T: DataRef,
|
||||
F: DataRef,
|
||||
S: DataRef,
|
||||
{
|
||||
// let mut out: GLWECiphertext<&mut [u8]> = out.to_mut();
|
||||
out.sub(self, t, f);
|
||||
out.external_product_inplace(self, s, scratch);
|
||||
out.to_mut().add_inplace(self, f);
|
||||
}
|
||||
}
|
||||
241
poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs
Normal file
241
poulpy-schemes/src/tfhe/bdd_arithmetic/key.rs
Normal file
@@ -0,0 +1,241 @@
|
||||
#[cfg(test)]
|
||||
use crate::tfhe::bdd_arithmetic::FheUintBlocksPrepDebug;
|
||||
use crate::tfhe::{
|
||||
bdd_arithmetic::{FheUintBlocks, FheUintBlocksPrep, UnsignedInteger},
|
||||
blind_rotation::{BlindRotationAlgo, BlindRotationKey, BlindRotationKeyAlloc, BlindRotationKeyEncryptSk},
|
||||
circuit_bootstrapping::{
|
||||
CircuitBootstrappingKey, CircuitBootstrappingKeyEncryptSk, CircuitBootstrappingKeyLayout,
|
||||
CircuitBootstrappingKeyPrepared, CirtuitBootstrappingExecute,
|
||||
},
|
||||
};
|
||||
use poulpy_core::{
|
||||
TakeGGSW, TakeGLWECt,
|
||||
layouts::{
|
||||
GLWESecret, GLWEToLWEKey, GLWEToLWEKeyLayout, LWECiphertext, LWESecret,
|
||||
prepared::{GLWEToLWESwitchingKeyPrepared, Prepare, PrepareAlloc},
|
||||
},
|
||||
};
|
||||
use poulpy_hal::{
|
||||
api::{
|
||||
ScratchAvailable, SvpApplyDftToDft, SvpApplyDftToDftInplace, SvpPPolAlloc, SvpPPolAllocBytes, SvpPrepare, TakeScalarZnx,
|
||||
TakeSvpPPol, TakeVecZnx, TakeVecZnxBig, TakeVecZnxDft, VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace,
|
||||
VecZnxAutomorphism, VecZnxAutomorphismInplace, VecZnxBigAddSmallInplace, VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes,
|
||||
VecZnxDftAllocBytes, VecZnxDftApply, VecZnxFillUniform, VecZnxIdftApplyConsume, VecZnxIdftApplyTmpA, VecZnxNormalize,
|
||||
VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotate, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing,
|
||||
VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPrepare,
|
||||
},
|
||||
layouts::{Backend, Data, DataMut, DataRef, Module, Scratch},
|
||||
source::Source,
|
||||
};
|
||||
|
||||
pub trait BDDKeyInfos {
|
||||
fn cbt_infos(&self) -> CircuitBootstrappingKeyLayout;
|
||||
fn ks_infos(&self) -> GLWEToLWEKeyLayout;
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct BDDKeyLayout {
|
||||
pub cbt: CircuitBootstrappingKeyLayout,
|
||||
pub ks: GLWEToLWEKeyLayout,
|
||||
}
|
||||
|
||||
impl BDDKeyInfos for BDDKeyLayout {
|
||||
fn cbt_infos(&self) -> CircuitBootstrappingKeyLayout {
|
||||
self.cbt
|
||||
}
|
||||
|
||||
fn ks_infos(&self) -> GLWEToLWEKeyLayout {
|
||||
self.ks
|
||||
}
|
||||
}
|
||||
|
||||
pub struct BDDKey<CBT, LWE, BRA>
|
||||
where
|
||||
CBT: Data,
|
||||
LWE: Data,
|
||||
BRA: BlindRotationAlgo,
|
||||
{
|
||||
cbt: CircuitBootstrappingKey<CBT, BRA>,
|
||||
ks: GLWEToLWEKey<LWE>,
|
||||
}
|
||||
|
||||
impl<BRA: BlindRotationAlgo> BDDKey<Vec<u8>, Vec<u8>, BRA> {
|
||||
pub fn encrypt_sk<DLwe, DGlwe, A, BE: Backend>(
|
||||
module: &Module<BE>,
|
||||
sk_lwe: &LWESecret<DLwe>,
|
||||
sk_glwe: &GLWESecret<DGlwe>,
|
||||
infos: &A,
|
||||
source_xa: &mut Source,
|
||||
source_xe: &mut Source,
|
||||
scratch: &mut Scratch<BE>,
|
||||
) -> Self
|
||||
where
|
||||
A: BDDKeyInfos,
|
||||
DLwe: DataRef,
|
||||
DGlwe: DataRef,
|
||||
BlindRotationKey<Vec<u8>, BRA>: BlindRotationKeyAlloc + BlindRotationKeyEncryptSk<BE>,
|
||||
Module<BE>: SvpApplyDftToDft<BE>
|
||||
+ VecZnxIdftApplyTmpA<BE>
|
||||
+ VecZnxAddScalarInplace
|
||||
+ VecZnxDftAllocBytes
|
||||
+ VecZnxBigNormalize<BE>
|
||||
+ VecZnxDftApply<BE>
|
||||
+ SvpApplyDftToDftInplace<BE>
|
||||
+ VecZnxIdftApplyConsume<BE>
|
||||
+ VecZnxNormalizeTmpBytes
|
||||
+ VecZnxFillUniform
|
||||
+ VecZnxSubInplace
|
||||
+ VecZnxAddInplace
|
||||
+ VecZnxNormalizeInplace<BE>
|
||||
+ VecZnxAddNormal
|
||||
+ VecZnxNormalize<BE>
|
||||
+ VecZnxSub
|
||||
+ SvpPrepare<BE>
|
||||
+ VecZnxSwitchRing
|
||||
+ SvpPPolAllocBytes
|
||||
+ SvpPPolAlloc<BE>
|
||||
+ VecZnxAutomorphism
|
||||
+ VecZnxAutomorphismInplace<BE>,
|
||||
Scratch<BE>: TakeVecZnxDft<BE> + ScratchAvailable + TakeVecZnx + TakeScalarZnx + TakeSvpPPol<BE> + TakeVecZnxBig<BE>,
|
||||
{
|
||||
let mut ks: GLWEToLWEKey<Vec<u8>> = GLWEToLWEKey::alloc(&infos.ks_infos());
|
||||
ks.encrypt_sk(module, sk_lwe, sk_glwe, source_xa, source_xe, scratch);
|
||||
|
||||
Self {
|
||||
cbt: CircuitBootstrappingKey::encrypt_sk(
|
||||
module,
|
||||
sk_lwe,
|
||||
sk_glwe,
|
||||
&infos.cbt_infos(),
|
||||
source_xa,
|
||||
source_xe,
|
||||
scratch,
|
||||
),
|
||||
ks,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct BDDKeyPrepared<CBT, LWE, BRA, BE>
|
||||
where
|
||||
CBT: Data,
|
||||
LWE: Data,
|
||||
BRA: BlindRotationAlgo,
|
||||
BE: Backend,
|
||||
{
|
||||
cbt: CircuitBootstrappingKeyPrepared<CBT, BRA, BE>,
|
||||
ks: GLWEToLWESwitchingKeyPrepared<LWE, BE>,
|
||||
}
|
||||
|
||||
impl<CBT: DataMut, LWE: DataMut, BRA: BlindRotationAlgo, BE: Backend> PrepareAlloc<BE, BDDKeyPrepared<CBT, LWE, BRA, BE>>
|
||||
for BDDKey<CBT, LWE, BRA>
|
||||
where
|
||||
CircuitBootstrappingKey<CBT, BRA>: PrepareAlloc<BE, CircuitBootstrappingKeyPrepared<CBT, BRA, BE>>,
|
||||
GLWEToLWEKey<LWE>: PrepareAlloc<BE, GLWEToLWESwitchingKeyPrepared<LWE, BE>>,
|
||||
{
|
||||
fn prepare_alloc(&self, module: &Module<BE>, scratch: &mut Scratch<BE>) -> BDDKeyPrepared<CBT, LWE, BRA, BE> {
|
||||
BDDKeyPrepared {
|
||||
cbt: self.cbt.prepare_alloc(module, scratch),
|
||||
ks: self.ks.prepare_alloc(module, scratch),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait FheUintPrepare<BE: Backend, OUT, IN> {
|
||||
fn prepare(&self, module: &Module<BE>, out: &mut OUT, bits: &IN, scratch: &mut Scratch<BE>);
|
||||
}
|
||||
|
||||
impl<CBT, OUT, IN, LWE, BRA, BE, T> FheUintPrepare<BE, FheUintBlocksPrep<OUT, BE, T>, FheUintBlocks<IN, T>>
|
||||
for BDDKeyPrepared<CBT, LWE, BRA, BE>
|
||||
where
|
||||
T: UnsignedInteger,
|
||||
CBT: DataRef,
|
||||
OUT: DataMut,
|
||||
IN: DataRef,
|
||||
LWE: DataRef,
|
||||
BRA: BlindRotationAlgo,
|
||||
BE: Backend,
|
||||
Module<BE>: VmpPrepare<BE>
|
||||
+ VecZnxRotate
|
||||
+ VecZnxDftAllocBytes
|
||||
+ VmpApplyDftToDftTmpBytes
|
||||
+ VecZnxBigNormalizeTmpBytes
|
||||
+ VmpApplyDftToDft<BE>
|
||||
+ VmpApplyDftToDftAdd<BE>
|
||||
+ VecZnxDftApply<BE>
|
||||
+ VecZnxIdftApplyConsume<BE>
|
||||
+ VecZnxBigAddSmallInplace<BE>
|
||||
+ VecZnxBigNormalize<BE>
|
||||
+ VecZnxNormalize<BE>
|
||||
+ VecZnxNormalizeTmpBytes,
|
||||
Scratch<BE>: ScratchAvailable + TakeVecZnxDft<BE> + TakeGLWECt + TakeVecZnx + TakeGGSW,
|
||||
CircuitBootstrappingKeyPrepared<CBT, BRA, BE>: CirtuitBootstrappingExecute<BE>,
|
||||
{
|
||||
fn prepare(
|
||||
&self,
|
||||
module: &Module<BE>,
|
||||
out: &mut FheUintBlocksPrep<OUT, BE, T>,
|
||||
bits: &FheUintBlocks<IN, T>,
|
||||
scratch: &mut Scratch<BE>,
|
||||
) {
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
assert_eq!(out.blocks.len(), bits.blocks.len());
|
||||
}
|
||||
let mut lwe: LWECiphertext<Vec<u8>> = LWECiphertext::alloc(&bits.blocks[0]); //TODO: add TakeLWE
|
||||
let (mut tmp_ggsw, scratch_1) = scratch.take_ggsw(out);
|
||||
for (dst, src) in out.blocks.iter_mut().zip(bits.blocks.iter()) {
|
||||
lwe.from_glwe(module, src, &self.ks, scratch_1);
|
||||
self.cbt
|
||||
.execute_to_constant(module, &mut tmp_ggsw, &lwe, 1, 1, scratch_1);
|
||||
dst.prepare(module, &tmp_ggsw, scratch_1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
impl<CBT, OUT, IN, LWE, BRA, BE, T> FheUintPrepare<BE, FheUintBlocksPrepDebug<OUT, T>, FheUintBlocks<IN, T>>
|
||||
for BDDKeyPrepared<CBT, LWE, BRA, BE>
|
||||
where
|
||||
T: UnsignedInteger,
|
||||
CBT: DataRef,
|
||||
OUT: DataMut,
|
||||
IN: DataRef,
|
||||
LWE: DataRef,
|
||||
BRA: BlindRotationAlgo,
|
||||
BE: Backend,
|
||||
Module<BE>: VmpPrepare<BE>
|
||||
+ VecZnxRotate
|
||||
+ VecZnxDftAllocBytes
|
||||
+ VmpApplyDftToDftTmpBytes
|
||||
+ VecZnxBigNormalizeTmpBytes
|
||||
+ VmpApplyDftToDft<BE>
|
||||
+ VmpApplyDftToDftAdd<BE>
|
||||
+ VecZnxDftApply<BE>
|
||||
+ VecZnxIdftApplyConsume<BE>
|
||||
+ VecZnxBigAddSmallInplace<BE>
|
||||
+ VecZnxBigNormalize<BE>
|
||||
+ VecZnxNormalize<BE>
|
||||
+ VecZnxNormalizeTmpBytes,
|
||||
Scratch<BE>: ScratchAvailable + TakeVecZnxDft<BE> + TakeGLWECt + TakeVecZnx + TakeGGSW,
|
||||
CircuitBootstrappingKeyPrepared<CBT, BRA, BE>: CirtuitBootstrappingExecute<BE>,
|
||||
{
|
||||
fn prepare(
|
||||
&self,
|
||||
module: &Module<BE>,
|
||||
out: &mut FheUintBlocksPrepDebug<OUT, T>,
|
||||
bits: &FheUintBlocks<IN, T>,
|
||||
scratch: &mut Scratch<BE>,
|
||||
) {
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
assert_eq!(out.blocks.len(), bits.blocks.len());
|
||||
}
|
||||
let mut lwe: LWECiphertext<Vec<u8>> = LWECiphertext::alloc(&bits.blocks[0]); //TODO: add TakeLWE
|
||||
for (dst, src) in out.blocks.iter_mut().zip(bits.blocks.iter()) {
|
||||
lwe.from_glwe(module, src, &self.ks, scratch);
|
||||
self.cbt
|
||||
.execute_to_constant(module, dst, &lwe, 1, 1, scratch);
|
||||
}
|
||||
}
|
||||
}
|
||||
86
poulpy-schemes/src/tfhe/bdd_arithmetic/mod.rs
Normal file
86
poulpy-schemes/src/tfhe/bdd_arithmetic/mod.rs
Normal file
@@ -0,0 +1,86 @@
|
||||
mod bdd_2w_to_1w;
|
||||
mod ciphertexts;
|
||||
mod circuits;
|
||||
mod eval;
|
||||
mod key;
|
||||
mod parameters;
|
||||
|
||||
pub use bdd_2w_to_1w::*;
|
||||
pub use ciphertexts::*;
|
||||
pub(crate) use circuits::*;
|
||||
pub(crate) use eval::*;
|
||||
pub use key::*;
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) use parameters::*;
|
||||
|
||||
#[cfg(test)]
|
||||
mod test;
|
||||
|
||||
pub trait UnsignedInteger: Copy + 'static {
|
||||
const WORD_SIZE: usize;
|
||||
}
|
||||
|
||||
impl UnsignedInteger for u8 {
|
||||
const WORD_SIZE: usize = 8;
|
||||
}
|
||||
impl UnsignedInteger for u16 {
|
||||
const WORD_SIZE: usize = 16;
|
||||
}
|
||||
impl UnsignedInteger for u32 {
|
||||
const WORD_SIZE: usize = 32;
|
||||
}
|
||||
impl UnsignedInteger for u64 {
|
||||
const WORD_SIZE: usize = 64;
|
||||
}
|
||||
impl UnsignedInteger for u128 {
|
||||
const WORD_SIZE: usize = 128;
|
||||
}
|
||||
|
||||
pub trait ToBits {
|
||||
fn bit(&self, i: usize) -> u8;
|
||||
}
|
||||
|
||||
macro_rules! impl_tobits {
|
||||
($($t:ty),*) => {
|
||||
$(
|
||||
impl ToBits for $t {
|
||||
fn bit(&self, i: usize) -> u8 {
|
||||
if i >= (std::mem::size_of::<$t>() * 8) {
|
||||
panic!("bit index {} out of range for {}", i, stringify!($t));
|
||||
}
|
||||
((self >> i) & 1) as u8
|
||||
}
|
||||
}
|
||||
)*
|
||||
};
|
||||
}
|
||||
|
||||
impl_tobits!(u8, u16, u32, u64, u128);
|
||||
|
||||
pub trait FromBits: Sized {
|
||||
fn from_bits(bits: &[u8]) -> Self;
|
||||
}
|
||||
|
||||
macro_rules! impl_from_bits {
|
||||
($($t:ty),*) => {
|
||||
$(
|
||||
impl FromBits for $t {
|
||||
fn from_bits(bits: &[u8]) -> Self {
|
||||
let mut value: $t = 0;
|
||||
let max_bits = std::mem::size_of::<$t>() * 8;
|
||||
let n = bits.len().min(max_bits);
|
||||
|
||||
for (i, &bit) in bits.iter().take(n).enumerate() {
|
||||
if bit != 0 {
|
||||
value |= 1 << i;
|
||||
}
|
||||
}
|
||||
value
|
||||
}
|
||||
}
|
||||
)*
|
||||
};
|
||||
}
|
||||
|
||||
impl_from_bits!(u8, u16, u32, u64, u128);
|
||||
80
poulpy-schemes/src/tfhe/bdd_arithmetic/parameters.rs
Normal file
80
poulpy-schemes/src/tfhe/bdd_arithmetic/parameters.rs
Normal file
@@ -0,0 +1,80 @@
|
||||
#[cfg(test)]
|
||||
use poulpy_core::layouts::{
|
||||
Base2K, Degree, Dnum, Dsize, GGLWEAutomorphismKeyLayout, GGLWETensorKeyLayout, GGSWCiphertextLayout, GLWECiphertextLayout,
|
||||
GLWEToLWEKeyLayout, Rank, TorusPrecision,
|
||||
};
|
||||
|
||||
#[cfg(test)]
|
||||
use crate::tfhe::{
|
||||
bdd_arithmetic::BDDKeyLayout, blind_rotation::BlindRotationKeyLayout, circuit_bootstrapping::CircuitBootstrappingKeyLayout,
|
||||
};
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) const TEST_N_GLWE: u32 = 512;
|
||||
#[cfg(test)]
|
||||
pub(crate) const TEST_N_LWE: u32 = 77;
|
||||
#[cfg(test)]
|
||||
pub(crate) const TEST_BASE2K: u32 = 13;
|
||||
#[cfg(test)]
|
||||
pub(crate) const TEST_K_GLWE: u32 = 26;
|
||||
#[cfg(test)]
|
||||
pub(crate) const TEST_K_GGSW: u32 = 39;
|
||||
#[cfg(test)]
|
||||
pub(crate) const TEST_BLOCK_SIZE: u32 = 7;
|
||||
#[cfg(test)]
|
||||
pub(crate) const TEST_RANK: u32 = 2;
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) static TEST_GLWE_INFOS: GLWECiphertextLayout = GLWECiphertextLayout {
|
||||
n: Degree(TEST_N_GLWE),
|
||||
base2k: Base2K(TEST_BASE2K),
|
||||
k: TorusPrecision(TEST_K_GLWE),
|
||||
rank: Rank(TEST_RANK),
|
||||
};
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) static TEST_GGSW_INFOS: GGSWCiphertextLayout = GGSWCiphertextLayout {
|
||||
n: Degree(TEST_N_GLWE),
|
||||
base2k: Base2K(TEST_BASE2K),
|
||||
k: TorusPrecision(TEST_K_GGSW),
|
||||
rank: Rank(TEST_RANK),
|
||||
dnum: Dnum(2),
|
||||
dsize: Dsize(1),
|
||||
};
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) static TEST_BDD_KEY_LAYOUT: BDDKeyLayout = BDDKeyLayout {
|
||||
cbt: CircuitBootstrappingKeyLayout {
|
||||
layout_brk: BlindRotationKeyLayout {
|
||||
n_glwe: Degree(TEST_N_GLWE),
|
||||
n_lwe: Degree(TEST_N_LWE),
|
||||
base2k: Base2K(TEST_BASE2K),
|
||||
k: TorusPrecision(52),
|
||||
dnum: Dnum(3),
|
||||
rank: Rank(TEST_RANK),
|
||||
},
|
||||
layout_atk: GGLWEAutomorphismKeyLayout {
|
||||
n: Degree(TEST_N_GLWE),
|
||||
base2k: Base2K(TEST_BASE2K),
|
||||
k: TorusPrecision(52),
|
||||
rank: Rank(TEST_RANK),
|
||||
dnum: Dnum(3),
|
||||
dsize: Dsize(1),
|
||||
},
|
||||
layout_tsk: GGLWETensorKeyLayout {
|
||||
n: Degree(TEST_N_GLWE),
|
||||
base2k: Base2K(TEST_BASE2K),
|
||||
k: TorusPrecision(52),
|
||||
rank: Rank(TEST_RANK),
|
||||
dnum: Dnum(3),
|
||||
dsize: Dsize(1),
|
||||
},
|
||||
},
|
||||
ks: GLWEToLWEKeyLayout {
|
||||
n: Degree(TEST_N_GLWE),
|
||||
base2k: Base2K(TEST_BASE2K),
|
||||
k: TorusPrecision(39),
|
||||
rank_in: Rank(TEST_RANK),
|
||||
dnum: Dnum(2),
|
||||
},
|
||||
};
|
||||
224
poulpy-schemes/src/tfhe/bdd_arithmetic/test.rs
Normal file
224
poulpy-schemes/src/tfhe/bdd_arithmetic/test.rs
Normal file
@@ -0,0 +1,224 @@
|
||||
use std::time::Instant;
|
||||
|
||||
use poulpy_backend::FFT64Ref;
|
||||
use poulpy_core::{
|
||||
TakeGGSW, TakeGLWEPt,
|
||||
layouts::{
|
||||
GGSWCiphertextLayout, GLWECiphertextLayout, GLWESecret, LWEInfos, LWESecret,
|
||||
prepared::{GLWESecretPrepared, PrepareAlloc},
|
||||
},
|
||||
};
|
||||
use poulpy_hal::{
|
||||
api::{
|
||||
ModuleNew, ScratchAvailable, ScratchOwnedAlloc, ScratchOwnedBorrow, SvpApplyDftToDft, SvpApplyDftToDftInplace,
|
||||
SvpPPolAlloc, SvpPPolAllocBytes, SvpPrepare, TakeScalarZnx, TakeSlice, TakeVecZnx, TakeVecZnxBig, TakeVecZnxDft,
|
||||
VecZnxAddInplace, VecZnxAddNormal, VecZnxAddScalarInplace, VecZnxAutomorphism, VecZnxAutomorphismInplace,
|
||||
VecZnxBigAddInplace, VecZnxBigAddSmallInplace, VecZnxBigAlloc, VecZnxBigAllocBytes, VecZnxBigAutomorphismInplace,
|
||||
VecZnxBigNormalize, VecZnxBigNormalizeTmpBytes, VecZnxBigSubSmallNegateInplace, VecZnxCopy, VecZnxDftAddInplace,
|
||||
VecZnxDftAlloc, VecZnxDftAllocBytes, VecZnxDftApply, VecZnxDftCopy, VecZnxFillUniform, VecZnxIdftApplyConsume,
|
||||
VecZnxIdftApplyTmpA, VecZnxNegateInplace, VecZnxNormalize, VecZnxNormalizeInplace, VecZnxNormalizeTmpBytes, VecZnxRotate,
|
||||
VecZnxRotateInplace, VecZnxRotateInplaceTmpBytes, VecZnxRshInplace, VecZnxSub, VecZnxSubInplace, VecZnxSwitchRing,
|
||||
VmpApplyDftToDft, VmpApplyDftToDftAdd, VmpApplyDftToDftTmpBytes, VmpPMatAlloc, VmpPrepare, ZnAddNormal, ZnFillUniform,
|
||||
ZnNormalizeInplace,
|
||||
},
|
||||
layouts::{Backend, Module, Scratch, ScratchOwned},
|
||||
oep::{
|
||||
ScratchAvailableImpl, ScratchOwnedAllocImpl, ScratchOwnedBorrowImpl, TakeMatZnxImpl, TakeScalarZnxImpl, TakeSvpPPolImpl,
|
||||
TakeVecZnxBigImpl, TakeVecZnxDftImpl, TakeVecZnxDftSliceImpl, TakeVecZnxImpl, TakeVecZnxSliceImpl,
|
||||
},
|
||||
source::Source,
|
||||
};
|
||||
use rand::RngCore;
|
||||
|
||||
use crate::tfhe::{
|
||||
bdd_arithmetic::{
|
||||
Add, BDDKey, BDDKeyLayout, BDDKeyPrepared, FheUintBlocks, FheUintBlocksPrep, FheUintBlocksPrepDebug, Sub,
|
||||
TEST_BDD_KEY_LAYOUT, TEST_BLOCK_SIZE, TEST_GGSW_INFOS, TEST_GLWE_INFOS, TEST_N_LWE,
|
||||
},
|
||||
blind_rotation::{
|
||||
BlincRotationExecute, BlindRotationAlgo, BlindRotationKey, BlindRotationKeyAlloc, BlindRotationKeyEncryptSk,
|
||||
BlindRotationKeyPrepared, CGGI,
|
||||
},
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn test_bdd_2w_to_1w_fft64_ref() {
|
||||
test_bdd_2w_to_1w::<FFT64Ref, CGGI>()
|
||||
}
|
||||
|
||||
fn test_bdd_2w_to_1w<BE: Backend, BRA: BlindRotationAlgo>()
|
||||
where
|
||||
Module<BE>: ModuleNew<BE> + SvpPPolAlloc<BE> + SvpPrepare<BE> + VmpPMatAlloc<BE>,
|
||||
ScratchOwned<BE>: ScratchOwnedAlloc<BE> + ScratchOwnedBorrow<BE>,
|
||||
Module<BE>: VecZnxAddScalarInplace
|
||||
+ VecZnxDftAllocBytes
|
||||
+ VecZnxBigNormalize<BE>
|
||||
+ VecZnxDftApply<BE>
|
||||
+ SvpApplyDftToDftInplace<BE>
|
||||
+ VecZnxIdftApplyConsume<BE>
|
||||
+ VecZnxNormalizeTmpBytes
|
||||
+ VecZnxFillUniform
|
||||
+ VecZnxSubInplace
|
||||
+ VecZnxAddInplace
|
||||
+ VecZnxNormalizeInplace<BE>
|
||||
+ VecZnxAddNormal
|
||||
+ VecZnxNormalize<BE>
|
||||
+ VecZnxSub
|
||||
+ VmpPrepare<BE>,
|
||||
Scratch<BE>: TakeVecZnxDft<BE> + ScratchAvailable + TakeVecZnx + TakeGGSW + TakeScalarZnx + TakeSlice,
|
||||
Module<BE>: VecZnxCopy + VecZnxNegateInplace + VmpApplyDftToDftTmpBytes + VmpApplyDftToDft<BE> + VmpApplyDftToDftAdd<BE>,
|
||||
Module<BE>: VecZnxBigAddInplace<BE> + VecZnxBigAddSmallInplace<BE> + VecZnxBigNormalize<BE>,
|
||||
Scratch<BE>: TakeVecZnxDft<BE> + TakeVecZnxBig<BE> + TakeGLWEPt<BE>,
|
||||
Module<BE>: VecZnxAutomorphism
|
||||
+ VecZnxSwitchRing
|
||||
+ VecZnxBigAllocBytes
|
||||
+ VecZnxIdftApplyTmpA<BE>
|
||||
+ SvpApplyDftToDft<BE>
|
||||
+ VecZnxBigAlloc<BE>
|
||||
+ VecZnxDftAlloc<BE>
|
||||
+ VecZnxBigNormalizeTmpBytes
|
||||
+ SvpPPolAllocBytes
|
||||
+ VecZnxRotateInplace<BE>
|
||||
+ VecZnxBigAutomorphismInplace<BE>
|
||||
+ VecZnxRshInplace<BE>
|
||||
+ VecZnxDftCopy<BE>
|
||||
+ VecZnxAutomorphismInplace<BE>
|
||||
+ VecZnxBigSubSmallNegateInplace<BE>
|
||||
+ VecZnxRotateInplaceTmpBytes
|
||||
+ VecZnxBigAllocBytes
|
||||
+ VecZnxDftAddInplace<BE>
|
||||
+ VecZnxRotate
|
||||
+ ZnFillUniform
|
||||
+ ZnAddNormal
|
||||
+ ZnNormalizeInplace<BE>,
|
||||
BE: Backend
|
||||
+ ScratchOwnedAllocImpl<BE>
|
||||
+ ScratchOwnedBorrowImpl<BE>
|
||||
+ TakeVecZnxDftImpl<BE>
|
||||
+ ScratchAvailableImpl<BE>
|
||||
+ TakeVecZnxImpl<BE>
|
||||
+ TakeScalarZnxImpl<BE>
|
||||
+ TakeSvpPPolImpl<BE>
|
||||
+ TakeVecZnxBigImpl<BE>
|
||||
+ TakeVecZnxDftSliceImpl<BE>
|
||||
+ TakeMatZnxImpl<BE>
|
||||
+ TakeVecZnxSliceImpl<BE>,
|
||||
BlindRotationKey<Vec<u8>, BRA>: PrepareAlloc<BE, BlindRotationKeyPrepared<Vec<u8>, BRA, BE>>,
|
||||
BlindRotationKeyPrepared<Vec<u8>, BRA, BE>: BlincRotationExecute<BE>,
|
||||
BlindRotationKey<Vec<u8>, BRA>: BlindRotationKeyAlloc + BlindRotationKeyEncryptSk<BE>,
|
||||
{
|
||||
let glwe_infos: GLWECiphertextLayout = TEST_GLWE_INFOS;
|
||||
let ggsw_infos: GGSWCiphertextLayout = TEST_GGSW_INFOS;
|
||||
|
||||
let n_glwe: usize = glwe_infos.n().into();
|
||||
|
||||
let module: Module<BE> = Module::<BE>::new(n_glwe as u64);
|
||||
let mut source: Source = Source::new([6u8; 32]);
|
||||
let mut source_xs: Source = Source::new([1u8; 32]);
|
||||
let mut source_xa: Source = Source::new([2u8; 32]);
|
||||
let mut source_xe: Source = Source::new([3u8; 32]);
|
||||
|
||||
let mut scratch: ScratchOwned<BE> = ScratchOwned::alloc(1 << 22);
|
||||
|
||||
let mut sk_glwe: GLWESecret<Vec<u8>> = GLWESecret::alloc(&glwe_infos);
|
||||
sk_glwe.fill_ternary_prob(0.5, &mut source_xs);
|
||||
let sk_glwe_prep: GLWESecretPrepared<Vec<u8>, BE> = sk_glwe.prepare_alloc(&module, scratch.borrow());
|
||||
|
||||
let a: u32 = source.next_u32();
|
||||
let b: u32 = source.next_u32();
|
||||
|
||||
println!("a: {a}");
|
||||
println!("b: {b}");
|
||||
|
||||
let mut a_enc_prep: FheUintBlocksPrep<Vec<u8>, BE, u32> = FheUintBlocksPrep::<Vec<u8>, BE, u32>::alloc(&module, &ggsw_infos);
|
||||
let mut b_enc_prep: FheUintBlocksPrep<Vec<u8>, BE, u32> = FheUintBlocksPrep::<Vec<u8>, BE, u32>::alloc(&module, &ggsw_infos);
|
||||
let mut c_enc: FheUintBlocks<Vec<u8>, u32> = FheUintBlocks::<Vec<u8>, u32>::alloc(&module, &glwe_infos);
|
||||
let mut c_enc_prep_debug: FheUintBlocksPrepDebug<Vec<u8>, u32> =
|
||||
FheUintBlocksPrepDebug::<Vec<u8>, u32>::alloc(&module, &ggsw_infos);
|
||||
let mut c_enc_prep: FheUintBlocksPrep<Vec<u8>, BE, u32> = FheUintBlocksPrep::<Vec<u8>, BE, u32>::alloc(&module, &ggsw_infos);
|
||||
|
||||
a_enc_prep.encrypt_sk(
|
||||
&module,
|
||||
a,
|
||||
&sk_glwe_prep,
|
||||
&mut source_xa,
|
||||
&mut source_xe,
|
||||
scratch.borrow(),
|
||||
);
|
||||
b_enc_prep.encrypt_sk(
|
||||
&module,
|
||||
b,
|
||||
&sk_glwe_prep,
|
||||
&mut source_xa,
|
||||
&mut source_xe,
|
||||
scratch.borrow(),
|
||||
);
|
||||
|
||||
let start: Instant = Instant::now();
|
||||
c_enc.sub(&module, &a_enc_prep, &b_enc_prep, scratch.borrow());
|
||||
|
||||
let duration: std::time::Duration = start.elapsed();
|
||||
println!("add: {} ms", duration.as_millis());
|
||||
|
||||
println!(
|
||||
"have: {}",
|
||||
c_enc.decrypt(&module, &sk_glwe_prep, scratch.borrow())
|
||||
);
|
||||
println!("want: {}", a.wrapping_sub(b));
|
||||
println!(
|
||||
"noise: {:?}",
|
||||
c_enc.noise(&module, &sk_glwe_prep, a.wrapping_sub(b), scratch.borrow())
|
||||
);
|
||||
|
||||
let n_lwe: u32 = TEST_N_LWE;
|
||||
let block_size: u32 = TEST_BLOCK_SIZE;
|
||||
|
||||
let mut sk_lwe: LWESecret<Vec<u8>> = LWESecret::alloc(n_lwe.into());
|
||||
sk_lwe.fill_binary_block(block_size as usize, &mut source_xs);
|
||||
|
||||
let bdd_key_infos: BDDKeyLayout = TEST_BDD_KEY_LAYOUT;
|
||||
|
||||
let now: Instant = Instant::now();
|
||||
let bdd_key: BDDKey<Vec<u8>, Vec<u8>, BRA> = BDDKey::encrypt_sk(
|
||||
&module,
|
||||
&sk_lwe,
|
||||
&sk_glwe,
|
||||
&bdd_key_infos,
|
||||
&mut source_xa,
|
||||
&mut source_xe,
|
||||
scratch.borrow(),
|
||||
);
|
||||
let bdd_key_prepared: BDDKeyPrepared<Vec<u8>, Vec<u8>, BRA, BE> = bdd_key.prepare_alloc(&module, scratch.borrow());
|
||||
println!("BDD-KGEN: {} ms", now.elapsed().as_millis());
|
||||
|
||||
let now: Instant = Instant::now();
|
||||
c_enc_prep_debug.prepare(&module, &c_enc, &bdd_key_prepared, scratch.borrow());
|
||||
println!("CBT: {} ms", now.elapsed().as_millis());
|
||||
|
||||
c_enc_prep_debug.noise(&module, &sk_glwe_prep, a.wrapping_sub(b));
|
||||
|
||||
let now: Instant = Instant::now();
|
||||
c_enc_prep.prepare(&module, &c_enc, &bdd_key_prepared, scratch.borrow());
|
||||
println!("CBT: {} ms", now.elapsed().as_millis());
|
||||
|
||||
let start: Instant = Instant::now();
|
||||
c_enc.add(&module, &c_enc_prep, &b_enc_prep, scratch.borrow());
|
||||
|
||||
let duration: std::time::Duration = start.elapsed();
|
||||
println!("add: {} ms", duration.as_millis());
|
||||
|
||||
println!(
|
||||
"have: {}",
|
||||
c_enc.decrypt(&module, &sk_glwe_prep, scratch.borrow())
|
||||
);
|
||||
println!("want: {}", b.wrapping_add(a.wrapping_sub(b)));
|
||||
println!(
|
||||
"noise: {:?}",
|
||||
c_enc.noise(
|
||||
&module,
|
||||
&sk_glwe_prep,
|
||||
b.wrapping_add(a.wrapping_sub(b)),
|
||||
scratch.borrow()
|
||||
)
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user