Skip to content

secp256k1: Separate packages for field implementations. - #3757

Merged
davecgh merged 8 commits into
decred:masterfrom
davecgh:secp256k1_separate_field_subpkgs
Nov 20, 2026
Merged

davecgh merged 8 commits into
decred:masterfrom
davecgh:secp256k1_separate_field_subpkgs

Conversation

@davecgh

@davecgh davecgh commented Jul 29, 2026

Copy link
Copy Markdown
Member

This requires #3754.


Historically, there has only ever been a single field implementation and the current release of the module reflects that fact with a single exported FieldVal for that implementation. However, recent work has introduced a new FieldVal64 implementation which is significantly more efficient on 64-bit hardware.

Ultimately, the main package will need to select the implementation based on the target architecture, but having two different types with different semantics exposed from the main package would be confusing. At the same time, consumers should retain the flexibility to use a specific implementation directly if their particular use case warrants it.

The primary design goals are:

  • Avoid breaking the public API in a way that would require a major module version bump
  • Make both variants available to consumers irrespective of the choice the module makes for internally performing its own calculations over the curve
    • This allows them to make independent calculations using their preferred implementation
  • Ensure the public FieldVal documentation always reflects the most restrictive semantics among all supported field implementations
    • Note that a simple type alias that changes based on the architecture does not provide this property
  • Provide a clean structure that allows adding additional assembler optimizations without bloating the main package

After considering a variety of approaches, the following design satisfies all of those goals:

  • Split the field implementations into separate subpackages and rename them to Element within their respective packages
  • Make FieldVal a thin wrapper over the chosen implementation

The required changes are largely mechanical, but they are quite extensive.

So, with the goal of making it much easier to review and reason about the correctness of the changes, this PR is split into a series of commits that will ultimately land on the described design.

The high-level overview of commits are as follows:

  • Move the entire FieldVal implementation, tests, and benchmarks into a separate package named field10x26 and only make the minimal required changes to ensure the code compiles and all tests pass
  • Rename FieldVal in the new field10x26 package to Element to more accurately reflect that it is an implementation-specific field element rather than the public compatibility type
  • Provide a thin wrapper in the main package to replace the original FieldVal which defers to a backend impelmentation
  • Add tests for the new wrapper
  • Add field10x26 README.md
  • Moves the entire FieldVal64 implementation, tests, and benchmarks into a separate package named field4x64 and only make the minimal required changes to ensure the code compiles and all tests pass
  • Rename FieldVal64 in the new field4x64 package to Element for the same reason as before
  • Add field4x64 README.md

See the individual commit descriptions for more details.

No functional changes are intended by this PR. It is strictly a refactoring that preserves the existing behavior while preparing for the architecture-specific backend selection.

@davecgh davecgh added this to the 2.2.0 milestone Jul 29, 2026

@jholdstock jholdstock left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Apologies in advance if any of the things I highlighted here are pre-existing or are introduced in PRs before this one. The commit chain is getting so long I am starting to lose track of what is what!

Comment thread dcrec/secp256k1/field10x26/element.go Resolved
Comment thread dcrec/secp256k1/fieldwrap.go Resolved
Comment thread dcrec/secp256k1/fieldwrap.go Resolved
Comment thread dcrec/secp256k1/field4x64/README.md Resolved
Comment thread dcrec/secp256k1/field4x64/element.go Resolved
Comment thread dcrec/secp256k1/field4x64/element.go Resolved
Comment thread dcrec/secp256k1/field4x64/element.go Resolved
Comment thread dcrec/secp256k1/field4x64/README.md Resolved
@davecgh
davecgh force-pushed the secp256k1_separate_field_subpkgs branch 3 times, most recently from ec3ef40 to 4ffc59e Compare July 30, 2026 10:39
@davecgh

davecgh commented Jul 30, 2026

Copy link
Copy Markdown
Member Author

I believe everything has been addressed.

Comment thread dcrec/secp256k1/field10x26/element.go Resolved
Comment thread dcrec/secp256k1/field4x64/README.md Resolved
Comment thread dcrec/secp256k1/field10x26/element.go Resolved
@davecgh
davecgh force-pushed the secp256k1_separate_field_subpkgs branch from 4ffc59e to 9181a8d Compare July 31, 2026 06:53
//
// IMPORTANT: The max allowed magnitude of a field element is 32.
type FieldVal struct {
impl fieldImpl

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Using an interface here appears to break escape analysis and introduce hot-path heap allocations.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It's just a type alias, not an interface. I see no additional allocations.

@davecgh davecgh Aug 3, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

For //go:build !(amd64 || arm64 || ppc64 || ppc64le || s390x || riscv64):

type fieldImpl = field10x26.Element

Or for //go:build amd64 || arm64 || ppc64 || ppc64le || s390x || riscv64:

type fieldImpl = field4x64.Element

EDIT: That is what happens in the next PR #3758.

This PR leaves it as field10x26.Element which matches the existing code prior to actually swapping out the backend in the next one.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Oh sorry. Ignore my wrong comment. I was mistaken in thinking that this was an interface.

Historically, there has only ever been a single field implementation and
the current release of the module reflects that fact with a single
exported FieldVal for that implementation.  However, recent work has
introduced a new FieldVal64 implementation which is significantly more
efficient on 64-bit hardware.

Ultimately, the main package will need to select the implementation
based on the target architecture, but having two different types with
different semantics exposed from the main package would be confusing.
At the same time, consumers should retain the flexibility to use a
specific implementation directly if their particular use case warrants
it.

The primary design goals are:

- Avoid breaking the public API in a way that would require a major
  module version bump
- Make both variants available to consumers irrespective of the choice
  the module makes for internally performing its own calculations over
  the curve
  - This allows them to make independent calculations using their
    preferred implementation
- Ensure the public FieldVal documentation always reflects the most
  restrictive semantics among all supported field implementations
  - Note that a simple type alias that changes based on the architecture
    does not provide this property
- Provide a clean structure that allows adding additional assembler
  optimizations without bloating the main package

After considering a variety of approaches, the following design
satisfies all of those goals:

- Split the field implementations into separate subpackages and rename
  them to Element within their respective packages
- Make FieldVal a thin wrapper over the chosen implementation

The required changes are largely mechanical, but they are quite
extensive.

So, with the goal of making it much easier to review and reason about
the correctness of the changes, this is the first in a series of commits
that will ultimately land on the described design.

This first commit moves the entire FieldVal implementation, tests, and
benchmarks into a separate package named field10x26 and only makes the
minimal required changes to ensure the code compiles and all tests pass.

It also temporarily aliases FieldVal to the new package.  That alias
will be replaced with the wrapper in a subsequent commit.

No functional changes are intended by this commit.  It is strictly a
refactoring that preserves the existing behavior while preparing for the
architecture-specific backend selection.
This renames FieldVal in the new field10x26 package to Element to more
accurately reflect that it is an implementation-specific field element
rather than the public compatibility type.  It also renames all tests
and benchmarks to match and updates various comments accordingly.

While here, also update the internal terminology to consistently use the
more technically accurate term "limbs".

It is a large change in terms of lines changed, but it is entirely
mechanical.

No functional changes are intended by this commit.  It is strictly a
refactoring that preserves the existing behavior.

This is part of a series of commits that will ultimately allow
architecture-specific backend selection.
This replaces the public FieldVal type alias with a wrapper around the
underlying field implementation.

This preserves the existing API while allowing the concrete field
implementation to vary independently of the public type.  It also
ensures the public documentation consistently describes the most
restrictive semantics required by all supported implementations rather
than those of whichever implementation happens to be selected.

The wrapper forwards all methods directly to the underlying
implementation, so no functional changes are intended.

This is part of a series of commits that will ultimately allow
architecture-specific backend selection.
This adds dedicated tests for the new FieldVal wrapper.

The tests intentionally do not perform exhaustive testing of each of the
methods since that is handled by each implementation.  Instead, the
focus is on making sure the wrapped methods return the same result as
the underlying implementation and the methods that involve chaining
return the same wrapped instance.

This is part of a series of commits that will ultimately allow
architecture-specific backend selection.
This is part of a series of commits that will ultimately allow
architecture-specific backend selection.
This moves the entire FieldVal64 implementation, tests, and benchmarks
into a separate package named field4x64 and only makes the minimal
required changes to ensure the code compiles and all tests pass.

It also modifies the Normalize method to match the signature used by
FieldVal for API parity.

No functional changes are intended by this commit.  It is strictly a
refactoring that preserves the existing behavior.

This is part of a series of commits that will ultimately allow
architecture-specific backend selection.
This renames FieldVal64 in the new field4x64 package to Element to more
accurately reflect that it is an implementation-specific field element
rather than the public compatibility type.  It also renames all tests
and benchmarks to match and updates various comments accordingly.

It is a large change in terms of lines changed, but it is entirely
mechanical.

No functional changes are intended by this commit.  It is strictly a
refactoring that preserves the existing behavior.

This is part of a series of commits that will ultimately allow
architecture-specific backend selection.
This is part of a series of commits that will ultimately allow
architecture-specific backend selection.
@davecgh
davecgh force-pushed the secp256k1_separate_field_subpkgs branch from 9181a8d to 3880d47 Compare November 20, 2026 07:35
@davecgh
davecgh merged commit 3880d47 into decred:master Nov 20, 2026
32 checks passed
@davecgh
davecgh deleted the secp256k1_separate_field_subpkgs branch November 20, 2026 07:39
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.

4 participants