Skip to content

Commit 2f6114b

Browse files
authored
Merge pull request #21 from ethereumjs/add-docs
Added API docs for README
2 parents 74d8976 + ac711bb commit 2f6114b

File tree

1 file changed

+101
-1
lines changed

1 file changed

+101
-1
lines changed

README.md

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# KZG-WASM
22

3-
This module implements a JS wrapper around a WASM compilation of the [`c-kzg-4844`](https://github.com/ethereum/c-kzg-4844) C library built for use with EIP-4844.
3+
This module implements a JS wrapper around a WASM compilation of the [`c-kzg-4844`](https://github.com/ethereum/c-kzg-4844) C library built for use with EIP-4844. Starting with v1.0.0, the library also support cell-based operations in the context of EIP-7594 (PeerDAS).
44

55
This library is produced by building the original C code to WASM using the [`empscripten`](https://empscripten.org) toolchain in [this fork](https://github.com/ETHCF/c-kzg-4844) of `c-kzg-4844`.
66

@@ -26,3 +26,103 @@ const main = async () => {
2626

2727
main()
2828
```
29+
30+
## API Reference
31+
32+
The `loadKZG()` function returns an object that implements the KZG interface with the following methods:
33+
34+
### Core KZG Operations (EIP-4844)
35+
36+
#### `loadTrustedSetup(trustedSetup?, precompute?)`
37+
```ts
38+
loadTrustedSetup(trustedSetup: TrustedSetup = mainnetTrustedSetup, precompute: number = 8): number
39+
```
40+
Loads a trusted setup for KZG operations. Returns 0 on success, non-zero on failure.
41+
42+
#### `freeTrustedSetup()`
43+
```ts
44+
freeTrustedSetup(): void
45+
```
46+
Frees the loaded trusted setup from memory.
47+
48+
#### `blobToKZGCommitment(blob)`
49+
```ts
50+
blobToKZGCommitment(blob: string): string
51+
```
52+
Converts a blob of data into a KZG commitment. Takes a prefixed hex string containing 4096 big endian KZG field elements and returns a 48-byte KZG commitment as a prefixed hex string.
53+
54+
#### `computeBlobKZGProof(blob, commitment)`
55+
```ts
56+
computeBlobKZGProof(blob: string, commitment: string): string
57+
```
58+
Computes a KZG proof for a blob and its commitment. Returns a 48-byte KZG proof as a prefixed hex string.
59+
60+
#### `verifyBlobKZGProof(blob, commitment, proof)`
61+
```ts
62+
verifyBlobKZGProof(blob: string, commitment: string, proof: string): boolean
63+
```
64+
Verifies a KZG proof against a blob and its commitment. Returns `true` if the proof is valid, `false` otherwise.
65+
66+
#### `verifyBlobKZGProofBatch(blobs, commitments, proofs)`
67+
```ts
68+
verifyBlobKZGProofBatch(blobs: string[], commitments: string[], proofs: string[]): boolean
69+
```
70+
Verifies multiple KZG proofs in batch. All arrays must have the same length. Returns `true` if all proofs are valid, `false` otherwise.
71+
72+
#### `verifyKZGProof(commitment, z, y, proof)`
73+
```ts
74+
verifyKZGProof(commitment: string, z: string, y: string, proof: string): boolean
75+
```
76+
Verifies a KZG proof for specific points z and y against a commitment. Returns `true` if the proof is valid, `false` otherwise.
77+
78+
### Cell-based Operations (EIP-7594)
79+
80+
#### `computeCellsAndKZGProofs(blob)`
81+
```ts
82+
computeCellsAndKZGProofs(blob: string): KZGProofWithCells
83+
```
84+
Computes all 128 cells and their corresponding KZG proofs for a blob. Returns an object containing arrays of proofs and cells.
85+
86+
#### `recoverCellsFromKZGProofs(cellIndices, partialCells, numCells)`
87+
```ts
88+
recoverCellsFromKZGProofs(cellIndices: number[], partialCells: string[], numCells: number): KZGProofWithCells
89+
```
90+
Recovers all cells and proofs from partial cell data. Requires at least 50% of the total cells to be provided. Returns the complete set of proofs and cells.
91+
92+
#### `verifyCellKZGProof(commitment, cells, proofs)`
93+
```ts
94+
verifyCellKZGProof(commitment: string, cells: string[], proofs: string[]): boolean
95+
```
96+
Verifies KZG proofs for specific cells against a commitment. Returns `true` if all proofs are valid, `false` otherwise.
97+
98+
#### `verifyCellKZGProofBatch(commitments, cellIndices, cells, proofs, numCells)`
99+
```ts
100+
verifyCellKZGProofBatch(commitments: string[], cellIndices: number[], cells: string[], proofs: string[], numCells: number): boolean
101+
```
102+
Verifies multiple cell KZG proofs in batch. All arrays must have the same length and match `numCells`. Returns `true` if all proofs are valid, `false` otherwise.
103+
104+
### Compatibility Aliases
105+
106+
The library also provides aliases for compatibility with different naming conventions:
107+
108+
- `blobToKzgCommitment` - alias for `blobToKZGCommitment`
109+
- `computeBlobProof` - alias for `computeBlobKZGProof`
110+
111+
### Types
112+
113+
#### `TrustedSetup`
114+
```ts
115+
type TrustedSetup = {
116+
g1_monomial: string
117+
g1_lagrange: string
118+
g2_monomial: string
119+
}
120+
```
121+
122+
#### `KZGProofWithCells`
123+
```ts
124+
type KZGProofWithCells = {
125+
proofs: string[] // Array of 128 proofs, each 48 bytes
126+
cells: string[] // Array of 128 cells, each 2048 bytes
127+
}
128+
```

0 commit comments

Comments
 (0)