Skip to content

feat: add Serialize(Span<byte>) to point types - #15

Merged
rubo merged 1 commit into
NethermindEth:mainfrom
svlachakis:perf/blst-serialize-span
Nov 15, 2026
Merged

rubo merged 1 commit into
NethermindEth:mainfrom
svlachakis:perf/blst-serialize-span

Conversation

@svlachakis

Copy link
Copy Markdown
Contributor

Changes

  • Add Serialize(Span<byte> output) to P1, P1Affine, P2 and P2Affine. It writes the uncompressed encoding (96 bytes for G1, 192 bytes for G2) into a caller buffer.
  • A buffer shorter than the encoding throws ArgumentOutOfRangeException. A longer buffer is accepted, and only the first 96 or 192 bytes are written.
  • The change is made in PointGroup.template, and Bls.G1.cs and Bls.G2.cs are regenerated.
  • The change is additive. byte[] Serialize() does not change.

Why

Serialize() allocates a new byte[] on every call. Callers that copy the bytes into another buffer (for example, the EIP-2537 precompile output encoding in Nethermind) cannot avoid this allocation now. With the new overload they can serialize into a stack buffer.

Measurements

BenchmarkDotNet 0.15.8, G1Benchmarks / G2Benchmarks, Serialize (existing array API) and SerializeToSpan (new overload, stackalloc buffer), same build, 6 runs per runner on GitHub Actions.

Runner Method Mean (range of 6 runs) Allocated
ubuntu-24.04, Intel Xeon Platinum 8573C, .NET 10.0.15 x64 G1 Serialize 3.448 - 3.454 us 120 B
G1 SerializeToSpan 3.427 - 3.433 us -
G2 Serialize 4.022 - 4.039 us 216 B
G2 SerializeToSpan 3.998 - 4.006 us -
ubuntu-24.04-arm, Neoverse-N2, .NET 10.0.15 arm64 G1 Serialize 3.158 - 3.161 us 120 B
G1 SerializeToSpan 3.142 - 3.143 us -
G2 Serialize 3.702 - 3.707 us 216 B
G2 SerializeToSpan 3.681 - 3.682 us -

The allocation goes to zero in every run. The time drops by 0.5 - 0.7% in every run, which is more than the BDN error. The most time is the native Jacobian-to-affine conversion, so the main gain is the removed allocation.

Tests

  • SerializeToSpanMatchesSerialize: for all four types, the span output equals Serialize(). It also covers the point at infinity, an exact-size buffer, and a longer buffer (the extra byte stays zero).
  • SerializeToSpanRejectsShortBuffer: 95-byte and 191-byte buffers throw for all four types.
  • The new tests do not compile on main because the overload does not exist there.
  • All 60 tests pass locally (osx-arm64).

Follow-up

After a package release, Nethermind can use the overload in Eip2537.EncodeRaw for G1 and G2 (serialize into stackalloc and copy into the padded output). That change is not part of this PR.

Writes the uncompressed encoding into a caller buffer, so callers that copy the bytes elsewhere avoid the byte[] allocation.
@rubo
rubo merged commit ba03a4e into NethermindEth:main Nov 15, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants