Skip to content

Commit

Permalink
WIP: switch to rust-kzg for das
Browse files Browse the repository at this point in the history
  • Loading branch information
hangleang committed Jan 16, 2025
1 parent 4a365bc commit e5584a0
Show file tree
Hide file tree
Showing 15 changed files with 442 additions and 326 deletions.
19 changes: 3 additions & 16 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,6 @@ binary_utils = { path = 'binary_utils' }
block_producer = { path = 'block_producer' }
bls = { path = 'bls' }
builder_api = { path = 'builder_api' }
c-kzg = { git = 'https://github.com/ethereum/c-kzg-4844', branch = 'das' }
clock = { path = 'clock' }
database = { path = 'database' }
deposit_tree = { path = 'deposit_tree' }
Expand Down
4 changes: 2 additions & 2 deletions eip_7594/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ workspace = true

[dependencies]
anyhow = { workspace = true }
c-kzg = { workspace = true }
helper_functions = { workspace = true }
itertools = { workspace = true }
kzg = { workspace = true }
kzg_utils = { workspace = true }
num-traits = { workspace = true }
ssz = { workspace = true}
sha2 = { workspace = true }
thiserror = { workspace = true }
try_from_iterator = { workspace = true }
typenum = { workspace = true }
types = { workspace = true }
prometheus_metrics = { workspace = true, optional = true }

Expand Down
96 changes: 33 additions & 63 deletions eip_7594/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
use std::collections::{BTreeSet, HashSet};

use anyhow::{ensure, Result};
use c_kzg::{
Blob as CKzgBlob, Bytes48, Cell as CKzgCell, KzgProof as CKzgProof, CELLS_PER_EXT_BLOB,
};
use helper_functions::{misc, predicates::is_valid_merkle_branch};
use itertools::Itertools as _;
use kzg as _;
use kzg_utils::eip_7594::{
compute_cells_and_kzg_proofs, recover_cells_and_kzg_proofs, verify_cell_kzg_proof_batch,
};
use num_traits::One as _;
use sha2::{Digest as _, Sha256};
use ssz::{ByteVector, ContiguousList, ContiguousVector, SszHash as _, Uint256};
use try_from_iterator::TryFromIterator as _;
use typenum::Unsigned;
use types::{
combined::SignedBeaconBlock,
config::Config,
deneb::primitives::{Blob, KzgProof},
deneb::{
consts::BytesPerFieldElement,
primitives::{Blob, KzgProof},
},
fulu::{
consts::NumberOfColumns,
containers::{DataColumnSidecar, MatrixEntry},
primitives::{Cell, ColumnIndex, CustodyIndex},
},
Expand All @@ -25,16 +29,13 @@ use types::{
};

use error::Error;
use trusted_setup::settings;

mod error;
mod trusted_setup;

#[cfg(test)]
mod tests;

type ColumnCells = [Cell; CELLS_PER_EXT_BLOB];
type ColumnProofs = [KzgProof; CELLS_PER_EXT_BLOB];
type ColumnCells = [Cell; NumberOfColumns::USIZE];
type ColumnProofs = [KzgProof; NumberOfColumns::USIZE];
type CellsAndKzgProofs = Vec<(ColumnCells, ColumnProofs)>;

#[cfg(feature = "metrics")]
Expand Down Expand Up @@ -187,30 +188,7 @@ pub fn verify_kzg_proofs<P: Preset>(data_column_sidecar: &DataColumnSidecar<P>)

let cell_indices: Vec<u64> = vec![*index; column.len()];

let cells = column
.clone()
.into_iter()
.map(|a| CKzgCell::from_bytes(a.as_bytes()).map_err(Into::into))
.collect::<Result<Vec<_>>>()?;

let commitments = kzg_commitments
.iter()
.map(|a| Bytes48::from_bytes(a.as_bytes()).map_err(Into::into))
.collect::<Result<Vec<_>>>()?;

let kzg_proofs = kzg_proofs
.iter()
.map(|a| Bytes48::from_bytes(a.as_bytes()).map_err(Into::into))
.collect::<Result<Vec<_>>>()?;

CKzgProof::verify_cell_kzg_proof_batch(
commitments.as_slice(),
cell_indices.as_slice(),
cells.as_slice(),
kzg_proofs.as_slice(),
settings(),
)
.map_err(Into::into)
verify_cell_kzg_proof_batch(kzg_commitments, cell_indices, column, kzg_proofs)
}

pub fn verify_sidecar_inclusion_proof<P: Preset>(
Expand Down Expand Up @@ -247,14 +225,15 @@ pub fn verify_sidecar_inclusion_proof<P: Preset>(
*
* This helper demonstrates the relationship between blobs and the matrix of cells/proofs.
*/
pub fn compute_matrix(blobs: &[CKzgBlob]) -> Result<Vec<MatrixEntry>> {
pub fn compute_matrix<P: Preset>(blobs: impl Iterator<Item = Blob<P>>) -> Result<Vec<MatrixEntry>> {
let mut matrix = vec![];
for (blob_index, blob) in blobs.iter().enumerate() {
let (cells, proofs) = CKzgCell::compute_cells_and_kzg_proofs(blob, settings())?;
for (cell_index, (cell, proof)) in cells.into_iter().zip(proofs.into_iter()).enumerate() {
for (blob_index, blob) in blobs.enumerate() {
let (cells, proofs) = compute_cells_and_kzg_proofs::<P>(&blob)?;
for (cell_index, (cell, kzg_proof)) in cells.into_iter().zip(proofs.into_iter()).enumerate()
{
matrix.push(MatrixEntry {
cell: try_convert_to_cell(&cell)?,
kzg_proof: KzgProof::from(proof.to_bytes().into_inner()),
cell: try_convert_to_cell(cell)?,
kzg_proof,
row_index: blob_index as u64,
column_index: cell_index as u64,
});
Expand All @@ -280,28 +259,24 @@ pub fn recover_matrix(

let mut matrix = vec![];
for blob_index in 0..blob_count {
let (cell_indexs, cells_bytes): (Vec<_>, Vec<_>) = partial_matrix
.iter()
// TODO(feature/fulu): use cell reference, remove clone
let (cell_indices, cells): (Vec<_>, Vec<_>) = partial_matrix
.into_iter()
.filter(|&e| (e.row_index == blob_index as u64))
.map(|e| (e.column_index, e.cell.as_bytes()))
.map(|e| (e.column_index, e.cell.clone()))
.unzip();

let cells = cells_bytes
.into_iter()
.map(|c| CKzgCell::from_bytes(c).map_err(Into::into))
.collect::<Result<Vec<CKzgCell>>>()?;

let (recovered_cells, recovered_proofs) =
CKzgCell::recover_cells_and_kzg_proofs(&cell_indexs, &cells, settings())?;
recover_cells_and_kzg_proofs(cell_indices, cells)?;

for (cell_index, (cell, proof)) in recovered_cells
.into_iter()
.zip(recovered_proofs.into_iter())
.enumerate()
{
matrix.push(MatrixEntry {
cell: try_convert_to_cell(&cell)?,
kzg_proof: KzgProof::from(proof.to_bytes().into_inner()),
cell: try_convert_to_cell(cell)?,
kzg_proof: proof,
row_index: blob_index as u64,
column_index: cell_index as u64,
});
Expand Down Expand Up @@ -365,27 +340,22 @@ pub fn try_convert_to_cells_and_kzg_proofs<P: Preset>(
blobs: impl Iterator<Item = Blob<P>>,
) -> Result<CellsAndKzgProofs> {
let cells_and_kzg_proofs = blobs
.map(|blob| {
let c_kzg_blob = CKzgBlob::from_bytes(blob.as_bytes())?;
CKzgCell::compute_cells_and_kzg_proofs(&c_kzg_blob, settings()).map_err(Into::into)
})
.map(|blob| compute_cells_and_kzg_proofs::<P>(&blob))
.collect::<Result<Vec<_>>>()?;

let mut result = vec![];
for (cells, proofs) in cells_and_kzg_proofs {
let cells = cells
.iter()
.into_iter()
.map(try_convert_to_cell)
.collect::<Result<Vec<_>>>()?;
let column_cells = cells
.try_into()
.expect("column cells should have length of CELLS_PER_EXT_BLOB");

let proofs = proofs
.iter()
.map(|proof| KzgProof::from(proof.to_bytes().into_inner()))
.collect::<Vec<_>>();
let column_proofs = proofs
.into_iter()
.collect::<Vec<_>>()
.try_into()
.expect("column proofs should have length of CELLS_PER_EXT_BLOB");

Expand All @@ -395,8 +365,8 @@ pub fn try_convert_to_cells_and_kzg_proofs<P: Preset>(
Ok(result)
}

fn try_convert_to_cell(cell: &CKzgCell) -> Result<Cell> {
ContiguousVector::try_from_iter(cell.to_bytes())
fn try_convert_to_cell(cell: [u8; BytesPerFieldElement::USIZE]) -> Result<Cell> {
ContiguousVector::try_from_iter(cell)
.map(ByteVector::from)
.map(Cell::from)
.map_err(Into::into)
Expand All @@ -410,7 +380,7 @@ pub fn construct_cells_and_kzg_proofs(
let mut cells_and_kzg_proofs = vec![
(
core::array::from_fn(|_| default_cell.clone()),
[KzgProof::zero(); CELLS_PER_EXT_BLOB],
[KzgProof::zero(); NumberOfColumns::USIZE],
);
blob_count
];
Expand Down
Loading

0 comments on commit e5584a0

Please sign in to comment.