Skip to content

Commit

Permalink
Resolves jdwhite48#16; Add serde for Com1, Com2, CRS, EquType, Commit…
Browse files Browse the repository at this point in the history
…s, Equations, EquProof
  • Loading branch information
AlvinHon committed Sep 19, 2024
1 parent 22ecc73 commit 0257a76
Show file tree
Hide file tree
Showing 6 changed files with 452 additions and 16 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ ark-ec = { version = "^0.4.0", default-features = false }
ark-ff = { version = "^0.4.0", default-features = false }
ark-std = { version = "^0.4.0", default-features = false }
rayon = { version = "^1.5.1" }
ark-serialize = { version = "^0.4.0", features = ["derive"] }

[dev-dependencies]
ark-bls12-381 = { version = "^0.4.0" }
Expand Down
57 changes: 51 additions & 6 deletions src/data_structures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use ark_ec::{
AffineRepr, CurveGroup,
};
use ark_ff::{Field, One, Zero};
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
use ark_std::{
fmt::Debug,
iter::Sum,
Expand Down Expand Up @@ -124,11 +125,11 @@ pub trait BT<E: Pairing, C1: B1<E>, C2: B2<E>>: B<E> + From<Matrix<PairingOutput
// SXDH instantiation's bilinear group for commitments

/// Base [`B1`](crate::data_structures::B1) for the commitment group in the SXDH instantiation.
#[derive(Copy, Clone, Debug)]
#[derive(Copy, Clone, Debug, CanonicalSerialize, CanonicalDeserialize)]
pub struct Com1<E: Pairing>(pub E::G1Affine, pub E::G1Affine);

/// Extension [`B2`](crate::data_structures::B2) for the commitment group in the SXDH instantiation.
#[derive(Copy, Clone, Debug)]
#[derive(Copy, Clone, Debug, CanonicalSerialize, CanonicalDeserialize)]
pub struct Com2<E: Pairing>(pub E::G2Affine, pub E::G2Affine);

/// Target [`BT`](crate::data_structures::BT) for the commitment group in the SXDH instantiation.
Expand Down Expand Up @@ -1300,19 +1301,63 @@ mod tests {
#[test]
fn test_B2_scalar_mul() {
let mut rng = test_rng();
let b = Com1::<F>(
G1Projective::rand(&mut rng).into_affine(),
G1Projective::rand(&mut rng).into_affine(),
let b = Com2::<F>(
G2Projective::rand(&mut rng).into_affine(),
G2Projective::rand(&mut rng).into_affine(),
);
let scalar = Fr::rand(&mut rng);
let b0 = b.0.mul(scalar);
let b1 = b.1.mul(scalar);
let bres = b.scalar_mul(&scalar);
let bexp = Com1::<F>(b0.into_affine(), b1.into_affine());
let bexp = Com2::<F>(b0.into_affine(), b1.into_affine());

assert_eq!(bres, bexp);
}

#[allow(non_snake_case)]
#[test]
fn test_B1_serde() {
let mut rng = test_rng();
let a = Com1::<F>(
G1Projective::rand(&mut rng).into_affine(),
G1Projective::rand(&mut rng).into_affine(),
);

// Serialize and deserialize Com1.

let mut c_bytes = Vec::new();
a.serialize_compressed(&mut c_bytes).unwrap();
let a_de = Com1::<F>::deserialize_compressed(&c_bytes[..]).unwrap();
assert_eq!(a, a_de);

let mut u_bytes = Vec::new();
a.serialize_uncompressed(&mut u_bytes).unwrap();
let a_de = Com1::<F>::deserialize_uncompressed(&u_bytes[..]).unwrap();
assert_eq!(a, a_de);
}

#[allow(non_snake_case)]
#[test]
fn test_B2_serde() {
let mut rng = test_rng();
let a = Com2::<F>(
G2Projective::rand(&mut rng).into_affine(),
G2Projective::rand(&mut rng).into_affine(),
);

// Serialize and deserialize Com2.

let mut c_bytes = Vec::new();
a.serialize_compressed(&mut c_bytes).unwrap();
let a_de = Com2::<F>::deserialize_compressed(&c_bytes[..]).unwrap();
assert_eq!(a, a_de);

let mut u_bytes = Vec::new();
a.serialize_uncompressed(&mut u_bytes).unwrap();
let a_de = Com2::<F>::deserialize_uncompressed(&u_bytes[..]).unwrap();
assert_eq!(a, a_de);
}

#[allow(non_snake_case)]
#[test]
fn test_B_pairing_zero_G1() {
Expand Down
33 changes: 30 additions & 3 deletions src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
//! the bilinear group generators `(g1, g2, gt)`.
//!
//! - As per [[GSW '10]](https://www.iacr.org/archive/pkc2010/60560179/60560179.pdf), the CRS
//! should either be generated by a trusted party or through some other form of trusted
//! computation as a binding key for a real system.
//! should either be generated by a trusted party or through some other form of trusted
//! computation as a binding key for a real system.
//! - The committment keys `u` and `v` should be computationally indistinguishable
//! under the SXDH assumption as to whethere they were instantiated as a:
//! under the SXDH assumption as to whethere they were instantiated as a:
//! 1) Perfect soundness string (i.e. perfectly binding), or
//! 2) Composable witness-indistinguishability string (i.e. perfectly hiding)

Expand All @@ -18,6 +18,7 @@ use ark_ec::{
CurveGroup,
};
use ark_ff::{UniformRand, Zero};
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
use ark_std::{ops::Mul, rand::Rng};

/// An abstract trait for denoting how to generate a CRS
Expand All @@ -31,6 +32,7 @@ pub trait AbstractCrs<E: Pairing> {
}

/// Contains the commitment keys and bilinear group generators
#[derive(Clone, Debug, CanonicalSerialize, CanonicalDeserialize)]
pub struct CRS<E: Pairing> {
pub u: Vec<Com1<E>>,
pub v: Vec<Com2<E>>,
Expand Down Expand Up @@ -178,4 +180,29 @@ mod tests {
assert_eq!(crs.u[1].1, v1.into_affine());
assert_eq!(crs.v[1].1, v2.into_affine());
}

#[allow(non_snake_case)]
#[test]
fn test_CRS_serde() {
let mut rng = test_rng();
let crs = CRS::<F>::generate_crs(&mut rng);

let mut c_bytes = Vec::new();
crs.serialize_compressed(&mut c_bytes).unwrap();
let crs_deserialized = CRS::<F>::deserialize_compressed(&c_bytes[..]).unwrap();
assert_eq!(crs.u, crs_deserialized.u);
assert_eq!(crs.v, crs_deserialized.v);
assert_eq!(crs.g1_gen, crs_deserialized.g1_gen);
assert_eq!(crs.g2_gen, crs_deserialized.g2_gen);
assert_eq!(crs.gt_gen, crs_deserialized.gt_gen);

let mut u_bytes = Vec::new();
crs.serialize_uncompressed(&mut u_bytes).unwrap();
let crs_deserialized = CRS::<F>::deserialize_uncompressed(&u_bytes[..]).unwrap();
assert_eq!(crs.u, crs_deserialized.u);
assert_eq!(crs.v, crs_deserialized.v);
assert_eq!(crs.g1_gen, crs_deserialized.g1_gen);
assert_eq!(crs.g2_gen, crs_deserialized.g2_gen);
assert_eq!(crs.gt_gen, crs_deserialized.gt_gen);
}
}
49 changes: 47 additions & 2 deletions src/prover/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#![allow(non_snake_case)]

use ark_ec::pairing::Pairing;
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
use ark_std::{fmt::Debug, rand::Rng, UniformRand};

use crate::data_structures::{col_vec_to_vec, vec_to_col_vec, Com1, Com2, Mat, Matrix, B1, B2};
Expand All @@ -14,13 +15,13 @@ pub trait Commit: Eq + Debug {
}

/// Contains both the commitment's values (as [`Com1`](crate::data_structures::Com1)) and its randomness.
#[derive(Clone, Debug)]
#[derive(Clone, Debug, CanonicalSerialize, CanonicalDeserialize)]
pub struct Commit1<E: Pairing> {
pub coms: Vec<Com1<E>>,
pub(super) rand: Matrix<E::ScalarField>,
}
/// Contains both the commitment's values (as [`Com2`](crate::data_structures::Com2)) and its randomness.
#[derive(Clone, Debug)]
#[derive(Clone, Debug, CanonicalSerialize, CanonicalDeserialize)]
pub struct Commit2<E: Pairing> {
pub coms: Vec<Com2<E>>,
pub(super) rand: Matrix<E::ScalarField>,
Expand Down Expand Up @@ -289,6 +290,50 @@ mod tests {
};
}

#[test]
fn test_commit_serde() {
let mut rng = test_rng();
let crs = CRS::<F>::generate_crs(&mut rng);
let r1 = Fr::rand(&mut rng);
let r2 = Fr::rand(&mut rng);
let com1 = Commit1::<F> {
coms: vec![Com1::<F>(
crs.g1_gen.mul(r1).into_affine(),
crs.g1_gen.mul(r2).into_affine(),
)],
rand: vec![vec![r1, r2]],
};
let com2 = Commit2::<F> {
coms: vec![Com2::<F>(
crs.g2_gen.mul(r1).into_affine(),
crs.g2_gen.mul(r2).into_affine(),
)],
rand: vec![vec![r1, r2]],
};

// Serialize and deserialize the commitment 1
let mut c_bytes = Vec::new();
com1.serialize_compressed(&mut c_bytes).unwrap();
let com1_de = Commit1::<F>::deserialize_compressed(&c_bytes[..]).unwrap();
assert_eq!(com1, com1_de);

let mut u_bytes = Vec::new();
com1.serialize_uncompressed(&mut u_bytes).unwrap();
let com1_de = Commit1::<F>::deserialize_uncompressed(&u_bytes[..]).unwrap();
assert_eq!(com1, com1_de);

// Serialize and deserialize the commitment 2
let mut c_bytes = Vec::new();
com2.serialize_compressed(&mut c_bytes).unwrap();
let com2_de = Commit2::<F>::deserialize_compressed(&c_bytes[..]).unwrap();
assert_eq!(com2, com2_de);

let mut u_bytes = Vec::new();
com2.serialize_uncompressed(&mut u_bytes).unwrap();
let com2_de = Commit2::<F>::deserialize_uncompressed(&u_bytes[..]).unwrap();
assert_eq!(com2, com2_de);
}

#[test]
fn test_commit_append_com1() {
std::env::set_var("DETERMINISTIC_TEST_RNG", "1");
Expand Down
Loading

0 comments on commit 0257a76

Please sign in to comment.