Skip to content

Commit

Permalink
chore: impl Deref for DASContext (#183)
Browse files Browse the repository at this point in the history
* impl Deref

* refactor code
  • Loading branch information
kevaundray committed Aug 14, 2024
1 parent 0d26de1 commit 85772eb
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 12 deletions.
2 changes: 1 addition & 1 deletion bindings/c/src/blob_to_kzg_commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub(crate) fn _blob_to_kzg_commitment(

// Dereference the input pointers
//
let ctx = deref_const(ctx).inner();
let ctx = deref_const(ctx);
let blob = create_array_ref::<BYTES_PER_BLOB, _>(blob);

// Computation
Expand Down
2 changes: 1 addition & 1 deletion bindings/c/src/compute_cells_and_kzg_proofs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub(crate) fn _compute_cells_and_kzg_proofs(

// Pointer checks
//
let ctx = deref_const(ctx).inner();
let ctx = deref_const(ctx);
let blob = create_array_ref::<BYTES_PER_BLOB, _>(blob);

// Computation
Expand Down
9 changes: 9 additions & 0 deletions bindings/c/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub use rust_eth_kzg::constants::{
CELLS_PER_EXT_BLOB, FIELD_ELEMENTS_PER_BLOB,
};
pub use rust_eth_kzg::Error;
use std::ops::Deref;

/*
* Note: All methods in this file have been prefixed with `eth_kzg`.
Expand All @@ -41,6 +42,14 @@ impl DASContext {
}
}

impl Deref for DASContext {
type Target = rust_eth_kzg::DASContext;

fn deref(&self) -> &Self::Target {
&self.inner
}
}

/// Create a new DASContext and return a pointer to it.
///
/// # Memory faults
Expand Down
2 changes: 1 addition & 1 deletion bindings/c/src/recover_cells_and_kzg_proofs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub(crate) fn _recover_cells_and_proofs(

// Dereference the input pointers
//
let ctx = deref_const(ctx).inner();
let ctx = deref_const(ctx);
let cells = ptr_ptr_to_vec_slice_const::<BYTES_PER_CELL>(cells, cells_length as usize);
let cell_indices = create_slice_view(cell_indices, cell_indices_length as usize);

Expand Down
2 changes: 1 addition & 1 deletion bindings/c/src/verify_cells_and_kzg_proofs_batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub(crate) fn _verify_cell_kzg_proof_batch(

// Dereference the input pointers
//
let ctx = deref_const(ctx).inner();
let ctx = deref_const(ctx);
let commitments = ptr_ptr_to_vec_slice_const::<BYTES_PER_COMMITMENT>(
commitments,
commitments_length as usize,
Expand Down
12 changes: 4 additions & 8 deletions bindings/java/rust_code/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ fn compute_cells_and_kzg_proofs<'local>(
let blob = env.convert_byte_array(blob)?;
let blob = slice_to_array_ref(&blob, "blob")?;

let (cells, proofs) = ctx.inner().compute_cells_and_kzg_proofs(blob)?;
let (cells, proofs) = ctx.compute_cells_and_kzg_proofs(blob)?;
let cells = cells.map(|cell| *cell);
cells_and_proofs_to_jobject(env, &cells, &proofs).map_err(Error::from)
}
Expand Down Expand Up @@ -76,7 +76,7 @@ fn blob_to_kzg_commitment<'local>(
let blob = env.convert_byte_array(blob)?;
let blob = slice_to_array_ref(&blob, "blob")?;

let commitment = ctx.inner().blob_to_kzg_commitment(blob)?;
let commitment = ctx.blob_to_kzg_commitment(blob)?;
env.byte_array_from_slice(&commitment).map_err(Error::from)
}

Expand Down Expand Up @@ -126,10 +126,7 @@ fn verify_cell_kzg_proof_batch<'local>(
.map(|proof| slice_to_array_ref(proof, "proof"))
.collect::<Result<_, _>>()?;

match ctx
.inner()
.verify_cell_kzg_proof_batch(commitments, cell_indices, cells, proofs)
{
match ctx.verify_cell_kzg_proof_batch(commitments, cell_indices, cells, proofs) {
Ok(_) => Ok(jboolean::from(true)),
Err(x) if x.invalid_proof() => Ok(jboolean::from(false)),
Err(err) => Err(Error::Cryptography(err)),
Expand Down Expand Up @@ -167,8 +164,7 @@ fn recover_cells_and_kzg_proofs<'local>(
.map(|cell| slice_to_array_ref(cell, "cell"))
.collect::<Result<_, _>>()?;

let (recovered_cells, recovered_proofs) =
ctx.inner().recover_cells_and_proofs(cell_ids, cells)?;
let (recovered_cells, recovered_proofs) = ctx.recover_cells_and_proofs(cell_ids, cells)?;
let recovered_cells = recovered_cells.map(|cell| *cell);
cells_and_proofs_to_jobject(env, &recovered_cells, &recovered_proofs).map_err(Error::from)
}
Expand Down

0 comments on commit 85772eb

Please sign in to comment.