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

Commit

Permalink
Merge pull request #171 from ethereumjs/fix-isValidSignature-method
Browse files Browse the repository at this point in the history
Fix isValidSignature method
  • Loading branch information
holgerd77 authored Feb 4, 2019
2 parents 9c4dbfe + 777241f commit fb864da
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
9 changes: 5 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -571,15 +571,16 @@ exports.addHexPrefix = function (str) {
/**
* Validate ECDSA signature
* @method isValidSignature
* @param {Buffer} v
* @param {Number} v
* @param {Buffer} r
* @param {Buffer} s
* @param {Boolean} [homestead=true]
* @param {Boolean} [homesteadOrLater=true] Indicates whether this is being used on either the homestead hardfork or a later one
* @param {Number} [chainId]
* @return {Boolean}
*/

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

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

if ((homestead === false) && (new BN(s).cmp(SECP256K1_N_DIV_2) === 1)) {
if (homesteadOrLater && (new BN(s).cmp(SECP256K1_N_DIV_2) === 1)) {
return false
}

Expand Down
18 changes: 18 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,24 @@ describe('isValidSignature', function () {
const s = Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex')
assert.equal(ethUtils.isValidSignature(29, r, s), false)
})
it('should fail when on homestead and s > secp256k1n/2', function () {
const SECP256K1_N_DIV_2 = new BN('7fffffffffffffffffffffffffffffff5d576e7357a4501ddfe92f46681b20a0', 16)

const r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex')
const s = Buffer.from(SECP256K1_N_DIV_2.add(new BN('1', 16)).toString(16), 'hex')

const v = 27
assert.equal(ethUtils.isValidSignature(v, r, s, true), false)
})
it('should not fail when not on homestead but s > secp256k1n/2', function () {
const SECP256K1_N_DIV_2 = new BN('7fffffffffffffffffffffffffffffff5d576e7357a4501ddfe92f46681b20a0', 16)

const r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex')
const s = Buffer.from(SECP256K1_N_DIV_2.add(new BN('1', 16)).toString(16), 'hex')

const v = 27
assert.equal(ethUtils.isValidSignature(v, r, s, false), true)
})
it('should work otherwise', function () {
const r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex')
const s = Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex')
Expand Down

0 comments on commit fb864da

Please sign in to comment.