Skip to content

Commit

Permalink
Remove blst dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
ArtiomTr authored and sauliusgrigaitis committed Jan 20, 2025
1 parent cb71d62 commit 720643a
Show file tree
Hide file tree
Showing 22 changed files with 275 additions and 188 deletions.
3 changes: 0 additions & 3 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion arkworks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ version = "0.1.0"
edition = "2021"

[dependencies]
blst = "0.3.11"
kzg = { path = "../kzg", default-features = false }
ark-std = { version = "^0.4.0", default-features = false }
ark-ff = { version = "^0.4.2", default-features = false, features = [ "asm" ] }
Expand Down
8 changes: 4 additions & 4 deletions arkworks/src/consts.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use kzg::eth::c_bindings::{blst_fp, blst_fp2, blst_p1, blst_p2};

use crate::kzg_types::{ArkG1, ArkG2};
use crate::P2;
use blst::{blst_fp, blst_fp2, blst_p1};

/** The G1 identity/infinity */
#[rustfmt::skip]
Expand Down Expand Up @@ -117,7 +117,7 @@ pub const G1_NEGATIVE_GENERATOR: ArkG1 = ArkG1::from_blst_p1(blst_p1 {
},
});

pub const G2_GENERATOR: ArkG2 = ArkG2::from_blst_p2(P2 {
pub const G2_GENERATOR: ArkG2 = ArkG2::from_blst_p2(blst_p2 {
x: blst_fp2 {
fp: [
blst_fp {
Expand Down Expand Up @@ -192,7 +192,7 @@ pub const G2_GENERATOR: ArkG2 = ArkG2::from_blst_p2(P2 {
},
});

pub const G2_NEGATIVE_GENERATOR: ArkG2 = ArkG2::from_blst_p2(P2 {
pub const G2_NEGATIVE_GENERATOR: ArkG2 = ArkG2::from_blst_p2(blst_p2 {
x: blst_fp2 {
fp: [
blst_fp {
Expand Down
7 changes: 3 additions & 4 deletions arkworks/src/kzg_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@ use ark_std::{One, Zero};

#[cfg(feature = "rand")]
use ark_std::UniformRand;
use kzg::eth::c_bindings::CKZGSettings;
use kzg::eth::c_bindings::{blst_fp, blst_fr, blst_p1, blst_p2, CKZGSettings};

use crate::fft_g1::fft_g1_fast;

use blst::{blst_fp, blst_fr, blst_p1};
use kzg::common_utils::reverse_bit_order;
use kzg::msm::precompute::{precompute, PrecomputationTable};
use kzg::{
Expand Down Expand Up @@ -422,11 +421,11 @@ impl PairingVerify<ArkG1, ArkG2> for ArkG1 {
pub struct ArkG2(pub Projective<g2::Config>);

impl ArkG2 {
pub const fn from_blst_p2(p2: blst::blst_p2) -> Self {
pub const fn from_blst_p2(p2: blst_p2) -> Self {
Self(blst_p2_into_pc_g2projective(&p2))
}

pub const fn to_blst_p2(&self) -> blst::blst_p2 {
pub const fn to_blst_p2(&self) -> blst_p2 {
pc_g2projective_into_blst_p2(self.0)
}
}
Expand Down
12 changes: 0 additions & 12 deletions arkworks/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
pub type Pairing = blst::Pairing;
pub type Fp = blst::blst_fp;
pub type Fp12 = blst::blst_fp12;
pub type Fp6 = blst::blst_fp6;
pub type Fr = blst::blst_fr;
pub type P1 = blst::blst_p1;
pub type P1Affine = blst::blst_p1_affine;
pub type P2 = blst::blst_p2;
pub type P2Affine = blst::blst_p2_affine;
pub type Scalar = blst::blst_scalar;
pub type Uniq = blst::blst_uniq;

pub mod consts;
pub mod das;
pub mod eip_4844;
Expand Down
38 changes: 13 additions & 25 deletions arkworks/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
use super::{Fp, P1};
use crate::kzg_proofs::FFTSettings;
use crate::kzg_types::{ArkFp, ArkFr, ArkG1, ArkG1Affine};
use crate::P2;
use ark_bls12_381::{g1, g2, Fq, Fq2, Fr as Bls12Fr};
use ark_ec::models::short_weierstrass::Projective;
use ark_ff::Fp2;
use ark_poly::univariate::DensePolynomial as DensePoly;
use ark_poly::DenseUVPolynomial;
use blst::{blst_fp, blst_fp2, blst_fr, blst_p1, blst_p2};

use kzg::eip_4844::PrecomputationTableManager;
use kzg::eth::c_bindings::CKZGSettings;
use kzg::eth::c_bindings::{blst_fp, blst_fp2, blst_fr, blst_p1, blst_p2, CKZGSettings};

use kzg::eth;

Expand Down Expand Up @@ -39,8 +36,8 @@ pub fn blst_poly_into_pc_poly(pd: &[ArkFr]) -> DensePoly<Bls12Fr> {
DensePoly::from_coefficients_vec(poly)
}

pub const fn pc_fq_into_blst_fp(fq: Fq) -> Fp {
Fp { l: fq.0 .0 }
pub const fn pc_fq_into_blst_fp(fq: Fq) -> blst_fp {
blst_fp { l: fq.0 .0 }
}

pub const fn blst_fr_into_pc_fr(fr: blst_fr) -> Bls12Fr {
Expand All @@ -51,10 +48,10 @@ pub const fn blst_fr_into_pc_fr(fr: blst_fr) -> Bls12Fr {
}

pub const fn pc_fr_into_blst_fr(fr: Bls12Fr) -> blst_fr {
blst::blst_fr { l: fr.0 .0 }
blst_fr { l: fr.0 .0 }
}

pub const fn blst_fp_into_pc_fq(fp: &Fp) -> Fq {
pub const fn blst_fp_into_pc_fq(fp: &blst_fp) -> Fq {
Fq {
0: ark_ff::BigInt(fp.l),
1: core::marker::PhantomData,
Expand All @@ -68,7 +65,7 @@ pub const fn blst_fp2_into_pc_fq2(fp: &blst_fp2) -> Fq2 {
}
}

pub const fn blst_p1_into_pc_g1projective(p1: &P1) -> Projective<g1::Config> {
pub const fn blst_p1_into_pc_g1projective(p1: &blst_p1) -> Projective<g1::Config> {
Projective {
x: blst_fp_into_pc_fq(&p1.x),
y: blst_fp_into_pc_fq(&p1.y),
Expand All @@ -84,7 +81,7 @@ pub const fn pc_g1projective_into_blst_p1(p1: Projective<g1::Config>) -> blst_p1
}
}

pub const fn blst_p2_into_pc_g2projective(p2: &P2) -> Projective<g2::Config> {
pub const fn blst_p2_into_pc_g2projective(p2: &blst_p2) -> Projective<g2::Config> {
Projective {
x: blst_fp2_into_pc_fq2(&p2.x),
y: blst_fp2_into_pc_fq2(&p2.y),
Expand All @@ -94,23 +91,14 @@ pub const fn blst_p2_into_pc_g2projective(p2: &P2) -> Projective<g2::Config> {

pub const fn pc_g2projective_into_blst_p2(p2: Projective<g2::Config>) -> blst_p2 {
blst_p2 {
x: blst::blst_fp2 {
fp: [
blst::blst_fp { l: p2.x.c0.0 .0 },
blst::blst_fp { l: p2.x.c1.0 .0 },
],
x: blst_fp2 {
fp: [blst_fp { l: p2.x.c0.0 .0 }, blst_fp { l: p2.x.c1.0 .0 }],
},
y: blst::blst_fp2 {
fp: [
blst::blst_fp { l: p2.y.c0.0 .0 },
blst::blst_fp { l: p2.y.c1.0 .0 },
],
y: blst_fp2 {
fp: [blst_fp { l: p2.y.c0.0 .0 }, blst_fp { l: p2.y.c1.0 .0 }],
},
z: blst::blst_fp2 {
fp: [
blst::blst_fp { l: p2.z.c0.0 .0 },
blst::blst_fp { l: p2.z.c1.0 .0 },
],
z: blst_fp2 {
fp: [blst_fp { l: p2.z.c0.0 .0 }, blst_fp { l: p2.z.c1.0 .0 }],
},
}
}
Expand Down
99 changes: 67 additions & 32 deletions arkworks3/src/kzg_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use crate::utils::{
blst_p2_into_pc_g2projective, pc_fr_into_blst_fr, pc_g1projective_into_blst_p1,
pc_g2projective_into_blst_p2, PolyData,
};
use crate::P2;
use ark_bls12_381::{g1, g2, Fr, G1Affine};
use ark_ec::ModelParameters;
use ark_ec::{models::short_weierstrass_jacobian::GroupProjective, AffineCurve, ProjectiveCurve};
Expand All @@ -22,13 +21,9 @@ use ark_std::{One, Zero};
#[cfg(feature = "rand")]
use ark_std::UniformRand;

use blst::{
blst_fp, blst_fp2, blst_fr, blst_p1, blst_p1_affine, blst_p1_compress, blst_p1_from_affine,
blst_p1_in_g1, blst_p1_uncompress, blst_p2, blst_p2_affine, blst_p2_from_affine,
blst_p2_uncompress, BLST_ERROR,
};
use kzg::common_utils::reverse_bit_order;
use kzg::eip_4844::{BYTES_PER_FIELD_ELEMENT, BYTES_PER_G1, BYTES_PER_G2};
use kzg::eth::c_bindings::{blst_fp, blst_fp2, blst_fr, blst_p1, blst_p2};
use kzg::msm::precompute::{precompute, PrecomputationTable};
use kzg::{
FFTFr, FFTSettings, FFTSettingsPoly, Fr as KzgFr, G1Affine as G1AffineTrait, G1Fp, G1GetFp,
Expand Down Expand Up @@ -83,14 +78,10 @@ impl KzgFr for ArkFr {
}

fn zero() -> Self {
// Self::from_u64(0)
Self { fr: Fr::zero() }
}

fn one() -> Self {
// let one = Fr::one();
// // assert_eq!(one.0.0, [0, 1, 1, 1], "must be eq");
// Self { fr: one }
Self::from_u64(1)
}

Expand Down Expand Up @@ -363,17 +354,21 @@ impl G1 for ArkG1 {
)
})
.and_then(|bytes: &[u8; BYTES_PER_G1]| {
let mut blst_affine = blst_p1_affine::default();
let result = unsafe { blst_p1_uncompress(&mut blst_affine, bytes.as_ptr()) };
let mut blst_affine = blst::blst_p1_affine::default();
let result = unsafe { blst::blst_p1_uncompress(&mut blst_affine, bytes.as_ptr()) };

if result != BLST_ERROR::BLST_SUCCESS {
if result != blst::BLST_ERROR::BLST_SUCCESS {
return Err("Failed to deserialize G1".to_owned());
}

let mut blst_point = blst_p1::default();
unsafe { blst_p1_from_affine(&mut blst_point, &blst_affine) };
let mut blst_point = blst::blst_p1::default();
unsafe { blst::blst_p1_from_affine(&mut blst_point, &blst_affine) };

Ok(ArkG1::from_blst_p1(blst_point))
Ok(ArkG1::from_blst_p1(blst_p1 {
x: blst_fp { l: blst_point.x.l },
y: blst_fp { l: blst_point.y.l },
z: blst_fp { l: blst_point.z.l },
}))
})
}

Expand All @@ -382,10 +377,18 @@ impl G1 for ArkG1 {
Self::from_bytes(&bytes)
}

fn to_bytes(&self) -> [u8; 48] {
fn to_bytes(&self) -> [u8; BYTES_PER_G1] {
let mut out = [0u8; BYTES_PER_G1];
let v = self.to_blst_p1();
unsafe {
blst_p1_compress(out.as_mut_ptr(), &self.to_blst_p1());
blst::blst_p1_compress(
out.as_mut_ptr(),
&blst::blst_p1 {
x: blst::blst_fp { l: v.x.l },
y: blst::blst_fp { l: v.y.l },
z: blst::blst_fp { l: v.z.l },
},
);
}
out
}
Expand All @@ -395,12 +398,13 @@ impl G1 for ArkG1 {
}

fn is_inf(&self) -> bool {
let temp = &self.0;
temp.z.is_zero()
self.0.is_zero()
}

fn is_valid(&self) -> bool {
unsafe { blst_p1_in_g1(&self.to_blst_p1()) }
let affine = self.0.into_affine();

affine.is_on_curve() && affine.is_in_correct_subgroup_assuming_on_curve()
}

fn dbl(&self) -> Self {
Expand Down Expand Up @@ -490,18 +494,18 @@ impl PairingVerify<ArkG1, ArkG2> for ArkG1 {
pub struct ArkG2(pub GroupProjective<g2::Parameters>);

impl ArkG2 {
pub fn from_blst_p2(p2: blst::blst_p2) -> Self {
pub fn from_blst_p2(p2: blst_p2) -> Self {
Self(blst_p2_into_pc_g2projective(&p2))
}

pub fn to_blst_p2(&self) -> blst::blst_p2 {
pub fn to_blst_p2(&self) -> blst_p2 {
pc_g2projective_into_blst_p2(self.0)
}
}

impl G2 for ArkG2 {
fn generator() -> Self {
ArkG2::from_blst_p2(P2 {
ArkG2::from_blst_p2(blst_p2 {
x: blst_fp2 {
fp: [
blst_fp {
Expand Down Expand Up @@ -578,7 +582,7 @@ impl G2 for ArkG2 {
}

fn negative_generator() -> Self {
ArkG2::from_blst_p2(P2 {
ArkG2::from_blst_p2(blst_p2 {
x: blst_fp2 {
fp: [
blst_fp {
Expand Down Expand Up @@ -666,17 +670,48 @@ impl G2 for ArkG2 {
)
})
.and_then(|bytes: &[u8; BYTES_PER_G2]| {
let mut blst_affine = blst_p2_affine::default();
let result = unsafe { blst_p2_uncompress(&mut blst_affine, bytes.as_ptr()) };
let mut blst_affine = blst::blst_p2_affine::default();
let result = unsafe { blst::blst_p2_uncompress(&mut blst_affine, bytes.as_ptr()) };

if result != BLST_ERROR::BLST_SUCCESS {
if result != blst::BLST_ERROR::BLST_SUCCESS {
return Err("Failed to deserialize G1".to_owned());
}

let mut blst_point = blst_p2::default();
unsafe { blst_p2_from_affine(&mut blst_point, &blst_affine) };

Ok(ArkG2::from_blst_p2(blst_point))
let mut blst_point = blst::blst_p2::default();
unsafe { blst::blst_p2_from_affine(&mut blst_point, &blst_affine) };

Ok(ArkG2::from_blst_p2(blst_p2 {
x: blst_fp2 {
fp: [
blst_fp {
l: blst_point.x.fp[0].l,
},
blst_fp {
l: blst_point.x.fp[1].l,
},
],
},
y: blst_fp2 {
fp: [
blst_fp {
l: blst_point.y.fp[0].l,
},
blst_fp {
l: blst_point.y.fp[1].l,
},
],
},
z: blst_fp2 {
fp: [
blst_fp {
l: blst_point.z.fp[0].l,
},
blst_fp {
l: blst_point.z.fp[1].l,
},
],
},
}))
})
}

Expand Down
12 changes: 0 additions & 12 deletions arkworks3/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
pub type Pairing = blst::Pairing;
pub type Fp = blst::blst_fp;
pub type Fp12 = blst::blst_fp12;
pub type Fp6 = blst::blst_fp6;
pub type Fr = blst::blst_fr;
pub type P1 = blst::blst_p1;
pub type P1Affine = blst::blst_p1_affine;
pub type P2 = blst::blst_p2;
pub type P2Affine = blst::blst_p2_affine;
pub type Scalar = blst::blst_scalar;
pub type Uniq = blst::blst_uniq;

pub mod consts;
pub mod das;
pub mod eip_4844;
Expand Down
Loading

0 comments on commit 720643a

Please sign in to comment.