Skip to content

Commit

Permalink
Merge pull request #28 from Andenis-Bu/eip-7594
Browse files Browse the repository at this point in the history
Arkworks3 Eip-7594 implementation
  • Loading branch information
ArtiomTr authored Dec 12, 2024
2 parents c588164 + d14d5b3 commit f06fab3
Show file tree
Hide file tree
Showing 34 changed files with 1,753 additions and 951 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions arkworks3/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ hex = "0.4.3"
rand = { version = "0.8.5", optional = true }
libc = { version = "0.2.148", default-features = false }
rayon = { version = "1.8.0", optional = true }
smallvec = { version = "1.11.1", features = ["const_generics"] }
rust-kzg-arkworks3-sppark = { path = "../arkworks3-sppark", version = "0.1.0", optional = true }
rust-kzg-arkworks3-sppark-wlc = { path = "../arkworks3-sppark-wlc", version = "0.1.0", optional = true }

Expand Down
2 changes: 1 addition & 1 deletion arkworks3/benches/das.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use criterion::{criterion_group, criterion_main, Criterion};
use kzg_bench::benches::das::bench_das_extension;
use rust_kzg_arkworks3::kzg_proofs::FFTSettings;
use rust_kzg_arkworks3::kzg_proofs::LFFTSettings as FFTSettings;
use rust_kzg_arkworks3::kzg_types::ArkFr;

fn bench_das_extension_(c: &mut Criterion) {
Expand Down
2 changes: 1 addition & 1 deletion arkworks3/benches/eip_4844.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use kzg::eip_4844::{
};
use kzg_bench::benches::eip_4844::bench_eip_4844;
use rust_kzg_arkworks3::eip_4844::load_trusted_setup_filename_rust;
use rust_kzg_arkworks3::kzg_proofs::{FFTSettings, KZGSettings};
use rust_kzg_arkworks3::kzg_proofs::{LFFTSettings as FFTSettings, LKZGSettings as KZGSettings};
use rust_kzg_arkworks3::kzg_types::{ArkFp, ArkFr, ArkG1, ArkG1Affine, ArkG2};
use rust_kzg_arkworks3::utils::PolyData;

Expand Down
2 changes: 1 addition & 1 deletion arkworks3/benches/fft.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use criterion::{criterion_group, criterion_main, Criterion};
use kzg_bench::benches::fft::{bench_fft_fr, bench_fft_g1};
use rust_kzg_arkworks3::kzg_proofs::FFTSettings;
use rust_kzg_arkworks3::kzg_proofs::LFFTSettings as FFTSettings;
use rust_kzg_arkworks3::kzg_types::{ArkFr, ArkG1};

fn bench_fft_fr_(c: &mut Criterion) {
Expand Down
4 changes: 3 additions & 1 deletion arkworks3/benches/fk_20.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use criterion::{criterion_group, criterion_main, Criterion};
use kzg_bench::benches::fk20::{bench_fk_multi_da, bench_fk_single_da};
use rust_kzg_arkworks3::fk20_proofs::{KzgFK20MultiSettings, KzgFK20SingleSettings};
use rust_kzg_arkworks3::kzg_proofs::{generate_trusted_setup, FFTSettings, KZGSettings};
use rust_kzg_arkworks3::kzg_proofs::{
generate_trusted_setup, LFFTSettings as FFTSettings, LKZGSettings as KZGSettings,
};
use rust_kzg_arkworks3::kzg_types::{ArkFp, ArkFr, ArkG1, ArkG1Affine, ArkG2};
use rust_kzg_arkworks3::utils::PolyData;

Expand Down
4 changes: 3 additions & 1 deletion arkworks3/benches/kzg.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use criterion::{criterion_group, criterion_main, Criterion};
use kzg_bench::benches::kzg::{bench_commit_to_poly, bench_compute_proof_single};

use rust_kzg_arkworks3::kzg_proofs::{generate_trusted_setup, FFTSettings, KZGSettings};
use rust_kzg_arkworks3::kzg_proofs::{
generate_trusted_setup, LFFTSettings as FFTSettings, LKZGSettings as KZGSettings,
};
use rust_kzg_arkworks3::kzg_types::{ArkFp, ArkFr, ArkG1, ArkG1Affine, ArkG2};
use rust_kzg_arkworks3::utils::PolyData;

Expand Down
2 changes: 1 addition & 1 deletion arkworks3/benches/recover.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use criterion::{criterion_group, criterion_main, Criterion};
use kzg_bench::benches::recover::bench_recover;

use rust_kzg_arkworks3::kzg_proofs::FFTSettings;
use rust_kzg_arkworks3::kzg_proofs::LFFTSettings as FFTSettings;
use rust_kzg_arkworks3::kzg_types::ArkFr;
use rust_kzg_arkworks3::utils::PolyData;

Expand Down
2 changes: 1 addition & 1 deletion arkworks3/benches/zero_poly.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use criterion::{criterion_group, criterion_main, Criterion};
use kzg_bench::benches::zero_poly::bench_zero_poly;

use rust_kzg_arkworks3::kzg_proofs::FFTSettings;
use rust_kzg_arkworks3::kzg_proofs::LFFTSettings as FFTSettings;
use rust_kzg_arkworks3::kzg_types::ArkFr;
use rust_kzg_arkworks3::utils::PolyData;

Expand Down
136 changes: 70 additions & 66 deletions arkworks3/src/das.rs
Original file line number Diff line number Diff line change
@@ -1,87 +1,91 @@
use crate::kzg_proofs::FFTSettings;
use crate::kzg_proofs::LFFTSettings;
use crate::kzg_types::ArkFr as BlstFr;
use kzg::{Fr, DAS};
use std::cmp::Ordering;

impl FFTSettings {
fn das_fft_extension_stride(&self, ab: &mut [BlstFr], stride: usize) {
match ab.len().cmp(&2_usize) {
Ordering::Less => {}
Ordering::Greater => {
let half = ab.len();
let halfhalf = half / 2;
impl LFFTSettings {
pub fn das_fft_extension_stride(&self, evens: &mut [BlstFr], stride: usize) {
match evens.len().cmp(&2) {
Ordering::Less => {
return;
}
Ordering::Equal => {
let x = evens[0].add(&evens[1]);
let y = evens[0].sub(&evens[1]);
let y_times_root = y.mul(&self.roots_of_unity[stride]);

for i in 0..halfhalf {
let tmp1 = ab[i].add(&ab[halfhalf + i]);
let tmp2 = ab[i].sub(&ab[halfhalf + i]);
ab[halfhalf + i] = tmp2.mul(&self.reverse_roots_of_unity[i * 2 * stride]);
ab[i] = tmp1;
}
evens[0] = x.add(&y_times_root);
evens[1] = x.sub(&y_times_root);

#[cfg(feature = "parallel")]
{
if ab.len() > 32 {
let (lo, hi) = ab.split_at_mut(halfhalf);
rayon::join(
|| self.das_fft_extension_stride(hi, stride * 2),
|| self.das_fft_extension_stride(lo, stride * 2),
);
} else {
self.das_fft_extension_stride(&mut ab[..halfhalf], stride * 2);
self.das_fft_extension_stride(&mut ab[halfhalf..], stride * 2);
}
}
#[cfg(not(feature = "parallel"))]
{
self.das_fft_extension_stride(&mut ab[..halfhalf], stride * 2);
self.das_fft_extension_stride(&mut ab[halfhalf..], stride * 2);
}
for i in 0..halfhalf {
let x = ab[i];
let y = ab[halfhalf + i];
let y_times_root = y.mul(&self.expanded_roots_of_unity[(1 + 2 * i) * stride]);
ab[i] = x.add(&y_times_root);
ab[halfhalf + i] = x.sub(&y_times_root);
}
return;
}
Ordering::Equal => {
let x = ab[0].add(&ab[1]);
let y = ab[0].sub(&ab[1]);
let tmp = y.mul(&self.expanded_roots_of_unity[stride]);
Ordering::Greater => {}
}

let half: usize = evens.len() / 2;
for i in 0..half {
let tmp1 = evens[i].add(&evens[half + i]);
let tmp2 = evens[i].sub(&evens[half + i]);
evens[half + i] = tmp2.mul(&self.reverse_roots_of_unity[i * 2 * stride]);

ab[0] = x.add(&tmp);
ab[1] = x.sub(&tmp);
evens[i] = tmp1;
}

#[cfg(feature = "parallel")]
{
if evens.len() > 32 {
let (lo, hi) = evens.split_at_mut(half);
rayon::join(
|| self.das_fft_extension_stride(hi, stride * 2),
|| self.das_fft_extension_stride(lo, stride * 2),
);
} else {
// Recurse
self.das_fft_extension_stride(&mut evens[..half], stride * 2);
self.das_fft_extension_stride(&mut evens[half..], stride * 2);
}
}
}
}

impl DAS<BlstFr> for FFTSettings {
fn das_fft_extension(&self, vals: &[BlstFr]) -> Result<Vec<BlstFr>, String> {
if vals.is_empty() {
return Err(String::from("vals can not be empty"));
#[cfg(not(feature = "parallel"))]
{
// Recurse
self.das_fft_extension_stride(&mut evens[..half], stride * 2);
self.das_fft_extension_stride(&mut evens[half..], stride * 2);
}
if !vals.len().is_power_of_two() {
return Err(String::from("vals lenght has to be power of 2"));

for i in 0..half {
let x = evens[i];
let y = evens[half + i];
let y_times_root: BlstFr = y.mul(&self.roots_of_unity[(1 + 2 * i) * stride]);

evens[i] = x.add(&y_times_root);
evens[half + i] = x.sub(&y_times_root);
}
if vals.len() * 2 > self.max_width {
}
}

impl DAS<BlstFr> for LFFTSettings {
fn das_fft_extension(&self, evens: &[BlstFr]) -> Result<Vec<BlstFr>, String> {

Check failure on line 68 in arkworks3/src/das.rs

View workflow job for this annotation

GitHub Actions / backend_ci (ubuntu-latest, arkworks3)

method `das_fft_extension` is not a member of trait `DAS`

Check failure on line 68 in arkworks3/src/das.rs

View workflow job for this annotation

GitHub Actions / benchmarks (ubuntu-latest, arkworks3)

method `das_fft_extension` is not a member of trait `DAS`

Check failure on line 68 in arkworks3/src/das.rs

View workflow job for this annotation

GitHub Actions / benchmarks (windows-latest, arkworks3)

method `das_fft_extension` is not a member of trait `DAS`

Check failure on line 68 in arkworks3/src/das.rs

View workflow job for this annotation

GitHub Actions / backend_ci (windows-latest, arkworks3)

method `das_fft_extension` is not a member of trait `DAS`

Check failure on line 68 in arkworks3/src/das.rs

View workflow job for this annotation

GitHub Actions / backend_ci (macos-latest, arkworks3)

method `das_fft_extension` is not a member of trait `DAS`

Check failure on line 68 in arkworks3/src/das.rs

View workflow job for this annotation

GitHub Actions / benchmarks (macos-latest, arkworks3)

method `das_fft_extension` is not a member of trait `DAS`
if evens.is_empty() {
return Err(String::from("A non-zero list ab expected"));
} else if !evens.len().is_power_of_two() {
return Err(String::from("A list with power-of-two length expected"));
} else if evens.len() * 2 > self.max_width {
return Err(String::from(
"vals lenght * 2 has to equal or less than FFTSetings max width",
"Supplied list is longer than the available max width",
));
}

let mut vals = vals.to_vec();
let stride = self.max_width / (vals.len() * 2);

self.das_fft_extension_stride(&mut vals, stride);
// In case more roots are provided with fft_settings, use a larger stride
let stride = self.max_width / (evens.len() * 2);
let mut odds = evens.to_vec();
self.das_fft_extension_stride(&mut odds, stride);

let invlen = BlstFr::from_u64(vals.len() as u64);
let invlen = invlen.inverse();

for val in &mut vals {
val.fr *= invlen.fr
}
// TODO: explain why each odd member is multiplied by euclidean inverse of length
let mut inv_len = BlstFr::from_u64(odds.len() as u64);
inv_len = inv_len.eucl_inverse();
let odds = odds.iter().map(|f| f.mul(&inv_len)).collect();

Ok(vals)
Ok(odds)
}
}
Loading

0 comments on commit f06fab3

Please sign in to comment.