Current section
Files
Jump to
Current section
Files
lib/kzg_elixir.ex
defmodule KzgElixir do
@moduledoc """
Ethereum EIP-4844 KZG trusted setup verification NIF bindings.
This module provides NIF bindings to `c-kzg-4844` for verifying KZG proofs,
implementing the cryptographic primitives required for EIP-4844 (Proto-Danksharding).
"""
@on_load :load_nif
@type blob :: binary()
@type commitment :: binary()
@type proof :: binary()
@type field_element :: binary()
@doc false
def load_nif do
path = :code.priv_dir(:kzg_elixir)
:erlang.load_nif(Path.join(path, "kzg_nif"), 0)
end
@doc """
Loads the KZG trusted setup from a file.
## Arguments
* `file`: Path to the trusted setup configuration file from `c-kzg-4844`.
## Returns
* `:ok` on success.
* `{:error, reason}` on failure (e.g., file not found, bad format).
"""
@spec load_trusted_setup(String.t()) :: :ok | {:error, term()}
def load_trusted_setup(_file), do: :erlang.nif_error(:nif_not_loaded)
@doc """
Computes the KZG commitment for a given blob.
## Arguments
* `blob`: The data blob (4096 field elements, 131072 bytes).
## Returns
* `{:ok, commitment}`: The 48-byte KZG commitment.
* `{:error, reason}`: If computation fails.
"""
@spec blob_to_kzg_commitment(blob()) :: {:ok, commitment()} | {:error, term()}
def blob_to_kzg_commitment(_blob), do: :erlang.nif_error(:nif_not_loaded)
@doc """
Computes a KZG proof for a blob at a given evaluation point `z`.
## Arguments
* `blob`: The data blob.
* `z`: The evaluation point (32 bytes).
## Returns
* `{:ok, {proof, y}}`: Tuple containing the 48-byte proof and the evaluation result `y`.
* `{:error, reason}`: If computation fails.
"""
@spec compute_kzg_proof(blob(), field_element()) :: {:ok, {proof(), field_element()}} | {:error, term()}
def compute_kzg_proof(_blob, _z), do: :erlang.nif_error(:nif_not_loaded)
@doc """
Verifies a KZG proof.
## Arguments
* `commitment`: The 48-byte KZG commitment.
* `z`: The evaluation point (32 bytes).
* `y`: The evaluation result (32 bytes).
* `proof`: The 48-byte proof.
## Returns
* `{:ok, true}` if valid.
* `{:ok, false}` if invalid.
* `{:error, reason}` on error.
"""
@spec verify_kzg_proof(commitment(), field_element(), field_element(), proof()) :: {:ok, boolean()} | {:error, term()}
def verify_kzg_proof(_commitment, _z, _y, _proof), do: :erlang.nif_error(:nif_not_loaded)
@doc """
Verifies a KZG proof for a blob.
## Arguments
* `blob`: The data blob.
* `commitment`: The 48-byte KZG commitment.
* `proof`: The 48-byte proof.
## Returns
* `{:ok, true}` if valid.
* `{:ok, false}` if invalid.
* `{:error, reason}` on error.
"""
@spec verify_blob_kzg_proof(blob(), commitment(), proof()) :: {:ok, boolean()} | {:error, term()}
def verify_blob_kzg_proof(_blob, _commitment, _proof), do: :erlang.nif_error(:nif_not_loaded)
end