Skip to content

Conversation

@BlazeWasHere
Copy link

@BlazeWasHere BlazeWasHere commented Dec 10, 2025

Motivation

Removes the overhead from elixir -> rust (via rustler) -> c and now does elixir -> c via these elixir bindings.

Description

Removes usage of the c-kzg Rustler NIF and replaces it with direct c-kzg bindings instead.

Getting speedups of ~40x with verify_blob_kzg_proof with this change

Note: This NIF loads the trusted setup (settings) once, so essentially closes #848

# Before
~> git fetch origin 4111b5d
~> mix run bench/kzg.exs
Operating System: macOS
CPU Information: Apple M2
Number of Available Cores: 8
Available memory: 16 GB
Elixir 1.18.4
Erlang 28.0
JIT enabled: true

Benchmark suite executing with the following configuration:
warmup: 2 s
time: 5 s
memory time: 0 ns
reduction time: 0 ns
parallel: 1
inputs: none specified
Estimated total run time: 35 s

Benchmarking blob_to_kzg_commitment ...
Benchmarking compute_blob_kzg_proof ...
Benchmarking compute_kzg_proof (z) ...
Benchmarking verify_blob_kzg_proof ...
Benchmarking verify_blob_kzg_proof_batch (4) ...
Calculating statistics...
Formatting results...

Name                                      ips        average  deviation         median         99th %
verify_blob_kzg_proof                   16.29       61.38 ms     ±0.89%       61.41 ms       63.65 ms
verify_blob_kzg_proof_batch (4)         15.22       65.69 ms     ±9.04%       64.71 ms      107.64 ms
compute_kzg_proof (z)                    9.86      101.42 ms     ±7.65%       99.41 ms      145.88 ms
compute_blob_kzg_proof                   9.10      109.92 ms     ±9.62%      106.37 ms      165.99 ms
blob_to_kzg_commitment                   8.96      111.61 ms     ±5.78%      111.75 ms      141.87 ms

Comparison: 
verify_blob_kzg_proof                   16.29
verify_blob_kzg_proof_batch (4)         15.22 - 1.07x slower +4.31 ms
compute_kzg_proof (z)                    9.86 - 1.65x slower +40.04 ms
compute_blob_kzg_proof                   9.10 - 1.79x slower +48.54 ms
blob_to_kzg_commitment                   8.96 - 1.82x slower +50.22 ms

# Now apply changes
~> git fetch origin 815a2c
~> mix deps.get
~> mix run bench/kzg.exs --mode db
[...]
Name                                      ips        average  deviation         median         99th %
verify_blob_kzg_proof                  634.65        1.58 ms    ±12.26%        1.56 ms        1.72 ms
verify_blob_kzg_proof_batch (4)        216.31        4.62 ms     ±1.71%        4.60 ms        5.02 ms
blob_to_kzg_commitment                  25.59       39.08 ms     ±5.46%       38.47 ms       55.22 ms
compute_kzg_proof (z)                   25.54       39.16 ms     ±2.14%       39.03 ms       45.09 ms
compute_blob_kzg_proof                  25.37       39.41 ms     ±2.47%       39.20 ms       44.52 ms

Comparison: 
verify_blob_kzg_proof                  634.65
verify_blob_kzg_proof_batch (4)        216.31 - 2.93x slower +3.05 ms
blob_to_kzg_commitment                  25.59 - 24.80x slower +37.50 ms
compute_kzg_proof (z)                   25.54 - 24.85x slower +37.58 ms
compute_blob_kzg_proof                  25.37 - 25.01x slower +37.84 ms

# Avoid compiling Rustler NIFs when `RUSTLER_SKIP_COMPILE` is set
if System.get_env("RUSTLER_SKIP_COMPILE") do
config :lambda_ethereum_consensus, Bls, skip_compilation?: true
config :lambda_ethereum_consensus, Kzg, skip_compilation?: true
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if there should be another flag to skip compiling the nif?


@type commitment :: <<_::384>>
@type proof :: <<_::768>>
@type proof :: <<_::384>>
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am pretty sure proof size is 384 and not 768, but feel free to revert

# TODO: only copy artifacts
# Copy precompiled rust crates. Rustler stores targets under _build
COPY --from=bls_nif_builder /bls_nif/libbls_nif.so /consensus/priv/native/libbls_nif.so
COPY --from=kzg_nif_builder /kzg_nif/libkzg_nif.so /consensus/priv/native/libkzg_nif.so
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure i should add precompiling the nif here instead?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

Load KZG's trusted setup only once

1 participant