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

feat: add pub methods for cell and export das items #386

Merged
merged 1 commit into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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};