From b7bcd077c011f9f68e8f88376796b24d51f94127 Mon Sep 17 00:00:00 2001 From: mmagician Date: Fri, 5 Apr 2024 16:26:51 +0200 Subject: [PATCH 1/5] `point_to_vec` takes reference, returns ref. Avoids cloning --- poly-commit/src/linear_codes/mod.rs | 6 +++--- poly-commit/src/linear_codes/multilinear_brakedown/mod.rs | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/poly-commit/src/linear_codes/mod.rs b/poly-commit/src/linear_codes/mod.rs index 30b524c0..1fd10658 100644 --- a/poly-commit/src/linear_codes/mod.rs +++ b/poly-commit/src/linear_codes/mod.rs @@ -106,7 +106,7 @@ where fn poly_to_vec(polynomial: &P) -> Vec; /// Represent the query point as a vector of Field elements. - fn point_to_vec(point: P::Point) -> Vec; + fn point_to_vec(point: &P::Point) -> &Vec; /// Arrange the coefficients of the polynomial into a matrix, /// and apply encoding to each row. @@ -349,7 +349,7 @@ where None }; - let point_vec = L::point_to_vec(point.clone()); + let point_vec = L::point_to_vec(point); sponge.absorb(&point_vec); proof_array.push(LinCodePCProof { @@ -415,7 +415,7 @@ where // 1. Seed the transcript with the point and the recieved vector // TODO Consider removing the evaluation point from the transcript. - let point_vec = L::point_to_vec(point.clone()); + let point_vec = L::point_to_vec(&point); sponge.absorb(&point_vec); sponge.absorb(&proof.opening.v); diff --git a/poly-commit/src/linear_codes/multilinear_brakedown/mod.rs b/poly-commit/src/linear_codes/multilinear_brakedown/mod.rs index bcfd9f0a..8785fb6c 100644 --- a/poly-commit/src/linear_codes/multilinear_brakedown/mod.rs +++ b/poly-commit/src/linear_codes/multilinear_brakedown/mod.rs @@ -92,7 +92,7 @@ where polynomial.to_evaluations() } - fn point_to_vec(point:

>::Point) -> Vec { + fn point_to_vec(point: &

>::Point) -> &Vec { point } @@ -103,7 +103,7 @@ where left_len: usize, _right_len: usize, ) -> (Vec, Vec) { - let point: Vec = Self::point_to_vec(point.clone()); + let point: &Vec = Self::point_to_vec(point); let split = log2(left_len) as usize; let left = &point[..split]; From c7abfd7fb68128d95fcb586b650085ba408ecb0f Mon Sep 17 00:00:00 2001 From: mmagician Date: Fri, 5 Apr 2024 22:03:56 +0200 Subject: [PATCH 2/5] can skip cloning the column during verification column (&Vec) implements borrow --- poly-commit/src/linear_codes/mod.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/poly-commit/src/linear_codes/mod.rs b/poly-commit/src/linear_codes/mod.rs index 1fd10658..8292d47c 100644 --- a/poly-commit/src/linear_codes/mod.rs +++ b/poly-commit/src/linear_codes/mod.rs @@ -162,7 +162,7 @@ where P: Polynomial, S: CryptographicSponge, C: Config + 'static, - Vec: Borrow<::Input>, + for<'a> &'a Vec: Borrow<::Input>, H::Output: Into + Send, C::Leaf: Sized + Clone + Default + Send + AsRef, H: CRHScheme + 'static, @@ -254,7 +254,7 @@ where let ext_mat_cols = ext_mat.cols(); let leaves: Vec = cfg_into_iter!(ext_mat_cols) .map(|col| { - H::evaluate(ck.col_hash_params(), col) + H::evaluate(ck.col_hash_params(), &col) .map_err(|_| Error::HashingError) .unwrap() }) @@ -428,7 +428,7 @@ where .columns .iter() .map(|c| { - H::evaluate(vk.col_hash_params(), c.clone()) + H::evaluate(vk.col_hash_params(), c) .map_err(|_| Error::HashingError) .unwrap() .into() From 3838223cd2fa80de04316de0b9499f70b525076a Mon Sep 17 00:00:00 2001 From: mmagician Date: Fri, 5 Apr 2024 22:05:48 +0200 Subject: [PATCH 3/5] also no need to clone leaves for checking merkle paths, consume directly --- 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 8292d47c..b7224372 100644 --- a/poly-commit/src/linear_codes/mod.rs +++ b/poly-commit/src/linear_codes/mod.rs @@ -438,13 +438,13 @@ where // 4. Verify the paths for each of the leaf hashes - this is only run once, // 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() { + for (j, (leaf, q_j)) in col_hashes.into_iter().zip(indices.iter()).enumerate() { let path = &proof.opening.paths[j]; if path.leaf_index != *q_j { return Err(Error::InvalidCommitment); } - path.verify(leaf_hash_param, two_to_one_hash_param, root, leaf.clone()) + path.verify(leaf_hash_param, two_to_one_hash_param, root, leaf) .map_err(|_| Error::InvalidCommitment)?; } From 06f574cc33200bae5c7510659ff7b0028c72bd3e Mon Sep 17 00:00:00 2001 From: mmagician Date: Fri, 5 Apr 2024 22:06:53 +0200 Subject: [PATCH 4/5] remove unnecessary clone of hash params --- poly-commit/src/linear_codes/mod.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/poly-commit/src/linear_codes/mod.rs b/poly-commit/src/linear_codes/mod.rs index b7224372..82fe9b3f 100644 --- a/poly-commit/src/linear_codes/mod.rs +++ b/poly-commit/src/linear_codes/mod.rs @@ -193,8 +193,7 @@ where ) -> Result { let leaf_hash_param = ::setup(rng).unwrap(); let two_to_one_hash_param = ::setup(rng) - .unwrap() - .clone(); + .unwrap(); let col_hash_params = ::setup(rng).unwrap(); let pp = L::setup::( max_degree, From 0c1e6c35aeb97e8ed7344d1d2db015deee80676b Mon Sep 17 00:00:00 2001 From: mmagician Date: Fri, 5 Apr 2024 22:17:15 +0200 Subject: [PATCH 5/5] fmt --- poly-commit/src/linear_codes/mod.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/poly-commit/src/linear_codes/mod.rs b/poly-commit/src/linear_codes/mod.rs index 82fe9b3f..614f6445 100644 --- a/poly-commit/src/linear_codes/mod.rs +++ b/poly-commit/src/linear_codes/mod.rs @@ -192,8 +192,7 @@ where rng: &mut R, ) -> Result { let leaf_hash_param = ::setup(rng).unwrap(); - let two_to_one_hash_param = ::setup(rng) - .unwrap(); + let two_to_one_hash_param = ::setup(rng).unwrap(); let col_hash_params = ::setup(rng).unwrap(); let pp = L::setup::( max_degree,