Skip to content
Snippets Groups Projects
  • STEVAN Antoine's avatar
    update the API (dragoon/komodo!71) · 6f6647cd
    STEVAN Antoine authored
    ## changelog
    - rename the `encode` function to `prove` and have it take _shards_ instead of an _encoding matrix_: this is to isolate the "encoding" process inside the `fec` module and leave the main `komodo::prove` only compute the "proof", i.e. the commits of the data
    
    from
    ```rust
    fn encode<F, G, P>(
        bytes: &[u8],
        encoding_mat: &Matrix<F>,
        powers: &Powers<F, G>,
    ) -> Result<Vec<Block<F, G>>, KomodoError>
    ```
    to
    ```rust
    fn prove<F, G, P>(
        bytes: &[u8],
        powers: &Powers<F, G>,
        k: usize,
    ) -> Result<Vec<Commitment<F, G>>, KomodoError>
    ```
    - rename `fec::Shard.combine` to `fec::Shard.recode_with` to get rid of "combine"
    - rename `fec::recode` to `fec::recode_with_coeffs` to show that this version takes a list of coefficients
    - rename `Block.commit` to `Block.proof`: "commit" should be "commits" and it's usually refered to as "proof"
    - split `prove` further into `prove` and `build`: `prove` now outputs a `Vec<Commitment<F>>`, `build` simply takes a `Vec<Shard<F>>` and a `Vec<Commitment<F>>` and outputs a `Vec<Block<F>>`
    - add `fec::recode_random` that does the "shard" part of `recode` to wrap around `fec::recode_with_coeffs`
    - remove `R: RngCore` from the signature of `zk::setup`, to avoid having to pass a generic `_` annotation everywhere `zk::setup` is used, same change has been applied to `recode` and the `generate_random_powers` in `main.rs`
    
    from
    ```rust
    fn setup<R: RngCore, F: PrimeField, G: CurveGroup<ScalarField = F>>(
        max_degree: usize,
        rng: &mut R,
    ) -> Result<Powers<F, G>, KomodoError> {
    ```
    to
    ```rust
    fn setup<F: PrimeField, G: CurveGroup<ScalarField = F>>(
        max_degree: usize,
        rng: &mut impl RngCore,
    ) -> Result<Powers<F, G>, KomodoError> {
    ```
    
    ### some extra minor changes
    - remove some useles generic type annotations, e.g. `prove::<F, G, P>` can become a simpler `prove` most of the time, i.e. when there is at least one generic annotation somewhere in the scope
    6f6647cd
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
Forked from Dragoon / Komodo
117 commits behind the upstream repository.
commit.rs 2.67 KiB
use std::time::Duration;

use ark_ec::{pairing::Pairing, CurveGroup};
use ark_ff::PrimeField;
use ark_poly::{univariate::DensePolynomial, DenseUVPolynomial};
use ark_poly_commit::kzg10::{Powers, KZG10};
use ark_std::ops::Div;

use criterion::{black_box, criterion_group, criterion_main, Criterion};

use komodo::zk;

fn commit_template<F, G, P>(c: &mut Criterion, degree: usize, curve: &str)
where
    F: PrimeField,
    G: CurveGroup<ScalarField = F>,
    P: DenseUVPolynomial<F>,
    for<'a, 'b> &'a P: Div<&'b P, Output = P>,
{
    let rng = &mut rand::thread_rng();

    let setup = zk::setup::<F, G>(degree, rng).unwrap();
    let polynomial = P::rand(degree, rng);

    c.bench_function(&format!("commit (komodo) {} on {}", degree, curve), |b| {
        b.iter(|| zk::commit(&setup, &polynomial))
    });
}

fn ark_commit_template<E, P>(c: &mut Criterion, degree: usize, curve: &str)
where
    E: Pairing,
    P: DenseUVPolynomial<E::ScalarField>,
    for<'a, 'b> &'a P: Div<&'b P, Output = P>,
{
    let rng = &mut rand::thread_rng();

    let setup = KZG10::<E, P>::setup(degree, false, rng).unwrap();
    let powers_of_g = setup.powers_of_g[..=degree].to_vec();
    let powers_of_gamma_g = (0..=degree).map(|i| setup.powers_of_gamma_g[&i]).collect();
    let powers = Powers::<E> {
        powers_of_g: ark_std::borrow::Cow::Owned(powers_of_g),
        powers_of_gamma_g: ark_std::borrow::Cow::Owned(powers_of_gamma_g),
    };
    let polynomial = P::rand(degree, rng);

    c.bench_function(&format!("commit (arkworks) {} on {}", degree, curve), |b| {
        b.iter(|| KZG10::commit(&powers, &polynomial, None, None))
    });
}

fn commit(c: &mut Criterion) {
    fn aux<F: PrimeField, G: CurveGroup<ScalarField = F>>(
        c: &mut Criterion,
        degree: usize,
        curve: &str,
    ) {
        commit_template::<F, G, DensePolynomial<F>>(c, black_box(degree), curve);
    }

    for n in [1, 2, 4, 8, 16] {
        aux::<ark_bls12_381::Fr, ark_bls12_381::G1Projective>(c, n, "BLS12-381");
        aux::<ark_bn254::Fr, ark_bn254::G1Projective>(c, n, "BN-254");
        aux::<ark_pallas::Fr, ark_pallas::Projective>(c, n, "PALLAS");
    }
}

fn ark_commit(c: &mut Criterion) {
    fn aux<E: Pairing>(c: &mut Criterion, degree: usize, curve: &str) {
        ark_commit_template::<E, DensePolynomial<E::ScalarField>>(c, black_box(degree), curve);
    }

    for n in [1, 2, 4, 8, 16] {
        aux::<ark_bls12_381::Bls12_381>(c, n, "BLS12-381");
        aux::<ark_bn254::Bn254>(c, n, "BN-254");
    }
}

criterion_group!(
    name = benches;
    config = Criterion::default()
        .warm_up_time(Duration::from_secs_f32(0.5))
        .sample_size(10);
    targets = commit, ark_commit
);
criterion_main!(benches);