Skip to content

Commit

Permalink
Add CommitmentProver trait, and add KZG prover to it (#62)
Browse files Browse the repository at this point in the history
* Add KZG commitment scheme adapted to vector commitment

Add KZG commitment scheme adapted to vector commitment
Also move the `src/pedersen.rs` into `src/commitment/pedersen.rs` where
it will coexist with `kzg.rs` and the trait defined in
`src/commitment/mod.rs`.

* Adapt Pedersen into the new CommitmentProver trait

* add CommitmentProver (Pedersen&KZG) homomorphic property test

* polishing

* Use divide_with_q_and_r, rename skip_first_zero_coeffs

Co-authored-by: han0110 <[email protected]>

---------

Co-authored-by: han0110 <[email protected]>
  • Loading branch information
2 people authored and dmpierre committed Feb 2, 2024
1 parent c5b7dd7 commit 644440b
Show file tree
Hide file tree
Showing 16 changed files with 628 additions and 239 deletions.
5 changes: 4 additions & 1 deletion folding-schemes/src/folding/hypernova/cccs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ use ark_std::{rand::Rng, UniformRand};

use super::utils::compute_sum_Mz;
use crate::ccs::CCS;
use crate::pedersen::{Params as PedersenParams, Pedersen};
use crate::commitment::{
pedersen::{Params as PedersenParams, Pedersen},
CommitmentProver,
};
use crate::utils::hypercube::BooleanHypercube;
use crate::utils::mle::matrix_to_mle;
use crate::utils::mle::vec_to_mle;
Expand Down
2 changes: 1 addition & 1 deletion folding-schemes/src/folding/hypernova/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,11 @@ mod tests {
tests::{get_test_ccs, get_test_z},
CCS,
},
commitment::pedersen::Pedersen,
folding::hypernova::utils::{
compute_c_from_sigmas_and_thetas, compute_sigmas_and_thetas, sum_ci_mul_prod_thetaj,
sum_muls_gamma_pows_eq_sigma,
},
pedersen::Pedersen,
utils::virtual_polynomial::eq_eval,
};
use ark_pallas::{Fr, Projective};
Expand Down
5 changes: 4 additions & 1 deletion folding-schemes/src/folding/hypernova/lcccs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ use ark_std::{rand::Rng, UniformRand};
use super::cccs::Witness;
use super::utils::{compute_all_sum_Mz_evals, compute_sum_Mz};
use crate::ccs::CCS;
use crate::pedersen::{Params as PedersenParams, Pedersen};
use crate::commitment::{
pedersen::{Params as PedersenParams, Pedersen},
CommitmentProver,
};
use crate::utils::mle::{matrix_to_mle, vec_to_mle};
use crate::utils::virtual_polynomial::VirtualPolynomial;
use crate::Error;
Expand Down
4 changes: 2 additions & 2 deletions folding-schemes/src/folding/hypernova/nimfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ where
//////////////////////////////////////////////////////////////////////
let mut g_over_bhc = C::ScalarField::zero();
for x in BooleanHypercube::new(ccs.s) {
g_over_bhc += g.evaluate(&x).unwrap();
g_over_bhc += g.evaluate(&x)?;
}

// note: this is the sum of g(x) over the whole boolean hypercube
Expand Down Expand Up @@ -378,7 +378,7 @@ pub mod tests {
use ark_std::test_rng;
use ark_std::UniformRand;

use crate::pedersen::Pedersen;
use crate::commitment::pedersen::Pedersen;
use ark_pallas::{Fr, Projective};

#[test]
Expand Down
2 changes: 1 addition & 1 deletion folding-schemes/src/folding/hypernova/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ pub mod tests {
use ark_std::Zero;

use crate::ccs::tests::{get_test_ccs, get_test_z};
use crate::pedersen::Pedersen;
use crate::commitment::pedersen::Pedersen;
use crate::utils::multilinear_polynomial::tests::fix_last_variables;
use crate::utils::virtual_polynomial::eq_eval;

Expand Down
2 changes: 1 addition & 1 deletion folding-schemes/src/folding/nova/circuits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,12 +470,12 @@ pub mod tests {
use tracing_subscriber::layer::SubscriberExt;

use crate::ccs::r1cs::{extract_r1cs, extract_w_x};
use crate::commitment::pedersen::Pedersen;
use crate::folding::nova::nifs::tests::prepare_simple_fold_inputs;
use crate::folding::nova::{
ivc::get_committed_instance_coordinates, nifs::NIFS, traits::NovaR1CS, Witness,
};
use crate::frontend::tests::CubicFCircuit;
use crate::pedersen::Pedersen;
use crate::transcript::poseidon::tests::poseidon_test_config;

#[test]
Expand Down
4 changes: 2 additions & 2 deletions folding-schemes/src/folding/nova/decider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ use ark_std::{One, Zero};
use core::{borrow::Borrow, marker::PhantomData};

use crate::ccs::r1cs::R1CS;
use crate::commitment::pedersen::Params as PedersenParams;
use crate::folding::nova::{
circuits::{CommittedInstanceVar, CF1, CF2},
ivc::IVC,
CommittedInstance, Witness,
};
use crate::frontend::FCircuit;
use crate::pedersen::Params as PedersenParams;
use crate::utils::gadgets::{
hadamard, mat_vec_mul_sparse, vec_add, vec_scalar_mul, SparseMatrixVar,
};
Expand Down Expand Up @@ -355,8 +355,8 @@ where
{
// imports here instead of at the top of the file, so we avoid having multiple
// `#[cfg(not(test))]
use crate::commitment::pedersen::PedersenGadget;
use crate::folding::nova::cyclefold::{CycleFoldCommittedInstanceVar, CF_IO_LEN};
use crate::pedersen::PedersenGadget;
use ark_r1cs_std::ToBitsGadget;

let cf_r1cs = R1CSVar::<
Expand Down
2 changes: 1 addition & 1 deletion folding-schemes/src/folding/nova/ivc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use super::{
use super::{nifs::NIFS, traits::NovaR1CS, CommittedInstance, Witness};
use crate::ccs::r1cs::R1CS;
use crate::ccs::r1cs::{extract_r1cs, extract_w_x};
use crate::commitment::pedersen::{Params as PedersenParams, Pedersen};
use crate::frontend::FCircuit;
use crate::pedersen::{Params as PedersenParams, Pedersen};
use crate::Error;

#[cfg(test)]
Expand Down
11 changes: 8 additions & 3 deletions folding-schemes/src/folding/nova/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ use ark_ec::{CurveGroup, Group};
use ark_std::fmt::Debug;
use ark_std::{One, Zero};

use crate::commitment::{
pedersen::{Params as PedersenParams, Pedersen},
CommitmentProver,
};
use crate::folding::circuits::nonnative::point_to_nonnative_limbs;
use crate::pedersen::{Params as PedersenParams, Pedersen};
use crate::utils::vec::is_zero_vec;
use crate::Error;

Expand Down Expand Up @@ -89,11 +92,13 @@ where
<C as Group>::ScalarField: Absorb,
{
pub fn new(w: Vec<C::ScalarField>, e_len: usize) -> Self {
// note: at the current version, we don't use the blinding factors and we set them to 0
// always.
Self {
E: vec![C::ScalarField::zero(); e_len],
rE: C::ScalarField::zero(), // because we use C::zero() as cmE
rE: C::ScalarField::zero(),
W: w,
rW: C::ScalarField::one(),
rW: C::ScalarField::zero(),
}
}
pub fn commit(
Expand Down
5 changes: 4 additions & 1 deletion folding-schemes/src/folding/nova/nifs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ use std::marker::PhantomData;

use super::{CommittedInstance, Witness};
use crate::ccs::r1cs::R1CS;
use crate::pedersen::{Params as PedersenParams, Pedersen, Proof as PedersenProof};
use crate::commitment::{
pedersen::{Params as PedersenParams, Pedersen, Proof as PedersenProof},
CommitmentProver,
};
use crate::transcript::Transcript;
use crate::utils::vec::*;
use crate::Error;
Expand Down
2 changes: 1 addition & 1 deletion folding-schemes/src/folding/protogalaxy/folding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ mod tests {
use ark_std::UniformRand;

use crate::ccs::r1cs::tests::{get_test_r1cs, get_test_z};
use crate::pedersen::Pedersen;
use crate::commitment::{pedersen::Pedersen, CommitmentProver};
use crate::transcript::poseidon::{tests::poseidon_test_config, PoseidonTranscript};

pub(crate) fn check_instance<C: CurveGroup>(
Expand Down
12 changes: 9 additions & 3 deletions folding-schemes/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ use thiserror::Error;
pub mod transcript;
use transcript::Transcript;
pub mod ccs;
pub mod commitment;
pub mod constants;
pub mod folding;
pub mod frontend;
pub mod pedersen;
pub mod utils;

#[derive(Debug, Error)]
Expand All @@ -21,6 +21,10 @@ pub enum Error {
SynthesisError(#[from] ark_relations::r1cs::SynthesisError),
#[error("ark_serialize::SerializationError")]
SerializationError(#[from] ark_serialize::SerializationError),
#[error("ark_poly_commit::Error")]
PolyCommitError(#[from] ark_poly_commit::Error),
#[error("crate::utils::espresso::virtual_polynomial::ArithErrors")]
ArithError(#[from] utils::espresso::virtual_polynomial::ArithErrors),
#[error("{0}")]
Other(String),

Expand All @@ -36,8 +40,8 @@ pub enum Error {
Empty,
#[error("Pedersen parameters length is not suficient (generators.len={0} < vector.len={1} unsatisfied)")]
PedersenParamsLen(usize, usize),
#[error("Pedersen verification failed")]
PedersenVerificationFail,
#[error("Commitment verification failed")]
CommitmentVerificationFail,
#[error("IVC verification failed")]
IVCVerificationFail,
#[error("R1CS instance is expected to not be relaxed")]
Expand All @@ -52,6 +56,8 @@ pub enum Error {
OutOfBounds,
#[error("Could not construct the Evaluation Domain")]
NewDomainFail,
#[error("Feature '{0}' not supported yet")]
NotSupportedYet(String),

#[error(transparent)]
ProtoGalaxy(folding::protogalaxy::ProtoGalaxyError),
Expand Down
Loading

0 comments on commit 644440b

Please sign in to comment.