Skip to content

Commit

Permalink
Add pub methods for cell and export das items (#386)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobkaufmann authored Jan 23, 2024
1 parent 802ee97 commit 68ee8e2
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 12 deletions.
2 changes: 1 addition & 1 deletion bindings/rust/rust-toolchain
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[toolchain]
channel = "nightly"
channel = "stable"
42 changes: 34 additions & 8 deletions bindings/rust/src/bindings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -541,21 +541,47 @@ impl KZGCommitment {
}

impl Cell {
/// NOTE: Assumes `proof` is valid for `data` and `(row, col)`.
pub fn new(
data: [Bytes32; FIELD_ELEMENTS_PER_CELL],
proof: Bytes48,
row: u32,
col: u32,
) -> Self {
Self {
data,
proof: KZGProof { bytes: *proof },
row_index: row,
column_index: col,
}
}

pub fn data(&self) -> [Bytes32; FIELD_ELEMENTS_PER_CELL] {
self.data
}

pub fn proof(&self) -> &KZGProof {
&self.proof
}

pub fn compute_cells(
blob: &Blob,
row_index: u32,
kzg_settings: &KZGSettings,
) -> Result<Box<[Cell; CELLS_PER_BLOB]>, Error> {
let mut cells: Box<MaybeUninit<[Cell; CELLS_PER_BLOB]>> = Box::new_uninit();
let mut cells: Vec<Cell> = Vec::with_capacity(CELLS_PER_BLOB);
unsafe {
let res = compute_cells(
cells.as_mut_ptr() as *mut Cell,
blob,
row_index,
kzg_settings,
);
let res = compute_cells(cells.as_mut_ptr(), blob, row_index, kzg_settings);
if let C_KZG_RET::C_KZG_OK = res {
Ok(cells.assume_init())
cells.set_len(CELLS_PER_BLOB);

let boxed_slice = cells.into_boxed_slice();
let boxed_array: Box<[Cell; CELLS_PER_BLOB]> = boxed_slice
.try_into()
.map_err(|_err| "invalid len for blob cell array")
.unwrap();

Ok(boxed_array)
} else {
Err(Error::CError(res))
}
Expand Down
6 changes: 3 additions & 3 deletions bindings/rust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![feature(new_uninit)]
#![cfg_attr(not(feature = "std"), no_std)]

#[macro_use]
Expand All @@ -19,7 +18,8 @@ pub use bindings::{
// Expose the constants.
pub use bindings::{
BYTES_PER_BLOB, BYTES_PER_COMMITMENT, BYTES_PER_FIELD_ELEMENT, BYTES_PER_G1_POINT,
BYTES_PER_G2_POINT, BYTES_PER_PROOF, FIELD_ELEMENTS_PER_BLOB,
BYTES_PER_G2_POINT, BYTES_PER_PROOF, CELLS_PER_BLOB, DATA_POINTS_PER_BLOB,
FIELD_ELEMENTS_PER_BLOB, FIELD_ELEMENTS_PER_CELL,
};
// Expose the remaining relevant types.
pub use bindings::{Blob, Bytes32, Bytes48, Error};
pub use bindings::{Blob, Bytes32, Bytes48, Cell, Error};

0 comments on commit 68ee8e2

Please sign in to comment.