refactor(tfhe): entities Clone + Debug and default parallel + serialization

This commit is contained in:
Arthur Meyre
2022-12-02 16:27:45 +01:00
committed by jborfila
parent ba984c2537
commit 8ac33a9f63
24 changed files with 29 additions and 66 deletions

View File

@@ -2,14 +2,11 @@ use crate::core_crypto::algorithms::slice_algorithms::*;
use crate::core_crypto::algorithms::*;
use crate::core_crypto::commons::crypto::secret::generators::EncryptionRandomGenerator;
use crate::core_crypto::commons::math::decomposition::DecompositionLevel;
use crate::core_crypto::commons::math::random::ByteRandomGenerator;
#[cfg(feature = "__commons_parallel")]
use crate::core_crypto::commons::math::random::ParallelByteRandomGenerator;
use crate::core_crypto::commons::math::random::{ByteRandomGenerator, ParallelByteRandomGenerator};
use crate::core_crypto::commons::math::torus::UnsignedTorus;
use crate::core_crypto::commons::traits::*;
use crate::core_crypto::entities::*;
use crate::core_crypto::specification::dispersion::DispersionParameter;
#[cfg(feature = "__commons_parallel")]
use rayon::prelude::*;
pub fn encrypt_ggsw_ciphertext<Scalar, KeyCont, OutputCont, Gen>(
@@ -89,7 +86,6 @@ pub fn encrypt_ggsw_ciphertext<Scalar, KeyCont, OutputCont, Gen>(
}
}
#[cfg(feature = "__commons_parallel")]
pub fn par_encrypt_ggsw_ciphertext<Scalar, KeyCont, OutputCont, Gen>(
glwe_secret_key: &GlweSecretKey<KeyCont>,
output: &mut GgswCiphertext<OutputCont>,

View File

@@ -1,14 +1,11 @@
use crate::core_crypto::algorithms::*;
use crate::core_crypto::commons::crypto::secret::generators::EncryptionRandomGenerator;
use crate::core_crypto::commons::math::random::ByteRandomGenerator;
#[cfg(feature = "__commons_parallel")]
use crate::core_crypto::commons::math::random::ParallelByteRandomGenerator;
use crate::core_crypto::commons::math::random::{ByteRandomGenerator, ParallelByteRandomGenerator};
use crate::core_crypto::commons::math::torus::UnsignedTorus;
use crate::core_crypto::commons::traits::*;
use crate::core_crypto::entities::*;
use crate::core_crypto::specification::dispersion::DispersionParameter;
use crate::core_crypto::specification::parameters::*;
#[cfg(feature = "__commons_parallel")]
use rayon::prelude::*;
pub fn generate_lwe_bootstrap_key<Scalar, InputKeyCont, OutputKeyCont, OutputCont, Gen>(
@@ -106,7 +103,6 @@ where
bsk
}
#[cfg(feature = "__commons_parallel")]
pub fn par_generate_lwe_bootstrap_key<Scalar, InputKeyCont, OutputKeyCont, OutputCont, Gen>(
input_lwe_secret_key: &LweSecretKey<InputKeyCont>,
output_glwe_secret_key: &GlweSecretKey<OutputKeyCont>,
@@ -311,7 +307,6 @@ mod test {
}
}
#[cfg(feature = "__commons_parallel")]
#[cfg(test)]
mod parallel_test {
use crate::core_crypto::algorithms::{

View File

@@ -5,9 +5,7 @@ use crate::core_crypto::algorithms::*;
use crate::core_crypto::commons::crypto::secret::generators::{
EncryptionRandomGenerator, SecretRandomGenerator,
};
use crate::core_crypto::commons::math::random::ByteRandomGenerator;
#[cfg(feature = "__commons_parallel")]
use crate::core_crypto::commons::math::random::ParallelByteRandomGenerator;
use crate::core_crypto::commons::math::random::{ByteRandomGenerator, ParallelByteRandomGenerator};
use crate::core_crypto::commons::math::torus::UnsignedTorus;
use crate::core_crypto::commons::numeric::UnsignedInteger;
use crate::core_crypto::commons::traits::*;
@@ -162,7 +160,6 @@ pub fn encrypt_lwe_ciphertext_list<Scalar, KeyCont, OutputCont, InputCont, Gen>(
}
}
#[cfg(feature = "__commons_parallel")]
use rayon::prelude::*;
pub fn par_encrypt_lwe_ciphertext_list<Scalar, KeyCont, OutputCont, InputCont, Gen>(
lwe_secret_key: &LweSecretKey<KeyCont>,

View File

@@ -2,15 +2,12 @@ use crate::core_crypto::algorithms::slice_algorithms::*;
use crate::core_crypto::algorithms::*;
use crate::core_crypto::commons::crypto::secret::generators::EncryptionRandomGenerator;
use crate::core_crypto::commons::math::decomposition::{DecompositionLevel, DecompositionTerm};
use crate::core_crypto::commons::math::random::ByteRandomGenerator;
#[cfg(feature = "__commons_parallel")]
use crate::core_crypto::commons::math::random::ParallelByteRandomGenerator;
use crate::core_crypto::commons::math::random::{ByteRandomGenerator, ParallelByteRandomGenerator};
use crate::core_crypto::commons::math::torus::UnsignedTorus;
use crate::core_crypto::commons::traits::*;
use crate::core_crypto::entities::*;
use crate::core_crypto::specification::dispersion::DispersionParameter;
use crate::core_crypto::specification::parameters::*;
#[cfg(feature = "__commons_parallel")]
use rayon::prelude::*;
pub fn generate_lwe_private_functional_packing_keyswitch_key<
@@ -113,7 +110,6 @@ pub fn generate_lwe_private_functional_packing_keyswitch_key<
}
}
#[cfg(feature = "__commons_parallel")]
pub fn par_generate_lwe_private_functional_packing_keyswitch_key<
Scalar,
InputKeyCont,

View File

@@ -1,8 +1,6 @@
use crate::core_crypto::algorithms::*;
use crate::core_crypto::commons::crypto::secret::generators::EncryptionRandomGenerator;
use crate::core_crypto::commons::math::random::ByteRandomGenerator;
#[cfg(feature = "__commons_parallel")]
use crate::core_crypto::commons::math::random::ParallelByteRandomGenerator;
use crate::core_crypto::commons::math::random::{ByteRandomGenerator, ParallelByteRandomGenerator};
use crate::core_crypto::commons::math::torus::UnsignedTorus;
use crate::core_crypto::commons::traits::*;
use crate::core_crypto::entities::*;
@@ -55,7 +53,6 @@ where
pk
}
#[cfg(feature = "__commons_parallel")]
pub fn par_generate_lwe_public_key<Scalar, InputKeyCont, OutputKeyCont, Gen>(
lwe_secret_key: &LweSecretKey<InputKeyCont>,
output: &mut LwePublicKey<OutputKeyCont>,
@@ -80,7 +77,6 @@ pub fn par_generate_lwe_public_key<Scalar, InputKeyCont, OutputKeyCont, Gen>(
par_encrypt_lwe_ciphertext_list(lwe_secret_key, output, &zeros, noise_parameters, generator)
}
#[cfg(feature = "__commons_parallel")]
pub fn par_allocate_and_generate_new_lwe_public_key<Scalar, InputKeyCont, Gen>(
lwe_secret_key: &LweSecretKey<InputKeyCont>,
zero_encryption_count: LwePublicKeyZeroEncryptionCount,

View File

@@ -1,8 +1,6 @@
use crate::core_crypto::algorithms::*;
use crate::core_crypto::commons::crypto::secret::generators::EncryptionRandomGenerator;
use crate::core_crypto::commons::math::random::ByteRandomGenerator;
#[cfg(feature = "__commons_parallel")]
use crate::core_crypto::commons::math::random::ParallelByteRandomGenerator;
use crate::core_crypto::commons::math::random::{ByteRandomGenerator, ParallelByteRandomGenerator};
use crate::core_crypto::commons::math::torus::UnsignedTorus;
use crate::core_crypto::commons::numeric::CastInto;
use crate::core_crypto::commons::traits::*;
@@ -17,7 +15,6 @@ use crate::core_crypto::specification::dispersion::DispersionParameter;
use crate::core_crypto::specification::parameters::*;
use concrete_fft::c64;
use dyn_stack::{DynStack, SizeOverflow, StackReq};
#[cfg(feature = "__commons_parallel")]
use rayon::prelude::*;
pub fn allocate_and_generate_new_circuit_bootstrap_lwe_pfpksk_list<
@@ -134,7 +131,6 @@ pub fn generate_circuit_bootstrap_lwe_pfpksk_list<
}
}
#[cfg(feature = "__commons_parallel")]
pub fn par_allocate_and_generate_new_circuit_bootstrap_lwe_pfpksk_list<
Scalar,
LweKeyCont,
@@ -177,7 +173,6 @@ where
cbs_pfpksk_list
}
#[cfg(feature = "__commons_parallel")]
pub fn par_generate_circuit_bootstrap_lwe_pfpksk_list<
Scalar,
OutputCont,

View File

@@ -13,7 +13,7 @@ use concrete_cuda::cuda_bind::cuda_get_number_of_gpus;
// A finer access to streams could allow for more overlapping of computations
// on a given device. We'll probably want to support it in the future, in an AdvancedCudaEngine
// for example.
#[derive(Debug, Clone)]
#[derive(Clone, Debug)]
pub struct CudaEngine {
streams: Vec<CudaStream>,
max_shared_memory: usize,

View File

@@ -1,5 +1,4 @@
use super::create_from::*;
#[cfg(feature = "__commons_parallel")]
use rayon::prelude::*;
type WrappingFunction<'data, Element, WrappingType> = fn(
@@ -17,7 +16,6 @@ type WrappingLendingIterator<'data, Element, WrappingType> = std::iter::Map<
WrappingFunction<'data, Element, WrappingType>,
>;
#[cfg(feature = "__commons_parallel")]
type ParallelWrappingLendingIterator<'data, Element, WrappingType> = rayon::iter::Map<
rayon::iter::Zip<
rayon::slice::Chunks<'data, Element>,
@@ -41,7 +39,6 @@ type WrappingLendingIteratorMut<'data, Element, WrappingType> = std::iter::Map<
WrappingFunctionMut<'data, Element, WrappingType>,
>;
#[cfg(feature = "__commons_parallel")]
type ParallelWrappingLendingIteratorMut<'data, Element, WrappingType> = rayon::iter::Map<
rayon::iter::Zip<
rayon::slice::ChunksMut<'data, Element>,
@@ -131,7 +128,6 @@ pub trait ContiguousEntityContainer: AsRef<[Self::Element]> {
.map(|(elt, meta)| Self::SelfView::<'_>::create_from(elt, meta))
}
#[cfg(feature = "__commons_parallel")]
fn par_iter<'this>(
&'this self,
) -> ParallelWrappingLendingIterator<'this, Self::Element, Self::EntityView<'this>>
@@ -223,7 +219,6 @@ pub trait ContiguousEntityContainerMut: ContiguousEntityContainer + AsMut<[Self:
.map(|(elt, meta)| Self::SelfMutView::<'_>::create_from(elt, meta))
}
#[cfg(feature = "__commons_parallel")]
fn par_iter_mut<'this>(
&'this mut self,
) -> ParallelWrappingLendingIteratorMut<'this, Self::Element, Self::EntityMutView<'this>>

View File

@@ -1,4 +1,5 @@
use crate::core_crypto::commons::numeric::Numeric;
/// A cleartext, not encoded, value
#[derive(Clone, Debug)]
pub struct Cleartext<T: Numeric>(pub T);

View File

@@ -2,6 +2,7 @@ use crate::core_crypto::commons::traits::*;
use crate::core_crypto::entities::*;
use crate::core_crypto::specification::parameters::*;
#[derive(Clone, Debug)]
pub struct GgswCiphertext<C: Container> {
data: C,
polynomial_size: PolynomialSize,

View File

@@ -2,6 +2,7 @@ use crate::core_crypto::commons::traits::*;
use crate::core_crypto::entities::*;
use crate::core_crypto::specification::parameters::*;
#[derive(Clone, Debug)]
pub struct GgswCiphertextList<C: Container> {
data: C,
glwe_size: GlweSize,

View File

@@ -2,6 +2,7 @@ use crate::core_crypto::commons::traits::*;
use crate::core_crypto::entities::*;
use crate::core_crypto::specification::parameters::*;
#[derive(Clone, Debug)]
pub struct GlweBody<C: Container> {
data: C,
}
@@ -30,6 +31,7 @@ impl<C: ContainerMut> GlweBody<C> {
}
}
#[derive(Clone, Debug)]
pub struct GlweMask<C: Container> {
data: C,
polynomial_size: PolynomialSize,

View File

@@ -2,6 +2,7 @@ use crate::core_crypto::commons::traits::*;
use crate::core_crypto::entities::*;
use crate::core_crypto::specification::parameters::*;
#[derive(Clone, Debug)]
pub struct GlweCiphertextList<C: Container> {
data: C,
polynomial_size: PolynomialSize,

View File

@@ -4,6 +4,7 @@ use crate::core_crypto::specification::parameters::*;
// An LweBootstrapKey is literally a GgswCiphertextList, so we wrap a GgswCiphetextList and use
// Deref to have access to all the primitives of the GgswCiphertextList easily
#[derive(Clone, Debug)]
pub struct LweBootstrapKey<C: Container> {
ggsw_list: GgswCiphertextList<C>,
}

View File

@@ -1,7 +1,9 @@
use crate::core_crypto::commons::traits::*;
use crate::core_crypto::specification::parameters::*;
#[derive(Clone, Debug)]
pub struct LweBody<T>(pub T);
#[derive(Clone, Debug)]
pub struct LweMask<C: Container> {
data: C,
}

View File

@@ -2,6 +2,7 @@ use crate::core_crypto::commons::traits::*;
use crate::core_crypto::entities::*;
use crate::core_crypto::specification::parameters::*;
#[derive(Clone, Debug)]
pub struct LwePrivateFunctionalPackingKeyswitchKey<C: Container> {
data: C,
decomp_base_log: DecompositionBaseLog,

View File

@@ -2,7 +2,7 @@ use crate::core_crypto::commons::traits::*;
use crate::core_crypto::entities::*;
use crate::core_crypto::specification::parameters::*;
#[derive(Clone, Copy, Debug)]
#[derive(Clone, Debug)]
pub struct LwePrivateFunctionalPackingKeyswitchKeyList<C: Container> {
data: C,
decomp_base_log: DecompositionBaseLog,

View File

@@ -2,6 +2,7 @@ use crate::core_crypto::commons::traits::*;
use crate::core_crypto::entities::*;
use crate::core_crypto::specification::parameters::*;
#[derive(Clone, Debug)]
pub struct PlaintextList<C: Container> {
data: C,
}

View File

@@ -2,6 +2,7 @@ use crate::core_crypto::commons::traits::*;
use crate::core_crypto::specification::parameters::*;
use std::ops::{Deref, DerefMut};
#[derive(Clone, Debug)]
pub struct Polynomial<C: Container> {
data: C,
}

View File

@@ -2,6 +2,7 @@ use crate::core_crypto::commons::traits::*;
use crate::core_crypto::entities::*;
use crate::core_crypto::specification::parameters::*;
#[derive(Clone, Debug)]
pub struct PolynomialList<C: Container> {
data: C,
polynomial_size: PolynomialSize,

View File

@@ -4,10 +4,8 @@ use crate::core_crypto::algorithms::extract_lwe_sample_from_glwe_ciphertext;
use crate::core_crypto::algorithms::polynomial_algorithms::*;
use crate::core_crypto::commons::math::torus::UnsignedTorus;
use crate::core_crypto::commons::numeric::CastInto;
#[cfg(feature = "backend_fft_serialization")]
use crate::core_crypto::commons::traits::ContainerOwned;
use crate::core_crypto::commons::traits::{
Container, ContiguousEntityContainer, ContiguousEntityContainerMut, Split,
Container, ContainerOwned, ContiguousEntityContainer, ContiguousEntityContainerMut, Split,
};
use crate::core_crypto::commons::utils::izip;
use crate::core_crypto::entities::*;
@@ -19,12 +17,8 @@ use aligned_vec::{avec, ABox, CACHELINE_ALIGN};
use concrete_fft::c64;
use dyn_stack::{DynStack, ReborrowMut, SizeOverflow, StackReq};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(
feature = "backend_fft_serialization",
derive(serde::Serialize, serde::Deserialize),
serde(bound(deserialize = "C: ContainerOwned"))
)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(bound(deserialize = "C: ContainerOwned"))]
pub struct FourierLweBootstrapKey<C: Container<Element = c64>> {
fourier: FourierPolynomialList<C>,
key_size: LweDimension,

View File

@@ -6,10 +6,8 @@ use super::super::math::polynomial::{FourierPolynomialUninitMutView, FourierPoly
use super::super::{as_mut_uninit, assume_init_mut};
use crate::core_crypto::commons::math::decomposition::{DecompositionLevel, SignedDecomposer};
use crate::core_crypto::commons::math::torus::UnsignedTorus;
#[cfg(feature = "backend_fft_serialization")]
use crate::core_crypto::commons::traits::ContainerOwned;
use crate::core_crypto::commons::traits::{
Container, ContiguousEntityContainer, ContiguousEntityContainerMut, Split,
Container, ContainerOwned, ContiguousEntityContainer, ContiguousEntityContainerMut, Split,
};
use crate::core_crypto::commons::utils::izip;
use crate::core_crypto::entities::*;
@@ -26,12 +24,8 @@ use core::arch::x86::*;
use core::arch::x86_64::*;
/// A GGSW ciphertext in the Fourier domain.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(
feature = "backend_fft_serialization",
derive(serde::Serialize, serde::Deserialize),
serde(bound(deserialize = "C: ContainerOwned"))
)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(bound(deserialize = "C: ContainerOwned"))]
pub struct FourierGgswCiphertext<C: Container<Element = c64>> {
fourier: FourierPolynomialList<C>,
glwe_size: GlweSize,

View File

@@ -880,7 +880,7 @@ pub fn circuit_bootstrap_boolean_vertical_packing<Scalar: UnsignedTorus + CastIn
lwe_in,
ggsw_res.as_mut_view(),
DeltaLog(Scalar::BITS - 1),
pfpksk_list,
pfpksk_list.as_view(),
fft,
stack.rb_mut(),
);

View File

@@ -5,9 +5,7 @@ use super::polynomial::{
};
use crate::core_crypto::commons::math::torus::UnsignedTorus;
use crate::core_crypto::commons::numeric::CastInto;
use crate::core_crypto::commons::traits::Container;
#[cfg(feature = "backend_fft_serialization")]
use crate::core_crypto::commons::traits::ContainerOwned;
use crate::core_crypto::commons::traits::{Container, ContainerOwned};
use crate::core_crypto::commons::utils::izip;
use crate::core_crypto::entities::*;
use crate::core_crypto::prelude::PolynomialSize;
@@ -347,8 +345,6 @@ impl<'a> FftView<'a> {
}
/// Serializes data in the Fourier domain.
#[cfg(feature = "backend_fft_serialization")]
#[cfg_attr(docsrs, doc(cfg(feature = "backend_fft_serialization")))]
pub fn serialize_fourier_buffer<S: serde::Serializer>(
self,
serializer: S,
@@ -358,8 +354,6 @@ impl<'a> FftView<'a> {
}
/// Deserializes data in the Fourier domain
#[cfg(feature = "backend_fft_serialization")]
#[cfg_attr(docsrs, doc(cfg(feature = "backend_fft_serialization")))]
pub fn deserialize_fourier_buffer<'de, D: serde::Deserializer<'de>>(
self,
deserializer: D,
@@ -530,7 +524,6 @@ pub struct FourierPolynomialList<C: Container<Element = c64>> {
pub polynomial_size: PolynomialSize,
}
#[cfg(feature = "backend_fft_serialization")]
impl<C: Container<Element = c64>> serde::Serialize for FourierPolynomialList<C> {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
fn serialize_impl<S: serde::Serializer>(
@@ -580,7 +573,6 @@ impl<C: Container<Element = c64>> serde::Serialize for FourierPolynomialList<C>
}
}
#[cfg(feature = "backend_fft_serialization")]
impl<'de, C: ContainerOwned<Element = c64>> serde::Deserialize<'de> for FourierPolynomialList<C> {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
use std::marker::PhantomData;