Repository navigation
Conversation
|
Approved CI run. |
1f3d515 to
ac92fde
Compare
Yep, this PR is blocked until we get new releases of the Go compiler and dcrec/secp256k1 containing the patches. |
Roasbeef
left a comment
There was a problem hiding this comment.
No actionable correctness regression found in the reviewed parsing, serialization, hashing, and verification changes. go test ./... passed from btcec at this head. I haven't independently reproduced the allocation/performance benchmarks.
Holding off on approval until this is rebased to resolve the conflicts and the Go/secp upstream-release prerequisites mentioned in the PR description are checked. Please confirm the dependency/toolchain versions containing those optimizations so the reported allocation results can be reproduced.
|
@Roasbeef, I've updated the description with the instructions to reproduce the benchmarks. Also, specified that we need to wait until go1.28.0 and dcrec v4.4.2 releases to merge this PR collecting 100% of the performance/allocation improvements. |
To optimize critical procedures used in validation loop, we first need all of them covered by benchmarks, so that we can have a baseline for future optimizations. btcec added benchmarks: * BenchmarkParseUncompressedPubkey btcec/ecdsa added benchmarks: * BenchmarkParseDERSignature * BenchmarkParseBERSignature btcec/schnorr added benchmarks: * BenchmarkParsePubKey * BenchmarkParseSignature btcec/ecdsa benchmark improvements: * Avoid unneccessary allocations on BenchmarkSigVerify. * Avoid unneccessary allocations on BenchmarkParseCompressedPubKey.
Previously, changing DER boolean argument of parseSig was breaking VerifyLowS behavior without breaking the tests. New sentinel errors and entries to the VerifyLowS test table were added, effectively catching regressions in the implementation.
Added unexported function that does not return a pointer to the public key to execute the key parsing, letting the exported ParsePubKey short and simple, making it eligible to inlining and therefore zeroing out allocations.
Added unexported function that does not return a pointer to the public key to execute the key parsing, letting the exported ParsePubKey short and simple, making it eligible to inlining and therefore zeroing out allocations.
btcec/schnorr: replace usage of SerializePubKey for serializePubKey32, which writes to a byte slice instead of allocating and returning a new one. chainhash: keep TaggedHash short and simple to make it eligible to inlining and avoid allocations.
Make ParseSignature and ParseDERSignature short and simple, making the procedures eligible to inlining and therefore avoiding allocations.
Make ParseSignature short and simple, becoming eligible to inlining which avoids allocations.
ac92fde to
4182225
Compare
|
Rebased on top of master. |
Change Description
I've optimized public key parsing, signature parsing and signature verification
for both ECDSA and Schnorr, as the benchmarks below show.
Obs: using the following patches for both benchmarks:
golang/go#79708: big.Int.ModInverse allocation zeroing.
decred/dcrd#3714: Public Key parsing and ModNScalar
modulo inverse allocation zeroing.
To avoid forgetting to update the dependencies in the future, even after approval, I think we should wait for upstream releases of both Go and
dcrec/secp256k1containing the patches mentioned above, so that when this code is merged, the benchmark report the exact same results in respect of allocation count.Merge blockers
Until the release of the upstream dependencies above, the benchmarks can
be reproduced in a container as described below.
Benchmark improvements:
The signature parsing time reduced in ~20%. But all the procedures in the
benchmarks above are not doing allocations anymore, reducing the
frequency between garbage collection cycles that must scan all the
reachable heap (that is considerably large in production).
Now we have more CPU available to perform block verification.
Reproducing the benchmarks
Containerfile:
init.sh:
Build the image:
Start the container that will run the benchmarks:
Pull Request Checklist
Testing
Code Style and Documentation
📝 Please see our Contribution Guidelines for further guidance.