Skip to content

Commit

Permalink
Merge PCCommitmentState with PCRandomness
Browse files Browse the repository at this point in the history
  • Loading branch information
autquis committed Nov 13, 2023
1 parent 4ee97d4 commit fb68290
Show file tree
Hide file tree
Showing 13 changed files with 62 additions and 130 deletions.
13 changes: 5 additions & 8 deletions bench-templates/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub fn commit<
LabeledPolynomial::new("test".to_string(), rand_poly(num_vars, rng), None, None);

let start = Instant::now();
let (_, _, _) = PCS::commit(&ck, [&labeled_poly], Some(rng)).unwrap();
let (_, _) = PCS::commit(&ck, [&labeled_poly], Some(rng)).unwrap();
start.elapsed()
}

Expand All @@ -91,7 +91,7 @@ pub fn commitment_size<
let labeled_poly =
LabeledPolynomial::new("test".to_string(), rand_poly(num_vars, rng), None, None);

let (coms, _, _) = PCS::commit(&ck, [&labeled_poly], Some(rng)).unwrap();
let (coms, _) = PCS::commit(&ck, [&labeled_poly], Some(rng)).unwrap();

coms[0].commitment().serialized_size(Compress::No)
}
Expand All @@ -114,7 +114,7 @@ where
let labeled_poly =
LabeledPolynomial::new("test".to_string(), rand_poly(num_vars, rng), None, None);

let (coms, states, randomness) = PCS::commit(&ck, [&labeled_poly], Some(rng)).unwrap();
let (coms, states) = PCS::commit(&ck, [&labeled_poly], Some(rng)).unwrap();
let point = P::Point::rand(rng);

let start = Instant::now();
Expand All @@ -125,7 +125,6 @@ where
&point,
&mut ChallengeGenerator::new_univariate(&mut test_sponge()),
&states,
&randomness,
Some(rng),
)
.unwrap();
Expand All @@ -149,7 +148,7 @@ where
let labeled_poly =
LabeledPolynomial::new("test".to_string(), rand_poly(num_vars, rng), None, None);

let (coms, states, randomness) = PCS::commit(&ck, [&labeled_poly], Some(rng)).unwrap();
let (coms, states) = PCS::commit(&ck, [&labeled_poly], Some(rng)).unwrap();
let point = P::Point::rand(rng);

let proofs = PCS::open(
Expand All @@ -159,7 +158,6 @@ where
&point,
&mut ChallengeGenerator::new_univariate(&mut test_sponge()),
&states,
&randomness,
Some(rng),
)
.unwrap();
Expand Down Expand Up @@ -187,7 +185,7 @@ where
let labeled_poly =
LabeledPolynomial::new("test".to_string(), rand_poly(num_vars, rng), None, None);

let (coms, states, randomness) = PCS::commit(&ck, [&labeled_poly], Some(rng)).unwrap();
let (coms, states) = PCS::commit(&ck, [&labeled_poly], Some(rng)).unwrap();
let point = P::Point::rand(rng);
let claimed_eval = labeled_poly.evaluate(&point);
let proof = PCS::open(
Expand All @@ -197,7 +195,6 @@ where
&point,
&mut ChallengeGenerator::new_univariate(&mut test_sponge()),
&states,
&randomness,
Some(rng),
)
.unwrap();
Expand Down
9 changes: 3 additions & 6 deletions poly-commit/src/data_structures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,16 @@ pub trait PCCommitment: Clone + CanonicalSerialize + CanonicalDeserialize {
fn has_degree_bound(&self) -> bool;
}

/// Defines the auxiliary data of the commitment
pub trait PCCommitmentState: Clone + Default + CanonicalSerialize + CanonicalDeserialize {}

/// Defines the minimal interface of prepared commitments for any polynomial
/// commitment scheme.
pub trait PCPreparedCommitment<UNPREPARED: PCCommitment>: Clone {
/// prepare
fn prepare(comm: &UNPREPARED) -> Self;
}

/// Defines the minimal interface of commitment randomness for any polynomial
/// commitment scheme.
pub trait PCRandomness: Clone + CanonicalSerialize + CanonicalDeserialize {
/// Defines the minimal interface of commitment state for any polynomial
/// commitment scheme. It might be randomness etc.
pub trait PCCommitmentState: Clone + CanonicalSerialize + CanonicalDeserialize {
/// Outputs empty randomness that does not hide the commitment.
fn empty() -> Self;

Expand Down
2 changes: 1 addition & 1 deletion poly-commit/src/ipa_pc/data_structures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ pub struct Randomness<G: AffineRepr> {
pub shifted_rand: Option<G::ScalarField>,
}

impl<G: AffineRepr> PCRandomness for Randomness<G> {
impl<G: AffineRepr> PCCommitmentState for Randomness<G> {
fn empty() -> Self {
Self {
rand: G::ScalarField::zero(),
Expand Down
20 changes: 7 additions & 13 deletions poly-commit/src/ipa_pc/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::kzg10::CommitmentState;
use crate::{BTreeMap, BTreeSet, String, ToString, Vec, CHALLENGE_SIZE};
use crate::{BatchLCProof, DenseUVPolynomial, Error, Evaluations, QuerySet};
use crate::{LabeledCommitment, LabeledPolynomial, LinearCombination};
use crate::{PCCommitterKey, PCRandomness, PCUniversalParams, PolynomialCommitment};
use crate::{PCCommitmentState, PCCommitterKey, PCUniversalParams, PolynomialCommitment};

use ark_ec::{AffineRepr, CurveGroup, VariableBaseMSM};
use ark_ff::{Field, One, PrimeField, UniformRand, Zero};
Expand Down Expand Up @@ -348,8 +347,7 @@ where
type CommitterKey = CommitterKey<G>;
type VerifierKey = VerifierKey<G>;
type Commitment = Commitment<G>;
type CommitmentState = CommitmentState;
type Randomness = Randomness<G>;
type CommitmentState = Randomness<G>;
type Proof = Proof<G>;
type BatchProof = Vec<Self::Proof>;
type Error = Error;
Expand Down Expand Up @@ -421,7 +419,6 @@ where
(
Vec<LabeledCommitment<Self::Commitment>>,
Vec<Self::CommitmentState>,
Vec<Self::Randomness>,
),
Self::Error,
>
Expand Down Expand Up @@ -483,7 +480,7 @@ where
}

end_timer!(commit_time);
Ok((comms, vec![CommitmentState {}; rands.len()], rands))
Ok((comms, rands))
}

fn open<'a>(
Expand All @@ -492,13 +489,12 @@ where
commitments: impl IntoIterator<Item = &'a LabeledCommitment<Self::Commitment>>,
point: &'a P::Point,
opening_challenges: &mut ChallengeGenerator<G::ScalarField, S>,
_states: impl IntoIterator<Item = &'a Self::CommitmentState>,
rands: impl IntoIterator<Item = &'a Self::Randomness>,
rands: impl IntoIterator<Item = &'a Self::CommitmentState>,
rng: Option<&mut dyn RngCore>,
) -> Result<Self::Proof, Self::Error>
where
Self::Commitment: 'a,
Self::Randomness: 'a,
Self::CommitmentState: 'a,
P: 'a,
{
let mut combined_polynomial = P::zero();
Expand Down Expand Up @@ -881,12 +877,11 @@ where
commitments: impl IntoIterator<Item = &'a LabeledCommitment<Self::Commitment>>,
query_set: &QuerySet<P::Point>,
opening_challenges: &mut ChallengeGenerator<G::ScalarField, S>,
_states: impl IntoIterator<Item = &'a Self::CommitmentState>,
rands: impl IntoIterator<Item = &'a Self::Randomness>,
rands: impl IntoIterator<Item = &'a Self::CommitmentState>,
rng: Option<&mut dyn RngCore>,
) -> Result<BatchLCProof<G::ScalarField, Self::BatchProof>, Self::Error>
where
Self::Randomness: 'a,
Self::CommitmentState: 'a,
Self::Commitment: 'a,
P: 'a,
{
Expand Down Expand Up @@ -977,7 +972,6 @@ where
lc_commitments.iter(),
&query_set,
opening_challenges,
&vec![CommitmentState {}; lc_randomness.len()],
lc_randomness.iter(),
rng,
)?;
Expand Down
7 changes: 1 addition & 6 deletions poly-commit/src/kzg10/data_structures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,11 +329,6 @@ pub struct Commitment<E: Pairing>(
pub E::G1Affine,
);

/// The auxiliary data for KZG commitment is empty.
#[derive(Clone, Default, CanonicalSerialize, CanonicalDeserialize)]
pub struct CommitmentState {}
impl PCCommitmentState for CommitmentState {}

impl<E: Pairing> PCCommitment for Commitment<E> {
#[inline]
fn empty() -> Self {
Expand Down Expand Up @@ -425,7 +420,7 @@ impl<F: PrimeField, P: DenseUVPolynomial<F>> Randomness<F, P> {
}
}

impl<F: PrimeField, P: DenseUVPolynomial<F>> PCRandomness for Randomness<F, P> {
impl<F: PrimeField, P: DenseUVPolynomial<F>> PCCommitmentState for Randomness<F, P> {
fn empty() -> Self {
Self {
blinding_polynomial: P::zero(),
Expand Down
2 changes: 1 addition & 1 deletion poly-commit/src/kzg10/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//! proposed by Kate, Zaverucha, and Goldberg ([KZG10](http://cacr.uwaterloo.ca/techreports/2010/cacr2010-10.pdf)).
//! This construction achieves extractability in the algebraic group model (AGM).

use crate::{BTreeMap, Error, LabeledPolynomial, PCRandomness, ToString, Vec};
use crate::{BTreeMap, Error, LabeledPolynomial, PCCommitmentState, ToString, Vec};
use ark_ec::AffineRepr;
use ark_ec::{pairing::Pairing, CurveGroup};
use ark_ec::{scalar_mul::fixed_base::FixedBase, VariableBaseMSM};
Expand Down
39 changes: 10 additions & 29 deletions poly-commit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,8 @@ pub trait PolynomialCommitment<F: PrimeField, P: Polynomial<F>, S: Cryptographic
type VerifierKey: PCVerifierKey;
/// The commitment to a polynomial.
type Commitment: PCCommitment + Default;
/// The state of committer
/// The state of commitment
type CommitmentState: PCCommitmentState;
/// The commitment randomness.
type Randomness: PCRandomness;
/// The evaluation proof for a single point.
type Proof: Clone;
/// The evaluation proof for a query set.
Expand Down Expand Up @@ -206,7 +204,6 @@ pub trait PolynomialCommitment<F: PrimeField, P: Polynomial<F>, S: Cryptographic
(
Vec<LabeledCommitment<Self::Commitment>>,
Vec<Self::CommitmentState>,
Vec<Self::Randomness>,
),
Self::Error,
>
Expand All @@ -221,12 +218,10 @@ pub trait PolynomialCommitment<F: PrimeField, P: Polynomial<F>, S: Cryptographic
point: &'a P::Point,
challenge_generator: &mut ChallengeGenerator<F, S>,
states: impl IntoIterator<Item = &'a Self::CommitmentState>,
rands: impl IntoIterator<Item = &'a Self::Randomness>,
rng: Option<&mut dyn RngCore>,
) -> Result<Self::Proof, Self::Error>
where
P: 'a,
Self::Randomness: 'a,
Self::CommitmentState: 'a,
Self::Commitment: 'a;

Expand Down Expand Up @@ -259,12 +254,10 @@ pub trait PolynomialCommitment<F: PrimeField, P: Polynomial<F>, S: Cryptographic
query_set: &QuerySet<P::Point>,
challenge_generator: &mut ChallengeGenerator<F, S>,
states: impl IntoIterator<Item = &'a Self::CommitmentState>,
rands: impl IntoIterator<Item = &'a Self::Randomness>,
rng: Option<&mut dyn RngCore>,
) -> Result<Self::BatchProof, Self::Error>
where
P: 'a,
Self::Randomness: 'a,
Self::CommitmentState: 'a,
Self::Commitment: 'a,
{
Expand All @@ -273,12 +266,11 @@ pub trait PolynomialCommitment<F: PrimeField, P: Polynomial<F>, S: Cryptographic
// the same point, then opening their commitments simultaneously with a
// single call to `open` (per point)
let rng = &mut crate::optional_rng::OptionalRng(rng);
let poly_rand_st_comm: BTreeMap<_, _> = labeled_polynomials
let poly_st_comm: BTreeMap<_, _> = labeled_polynomials
.into_iter()
.zip(states)
.zip(rands)
.zip(commitments.into_iter())
.map(|(((poly, st), r), comm)| (poly.label(), (poly, r, st, comm)))
.map(|((poly, st), comm)| (poly.label(), (poly, st, comm)))
.collect();

let open_time = start_timer!(|| format!(
Expand Down Expand Up @@ -306,23 +298,19 @@ pub trait PolynomialCommitment<F: PrimeField, P: Polynomial<F>, S: Cryptographic
let mut proofs = Vec::new();
for (_point_label, (point, labels)) in query_to_labels_map.into_iter() {
let mut query_polys: Vec<&'a LabeledPolynomial<_, _>> = Vec::new();
let mut query_rands: Vec<&'a Self::Randomness> = Vec::new();
let mut query_states: Vec<&'a Self::CommitmentState> = Vec::new();
let mut query_comms: Vec<&'a LabeledCommitment<Self::Commitment>> = Vec::new();

// Constructing matching vectors with the polynomial, commitment
// randomness and actual commitment for each polynomial being
// queried at `point`
for label in labels {
let (polynomial, rand, state, comm) =
poly_rand_st_comm
.get(label)
.ok_or(Error::MissingPolynomial {
label: label.to_string(),
})?;
let (polynomial, state, comm) =
poly_st_comm.get(label).ok_or(Error::MissingPolynomial {
label: label.to_string(),
})?;

query_polys.push(polynomial);
query_rands.push(rand);
query_states.push(state);
query_comms.push(comm);
}
Expand All @@ -338,7 +326,6 @@ pub trait PolynomialCommitment<F: PrimeField, P: Polynomial<F>, S: Cryptographic
&point,
challenge_generator,
query_states,
query_rands,
Some(rng),
)?;

Expand Down Expand Up @@ -452,11 +439,9 @@ pub trait PolynomialCommitment<F: PrimeField, P: Polynomial<F>, S: Cryptographic
query_set: &QuerySet<P::Point>,
challenge_generator: &mut ChallengeGenerator<F, S>,
states: impl IntoIterator<Item = &'a Self::CommitmentState>,
rands: impl IntoIterator<Item = &'a Self::Randomness>,
rng: Option<&mut dyn RngCore>,
) -> Result<BatchLCProof<F, Self::BatchProof>, Self::Error>
where
Self::Randomness: 'a,
Self::CommitmentState: 'a,
Self::Commitment: 'a,
P: 'a,
Expand All @@ -480,7 +465,6 @@ pub trait PolynomialCommitment<F: PrimeField, P: Polynomial<F>, S: Cryptographic
&poly_query_set,
challenge_generator,
states,
rands,
rng,
)?;
Ok(BatchLCProof {
Expand Down Expand Up @@ -731,7 +715,7 @@ pub mod tests {
)?;
println!("Trimmed");

let (comms, states, rands) = PC::commit(&ck, &polynomials, Some(rng))?;
let (comms, states) = PC::commit(&ck, &polynomials, Some(rng))?;

let mut query_set = QuerySet::new();
let mut values = Evaluations::new();
Expand All @@ -750,7 +734,6 @@ pub mod tests {
&query_set,
&mut (challenge_gen.clone()),
&states,
&rands,
Some(rng),
)?;
let result = PC::batch_check(
Expand Down Expand Up @@ -865,7 +848,7 @@ pub mod tests {
)?;
println!("Trimmed");

let (comms, states, rands) = PC::commit(&ck, &polynomials, Some(rng))?;
let (comms, states) = PC::commit(&ck, &polynomials, Some(rng))?;

// Construct query set
let mut query_set = QuerySet::new();
Expand All @@ -887,7 +870,6 @@ pub mod tests {
&query_set,
&mut (challenge_gen.clone()),
&states,
&rands,
Some(rng),
)?;
let result = PC::batch_check(
Expand Down Expand Up @@ -1014,7 +996,7 @@ pub mod tests {
)?;
println!("Trimmed");

let (comms, states, rands) = PC::commit(&ck, &polynomials, Some(rng))?;
let (comms, states) = PC::commit(&ck, &polynomials, Some(rng))?;

// Let's construct our equations
let mut linear_combinations = Vec::new();
Expand Down Expand Up @@ -1067,7 +1049,6 @@ pub mod tests {
&query_set,
&mut (challenge_gen.clone()),
&states,
&rands,
Some(rng),
)?;
println!("Generated proof");
Expand Down
6 changes: 3 additions & 3 deletions poly-commit/src/marlin/marlin_pc/data_structures.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
DenseUVPolynomial, PCCommitment, PCCommitterKey, PCPreparedCommitment, PCPreparedVerifierKey,
PCRandomness, PCVerifierKey, Vec,
DenseUVPolynomial, PCCommitment, PCCommitmentState, PCCommitterKey, PCPreparedCommitment,
PCPreparedVerifierKey, PCVerifierKey, Vec,
};
use ark_ec::pairing::Pairing;
use ark_ec::AdditiveGroup;
Expand Down Expand Up @@ -360,7 +360,7 @@ impl<'a, F: PrimeField, P: DenseUVPolynomial<F>> AddAssign<(F, &'a Randomness<F,
}
}

impl<F: PrimeField, P: DenseUVPolynomial<F>> PCRandomness for Randomness<F, P> {
impl<F: PrimeField, P: DenseUVPolynomial<F>> PCCommitmentState for Randomness<F, P> {
fn empty() -> Self {
Self {
rand: kzg10::Randomness::empty(),
Expand Down
Loading

0 comments on commit fb68290

Please sign in to comment.