Skip to content

Commit

Permalink
Merge pull request #18 from dapplion/serde-compact
Browse files Browse the repository at this point in the history
Implement compact byte serde
  • Loading branch information
asn-d6 authored Oct 12, 2023
2 parents 8f4153c + 4b46c98 commit 0c62a1d
Show file tree
Hide file tree
Showing 10 changed files with 183 additions and 23 deletions.
2 changes: 2 additions & 0 deletions src/commitments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ use std::ops::{Add, Mul};
/// A GroupCommitment object
///
/// $GroupCommitment((G , H); T ; r ) = cm_T = (cm_{T,1} , cm_{T,2} ) = (r G , T + r H)$
///
/// CanonicalSerde produces compact representation since they type has no dyn vecs
#[derive(Copy, Clone, CanonicalDeserialize, CanonicalSerialize, Debug, PartialEq, Eq)]
pub struct GroupCommitment {
/// Given $GroupCommitment((G , H); T ; r )$ this is $rG$
Expand Down
5 changes: 5 additions & 0 deletions src/crs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ impl CurdleproofsCrs {
.collect::<Vec<_>>();
CurdleproofsCrs::from_points(ell, &points).expect("unexpected points len")
}

pub fn log2_n(&self) -> usize {
let n = self.vec_H.len() + self.vec_G.len();
(n as f64).log2().ceil() as usize
}
}

type G1AffineHex = String;
Expand Down
29 changes: 28 additions & 1 deletion src/curdleproofs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
pub use ark_bls12_381::{Fr, G1Affine, G1Projective};
use ark_ec::{AffineRepr, CurveGroup};
pub use ark_serialize::{CanonicalDeserialize, CanonicalSerialize, SerializationError};
use ark_serialize::{Read, Write};
use ark_std::rand::RngCore;
use ark_std::{UniformRand, Zero};

Expand All @@ -20,12 +21,13 @@ use crate::same_scalar_argument::SameScalarProof;

use crate::N_BLINDERS;

/// Generate a randomly generated CRS
pub fn generate_crs(ell: usize) -> CurdleproofsCrs {
CurdleproofsCrs::generate_crs(ell)
}

/// A Curdleproofs proof object
#[derive(Clone, Debug, CanonicalSerialize, CanonicalDeserialize)]
#[derive(Clone, Debug)]
pub struct CurdleproofsProof {
A: G1Projective,
cm_T: GroupCommitment,
Expand Down Expand Up @@ -294,6 +296,31 @@ impl CurdleproofsProof {
// Do the final verification on our MSM accumulator
msm_accumulator.verify()
}

pub fn serialize<W: Write>(&self, mut w: W) -> Result<(), SerializationError> {
self.A.serialize_compressed(&mut w)?;
self.cm_T.serialize_compressed(&mut w)?;
self.cm_U.serialize_compressed(&mut w)?;
self.R.serialize_compressed(&mut w)?;
self.S.serialize_compressed(&mut w)?;
self.same_perm_proof.serialize(&mut w)?;
self.same_scalar_proof.serialize_compressed(&mut w)?;
self.same_multiscalar_proof.serialize(&mut w)?;
Ok(())
}

pub fn deserialize<R: Read>(mut r: R, log2_n: usize) -> Result<Self, SerializationError> {
Ok(Self {
A: G1Projective::deserialize_compressed(&mut r)?,
cm_T: GroupCommitment::deserialize_compressed(&mut r)?,
cm_U: GroupCommitment::deserialize_compressed(&mut r)?,
R: G1Projective::deserialize_compressed(&mut r)?,
S: G1Projective::deserialize_compressed(&mut r)?,
same_perm_proof: SamePermutationProof::deserialize(&mut r, log2_n)?,
same_scalar_proof: SameScalarProof::deserialize_compressed(&mut r)?,
same_multiscalar_proof: SameMultiscalarProof::deserialize(&mut r, log2_n)?,
})
}
}

#[cfg(test)]
Expand Down
17 changes: 16 additions & 1 deletion src/grand_product_argument.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::ops::Mul;
use ark_bls12_381::{Fr, G1Affine, G1Projective};
use ark_ec::CurveGroup;
use ark_ff::{Field, One};
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize, Read, SerializationError, Write};
use ark_std::rand::RngCore;
use ark_std::Zero;

Expand Down Expand Up @@ -244,6 +244,21 @@ impl GrandProductProof {

Ok(())
}

pub fn serialize<W: Write>(&self, mut w: W) -> Result<(), SerializationError> {
self.C.serialize_compressed(&mut w)?;
self.r_p.serialize_compressed(&mut w)?;
self.ipa_proof.serialize(&mut w)?;
Ok(())
}

pub fn deserialize<R: Read>(mut r: R, log2_n: usize) -> Result<Self, SerializationError> {
Ok(Self {
C: G1Projective::deserialize_compressed(&mut r)?,
r_p: Fr::deserialize_compressed(&mut r)?,
ipa_proof: InnerProductProof::deserialize(&mut r, log2_n)?,
})
}
}

#[cfg(test)]
Expand Down
29 changes: 28 additions & 1 deletion src/inner_product_argument.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::ops::Mul;
use ark_bls12_381::{Fr, G1Affine, G1Projective};
use ark_ec::CurveGroup;
use ark_ff::{batch_inversion, Field};
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize, Read, SerializationError, Write};
use ark_std::rand::RngCore;
use ark_std::{One, Zero};

Expand All @@ -13,6 +13,8 @@ use merlin::Transcript;
use crate::errors::ProofError;
use crate::msm_accumulator::MsmAccumulator;
use crate::transcript::CurdleproofsTranscript;
use crate::util::deserialize_g1projective_vec;
use crate::util::serialize_g1projective_vec;
use crate::util::{
generate_blinders, get_verification_scalars_bitstring, inner_product, msm, msm_from_projective,
};
Expand Down Expand Up @@ -322,6 +324,31 @@ impl InnerProductProof {

Ok(())
}

pub fn serialize<W: Write>(&self, mut w: W) -> Result<(), SerializationError> {
self.B_c.serialize_compressed(&mut w)?;
self.B_d.serialize_compressed(&mut w)?;
serialize_g1projective_vec(&self.vec_L_C, &mut w)?;
serialize_g1projective_vec(&self.vec_R_C, &mut w)?;
serialize_g1projective_vec(&self.vec_L_D, &mut w)?;
serialize_g1projective_vec(&self.vec_R_D, &mut w)?;
self.c_final.serialize_compressed(&mut w)?;
self.d_final.serialize_compressed(&mut w)?;
Ok(())
}

pub fn deserialize<R: Read>(mut r: R, log2_n: usize) -> Result<Self, SerializationError> {
Ok(Self {
B_c: G1Projective::deserialize_compressed(&mut r)?,
B_d: G1Projective::deserialize_compressed(&mut r)?,
vec_L_C: deserialize_g1projective_vec(&mut r, log2_n)?,
vec_R_C: deserialize_g1projective_vec(&mut r, log2_n)?,
vec_L_D: deserialize_g1projective_vec(&mut r, log2_n)?,
vec_R_D: deserialize_g1projective_vec(&mut r, log2_n)?,
c_final: Fr::deserialize_compressed(&mut r)?,
d_final: Fr::deserialize_compressed(&mut r)?,
})
}
}

#[cfg(test)]
Expand Down
35 changes: 34 additions & 1 deletion src/same_multiscalar_argument.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ use std::ops::Mul;
use ark_bls12_381::{Fr, G1Affine, G1Projective};
use ark_ec::CurveGroup;
use ark_ff::{batch_inversion, Field, One};
use ark_serialize::Read;
use ark_serialize::SerializationError;
use ark_serialize::Write;
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
use ark_std::rand::RngCore;

use crate::transcript::CurdleproofsTranscript;
use crate::util::deserialize_g1projective_vec;
use crate::util::serialize_g1projective_vec;
use merlin::Transcript;

use crate::errors::ProofError;
Expand All @@ -17,7 +22,7 @@ use crate::util::{
};

/// A $SameMsm$ proof object
#[derive(Clone, Debug, CanonicalSerialize, CanonicalDeserialize)]
#[derive(Clone, Debug)]
pub struct SameMultiscalarProof {
B_a: G1Projective,
B_t: G1Projective,
Expand Down Expand Up @@ -254,6 +259,34 @@ impl SameMultiscalarProof {
msm_accumulator.accumulate_check(&point_lhs, &vec_x_times_s, vec_U, rng);
Ok(())
}

pub fn serialize<W: Write>(&self, mut w: W) -> Result<(), SerializationError> {
self.B_a.serialize_compressed(&mut w)?;
self.B_t.serialize_compressed(&mut w)?;
self.B_u.serialize_compressed(&mut w)?;
serialize_g1projective_vec(&self.vec_L_A, &mut w)?;
serialize_g1projective_vec(&self.vec_L_T, &mut w)?;
serialize_g1projective_vec(&self.vec_L_U, &mut w)?;
serialize_g1projective_vec(&self.vec_R_A, &mut w)?;
serialize_g1projective_vec(&self.vec_R_T, &mut w)?;
serialize_g1projective_vec(&self.vec_R_U, &mut w)?;
self.x_final.serialize_compressed(&mut w)?;
Ok(())
}
pub fn deserialize<R: Read>(mut r: R, log2_n: usize) -> Result<Self, SerializationError> {
Ok(Self {
B_a: G1Projective::deserialize_compressed(&mut r)?,
B_t: G1Projective::deserialize_compressed(&mut r)?,
B_u: G1Projective::deserialize_compressed(&mut r)?,
vec_L_A: deserialize_g1projective_vec(&mut r, log2_n)?,
vec_L_T: deserialize_g1projective_vec(&mut r, log2_n)?,
vec_L_U: deserialize_g1projective_vec(&mut r, log2_n)?,
vec_R_A: deserialize_g1projective_vec(&mut r, log2_n)?,
vec_R_T: deserialize_g1projective_vec(&mut r, log2_n)?,
vec_R_U: deserialize_g1projective_vec(&mut r, log2_n)?,
x_final: Fr::deserialize_compressed(&mut r)?,
})
}
}

#[cfg(test)]
Expand Down
17 changes: 15 additions & 2 deletions src/same_permutation_argument.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use core::iter;
use std::ops::Mul;

use ark_bls12_381::{Fr, G1Affine, G1Projective};
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize, Read, SerializationError, Write};
use ark_std::rand::RngCore;

use crate::transcript::CurdleproofsTranscript;
Expand All @@ -15,7 +15,7 @@ use crate::msm_accumulator::MsmAccumulator;
use crate::util::{get_permutation, msm};

/// A same permutation proof object
#[derive(Clone, Debug, CanonicalSerialize, CanonicalDeserialize)]
#[derive(Clone, Debug)]
pub struct SamePermutationProof {
B: G1Projective,

Expand Down Expand Up @@ -169,6 +169,19 @@ impl SamePermutationProof {

Ok(())
}

pub fn serialize<W: Write>(&self, mut w: W) -> Result<(), SerializationError> {
self.B.serialize_compressed(&mut w)?;
self.grand_product_proof.serialize(&mut w)?;
Ok(())
}

pub fn deserialize<R: Read>(mut r: R, log2_n: usize) -> Result<Self, SerializationError> {
Ok(Self {
B: G1Projective::deserialize_compressed(&mut r)?,
grand_product_proof: GrandProductProof::deserialize(&mut r, log2_n)?,
})
}
}

#[cfg(test)]
Expand Down
1 change: 1 addition & 0 deletions src/same_scalar_argument.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::errors::ProofError;
use crate::transcript::CurdleproofsTranscript;
use merlin::Transcript;

/// CanonicalSerde produces compact representation since they type has no dyn vecs
#[derive(Clone, Debug, CanonicalSerialize, CanonicalDeserialize)]
pub struct SameScalarProof {
cm_A: GroupCommitment,
Expand Down
23 changes: 21 additions & 2 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
#![allow(non_snake_case)]

use ark_bls12_381::{Fr, G1Affine, G1Projective};
use ark_ec::{AffineRepr, CurveGroup};
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize, Read, SerializationError, Write};
use ark_std::rand::RngCore;
use ark_std::{UniformRand, Zero};

use ark_ec::VariableBaseMSM;
use ark_ec::{AffineRepr, CurveGroup, VariableBaseMSM};
use core::iter;
use std::ops::Mul;

Expand Down Expand Up @@ -112,3 +112,22 @@ pub(crate) fn sum_affine_points(affine_points: &[G1Affine]) -> G1Affine {
.sum::<G1Projective>()
.into_affine()
}

pub fn deserialize_g1projective_vec<R: Read>(
mut r: R,
n: usize,
) -> Result<Vec<G1Projective>, SerializationError> {
(0..n)
.map(|_| G1Projective::deserialize_compressed(&mut r))
.collect()
}

pub fn serialize_g1projective_vec<W: Write>(
v: &[G1Projective],
mut w: W,
) -> Result<(), SerializationError> {
for p in v {
p.serialize_compressed(&mut w)?;
}
Ok(())
}
Loading

0 comments on commit 0c62a1d

Please sign in to comment.