Skip to content

Conversation

jorgecuesta
Copy link

Description

This PR introduces a new EncodeInto(dst []byte) int method on types.Point, implemented for both secp256k1 and ed25519 points.

The goal is to allow callers (like ring-go) to reuse buffers when serializing points, instead of always allocating a fresh slice. This is particularly helpful inside signature verification loops where point encoding is on the hot path.

Key changes:

  • Added EncodeInto to types.Point interface.
  • Implemented EncodeInto for secp256k1 and ed25519 points.
  • Added benchmarks to compare Encode vs EncodeInto and demonstrate that:
    • Neutral on secp256k1 (no regressions).
    • Fewer allocations on ed25519 (allocation-free path).
  • Added simple tests to ensure EncodeInto produces the same bytes as Encode.

Benchmarks

Ran on AMD Ryzen 9 5950X, Go 1.23:

BenchmarkChallenge_Secp256k1/with_EncodeInto-32       65546   17426 ns/op   1483 B/op   12 allocs/op
BenchmarkChallenge_Secp256k1/with_Encode-32           67558   17454 ns/op   1483 B/op   12 allocs/op

BenchmarkChallenge_Ed25519/with_EncodeInto-32        171180    6961 ns/op   1064 B/op    6 allocs/op
BenchmarkChallenge_Ed25519/with_Encode-32            167292    7025 ns/op   1128 B/op    8 allocs/op

BenchmarkEncodeInto_vs_Encode_Secp256k1/EncodeInto-32 150338   8136 ns/op     48 B/op    1 allocs/op
BenchmarkEncodeInto_vs_Encode_Secp256k1/Encode-32     147151   8121 ns/op     48 B/op    1 allocs/op

BenchmarkEncodeInto_vs_Encode_Ed25519/EncodeInto-32  402760    2935 ns/op      0 B/op    0 allocs/op
BenchmarkEncodeInto_vs_Encode_Ed25519/Encode-32      401104    2988 ns/op     32 B/op    1 allocs/op

Summary

  • ✅ Keeps backward compatibility (Encode unchanged).
  • ✅ Provides an allocation-free path for projects that want to reuse buffers.
  • ✅ Neutral on secp256k1, positive on ed25519.
  • ✅ Includes tests and benchmarks to validate behavior.

…coding.

This commit introduces the `PointEncodeInto` interface for zero-allocation point serialization. Benchmarks comparing `EncodeInto` and `Encode` methods for secp256k1 and ed25519 have been added to evaluate efficiency improvements.
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.

1 participant