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

deneb: implement poly comm spec #225

Merged
merged 25 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
1f19c68
Create initial polynomial commitments spec
EchoAlice Sep 7, 2023
976ac3f
Implement blob_to_kzg_commitment() leveraging C-KZG lib. Remove old …
EchoAlice Sep 14, 2023
e6ad0ca
Implement compute_kzg_proof()
EchoAlice Sep 19, 2023
7943c25
Return proof *and* evaluation of polynomial at 'z' for compute_kzg_pr…
EchoAlice Sep 19, 2023
c61c333
Implement compute_blob_kzg_proof
EchoAlice Sep 19, 2023
ab8d032
Iterate on Alex's feedback.
EchoAlice Sep 20, 2023
6bfda99
Implement remaining interface functions from C-KZG
EchoAlice Sep 21, 2023
f376160
Remove unnecessary comment
EchoAlice Sep 25, 2023
61bc784
Create ProofAndEvaluation struct. Simplify verify...batch() logic
EchoAlice Sep 27, 2023
1add26d
Reexport KzgSettings
EchoAlice Oct 2, 2023
0aa5fd2
Remove private Deneb item
EchoAlice Oct 2, 2023
e78d66d
Fix botched rebase
EchoAlice Oct 2, 2023
88758e2
Leverage Blob type and Bytes32 struct from repository. Change argumen…
EchoAlice Oct 6, 2023
99f62e5
Use custom KzgCommitment and KzgProof types as arguments to relevant …
EchoAlice Oct 6, 2023
f45f5ea
Create type alias' with custom type `Bytes48`. Remove unused consts
EchoAlice Oct 6, 2023
8c78fe6
Fix bogus fn outputs
EchoAlice Oct 6, 2023
91b5672
Implement more idiomatic return types for functions.
EchoAlice Oct 7, 2023
351bcb6
WIP - Iteration over Alex's feedback
EchoAlice Oct 10, 2023
f391e4d
Convert `.unwrap()`s to `.expect()`s and iterate on other feedback (WIP)
EchoAlice Oct 10, 2023
aac268b
Remove unnecessary type alias. Refactor result logic
EchoAlice Oct 11, 2023
5bf73fe
Create custom Error type so we don't leak c_kzg info from our module
EchoAlice Oct 11, 2023
5556b2e
Apply suggestions from code review
ralexstokes Oct 11, 2023
16886a4
Create Error enum that handles all cases for errors to return from ou…
EchoAlice Oct 11, 2023
0842b88
Move features
EchoAlice Oct 11, 2023
b164746
Apply suggestions from code review
ralexstokes Oct 12, 2023
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
1 change: 1 addition & 0 deletions ethereum-consensus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ multihash = { version = "0.16", default-features = false, features = [
"sha2",
] }
multiaddr = "0.14.0"
c-kzg = "0.1.1"

serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0.81", optional = true }
Expand Down
3 changes: 1 addition & 2 deletions ethereum-consensus/src/deneb/beacon_block.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::{
altair::SyncAggregate,
capella::SignedBlsToExecutionChange,
deneb::ExecutionPayload,
kzg::KzgCommitment,
deneb::{polynomial_commitments::KzgCommitment, ExecutionPayload},
phase0::{
Attestation, AttesterSlashing, Deposit, Eth1Data, ProposerSlashing, SignedVoluntaryExit,
},
Expand Down
3 changes: 1 addition & 2 deletions ethereum-consensus/src/deneb/blinded_beacon_block.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::{
altair::SyncAggregate,
capella::SignedBlsToExecutionChange,
deneb::ExecutionPayloadHeader,
kzg::KzgCommitment,
deneb::{polynomial_commitments::KzgCommitment, ExecutionPayloadHeader},
phase0::{
Attestation, AttesterSlashing, Deposit, Eth1Data, ProposerSlashing, SignedVoluntaryExit,
},
Expand Down
2 changes: 1 addition & 1 deletion ethereum-consensus/src/deneb/blinded_blob_sidecar.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
kzg::{KzgCommitment, KzgProof},
deneb::polynomial_commitments::{KzgCommitment, KzgProof},
primitives::{BlobIndex, BlsSignature, Root, Slot, ValidatorIndex},
ssz::prelude::*,
};
Expand Down
2 changes: 1 addition & 1 deletion ethereum-consensus/src/deneb/blob_sidecar.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
kzg::{KzgCommitment, KzgProof},
deneb::polynomial_commitments::{KzgCommitment, KzgProof},
primitives::{BlobIndex, BlsSignature, Root, Slot, ValidatorIndex},
ssz::prelude::*,
};
Expand Down
3 changes: 1 addition & 2 deletions ethereum-consensus/src/deneb/execution_engine.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::{
deneb::execution_payload::ExecutionPayload,
kzg::VersionedHash,
deneb::{execution_payload::ExecutionPayload, polynomial_commitments::VersionedHash},
primitives::Root,
state_transition::{ExecutionEngineError, Result},
};
Expand Down
7 changes: 4 additions & 3 deletions ethereum-consensus/src/deneb/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ use crate::{
},
crypto::hash,
deneb::{
beacon_state::BeaconState, get_block_root, get_block_root_at_slot, get_current_epoch,
get_validator_churn_limit, AttestationData, VERSIONED_HASH_VERSION_KZG,
beacon_state::BeaconState,
get_block_root, get_block_root_at_slot, get_current_epoch, get_validator_churn_limit,
polynomial_commitments::{KzgCommitment, VersionedHash},
AttestationData, VERSIONED_HASH_VERSION_KZG,
},
kzg::{KzgCommitment, VersionedHash},
state_transition::{
invalid_operation_error, Context, InvalidAttestation, InvalidOperation, Result,
},
Expand Down
1 change: 1 addition & 0 deletions ethereum-consensus/src/deneb/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub mod fork;
pub mod genesis;
pub mod helpers;
pub mod networking;
pub mod polynomial_commitments;
pub mod presets;
pub mod spec;

Expand Down
137 changes: 137 additions & 0 deletions ethereum-consensus/src/deneb/polynomial_commitments.rs
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll try to be less sloppy next time

Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
use crate::{deneb::blob_sidecar::Blob, primitives::Bytes32, ssz::prelude::*};
pub use c_kzg::KzgSettings;
use thiserror::Error;

pub const BYTES_PER_FIELD_ELEMENT: usize = 32;
pub const BYTES_PER_COMMITMENT: usize = 48;
pub const BYTES_PER_PROOF: usize = 48;

pub type VersionedHash = Bytes32;
pub type FieldElement = Bytes32;
pub type KzgCommitment = ByteVector<BYTES_PER_COMMITMENT>;
pub type KzgProof = ByteVector<BYTES_PER_PROOF>;

#[derive(Debug, Error)]
pub enum Error {
#[error(transparent)]
CKzg(#[from] c_kzg::Error),
#[error("proof verification failed")]
InvalidProof,
}

EchoAlice marked this conversation as resolved.
Show resolved Hide resolved
pub struct ProofAndEvaluation {
pub proof: KzgProof,
pub evaluation: FieldElement,
}

pub fn blob_to_kzg_commitment<const BYTES_PER_BLOB: usize>(
blob: &Blob<BYTES_PER_BLOB>,
kzg_settings: &KzgSettings,
) -> Result<KzgCommitment, Error> {
let blob = c_kzg::Blob::from_bytes(blob.as_ref())?;

let commitment = c_kzg::KzgCommitment::blob_to_kzg_commitment(&blob, kzg_settings)?;
let inner = KzgCommitment::try_from(commitment.to_bytes().as_slice()).expect("correct size");
Ok(inner)
}

pub fn compute_kzg_proof<const BYTES_PER_BLOB: usize>(
blob: &Blob<BYTES_PER_BLOB>,
evaluation_point: &FieldElement,
kzg_settings: &KzgSettings,
) -> Result<ProofAndEvaluation, Error> {
let blob = c_kzg::Blob::from_bytes(blob.as_ref())?;
let evaluation_point = c_kzg::Bytes32::from_bytes(evaluation_point.as_ref())?;

let (proof, evaluation) =
c_kzg::KzgProof::compute_kzg_proof(&blob, &evaluation_point, kzg_settings)?;
let proof = KzgProof::try_from(proof.to_bytes().as_ref()).expect("correct size");
let evaluation = FieldElement::try_from(evaluation.as_slice()).expect("correct size");

let result = ProofAndEvaluation { proof, evaluation };
Ok(result)
}

pub fn compute_blob_kzg_proof<const BYTES_PER_BLOB: usize>(
blob: &Blob<BYTES_PER_BLOB>,
commitment: &KzgCommitment,
kzg_settings: &KzgSettings,
) -> Result<KzgProof, Error> {
let blob = c_kzg::Blob::from_bytes(blob.as_ref())?;
let commitment = c_kzg::Bytes48::from_bytes(commitment.as_ref()).expect("correct size");

let proof = c_kzg::KzgProof::compute_blob_kzg_proof(&blob, &commitment, kzg_settings)?;

Ok(KzgProof::try_from(proof.to_bytes().as_ref()).expect("input is correct size"))
}

pub fn verify_kzg_proof(
commitment: &KzgCommitment,
evaluation_point: &FieldElement,
result_point: &FieldElement,
proof: &KzgProof,
kzg_settings: &KzgSettings,
) -> Result<(), Error> {
let evaluation_point = c_kzg::Bytes32::from_bytes(evaluation_point.as_ref())?;
let result_point = c_kzg::Bytes32::from_bytes(result_point.as_ref())?;
let commitment = c_kzg::Bytes48::from_bytes(commitment.as_ref()).expect("correct size");
let proof = c_kzg::Bytes48::from_bytes(proof.as_ref()).expect("correct size");

let res = c_kzg::KzgProof::verify_kzg_proof(
&commitment,
&evaluation_point,
&result_point,
&proof,
kzg_settings,
)?;

res.then_some(()).ok_or(Error::InvalidProof)
EchoAlice marked this conversation as resolved.
Show resolved Hide resolved
}

pub fn verify_blob_kzg_proof<const BYTES_PER_BLOB: usize>(
blob: &Blob<BYTES_PER_BLOB>,
commitment: &KzgCommitment,
proof: &KzgProof,
kzg_settings: &KzgSettings,
) -> Result<(), Error> {
let blob = c_kzg::Blob::from_bytes(blob.as_ref())?;
let commitment = c_kzg::Bytes48::from_bytes(commitment.as_ref()).unwrap();
let proof = c_kzg::Bytes48::from_bytes(proof.as_ref()).unwrap();

let res = c_kzg::KzgProof::verify_blob_kzg_proof(&blob, &commitment, &proof, kzg_settings)?;

res.then_some(()).ok_or(Error::InvalidProof)
}

pub fn verify_blob_kzg_proof_batch<const BYTES_PER_BLOB: usize>(
blobs: &[Blob<BYTES_PER_BLOB>],
commitments: &[KzgCommitment],
proofs: &[KzgProof],
kzg_settings: &KzgSettings,
) -> Result<(), Error> {
let mut c_kzg_blobs = Vec::with_capacity(blobs.len());
let mut c_kzg_commitments = Vec::with_capacity(commitments.len());
let mut c_kzg_proofs = Vec::with_capacity(proofs.len());

for blob in blobs {
let blob = c_kzg::Blob::from_bytes(blob.as_ref())?;
c_kzg_blobs.push(blob);
}
for commitment in commitments {
let commitment = c_kzg::Bytes48::from_bytes(commitment.as_ref()).unwrap();
c_kzg_commitments.push(commitment);
}
for proof in proofs {
let proof = c_kzg::Bytes48::from_bytes(proof.as_ref()).unwrap();
c_kzg_proofs.push(proof);
}

let res = c_kzg::KzgProof::verify_blob_kzg_proof_batch(
&c_kzg_blobs,
&c_kzg_commitments,
&c_kzg_proofs,
kzg_settings,
)?;

res.then_some(()).ok_or(Error::InvalidProof)
}
3 changes: 2 additions & 1 deletion ethereum-consensus/src/deneb/presets/mainnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ pub const FIELD_ELEMENTS_PER_BLOB: usize = 4096;
pub const MAX_BLOB_COMMITMENTS_PER_BLOCK: usize = 4096;
pub const MAX_BLOBS_PER_BLOCK: usize = 6;

pub const BYTES_PER_BLOB: usize = crate::kzg::BYTES_PER_FIELD_ELEMENT * FIELD_ELEMENTS_PER_BLOB;
pub const BYTES_PER_BLOB: usize =
crate::deneb::polynomial_commitments::BYTES_PER_FIELD_ELEMENT * FIELD_ELEMENTS_PER_BLOB;

pub const MAX_REQUEST_BLOB_SIDECARS: usize = MAX_REQUEST_BLOCKS_DENEB * MAX_BLOBS_PER_BLOCK;

Expand Down
3 changes: 2 additions & 1 deletion ethereum-consensus/src/deneb/presets/minimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ pub const FIELD_ELEMENTS_PER_BLOB: usize = 4;
pub const MAX_BLOB_COMMITMENTS_PER_BLOCK: usize = 16;
pub const MAX_BLOBS_PER_BLOCK: usize = 6;

pub const BYTES_PER_BLOB: usize = crate::kzg::BYTES_PER_FIELD_ELEMENT * FIELD_ELEMENTS_PER_BLOB;
pub const BYTES_PER_BLOB: usize =
crate::deneb::polynomial_commitments::BYTES_PER_FIELD_ELEMENT * FIELD_ELEMENTS_PER_BLOB;

pub const MAX_REQUEST_BLOB_SIDECARS: usize = MAX_REQUEST_BLOCKS_DENEB * MAX_BLOBS_PER_BLOCK;

Expand Down
9 changes: 0 additions & 9 deletions ethereum-consensus/src/kzg.rs

This file was deleted.

1 change: 0 additions & 1 deletion ethereum-consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ pub mod crypto;
pub mod deneb;
pub mod domains;
mod fork;
pub mod kzg;
pub mod networking;
pub mod networks;
pub mod phase0;
Expand Down
3 changes: 3 additions & 0 deletions ethereum-consensus/src/state_transition/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{
capella::Withdrawal,
crypto::Error as CryptoError,
deneb::polynomial_commitments::Error as PolynomialCommitmentError,
phase0::{AttestationData, BeaconBlockHeader, Checkpoint},
primitives::{BlsPublicKey, BlsSignature, Bytes32, Epoch, Hash32, Root, Slot, ValidatorIndex},
ssz::prelude::*,
Expand Down Expand Up @@ -55,6 +56,8 @@ pub enum Error {
UnknownPreset(String),
#[error(transparent)]
ExecutionEngine(#[from] ExecutionEngineError),
#[error(transparent)]
PolynomialCommitment(#[from] PolynomialCommitmentError),
}

#[derive(Debug, Error)]
Expand Down
3 changes: 1 addition & 2 deletions ethereum-consensus/src/types/beacon_block_body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ use crate::{
altair::{beacon_block as altair, SyncAggregate},
bellatrix::beacon_block as bellatrix,
capella::{beacon_block as capella, SignedBlsToExecutionChange},
deneb::beacon_block as deneb,
kzg::KzgCommitment,
deneb::{beacon_block as deneb, polynomial_commitments::KzgCommitment},
phase0::{
beacon_block as phase0, Attestation, AttesterSlashing, Deposit, Eth1Data, ProposerSlashing,
SignedVoluntaryExit,
Expand Down
3 changes: 1 addition & 2 deletions ethereum-consensus/src/types/blinded_beacon_block_body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ use crate::{
altair::SyncAggregate,
bellatrix::blinded_beacon_block as bellatrix,
capella::{blinded_beacon_block as capella, SignedBlsToExecutionChange},
deneb::blinded_beacon_block as deneb,
kzg::KzgCommitment,
deneb::{blinded_beacon_block as deneb, polynomial_commitments::KzgCommitment},
phase0::{
Attestation, AttesterSlashing, Deposit, Eth1Data, ProposerSlashing, SignedVoluntaryExit,
},
Expand Down