From e265f0463d87e955059952a43f14f731bfee75b0 Mon Sep 17 00:00:00 2001 From: mmagician Date: Tue, 14 Nov 2023 11:30:55 +0100 Subject: [PATCH 1/5] conversion to `into_iter` is a no-op --- poly-commit/src/linear_codes/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/poly-commit/src/linear_codes/mod.rs b/poly-commit/src/linear_codes/mod.rs index ccddbb4e..14c54d39 100644 --- a/poly-commit/src/linear_codes/mod.rs +++ b/poly-commit/src/linear_codes/mod.rs @@ -238,7 +238,7 @@ where let mut commitments = Vec::new(); let mut states = Vec::new(); - for labeled_polynomial in polynomials.into_iter() { + for labeled_polynomial in polynomials { let polynomial = labeled_polynomial.polynomial(); // 1. Arrange the coefficients of the polynomial into a matrix, From 31b717926bfe72f6448d1fff95380b4ad76bdd0f Mon Sep 17 00:00:00 2001 From: mmagician Date: Tue, 14 Nov 2023 11:41:46 +0100 Subject: [PATCH 2/5] remove explicit casts to vecs --- poly-commit/src/linear_codes/mod.rs | 74 +++++++++++++---------------- 1 file changed, 33 insertions(+), 41 deletions(-) diff --git a/poly-commit/src/linear_codes/mod.rs b/poly-commit/src/linear_codes/mod.rs index 14c54d39..6b539380 100644 --- a/poly-commit/src/linear_codes/mod.rs +++ b/poly-commit/src/linear_codes/mod.rs @@ -301,7 +301,7 @@ where fn open<'a>( ck: &Self::CommitterKey, - labeled_polynomials: impl IntoIterator>, + _labeled_polynomials: impl IntoIterator>, commitments: impl IntoIterator>, point: &'a P::Point, _challenge_generator: &mut crate::challenge::ChallengeGenerator, @@ -314,22 +314,10 @@ where Self::Commitment: 'a, { let mut proof_array = LPCPArray::default(); - let labeled_commitments: Vec<&'a LabeledCommitment> = - commitments.into_iter().collect(); - let labeled_polynomials: Vec<&'a LabeledPolynomial> = - labeled_polynomials.into_iter().collect(); - let states: Vec<&'a Self::CommitmentState> = states.into_iter().collect(); - - if labeled_commitments.len() != labeled_polynomials.len() { - return Err(Error::IncorrectInputLength(format!( - "Mismatched lengths: {} commitments, {} polynomials", - labeled_commitments.len(), - labeled_polynomials.len() - ))); - } + let mut states = states.into_iter(); - for i in 0..labeled_polynomials.len() { - let commitment = labeled_commitments[i].commitment(); + for labeled_commitments in commitments { + let commitment = labeled_commitments.commitment(); let n_rows = commitment.metadata.n_rows; let n_cols = commitment.metadata.n_cols; let root = &commitment.root; @@ -341,7 +329,12 @@ where mat, ext_mat, leaves: col_hashes, - } = states[i]; + } = states.next().ok_or_else(|| { + Error::IncorrectInputLength( + "Mismatched lengths: missing CommitmentState for the provided commitment" + .to_string(), + ) + })?; let mut col_hashes: Vec = col_hashes.clone().into_iter().map(|h| h.into()).collect(); @@ -416,25 +409,16 @@ where where Self::Commitment: 'a, { - let labeled_commitments: Vec<&'a LabeledCommitment> = - commitments.into_iter().collect(); - let values: Vec = values.into_iter().collect(); - - if labeled_commitments.len() != proof_array.len() - || labeled_commitments.len() != values.len() - { - return Err(Error::IncorrectInputLength( - format!( - "Mismatched lengths: {} proofs were provided for {} commitments with {} claimed values",labeled_commitments.len(), proof_array.len(), values.len() - ) - )); - } + let mut values = values.into_iter(); + let leaf_hash_param: &<::LeafHash as CRHScheme>::Parameters = vk.leaf_hash_param(); let two_to_one_hash_param: &<::TwoToOneHash as TwoToOneCRHScheme>::Parameters = vk.two_to_one_hash_param(); - for (i, labeled_commitment) in labeled_commitments.iter().enumerate() { + let mut i = 0; + for labeled_commitment in commitments { + let proof = &proof_array[i]; let commitment = labeled_commitment.commitment(); let n_rows = commitment.metadata.n_rows; let n_cols = commitment.metadata.n_cols; @@ -448,10 +432,10 @@ where .map_err(|_| Error::TranscriptError)?; let out = if vk.check_well_formedness() { - if proof_array[i].well_formedness.is_none() { + if proof.well_formedness.is_none() { return Err(Error::InvalidCommitment); } - let tmp = &proof_array[i].well_formedness.as_ref(); + let tmp = &proof.well_formedness.as_ref(); let well_formedness = tmp.unwrap(); let mut r = Vec::with_capacity(n_rows); for _ in 0..n_rows { @@ -480,14 +464,14 @@ where .map_err(|_| Error::TranscriptError)?; } transcript - .append_serializable_element(b"v", &proof_array[i].opening.v) + .append_serializable_element(b"v", &proof.opening.v) .map_err(|_| Error::TranscriptError)?; // 2. Ask random oracle for the `t` indices where the checks happen. let indices = get_indices_from_transcript::(n_ext_cols, t, &mut transcript)?; // 3. Hash the received columns into leaf hashes. - let col_hashes: Vec = proof_array[i] + let col_hashes: Vec = proof .opening .columns .iter() @@ -503,7 +487,7 @@ where // even if we have a well-formedness check (i.e., we save sending and checking the columns). // See "Concrete optimizations to the commitment scheme", p.12 of [Brakedown](https://eprint.iacr.org/2021/1043.pdf). for (j, (leaf, q_j)) in col_hashes.iter().zip(indices.iter()).enumerate() { - let path = &proof_array[i].opening.paths[j]; + let path = &proof.opening.paths[j]; if path.leaf_index != *q_j { return Err(Error::InvalidCommitment); } @@ -522,7 +506,7 @@ where }; // 5. Compute the encoding w = E(v). - let w = L::encode(&proof_array[i].opening.v, vk)?; + let w = L::encode(&proof.opening.v, vk)?; // 6. Compute `a`, `b` to right- and left- multiply with the matrix `M`. let (a, b) = L::tensor(point, n_cols, n_rows); @@ -535,12 +519,12 @@ where for (transcript_index, matrix_index) in indices.iter().enumerate() { check_inner_product( &r, - &proof_array[i].opening.columns[transcript_index], + &proof.opening.columns[transcript_index], w_well_formedness[*matrix_index], )?; check_inner_product( &b, - &proof_array[i].opening.columns[transcript_index], + &proof.opening.columns[transcript_index], w[*matrix_index], )?; } @@ -548,16 +532,24 @@ where for (transcript_index, matrix_index) in indices.iter().enumerate() { check_inner_product( &b, - &proof_array[i].opening.columns[transcript_index], + &proof.opening.columns[transcript_index], w[*matrix_index], )?; } } - if inner_product(&proof_array[i].opening.v, &a) != values[i] { + if inner_product(&proof.opening.v, &a) + != values.next().ok_or_else(|| { + Error::IncorrectInputLength( + "Mismatched lengths: not enough claimed values for the provided commitments" + .to_string(), + ) + })? + { eprintln!("Function check: claimed value in position {i} does not match the evaluation of the committed polynomial in the same position"); return Ok(false); } + i += 1; } Ok(true) From 1befeeec19c94ba68f068dd75ca220d84b8ecfb9 Mon Sep 17 00:00:00 2001 From: mmagician Date: Tue, 14 Nov 2023 12:35:57 +0100 Subject: [PATCH 3/5] rename to use singular of `labeled_commitment` --- poly-commit/src/linear_codes/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/poly-commit/src/linear_codes/mod.rs b/poly-commit/src/linear_codes/mod.rs index 6b539380..83e3ad65 100644 --- a/poly-commit/src/linear_codes/mod.rs +++ b/poly-commit/src/linear_codes/mod.rs @@ -316,8 +316,8 @@ where let mut proof_array = LPCPArray::default(); let mut states = states.into_iter(); - for labeled_commitments in commitments { - let commitment = labeled_commitments.commitment(); + for labeled_commitment in commitments { + let commitment = labeled_commitment.commitment(); let n_rows = commitment.metadata.n_rows; let n_cols = commitment.metadata.n_cols; let root = &commitment.root; From 8329039a40d6daafed9d30fb3b3ecc8aefe700e7 Mon Sep 17 00:00:00 2001 From: mmagician Date: Tue, 14 Nov 2023 12:59:42 +0100 Subject: [PATCH 4/5] simplify the iterators even further by zipping two iters --- poly-commit/src/linear_codes/mod.rs | 27 ++++++--------------------- 1 file changed, 6 insertions(+), 21 deletions(-) diff --git a/poly-commit/src/linear_codes/mod.rs b/poly-commit/src/linear_codes/mod.rs index 83e3ad65..74b89789 100644 --- a/poly-commit/src/linear_codes/mod.rs +++ b/poly-commit/src/linear_codes/mod.rs @@ -314,9 +314,8 @@ where Self::Commitment: 'a, { let mut proof_array = LPCPArray::default(); - let mut states = states.into_iter(); - for labeled_commitment in commitments { + for (labeled_commitment, state) in commitments.into_iter().zip(states) { let commitment = labeled_commitment.commitment(); let n_rows = commitment.metadata.n_rows; let n_cols = commitment.metadata.n_cols; @@ -329,12 +328,7 @@ where mat, ext_mat, leaves: col_hashes, - } = states.next().ok_or_else(|| { - Error::IncorrectInputLength( - "Mismatched lengths: missing CommitmentState for the provided commitment" - .to_string(), - ) - })?; + } = state; let mut col_hashes: Vec = col_hashes.clone().into_iter().map(|h| h.into()).collect(); @@ -409,15 +403,13 @@ where where Self::Commitment: 'a, { - let mut values = values.into_iter(); - let leaf_hash_param: &<::LeafHash as CRHScheme>::Parameters = vk.leaf_hash_param(); let two_to_one_hash_param: &<::TwoToOneHash as TwoToOneCRHScheme>::Parameters = vk.two_to_one_hash_param(); - let mut i = 0; - for labeled_commitment in commitments { + // let mut i = 0; + for (i, (labeled_commitment, value)) in commitments.into_iter().zip(values).enumerate() { let proof = &proof_array[i]; let commitment = labeled_commitment.commitment(); let n_rows = commitment.metadata.n_rows; @@ -538,18 +530,11 @@ where } } - if inner_product(&proof.opening.v, &a) - != values.next().ok_or_else(|| { - Error::IncorrectInputLength( - "Mismatched lengths: not enough claimed values for the provided commitments" - .to_string(), - ) - })? - { + if inner_product(&proof.opening.v, &a) != value { eprintln!("Function check: claimed value in position {i} does not match the evaluation of the committed polynomial in the same position"); return Ok(false); } - i += 1; + // i += 1; } Ok(true) From 64988e0f8ea0c443197562c6a48fce9896e0a706 Mon Sep 17 00:00:00 2001 From: mmagician Date: Tue, 14 Nov 2023 13:04:42 +0100 Subject: [PATCH 5/5] Apply suggestions from code review --- poly-commit/src/linear_codes/mod.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/poly-commit/src/linear_codes/mod.rs b/poly-commit/src/linear_codes/mod.rs index 74b89789..14ac8306 100644 --- a/poly-commit/src/linear_codes/mod.rs +++ b/poly-commit/src/linear_codes/mod.rs @@ -408,7 +408,6 @@ where let two_to_one_hash_param: &<::TwoToOneHash as TwoToOneCRHScheme>::Parameters = vk.two_to_one_hash_param(); - // let mut i = 0; for (i, (labeled_commitment, value)) in commitments.into_iter().zip(values).enumerate() { let proof = &proof_array[i]; let commitment = labeled_commitment.commitment(); @@ -534,7 +533,6 @@ where eprintln!("Function check: claimed value in position {i} does not match the evaluation of the committed polynomial in the same position"); return Ok(false); } - // i += 1; } Ok(true)