Skip to main content

Nethereum.EVM.Precompiles.Kzg

NuGet: Nethereum.EVM.Precompiles.Kzg | Source: src/Nethereum.EVM.Precompiles.Kzg/

Nethereum.EVM.Precompiles.Kzg

EIP-4844 KZG point-evaluation precompile for Nethereum.EVM.Core. Activated at the Cancun hardfork.

Overview​

EIP-4844 (proto-danksharding) introduced a single new precompile at address 0x0a: point_evaluation_precompile. It verifies a KZG opening proof against a given commitment / evaluation point / claimed value using the trusted KZG setup.

This package plugs the handler into a Nethereum.EVM.Core.HardforkConfig by extension method, delegating the cryptographic work to a pluggable IKzgOperations implementation (native C-KZG by default, custom for zkVM / alternative backends).

Installation​

dotnet add package Nethereum.EVM.Precompiles.Kzg

Dependencies​

  • Nethereum.EVM — HardforkConfig, PrecompileRegistry.
  • Ckzg.Bindings 1.0.2.828 — the native c-kzg-4844 bindings behind CkzgOperations.
  • The project ships CkzgOperations (wraps native C-KZG) as the default implementation of IKzgOperations.

Usage​

HardforkConfig.WithKzgBackend()​

Default (uses the embedded trusted setup + native C-KZG):

using Nethereum.EVM;
using Nethereum.EVM.Precompiles; // DefaultHardforkConfigs

HardforkConfig cancun = DefaultHardforkConfigs.Cancun
.WithKzgBackend();

HardforkConfig.WithKzgBackend(IKzgOperations)​

Inject a custom operations implementation (test double, alternative backend, trimmed build, …):

using Nethereum.EVM;                   // HardforkConfig, WithKzgBackend
using Nethereum.EVM.Precompiles; // DefaultHardforkConfigs
using Nethereum.EVM.Precompiles.Kzg; // CkzgOperations

CkzgOperations.InitializeFromEmbeddedSetup();
IKzgOperations kzgOps = new CkzgOperations(); // or your own
HardforkConfig cancun = DefaultHardforkConfigs.Cancun
.WithKzgBackend(kzgOps);

PrecompileRegistry.WithKzgBackend(...)​

Compose KZG on top of any base registry:

using Nethereum.EVM.Execution.Precompiles;
using Nethereum.EVM.Precompiles.Kzg; // PrecompileRegistry.WithKzgBackend

var cancunBase = PrecompileRegistries.CancunBase(/* backends... */);
var withKzg = cancunBase.WithKzgBackend(kzgOps);

Requirement Note​

WithKzgBackend throws InvalidOperationException when called against a HardforkConfig whose Precompiles registry is null — the KZG handler layers on top of the Cancun base registry. Obtain the HardforkConfig through MainnetHardforkRegistry.Build(backends).Get(HardforkName.Cancun) or DefaultHardforkConfigs.Cancun before calling the extension.

Blob Transaction Support​

Beyond the precompile, this package provides the KZG operations needed to build EIP-4844 blob transactions:

using Nethereum.EVM.Precompiles.Kzg;
using Nethereum.Model;

// Initialize KZG trusted setup (once per process)
CkzgOperations.InitializeFromEmbeddedSetup();
var kzg = new CkzgOperations();

// Full pipeline: data → blobs → KZG commitments → proofs → versioned hashes
var builder = new BlobSidecarBuilder(kzg);
var (sidecar, versionedHashes) = builder.BuildFromData(myData);

// Or use individual operations
byte[] commitment = kzg.BlobToKzgCommitment(blob); // 131072 bytes → 48 bytes
byte[] proof = kzg.ComputeBlobKzgProof(blob, commitment); // 48 bytes
byte[] versionedHash = kzg.ComputeVersionedHash(commitment); // 0x01 || SHA256[1:]
bool valid = kzg.VerifyBlobKzgProof(blob, commitment, proof);

See Also​