Skip to content

Commit

Permalink
Introduce type alias CellsAndKzgProofs to address type complexity.
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmygchen committed Jun 19, 2024
1 parent 5655ebd commit d857d73
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 38 deletions.
38 changes: 9 additions & 29 deletions crypto/kzg/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ pub use c_kzg::{
pub use c_kzg::{Cell, CELLS_PER_EXT_BLOB};
use mockall::automock;

pub type CellsAndKzgProofs = (
Box<[Cell; CELLS_PER_EXT_BLOB]>,
Box<[KzgProof; CELLS_PER_EXT_BLOB]>,
);

#[derive(Debug)]
pub enum Error {
/// An error from the underlying kzg library.
Expand Down Expand Up @@ -152,17 +157,7 @@ impl Kzg {
}

/// Computes the cells and associated proofs for a given `blob` at index `index`.
#[allow(clippy::type_complexity)]
pub fn compute_cells_and_proofs(
&self,
blob: &Blob,
) -> Result<
(
Box<[Cell; CELLS_PER_EXT_BLOB]>,
Box<[KzgProof; CELLS_PER_EXT_BLOB]>,
),
Error,
> {
pub fn compute_cells_and_proofs(&self, blob: &Blob) -> Result<CellsAndKzgProofs, Error> {
let (cells, proofs) = c_kzg::Cell::compute_cells_and_kzg_proofs(blob, &self.trusted_setup)
.map_err(Into::<Error>::into)?;
let proofs = Box::new(proofs.map(|proof| KzgProof::from(proof.to_bytes().into_inner())));
Expand Down Expand Up @@ -204,35 +199,20 @@ impl Kzg {
&self,
cell_ids: &[u64],
cells: &[Cell],
) -> Result<
(
Box<[Cell; CELLS_PER_EXT_BLOB]>,
Box<[KzgProof; CELLS_PER_EXT_BLOB]>,
),
Error,
> {
) -> Result<CellsAndKzgProofs, Error> {
let all_cells = c_kzg::Cell::recover_all_cells(cell_ids, cells, &self.trusted_setup)?;
let blob = self.cells_to_blob(&all_cells)?;
self.compute_cells_and_proofs(&blob)
}
}

pub mod mock {
use crate::{Error, KzgProof};
use crate::{CellsAndKzgProofs, Error, KzgProof};
use c_kzg::{Blob, Cell, CELLS_PER_EXT_BLOB};

pub const MOCK_KZG_BYTES_PER_CELL: usize = 2048;

#[allow(clippy::type_complexity)]
pub fn compute_cells_and_proofs(
_blob: &Blob,
) -> Result<
(
Box<[Cell; CELLS_PER_EXT_BLOB]>,
Box<[KzgProof; CELLS_PER_EXT_BLOB]>,
),
Error,
> {
pub fn compute_cells_and_proofs(_blob: &Blob) -> Result<CellsAndKzgProofs, Error> {
let empty_cell = Cell::new([0; MOCK_KZG_BYTES_PER_CELL]);
Ok((
Box::new([empty_cell; CELLS_PER_EXT_BLOB]),
Expand Down
11 changes: 2 additions & 9 deletions testing/ef_tests/src/cases/kzg_compute_cells_and_kzg_proofs.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use super::*;
use crate::case_result::compare_result;
use kzg::{Blob as KzgBlob, Cell};
use kzg::{KzgProof, CELLS_PER_EXT_BLOB};
use kzg::{Blob as KzgBlob, CellsAndKzgProofs};
use serde::Deserialize;
use std::marker::PhantomData;

Expand Down Expand Up @@ -62,12 +61,6 @@ impl<E: EthSpec> Case for KZGComputeCellsAndKZGProofs<E> {
.ok()
});

compare_result::<
(
Box<[Cell; CELLS_PER_EXT_BLOB]>,
Box<[KzgProof; CELLS_PER_EXT_BLOB]>,
),
_,
>(&cells_and_proofs, &expected)
compare_result::<CellsAndKzgProofs, _>(&cells_and_proofs, &expected)
}
}

0 comments on commit d857d73

Please sign in to comment.