btcec/schnorr: reject s >= group order in ParseSignature#2561
Open
Lrifton92 wants to merge 1 commit into
Open
Conversation
dca1759 to
5651320
Compare
Author
|
Thanks for the review @allocz — and for confirming the fix against BIP-340. Addressed both formatting notes in the test:
|
allocz
suggested changes
Jul 6, 2026
ParseSignature documents that it enforces the BIP-340 requirement that the s component lie in the range [0, n-1], and the r component is already checked against the field prime accordingly. However, the overflow return value of s.SetByteSlice was discarded, so an s value greater than or equal to the group order n was silently reduced modulo n and accepted instead of being rejected. This restores parity with the reference implementation in decred/dcrd/dcrec/secp256k1/schnorr (of which this code is a port) and with Bitcoin Core, both of which reject such encodings at parse time. The ErrSigSTooBig error kind is already defined in the imported schnorr package but was previously unused here. A regression test covering the s == n and s > n encodings is added; it fails before this change (the signatures are silently accepted) and passes after. Signed-off-by: Lrifton92 <Lrifton92@users.noreply.github.com>
5651320 to
83d081a
Compare
Author
|
Thanks @allocz — you're right, the wrapped continuation still exceeded 80 columns. I've now unwrapped the sig strings entirely (single line each, as you suggested you'd approve), and re-ran |
allocz
approved these changes
Jul 7, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Change Description
schnorr.ParseSignaturedocuments that it enforces the BIP-340 requirement that thescomponent lie in the range[0, n-1], alongside the range check it already performs onr:The
rcheck is implemented correctly (overflow →ErrSigRTooBig), but forsthe overflow return value ofModNScalar.SetByteSlicewas discarded:Because
SetByteSlicereduces its input modulonand reports the overflow via its return value, an encoding withs >= nwas silently reduced modulo n and accepted instead of being rejected. This diverges from:sis enforced to be in[0, n-1]);decred/dcrd/dcrec/secp256k1/schnorr— of which this file is a port — which rejects the same input withErrSigSTooBig. TheErrSigSTooBigerror kind is already defined in the importedschnorrpackage but was unused here, which is what tipped me off that the check was dropped during the port.The fix mirrors the existing
rcheck exactly:ParseSignatureis on the taproot signature-validation path (txscriptkey-path and script-path spends), so this also brings btcd's strictness in line with Bitcoin Core, which rejects non-canonical (s >= n) Schnorr encodings.Test
Adds
TestParseSignatureComponentRange, reusing the references == nands > nvectors. The test fails before this change (both encodings are accepted, returningnil) and passes after. Verified by reverting the one-line fix while keeping the test:go build ./... && go test ./...andgo vet ./...are green across the wholebtcecmodule.Notes
This is intentionally minimal and independent of the other open schnorr PRs (#2546, #2501) — neither touches the
s-range check inParseSignature(I checked their diffs). Happy to rebase or fold this into #2546 if a maintainer prefers.