Skip to content

Commit

Permalink
refactor(all): remove some manual impl of versionize
Browse files Browse the repository at this point in the history
  • Loading branch information
nsarlin-zama committed Oct 9, 2024
1 parent 961962c commit 9dc7e3f
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 749 deletions.
32 changes: 2 additions & 30 deletions tfhe-zk-pok/src/proofs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,12 @@ use crate::serialization::{
};
use core::ops::{Index, IndexMut};
use rand::{Rng, RngCore};
use tfhe_versionable::{Unversionize, Versionize, VersionizeOwned};
use tfhe_versionable::Versionize;

#[derive(Clone, Copy, Debug, serde::Serialize, serde::Deserialize)]
#[derive(Clone, Copy, Debug, serde::Serialize, serde::Deserialize, Versionize)]
#[repr(transparent)]
pub(crate) struct OneBased<T: ?Sized>(T);

// TODO: these impl could be removed by adding support for `repr(transparent)` in tfhe-versionable
impl<T: Versionize> Versionize for OneBased<T> {
type Versioned<'vers>
= T::Versioned<'vers>
where
T: 'vers;

fn versionize(&self) -> Self::Versioned<'_> {
self.0.versionize()
}
}

impl<T: VersionizeOwned> VersionizeOwned for OneBased<T> {
type VersionedOwned = T::VersionedOwned;

fn versionize_owned(self) -> Self::VersionedOwned {
self.0.versionize_owned()
}
}

impl<T: Unversionize> Unversionize for OneBased<T> {
fn unversionize(
versioned: Self::VersionedOwned,
) -> Result<Self, tfhe_versionable::UnversionizeError> {
Ok(Self(T::unversionize(versioned)?))
}
}

/// The proving scheme is available in 2 versions, one that puts more load on the prover and one
/// that puts more load on the verifier
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
Expand Down
57 changes: 3 additions & 54 deletions tfhe-zk-pok/src/proofs/pke.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// TODO: refactor copy-pasted code in proof/verify

use crate::backward_compatibility::{
PKEv1CompressedProofVersions, PKEv1ProofVersions, SerializablePKEv1PublicParamsVersions,
};
use crate::backward_compatibility::{PKEv1CompressedProofVersions, PKEv1ProofVersions};
use crate::serialization::{
try_vec_to_array, InvalidSerializedAffineError, InvalidSerializedPublicParamsError,
SerializableGroupElements, SerializablePKEv1PublicParams,
Expand All @@ -13,14 +11,12 @@ use core::marker::PhantomData;

use rayon::prelude::*;
use serde::{Deserialize, Serialize};
use std::error::Error;
use tfhe_versionable::{UnversionizeError, VersionsDispatch};

fn bit_iter(x: u64, nbits: u32) -> impl Iterator<Item = bool> {
(0..nbits).map(move |idx| ((x >> idx) & 1) != 0)
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[derive(Clone, Debug, Serialize, Deserialize, Versionize)]
#[serde(
try_from = "SerializablePKEv1PublicParams",
into = "SerializablePKEv1PublicParams",
Expand All @@ -29,6 +25,7 @@ fn bit_iter(x: u64, nbits: u32) -> impl Iterator<Item = bool> {
serialize = "PublicParams<G>: Into<SerializablePKEv1PublicParams>"
)
)]
#[versionize(try_convert = SerializablePKEv1PublicParams)]
pub struct PublicParams<G: Curve> {
pub(crate) g_lists: GroupElements<G>,
pub(crate) big_d: usize,
Expand All @@ -48,54 +45,6 @@ pub struct PublicParams<G: Curve> {
pub(crate) hash_w: [u8; HASH_METADATA_LEN_BYTES],
}

// Manual impl of Versionize because TryFrom + generics is currently badly handled by the proc macro
impl<G: Curve> Versionize for PublicParams<G>
where
Self: Clone,
SerializablePKEv1PublicParamsVersions: VersionsDispatch<SerializablePKEv1PublicParams>,
GroupElements<G>: Into<SerializableGroupElements>,
{
type Versioned<'vers>
= <SerializablePKEv1PublicParamsVersions as VersionsDispatch<
SerializablePKEv1PublicParams,
>>::Owned
where
G: 'vers;
fn versionize(&self) -> Self::Versioned<'_> {
VersionizeOwned::versionize_owned(SerializablePKEv1PublicParams::from(self.to_owned()))
}
}

impl<G: Curve> VersionizeOwned for PublicParams<G>
where
Self: Clone,
SerializablePKEv1PublicParamsVersions: VersionsDispatch<SerializablePKEv1PublicParams>,
GroupElements<G>: Into<SerializableGroupElements>,
{
type VersionedOwned = <SerializablePKEv1PublicParamsVersions as VersionsDispatch<
SerializablePKEv1PublicParams,
>>::Owned;
fn versionize_owned(self) -> Self::VersionedOwned {
VersionizeOwned::versionize_owned(SerializablePKEv1PublicParams::from(self.to_owned()))
}
}

impl<G: Curve, E> Unversionize for PublicParams<G>
where
Self: Clone,
SerializablePKEv1PublicParamsVersions: VersionsDispatch<SerializablePKEv1PublicParams>,
GroupElements<G>: Into<SerializableGroupElements>,
Self: TryFrom<SerializablePKEv1PublicParams, Error = E>,
E: Error + Send + Sync + 'static,
{
fn unversionize(versioned: Self::VersionedOwned) -> Result<Self, UnversionizeError> {
SerializablePKEv1PublicParams::unversionize(versioned).and_then(|value| {
TryInto::<Self>::try_into(value)
.map_err(|e| UnversionizeError::conversion("SerializablePublicParams", e))
})
}
}

impl<G: Curve> Compressible for PublicParams<G>
where
GroupElements<G>: Compressible<
Expand Down
57 changes: 3 additions & 54 deletions tfhe-zk-pok/src/proofs/pke_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
#![allow(non_snake_case)]

use super::*;
use crate::backward_compatibility::{
PKEv2CompressedProofVersions, PKEv2ProofVersions, SerializablePKEv2PublicParamsVersions,
};
use crate::backward_compatibility::{PKEv2CompressedProofVersions, PKEv2ProofVersions};
use crate::four_squares::*;
use crate::serialization::{
try_vec_to_array, InvalidSerializedAffineError, InvalidSerializedPublicParamsError,
Expand All @@ -13,15 +11,13 @@ use crate::serialization::{
use core::marker::PhantomData;
use rayon::prelude::*;
use serde::{Deserialize, Serialize};
use std::error::Error;
use tfhe_versionable::{UnversionizeError, VersionsDispatch};

fn bit_iter(x: u64, nbits: u32) -> impl Iterator<Item = bool> {
(0..nbits).map(move |idx| ((x >> idx) & 1) != 0)
}

/// The CRS of the zk scheme
#[derive(Clone, Debug, Serialize, Deserialize)]
#[derive(Clone, Debug, Serialize, Deserialize, Versionize)]
#[serde(
try_from = "SerializablePKEv2PublicParams",
into = "SerializablePKEv2PublicParams",
Expand All @@ -30,6 +26,7 @@ fn bit_iter(x: u64, nbits: u32) -> impl Iterator<Item = bool> {
serialize = "PublicParams<G>: Into<SerializablePKEv2PublicParams>"
)
)]
#[versionize(convert = SerializablePKEv2PublicParams)]
pub struct PublicParams<G: Curve> {
pub(crate) g_lists: GroupElements<G>,
pub(crate) D: usize,
Expand All @@ -55,54 +52,6 @@ pub struct PublicParams<G: Curve> {
pub(crate) hash_chi: [u8; HASH_METADATA_LEN_BYTES],
}

// Manual impl of Versionize because TryFrom + generics is currently badly handled by the proc macro
impl<G: Curve> Versionize for PublicParams<G>
where
Self: Clone,
SerializablePKEv2PublicParamsVersions: VersionsDispatch<SerializablePKEv2PublicParams>,
GroupElements<G>: Into<SerializableGroupElements>,
{
type Versioned<'vers>
= <SerializablePKEv2PublicParamsVersions as VersionsDispatch<
SerializablePKEv2PublicParams,
>>::Owned
where
G: 'vers;
fn versionize(&self) -> Self::Versioned<'_> {
VersionizeOwned::versionize_owned(SerializablePKEv2PublicParams::from(self.to_owned()))
}
}

impl<G: Curve> VersionizeOwned for PublicParams<G>
where
Self: Clone,
SerializablePKEv2PublicParamsVersions: VersionsDispatch<SerializablePKEv2PublicParams>,
GroupElements<G>: Into<SerializableGroupElements>,
{
type VersionedOwned = <SerializablePKEv2PublicParamsVersions as VersionsDispatch<
SerializablePKEv2PublicParams,
>>::Owned;
fn versionize_owned(self) -> Self::VersionedOwned {
VersionizeOwned::versionize_owned(SerializablePKEv2PublicParams::from(self.to_owned()))
}
}

impl<E, G: Curve> Unversionize for PublicParams<G>
where
Self: Clone,
SerializablePKEv2PublicParamsVersions: VersionsDispatch<SerializablePKEv2PublicParams>,
GroupElements<G>: Into<SerializableGroupElements>,
Self: TryFrom<SerializablePKEv2PublicParams, Error = E>,
E: Error + Send + Sync + 'static,
{
fn unversionize(versioned: Self::VersionedOwned) -> Result<Self, UnversionizeError> {
SerializablePKEv2PublicParams::unversionize(versioned).and_then(|value| {
TryInto::<Self>::try_into(value)
.map_err(|e| UnversionizeError::conversion("SerializablePublicParams", e))
})
}
}

impl<G: Curve> Compressible for PublicParams<G>
where
GroupElements<G>: Compressible<
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
use concrete_fft::c64;
use serde::{Deserialize, Serialize};
use tfhe_versionable::{UnversionizeError, VersionsDispatch};
use tfhe_versionable::VersionsDispatch;

use crate::core_crypto::prelude::{
Container, FourierLweMultiBitBootstrapKey, FourierLweMultiBitBootstrapKeyVersion,
FourierLweMultiBitBootstrapKeyVersionOwned, IntoContainerOwned, LweMultiBitBootstrapKey,
UnsignedInteger,
Container, FourierLweMultiBitBootstrapKey, LweMultiBitBootstrapKey, UnsignedInteger,
};

#[derive(VersionsDispatch)]
Expand All @@ -16,42 +13,7 @@ where
V0(LweMultiBitBootstrapKey<C>),
}

#[derive(Serialize)]
#[cfg_attr(tfhe_lints, allow(tfhe_lints::serialize_without_versionize))]
pub enum FourierLweMultiBitBootstrapKeyVersioned<'vers> {
V0(FourierLweMultiBitBootstrapKeyVersion<'vers>),
}

impl<'vers, C: Container<Element = c64>> From<&'vers FourierLweMultiBitBootstrapKey<C>>
for FourierLweMultiBitBootstrapKeyVersioned<'vers>
{
fn from(value: &'vers FourierLweMultiBitBootstrapKey<C>) -> Self {
Self::V0(value.into())
}
}

#[derive(Serialize, Deserialize)]
#[cfg_attr(tfhe_lints, allow(tfhe_lints::serialize_without_versionize))]
pub enum FourierLweMultiBitBootstrapKeyVersionedOwned {
V0(FourierLweMultiBitBootstrapKeyVersionOwned),
}

impl<C: Container<Element = c64>> From<FourierLweMultiBitBootstrapKey<C>>
for FourierLweMultiBitBootstrapKeyVersionedOwned
{
fn from(value: FourierLweMultiBitBootstrapKey<C>) -> Self {
Self::V0(value.into())
}
}

impl<C: IntoContainerOwned<Element = c64>> TryFrom<FourierLweMultiBitBootstrapKeyVersionedOwned>
for FourierLweMultiBitBootstrapKey<C>
{
type Error = UnversionizeError;

fn try_from(value: FourierLweMultiBitBootstrapKeyVersionedOwned) -> Result<Self, Self::Error> {
match value {
FourierLweMultiBitBootstrapKeyVersionedOwned::V0(v0) => Self::try_from(v0),
}
}
#[derive(VersionsDispatch)]
pub enum FourierLweMultiBitBootstrapKeyVersions<C: Container<Element = c64>> {
V0(FourierLweMultiBitBootstrapKey<C>),
}
90 changes: 7 additions & 83 deletions tfhe/src/core_crypto/backward_compatibility/fft_impl/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
use tfhe_versionable::{UnversionizeError, VersionsDispatch};
use tfhe_versionable::VersionsDispatch;

use aligned_vec::ABox;
use concrete_fft::c64;
use serde::{Deserialize, Serialize};

use crate::core_crypto::fft_impl::fft64::crypto::bootstrap::{
FourierLweBootstrapKeyVersion, FourierLweBootstrapKeyVersionOwned,
};
use crate::core_crypto::fft_impl::fft64::crypto::ggsw::{
FourierGgswCiphertextVersion, FourierGgswCiphertextVersionOwned,
};
use crate::core_crypto::fft_impl::fft64::math::fft::FourierPolynomialList;
use crate::core_crypto::prelude::{
Container, Fourier128GgswCiphertext, Fourier128LweBootstrapKey, FourierGgswCiphertext,
Expand Down Expand Up @@ -66,84 +60,14 @@ impl<C: IntoContainerOwned<Element = c64>> From<FourierPolynomialListVersionedOw
}
}

#[derive(Serialize)]
#[cfg_attr(tfhe_lints, allow(tfhe_lints::serialize_without_versionize))]
pub enum FourierLweBootstrapKeyVersioned<'vers> {
V0(FourierLweBootstrapKeyVersion<'vers>),
}

impl<'vers, C: Container<Element = c64>> From<&'vers FourierLweBootstrapKey<C>>
for FourierLweBootstrapKeyVersioned<'vers>
{
fn from(value: &'vers FourierLweBootstrapKey<C>) -> Self {
Self::V0(value.into())
}
}

#[derive(Serialize, Deserialize)]
#[cfg_attr(tfhe_lints, allow(tfhe_lints::serialize_without_versionize))]
pub enum FourierLweBootstrapKeyVersionedOwned {
V0(FourierLweBootstrapKeyVersionOwned),
}

impl<C: Container<Element = c64>> From<FourierLweBootstrapKey<C>>
for FourierLweBootstrapKeyVersionedOwned
{
fn from(value: FourierLweBootstrapKey<C>) -> Self {
Self::V0(value.into())
}
}

impl<C: IntoContainerOwned<Element = c64>> TryFrom<FourierLweBootstrapKeyVersionedOwned>
for FourierLweBootstrapKey<C>
{
type Error = UnversionizeError;

fn try_from(value: FourierLweBootstrapKeyVersionedOwned) -> Result<Self, Self::Error> {
match value {
FourierLweBootstrapKeyVersionedOwned::V0(v0) => Self::try_from(v0),
}
}
}

#[derive(Serialize)]
#[cfg_attr(tfhe_lints, allow(tfhe_lints::serialize_without_versionize))]
pub enum FourierGgswCiphertextVersioned<'vers> {
V0(FourierGgswCiphertextVersion<'vers>),
}

impl<'vers, C: Container<Element = c64>> From<&'vers FourierGgswCiphertext<C>>
for FourierGgswCiphertextVersioned<'vers>
{
fn from(value: &'vers FourierGgswCiphertext<C>) -> Self {
Self::V0(value.into())
}
}

#[derive(Serialize, Deserialize)]
#[cfg_attr(tfhe_lints, allow(tfhe_lints::serialize_without_versionize))]
pub enum FourierGgswCiphertextVersionedOwned {
V0(FourierGgswCiphertextVersionOwned),
}

impl<C: Container<Element = c64>> From<FourierGgswCiphertext<C>>
for FourierGgswCiphertextVersionedOwned
{
fn from(value: FourierGgswCiphertext<C>) -> Self {
Self::V0(value.into())
}
#[derive(VersionsDispatch)]
pub enum FourierLweBootstrapKeyVersions<C: Container<Element = c64>> {
V0(FourierLweBootstrapKey<C>),
}

impl<C: IntoContainerOwned<Element = c64>> TryFrom<FourierGgswCiphertextVersionedOwned>
for FourierGgswCiphertext<C>
{
type Error = UnversionizeError;

fn try_from(value: FourierGgswCiphertextVersionedOwned) -> Result<Self, Self::Error> {
match value {
FourierGgswCiphertextVersionedOwned::V0(v0) => Self::try_from(v0),
}
}
#[derive(VersionsDispatch)]
pub enum FourierGgswCiphertextVersions<C: Container<Element = c64>> {
V0(FourierGgswCiphertext<C>),
}

#[derive(VersionsDispatch)]
Expand Down
Loading

0 comments on commit 9dc7e3f

Please sign in to comment.