Skip to content

secp256k1: Separate generic field64 impls. - #3754

Merged
davecgh merged 4 commits into
decred:masterfrom
davecgh:secp256k1_field64_sel_rework
Nov 20, 2026
Merged

davecgh merged 4 commits into
decred:masterfrom
davecgh:secp256k1_field64_sel_rework

Conversation

@davecgh

@davecgh davecgh commented Jul 27, 2026

Copy link
Copy Markdown
Member

This requires #3753.

The recent additions for dispatching the architecture-dependent field64 implementation in #3734 conflates three distinct concerns:

  • The reduction algorithm (field64Reduce512)
  • Generic multiply/square + reduction implementations
  • ISA dispatch

Notably, the generic implementations of the fused field multiplication and squaring operations are currently duplicated in the amd64-specific code and the generic fallback code.

Rather than duplicate that code, this moves the shared reduction implementation and generic fused multiply/square helpers into field64_generic.go and updates the architecture-specific dispatch to invoke the shared generic implementation when the optimized implementation is unavailable.

To accomplish that, a new field64_noasm.go is introduced that only builds when the architecture-specific code is not built.

Finally, it moves the amd64 instruction set architecture dispatch into field64isa_amd64.go file so that it only builds on amd64 and clearly separates ISA selection from the implementation itself.

These modifications mirror the approach used for architecture-dependent code in the rest of the code base.

While here, it also cleans up the associated benchmarks and renames the field64Mul and field64Square funcs and their variants to include Reduce in their name to make it clear they are fused multiply/square-and-reduce operations unlike the generic arith.Mul512 and field64Reduce512 primitives.

@davecgh davecgh added this to the 2.2.0 milestone Jul 27, 2026
@davecgh
davecgh force-pushed the secp256k1_field64_sel_rework branch 3 times, most recently from 7b165e0 to 33262ed Compare July 29, 2026 00:30

@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.

Not specific to this PR, but I wonder if we want to add a purego test/build to CI to ensure all of the code/test paths are actually exercised by GitHub

Comment thread dcrec/secp256k1/field64_noasm.go Resolved
@davecgh

davecgh commented Jul 29, 2026

Copy link
Copy Markdown
Member Author

Not specific to this PR, but I wonder if we want to add a purego test/build to CI to ensure all of the code/test paths are actually exercised by GitHub

The way I handled it in blake256 is the same way the Benchmark is done in this one. It calls all variants it can by manipulating the package-level flags so long as the hardware can actually support the variant, with the first case always being the generic Go implementation.

I suspect that is one of the cleanest ways and would work well for this module too.

@davecgh
davecgh force-pushed the secp256k1_field64_sel_rework branch from 28a20b8 to 2bf8bdc Compare July 30, 2026 09:05
@davecgh

davecgh commented Jul 30, 2026 •

Copy link
Copy Markdown
Member Author

The way I handled it in blake256 is the same way the Benchmark is done in this one. It calls all variants it can by manipulating the package-level flags so long as the hardware can actually support the variant, with the first case always being the generic Go implementation.

I suspect that is one of the cleanest ways and would work well for this module too.

I've implemented this via a separate commit now. I modified it slightly from the blake256 code to use t.Skipf instead, but it's the same in spirit.

--- PASS: TestField64AMD64 (0.00s)
    --- PASS: TestField64AMD64/Pure_Go (0.00s)
        --- PASS: TestField64AMD64/Pure_Go/Mul (0.00s)
        --- PASS: TestField64AMD64/Pure_Go/Square (0.00s)
    --- PASS: TestField64AMD64/BMI2/ADX (0.00s)
        --- PASS: TestField64AMD64/BMI2/ADX/Mul (0.00s)
        --- PASS: TestField64AMD64/BMI2/ADX/Square (0.00s)
PASS

@davecgh
davecgh force-pushed the secp256k1_field64_sel_rework branch from 2bf8bdc to a12dfc5 Compare July 31, 2026 05:34
@davecgh

davecgh commented Jul 31, 2026 •

Copy link
Copy Markdown
Member Author

I noticed I made a small mistake I made when adding the new tests for the purego test builds since the variable the test tries to access wasn't defined for noasm builds. Updated to fix that.

On amd64 with BMI2/MULX support:

$ go test -run=TestField64AMD64 -v
...
--- PASS: TestField64AMD64 (0.00s)
    --- PASS: TestField64AMD64/Pure_Go (0.00s)
        --- PASS: TestField64AMD64/Pure_Go/Mul (0.00s)    
        --- PASS: TestField64AMD64/Pure_Go/Square (0.00s) 
    --- PASS: TestField64AMD64/BMI2/ADX (0.00s)
        --- PASS: TestField64AMD64/BMI2/ADX/Mul (0.00s)   
        --- PASS: TestField64AMD64/BMI2/ADX/Square (0.00s)
PASS
ok      github.com/decred/dcrd/dcrec/secp256k1/v4       0.389s

With -tags=purego:

$ go test -tags=purego -run=TestField64AMD64 -v
...
=== RUN   TestField64AMD64/BMI2/ADX
    field64_amd64_test.go:44: Skipping BMI2/ADX tests (disabled or no instruction set support)
--- PASS: TestField64AMD64 (0.00s)
    --- PASS: TestField64AMD64/Pure_Go (0.00s)
        --- PASS: TestField64AMD64/Pure_Go/Mul (0.00s)
        --- PASS: TestField64AMD64/Pure_Go/Square (0.00s)
    --- SKIP: TestField64AMD64/BMI2/ADX (0.00s)
PASS
ok      github.com/decred/dcrd/dcrec/secp256k1/v4       0.387s

@davecgh
davecgh force-pushed the secp256k1_field64_sel_rework branch from a12dfc5 to 7333cfb Compare July 31, 2026 06:44
@davecgh

davecgh commented Jul 31, 2026

Copy link
Copy Markdown
Member Author

Switched the testing back to use continue instead of t.Skipf because tinygo does not support the latter:

$ tinygo test ./...
...
--- FAIL: TestElementAMD64 (0.00s)
    --- FAIL: TestElementAMD64/BMI2/ADX (0.00s)
        Skipping BMI2/ADX tests (disabled or no instruction set support)
        SkipNow is incomplete, requires runtime.Goexit()

But with the continue:

$ tinygo test ./...
ok      github.com/decred/dcrd/dcrec/secp256k1/v4       0.769s
ok      github.com/decred/dcrd/dcrec/secp256k1/v4/ecdsa 0.828s
ok      github.com/decred/dcrd/dcrec/secp256k1/v4/field10x26    0.216s
ok      github.com/decred/dcrd/dcrec/secp256k1/v4/field4x64     0.194s
ok      github.com/decred/dcrd/dcrec/secp256k1/v4/internal/arith        0.191s
ok      github.com/decred/dcrd/dcrec/secp256k1/v4/internal/cpufeat      0.086s
ok      github.com/decred/dcrd/dcrec/secp256k1/v4/schnorr       0.415s

@davecgh
davecgh force-pushed the secp256k1_field64_sel_rework branch from 7333cfb to 1ed1176 Compare July 31, 2026 06:49
This modifies the field64 amd64 benchmarks to be more consistent with
the approach used for architecture-specific benchmarks in the rest of
the code base.

This approach is cleaner and makes it easier to add additional
specialized implementations in the future.
This renames the field64Mul and field64Square funcs and their variants
to include Reduce in their name to make it clear they are fused
multiply/square-and-reduce operations unlike the generic arith.Mul512
and field64Reduce512 primitives.
The generic implementations of the fused field multiplication and
squaring operations are currently duplicated in the amd64-specific code
and the generic fallback code.

Rather than duplicate that code, this moves the shared reduction
implementation and generic fused multiply/square helpers into
field64_generic.go and updates the architecture-specific dispatch to
invoke the shared generic implementation when the optimized
implementation is unavailable.

To accomplish that, a new field64_noasm.go is introduced that only
builds when the architecture-specific code is not built.

Finally, it moves the amd64 instruction set architecture dispatch into
field64isa_amd64.go file so that it only builds on amd64 and clearly
separates ISA selection from the implementation itself.

These modifications mirror the approach used for architecture-dependent
code in the rest of the code base.
This adds tests to ensure all supported variants of the mul and square
methods that choose different implementations at run time depending on
available hardware are exercised when run on hardware that supports
them.

In particular, because pure Go is always supported, it ensures the pure
Go code paths are for the methods are always tested versus only testing
the optimized paths.
@davecgh
davecgh force-pushed the secp256k1_field64_sel_rework branch from 1ed1176 to ae4d56d Compare November 20, 2026 07:29
@davecgh
davecgh merged commit ae4d56d into decred:master Nov 20, 2026
32 checks passed
@davecgh
davecgh deleted the secp256k1_field64_sel_rework branch November 20, 2026 07:35
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