Skip to content

Commit

Permalink
Put Randomness in CommitmentState
Browse files Browse the repository at this point in the history
  • Loading branch information
autquis committed Jan 3, 2024
1 parent 5a5993e commit 1c04050
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 57 deletions.
6 changes: 4 additions & 2 deletions poly-commit/src/data_structures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ pub trait PCPreparedCommitment<UNPREPARED: PCCommitment>: Clone {
/// Defines the minimal interface of commitment state for any polynomial
/// commitment scheme. It might be randomness etc.
pub trait PCCommitmentState: Clone + CanonicalSerialize + CanonicalDeserialize {
/// blah
type Randomness: Clone + CanonicalSerialize + CanonicalDeserialize;

/// Outputs empty randomness that does not hide the commitment.
fn empty() -> Self;

Expand All @@ -86,9 +89,8 @@ pub trait PCCommitmentState: Clone + CanonicalSerialize + CanonicalDeserialize {
has_degree_bound: bool,
num_vars: Option<usize>,
rng: &mut R,
) -> Self;
) -> Self::Randomness;
}

/// A proof of satisfaction of linear combinations.
#[derive(Clone, CanonicalSerialize, CanonicalDeserialize)]
pub struct BatchLCProof<F: PrimeField, T: Clone + CanonicalSerialize + CanonicalDeserialize> {
Expand Down
1 change: 1 addition & 0 deletions poly-commit/src/ipa_pc/data_structures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ pub struct Randomness<G: AffineRepr> {
}

impl<G: AffineRepr> PCCommitmentState for Randomness<G> {
type Randomness = Self;
fn empty() -> Self {
Self {
rand: G::ScalarField::zero(),
Expand Down
36 changes: 18 additions & 18 deletions poly-commit/src/ipa_pc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ where
{
let rng = &mut crate::optional_rng::OptionalRng(rng);
let mut comms = Vec::new();
let mut rands = Vec::new();
let mut states = Vec::new();

let commit_time = start_timer!(|| "Committing to polynomials");
for labeled_polynomial in polynomials {
Expand All @@ -446,7 +446,7 @@ where
hiding_bound,
));

let randomness = if let Some(h) = hiding_bound {
let state = if let Some(h) = hiding_bound {
Randomness::rand(h, degree_bound.is_some(), None, rng)
} else {
Randomness::empty()
Expand All @@ -456,7 +456,7 @@ where
&ck.comm_key[..(polynomial.degree() + 1)],
&polynomial.coeffs(),
Some(ck.s),
Some(randomness.rand),
Some(state.rand),
)
.into();

Expand All @@ -465,7 +465,7 @@ where
&ck.comm_key[(ck.supported_degree() - d)..],
&polynomial.coeffs(),
Some(ck.s),
randomness.shifted_rand,
state.shifted_rand,
)
.into()
});
Expand All @@ -474,13 +474,13 @@ where
let labeled_comm = LabeledCommitment::new(label.to_string(), commitment, degree_bound);

comms.push(labeled_comm);
rands.push(randomness);
states.push(state);

end_timer!(commit_time);
}

end_timer!(commit_time);
Ok((comms, rands))
Ok((comms, states))
}

fn open<'a>(
Expand All @@ -489,7 +489,7 @@ where
commitments: impl IntoIterator<Item = &'a LabeledCommitment<Self::Commitment>>,
point: &'a P::Point,
opening_challenges: &mut ChallengeGenerator<G::ScalarField, S>,
rands: impl IntoIterator<Item = &'a Self::CommitmentState>,
states: impl IntoIterator<Item = &'a Self::CommitmentState>,
rng: Option<&mut dyn RngCore>,
) -> Result<Self::Proof, Self::Error>
where
Expand All @@ -504,15 +504,15 @@ where
let mut has_hiding = false;

let polys_iter = labeled_polynomials.into_iter();
let rands_iter = rands.into_iter();
let states_iter = states.into_iter();
let comms_iter = commitments.into_iter();

let combine_time = start_timer!(|| "Combining polynomials, randomness, and commitments.");

let mut cur_challenge = opening_challenges.try_next_challenge_of_size(CHALLENGE_SIZE);

for (labeled_polynomial, (labeled_commitment, randomness)) in
polys_iter.zip(comms_iter.zip(rands_iter))
for (labeled_polynomial, (labeled_commitment, state)) in
polys_iter.zip(comms_iter.zip(states_iter))
{
let label = labeled_polynomial.label();
assert_eq!(labeled_polynomial.label(), labeled_commitment.label());
Expand All @@ -528,7 +528,7 @@ where

if hiding_bound.is_some() {
has_hiding = true;
combined_rand += &(cur_challenge * &randomness.rand);
combined_rand += &(cur_challenge * &state.rand);
}

cur_challenge = opening_challenges.try_next_challenge_of_size(CHALLENGE_SIZE);
Expand All @@ -554,7 +554,7 @@ where
combined_commitment_proj += &commitment.shifted_comm.unwrap().mul(cur_challenge);

if hiding_bound.is_some() {
let shifted_rand = randomness.shifted_rand;
let shifted_rand = state.shifted_rand;
assert!(
shifted_rand.is_some(),
"shifted_rand.is_none() for {}",
Expand Down Expand Up @@ -877,7 +877,7 @@ where
commitments: impl IntoIterator<Item = &'a LabeledCommitment<Self::Commitment>>,
query_set: &QuerySet<P::Point>,
opening_challenges: &mut ChallengeGenerator<G::ScalarField, S>,
rands: impl IntoIterator<Item = &'a Self::CommitmentState>,
states: impl IntoIterator<Item = &'a Self::CommitmentState>,
rng: Option<&mut dyn RngCore>,
) -> Result<BatchLCProof<G::ScalarField, Self::BatchProof>, Self::Error>
where
Expand All @@ -887,13 +887,13 @@ where
{
let label_poly_map = polynomials
.into_iter()
.zip(rands)
.zip(states)
.zip(commitments)
.map(|((p, r), c)| (p.label(), (p, r, c)))
.map(|((p, s), c)| (p.label(), (p, s, c)))
.collect::<BTreeMap<_, _>>();

let mut lc_polynomials = Vec::new();
let mut lc_randomness = Vec::new();
let mut lc_states = Vec::new();
let mut lc_commitments = Vec::new();
let mut lc_info = Vec::new();

Expand Down Expand Up @@ -951,7 +951,7 @@ where
let lc_poly =
LabeledPolynomial::new(lc_label.clone(), poly, degree_bound, hiding_bound);
lc_polynomials.push(lc_poly);
lc_randomness.push(Randomness {
lc_states.push(Randomness {
rand: combined_rand,
shifted_rand: combined_shifted_rand,
});
Expand All @@ -972,7 +972,7 @@ where
lc_commitments.iter(),
&query_set,
opening_challenges,
lc_randomness.iter(),
lc_states.iter(),
rng,
)?;
Ok(BatchLCProof { proof, evals: None })
Expand Down
1 change: 1 addition & 0 deletions poly-commit/src/kzg10/data_structures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ impl<F: PrimeField, P: DenseUVPolynomial<F>> Randomness<F, P> {
}

impl<F: PrimeField, P: DenseUVPolynomial<F>> PCCommitmentState for Randomness<F, P> {
type Randomness = Self;
fn empty() -> Self {
Self {
blinding_polynomial: P::zero(),
Expand Down
1 change: 1 addition & 0 deletions poly-commit/src/marlin/marlin_pc/data_structures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ impl<'a, F: PrimeField, P: DenseUVPolynomial<F>> AddAssign<(F, &'a Randomness<F,
}

impl<F: PrimeField, P: DenseUVPolynomial<F>> PCCommitmentState for Randomness<F, P> {
type Randomness = Self;
fn empty() -> Self {
Self {
rand: kzg10::Randomness::empty(),
Expand Down
26 changes: 13 additions & 13 deletions poly-commit/src/marlin/marlin_pc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ where
let commit_time = start_timer!(|| "Committing to polynomials");

let mut commitments = Vec::new();
let mut randomness = Vec::new();
let mut states = Vec::new();

for p in polynomials {
let label = p.label();
Expand Down Expand Up @@ -232,17 +232,17 @@ where
};

let comm = Commitment { comm, shifted_comm };
let rand = Randomness { rand, shifted_rand };
let state = Randomness { rand, shifted_rand };
commitments.push(LabeledCommitment::new(
label.to_string(),
comm,
degree_bound,
));
randomness.push(rand);
states.push(state);
end_timer!(commit_time);
}
end_timer!(commit_time);
Ok((commitments, randomness))
Ok((commitments, states))
}

/// On input a polynomial `p` and a point `point`, outputs a proof for the same.
Expand All @@ -252,7 +252,7 @@ where
_commitments: impl IntoIterator<Item = &'a LabeledCommitment<Self::Commitment>>,
point: &'a P::Point,
opening_challenges: &mut ChallengeGenerator<E::ScalarField, S>,
rands: impl IntoIterator<Item = &'a Self::CommitmentState>,
states: impl IntoIterator<Item = &'a Self::CommitmentState>,
_rng: Option<&mut dyn RngCore>,
) -> Result<Self::Proof, Self::Error>
where
Expand All @@ -267,7 +267,7 @@ where
let mut shifted_r_witness = P::zero();

let mut enforce_degree_bound = false;
for (polynomial, rand) in labeled_polynomials.into_iter().zip(rands) {
for (polynomial, rand) in labeled_polynomials.into_iter().zip(states) {
let degree_bound = polynomial.degree_bound();
assert_eq!(degree_bound.is_some(), rand.shifted_rand.is_some());

Expand Down Expand Up @@ -408,7 +408,7 @@ where
commitments: impl IntoIterator<Item = &'a LabeledCommitment<Self::Commitment>>,
query_set: &QuerySet<P::Point>,
opening_challenges: &mut ChallengeGenerator<E::ScalarField, S>,
rands: impl IntoIterator<Item = &'a Self::CommitmentState>,
states: impl IntoIterator<Item = &'a Self::CommitmentState>,
rng: Option<&mut dyn RngCore>,
) -> Result<BatchLCProof<E::ScalarField, Self::BatchProof>, Self::Error>
where
Expand All @@ -423,7 +423,7 @@ where
commitments,
query_set,
opening_challenges,
rands,
states,
rng,
)
}
Expand Down Expand Up @@ -463,7 +463,7 @@ where
commitments: impl IntoIterator<Item = &'a LabeledCommitment<Commitment<E>>>,
query_set: &QuerySet<P::Point>,
opening_challenges: &mut ChallengeGenerator<E::ScalarField, S>,
rands: impl IntoIterator<Item = &'a Self::CommitmentState>,
states: impl IntoIterator<Item = &'a Self::CommitmentState>,
rng: Option<&mut dyn RngCore>,
) -> Result<Vec<kzg10::Proof<E>>, Error>
where
Expand All @@ -474,7 +474,7 @@ where
let rng = &mut crate::optional_rng::OptionalRng(rng);
let poly_rand_comm: BTreeMap<_, _> = labeled_polynomials
.into_iter()
.zip(rands)
.zip(states)
.zip(commitments.into_iter())
.map(|((poly, r), comm)| (poly.label(), (poly, r, comm)))
.collect();
Expand All @@ -497,7 +497,7 @@ where
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::CommitmentState> = Vec::new();
let mut query_states: Vec<&'a Self::CommitmentState> = Vec::new();
let mut query_comms: Vec<&'a LabeledCommitment<Self::Commitment>> = Vec::new();

for label in labels {
Expand All @@ -507,7 +507,7 @@ where
})?;

query_polys.push(polynomial);
query_rands.push(rand);
query_states.push(rand);
query_comms.push(comm);
}

Expand All @@ -518,7 +518,7 @@ where
query_comms,
point,
opening_challenges,
query_rands,
query_states,
Some(rng),
)?;

Expand Down
1 change: 1 addition & 0 deletions poly-commit/src/marlin/marlin_pst13_pc/data_structures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ where
P: DenseMVPolynomial<E::ScalarField>,
P::Point: Index<usize, Output = E::ScalarField>,
{
type Randomness = Self;
fn empty() -> Self {
Self {
blinding_polynomial: P::zero(),
Expand Down
10 changes: 5 additions & 5 deletions poly-commit/src/marlin/marlin_pst13_pc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ where
_commitments: impl IntoIterator<Item = &'a LabeledCommitment<Self::Commitment>>,
point: &P::Point,
opening_challenges: &mut ChallengeGenerator<E::ScalarField, S>,
rands: impl IntoIterator<Item = &'a Self::CommitmentState>,
states: impl IntoIterator<Item = &'a Self::CommitmentState>,
_rng: Option<&mut dyn RngCore>,
) -> Result<Self::Proof, Self::Error>
where
Expand All @@ -452,14 +452,14 @@ where
// Compute random linear combinations of committed polynomials and randomness
let mut p = P::zero();
let mut r = Randomness::empty();
for (polynomial, rand) in labeled_polynomials.into_iter().zip(rands) {
for (polynomial, state) in labeled_polynomials.into_iter().zip(states) {
Self::check_degrees_and_bounds(ck.supported_degree, &polynomial)?;

// compute challenge^j and challenge^{j+1}.
let challenge_j = opening_challenges.try_next_challenge_of_size(CHALLENGE_SIZE);

p += (challenge_j, polynomial.polynomial());
r += (challenge_j, rand);
r += (challenge_j, state);
}

let open_time = start_timer!(|| format!("Opening polynomial of degree {}", p.degree()));
Expand Down Expand Up @@ -661,7 +661,7 @@ where
commitments: impl IntoIterator<Item = &'a LabeledCommitment<Self::Commitment>>,
query_set: &QuerySet<P::Point>,
opening_challenges: &mut ChallengeGenerator<E::ScalarField, S>,
rands: impl IntoIterator<Item = &'a Self::CommitmentState>,
states: impl IntoIterator<Item = &'a Self::CommitmentState>,
rng: Option<&mut dyn RngCore>,
) -> Result<BatchLCProof<E::ScalarField, Self::BatchProof>, Self::Error>
where
Expand All @@ -676,7 +676,7 @@ where
commitments,
query_set,
opening_challenges,
rands,
states,
rng,
)
}
Expand Down
Loading

0 comments on commit 1c04050

Please sign in to comment.