From b16767efbe247173ab2a1a8656c5a6b6002e437b Mon Sep 17 00:00:00 2001 From: Victor Sint Nicolaas Date: Mon, 27 Nov 2023 15:46:09 +0100 Subject: [PATCH] Remove dead code --- algorithms/src/polycommit/sonic_pc/mod.rs | 63 +++++----- .../src/polycommit/sonic_pc/polynomial.rs | 114 +++--------------- 2 files changed, 42 insertions(+), 135 deletions(-) diff --git a/algorithms/src/polycommit/sonic_pc/mod.rs b/algorithms/src/polycommit/sonic_pc/mod.rs index 1d840b45a9..847483ca69 100644 --- a/algorithms/src/polycommit/sonic_pc/mod.rs +++ b/algorithms/src/polycommit/sonic_pc/mod.rs @@ -209,43 +209,34 @@ impl> SonicKZG10 { hiding_bound, )); - let (comm, rand) = p - .sum()? - .map(move |p| { - let rng_ref = rng.as_mut().map(|s| s as _); - match p { - PolynomialWithBasis::Lagrange { evaluations } => { - let domain = crate::fft::EvaluationDomain::new(evaluations.evaluations.len()).unwrap(); - let lagrange_basis = ck - .lagrange_basis(domain) - .ok_or(PCError::UnsupportedLagrangeBasisSize(domain.size()))?; - assert!(domain.size().is_power_of_two()); - assert!(lagrange_basis.size().is_power_of_two()); - kzg10::KZG10::commit_lagrange( - &lagrange_basis, - &evaluations.evaluations, - hiding_bound, - rng_ref, - ) - } - PolynomialWithBasis::Monomial { polynomial, degree_bound } => { - let powers = if let Some(degree_bound) = degree_bound { - ck.shifted_powers_of_beta_g(degree_bound).unwrap() - } else { - ck.powers() - }; - - kzg10::KZG10::commit(&powers, &polynomial, hiding_bound, rng_ref) - } + let (comm, rand) = { + let rng_ref = rng.as_mut().map(|s| s as _); + match p.polynomial { + PolynomialWithBasis::Lagrange { evaluations } => { + let domain = crate::fft::EvaluationDomain::new(evaluations.evaluations.len()).unwrap(); + let lagrange_basis = ck + .lagrange_basis(domain) + .ok_or(PCError::UnsupportedLagrangeBasisSize(domain.size()))?; + assert!(domain.size().is_power_of_two()); + assert!(lagrange_basis.size().is_power_of_two()); + kzg10::KZG10::commit_lagrange( + &lagrange_basis, + &evaluations.evaluations, + hiding_bound, + rng_ref, + )? } - }) - .try_fold((E::G1Projective::zero(), Randomness::empty()), |mut a, b| { - let b = b?; - a.0.add_assign_mixed(&b.0.0); - a.1 += (E::Fr::one(), &b.1); - Ok::<_, PCError>(a) - })?; - let comm = kzg10::KZGCommitment(comm.to_affine()); + PolynomialWithBasis::Monomial { polynomial, degree_bound } => { + let powers = if let Some(degree_bound) = degree_bound { + ck.shifted_powers_of_beta_g(degree_bound).unwrap() + } else { + ck.powers() + }; + + kzg10::KZG10::commit(&powers, &polynomial, hiding_bound, rng_ref)? + } + } + }; Ok((LabeledCommitment::new(label.to_string(), comm, degree_bound), rand)) }); diff --git a/algorithms/src/polycommit/sonic_pc/polynomial.rs b/algorithms/src/polycommit/sonic_pc/polynomial.rs index c620989bde..a18a0bbace 100644 --- a/algorithms/src/polycommit/sonic_pc/polynomial.rs +++ b/algorithms/src/polycommit/sonic_pc/polynomial.rs @@ -17,8 +17,7 @@ use crate::fft::{DensePolynomial, EvaluationDomain, Evaluations as EvaluationsOn use snarkvm_fields::{Field, PrimeField}; use snarkvm_utilities::{cfg_iter, cfg_iter_mut, CanonicalDeserialize, CanonicalSerialize}; -use anyhow::{ensure, Result}; -use hashbrown::HashMap; +use anyhow::Result; use std::borrow::Cow; #[cfg(feature = "serial")] @@ -142,7 +141,7 @@ impl LabeledPolynomial { #[derive(Debug, Clone)] pub struct LabeledPolynomialWithBasis<'a, F: PrimeField> { pub info: PolynomialInfo, - pub polynomial: Vec<(F, PolynomialWithBasis<'a, F>)>, + pub polynomial: PolynomialWithBasis<'a, F>, } impl<'a, F: PrimeField> LabeledPolynomialWithBasis<'a, F> { @@ -155,16 +154,6 @@ impl<'a, F: PrimeField> LabeledPolynomialWithBasis<'a, F> { ) -> Self { let polynomial = PolynomialWithBasis::new_monomial_basis_ref(polynomial, degree_bound); let info = PolynomialInfo::new(label, degree_bound, hiding_bound); - Self { info, polynomial: vec![(F::one(), polynomial)] } - } - - /// Construct a new labeled polynomial by consuming `polynomial`. - pub fn new_linear_combination( - label: PolynomialLabel, - polynomial: Vec<(F, PolynomialWithBasis<'a, F>)>, - hiding_bound: Option, - ) -> Self { - let info = PolynomialInfo::new(label, None, hiding_bound); Self { info, polynomial } } @@ -175,7 +164,7 @@ impl<'a, F: PrimeField> LabeledPolynomialWithBasis<'a, F> { ) -> Self { let polynomial = PolynomialWithBasis::new_lagrange_basis(polynomial); let info = PolynomialInfo::new(label, None, hiding_bound); - Self { info, polynomial: vec![(F::one(), polynomial)] } + Self { info, polynomial } } pub fn new_lagrange_basis_ref( @@ -185,7 +174,7 @@ impl<'a, F: PrimeField> LabeledPolynomialWithBasis<'a, F> { ) -> Self { let polynomial = PolynomialWithBasis::new_lagrange_basis_ref(polynomial); let info = PolynomialInfo::new(label, None, hiding_bound); - Self { info, polynomial: vec![(F::one(), polynomial)] } + Self { info, polynomial } } /// Return the label for `self`. @@ -199,96 +188,23 @@ impl<'a, F: PrimeField> LabeledPolynomialWithBasis<'a, F> { } pub fn degree(&self) -> usize { - self.polynomial - .iter() - .map(|(_, p)| match p { - PolynomialWithBasis::Lagrange { evaluations } => evaluations.domain().size() - 1, - PolynomialWithBasis::Monomial { polynomial, .. } => polynomial.degree(), - }) - .max() - .unwrap_or(0) + match &self.polynomial { + PolynomialWithBasis::Lagrange { evaluations } => evaluations.domain().size() - 1, + PolynomialWithBasis::Monomial { polynomial, .. } => polynomial.degree(), + } } /// Evaluate the polynomial in `self`. pub fn evaluate(&self, point: F) -> F { - self.polynomial.iter().map(|(coeff, p)| p.evaluate(point) * coeff).sum() - } - - /// Compute a linear combination of the terms in `self.polynomial`, producing an iterator - /// over polynomials of the same time. - pub fn sum(&self) -> Result>> { - if self.polynomial.len() == 1 && self.polynomial[0].0.is_one() { - Ok(vec![self.polynomial[0].1.clone()].into_iter()) - } else { - use PolynomialWithBasis::*; - let mut lagrange_polys = HashMap::>::new(); - let mut dense_polys = HashMap::<_, DensePolynomial>::new(); - let mut sparse_poly = SparsePolynomial::zero(); - // We have sets of polynomials divided along three criteria: - // 1. All `Lagrange` polynomials are in the set corresponding to their domain. - // 2. All `Dense` polynomials are in the set corresponding to their degree bound. - // 3. All `Sparse` polynomials are in the set corresponding to their degree bound. - for (c, poly) in self.polynomial.iter() { - match poly { - Monomial { polynomial, degree_bound } => { - use Polynomial::*; - match polynomial.as_ref() { - Dense(p) => { - if let Some(e) = dense_polys.get_mut(degree_bound) { - // Zip safety: `p` could be of smaller degree than `e`, - // so it's okay to just use `zip` here. - ensure!(e.len() >= p.coeffs.len()); - cfg_iter_mut!(e).zip(&p.coeffs).for_each(|(e, f)| *e += *c * f) - } else { - let mut e: DensePolynomial = p.clone().into_owned(); - cfg_iter_mut!(e).for_each(|e| *e *= c); - dense_polys.insert(degree_bound, e); - } - } - Sparse(p) => sparse_poly += (*c, p.as_ref()), - } - } - Lagrange { evaluations } => { - let domain = evaluations.domain().size(); - if let Some(e) = lagrange_polys.get_mut(&domain) { - ensure!(e.len() == evaluations.evaluations.len()); - cfg_iter_mut!(e).zip_eq(&evaluations.evaluations).for_each(|(e, f)| *e += *c * f) - } else { - let mut e = evaluations.clone().into_owned().evaluations; - cfg_iter_mut!(e).for_each(|e| *e *= c); - lagrange_polys.insert(domain, e); - } - } - } - } - let sparse_poly = Polynomial::from(sparse_poly); - let sparse_poly = Monomial { polynomial: Cow::Owned(sparse_poly), degree_bound: None }; - Ok(lagrange_polys - .into_iter() - .map(|(k, v)| { - let domain = EvaluationDomain::new(k).unwrap(); - Lagrange { evaluations: Cow::Owned(EvaluationsOnDomain::from_vec_and_domain(v, domain)) } - }) - .chain({ - dense_polys - .into_iter() - .map(|(degree_bound, p)| PolynomialWithBasis::new_dense_monomial_basis(p, *degree_bound)) - }) - .chain([sparse_poly]) - .collect::>() - .into_iter()) - } + self.polynomial.evaluate(point) } /// Retrieve the degree bound in `self`. pub fn degree_bound(&self) -> Option { - self.polynomial - .iter() - .filter_map(|(_, p)| match p { - PolynomialWithBasis::Monomial { degree_bound, .. } => *degree_bound, - _ => None, - }) - .max() + match self.polynomial { + PolynomialWithBasis::Monomial { degree_bound, .. } => degree_bound, + _ => None, + } } /// Retrieve whether the polynomial in `self` should be hidden. @@ -308,7 +224,7 @@ impl<'a, F: PrimeField> From<&'a LabeledPolynomial> for LabeledPolynomialWith polynomial: Cow::Borrowed(other.polynomial()), degree_bound: other.degree_bound(), }; - Self { info: other.info.clone(), polynomial: vec![(F::one(), polynomial)] } + Self { info: other.info.clone(), polynomial } } } @@ -318,7 +234,7 @@ impl<'a, F: PrimeField> From> for LabeledPolynomialWithBasi polynomial: Cow::Owned(other.polynomial), degree_bound: other.info.degree_bound, }; - Self { info: other.info.clone(), polynomial: vec![(F::one(), polynomial)] } + Self { info: other.info.clone(), polynomial } } }