Skip to content

Commit

Permalink
fix edge case for publicKeyTweakAdd (#140)
Browse files Browse the repository at this point in the history
After base point multiplication and adding point we should check that
result is not infinity point.
  • Loading branch information
fanatid authored Jan 28, 2019
1 parent 365732a commit 86cbc02
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
5 changes: 4 additions & 1 deletion lib/elliptic/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,10 @@ exports.publicKeyTweakAdd = function (publicKey, tweak, compressed) {
tweak = new BN(tweak)
if (tweak.cmp(ecparams.n) >= 0) throw new Error(messages.EC_PUBLIC_KEY_TWEAK_ADD_FAIL)

return Buffer.from(ecparams.g.mul(tweak).add(pair.pub).encode(true, compressed))
var point = ecparams.g.mul(tweak).add(pair.pub)
if (point.isInfinity()) throw new Error(messages.EC_PUBLIC_KEY_TWEAK_ADD_FAIL)

return Buffer.from(point.encode(true, compressed))
}

exports.publicKeyTweakMul = function (publicKey, tweak, compressed) {
Expand Down
5 changes: 4 additions & 1 deletion lib/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ exports.publicKeyTweakAdd = function (publicKey, tweak, compressed) {
tweak = BN.fromBuffer(tweak)
if (tweak.isOverflow()) throw new Error(messages.EC_PUBLIC_KEY_TWEAK_ADD_FAIL)

return g.mul(tweak).add(point).toPublicKey(compressed)
var result = g.mul(tweak).add(point)
if (result.inf) throw new Error(messages.EC_PUBLIC_KEY_TWEAK_ADD_FAIL)

return result.toPublicKey(compressed)
}

exports.publicKeyTweakMul = function (publicKey, tweak, compressed) {
Expand Down
12 changes: 12 additions & 0 deletions test/publickey.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,18 @@ module.exports = function (t, secp256k1) {
t.end()
})

t.test('tweak produce infinity point', function (t) {
// G * 1 - G = 0
t.throws(function () {
var publicKey = Buffer.from(util.ec.g.encode(null, true))
publicKey[0] = publicKey[0] ^ 0x01 // change sign of G
var tweak = util.BN_ONE.toArrayLike(Buffer, 'be', 32)
secp256k1.publicKeyTweakAdd(publicKey, tweak, true)
}, new RegExp('^Error: ' + messages.EC_PUBLIC_KEY_TWEAK_ADD_FAIL + '$'))

t.end()
})

t.test('compressed should be a boolean', function (t) {
t.throws(function () {
var privateKey = util.getPrivateKey()
Expand Down

0 comments on commit 86cbc02

Please sign in to comment.