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: update node bindings #421

Merged
merged 5 commits into from
May 6, 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
110 changes: 109 additions & 1 deletion bindings/node.js/README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
# C-KZG-4844

**Note: This is an alpha release with peerDas functionality. For the current stable release use v3**

This is a TypeScript library for EIP-4844 that implements the [Polynomial
Commitments](https://github.com/ethereum/consensus-specs/blob/dev/specs/deneb/polynomial-commitments.md)
API. The core functionality was originally a stripped-down copy of
[C-KZG](https://github.com/benjaminion/c-kzg), but has been heavily modified
since then. This package wraps that native `c-kzg` C code in C/C++ NAPI
bindings for use in node.js applications.

Spec: <https://github.com/ethereum/consensus-specs/blob/dev/specs/deneb/polynomial-commitments.md>
Important Links:
[EIP-4844 - Polynomial Commitments](https://github.com/ethereum/consensus-specs/blob/dev/specs/deneb/polynomial-commitments.md)
[EIP-7594 - Polynomial Commitments Sampling](https://github.com/ethereum/consensus-specs/blob/dev/specs/_features/eip7594/polynomial-commitments-sampling.md)

## Prerequisites

Expand Down Expand Up @@ -177,3 +181,107 @@ verifyBlobKzgProofBatch(
proofsBytes: Bytes48[],
): boolean;
```

### `computeCells`

```ts
/**
* Get the cells for a given blob.
*
* @param {Blob} blob - The blob to get cells for
*
* @return {Cell[]} - An array of cells
*
* @throws {Error} - Failure to allocate or compute cells
*/
export function computeCells(blob: Blob): Cell[];
```

### `computeCellsAndKzgProofs`

```ts
/**
* Get the cells and proofs for a given blob.
*
* @param {Blob} blob - the blob to get cells/proofs for
*
* @return {[Cell[], KZGProof[]]} - A tuple of cells and proofs
*
* @throws {Error} - Failure to allocate or compute cells and proofs
*/
export function computeCellsAndKzgProofs(blob: Blob): [Cell[], KZGProof[]];
```

### `cellsToBlob`

```ts
/**
* Convert an array of cells to a blob.
*
* @param {Cell[]} cells - The cells to convert to a blob
*
* @return {Blob} - The blob for the given cells
*
* @throws {Error} - Invalid input, failure to allocate, or invalid conversion
*/
export function cellsToBlob(cells: Cell[]): Blob;
```

### `recoverAllCells`

```ts
/**
* Given at least 50% of cells, reconstruct the missing ones.
*
* @param {number[]} cellIds - The identifiers for the cells you have
* @param {Cell[]} cells - The cells you have
*
* @return {Cell[]} - All cells for that blob
*
* @throws {Error} - Invalid input, failure to allocate or error recovering cells
*/
export function recoverAllCells(cellIds: number[], cells: Cell[]): Cell[];
```

### `verifyCellKzgProof`

```ts
/**
* Verify that a cell's proof is valid.
*
* @param {Bytes48} commitmentBytes - Commitment bytes
* @param {number} cellId - The cell identifier
* @param {Cell} cell - The cell to verify
* @param {Bytes48} proofBytes - The proof for the cell
*
* @return {boolean} - True if the cell is valid with respect to this commitment
*
* @throws {Error} - Errors validating cell's proof
*/
export function verifyCellKzgProof(commitmentBytes: Bytes48, cellId: number, cell: Cell, proofBytes: Bytes48): boolean;
```

### `verifyCellKzgProofBatch`

```ts
/**
* Verify that multiple cells' proofs are valid.
*
* @param {Bytes48[]} commitmentsBytes - The commitments for all blobs
* @param {number[]} rowIndices - The row index for each cell
* @param {number[]} columnIndices - The column index for each cell
* @param {Cell[]} cells - The cells to verify
* @param {Bytes48[]} proofsBytes - The proof for each cell
*
* @return {boolean} - True if the cells are valid with respect to the given commitments
*
* @throws {Error} - Invalid input, failure to allocate memory, or errors verifying batch
*/
export function verifyCellKzgProofBatch(
commitmentsBytes: Bytes48[],
rowIndices: number[],
columnIndices: number[],
cells: Cell[],
proofsBytes: Bytes48[]
): boolean;
```
58 changes: 38 additions & 20 deletions bindings/node.js/lib/kzg.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,56 +136,74 @@ export function verifyBlobKzgProofBatch(blobs: Blob[], commitmentsBytes: Bytes48
/**
* Get the cells for a given blob.
*
* @param blob the blob to get cells for
* @return an array of cells
* @param {Blob} blob - The blob to get cells for
*
* @return {Cell[]} - An array of cells
*
* @throws {Error} - Failure to allocate or compute cells
*/
export function computeCells(blob: Blob): Cell[];

/**
* Get the cells and proofs for a given blob.
*
* @param blob the blob to get cells/proofs for
* @return a tuple of cells and proofs
* @param {Blob} blob - the blob to get cells/proofs for
*
* @return {[Cell[], KZGProof[]]} - A tuple of cells and proofs
*
* @throws {Error} - Failure to allocate or compute cells and proofs
*/
export function computeCellsAndKzgProofs(blob: Blob): [Cell[], KZGProof[]];

/**
* Convert an array of cells to a blob.
*
* @param cells the cells to convert to a blob
* @return the blob for the given cells
* @param {Cell[]} cells - The cells to convert to a blob
*
* @return {Blob} - The blob for the given cells
*
* @throws {Error} - Invalid input, failure to allocate, or invalid conversion
*/
export function cellsToBlob(cells: Cell[]): Blob;

/**
* Given at least 50% of cells, reconstruct the missing ones.
*
* @param cellIds the identifiers for the cells you have
* @param cells the cells you have
* @return all cells for that blob
* @param {number[]} cellIds - The identifiers for the cells you have
* @param {Cell[]} cells - The cells you have
*
* @return {Cell[]} - All cells for that blob
*
* @throws {Error} - Invalid input, failure to allocate or error recovering cells
*/
export function recoverAllCells(cellIds: number[], cells: Cell[]): Cell[];

/**
* Verify that a cell's proof is valid.
*
* @param commitmentBytes commitment bytes
* @param cellId the cell identifier
* @param cell the cell to verify
* @param proofBytes the proof for the cell
* @return true if the cell is valid with respect to this commitment
* @param {Bytes48} commitmentBytes - Commitment bytes
* @param {number} cellId - The cell identifier
* @param {Cell} cell - The cell to verify
* @param {Bytes48} proofBytes - The proof for the cell
*
* @return {boolean} - True if the cell is valid with respect to this commitment
*
* @throws {Error} - Errors validating cell's proof
*/
export function verifyCellKzgProof(commitmentBytes: Bytes48, cellId: number, cell: Cell, proofBytes: Bytes48): boolean;

/**
* Verify that multiple cells' proofs are valid.
*
* @param commitmentsBytes the commitments for all blobs
* @param rowIndices the row index for each cell
* @param columnIndices the column index for each cell
* @param cells the cells to verify
* @param proofsBytes the proof for each cell
* @return true if the cells are valid with respect to the given commitments
* @param {Bytes48[]} commitmentsBytes - The commitments for all blobs
* @param {number[]} rowIndices - The row index for each cell
* @param {number[]} columnIndices - The column index for each cell
* @param {Cell[]} cells - The cells to verify
* @param {Bytes48[]} proofsBytes - The proof for each cell
*
* @return {boolean} - True if the cells are valid with respect to the given commitments
*
* @throws {Error} - Invalid input, failure to allocate memory, or errors verifying batch
*/
export function verifyCellKzgProofBatch(
commitmentsBytes: Bytes48[],
Expand Down
2 changes: 1 addition & 1 deletion bindings/node.js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "c-kzg",
"version": "3.0.0",
"version": "4.0.0-alpha.0",
"description": "NodeJS bindings for C-KZG",
"contributors": [
"Dan Coffman <dgcoffman@gmail.com>",
Expand Down
65 changes: 65 additions & 0 deletions bindings/node.js/src/kzg.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,15 @@ Napi::Value VerifyBlobKzgProofBatch(const Napi::CallbackInfo &info) {
return result;
}

/**
* Get the cells for a given blob.
*
* @param[in] {Blob} blob - The blob to get cells for
*
* @return {Cell[]} - An array of cells
*
* @throws {Error} - Failure to allocate or compute cells
*/
Napi::Value ComputeCells(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
Napi::Value result = env.Null();
Expand Down Expand Up @@ -593,6 +602,15 @@ Napi::Value ComputeCells(const Napi::CallbackInfo &info) {
return result;
}

/**
* Get the cells and proofs for a given blob.
*
* @param[in] {Blob} blob - the blob to get cells/proofs for
*
* @return {[Cell[], KZGProof[]]} - A tuple of cells and proofs
*
* @throws {Error} - Failure to allocate or compute cells and proofs
*/
Napi::Value ComputeCellsAndKzgProofs(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
Napi::Value result = env.Null();
Expand Down Expand Up @@ -664,6 +682,15 @@ Napi::Value ComputeCellsAndKzgProofs(const Napi::CallbackInfo &info) {
return result;
}

/**
* Convert an array of cells to a blob.
*
* @param[in] {Cell[]} cells - The cells to convert to a blob
*
* @return {Blob} - The blob for the given cells
*
* @throws {Error} - Invalid input, failure to allocate, or invalid conversion
*/
Napi::Value CellsToBlob(const Napi::CallbackInfo &info) {
C_KZG_RET ret;
Cell *cells = NULL;
Expand Down Expand Up @@ -718,6 +745,17 @@ Napi::Value CellsToBlob(const Napi::CallbackInfo &info) {
return result;
}

/**
* Given at least 50% of cells, reconstruct the missing ones.
*
* @param[in] {number[]} cellIds - The identifiers for the cells you have
* @param[in] {Cell[]} cells - The cells you have
*
* @return {Cell[]} - All cells for that blob
*
* @throws {Error} - Invalid input, failure to allocate or error recovering
* cells
*/
Napi::Value RecoverAllCells(const Napi::CallbackInfo &info) {
C_KZG_RET ret;
uint64_t *cell_ids = NULL;
Expand Down Expand Up @@ -811,6 +849,18 @@ Napi::Value RecoverAllCells(const Napi::CallbackInfo &info) {
return result;
}

/**
* Verify that a cell's proof is valid.
*
* @param[in] {Bytes48} commitmentBytes - Commitment bytes
* @param[in] {number} cellId - The cell identifier
* @param[in] {Cell} cell - The cell to verify
* @param[in] {Bytes48} proofBytes - The proof for the cell
*
* @return {boolean} - True if the cell is valid with respect to this commitment
*
* @throws {Error} - Errors validating cell's proof
*/
Napi::Value VerifyCellKzgProof(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
Bytes48 *commitment_bytes = get_bytes48(env, info[0], "commitmentBytes");
Expand Down Expand Up @@ -846,6 +896,21 @@ Napi::Value VerifyCellKzgProof(const Napi::CallbackInfo &info) {
return Napi::Boolean::New(env, out);
}

/**
* Verify that multiple cells' proofs are valid.
*
* @param[in] {Bytes48[]} commitmentsBytes - The commitments for all blobs
* @param[in] {number[]} rowIndices - The row index for each cell
* @param[in] {number[]} columnIndices - The column index for each cell
* @param[in] {Cell[]} cells - The cells to verify
* @param[in] {Bytes48[]} proofsBytes - The proof for each cell
*
* @return {boolean} - True if the cells are valid with respect to the given
* commitments
*
* @throws {Error} - Invalid input, failure to allocate memory, or errors
* verifying batch
*/
Napi::Value VerifyCellKzgProofBatch(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
Napi::Value result = env.Null();
Expand Down