Repository navigation
feat: add Serialize(Span<byte>) to point types - #15
Merged
Merged
Conversation
Writes the uncompressed encoding into a caller buffer, so callers that copy the bytes elsewhere avoid the byte[] allocation.
LukaszRozmej
approved these changes
Nov 14, 2026
rubo
approved these changes
Nov 15, 2026
rubo
approved these changes
Nov 15, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Changes
Serialize(Span<byte> output)toP1,P1Affine,P2andP2Affine. It writes the uncompressed encoding (96 bytes for G1, 192 bytes for G2) into a caller buffer.ArgumentOutOfRangeException. A longer buffer is accepted, and only the first 96 or 192 bytes are written.PointGroup.template, andBls.G1.csandBls.G2.csare regenerated.byte[] Serialize()does not change.Why
Serialize()allocates a newbyte[]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) andSerializeToSpan(new overload,stackallocbuffer), same build, 6 runs per runner on GitHub Actions.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 equalsSerialize(). 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.mainbecause the overload does not exist there.Follow-up
After a package release, Nethermind can use the overload in
Eip2537.EncodeRawforG1andG2(serialize intostackallocand copy into the padded output). That change is not part of this PR.