Skip to content

Commit

Permalink
Switch to upstream version of KZG library
Browse files Browse the repository at this point in the history
  • Loading branch information
nazar-pc committed Oct 12, 2023
1 parent a81c999 commit a6d835d
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 67 deletions.
41 changes: 21 additions & 20 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ blake2 = { opt-level = 3 }
blake3 = { opt-level = 3 }
blake2b_simd = { opt-level = 3 }
blst = { opt-level = 3 }
blst_rust = { opt-level = 3 }
rust-kzg-blst = { opt-level = 3 }
chacha20 = { opt-level = 3 }
chacha20poly1305 = { opt-level = 3 }
cranelift-codegen = { opt-level = 3 }
Expand Down
10 changes: 4 additions & 6 deletions crates/subspace-core-primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,14 @@ bench = false

[dependencies]
blake3 = { version = "1.4.1", default-features = false }
# TODO: Switch to upstream `main` once https://github.com/sifraitech/rust-kzg/pull/204 is merged and blst has upstream no_std support
blst_rust = { git = "https://github.com/subspace/rust-kzg", rev = "1058cc8c8af8461b490dc212c41d7d506a746577", default-features = false }
derive_more = "0.99.17"
hex = { version = "0.4.3", default-features = false, features = ["alloc"] }
# TODO: Switch to upstream `main` once https://github.com/sifraitech/rust-kzg/pull/204 is merged and blst has upstream no_std support
kzg = { git = "https://github.com/subspace/rust-kzg", rev = "1058cc8c8af8461b490dc212c41d7d506a746577", default-features = false }
kzg = { git = "https://github.com/sifraitech/rust-kzg", rev = "c34b73916af9b8a699a74bd0186f82f25e72861c", default-features = false }
num-traits = { version = "0.2.16", default-features = false }
parity-scale-codec = { version = "3.6.5", default-features = false, features = ["derive", "max-encoded-len"] }
parking_lot = { version = "0.12.1", optional = true }
rayon = { version = "1.8.0", optional = true }
rust-kzg-blst = { git = "https://github.com/sifraitech/rust-kzg", rev = "c34b73916af9b8a699a74bd0186f82f25e72861c", default-features = false }
scale-info = { version = "2.7.0", default-features = false, features = ["derive"] }
serde = { version = "1.0.183", optional = true, features = ["alloc", "derive"] }
serde_arrays = { version = "0.1.0", optional = true }
Expand Down Expand Up @@ -54,7 +52,7 @@ embedded-kzg-settings = []
# Enables some APIs and internal parallelism for KZG
parallel = [
"blake3/rayon",
"blst_rust/parallel",
"rust-kzg-blst/parallel",
"dep:rayon",
]
serde = [
Expand All @@ -65,7 +63,7 @@ serde = [
]
std = [
"blake3/std",
"blst_rust/std",
"rust-kzg-blst/std",
"hex/std",
"kzg/std",
"num-traits/std",
Expand Down
11 changes: 4 additions & 7 deletions crates/subspace-core-primitives/src/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@ extern crate alloc;
pub mod kzg;

use crate::Blake3Hash;
use ::kzg::Fr;
use alloc::format;
use alloc::string::String;
use alloc::vec::Vec;
use blst_rust::types::fr::FsFr;
use core::cmp::Ordering;
use core::hash::{Hash, Hasher};
use core::mem;
use derive_more::{AsMut, AsRef, Deref, DerefMut, From, Into};
use parity_scale_codec::{Decode, Encode, EncodeLike, Input, MaxEncodedLen};
use rust_kzg_blst::types::fr::FsFr;
use scale_info::{Type, TypeInfo};

/// BLAKE3 hashing of a single value.
Expand Down Expand Up @@ -204,18 +205,14 @@ impl TryFrom<[u8; Self::FULL_BYTES]> for Scalar {

#[inline]
fn try_from(value: [u8; Self::FULL_BYTES]) -> Result<Self, Self::Error> {
FsFr::from_scalar(value)
.map_err(|error_code| {
format!("Failed to create scalar from bytes with code: {error_code}")
})
.map(Scalar)
FsFr::from_bytes(&value).map(Scalar)
}
}

impl From<&Scalar> for [u8; Scalar::FULL_BYTES] {
#[inline]
fn from(value: &Scalar) -> Self {
value.0.to_scalar()
value.0.to_bytes()
}
}

Expand Down
28 changes: 8 additions & 20 deletions crates/subspace-core-primitives/src/crypto/kzg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ use alloc::collections::BTreeMap;
use alloc::string::{String, ToString};
use alloc::sync::Arc;
use alloc::vec::Vec;
use blst_rust::types::fft_settings::FsFFTSettings;
use blst_rust::types::g1::FsG1;
use blst_rust::types::g2::FsG2;
use blst_rust::types::kzg_settings::FsKZGSettings;
use blst_rust::types::poly::FsPoly;
use core::mem;
use derive_more::{AsMut, AsRef, Deref, DerefMut, From, Into};
use kzg::eip_4844::{BYTES_PER_G1, BYTES_PER_G2};
use kzg::{FFTFr, FFTSettings, Fr, KZGSettings};
use kzg::{FFTFr, FFTSettings, Fr, KZGSettings, G1, G2};
#[cfg(feature = "std")]
use parking_lot::Mutex;
use rust_kzg_blst::types::fft_settings::FsFFTSettings;
use rust_kzg_blst::types::g1::FsG1;
use rust_kzg_blst::types::g2::FsG2;
use rust_kzg_blst::types::kzg_settings::FsKZGSettings;
use rust_kzg_blst::types::poly::FsPoly;
#[cfg(not(feature = "std"))]
use spin::Mutex;
use tracing::debug;
Expand Down Expand Up @@ -53,23 +53,11 @@ pub fn bytes_to_kzg_settings(
let (secret_g1_bytes, secret_g2_bytes) = bytes.split_at(BYTES_PER_G1 * num_g1_powers);
let secret_g1 = secret_g1_bytes
.chunks_exact(BYTES_PER_G1)
.map(|bytes| {
FsG1::from_bytes(
bytes
.try_into()
.expect("Chunked into correct number of bytes above; qed"),
)
})
.map(FsG1::from_bytes)
.collect::<Result<Vec<_>, _>>()?;
let secret_g2 = secret_g2_bytes
.chunks_exact(BYTES_PER_G2)
.map(|bytes| {
FsG2::from_bytes(
bytes
.try_into()
.expect("Chunked into correct number of bytes above; qed"),
)
})
.map(FsG2::from_bytes)
.collect::<Result<Vec<_>, _>>()?;

let fft_settings = FsFFTSettings::new(
Expand Down
13 changes: 5 additions & 8 deletions crates/subspace-erasure-coding/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,23 @@ include = [
bench = false

[dependencies]
# TODO: Switch to upstream `main` once https://github.com/sifraitech/rust-kzg/pull/204 is merged and blst has upstream no_std support
blst_rust = { git = "https://github.com/subspace/rust-kzg", rev = "1058cc8c8af8461b490dc212c41d7d506a746577", default-features = false }
# TODO: Switch to upstream `main` once https://github.com/sifraitech/rust-kzg/pull/204 is merged and blst has upstream no_std support
kzg = { git = "https://github.com/subspace/rust-kzg", rev = "1058cc8c8af8461b490dc212c41d7d506a746577", default-features = false }
kzg = { git = "https://github.com/sifraitech/rust-kzg", rev = "c34b73916af9b8a699a74bd0186f82f25e72861c", default-features = false }
rust-kzg-blst = { git = "https://github.com/sifraitech/rust-kzg", rev = "c34b73916af9b8a699a74bd0186f82f25e72861c", default-features = false }
subspace-core-primitives = { version = "0.1.0", path = "../subspace-core-primitives", default-features = false }

[dev-dependencies]
# TODO: Switch to upstream `main` once https://github.com/sifraitech/rust-kzg/pull/204 is merged and blst has upstream no_std support
blst_rust = { git = "https://github.com/subspace/rust-kzg", rev = "1058cc8c8af8461b490dc212c41d7d506a746577" }
rust-kzg-blst = { git = "https://github.com/sifraitech/rust-kzg", rev = "c34b73916af9b8a699a74bd0186f82f25e72861c" }
criterion = "0.5.1"
rand = "0.8.5"

[features]
default = ["std", "parallel"]
std = [
"blst_rust/std",
"kzg/std",
"rust-kzg-blst/std",
"subspace-core-primitives/std",
]
parallel = ["blst_rust/parallel"]
parallel = ["rust-kzg-blst/parallel"]

[[bench]]
name = "commitments"
Expand Down
2 changes: 1 addition & 1 deletion crates/subspace-erasure-coding/benches/commitments.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use blst_rust::types::g1::FsG1;
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use kzg::G1;
use rust_kzg_blst::types::g1::FsG1;
use std::num::NonZeroUsize;
use subspace_core_primitives::crypto::kzg::Commitment;
use subspace_core_primitives::ArchivedHistorySegment;
Expand Down
6 changes: 3 additions & 3 deletions crates/subspace-erasure-coding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ extern crate alloc;
use alloc::string::String;
use alloc::sync::Arc;
use alloc::vec::Vec;
use blst_rust::types::fft_settings::FsFFTSettings;
use blst_rust::types::g1::FsG1;
use blst_rust::types::poly::FsPoly;
use core::num::NonZeroUsize;
use kzg::{FFTSettings, PolyRecover, DAS, FFTG1, G1};
use rust_kzg_blst::types::fft_settings::FsFFTSettings;
use rust_kzg_blst::types::g1::FsG1;
use rust_kzg_blst::types::poly::FsPoly;
use subspace_core_primitives::crypto::kzg::{Commitment, Polynomial};
use subspace_core_primitives::crypto::Scalar;

Expand Down
2 changes: 1 addition & 1 deletion crates/subspace-erasure-coding/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::ErasureCoding;
use blst_rust::types::g1::FsG1;
use kzg::G1;
use rust_kzg_blst::types::g1::FsG1;
use std::iter;
use std::num::NonZeroUsize;
use subspace_core_primitives::crypto::kzg::Commitment;
Expand Down

0 comments on commit a6d835d

Please sign in to comment.