Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: re-export c-kzg types and impl rlp traits #4084

Merged
merged 5 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ jsonrpsee-types = { version = "0.19" }

## crypto
secp256k1 = { version = "0.27.0", default-features = false, features = ["global-context", "rand-std", "recovery"] }
# for eip-4844
c-kzg = { git = "https://github.com/ethereum/c-kzg-4844" }

### misc-testing
proptest = "1.0"
Expand Down
5 changes: 4 additions & 1 deletion crates/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ crunchy = { version = "0.2.2", default-features = false, features = ["limit_256"
ruint = { version = "1.9.0", features = ["primitive-types", "rlp"] }

# Bloom
fixed-hash = { version = "0.8", default-features = false, features = ["rustc-hex"] }
fixed-hash = { version = "0.8", default-features = false, features = ["rustc-hex"] }

# crypto
secp256k1 = { workspace = true, default-features = false, features = [
Expand All @@ -31,6 +31,9 @@ secp256k1 = { workspace = true, default-features = false, features = [
"recovery",
] }

# for eip-4844
c-kzg = { workspace = true }

# used for forkid
crc = "3"

Expand Down
5 changes: 5 additions & 0 deletions crates/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ pub mod utils {
pub use ethers_core::types::serde_helpers;
}

/// EIP-4844 + KZG helpers
pub mod kzg {
pub use c_kzg::*;
}

/// Helpers for working with serde
pub mod serde_helper;

Expand Down
5 changes: 5 additions & 0 deletions crates/rlp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ ethereum-types = { version = "0.14", features = ["codec"], optional = true }
revm-primitives = { workspace = true, features = ["serde"] }
reth-rlp-derive = { path = "./rlp-derive", optional = true }

# for eip-4844
c-kzg = { workspace = true, optional = true }

[dev-dependencies]
reth-rlp = { workspace = true, features = [
"derive",
Expand All @@ -31,9 +34,11 @@ criterion = "0.5.0"
pprof = { version = "0.12", features = ["flamegraph", "frame-pointer", "criterion"] }

[features]
default = ["kzg"]
alloc = []
derive = ["reth-rlp-derive"]
std = ["alloc"]
kzg = ["c-kzg"]

[[bench]]
name = "bench"
Expand Down
71 changes: 71 additions & 0 deletions crates/rlp/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,77 @@ pub fn encode_fixed_size<E: MaxEncodedLen<LEN>, const LEN: usize>(v: &E) -> Arra
out
}

#[cfg(feature = "kzg")]
mod kzg_support {
extern crate c_kzg;

use super::BufMut;
use crate::{Decodable, DecodeError, Encodable};
use c_kzg::{Blob, Bytes48, KzgCommitment, KzgProof, BYTES_PER_BLOB, BYTES_PER_COMMITMENT};
use core::ops::Deref;

impl Encodable for Blob {
fn encode(&self, out: &mut dyn BufMut) {
// Deref is implemented to get the underlying bytes
self.deref().encode(out);
}

fn length(&self) -> usize {
self.deref().length()
}
}

impl Decodable for Blob {
fn decode(buf: &mut &[u8]) -> Result<Self, DecodeError> {
let bytes: [u8; BYTES_PER_BLOB] = Decodable::decode(buf)?;
Ok(Blob::from(bytes))
}
}

impl Encodable for Bytes48 {
fn encode(&self, out: &mut dyn BufMut) {
self.deref().encode(out);
}

fn length(&self) -> usize {
self.deref().length()
}
}

impl Decodable for Bytes48 {
fn decode(buf: &mut &[u8]) -> Result<Self, DecodeError> {
let bytes: [u8; BYTES_PER_COMMITMENT] = Decodable::decode(buf)?;
Ok(Bytes48::from(bytes))
}
}

/// Only [Encodable] is implemented for [KzgCommitment] because this is a validated type - it
/// should be decoded using [Decodable] into a [Bytes48] type, validated, _then_ converted
/// into a [KzgCommitment].
impl Encodable for KzgCommitment {
fn encode(&self, out: &mut dyn BufMut) {
self.deref().encode(out);
}

fn length(&self) -> usize {
self.deref().length()
}
}

/// Only [Encodable] is implemented for [KzgProof] because this is a validated type - it should
/// be decoded using [Decodable] into a [Bytes48] type, validated, _then_ converted into a
/// [KzgProof].
impl Encodable for KzgProof {
fn encode(&self, out: &mut dyn BufMut) {
self.deref().encode(out);
}

fn length(&self) -> usize {
self.deref().length()
}
}
}

#[cfg(test)]
mod tests {
extern crate alloc;
Expand Down
7 changes: 7 additions & 0 deletions deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ name = "rustls-webpki"
expression = "LicenseRef-rustls-webpki"
license-files = [{ path = "LICENSE", hash = 0x001c7e6c }]

[[licenses.clarify]]
name = "c-kzg"
expression = "Apache-2.0"
# The crate is in `bindings/rust` so we have to go up two directories for the
# license
license-files = [{ path = "../../LICENSE", hash = 0x13cec820 }]

# This section is considered when running `cargo deny check sources`.
# More documentation about the 'sources' section can be found here:
# https://embarkstudios.github.io/cargo-deny/checks/sources/cfg.html
Expand Down
Loading