Skip to content

Commit 6b5b98c

Browse files
Fix ECDSA nonce boundary checks (TOB-BSV-23) and add validation tests
1 parent dbda87f commit 6b5b98c

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

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)