Skip to content

Commit 86cbc02

Browse files
authored
fix edge case for publicKeyTweakAdd (#140)
After base point multiplication and adding point we should check that result is not infinity point.
1 parent 365732a commit 86cbc02

3 files changed

Lines changed: 20 additions & 2 deletions

File tree

lib/elliptic/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,10 @@ exports.publicKeyTweakAdd = function (publicKey, tweak, compressed) {
130130
tweak = new BN(tweak)
131131
if (tweak.cmp(ecparams.n) >= 0) throw new Error(messages.EC_PUBLIC_KEY_TWEAK_ADD_FAIL)
132132

133-
return Buffer.from(ecparams.g.mul(tweak).add(pair.pub).encode(true, compressed))
133+
var point = ecparams.g.mul(tweak).add(pair.pub)
134+
if (point.isInfinity()) throw new Error(messages.EC_PUBLIC_KEY_TWEAK_ADD_FAIL)
135+
136+
return Buffer.from(point.encode(true, compressed))
134137
}
135138

136139
exports.publicKeyTweakMul = function (publicKey, tweak, compressed) {

lib/js/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,10 @@ exports.publicKeyTweakAdd = function (publicKey, tweak, compressed) {
7878
tweak = BN.fromBuffer(tweak)
7979
if (tweak.isOverflow()) throw new Error(messages.EC_PUBLIC_KEY_TWEAK_ADD_FAIL)
8080

81-
return g.mul(tweak).add(point).toPublicKey(compressed)
81+
var result = g.mul(tweak).add(point)
82+
if (result.inf) throw new Error(messages.EC_PUBLIC_KEY_TWEAK_ADD_FAIL)
83+
84+
return result.toPublicKey(compressed)
8285
}
8386

8487
exports.publicKeyTweakMul = function (publicKey, tweak, compressed) {

test/publickey.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,18 @@ module.exports = function (t, secp256k1) {
241241
t.end()
242242
})
243243

244+
t.test('tweak produce infinity point', function (t) {
245+
// G * 1 - G = 0
246+
t.throws(function () {
247+
var publicKey = Buffer.from(util.ec.g.encode(null, true))
248+
publicKey[0] = publicKey[0] ^ 0x01 // change sign of G
249+
var tweak = util.BN_ONE.toArrayLike(Buffer, 'be', 32)
250+
secp256k1.publicKeyTweakAdd(publicKey, tweak, true)
251+
}, new RegExp('^Error: ' + messages.EC_PUBLIC_KEY_TWEAK_ADD_FAIL + '$'))
252+
253+
t.end()
254+
})
255+
244256
t.test('compressed should be a boolean', function (t) {
245257
t.throws(function () {
246258
var privateKey = util.getPrivateKey()

0 commit comments

Comments
 (0)