Skip to content

btcec/ecdsa: add SignLowR to grind for low R - #2630

Open
fametrano wants to merge 1 commit into
btcsuite:masterfrom
fametrano:ecdsa-low-r-grinding
Open

fametrano wants to merge 1 commit into
btcsuite:masterfrom
fametrano:ecdsa-low-r-grinding

Conversation

@fametrano

Copy link
Copy Markdown
Contributor

Change Description

I added ecdsa.SignLowR, an opt-in signer that grinds for a low R (first byte below 0x80) as Bitcoin Core's CKey::Sign does, so DER never pads R. Sign is unchanged. This fixes #1349 the second way it proposed: a new function, not a new default.

The first attempt is the plain RFC6979 signature, and attempt i > 0 passes i as 32 little-endian bytes of extra nonce data. For any hash below the group order, the result is Core's signature. dcrd's secp256k1 can derive such a nonce but cannot sign with one, so the signing step is written here on its exported nonce and curve functions.

The tests hold signatures made by libsecp256k1 (through electrum-ecc) that also match btclib-ecc. They include a first attempt with a high R that is 70 bytes in DER, which a check on length alone would keep, and counters 2 and 5.

Steps to Test

cd btcec && go test ./ecdsa -run LowR

Pull Request Checklist

Testing

  • Your PR passes all CI checks.
  • Tests covering the positive and negative (error paths) are included.
  • Bug fixes contain tests triggering the bug to prevent regressions.

Code Style and Documentation

Assisted-by: my usual tools (LLM, Internet, computer). Mistakes are mine.

@fametrano
fametrano force-pushed the ecdsa-low-r-grinding branch from b54bb53 to b4914db Compare October 13, 2026 11:27

@allocz allocz left a comment

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.

I don't think losing part of the entropy worth the gains.

@fametrano

Copy link
Copy Markdown
Contributor Author

Grinding costs the nonce one bit of entropy, but not a secret bit: it is the top bit of R's x-coordinate, which the signature shows anyway. Each attempt is a fresh RFC6979 nonce, so the accepted k is uniform among the nonces with a low R. No known method relates R's x-coordinate to k's bits, so a lattice attack gets no bias in k to use.

Bitcoin Core has signed this way by default since 0.17 (bitcoin/bitcoin#13666). SignLowR is opt-in and leaves Sign unchanged. It saves one byte in about half of the signatures and gives Core's signature for the same key and hash, which the tests check against libsecp256k1. If that is still not worth it for btcd, I am fine closing this.

Add SignLowR, which signs like Sign but searches for a signature whose
R has its first byte below 0x80, so DER never pads it. It follows
Bitcoin Core's CKey::Sign: the first attempt is the plain RFC6979
signature, and attempt i > 0 passes i as 32 little-endian bytes of
extra entropy to the nonce. For any hash below the group order, the
result is the signature Core makes.

Sign is unchanged. Nothing in dcrd's secp256k1 signs with extra nonce
data, so the signing step is written here on top of the exported nonce
and curve functions.

The tests hold signatures made by libsecp256k1, which also match
btclib-ecc. They include a first attempt with a high R that is 70
bytes in DER, which a check on length alone would keep, and counters
above 1.

Fixes btcsuite#1349.
@fametrano
fametrano force-pushed the ecdsa-low-r-grinding branch from b4914db to 5c97e62 Compare November 13, 2026 00:40
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.

[Pull Request welcome?] Grind for low R values

2 participants