Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
sauliusgrigaitis committed Dec 25, 2023
2 parents 279f3df + ee0803b commit 9e4babc
Show file tree
Hide file tree
Showing 49 changed files with 1,839 additions and 575 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions arkworks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ rand = [
"dep:rand",
"kzg/rand",
]
bgmw = [
"kzg/bgmw"
]
arkmsm = [
"kzg/arkmsm"
]

[[bench]]
name = "fft"
Expand Down
4 changes: 2 additions & 2 deletions arkworks/benches/eip_4844.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ use kzg::eip_4844::{
use kzg_bench::benches::eip_4844::bench_eip_4844;
use rust_kzg_arkworks::eip_4844::load_trusted_setup_filename_rust;
use rust_kzg_arkworks::kzg_proofs::{FFTSettings, KZGSettings};
use rust_kzg_arkworks::kzg_types::{ArkFr, ArkG1, ArkG2};
use rust_kzg_arkworks::kzg_types::{ArkFp, ArkFr, ArkG1, ArkG1Affine, ArkG2};
use rust_kzg_arkworks::utils::PolyData;

fn bench_eip_4844_(c: &mut Criterion) {
bench_eip_4844::<ArkFr, ArkG1, ArkG2, PolyData, FFTSettings, KZGSettings>(
bench_eip_4844::<ArkFr, ArkG1, ArkG2, PolyData, FFTSettings, KZGSettings, ArkFp, ArkG1Affine>(
c,
&load_trusted_setup_filename_rust,
&blob_to_kzg_commitment_rust,
Expand Down
19 changes: 14 additions & 5 deletions arkworks/benches/fk_20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use criterion::{criterion_group, criterion_main, Criterion};
use kzg_bench::benches::fk20::{bench_fk_multi_da, bench_fk_single_da};
use rust_kzg_arkworks::fk20_proofs::{KzgFK20MultiSettings, KzgFK20SingleSettings};
use rust_kzg_arkworks::kzg_proofs::{generate_trusted_setup, FFTSettings, KZGSettings};
use rust_kzg_arkworks::kzg_types::{ArkFr, ArkG1, ArkG2};
use rust_kzg_arkworks::kzg_types::{ArkFp, ArkFr, ArkG1, ArkG1Affine, ArkG2};
use rust_kzg_arkworks::utils::PolyData;

fn bench_fk_single_da_(c: &mut Criterion) {
Expand All @@ -14,14 +14,23 @@ fn bench_fk_single_da_(c: &mut Criterion) {
FFTSettings,
KZGSettings,
KzgFK20SingleSettings,
ArkFp,
ArkG1Affine,
>(c, &generate_trusted_setup)
}

fn bench_fk_multi_da_(c: &mut Criterion) {
bench_fk_multi_da::<ArkFr, ArkG1, ArkG2, PolyData, FFTSettings, KZGSettings, KzgFK20MultiSettings>(
c,
&generate_trusted_setup,
)
bench_fk_multi_da::<
ArkFr,
ArkG1,
ArkG2,
PolyData,
FFTSettings,
KZGSettings,
KzgFK20MultiSettings,
ArkFp,
ArkG1Affine,
>(c, &generate_trusted_setup)
}

criterion_group! {
Expand Down
30 changes: 21 additions & 9 deletions arkworks/benches/kzg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,33 @@ use criterion::{criterion_group, criterion_main, Criterion};
use kzg_bench::benches::kzg::{bench_commit_to_poly, bench_compute_proof_single};

use rust_kzg_arkworks::kzg_proofs::{generate_trusted_setup, FFTSettings, KZGSettings};
use rust_kzg_arkworks::kzg_types::{ArkFr, ArkG1, ArkG2};
use rust_kzg_arkworks::kzg_types::{ArkFp, ArkFr, ArkG1, ArkG1Affine, ArkG2};
use rust_kzg_arkworks::utils::PolyData;

fn bench_commit_to_poly_(c: &mut Criterion) {
bench_commit_to_poly::<ArkFr, ArkG1, ArkG2, PolyData, FFTSettings, KZGSettings>(
c,
&generate_trusted_setup,
);
bench_commit_to_poly::<
ArkFr,
ArkG1,
ArkG2,
PolyData,
FFTSettings,
KZGSettings,
ArkFp,
ArkG1Affine,
>(c, &generate_trusted_setup);
}

fn bench_compute_proof_single_(c: &mut Criterion) {
bench_compute_proof_single::<ArkFr, ArkG1, ArkG2, PolyData, FFTSettings, KZGSettings>(
c,
&generate_trusted_setup,
);
bench_compute_proof_single::<
ArkFr,
ArkG1,
ArkG2,
PolyData,
FFTSettings,
KZGSettings,
ArkFp,
ArkG1Affine,
>(c, &generate_trusted_setup);
}

criterion_group! {
Expand Down
4 changes: 2 additions & 2 deletions arkworks/benches/lincomb.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use criterion::{criterion_group, criterion_main, Criterion};
use kzg_bench::benches::lincomb::bench_g1_lincomb;
use rust_kzg_arkworks::fft_g1::g1_linear_combination;
use rust_kzg_arkworks::kzg_types::{ArkFr, ArkG1};
use rust_kzg_arkworks::kzg_types::{ArkFp, ArkFr, ArkG1, ArkG1Affine};

fn bench_g1_lincomb_(c: &mut Criterion) {
bench_g1_lincomb::<ArkFr, ArkG1>(c, &g1_linear_combination);
bench_g1_lincomb::<ArkFr, ArkG1, ArkFp, ArkG1Affine>(c, &g1_linear_combination);
}

criterion_group! {
Expand Down
2 changes: 2 additions & 0 deletions arkworks/src/eip_4844.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ fn kzg_settings_to_rust(c_settings: &CKZGSettings) -> Result<KZGSettings, String
fs: fft_settings_to_rust(c_settings)?,
secret_g1,
secret_g2,
// TODO:
precomputation: None,
})
}

Expand Down
16 changes: 14 additions & 2 deletions arkworks/src/fft_g1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,24 @@ use crate::kzg_types::ArkG1ProjAddAffine;

use kzg::msm::msm_impls::msm;

use kzg::msm::precompute::PrecomputationTable;
use kzg::{Fr as KzgFr, G1Mul};
use kzg::{FFTG1, G1};
use std::ops::MulAssign;

pub fn g1_linear_combination(out: &mut ArkG1, points: &[ArkG1], scalars: &[ArkFr], len: usize) {
*out = msm::<ArkG1, ArkFp, ArkG1Affine, ArkG1ProjAddAffine, ArkFr>(points, scalars, len);
pub fn g1_linear_combination(
out: &mut ArkG1,
points: &[ArkG1],
scalars: &[ArkFr],
len: usize,
precomputation: Option<&PrecomputationTable<ArkFr, ArkG1, ArkFp, ArkG1Affine>>,
) {
*out = msm::<ArkG1, ArkFp, ArkG1Affine, ArkG1ProjAddAffine, ArkFr>(
points,
scalars,
len,
precomputation,
);
}

pub fn make_data(data: usize) -> Vec<ArkG1> {
Expand Down
7 changes: 4 additions & 3 deletions arkworks/src/fk20_proofs.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::consts::G1_IDENTITY;
use crate::kzg_proofs::{FFTSettings, KZGSettings};
use crate::kzg_types::{ArkFr as BlstFr, ArkG1, ArkG2};
use crate::kzg_types::{ArkFp, ArkFr as BlstFr, ArkG1, ArkG1Affine, ArkG2};
use crate::utils::PolyData;
use kzg::common_utils::reverse_bit_order;
use kzg::{FFTFr, FK20MultiSettings, FK20SingleSettings, Fr, G1Mul, Poly, FFTG1, G1};
Expand All @@ -25,7 +25,8 @@ pub struct KzgFK20MultiSettings {
pub length: usize,
}

impl FK20SingleSettings<BlstFr, ArkG1, ArkG2, FFTSettings, PolyData, KZGSettings>
impl
FK20SingleSettings<BlstFr, ArkG1, ArkG2, FFTSettings, PolyData, KZGSettings, ArkFp, ArkG1Affine>
for KzgFK20SingleSettings
{
fn new(ks: &KZGSettings, n2: usize) -> Result<Self, String> {
Expand Down Expand Up @@ -84,7 +85,7 @@ impl FK20SingleSettings<BlstFr, ArkG1, ArkG2, FFTSettings, PolyData, KZGSettings
}
}

impl FK20MultiSettings<BlstFr, ArkG1, ArkG2, FFTSettings, PolyData, KZGSettings>
impl FK20MultiSettings<BlstFr, ArkG1, ArkG2, FFTSettings, PolyData, KZGSettings, ArkFp, ArkG1Affine>
for KzgFK20MultiSettings
{
fn new(ks: &KZGSettings, n2: usize, chunk_len: usize) -> Result<Self, String> {
Expand Down
4 changes: 3 additions & 1 deletion arkworks/src/kzg_proofs.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#![allow(non_camel_case_types)]
use super::utils::{blst_poly_into_pc_poly, PolyData};
use crate::consts::{G1_GENERATOR, G2_GENERATOR};
use crate::kzg_types::ArkFr;
use crate::kzg_types::{ArkFp, ArkFr, ArkG1Affine};
use crate::kzg_types::{ArkFr as BlstFr, ArkG1, ArkG2};
use ark_bls12_381::Bls12_381;
use ark_ec::pairing::Pairing;
use ark_ec::CurveGroup;
use ark_poly::Polynomial;
use ark_std::{vec, One};
use kzg::eip_4844::hash_to_bls_field;
use kzg::msm::precompute::PrecomputationTable;
use kzg::Fr as FrTrait;
use kzg::{G1Mul, G2Mul};
use std::ops::Neg;
Expand Down Expand Up @@ -45,6 +46,7 @@ pub struct KZGSettings {
pub fs: FFTSettings,
pub secret_g1: Vec<ArkG1>,
pub secret_g2: Vec<ArkG2>,
pub precomputation: Option<PrecomputationTable<ArkFr, ArkG1, ArkFp, ArkG1Affine>>,
}

pub fn generate_trusted_setup(len: usize, secret: [u8; 32usize]) -> (Vec<ArkG1>, Vec<ArkG2>) {
Expand Down
29 changes: 24 additions & 5 deletions arkworks/src/kzg_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ use ark_std::UniformRand;
use blst::{blst_fp, blst_fr, blst_p1};
use kzg::common_utils::reverse_bit_order;
use kzg::eip_4844::{BYTES_PER_FIELD_ELEMENT, BYTES_PER_G1, BYTES_PER_G2};
use kzg::msm::precompute::{precompute, PrecomputationTable};
use kzg::{
FFTFr, FFTSettings, FFTSettingsPoly, Fr as KzgFr, G1Affine as G1AffineTrait, G1Fp, G1GetFp,
G1Mul, G1ProjAddAffine, G2Mul, KZGSettings, PairingVerify, Poly, Scalar256, G1, G2,
G1LinComb, G1Mul, G1ProjAddAffine, G2Mul, KZGSettings, PairingVerify, Poly, Scalar256, G1, G2,
};
use std::ops::{AddAssign, Mul, Neg, Sub};

Expand Down Expand Up @@ -385,10 +386,17 @@ impl G1Mul<ArkFr> for ArkG1 {
fn mul(&self, b: &ArkFr) -> Self {
Self(self.0.mul(b.fr))
}
}

fn g1_lincomb(points: &[Self], scalars: &[ArkFr], len: usize) -> Self {
impl G1LinComb<ArkFr, ArkFp, ArkG1Affine> for ArkG1 {
fn g1_lincomb(
points: &[Self],
scalars: &[ArkFr],
len: usize,
precomputation: Option<&PrecomputationTable<ArkFr, Self, ArkFp, ArkG1Affine>>,
) -> Self {
let mut out = Self::default();
g1_linear_combination(&mut out, points, scalars, len);
g1_linear_combination(&mut out, points, scalars, len, precomputation);
out
}
}
Expand Down Expand Up @@ -611,7 +619,7 @@ impl FFTSettings<ArkFr> for LFFTSettings {
}
}

impl KZGSettings<ArkFr, ArkG1, ArkG2, LFFTSettings, PolyData> for LKZGSettings {
impl KZGSettings<ArkFr, ArkG1, ArkG2, LFFTSettings, PolyData, ArkFp, ArkG1Affine> for LKZGSettings {
fn new(
secret_g1: &[ArkG1],
secret_g2: &[ArkG2],
Expand All @@ -622,6 +630,7 @@ impl KZGSettings<ArkFr, ArkG1, ArkG2, LFFTSettings, PolyData> for LKZGSettings {
secret_g1: secret_g1.to_vec(),
secret_g2: secret_g2.to_vec(),
fs: fft_settings.clone(),
precomputation: precompute(secret_g1).ok().flatten(),
})
}

Expand All @@ -631,7 +640,13 @@ impl KZGSettings<ArkFr, ArkG1, ArkG2, LFFTSettings, PolyData> for LKZGSettings {
}

let mut out = ArkG1::default();
g1_linear_combination(&mut out, &self.secret_g1, &p.coeffs, p.coeffs.len());
g1_linear_combination(
&mut out,
&self.secret_g1,
&p.coeffs,
p.coeffs.len(),
self.get_precomputation(),
);

Ok(out)
}
Expand Down Expand Up @@ -776,6 +791,10 @@ impl KZGSettings<ArkFr, ArkG1, ArkG2, LFFTSettings, PolyData> for LKZGSettings {
fn get_g2_secret(&self) -> &[ArkG2] {
&self.secret_g2
}

fn get_precomputation(&self) -> Option<&PrecomputationTable<ArkFr, ArkG1, ArkFp, ArkG1Affine>> {
self.precomputation.as_ref()
}
}

type ArkFpInt = <ark_bls12_381::g1::Config as CurveConfig>::BaseField;
Expand Down
6 changes: 3 additions & 3 deletions arkworks/tests/bls12_381.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod tests {
use kzg_bench::tests::bls12_381::*;
use rust_kzg_arkworks::fft_g1::g1_linear_combination;
use rust_kzg_arkworks::kzg_proofs::pairings_verify;
use rust_kzg_arkworks::kzg_types::{ArkFr, ArkG1, ArkG2};
use rust_kzg_arkworks::kzg_types::{ArkFp, ArkFr, ArkG1, ArkG1Affine, ArkG2};

#[test]
pub fn log_2_byte_works_() {
Expand Down Expand Up @@ -94,12 +94,12 @@ mod tests {

#[test]
pub fn g1_make_linear_combination_() {
g1_make_linear_combination::<ArkFr, ArkG1>(&g1_linear_combination);
g1_make_linear_combination::<ArkFr, ArkG1, ArkFp, ArkG1Affine>(&g1_linear_combination);
}

#[test]
pub fn g1_random_linear_combination_() {
g1_random_linear_combination::<ArkFr, ArkG1>(&g1_linear_combination);
g1_random_linear_combination::<ArkFr, ArkG1, ArkFp, ArkG1Affine>(&g1_linear_combination);
}

#[test]
Expand Down
Loading

0 comments on commit 9e4babc

Please sign in to comment.