Skip to content

Commit d509815

Browse files
authored
Merge pull request #406 from bsv-blockchain/TOB-23-ESDCA
Fix ECDSA nonce boundary checks (TOB-BSV-23) and add validation tests
2 parents dbda87f + c6eb43d commit d509815

File tree

5 files changed

+49
-4
lines changed

5 files changed

+49
-4
lines changed

CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ All notable changes to this project will be documented in this file. The format
55
## Table of Contents
66

77
- [Unreleased](#unreleased)
8+
- [1.9.20 - 2025-12-02](#1920---2025-12-02)
89
- [1.9.19 - 2025-12-02](#1919---2025-12-02)
910
- [1.9.18 - 2025-12-02](#1918---2025-12-02)
1011
- [1.9.17 - 2025-12-01](#1917---2025-12-01)
@@ -191,6 +192,21 @@ All notable changes to this project will be documented in this file. The format
191192
### Security
192193
---
193194

195+
### [1.9.20] - 2025-12-02
196+
197+
### Security
198+
199+
- Corrected ECDSA nonce range validation to strictly follow the specification.
200+
The implementation now correctly accepts `k = 1` and `k = n-1` when generating or
201+
validating nonces, resolving the issue raised in TOB-23.
202+
203+
### Fixed
204+
205+
- Updated both locations in the ECDSA sign function where the incorrect bound
206+
check was performed, ensuring consistent and standards-compliant behavior.
207+
208+
---
209+
194210
### [1.9.19] - 2025-12-02
195211

196212
### Added

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@bsv/sdk",
3-
"version": "1.9.18",
3+
"version": "1.9.19",
44
"type": "module",
55
"description": "BSV Blockchain Software Development Kit",
66
"main": "dist/cjs/mod.js",

src/primitives/ECDSA.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export const sign = (
8787
if (kBN == null) throw new Error('k is undefined')
8888
kBN = truncateToN(kBN, true)
8989

90-
if (kBN.cmpn(1) <= 0 || kBN.cmp(ns1) >= 0) {
90+
if (kBN.cmpn(1) < 0 || kBN.cmp(ns1) > 0) {
9191
if (BigNumber.isBN(customK)) {
9292
throw new Error('Invalid fixed custom K value (must be >1 and <N‑1)')
9393
}

src/primitives/__tests/ECDSA.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,33 @@ describe('ECDSA', () => {
6161
const signature = ECDSA.sign(msg, key)
6262
expect(ECDSA.verify(msg, signature, wrongPub)).toBeFalsy()
6363
})
64+
65+
it('should accept custom k = 1 and k = n-1', () => {
66+
const n = curve.n
67+
const one = new BigNumber(1)
68+
69+
// k = 1 → valid
70+
const k1 = one
71+
const sig1 = ECDSA.sign(msg, key, undefined, k1)
72+
expect(ECDSA.verify(msg, sig1, pub)).toBeTruthy()
73+
74+
// k = n-1 → valid
75+
const km1 = n.subn(1)
76+
const sig2 = ECDSA.sign(msg, key, undefined, km1)
77+
expect(ECDSA.verify(msg, sig2, pub)).toBeTruthy()
78+
})
79+
80+
it('should reject custom k < 1 or k > n-1', () => {
81+
const n = curve.n
82+
83+
// k = 0 → invalid
84+
expect(() =>
85+
ECDSA.sign(msg, key, undefined, new BigNumber(0))
86+
).toThrow()
87+
88+
// k = n → invalid
89+
expect(() =>
90+
ECDSA.sign(msg, key, undefined, n)
91+
).toThrow()
92+
})
6493
})

0 commit comments

Comments
 (0)