Skip to content

Commit

Permalink
fix(versionable): compatibility between "convert" and generics
Browse files Browse the repository at this point in the history
  • Loading branch information
nsarlin-zama committed Oct 7, 2024
1 parent 620b35b commit 8cfa7b7
Show file tree
Hide file tree
Showing 8 changed files with 642 additions and 202 deletions.
40 changes: 6 additions & 34 deletions tfhe-zk-pok/src/curve_api/bls12_381.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,13 @@ fn bigint_to_le_bytes(x: [u64; 6]) -> [u8; 6 * 8] {
mod g1 {
use tfhe_versionable::Versionize;

use crate::backward_compatibility::SerializableG1AffineVersions;
use crate::serialization::{InvalidSerializedAffineError, SerializableG1Affine};

use super::*;

#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Hash, Versionize)]
#[serde(try_from = "SerializableG1Affine", into = "SerializableG1Affine")]
#[versionize(
SerializableG1AffineVersions,
try_from = "SerializableG1Affine",
into = "SerializableG1Affine"
)]
#[versionize(try_from = "SerializableG1Affine", into = "SerializableG1Affine")]
#[repr(transparent)]
pub struct G1Affine {
pub(crate) inner: ark_bls12_381::g1::G1Affine,
Expand Down Expand Up @@ -99,11 +94,7 @@ mod g1 {

#[derive(Copy, Clone, PartialEq, Eq, Serialize, Deserialize, Hash, Versionize)]
#[serde(try_from = "SerializableG1Affine", into = "SerializableG1Affine")]
#[versionize(
SerializableG1AffineVersions,
try_from = "SerializableG1Affine",
into = "SerializableG1Affine"
)]
#[versionize(try_from = "SerializableG1Affine", into = "SerializableG1Affine")]
#[repr(transparent)]
pub struct G1 {
pub(crate) inner: ark_bls12_381::G1Projective,
Expand Down Expand Up @@ -264,18 +255,13 @@ mod g1 {
mod g2 {
use tfhe_versionable::Versionize;

use crate::backward_compatibility::SerializableG2AffineVersions;
use crate::serialization::{InvalidSerializedAffineError, SerializableG2Affine};

use super::*;

#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Hash, Versionize)]
#[serde(try_from = "SerializableG2Affine", into = "SerializableG2Affine")]
#[versionize(
SerializableG2AffineVersions,
try_from = "SerializableG2Affine",
into = "SerializableG2Affine"
)]
#[versionize(try_from = "SerializableG2Affine", into = "SerializableG2Affine")]
#[repr(transparent)]
pub struct G2Affine {
pub(crate) inner: ark_bls12_381::g2::G2Affine,
Expand Down Expand Up @@ -328,11 +314,7 @@ mod g2 {

#[derive(Copy, Clone, PartialEq, Eq, Serialize, Deserialize, Hash, Versionize)]
#[serde(try_from = "SerializableG2Affine", into = "SerializableG2Affine")]
#[versionize(
SerializableG2AffineVersions,
try_from = "SerializableG2Affine",
into = "SerializableG2Affine"
)]
#[versionize(try_from = "SerializableG2Affine", into = "SerializableG2Affine")]
#[repr(transparent)]
pub struct G2 {
pub(crate) inner: ark_bls12_381::G2Projective,
Expand Down Expand Up @@ -539,7 +521,6 @@ mod g2 {
}

mod gt {
use crate::backward_compatibility::SerializableFp12Versions;
use crate::serialization::InvalidArraySizeError;

use super::*;
Expand All @@ -548,11 +529,7 @@ mod gt {

#[derive(Copy, Clone, PartialEq, Eq, Serialize, Deserialize, Versionize, Hash)]
#[serde(try_from = "SerializableFp12", into = "SerializableFp12")]
#[versionize(
SerializableFp12Versions,
try_from = "SerializableFp12",
into = "SerializableFp12"
)]
#[versionize(try_from = "SerializableFp12", into = "SerializableFp12")]
#[repr(transparent)]
pub struct Gt {
inner: ark_ec::pairing::PairingOutput<ark_bls12_381::Bls12_381>,
Expand Down Expand Up @@ -697,7 +674,6 @@ mod gt {
}

mod zp {
use crate::backward_compatibility::SerializableFpVersions;
use crate::serialization::InvalidArraySizeError;

use super::*;
Expand Down Expand Up @@ -741,11 +717,7 @@ mod zp {

#[derive(Copy, Clone, PartialEq, Eq, Serialize, Deserialize, Versionize, Hash, Zeroize)]
#[serde(try_from = "SerializableFp", into = "SerializableFp")]
#[versionize(
SerializableFpVersions,
try_from = "SerializableFp",
into = "SerializableFp"
)]
#[versionize(try_from = "SerializableFp", into = "SerializableFp")]
#[repr(transparent)]
pub struct Zp {
pub(crate) inner: ark_bls12_381::Fr,
Expand Down
41 changes: 6 additions & 35 deletions tfhe-zk-pok/src/curve_api/bls12_446.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,13 @@ fn bigint_to_le_bytes(x: [u64; 7]) -> [u8; 7 * 8] {
mod g1 {
use tfhe_versionable::Versionize;

use crate::backward_compatibility::SerializableG1AffineVersions;
use crate::serialization::{InvalidSerializedAffineError, SerializableG1Affine};

use super::*;

#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Hash, Versionize)]
#[serde(try_from = "SerializableG1Affine", into = "SerializableG1Affine")]
#[versionize(
SerializableG1AffineVersions,
try_from = "SerializableG1Affine",
into = "SerializableG1Affine"
)]
#[versionize(try_from = "SerializableG1Affine", into = "SerializableG1Affine")]
#[repr(transparent)]
pub struct G1Affine {
pub(crate) inner: crate::curve_446::g1::G1Affine,
Expand Down Expand Up @@ -101,11 +96,7 @@ mod g1 {

#[derive(Copy, Clone, PartialEq, Eq, Serialize, Deserialize, Hash, Versionize)]
#[serde(try_from = "SerializableG1Affine", into = "SerializableG1Affine")]
#[versionize(
SerializableG1AffineVersions,
try_from = "SerializableG1Affine",
into = "SerializableG1Affine"
)]
#[versionize(try_from = "SerializableG1Affine", into = "SerializableG1Affine")]
#[repr(transparent)]
pub struct G1 {
pub(crate) inner: crate::curve_446::g1::G1Projective,
Expand Down Expand Up @@ -267,19 +258,14 @@ mod g1 {
mod g2 {
use tfhe_versionable::Versionize;

use crate::backward_compatibility::SerializableG2AffineVersions;
use crate::serialization::SerializableG2Affine;

use super::*;
use crate::serialization::InvalidSerializedAffineError;

#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Hash, Versionize)]
#[serde(try_from = "SerializableG2Affine", into = "SerializableG2Affine")]
#[versionize(
SerializableG2AffineVersions,
try_from = "SerializableG2Affine",
into = "SerializableG2Affine"
)]
#[versionize(try_from = "SerializableG2Affine", into = "SerializableG2Affine")]
#[repr(transparent)]
pub struct G2Affine {
pub(crate) inner: crate::curve_446::g2::G2Affine,
Expand Down Expand Up @@ -423,11 +409,7 @@ mod g2 {

#[derive(Copy, Clone, PartialEq, Eq, Serialize, Deserialize, Hash, Versionize)]
#[serde(try_from = "SerializableG2Affine", into = "SerializableG2Affine")]
#[versionize(
SerializableG2AffineVersions,
try_from = "SerializableG2Affine",
into = "SerializableG2Affine"
)]
#[versionize(try_from = "SerializableG2Affine", into = "SerializableG2Affine")]
#[repr(transparent)]
pub struct G2 {
pub(crate) inner: crate::curve_446::g2::G2Projective,
Expand Down Expand Up @@ -633,7 +615,6 @@ mod g2 {
}

mod gt {
use crate::backward_compatibility::SerializableFp12Versions;
use crate::curve_446::{Fq, Fq12, Fq2};
use crate::serialization::InvalidSerializedAffineError;

Expand Down Expand Up @@ -812,11 +793,7 @@ mod gt {

#[derive(Copy, Clone, PartialEq, Eq, Serialize, Deserialize, Versionize, Hash)]
#[serde(try_from = "SerializableFp12", into = "SerializableFp12")]
#[versionize(
SerializableFp12Versions,
try_from = "SerializableFp12",
into = "SerializableFp12"
)]
#[versionize(try_from = "SerializableFp12", into = "SerializableFp12")]
#[repr(transparent)]
pub struct Gt {
pub(crate) inner: ark_ec::pairing::PairingOutput<crate::curve_446::Bls12_446>,
Expand Down Expand Up @@ -959,8 +936,6 @@ mod gt {
}

mod zp {
use crate::backward_compatibility::SerializableFpVersions;

use super::*;
use crate::serialization::InvalidArraySizeError;
use ark_ff::Fp;
Expand Down Expand Up @@ -1003,11 +978,7 @@ mod zp {

#[derive(Copy, Clone, PartialEq, Eq, Serialize, Deserialize, Versionize, Hash, Zeroize)]
#[serde(try_from = "SerializableFp", into = "SerializableFp")]
#[versionize(
SerializableFpVersions,
try_from = "SerializableFp",
into = "SerializableFp"
)]
#[versionize(try_from = "SerializableFp", into = "SerializableFp")]
#[repr(transparent)]
pub struct Zp {
pub inner: crate::curve_446::Fr,
Expand Down
11 changes: 6 additions & 5 deletions utils/tfhe-versionable-derive/src/associated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ use syn::{
};

use crate::{
add_lifetime_bound, add_trait_where_clause, add_where_lifetime_bound, extend_where_clause,
parse_const_str, DESERIALIZE_TRAIT_NAME, LIFETIME_NAME, SERIALIZE_TRAIT_NAME,
add_lifetime_param, add_trait_where_clause, add_where_lifetime_bound_to_generics,
extend_where_clause, parse_const_str, DESERIALIZE_TRAIT_NAME, LIFETIME_NAME,
SERIALIZE_TRAIT_NAME,
};

/// Generates an impl block for the From trait. This will be:
Expand Down Expand Up @@ -116,7 +117,7 @@ pub(crate) trait AssociatedType: Sized {
let mut generics = self.orig_type_generics().clone();
if let AssociatedTypeKind::Ref(opt_lifetime) = &self.kind() {
if let Some(lifetime) = opt_lifetime {
add_lifetime_bound(&mut generics, lifetime);
add_lifetime_param(&mut generics, lifetime);
}
add_trait_where_clause(&mut generics, self.inner_types()?, Self::REF_BOUNDS)?;
} else {
Expand Down Expand Up @@ -214,8 +215,8 @@ impl<T: AssociatedType> AssociatingTrait<T> {
let mut ref_type_generics = self.ref_type.orig_type_generics().clone();
// If the original type has some generics, we need to add a lifetime bound on them
if let Some(lifetime) = self.ref_type.lifetime() {
add_lifetime_bound(&mut ref_type_generics, lifetime);
add_where_lifetime_bound(&mut ref_type_generics, lifetime);
add_lifetime_param(&mut ref_type_generics, lifetime);
add_where_lifetime_bound_to_generics(&mut ref_type_generics, lifetime);
}

let (impl_generics, orig_generics, where_clause) = generics.split_for_impl();
Expand Down
Loading

0 comments on commit 8cfa7b7

Please sign in to comment.