diff --git a/src/circuit.rs b/src/circuit.rs index aa3792b..249c206 100644 --- a/src/circuit.rs +++ b/src/circuit.rs @@ -1,5 +1,5 @@ #![allow(non_snake_case)] -///! Definition and implementation of the Bulletproofs++ arithmetic circuit protocol. +//! Definition and implementation of the Bulletproofs++ arithmetic circuit protocol. use std::ops::{Add, Mul, Sub}; use k256::{AffinePoint, ProjectivePoint, Scalar}; @@ -51,8 +51,8 @@ impl From<&SerializableProof> for Proof { c_r: ProjectivePoint::from(&value.c_r), c_o: ProjectivePoint::from(&value.c_o), c_s: ProjectivePoint::from(&value.c_s), - r: value.r.iter().map(|r_val| ProjectivePoint::from(r_val)).collect::>(), - x: value.x.iter().map(|x_val| ProjectivePoint::from(x_val)).collect::>(), + r: value.r.iter().map(ProjectivePoint::from).collect::>(), + x: value.x.iter().map(ProjectivePoint::from).collect::>(), l: value.l.clone(), n: value.n.clone(), }; @@ -142,15 +142,15 @@ impl

ArithmeticCircuit

P: Fn(PartitionType, usize) -> Option { /// Creates commitment to the arithmetic circuit witness. - pub fn commit(&self, v: &Vec, s: &Scalar) -> ProjectivePoint { + pub fn commit(&self, v: &[Scalar], s: &Scalar) -> ProjectivePoint { self. g.mul(&v[0]). add(&self.h_vec[0].mul(s)). - add(&vector_mul(&self.h_vec[9..].to_vec(), &v[1..].to_vec())) + add(&vector_mul(&self.h_vec[9..], &v[1..])) } /// Verifies arithmetic circuit proof with respect to the `v` commitments vector. - pub fn verify(&self, v: &Vec, t: &mut Transcript, proof: Proof) -> bool { + pub fn verify(&self, v: &[ProjectivePoint], t: &mut Transcript, proof: Proof) -> bool { transcript::app_point(b"commitment_cl", &proof.c_l, t); transcript::app_point(b"commitment_cr", &proof.c_r, t); transcript::app_point(b"commitment_co", &proof.c_o, t); @@ -222,7 +222,7 @@ impl

ArithmeticCircuit

cl_tau = vector_mul_on_scalar(&cl_tau, &Scalar::from(2u32)); cl_tau = vector_sub(&cl_tau, &c_l0); - let mut c = Vec::from([&cr_tau[..], &cl_tau[..]].concat()); + let mut c = [&cr_tau[..], &cl_tau[..]].concat(); let commitment = pt. add(&proof.c_s.mul(&tau_inv)). @@ -236,25 +236,25 @@ impl

ArithmeticCircuit

} let wnla = WeightNormLinearArgument { - g: ProjectivePoint::from(self.g), - g_vec: Vec::from([&self.g_vec[..], &self.g_vec_[..]].concat()), - h_vec: Vec::from([&self.h_vec[..], &self.h_vec_[..]].concat()), + g: self.g, + g_vec: [&self.g_vec[..], &self.g_vec_[..]].concat(), + h_vec: [&self.h_vec[..], &self.h_vec_[..]].concat(), c, rho, mu, }; - return wnla.verify(&commitment, t, wnla::Proof { + wnla.verify(&commitment, t, wnla::Proof { r: proof.r, x: proof.x, l: proof.l, n: proof.n, - }); + }) } /// Creates arithmetic circuit proof for the corresponding witness. Also, `v` commitments vector /// should correspond input witness in `witness` argument. - pub fn prove(&self, v: &Vec, witness: Witness, t: &mut Transcript, rng: &mut R) -> Proof + pub fn prove(&self, v: &[ProjectivePoint], witness: Witness, t: &mut Transcript, rng: &mut R) -> Proof where R: RngCore + CryptoRng { @@ -299,7 +299,7 @@ impl

ArithmeticCircuit

let no = (0..self.dim_nm).map(|j| if let Some(i) = (self.partition)(PartitionType::NO, j) { - Scalar::from(witness.w_o[i]) + witness.w_o[i] } else { Scalar::ZERO } @@ -307,7 +307,7 @@ impl

ArithmeticCircuit

let lo = (0..self.dim_nv).map(|j| if let Some(i) = (self.partition)(PartitionType::LO, j) { - Scalar::from(witness.w_o[i]) + witness.w_o[i] } else { Scalar::ZERO } @@ -315,7 +315,7 @@ impl

ArithmeticCircuit

let ll = (0..self.dim_nv).map(|j| if let Some(i) = (self.partition)(PartitionType::LL, j) { - Scalar::from(witness.w_o[i]) + witness.w_o[i] } else { Scalar::ZERO } @@ -323,22 +323,22 @@ impl

ArithmeticCircuit

let lr = (0..self.dim_nv).map(|j| if let Some(i) = (self.partition)(PartitionType::LR, j) { - Scalar::from(witness.w_o[i]) + witness.w_o[i] } else { Scalar::ZERO } ).collect::>(); let co = - vector_mul(&self.h_vec, &Vec::from([&ro[..], &lo[..]].concat())). + vector_mul(&self.h_vec, &[&ro[..], &lo[..]].concat()). add(vector_mul(&self.g_vec, &no)); let cl = - vector_mul(&self.h_vec, &Vec::from([&rl[..], &ll[..]].concat())). + vector_mul(&self.h_vec, &[&rl[..], &ll[..]].concat()). add(vector_mul(&self.g_vec, &nl)); let cr = - vector_mul(&self.h_vec, &Vec::from([&rr[..], &lr[..]].concat())). + vector_mul(&self.h_vec, &[&rr[..], &lr[..]].concat()). add(vector_mul(&self.g_vec, &nr)); transcript::app_point(b"commitment_cl", &cl, t); @@ -365,8 +365,8 @@ impl

ArithmeticCircuit

c_lO ) = self.collect_c(&lambda_vec, &mu_vec, &mu); - let ls = (0..self.dim_nv).map(|_| Scalar::generate_biased(rng)).collect(); - let ns = (0..self.dim_nm).map(|_| Scalar::generate_biased(rng)).collect(); + let ls = (0..self.dim_nv).map(|_| Scalar::generate_biased(rng)).collect::>(); + let ns = (0..self.dim_nm).map(|_| Scalar::generate_biased(rng)).collect::>(); let mut v_0 = Scalar::ZERO; (0..self.k). @@ -385,7 +385,7 @@ impl

ArithmeticCircuit

let mut v_1 = vec![Scalar::ZERO; self.dim_nv - 1]; (0..self.k). for_each(|i| - v_1 = vector_add(&v_1, &vector_mul_on_scalar(&witness.v[i][1..].to_vec(), &self.linear_comb_coef(i, &lambda, &mu))) + v_1 = vector_add(&v_1, &vector_mul_on_scalar(&witness.v[i][1..], &self.linear_comb_coef(i, &lambda, &mu))) ); v_1 = vector_mul_on_scalar(&v_1, &Scalar::from(2u32)); @@ -461,7 +461,7 @@ impl

ArithmeticCircuit

f_[7].mul(&beta_inv).add(&ro[7].mul(&delta)).sub(&rl[6]).add(&rr[5]), ]; - let cs = vector_mul(&self.h_vec, &Vec::from([&rs[..], &ls[..]].concat())). + let cs = vector_mul(&self.h_vec, &[&rs[..], &ls[..]].concat()). add(vector_mul(&self.g_vec, &ns)); transcript::app_point(b"commitment_cs", &cs, t); @@ -471,11 +471,11 @@ impl

ArithmeticCircuit

let tau2 = tau.mul(&tau); let tau3 = tau2.mul(&tau); - let mut l = vector_mul_on_scalar(&Vec::from([&rs[..], &ls[..]].concat()), &tau_inv); - l = vector_sub(&l, &vector_mul_on_scalar(&Vec::from([&ro[..], &lo[..]].concat()), &delta)); - l = vector_add(&l, &vector_mul_on_scalar(&Vec::from([&rl[..], &ll[..]].concat()), &tau)); - l = vector_sub(&l, &vector_mul_on_scalar(&Vec::from([&rr[..], &lr[..]].concat()), &tau2)); - l = vector_add(&l, &vector_mul_on_scalar(&Vec::from([&rv[..], &v_1[..]].concat()), &tau3)); + let mut l = vector_mul_on_scalar(&[&rs[..], &ls[..]].concat(), &tau_inv); + l = vector_sub(&l, &vector_mul_on_scalar(&[&ro[..], &lo[..]].concat(), &delta)); + l = vector_add(&l, &vector_mul_on_scalar(&[&rl[..], &ll[..]].concat(), &tau)); + l = vector_sub(&l, &vector_mul_on_scalar(&[&rr[..], &lr[..]].concat(), &tau2)); + l = vector_add(&l, &vector_mul_on_scalar(&[&rv[..], &v_1[..]].concat(), &tau3)); let mut pn_tau = vector_mul_on_scalar(&c_nO, &tau3.mul(&delta_inv)); pn_tau = vector_sub(&pn_tau, &vector_mul_on_scalar(&c_nL, &tau2)); @@ -510,7 +510,7 @@ impl

ArithmeticCircuit

cl_tau = vector_mul_on_scalar(&cl_tau, &Scalar::from(2u32)); cl_tau = vector_sub(&cl_tau, &c_l0); - let mut c = Vec::from([&cr_tau[..], &cl_tau[..]].concat()); + let mut c = [&cr_tau[..], &cl_tau[..]].concat(); let v = ps_tau.add(&tau3.mul(&v_0)); @@ -528,9 +528,9 @@ impl

ArithmeticCircuit

} let wnla = WeightNormLinearArgument { - g: ProjectivePoint::from(self.g), - g_vec: Vec::from([&self.g_vec[..], &self.g_vec_[..]].concat()), - h_vec: Vec::from([&self.h_vec[..], &self.h_vec_[..]].concat()), + g: self.g, + g_vec: [&self.g_vec[..], &self.g_vec_[..]].concat(), + h_vec: [&self.h_vec[..], &self.h_vec_[..]].concat(), c, rho, mu, @@ -538,7 +538,7 @@ impl

ArithmeticCircuit

let proof_wnla = wnla.prove(&commitment, t, l, n); - return Proof { + Proof { c_l: cl, c_r: cr, c_o: co, @@ -547,18 +547,18 @@ impl

ArithmeticCircuit

x: proof_wnla.x, l: proof_wnla.l, n: proof_wnla.n, - }; + } } fn linear_comb_coef(&self, i: usize, lambda: &Scalar, mu: &Scalar) -> Scalar { let mut coef = Scalar::ZERO; if self.f_l { - coef = coef.add(pow(&lambda, self.dim_nv * i)) + coef = coef.add(pow(lambda, self.dim_nv * i)) } if self.f_m { - coef = coef.add(pow(&mu, self.dim_nv * i + 1)) + coef = coef.add(pow(mu, self.dim_nv * i + 1)) } coef @@ -567,30 +567,30 @@ impl

ArithmeticCircuit

fn collect_cl0(&self, lambda: &Scalar, mu: &Scalar) -> Vec { let mut c_l0 = vec![Scalar::ZERO; self.dim_nv - 1]; if self.f_l { - c_l0 = e(&lambda, self.dim_nv)[1..].to_vec(); + c_l0 = e(lambda, self.dim_nv)[1..].to_vec(); } if self.f_m { - c_l0 = vector_sub(&c_l0, &vector_mul_on_scalar(&e(&mu, self.dim_nv)[1..].to_vec(), &mu)); + c_l0 = vector_sub(&c_l0, &vector_mul_on_scalar(&e(mu, self.dim_nv)[1..], mu)); } c_l0 } - fn collect_c(&self, lambda_vec: &Vec, mu_vec: &Vec, mu: &Scalar) -> (Vec, Vec, Vec, Vec, Vec, Vec) { + fn collect_c(&self, lambda_vec: &[Scalar], mu_vec: &[Scalar], mu: &Scalar) -> (Vec, Vec, Vec, Vec, Vec, Vec) { let (M_lnL, M_mnL, M_lnR, M_mnR) = self.collect_m_rl(); let (M_lnO, M_mnO, M_llL, M_mlL, M_llR, M_mlR, M_llO, M_mlO) = self.collect_m_o(); - let mu_diag_inv = diag_inv(&mu, self.dim_nm); + let mu_diag_inv = diag_inv(mu, self.dim_nm); - let c_nL = vector_mul_on_matrix(&vector_sub(&vector_mul_on_matrix(&lambda_vec, &M_lnL), &vector_mul_on_matrix(&mu_vec, &M_mnL)), &mu_diag_inv); - let c_nR = vector_mul_on_matrix(&vector_sub(&vector_mul_on_matrix(&lambda_vec, &M_lnR), &vector_mul_on_matrix(&mu_vec, &M_mnR)), &mu_diag_inv); - let c_nO = vector_mul_on_matrix(&vector_sub(&vector_mul_on_matrix(&lambda_vec, &M_lnO), &vector_mul_on_matrix(&mu_vec, &M_mnO)), &mu_diag_inv); + let c_nL = vector_mul_on_matrix(&vector_sub(&vector_mul_on_matrix(lambda_vec, &M_lnL), &vector_mul_on_matrix(mu_vec, &M_mnL)), &mu_diag_inv); + let c_nR = vector_mul_on_matrix(&vector_sub(&vector_mul_on_matrix(lambda_vec, &M_lnR), &vector_mul_on_matrix(mu_vec, &M_mnR)), &mu_diag_inv); + let c_nO = vector_mul_on_matrix(&vector_sub(&vector_mul_on_matrix(lambda_vec, &M_lnO), &vector_mul_on_matrix(mu_vec, &M_mnO)), &mu_diag_inv); - let c_lL = vector_sub(&vector_mul_on_matrix(&lambda_vec, &M_llL), &vector_mul_on_matrix(&mu_vec, &M_mlL)); - let c_lR = vector_sub(&vector_mul_on_matrix(&lambda_vec, &M_llR), &vector_mul_on_matrix(&mu_vec, &M_mlR)); - let c_lO = vector_sub(&vector_mul_on_matrix(&lambda_vec, &M_llO), &vector_mul_on_matrix(&mu_vec, &M_mlO)); + let c_lL = vector_sub(&vector_mul_on_matrix(lambda_vec, &M_llL), &vector_mul_on_matrix(mu_vec, &M_mlL)); + let c_lR = vector_sub(&vector_mul_on_matrix(lambda_vec, &M_llR), &vector_mul_on_matrix(mu_vec, &M_mlR)); + let c_lO = vector_sub(&vector_mul_on_matrix(lambda_vec, &M_llO), &vector_mul_on_matrix(mu_vec, &M_mlO)); - return (c_nL, c_nR, c_nO, c_lL, c_lR, c_lO); + (c_nL, c_nR, c_nO, c_lL, c_lR, c_lO) } fn collect_lambda(&self, lambda: &Scalar, mu: &Scalar) -> Vec { @@ -605,7 +605,7 @@ impl

ArithmeticCircuit

); } - return lambda_vec; + lambda_vec } fn collect_m_rl(&self) -> (Vec>, Vec>, Vec>, Vec>) { @@ -613,7 +613,7 @@ impl

ArithmeticCircuit

let M_mnL = (0..self.dim_nm).map(|i| Vec::from(&self.W_m[i][..self.dim_nm])).collect::>>(); let M_lnR = (0..self.dim_nl).map(|i| Vec::from(&self.W_l[i][self.dim_nm..self.dim_nm * 2])).collect::>>(); let M_mnR = (0..self.dim_nm).map(|i| Vec::from(&self.W_m[i][self.dim_nm..self.dim_nm * 2])).collect::>>(); - return (M_lnL, M_mnL, M_lnR, M_mnR); + (M_lnL, M_mnL, M_lnR, M_mnR) } fn collect_m_o(&self) -> (Vec>, Vec>, Vec>, Vec>, Vec>, Vec>, Vec>, Vec>) { @@ -624,7 +624,7 @@ impl

ArithmeticCircuit

(0..isz).map(|i| (0..jsz).map(|j| if let Some(j_) = (self.partition)(typ, j) { - Scalar::from(W_x[i][j_]) + W_x[i][j_] } else { Scalar::ZERO } diff --git a/src/range_proof/reciprocal.rs b/src/range_proof/reciprocal.rs index ad20ff3..08614f3 100644 --- a/src/range_proof/reciprocal.rs +++ b/src/range_proof/reciprocal.rs @@ -1,6 +1,6 @@ #![allow(non_snake_case)] -///! Definition and implementation of the reciprocal range-proof protocol based on arithmetic circuits protocol. +//! Definition and implementation of the reciprocal range-proof protocol based on arithmetic circuits protocol. use std::ops::{Add, Mul}; use k256::{AffinePoint, ProjectivePoint, Scalar}; @@ -41,19 +41,19 @@ pub struct SerializableProof { impl From<&SerializableProof> for Proof { fn from(value: &SerializableProof) -> Self { - return Proof { + Proof { circuit_proof: circuit::Proof::from(&value.circuit_proof), r: ProjectivePoint::from(value.r), - }; + } } } impl From<&Proof> for SerializableProof { fn from(value: &Proof) -> Self { - return SerializableProof { + SerializableProof { circuit_proof: circuit::SerializableProof::from(&value.circuit_proof), r: value.r.to_affine(), - }; + } } } @@ -89,8 +89,8 @@ impl ReciprocalRangeProofProtocol { } /// Creates commitment for the reciprocals and blinding: `commitment = s*h_vec[0] + ` - pub fn commit_poles(&self, r: &Vec, s: &Scalar) -> ProjectivePoint { - self.h_vec[0].mul(s).add(&vector_mul(&self.h_vec[9..].to_vec(), &r)) + pub fn commit_poles(&self, r: &[Scalar], s: &Scalar) -> ProjectivePoint { + self.h_vec[0].mul(s).add(&vector_mul(&self.h_vec[9..], r)) } /// Verifies zk-proof that committed value lies in [0..dim_np^dim_nd) range. @@ -102,7 +102,7 @@ impl ReciprocalRangeProofProtocol { let circuit_commitment = commitment.add(&proof.r); - return circuit.verify(&vec![circuit_commitment], t, proof.circuit_proof); + circuit.verify(&[circuit_commitment], t, proof.circuit_proof) } /// Creates zk-proof that committed value lies in [0..dim_np^dim_nd) range. @@ -138,14 +138,14 @@ impl ReciprocalRangeProofProtocol { }; let circuit_commitment = circuit.commit(&circuit_witness.v[0], &circuit_witness.s_v[0]); - return Proof { - circuit_proof: circuit.prove::(&vec![circuit_commitment], circuit_witness, t, rng), + Proof { + circuit_proof: circuit.prove::(&[circuit_commitment], circuit_witness, t, rng), r: r_com, - }; + } } /// Creates circuit parameters based on provided challenge. For the same challenge will generate same parameters. - fn make_circuit<'a>(&'a self, e: Scalar) -> ArithmeticCircuit Option + 'a> + fn make_circuit(&self, e: Scalar) -> ArithmeticCircuit Option + '_> { let dim_nm = self.dim_nd; let dim_no = self.dim_np; @@ -196,7 +196,7 @@ impl ReciprocalRangeProofProtocol { dim_nl, dim_nv, dim_nw, - g: self.g.clone(), + g: self.g, g_vec: self.g_vec.clone(), h_vec: self.h_vec.clone(), W_m, diff --git a/src/range_proof/u64_proof.rs b/src/range_proof/u64_proof.rs index cd5cbc3..22bd01b 100644 --- a/src/range_proof/u64_proof.rs +++ b/src/range_proof/u64_proof.rs @@ -1,5 +1,5 @@ #![allow(non_snake_case)] -///! Definition and implementation of the u64 range-proof protocol based on reciprocal protocol. +//! Definition and implementation of the u64 range-proof protocol based on reciprocal protocol. use std::ops::{Add, Mul}; use k256::{ProjectivePoint, Scalar}; @@ -73,7 +73,7 @@ impl U64RangeProofProtocol { let witness = Witness { x: Scalar::from(x), - s: s.clone(), + s: *s, m: poles, digits, }; @@ -84,7 +84,7 @@ impl U64RangeProofProtocol { pub fn u64_to_hex(mut x: u64) -> Vec { (0..16).map(|_| { let val = Scalar::from(x % 16); - x = x / 16; + x /= 16; val }).collect::>() } @@ -95,7 +95,7 @@ impl U64RangeProofProtocol { (0..16).for_each(|_| { let digit = (x % 16) as usize; result[digit] = result[digit].add(Scalar::ONE); - x = x / 16; + x /= 16; }); result diff --git a/src/tests.rs b/src/tests.rs index 56dceeb..6c01c74 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -126,7 +126,7 @@ mod tests { w_o, }; - let v = (0..k).map(|i| circuit.commit(&witness.v[i], &witness.s_v[i])).collect(); + let v = (0..k).map(|i| circuit.commit(&witness.v[i], &witness.s_v[i])).collect::>(); let mut pt = merlin::Transcript::new(b"circuit test"); let proof = circuit.prove::(&v, witness, &mut pt, &mut rand); diff --git a/src/util.rs b/src/util.rs index b3fc806..0c47d19 100644 --- a/src/util.rs +++ b/src/util.rs @@ -3,7 +3,7 @@ use std::ops::{Add, Mul, Sub}; use k256::elliptic_curve::Field; use k256::Scalar; -pub fn reduce(v: &Vec) -> (Vec, Vec) where T: Copy { +pub fn reduce(v: &[T]) -> (Vec, Vec) where T: Copy { let res0 = v.iter(). enumerate(). filter(|(i, _)| *i as i32 % 2 == 0). @@ -20,11 +20,11 @@ pub fn reduce(v: &Vec) -> (Vec, Vec) where T: Copy { (res0, res1) } -pub fn vector_extend(v: &Vec, n: usize) -> Vec where T: Copy + Default { +pub fn vector_extend(v: &[T], n: usize) -> Vec where T: Copy + Default { (0..n).map(|i| if i < v.len() { v[i] } else { T::default() }).collect::>() } -pub fn weight_vector_mul(a: &Vec, b: &Vec, weight: &Scalar) -> T +pub fn weight_vector_mul(a: &[T], b: &[Scalar], weight: &Scalar) -> T where T: Copy + Default + Mul + Add { @@ -39,10 +39,10 @@ pub fn weight_vector_mul(a: &Vec, b: &Vec, weight: &Scalar) -> T result = result.add(a_val.mul(b_val.mul(&exp))); }); - return result; + result } -pub fn vector_mul(a: &Vec, b: &Vec) -> T +pub fn vector_mul(a: &[T], b: &[Scalar]) -> T where T: Copy + Default + Mul + Add { @@ -55,17 +55,17 @@ pub fn vector_mul(a: &Vec, b: &Vec) -> T result = result.add(a_val.mul(*b_val)); }); - return result; + result } -pub fn vector_mul_on_scalar<'a, T>(a: &Vec, s: &'a Scalar) -> Vec +pub fn vector_mul_on_scalar<'a, T>(a: &[T], s: &'a Scalar) -> Vec where T: Copy + Mul<&'a Scalar, Output=T> { a.iter().map(|x| x.mul(s)).collect::>() } -pub fn vector_add(a: &Vec, b: &Vec) -> Vec +pub fn vector_add(a: &[T], b: &[T]) -> Vec where T: Copy + Default + Add { @@ -74,7 +74,7 @@ pub fn vector_add(a: &Vec, b: &Vec) -> Vec a_ext.iter().zip(b_ext).map(|(a_val, b_val)| a_val.add(*b_val)).collect::>() } -pub fn vector_sub<'a, T>(a: &'a Vec, b: &'a Vec) -> Vec +pub fn vector_sub<'a, T>(a: &'a [T], b: &'a [T]) -> Vec where T: Copy + Default + Sub { @@ -94,11 +94,11 @@ pub fn e(v: &Scalar, n: usize) -> Vec { } pub fn pow(s: &Scalar, n: usize) -> Scalar { - return s.pow(&[n as u64]); + s.pow([n as u64]) } #[allow(dead_code)] -pub fn vector_hadamard_mul(a: &Vec, b: &Vec) -> Vec +pub fn vector_hadamard_mul(a: &[T], b: &[Scalar]) -> Vec where T: Copy + Default + Mul { @@ -107,11 +107,11 @@ pub fn vector_hadamard_mul(a: &Vec, b: &Vec) -> Vec a_ext.iter().zip(b_ext).map(|(a_val, b_val)| a_val.mul(*b_val)).collect::>() } -pub fn vector_tensor_mul<'a, T>(a: &'a Vec, b: &'a Vec) -> Vec +pub fn vector_tensor_mul<'a, T>(a: &'a [T], b: &'a [Scalar]) -> Vec where T: Copy + Mul<&'a Scalar, Output=T> { - b.iter().map(|x| vector_mul_on_scalar(&a, x)).collect::>>().concat() + b.iter().map(|x| vector_mul_on_scalar(a, x)).collect::>>().concat() } pub fn diag_inv(x: &Scalar, n: usize) -> Vec> { @@ -130,7 +130,7 @@ pub fn diag_inv(x: &Scalar, n: usize) -> Vec> { ).collect::>>() } -pub fn vector_mul_on_matrix(a: &Vec, m: &Vec>) -> Vec +pub fn vector_mul_on_matrix(a: &[T], m: &[Vec]) -> Vec where T: Copy + Default + Mul + Add { @@ -141,7 +141,7 @@ pub fn vector_mul_on_matrix(a: &Vec, m: &Vec>) -> Vec } #[allow(dead_code)] -pub fn matrix_mul_on_vector(a: &Vec, m: &Vec>) -> Vec +pub fn matrix_mul_on_vector(a: &[T], m: &[Vec]) -> Vec where T: Copy + Default + Mul + Add { diff --git a/src/wnla.rs b/src/wnla.rs index be5b555..e048698 100644 --- a/src/wnla.rs +++ b/src/wnla.rs @@ -1,4 +1,4 @@ -///! Definition and implementation of the Bulletproofs++ weight norm linear argument protocol. +//! Definition and implementation of the Bulletproofs++ weight norm linear argument protocol. use std::ops::{Add, Mul}; use k256::{AffinePoint, ProjectivePoint, Scalar}; use merlin::Transcript; @@ -40,8 +40,8 @@ pub struct SerializableProof { impl From<&SerializableProof> for Proof { fn from(value: &SerializableProof) -> Self { return Proof { - r: value.r.iter().map(|r_val| ProjectivePoint::from(r_val)).collect::>(), - x: value.x.iter().map(|x_val| ProjectivePoint::from(x_val)).collect::>(), + r: value.r.iter().map(ProjectivePoint::from).collect::>(), + x: value.x.iter().map(ProjectivePoint::from).collect::>(), l: value.l.clone(), n: value.n.clone(), }; @@ -62,7 +62,7 @@ impl From<&Proof> for SerializableProof { impl WeightNormLinearArgument { /// Creates weight norm linear argument commitment to vectors `l`, `n`: /// `C = v*g + + `, where `v = |n|_{mu}^2 + ` - pub fn commit(&self, l: &Vec, n: &Vec) -> ProjectivePoint { + pub fn commit(&self, l: &[Scalar], n: &[Scalar]) -> ProjectivePoint { let v = vector_mul(&self.c, l).add(weight_vector_mul(n, n, &self.mu)); self. g.mul(v). @@ -76,7 +76,7 @@ impl WeightNormLinearArgument { return false; } - if proof.x.len() == 0 { + if proof.x.is_empty() { return commitment.eq(&self.commit(&proof.l, &proof.n)); } @@ -85,8 +85,8 @@ impl WeightNormLinearArgument { let (h0, h1) = reduce(&self.h_vec); transcript::app_point(b"wnla_com", commitment, t); - transcript::app_point(b"wnla_x", &proof.x.last().unwrap(), t); - transcript::app_point(b"wnla_r", &proof.r.last().unwrap(), t); + transcript::app_point(b"wnla_x", proof.x.last().unwrap(), t); + transcript::app_point(b"wnla_r", proof.r.last().unwrap(), t); t.append_u64(b"l.sz", self.h_vec.len() as u64); t.append_u64(b"n.sz", self.g_vec.len() as u64); @@ -116,7 +116,7 @@ impl WeightNormLinearArgument { n: proof.n, }; - return wnla.verify(&com_, t, proof_); + wnla.verify(&com_, t, proof_) } /// Creates weight norm linear argument proof. `commitment` argument should be a weight norm