crypto/internal/fips140/nistec/fiat: optimize P256mul on riscv64 #77069
+608
−1
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.
This PR optimizes the core
p256Muloperation for P256 elliptic curve cryptography on the RISCV64 architecture.Key Optimization:
Leverages the structural characteristics of the P256 modulus (p = 2²⁵⁶ - 2²²⁴ + 2¹⁹⁶ + 2⁹⁶ - 1). Converts the multiplication operation acc0 * p into shift operations, replacing 64-bit multiplications in the generic Go implementation and avoiding the overhead of big-integer multiplication.
Direct Register Operations: Uses registers for data transfer throughout the implementation, eliminating the extra overhead of stack-based data transfer in the Go-compiled version.
Unit Tests: TestP256Mul was implemented to verify the correctness of assembly implementation.
Benchmark: in src/crypto/internal/fips140/nistec/fiat/benchmark_test.go
Peformance:
goos: linux
goarch: riscv64
pkg: crypto/internal/fips140/nistec
cpu: SG2044
│ p256mul_base.riscv64.stats │ p256mul_opt.riscv64.stats │
│ sec/op │ sec/op vs base │
ScalarMult/P256-30 564.7µ ± 5% 491.8µ ± 6% -12.91% (p=0.002 n=6)