README.md
1 # C-KZG-4844 2 3 This is a TypeScript library for EIP-4844 that implements the [Polynomial 4 Commitments](https://github.com/ethereum/consensus-specs/blob/dev/specs/deneb/polynomial-commitments.md) 5 API. The core functionality was originally a stripped-down copy of 6 [C-KZG](https://github.com/benjaminion/c-kzg), but has been heavily modified 7 since then. This package wraps that native `c-kzg` C code in C/C++ NAPI 8 bindings for use in node.js applications. 9 10 Spec: <https://github.com/ethereum/consensus-specs/blob/dev/specs/deneb/polynomial-commitments.md> 11 12 ## Prerequisites 13 14 Installation requires compilation of C code. Target environment must have: 15 16 - A compiler like g++ or clang 17 - [`make`](https://www.gnu.org/software/make/manual/make.html) 18 - [`python3`](https://docs.python.org/3/) 19 20 ## Installation 21 22 ```sh 23 yarn add c-kzg 24 # or 25 npm i -S c-kzg 26 ``` 27 28 ## Usage 29 30 ```ts 31 import { 32 BYTES_PER_BLOB, 33 Blob, 34 Bytes48, 35 blobToKzgCommitment, 36 computeBlobKzgProof, 37 verifyBlobKzgProofBatch, 38 } from "c-kzg"; 39 40 const blobs = [] as Blob[]; 41 const commitments = [] as Bytes48[]; 42 const proofs = [] as Bytes48[]; 43 44 for (let i = 0; i < BATCH_SIZE; i++) { 45 blobs.push(Buffer.alloc(BYTES_PER_BLOB, "*")); 46 commitments.push(blobToKzgCommitment(blobs[i])); 47 proofs.push(computeBlobKzgProof(blobs[i], commitments[i])); 48 } 49 50 const isValid = verifyBlobKzgProofBatch(blobs, commitments, proofs); 51 ``` 52 53 ## API 54 55 ### `loadTrustedSetup` 56 57 ```ts 58 /** 59 * Initialize the library with a trusted setup file. 60 * 61 * Can pass either a .txt or a .json file with setup configuration. Converts 62 * JSON formatted trusted setup into the native format that the base library 63 * requires. The created file will be in the same as the origin file but with a 64 * ".txt" extension. 65 * 66 * Uses user provided location first. If one is not provided then defaults to 67 * the official Ethereum mainnet setup from the KZG ceremony. Should only be 68 * used for cases where the Ethereum official mainnet KZG setup is acceptable. 69 * 70 * @param {string | undefined} filePath - .txt/.json file with setup configuration 71 * @default - If no string is passed the default trusted setup from the Ethereum KZG ceremony is used 72 * 73 * @throws {TypeError} - Non-String input 74 * @throws {Error} - For all other errors. See error message for more info 75 */ 76 loadTrustedSetup(filePath: string): void; 77 ``` 78 79 ### `blobToKzgCommitment` 80 81 ```ts 82 /** 83 * Convert a blob to a KZG commitment. 84 * 85 * @param {Blob} blob - The blob representing the polynomial to be committed to 86 */ 87 blobToKzgCommitment(blob: Blob): KZGCommitment; 88 ``` 89 90 ### `computeKzgProof` 91 92 ```ts 93 /** 94 * Compute KZG proof for polynomial in Lagrange form at position z. 95 * 96 * @param {Blob} blob - The blob (polynomial) to generate a proof for 97 * @param {Bytes32} zBytes - The generator z-value for the evaluation points 98 * 99 * @return {ProofResult} - Tuple containing the resulting proof and evaluation 100 * of the polynomial at the evaluation point z 101 */ 102 computeKzgProof(blob: Blob, zBytes: Bytes32): ProofResult; 103 ``` 104 105 ### `computeBlobKzgProof` 106 107 ```ts 108 /** 109 * Given a blob, return the KZG proof that is used to verify it against the 110 * commitment. 111 * 112 * @param {Blob} blob - The blob (polynomial) to generate a proof for 113 * @param {Bytes48} commitmentBytes - Commitment to verify 114 */ 115 computeBlobKzgProof( 116 blob: Blob, 117 commitmentBytes: Bytes48, 118 ): KZGProof; 119 ``` 120 121 ### `verifyKzgProof` 122 123 ```ts 124 /** 125 * Verify a KZG poof claiming that `p(z) == y`. 126 * 127 * @param {Bytes48} commitmentBytes - The serialized commitment corresponding to 128 * polynomial p(x) 129 * @param {Bytes32} zBytes - The serialized evaluation point 130 * @param {Bytes32} yBytes - The serialized claimed evaluation result 131 * @param {Bytes48} proofBytes - The serialized KZG proof 132 */ 133 verifyKzgProof( 134 commitmentBytes: Bytes48, 135 zBytes: Bytes32, 136 yBytes: Bytes32, 137 proofBytes: Bytes48, 138 ): boolean; 139 ``` 140 141 ### `verifyBlobKzgProof` 142 143 ```ts 144 /** 145 * Given a blob and its proof, verify that it corresponds to the provided 146 * commitment. 147 * 148 * @param {Blob} blob - The serialized blob to verify 149 * @param {Bytes48} commitmentBytes - The serialized commitment to verify 150 * @param {Bytes48} proofBytes - The serialized KZG proof for verification 151 */ 152 verifyBlobKzgProof( 153 blob: Blob, 154 commitmentBytes: Bytes48, 155 proofBytes: Bytes48, 156 ): boolean; 157 ``` 158 159 ### `verifyBlobKzgProofBatch` 160 161 ```ts 162 /** 163 * Given an array of blobs and their proofs, verify that they correspond to 164 * their provided commitment. 165 * 166 * Note: blobs[0] relates to commitmentBytes[0] and proofBytes[0] 167 * 168 * @param {Blob} blobs - An array of serialized blobs to verify 169 * @param {Bytes48} commitmentBytes - An array of serialized commitments to 170 * verify 171 * @param {Bytes48} proofBytes - An array of serialized KZG proofs for 172 * verification 173 */ 174 verifyBlobKzgProofBatch( 175 blobs: Blob[], 176 commitmentsBytes: Bytes48[], 177 proofsBytes: Bytes48[], 178 ): boolean; 179 ```