c-kzg-4844/bindings/node.js
Matthew Keil 51a669ff80
test(node-bindings): add argument count unit tests (#311)
* test(node-bindings): add argument count unit tests

* test(node-bindings): make arg length check more explicit

* refactor(node-bindings): make getValidTest to DRY code

* test(node-bindings): extra args give same result

* refactor(node-bindings): adjust unit test spacing

* refactor(node-bindings): move argument length checks to it block

* refactor(node-bindings): simplify arg slicing

* chore(node-bindings): lint/format code

* docs(node-bindings): add docstring to test helper functions

* test(node-bindings): fix bytesEqual and add docstrings to all helper functions

* refactor(node-bindings): change name to assertBytesEqual
2023-06-09 12:06:12 +03:00
..
lib Speed up loading trusted setup (#299) 2023-05-12 16:44:37 +03:00
src Format node bindings with clang-format (#268) 2023-03-30 11:20:25 -05:00
test test(node-bindings): add argument count unit tests (#311) 2023-06-09 12:06:12 +03:00
.clang-format Format node bindings with clang-format (#268) 2023-03-30 11:20:25 -05:00
.eslintrc.js Add eslint to node bindings (#269) 2023-03-31 17:46:54 +09:00
.gitignore .gitignore node deps (#265) 2023-03-30 08:25:24 -05:00
.prettierignore Reorganize/Clean-Up Node Bindings (#189) 2023-03-09 18:21:28 +02:00
.prettierrc.yml Add eslint to node bindings (#269) 2023-03-31 17:46:54 +09:00
CONTRIBUTING.md Multi-platform nodejs bindings (#242) 2023-03-28 11:01:07 -05:00
Dockerfile Multi-platform nodejs bindings (#242) 2023-03-28 11:01:07 -05:00
Makefile Fix some minor nits (#298) 2023-05-09 16:40:36 +03:00
README.md Update node README (#228) 2023-03-21 09:53:27 -05:00
binding.gyp Multi-platform nodejs bindings (#242) 2023-03-28 11:01:07 -05:00
jest.config.js Cleanup 2022-11-03 15:13:49 -07:00
package.json chore: Publish to npm version 2.1.0 for big endian release (#310) 2023-06-01 16:09:59 +03:00
tsconfig.build.json Reorganize/Clean-Up Node Bindings (#189) 2023-03-09 18:21:28 +02:00
tsconfig.json Reorganize/Clean-Up Node Bindings (#189) 2023-03-09 18:21:28 +02:00
yarn.lock Add eslint to node bindings (#269) 2023-03-31 17:46:54 +09:00

README.md

C-KZG-4844

This is a TypeScript library for EIP-4844 that implements the Polynomial Commitments API. The core functionality was originally a stripped-down copy of 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/eip4844/polynomial-commitments.md

Prerequisites

Installation requires compilation of C code. Target environment must have:

Installation

yarn add c-kzg
# or
npm i -S c-kzg

Usage

import {
  BYTES_PER_BLOB,
  Blob,
  Bytes48,
  blobToKzgCommitment,
  computeBlobKzgProof,
  verifyBlobKzgProofBatch,
} from "c-kzg";

const blobs = [] as Blob[];
const commitments = [] as Bytes48[];
const proofs = [] as Bytes48[];

for (let i = 0; i < BATCH_SIZE; i++) {
  blobs.push(Buffer.alloc(BYTES_PER_BLOB, "*"));
  commitments.push(blobToKzgCommitment(blobs[i]));
  proofs.push(computeBlobKzgProof(blobs[i], commitments[i]));
}

const isValid = verifyBlobKzgProofBatch(blobs, commitments, proofs);

API

loadTrustedSetup

/**
 * Sets up the c-kzg library. Pass in a properly formatted trusted setup file
 * to configure the library.  File must be in json format, see TrustedSetupJson
 * interface for more details, or as a properly formatted utf-8 encoded file.
 *
 * @remark This function must be run before any other functions in this
 *         library can be run.
 *
 * @param {string} filePath - The absolute path of the trusted setup
 */
loadTrustedSetup(filePath: string): void;

blobToKzgCommitment

/**
 * Convert a blob to a KZG commitment.
 *
 * @param {Blob} blob - The blob representing the polynomial to be committed to
 */
blobToKzgCommitment(blob: Blob): KZGCommitment;

computeKzgProof

/**
 * Compute KZG proof for polynomial in Lagrange form at position z.
 *
 * @param {Blob}    blob - The blob (polynomial) to generate a proof for
 * @param {Bytes32} zBytes - The generator z-value for the evaluation points
 *
 * @return {ProofResult} - Tuple containing the resulting proof and evaluation
 *                         of the polynomial at the evaluation point z
 */
computeKzgProof(blob: Blob, zBytes: Bytes32): ProofResult;

computeBlobKzgProof

/**
 * Given a blob, return the KZG proof that is used to verify it against the
 * commitment.
 *
 * @param {Blob}    blob - The blob (polynomial) to generate a proof for
 * @param {Bytes48} commitmentBytes - Commitment to verify
 */
computeBlobKzgProof(
  blob: Blob,
  commitmentBytes: Bytes48,
): KZGProof;

verifyKzgProof

/**
 * Verify a KZG poof claiming that `p(z) == y`.
 *
 * @param {Bytes48} commitmentBytes - The serialized commitment corresponding to
 *                                    polynomial p(x)
 * @param {Bytes32} zBytes - The serialized evaluation point
 * @param {Bytes32} yBytes - The serialized claimed evaluation result
 * @param {Bytes48} proofBytes - The serialized KZG proof
 */
verifyKzgProof(
  commitmentBytes: Bytes48,
  zBytes: Bytes32,
  yBytes: Bytes32,
  proofBytes: Bytes48,
): boolean;

verifyBlobKzgProof

/**
 * Given a blob and its proof, verify that it corresponds to the provided
 * commitment.
 *
 * @param {Blob}    blob - The serialized blob to verify
 * @param {Bytes48} commitmentBytes - The serialized commitment to verify
 * @param {Bytes48} proofBytes - The serialized KZG proof for verification
 */
verifyBlobKzgProof(
  blob: Blob,
  commitmentBytes: Bytes48,
  proofBytes: Bytes48,
): boolean;

verifyBlobKzgProofBatch

/**
 * Given an array of blobs and their proofs, verify that they corresponds to
 * their provided commitment.
 *
 * Note: blobs[0] relates to commitmentBytes[0] and proofBytes[0]
 *
 * @param {Blob}    blobs - An array of serialized blobs to verify
 * @param {Bytes48} commitmentBytes - An array of serialized commitments to
 *                                    verify
 * @param {Bytes48} proofBytes - An array of serialized KZG proofs for
 *                               verification
 */
verifyBlobKzgProofBatch(
  blobs: Blob[],
  commitmentsBytes: Bytes48[],
  proofsBytes: Bytes48[],
): boolean;