Skip to content
Snippets Groups Projects
Commit de3f1e33 authored by STEVAN Antoine's avatar STEVAN Antoine :crab:
Browse files

cleanup (!70)

## changelog
* eb1b1381 don't use a T in the lib `run_template` test
* fbd503c6 remove the useless unwrap and TODO
* b550d712 remove some pub
* 339c3038 remove useless `.iter()`
* 537993f0 remove useless `.add(...)`
* d7720907 remove hiding_bound from timer in commit
* eecab5a6 move `commit` to inlined `zk::batch_commit`
parent 5f582bbc
No related branches found
No related tags found
1 merge request!70cleanup
Pipeline #4750 passed
...@@ -45,7 +45,7 @@ impl<F: PrimeField> Shard<F> { ...@@ -45,7 +45,7 @@ impl<F: PrimeField> Shard<F> {
.data .data
.iter() .iter()
.zip(other.data.iter()) .zip(other.data.iter())
.map(|(es, eo)| es.mul(alpha).add(eo.mul(beta))) .map(|(es, eo)| es.mul(alpha) + eo.mul(beta))
.collect::<Vec<_>>(), .collect::<Vec<_>>(),
size: self.size, size: self.size,
} }
...@@ -69,7 +69,7 @@ pub fn combine<F: PrimeField>(shards: &[Shard<F>], coeffs: &[F]) -> Option<Shard ...@@ -69,7 +69,7 @@ pub fn combine<F: PrimeField>(shards: &[Shard<F>], coeffs: &[F]) -> Option<Shard
let (s, _) = shards let (s, _) = shards
.iter() .iter()
.zip(coeffs.iter()) .zip(coeffs)
.skip(1) .skip(1)
.fold((shards[0].clone(), coeffs[0]), |(acc_s, acc_c), (s, c)| { .fold((shards[0].clone(), coeffs[0]), |(acc_s, acc_c), (s, c)| {
(acc_s.combine(acc_c, s, *c), F::one()) (acc_s.combine(acc_c, s, *c), F::one())
......
...@@ -28,7 +28,7 @@ use crate::{ ...@@ -28,7 +28,7 @@ use crate::{
#[derive(Debug, Default, Clone, PartialEq, CanonicalSerialize, CanonicalDeserialize)] #[derive(Debug, Default, Clone, PartialEq, CanonicalSerialize, CanonicalDeserialize)]
pub struct Block<F: PrimeField, G: CurveGroup<ScalarField = F>> { pub struct Block<F: PrimeField, G: CurveGroup<ScalarField = F>> {
pub shard: fec::Shard<F>, pub shard: fec::Shard<F>,
pub commit: Vec<Commitment<F, G>>, commit: Vec<Commitment<F, G>>,
} }
impl<F: PrimeField, G: CurveGroup<ScalarField = F>> std::fmt::Display for Block<F, G> { impl<F: PrimeField, G: CurveGroup<ScalarField = F>> std::fmt::Display for Block<F, G> {
...@@ -80,34 +80,6 @@ impl<F: PrimeField, G: CurveGroup<ScalarField = F>> std::fmt::Display for Block< ...@@ -80,34 +80,6 @@ impl<F: PrimeField, G: CurveGroup<ScalarField = F>> std::fmt::Display for Block<
} }
} }
/// compute the commitments of a set of polynomials
///
/// this function uses the commit scheme of KZG.
///
/// > **Note**
/// > - `powers` can be generated with functions like [`zk::setup`]
/// > - if `polynomials` has length `n`, then [`commit`] will generate `n`
/// > commits.
#[allow(clippy::type_complexity)]
pub fn commit<F, G, P>(
powers: &Powers<F, G>,
polynomials: &[P],
) -> Result<Vec<Commitment<F, G>>, KomodoError>
where
F: PrimeField,
G: CurveGroup<ScalarField = F>,
P: DenseUVPolynomial<F>,
for<'a, 'b> &'a P: Div<&'b P, Output = P>,
{
let mut commits = Vec::new();
for polynomial in polynomials {
let commit = zk::commit(powers, polynomial)?;
commits.push(commit);
}
Ok(commits)
}
/// compute encoded and proven blocks of data from some data and an encoding /// compute encoded and proven blocks of data from some data and an encoding
/// method /// method
/// ///
...@@ -146,10 +118,9 @@ where ...@@ -146,10 +118,9 @@ where
.collect::<Vec<P>>(); .collect::<Vec<P>>();
debug!("committing the polynomials"); debug!("committing the polynomials");
let commits = commit(powers, &polynomials_to_commit)?; let commits = zk::batch_commit(powers, &polynomials_to_commit)?;
Ok(fec::encode(bytes, encoding_mat) Ok(fec::encode(bytes, encoding_mat)?
.unwrap() // TODO: don't unwrap here
.iter() .iter()
.map(|s| Block { .map(|s| Block {
shard: s.clone(), shard: s.clone(),
...@@ -242,7 +213,7 @@ where ...@@ -242,7 +213,7 @@ where
mod tests { mod tests {
use ark_bls12_381::{Fr, G1Projective}; use ark_bls12_381::{Fr, G1Projective};
use ark_ec::CurveGroup; use ark_ec::CurveGroup;
use ark_ff::{Field, PrimeField}; use ark_ff::PrimeField;
use ark_poly::{univariate::DensePolynomial, DenseUVPolynomial}; use ark_poly::{univariate::DensePolynomial, DenseUVPolynomial};
use ark_std::{ops::Div, test_rng}; use ark_std::{ops::Div, test_rng};
...@@ -412,11 +383,10 @@ mod tests { ...@@ -412,11 +383,10 @@ mod tests {
// NOTE: this is part of an experiment, to be honest, to be able to see how // NOTE: this is part of an experiment, to be honest, to be able to see how
// much these tests could be refactored and simplified // much these tests could be refactored and simplified
fn run_template<F, T, P, Fun>(test: Fun) fn run_template<F, P, Fun>(test: Fun)
where where
F: PrimeField, F: PrimeField,
T: Field, Fun: Fn(&[u8], &Matrix<F>) -> Result<(), KomodoError>,
Fun: Fn(&[u8], &Matrix<T>) -> Result<(), KomodoError>,
P: DenseUVPolynomial<F>, P: DenseUVPolynomial<F>,
for<'a, 'b> &'a P: Div<&'b P, Output = P>, for<'a, 'b> &'a P: Div<&'b P, Output = P>,
{ {
...@@ -435,35 +405,35 @@ mod tests { ...@@ -435,35 +405,35 @@ mod tests {
#[test] #[test]
fn verification() { fn verification() {
run_template::<Fr, _, DensePolynomial<Fr>, _>( run_template::<Fr, DensePolynomial<Fr>, _>(
verify_template::<Fr, G1Projective, DensePolynomial<Fr>>, verify_template::<Fr, G1Projective, DensePolynomial<Fr>>,
); );
} }
#[test] #[test]
fn verify_with_errors() { fn verify_with_errors() {
run_template::<Fr, _, DensePolynomial<Fr>, _>( run_template::<Fr, DensePolynomial<Fr>, _>(
verify_with_errors_template::<Fr, G1Projective, DensePolynomial<Fr>>, verify_with_errors_template::<Fr, G1Projective, DensePolynomial<Fr>>,
); );
} }
#[test] #[test]
fn verify_recoding() { fn verify_recoding() {
run_template::<Fr, _, DensePolynomial<Fr>, _>( run_template::<Fr, DensePolynomial<Fr>, _>(
verify_recoding_template::<Fr, G1Projective, DensePolynomial<Fr>>, verify_recoding_template::<Fr, G1Projective, DensePolynomial<Fr>>,
); );
} }
#[test] #[test]
fn end_to_end() { fn end_to_end() {
run_template::<Fr, _, DensePolynomial<Fr>, _>( run_template::<Fr, DensePolynomial<Fr>, _>(
end_to_end_template::<Fr, G1Projective, DensePolynomial<Fr>>, end_to_end_template::<Fr, G1Projective, DensePolynomial<Fr>>,
); );
} }
#[test] #[test]
fn end_to_end_with_recoding() { fn end_to_end_with_recoding() {
run_template::<Fr, _, DensePolynomial<Fr>, _>( run_template::<Fr, DensePolynomial<Fr>, _>(
end_to_end_with_recoding_template::<Fr, G1Projective, DensePolynomial<Fr>>, end_to_end_with_recoding_template::<Fr, G1Projective, DensePolynomial<Fr>>,
); );
} }
......
...@@ -118,7 +118,7 @@ fn throw_error(code: i32, message: &str) { ...@@ -118,7 +118,7 @@ fn throw_error(code: i32, message: &str) {
exit(code); exit(code);
} }
pub fn generate_random_powers<F, G, P, R>( fn generate_random_powers<F, G, P, R>(
n: usize, n: usize,
powers_dir: &Path, powers_dir: &Path,
powers_filename: Option<&str>, powers_filename: Option<&str>,
...@@ -139,7 +139,7 @@ where ...@@ -139,7 +139,7 @@ where
Ok(()) Ok(())
} }
pub fn verify_blocks<F, G, P>( fn verify_blocks<F, G, P>(
blocks: &[(String, Block<F, G>)], blocks: &[(String, Block<F, G>)],
powers: Powers<F, G>, powers: Powers<F, G>,
) -> Result<(), KomodoError> ) -> Result<(), KomodoError>
......
...@@ -3,7 +3,7 @@ use ark_ec::{scalar_mul::fixed_base::FixedBase, CurveGroup, VariableBaseMSM}; ...@@ -3,7 +3,7 @@ use ark_ec::{scalar_mul::fixed_base::FixedBase, CurveGroup, VariableBaseMSM};
use ark_ff::PrimeField; use ark_ff::PrimeField;
use ark_poly::DenseUVPolynomial; use ark_poly::DenseUVPolynomial;
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize}; use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
use ark_std::{end_timer, rand::RngCore, start_timer}; use ark_std::{end_timer, ops::Div, rand::RngCore, start_timer};
use crate::error::KomodoError; use crate::error::KomodoError;
...@@ -112,11 +112,8 @@ where ...@@ -112,11 +112,8 @@ where
{ {
check_degree_is_too_large(polynomial.degree(), powers.len())?; check_degree_is_too_large(polynomial.degree(), powers.len())?;
let commit_time = start_timer!(|| format!( let commit_time =
"Committing to polynomial of degree {} with hiding_bound: {:?}", start_timer!(|| format!("Committing to polynomial of degree {}", polynomial.degree(),));
polynomial.degree(),
hiding_bound,
));
let (num_leading_zeros, plain_coeffs) = skip_leading_zeros_and_convert_to_bigints(polynomial); let (num_leading_zeros, plain_coeffs) = skip_leading_zeros_and_convert_to_bigints(polynomial);
...@@ -132,6 +129,35 @@ where ...@@ -132,6 +129,35 @@ where
Ok(Commitment(commitment.into())) Ok(Commitment(commitment.into()))
} }
/// compute the commitments of a set of polynomials
///
/// this function uses the commit scheme of KZG.
///
/// > **Note**
/// > - `powers` can be generated with functions like [`zk::setup`]
/// > - if `polynomials` has length `n`, then [`commit`] will generate `n`
/// > commits.
#[allow(clippy::type_complexity)]
#[inline(always)]
pub fn batch_commit<F, G, P>(
powers: &Powers<F, G>,
polynomials: &[P],
) -> Result<Vec<Commitment<F, G>>, KomodoError>
where
F: PrimeField,
G: CurveGroup<ScalarField = F>,
P: DenseUVPolynomial<F>,
for<'a, 'b> &'a P: Div<&'b P, Output = P>,
{
let mut commits = Vec::new();
for polynomial in polynomials {
let commit = commit(powers, polynomial)?;
commits.push(commit);
}
Ok(commits)
}
// compute the number of elements that a _trusted setup_ should have for data of // compute the number of elements that a _trusted setup_ should have for data of
// a certain expected size // a certain expected size
pub fn nb_elements_in_setup<F: PrimeField>(nb_bytes: usize) -> usize { pub fn nb_elements_in_setup<F: PrimeField>(nb_bytes: usize) -> usize {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment