# WASM KZG

Ox provides an opt-in KZG implementation backed by
[`c-kzg-4844`](https://github.com/ethereum/c-kzg-4844). It implements the
existing [`Kzg.Kzg`](/api/Kzg) interface without changing the global
[`Engine`](/api/Engine) registry.

## Create an instance

Supply the Ethereum trusted setup when creating an instance:

```ts twoslash
import { Blobs } from 'ox'
import { Setups } from 'ox/trusted-setups'
import { Kzg } from 'ox/wasm'

const kzg = await Kzg.create({ trustedSetup: Setups.mainnet })

try {
  const blobs = Blobs.from('0xdeadbeef')
  const commitments = Blobs.toCommitments(blobs, { kzg })
} finally {
  kzg.dispose()
}
```

`Setups.mainnet` contains Ox's packed copy of the canonical Ethereum setup. You
can reuse the same value across factory calls because each call copies the
setup before asynchronous initialization.

Ox sources the setup from an explicit tagged `c-kzg-4844` release and records
both source and generated-data SHA-256 values. Release updates require an
explicit review and regeneration; they never follow a moving branch.

The factory also accepts custom setups with the standard `g1_lagrange`,
`g1_monomial`, and `g2_monomial` fields. Each field may contain hex points or
one packed byte array.

The instance implements all EIP-4844 and EIP-7594 operations in `Kzg.Kzg`:

* `blobToKzgCommitment`
* `computeCells`
* `computeCellsAndKzgProofs`
* `recoverCellsAndKzgProofs`
* `verifyCellKzgProofBatch`

Import `ox/wasm/Kzg` directly when only KZG is needed. The dedicated artifact
does not load through `ox`, another WASM module, or an `Engine` slot.

Import `ox/trusted-setups/Setups` directly when only setup data is needed. The
packed setup is not loaded by `ox`, `ox/wasm`, or `ox/trusted-setups/Paths`.

## Ownership and cleanup

Each `create` call owns separate WASM memory and trusted-setup state. Concurrent
factory calls share only the compiled module, not mutable memory or setup
state.

Operations are synchronous and run to completion. Create one instance inside
each JavaScript worker that performs KZG operations.

`dispose` is idempotent. It frees c-kzg setup allocations, clears the module
reference, and makes later operations throw `Kzg.DisposedError`.

The JavaScript runtime controls when the underlying `WebAssembly.Memory` object
is collected. Drop other references to the instance so the runtime can reclaim
that memory.

Inputs are copied into linear memory. Scratch inputs and outputs are zeroed and
freed after each operation, including failed operations.

## Memory and precomputation

The KZG artifact is 166.1 KiB raw, 221.4 KiB as embedded base64, and 51.4 KiB
gzip. The trusted setup contains 390.1 KiB of packed points, uses 520.1 KiB of
embedded base64, and produces a 395.6 KiB gzip standalone bundle.

Each KZG instance starts with 8 MiB and may grow to 128 MiB.

`precompute` selects c-kzg's fixed-base MSM window. The default `0` uses the
least memory. Larger windows trade memory for proof-generation performance.

| `precompute` | Linear memory after initialization |
| ------------ | ---------------------------------- |
| 0            | 8 MiB                              |
| 1            | 8 MiB                              |
| 2            | 8 MiB                              |
| 3            | 8 MiB                              |
| 4            | 10.5 MiB                           |
| 5            | 16.5 MiB                           |
| 6            | 28.5 MiB                           |
| 7            | 52.5 MiB                           |
| 8            | 100.5 MiB                          |

These values measure WASM linear memory immediately after trusted-setup
initialization. Operation inputs and outputs may grow memory further, within the
128 MiB limit.

Use the smallest window that meets the target workload. Run
`pnpm bench src/wasm/Kzg.bench.ts --project core` on the deployment hardware to
compare initialization and every supported operation.
