/ bindings / node.js / lib / kzg.d.ts
kzg.d.ts
  1  /**
  2   * The public interface of this module exposes the functions as specified by
  3   * https://github.com/ethereum/consensus-specs/blob/dev/specs/eip4844/polynomial-commitments.md#kzg
  4   */
  5  export type Bytes32 = Uint8Array; // 32 bytes
  6  export type Bytes48 = Uint8Array; // 48 bytes
  7  export type KZGProof = Uint8Array; // 48 bytes
  8  export type KZGCommitment = Uint8Array; // 48 bytes
  9  export type Blob = Uint8Array; // 4096 * 32 bytes
 10  export type ProofResult = [KZGProof, Bytes32];
 11  export interface TrustedSetupJson {
 12    setup_G1: string[];
 13    setup_G2: string[];
 14    setup_G1_lagrange: string[];
 15    roots_of_unity: string[];
 16  }
 17  
 18  export const BYTES_PER_BLOB: number;
 19  export const BYTES_PER_COMMITMENT: number;
 20  export const BYTES_PER_FIELD_ELEMENT: number;
 21  export const BYTES_PER_PROOF: number;
 22  export const FIELD_ELEMENTS_PER_BLOB: number;
 23  
 24  /**
 25   * Initialize the library with a trusted setup file.
 26   *
 27   * Can pass either a .txt or a .json file with setup configuration. Converts
 28   * JSON formatted trusted setup into the native format that the base library
 29   * requires. The created file will be in the same as the origin file but with a
 30   * ".txt" extension.
 31   *
 32   * Uses user provided location first. If one is not provided then defaults to
 33   * the official Ethereum mainnet setup from the KZG ceremony. Should only be
 34   * used for cases where the Ethereum official mainnet KZG setup is acceptable.
 35   *
 36   * @param {string | undefined} filePath
 37   * @default - If no string is passed the default trusted setup from the Ethereum KZG ceremony is used
 38   *
 39   * @throws {TypeError} - Non-String input
 40   * @throws {Error} - For all other errors. See error message for more info
 41   */
 42  export function loadTrustedSetup(filePath?: string): void;
 43  
 44  /**
 45   * Convert a blob to a KZG commitment.
 46   *
 47   * @param {Blob} blob - The blob representing the polynomial to be committed to
 48   *
 49   * @return {KZGCommitment} - The resulting commitment
 50   *
 51   * @throws {TypeError} - For invalid arguments or failure of the native library
 52   */
 53  export function blobToKzgCommitment(blob: Blob): KZGCommitment;
 54  
 55  /**
 56   * Compute KZG proof for polynomial in Lagrange form at position z.
 57   *
 58   * @param {Blob}    blob - The blob (polynomial) to generate a proof for
 59   * @param {Bytes32} zBytes - The generator z-value for the evaluation points
 60   *
 61   * @return {ProofResult} - Tuple containing the resulting proof and evaluation
 62   *                         of the polynomial at the evaluation point z
 63   *
 64   * @throws {TypeError} - For invalid arguments or failure of the native library
 65   */
 66  export function computeKzgProof(blob: Blob, zBytes: Bytes32): ProofResult;
 67  
 68  /**
 69   * Given a blob, return the KZG proof that is used to verify it against the
 70   * commitment.
 71   *
 72   * @param {Blob}    blob - The blob (polynomial) to generate a proof for
 73   * @param {Bytes48} commitmentBytes - Commitment to verify
 74   *
 75   * @return {KZGProof} - The resulting proof
 76   *
 77   * @throws {TypeError} - For invalid arguments or failure of the native library
 78   */
 79  export function computeBlobKzgProof(blob: Blob, commitmentBytes: Bytes48): KZGProof;
 80  
 81  /**
 82   * Verify a KZG poof claiming that `p(z) == y`.
 83   *
 84   * @param {Bytes48} commitmentBytes - The serialized commitment corresponding to polynomial p(x)
 85   * @param {Bytes32} zBytes - The serialized evaluation point
 86   * @param {Bytes32} yBytes - The serialized claimed evaluation result
 87   * @param {Bytes48} proofBytes - The serialized KZG proof
 88   *
 89   * @return {boolean} - true/false depending on proof validity
 90   *
 91   * @throws {TypeError} - For invalid arguments or failure of the native library
 92   */
 93  export function verifyKzgProof(
 94    commitmentBytes: Bytes48,
 95    zBytes: Bytes32,
 96    yBytes: Bytes32,
 97    proofBytes: Bytes48
 98  ): boolean;
 99  
100  /**
101   * Given a blob and its proof, verify that it corresponds to the provided
102   * commitment.
103   *
104   * @param {Blob}    blob - The serialized blob to verify
105   * @param {Bytes48} commitmentBytes - The serialized commitment to verify
106   * @param {Bytes48} proofBytes - The serialized KZG proof for verification
107   *
108   * @return {boolean} - true/false depending on proof validity
109   *
110   * @throws {TypeError} - For invalid arguments or failure of the native library
111   */
112  export function verifyBlobKzgProof(blob: Blob, commitmentBytes: Bytes48, proofBytes: Bytes48): boolean;
113  
114  /**
115   * Given an array of blobs and their proofs, verify that they correspond to their
116   * provided commitment.
117   *
118   * Note: blobs[0] relates to commitmentBytes[0] and proofBytes[0]
119   *
120   * @param {Blob}    blobs - An array of serialized blobs to verify
121   * @param {Bytes48} commitmentsBytes - An array of serialized commitments to verify
122   * @param {Bytes48} proofsBytes - An array of serialized KZG proofs for verification
123   *
124   * @return {boolean} - true/false depending on batch validity
125   *
126   * @throws {TypeError} - For invalid arguments or failure of the native library
127   */
128  export function verifyBlobKzgProofBatch(blobs: Blob[], commitmentsBytes: Bytes48[], proofsBytes: Bytes48[]): boolean;