diff --git a/Cargo.lock b/Cargo.lock index 2376a7175..0d08c1a66 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -708,6 +708,15 @@ dependencies = [ "pkg-config", ] +[[package]] +name = "c-kzg" +version = "0.1.0" +source = "git+https://github.com/pawanjay176/c-kzg-4844?rev=f4b0c2a84e7a90fa2e0e4e04e5d777be146c6e94#f4b0c2a84e7a90fa2e0e4e04e5d777be146c6e94" +dependencies = [ + "hex", + "libc", +] + [[package]] name = "cached_tree_hash" version = "0.1.0" @@ -3071,6 +3080,25 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f9b7d56ba4a8344d6be9729995e6b06f928af29998cdf79fe390cbf6b1fee838" +[[package]] +name = "kzg" +version = "0.1.0" +dependencies = [ + "c-kzg", + "derivative", + "eth2_hashing", + "eth2_serde_utils", + "eth2_ssz", + "eth2_ssz_derive", + "ethereum-types 0.12.1", + "hex", + "rand 0.7.3", + "serde", + "serde-big-array", + "serde_derive", + "tree_hash", +] + [[package]] name = "lazy_static" version = "1.4.0" @@ -6933,6 +6961,7 @@ dependencies = [ "hex", "int_to_bytes", "itertools", + "kzg", "lazy_static", "log", "maplit", @@ -6944,7 +6973,6 @@ dependencies = [ "rusqlite", "safe_arith", "serde", - "serde-big-array", "serde_derive", "serde_json", "serde_with", diff --git a/Cargo.toml b/Cargo.toml index 02cf4d943..d315d689f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -62,10 +62,12 @@ members = [ "consensus/tree_hash_derive", "crypto/bls", + "crypto/kzg", "crypto/eth2_hashing", "crypto/eth2_key_derivation", "crypto/eth2_keystore", "crypto/eth2_wallet", + "crypto/kzg", "lcli", diff --git a/beacon_node/beacon_chain/src/kzg_utils.rs b/beacon_node/beacon_chain/src/kzg_utils.rs new file mode 100644 index 000000000..11060a131 --- /dev/null +++ b/beacon_node/beacon_chain/src/kzg_utils.rs @@ -0,0 +1,27 @@ +use types::{Blob, BlobsSidecar, EthSpec, KzgCommitment, KzgProof}; + +pub fn validate_blobs_sidecar( + slot: Slot, + beacon_block_root: Hash256, + expected_kzg_commitments: &[KzgCommitment], + blobs_sidecar: BlobsSidecar, +) -> bool { + //TODO(pawan): change to a Result later + if slot != blobs_sidecar.blobs + || beacon_block_root != blobs_sidecar.beacon_block_root + || blobs_sidecar.blobs.len() != expected_kzg_commitments.len() + || !verify_aggregate_kzg_proof( + blobs_sidecar.blobs, + expected_kzg_commitments, + blobs_sidecar.kzg_aggregate_proof, + ) + { + return false; + } else { + return true; + } +} + +pub fn compute_aggregate_kzg_proof(blobs: &[Blob]) -> KzgProof { + unimplemented!() +} diff --git a/consensus/types/Cargo.toml b/consensus/types/Cargo.toml index c787a7a87..36353d679 100644 --- a/consensus/types/Cargo.toml +++ b/consensus/types/Cargo.toml @@ -9,8 +9,8 @@ name = "benches" harness = false [dependencies] -serde-big-array = {version = "0.3.2", features = ["const-generics"]} bls = { path = "../../crypto/bls" } +kzg = { path = "../../crypto/kzg" } compare_fields = { path = "../../common/compare_fields" } compare_fields_derive = { path = "../../common/compare_fields_derive" } eth2_interop_keypairs = { path = "../../common/eth2_interop_keypairs" } diff --git a/consensus/types/src/beacon_block_body.rs b/consensus/types/src/beacon_block_body.rs index 1dd938ac4..e3d1be5b1 100644 --- a/consensus/types/src/beacon_block_body.rs +++ b/consensus/types/src/beacon_block_body.rs @@ -1,4 +1,4 @@ -use crate::kzg_commitment::KzgCommitment; +use super::KzgCommitment; use crate::test_utils::TestRandom; use crate::*; use derivative::Derivative; diff --git a/consensus/types/src/blobs_sidecar.rs b/consensus/types/src/blobs_sidecar.rs index d4e779606..6a7aa7b66 100644 --- a/consensus/types/src/blobs_sidecar.rs +++ b/consensus/types/src/blobs_sidecar.rs @@ -1,5 +1,5 @@ -use crate::kzg_proof::KzgProof; use crate::{Blob, EthSpec, Hash256, SignedRoot, Slot}; +use kzg::KzgProof; use serde_derive::{Deserialize, Serialize}; use ssz::Encode; use ssz_derive::{Decode, Encode}; diff --git a/consensus/types/src/lib.rs b/consensus/types/src/lib.rs index 077ad7ecc..c5faf5049 100644 --- a/consensus/types/src/lib.rs +++ b/consensus/types/src/lib.rs @@ -97,8 +97,6 @@ pub mod slot_data; pub mod sqlite; pub mod blobs_sidecar; -pub mod kzg_commitment; -pub mod kzg_proof; pub mod signed_block_and_blobs; use ethereum_types::{H160, H256}; @@ -151,8 +149,6 @@ pub use crate::free_attestation::FreeAttestation; pub use crate::graffiti::{Graffiti, GRAFFITI_BYTES_LEN}; pub use crate::historical_batch::HistoricalBatch; pub use crate::indexed_attestation::IndexedAttestation; -pub use crate::kzg_commitment::KzgCommitment; -pub use crate::kzg_proof::KzgProof; pub use crate::participation_flags::ParticipationFlags; pub use crate::participation_list::ParticipationList; pub use crate::payload::{ @@ -195,7 +191,6 @@ pub use crate::validator_registration_data::*; pub use crate::validator_subscription::ValidatorSubscription; pub use crate::voluntary_exit::VoluntaryExit; pub use crate::withdrawal::Withdrawal; -use serde_big_array::BigArray; pub type CommitteeIndex = u64; pub type Hash256 = H256; @@ -210,5 +205,8 @@ pub use bls::{ AggregatePublicKey, AggregateSignature, Keypair, PublicKey, PublicKeyBytes, SecretKey, Signature, SignatureBytes, }; + +pub use kzg::{KzgCommitment, KzgProof}; + pub use ssz_types::{typenum, typenum::Unsigned, BitList, BitVector, FixedVector, VariableList}; pub use superstruct::superstruct; diff --git a/consensus/types/src/test_utils/test_random.rs b/consensus/types/src/test_utils/test_random.rs index 43396dedc..a9e79d14f 100644 --- a/consensus/types/src/test_utils/test_random.rs +++ b/consensus/types/src/test_utils/test_random.rs @@ -10,6 +10,8 @@ mod address; mod aggregate_signature; mod bitfield; mod hash256; +mod kzg_commitment; +mod kzg_proof; mod public_key; mod public_key_bytes; mod secret_key; diff --git a/consensus/types/src/test_utils/test_random/kzg_commitment.rs b/consensus/types/src/test_utils/test_random/kzg_commitment.rs new file mode 100644 index 000000000..7ba5f780c --- /dev/null +++ b/consensus/types/src/test_utils/test_random/kzg_commitment.rs @@ -0,0 +1,8 @@ +use super::*; +use crate::KzgCommitment; + +impl TestRandom for KzgCommitment { + fn random_for_test(rng: &mut impl rand::RngCore) -> Self { + KzgCommitment(<[u8; 48] as TestRandom>::random_for_test(rng)) + } +} diff --git a/consensus/types/src/test_utils/test_random/kzg_proof.rs b/consensus/types/src/test_utils/test_random/kzg_proof.rs new file mode 100644 index 000000000..a93253a23 --- /dev/null +++ b/consensus/types/src/test_utils/test_random/kzg_proof.rs @@ -0,0 +1,11 @@ +use super::*; +use kzg::KzgProof; + +impl TestRandom for KzgProof { + fn random_for_test(rng: &mut impl RngCore) -> Self { + // TODO(pawan): use the length constant here + let mut bytes = [0; 48]; + rng.fill_bytes(&mut bytes); + Self(bytes) + } +} diff --git a/crypto/kzg/Cargo.toml b/crypto/kzg/Cargo.toml new file mode 100644 index 000000000..d861eb3a4 --- /dev/null +++ b/crypto/kzg/Cargo.toml @@ -0,0 +1,22 @@ +[package] +name = "kzg" +version = "0.1.0" +authors = ["Pawan Dhananjay "] +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +eth2_ssz = "0.4.1" +eth2_ssz_derive = "0.3.1" +tree_hash = "0.4.1" +derivative = "2.1.1" +rand = "0.7.3" +serde = "1.0.116" +serde_derive = "1.0.116" +serde-big-array = {version = "0.3.2", features = ["const-generics"]} +eth2_serde_utils = "0.1.1" +hex = "0.4.2" +eth2_hashing = "0.3.0" +ethereum-types = "0.12.1" +c-kzg = {git = "https://github.com/pawanjay176/c-kzg-4844", rev = "f4b0c2a84e7a90fa2e0e4e04e5d777be146c6e94" } \ No newline at end of file diff --git a/consensus/types/src/kzg_commitment.rs b/crypto/kzg/src/kzg_commitment.rs similarity index 82% rename from consensus/types/src/kzg_commitment.rs rename to crypto/kzg/src/kzg_commitment.rs index 9844df028..e2b977596 100644 --- a/consensus/types/src/kzg_commitment.rs +++ b/crypto/kzg/src/kzg_commitment.rs @@ -1,6 +1,5 @@ -use crate::test_utils::TestRandom; -use crate::*; use derivative::Derivative; +use serde_big_array::BigArray; use serde_derive::{Deserialize, Serialize}; use ssz_derive::{Decode, Encode}; use std::fmt; @@ -35,9 +34,3 @@ impl TreeHash for KzgCommitment { self.0.tree_hash_root() } } - -impl TestRandom for KzgCommitment { - fn random_for_test(rng: &mut impl rand::RngCore) -> Self { - KzgCommitment(<[u8; 48] as TestRandom>::random_for_test(rng)) - } -} diff --git a/consensus/types/src/kzg_proof.rs b/crypto/kzg/src/kzg_proof.rs similarity index 85% rename from consensus/types/src/kzg_proof.rs rename to crypto/kzg/src/kzg_proof.rs index 1c8e49a44..160e37845 100644 --- a/consensus/types/src/kzg_proof.rs +++ b/crypto/kzg/src/kzg_proof.rs @@ -1,4 +1,3 @@ -use crate::test_utils::{RngCore, TestRandom}; use serde::{Deserialize, Serialize}; use serde_big_array::BigArray; use ssz_derive::{Decode, Encode}; @@ -53,11 +52,3 @@ impl TreeHash for KzgProof { self.0.tree_hash_root() } } - -impl TestRandom for KzgProof { - fn random_for_test(rng: &mut impl RngCore) -> Self { - let mut bytes = [0; KZG_PROOF_BYTES_LEN]; - rng.fill_bytes(&mut bytes); - Self(bytes) - } -} diff --git a/crypto/kzg/src/lib.rs b/crypto/kzg/src/lib.rs new file mode 100644 index 000000000..daf958c3a --- /dev/null +++ b/crypto/kzg/src/lib.rs @@ -0,0 +1,30 @@ +mod kzg_commitment; +mod kzg_proof; + +use std::path::PathBuf; + +use c_kzg::{Error as CKzgError, KZGSettings}; + +pub use crate::{kzg_commitment::KzgCommitment, kzg_proof::KzgProof}; + +#[derive(Debug)] +pub enum Error { + InvalidTrustedSetup(CKzgError), +} + +pub struct Kzg { + _trusted_setup: KZGSettings, +} + +impl Kzg { + pub fn new_from_file(file_path: PathBuf) -> Result { + Ok(Self { + _trusted_setup: KZGSettings::load_trusted_setup(file_path) + .map_err(|e| Error::InvalidTrustedSetup(e))?, + }) + } + + pub fn verify_aggregate_kzg_proof() {} + + pub fn blob_to_kzg_commitment() {} +}