Skip to content
This repository was archived by the owner on Jun 17, 2021. It is now read-only.

Commit fb864da

Browse files
authored
Merge pull request #171 from ethereumjs/fix-isValidSignature-method
Fix isValidSignature method
2 parents 9c4dbfe + 777241f commit fb864da

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -571,15 +571,16 @@ exports.addHexPrefix = function (str) {
571571
/**
572572
* Validate ECDSA signature
573573
* @method isValidSignature
574-
* @param {Buffer} v
574+
* @param {Number} v
575575
* @param {Buffer} r
576576
* @param {Buffer} s
577-
* @param {Boolean} [homestead=true]
577+
* @param {Boolean} [homesteadOrLater=true] Indicates whether this is being used on either the homestead hardfork or a later one
578578
* @param {Number} [chainId]
579579
* @return {Boolean}
580580
*/
581581

582-
exports.isValidSignature = function (v, r, s, homestead, chainId) {
582+
exports.isValidSignature = function (v, r, s, homesteadOrLater, chainId) {
583+
homesteadOrLater = homesteadOrLater === undefined ? true : homesteadOrLater
583584
const SECP256K1_N_DIV_2 = new BN('7fffffffffffffffffffffffffffffff5d576e7357a4501ddfe92f46681b20a0', 16)
584585
const SECP256K1_N = new BN('fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141', 16)
585586

@@ -598,7 +599,7 @@ exports.isValidSignature = function (v, r, s, homestead, chainId) {
598599
return false
599600
}
600601

601-
if ((homestead === false) && (new BN(s).cmp(SECP256K1_N_DIV_2) === 1)) {
602+
if (homesteadOrLater && (new BN(s).cmp(SECP256K1_N_DIV_2) === 1)) {
602603
return false
603604
}
604605

test/index.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,24 @@ describe('isValidSignature', function () {
558558
const s = Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex')
559559
assert.equal(ethUtils.isValidSignature(29, r, s), false)
560560
})
561+
it('should fail when on homestead and s > secp256k1n/2', function () {
562+
const SECP256K1_N_DIV_2 = new BN('7fffffffffffffffffffffffffffffff5d576e7357a4501ddfe92f46681b20a0', 16)
563+
564+
const r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex')
565+
const s = Buffer.from(SECP256K1_N_DIV_2.add(new BN('1', 16)).toString(16), 'hex')
566+
567+
const v = 27
568+
assert.equal(ethUtils.isValidSignature(v, r, s, true), false)
569+
})
570+
it('should not fail when not on homestead but s > secp256k1n/2', function () {
571+
const SECP256K1_N_DIV_2 = new BN('7fffffffffffffffffffffffffffffff5d576e7357a4501ddfe92f46681b20a0', 16)
572+
573+
const r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex')
574+
const s = Buffer.from(SECP256K1_N_DIV_2.add(new BN('1', 16)).toString(16), 'hex')
575+
576+
const v = 27
577+
assert.equal(ethUtils.isValidSignature(v, r, s, false), true)
578+
})
561579
it('should work otherwise', function () {
562580
const r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex')
563581
const s = Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex')

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy