Skip to content

Commit

Permalink
feat(zk): Versionize ProvenCompactCiphertextList
Browse files Browse the repository at this point in the history
  • Loading branch information
nsarlin-zama committed Aug 19, 2024
1 parent 9c95ed1 commit 3148b87
Show file tree
Hide file tree
Showing 14 changed files with 195 additions and 65 deletions.
1 change: 1 addition & 0 deletions tfhe-zk-pok/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ sha3 = "0.10.8"
serde = { version = "~1.0", features = ["derive"] }
zeroize = "1.7.0"
num-bigint = "0.4.5"
tfhe-versionable = { version = "0.1.0", path = "../utils/tfhe-versionable" } # TODO update to latest after rebase

[dev-dependencies]
serde_json = "~1.0"
7 changes: 5 additions & 2 deletions tfhe-zk-pok/src/curve_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
use core::fmt;
use core::ops::{Add, AddAssign, Div, Mul, Neg, Sub, SubAssign};
use serde::{Deserialize, Serialize};
use tfhe_versionable::NotVersioned;

use crate::serialization::{SerializableAffine, SerializableFp, SerializableFp12, SerializableFp2};

Expand Down Expand Up @@ -412,9 +413,11 @@ impl PairingGroupOps<bls12_446::Zp, bls12_446::G1, bls12_446::G2> for bls12_446:
}
}

#[derive(Debug, Copy, Clone, serde::Serialize, serde::Deserialize)]
// These are just ZSTs that are not actually produced and are only used for their
// associated types. So it's ok to derive "NotVersioned" for them.
#[derive(Debug, Copy, Clone, serde::Serialize, serde::Deserialize, NotVersioned)]
pub struct Bls12_381;
#[derive(Debug, Copy, Clone, serde::Serialize, serde::Deserialize)]
#[derive(Debug, Copy, Clone, serde::Serialize, serde::Deserialize, NotVersioned)]
pub struct Bls12_446;

impl Curve for Bls12_381 {
Expand Down
66 changes: 52 additions & 14 deletions tfhe-zk-pok/src/curve_api/bls12_381.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ 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::SerializableG1Affine;

use super::*;

#[derive(
Expand All @@ -47,10 +52,13 @@ mod g1 {
Hash,
CanonicalSerialize,
CanonicalDeserialize,
Versionize,
)]
#[serde(
try_from = "SerializableAffine<SerializableFp>",
into = "SerializableAffine<SerializableFp>"
#[serde(try_from = "SerializableG1Affine", into = "SerializableG1Affine")]
#[versionize(
SerializableG1AffineVersions,
try_from = "SerializableG1Affine",
into = "SerializableG1Affine"
)]
#[repr(transparent)]
pub struct G1Affine {
Expand Down Expand Up @@ -98,10 +106,13 @@ mod g1 {
Hash,
CanonicalSerialize,
CanonicalDeserialize,
Versionize,
)]
#[serde(
try_from = "SerializableAffine<SerializableFp>",
into = "SerializableAffine<SerializableFp>"
#[serde(try_from = "SerializableG1Affine", into = "SerializableG1Affine")]
#[versionize(
SerializableG1AffineVersions,
try_from = "SerializableG1Affine",
into = "SerializableG1Affine"
)]
#[repr(transparent)]
pub struct G1 {
Expand Down Expand Up @@ -250,6 +261,11 @@ mod g1 {
}

mod g2 {
use tfhe_versionable::Versionize;

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

use super::*;

#[derive(
Expand All @@ -263,10 +279,13 @@ mod g2 {
Hash,
CanonicalSerialize,
CanonicalDeserialize,
Versionize,
)]
#[serde(
try_from = "SerializableAffine<SerializableFp2>",
into = "SerializableAffine<SerializableFp2>"
#[serde(try_from = "SerializableG2Affine", into = "SerializableG2Affine")]
#[versionize(
SerializableG2AffineVersions,
try_from = "SerializableG2Affine",
into = "SerializableG2Affine"
)]
#[repr(transparent)]
pub struct G2Affine {
Expand Down Expand Up @@ -314,10 +333,13 @@ mod g2 {
Hash,
CanonicalSerialize,
CanonicalDeserialize,
Versionize,
)]
#[serde(
try_from = "SerializableAffine<SerializableFp2>",
into = "SerializableAffine<SerializableFp2>"
#[serde(try_from = "SerializableG2Affine", into = "SerializableG2Affine")]
#[versionize(
SerializableG2AffineVersions,
try_from = "SerializableG2Affine",
into = "SerializableG2Affine"
)]
#[repr(transparent)]
pub struct G2 {
Expand Down Expand Up @@ -513,11 +535,19 @@ mod g2 {
}

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

use super::*;
use ark_ec::pairing::Pairing;
use tfhe_versionable::Versionize;

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

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

use super::*;
use ark_ff::Fp;
use tfhe_versionable::Versionize;
use zeroize::Zeroize;

fn redc(n: [u64; 4], nprime: u64, mut t: [u64; 6]) -> [u64; 4] {
Expand Down Expand Up @@ -700,8 +733,13 @@ mod zp {
t
}

#[derive(Copy, Clone, PartialEq, Eq, Serialize, Deserialize, Hash, Zeroize)]
#[derive(Copy, Clone, PartialEq, Eq, Serialize, Deserialize, Versionize, Hash, Zeroize)]
#[serde(try_from = "SerializableFp", into = "SerializableFp")]
#[versionize(
SerializableFpVersions,
try_from = "SerializableFp",
into = "SerializableFp"
)]
#[repr(transparent)]
pub struct Zp {
pub(crate) inner: ark_bls12_381::Fr,
Expand Down
65 changes: 51 additions & 14 deletions tfhe-zk-pok/src/curve_api/bls12_446.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ 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::SerializableG1Affine;

use super::*;

#[derive(
Expand All @@ -47,10 +52,13 @@ mod g1 {
Hash,
CanonicalDeserialize,
CanonicalSerialize,
Versionize,
)]
#[serde(
try_from = "SerializableAffine<SerializableFp>",
into = "SerializableAffine<SerializableFp>"
#[serde(try_from = "SerializableG1Affine", into = "SerializableG1Affine")]
#[versionize(
SerializableG1AffineVersions,
try_from = "SerializableG1Affine",
into = "SerializableG1Affine"
)]
#[repr(transparent)]
pub struct G1Affine {
Expand Down Expand Up @@ -99,10 +107,13 @@ mod g1 {
Hash,
CanonicalSerialize,
CanonicalDeserialize,
Versionize,
)]
#[serde(
try_from = "SerializableAffine<SerializableFp>",
into = "SerializableAffine<SerializableFp>"
#[serde(try_from = "SerializableG1Affine", into = "SerializableG1Affine")]
#[versionize(
SerializableG1AffineVersions,
try_from = "SerializableG1Affine",
into = "SerializableG1Affine"
)]
#[repr(transparent)]
pub struct G1 {
Expand Down Expand Up @@ -251,6 +262,11 @@ mod g1 {
}

mod g2 {
use tfhe_versionable::Versionize;

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

use super::*;

#[derive(
Expand All @@ -264,10 +280,13 @@ mod g2 {
Hash,
CanonicalSerialize,
CanonicalDeserialize,
Versionize,
)]
#[serde(
try_from = "SerializableAffine<SerializableFp2>",
into = "SerializableAffine<SerializableFp2>"
#[serde(try_from = "SerializableG2Affine", into = "SerializableG2Affine")]
#[versionize(
SerializableG2AffineVersions,
try_from = "SerializableG2Affine",
into = "SerializableG2Affine"
)]
#[repr(transparent)]
pub struct G2Affine {
Expand Down Expand Up @@ -406,10 +425,13 @@ mod g2 {
Hash,
CanonicalSerialize,
CanonicalDeserialize,
Versionize,
)]
#[serde(
try_from = "SerializableAffine<SerializableFp2>",
into = "SerializableAffine<SerializableFp2>"
#[serde(try_from = "SerializableG2Affine", into = "SerializableG2Affine")]
#[versionize(
SerializableG2AffineVersions,
try_from = "SerializableG2Affine",
into = "SerializableG2Affine"
)]
#[repr(transparent)]
pub struct G2 {
Expand Down Expand Up @@ -604,11 +626,13 @@ mod g2 {
}

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

use super::*;
use ark_ec::pairing::{MillerLoopOutput, Pairing};
use ark_ff::{CubicExtField, QuadExtField};
use tfhe_versionable::Versionize;

type Bls = crate::curve_446::Bls12_446;

Expand Down Expand Up @@ -778,8 +802,13 @@ mod gt {
}
}

#[derive(Copy, Clone, PartialEq, Eq, Serialize, Deserialize, Hash)]
#[derive(Copy, Clone, PartialEq, Eq, Serialize, Deserialize, Versionize, Hash)]
#[serde(try_from = "SerializableFp12", into = "SerializableFp12")]
#[versionize(
SerializableFp12Versions,
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 @@ -922,8 +951,11 @@ mod gt {
}

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

use super::*;
use ark_ff::Fp;
use tfhe_versionable::Versionize;
use zeroize::Zeroize;

fn redc(n: [u64; 5], nprime: u64, mut t: [u64; 7]) -> [u64; 5] {
Expand Down Expand Up @@ -960,8 +992,13 @@ mod zp {
t
}

#[derive(Copy, Clone, PartialEq, Eq, Serialize, Deserialize, Hash, Zeroize)]
#[derive(Copy, Clone, PartialEq, Eq, Serialize, Deserialize, Versionize, Hash, Zeroize)]
#[serde(try_from = "SerializableFp", into = "SerializableFp")]
#[versionize(
SerializableFpVersions,
try_from = "SerializableFp",
into = "SerializableFp"
)]
#[repr(transparent)]
pub struct Zp {
pub inner: crate::curve_446::Fr,
Expand Down
1 change: 1 addition & 0 deletions tfhe-zk-pok/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ pub mod curve_api;
pub mod proofs;
pub mod serialization;

pub mod backward_compatibility;
mod four_squares;
8 changes: 7 additions & 1 deletion tfhe-zk-pok/src/proofs/pke_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
#![allow(non_snake_case)]

use super::*;
use crate::backward_compatibility::ProofVersions;
use crate::four_squares::*;
use core::marker::PhantomData;
use rayon::prelude::*;
use tfhe_versionable::{Unversionize, Versionize};

fn bit_iter(x: u64, nbits: u32) -> impl Iterator<Item = bool> {
(0..nbits).map(move |idx| ((x >> idx) & 1) != 0)
Expand Down Expand Up @@ -88,11 +90,15 @@ impl<G: Curve> PublicParams<G> {
}
}

#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize, Versionize)]
#[serde(bound(
deserialize = "G: Curve, G::G1: serde::Deserialize<'de>, G::G2: serde::Deserialize<'de>",
serialize = "G: Curve, G::G1: serde::Serialize, G::G2: serde::Serialize"
))]
#[versionize(
ProofVersions,
bound = "G::G1: Versionize + Unversionize, G::G2: Versionize + Unversionize"
)]
pub struct Proof<G: Curve> {
C_hat_e: G::G2,
C_e: G::G1,
Expand Down
Loading

0 comments on commit 3148b87

Please sign in to comment.