This commit is contained in:
Pawan Dhananjay 2022-11-16 00:16:52 +05:30 committed by realbigsean
parent 48b2efce9f
commit 3288404ec1
No known key found for this signature in database
GPG Key ID: B372B64D866BF8CC
14 changed files with 138 additions and 26 deletions

30
Cargo.lock generated
View File

@ -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",

View File

@ -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",

View File

@ -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<T: EthSpec>,
) -> 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<T: EthSpec>]) -> KzgProof {
unimplemented!()
}

View File

@ -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" }

View File

@ -1,4 +1,4 @@
use crate::kzg_commitment::KzgCommitment;
use super::KzgCommitment;
use crate::test_utils::TestRandom;
use crate::*;
use derivative::Derivative;

View File

@ -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};

View File

@ -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;

View File

@ -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;

View File

@ -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))
}
}

View File

@ -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)
}
}

22
crypto/kzg/Cargo.toml Normal file
View File

@ -0,0 +1,22 @@
[package]
name = "kzg"
version = "0.1.0"
authors = ["Pawan Dhananjay <pawandhananjay@gmail.com>"]
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" }

View File

@ -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))
}
}

View File

@ -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)
}
}

30
crypto/kzg/src/lib.rs Normal file
View File

@ -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<Self, Error> {
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() {}
}