Repository navigation
secp256k1: Separate packages for field implementations. - #3757
Conversation
jholdstock
left a comment
There was a problem hiding this comment.
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!
ec3ef40 to
4ffc59e
Compare
|
I believe everything has been addressed. |
4ffc59e to
9181a8d
Compare
| // | ||
| // IMPORTANT: The max allowed magnitude of a field element is 32. | ||
| type FieldVal struct { | ||
| impl fieldImpl |
There was a problem hiding this comment.
Using an interface here appears to break escape analysis and introduce hot-path heap allocations.
There was a problem hiding this comment.
It's just a type alias, not an interface. I see no additional allocations.
There was a problem hiding this comment.
For //go:build !(amd64 || arm64 || ppc64 || ppc64le || s390x || riscv64):
type fieldImpl = field10x26.ElementOr for //go:build amd64 || arm64 || ppc64 || ppc64le || s390x || riscv64:
type fieldImpl = field4x64.ElementEDIT: 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.
There was a problem hiding this comment.
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.
9181a8d to
3880d47
Compare
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
FieldValfor that implementation. However, recent work has introduced a newFieldVal64implementation 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:
FieldValdocumentation always reflects the most restrictive semantics among all supported field implementationsAfter considering a variety of approaches, the following design satisfies all of those goals:
Elementwithin their respective packagesFieldVala thin wrapper over the chosen implementationThe 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:
FieldValimplementation, tests, and benchmarks into a separate package namedfield10x26and only make the minimal required changes to ensure the code compiles and all tests passFieldValin the newfield10x26package toElementto more accurately reflect that it is an implementation-specific field element rather than the public compatibility typeFieldValwhich defers to a backend impelmentationfield10x26README.mdFieldVal64implementation, tests, and benchmarks into a separate package namedfield4x64and only make the minimal required changes to ensure the code compiles and all tests passFieldVal64in the newfield4x64package toElementfor the same reason as beforefield4x64README.mdSee 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.