FFFF fix: reject high-s signatures in Secp256k1.verify address path by brendanjryan · Pull Request #716 · wevm/ox · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tempo-envelope-low-s.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'ox': patch
---

Fixed `Secp256k1.verify` to reject signatures with a high `s` value when verifying against an `address`, matching the `publicKey` path and fixing `SignatureEnvelope.verify` in `ox/tempo`.
11 changes: 9 additions & 2 deletions src/core/Secp256k1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -632,15 +632,19 @@ export declare namespace sign {
*/
export function verify(options: verify.Options): boolean {
const { hash, payload } = options
if (options.address)
const sig = normalizeSignature(options.signature)
if (options.address) {
// Recovery accepts high-s signatures, so reject them as `engine.verify` does.
if (Bytes.toBigInt(toCompactBytes(sig).subarray(32)) > halfOrder)
return false
return Address.isEqual(
options.address,
recoverAddress({
payload: hash === true ? Hash.sha256(payload) : payload,
signature: options.signature,
}),
)
const sig = normalizeSignature(options.signature)
}
return engine.verify(
toCompactBytes(sig),
Bytes.from(payload),
Expand Down Expand Up @@ -726,5 +730,8 @@ export declare namespace InvalidSeedSizeError {
}
}

/** Half the secp256k1 curve order, the largest `s` a low-s signature can have. */
const halfOrder =
0x7fffffffffffffffffffffffffffffff5d576e7357a4501ddfe92f46681b20a0n
const fromPrfDomain = Bytes.fromString('ox.secp256k1.fromPrf.v1')
const fromSeedDomain = Bytes.fromString('ox.secp256k1.fromSeed.v1')
20 changes: 20 additions & 0 deletions src/core/_test/Secp256k1.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,26 @@ describe('verify', () => {
).toBe(true)
})

test('behavior: rejects high-s signature', () => {
const payload = '0xdeadbeef'
const { r, s, yParity } = Secp256k1.sign({ payload, privateKey })
const publicKey = Secp256k1.getPublicKey({ privateKey })
const signature = {
r,
s: Hex.fromNumber(
0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141n -
Hex.toBigInt(s),
{ size: 32 },
),
yParity: yParity === 0 ? 1 : 0,
}

// The high-s twin still recovers the signer's address.
expect(Secp256k1.recoverAddress({ payload, signature })).toBe(address)
expect(Secp256k1.verify({ address, payload, signature })).toBe(false)
expect(Secp256k1.verify({ payload, publicKey, signature })).toBe(false)
})

test('options: hash', () => {
const payload = '0xdeadbeef'
const { r, s } = Secp256k1.sign({ hash: true, payload, privateKey })
Expand Down
30 changes: 30 additions & 0 deletions src/tempo/SignatureEnvelope.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2148,6 +2148,36 @@ describe('verify', () => {
).toBe(true)
})

test('behavior: returns false for high-s signature', () => {
const privateKey = Secp256k1.randomPrivateKey()
const publicKey = Secp256k1.getPublicKey({ privateKey })
const address = Address.fromPublicKey(publicKey)
const payload = '0xdeadbeef' as const

const { r, s, yParity } = Secp256k1.sign({ payload, privateKey })
const highS = {
r,
s: Hex.fromNumber(
0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141n -
Hex.toBigInt(s),
{ size: 32 },
),
yParity: yParity === 0 ? 1 : 0,
}
const envelope = SignatureEnvelope.from(highS)

// The high-s twin recovers the same address, but Tempo rejects it.
expect(Secp256k1.recoverAddress({ payload, signature: highS })).toBe(
address,
)
expect(SignatureEnvelope.verify(envelope, { address, payload })).toBe(
false,
)
expect(SignatureEnvelope.verify(envelope, { payload, publicKey })).toBe(
false,
)
})

test('behavior: returns false for wrong publicKey', () => {
const privateKey = Secp256k1.randomPrivateKey()
const wrongPrivateKey = Secp256k1.randomPrivateKey()
Expand Down
Loading
0