Skip to content

Commit e256905

Browse files
ChALkeRfanatid
authored andcommitted
elliptic: fix key verification in loadCompressedPublicKey
1 parent 289dbc3 commit e256905

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

lib/elliptic/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ function loadCompressedPublicKey (first, xBuffer) {
2020
var y = x.redSqr().redIMul(x).redIAdd(ecparams.b).redSqrt()
2121
if ((first === 0x03) !== y.isOdd()) y = y.redNeg()
2222

23+
// x*x*x + b = y*y
24+
const x3 = x.redSqr().redIMul(x)
25+
if (!y.redSqr().redISub(x3.redIAdd(ecparams.b)).isZero()) return null
26+
2327
return ec.keyPair({ pub: { x: x, y: y } })
2428
}
2529

test/publickey.js

+10
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,16 @@ module.exports = function (t, secp256k1) {
171171
t.end()
172172
})
173173

174+
t.test('zero key', function (t) {
175+
const zeroUncompressed = Buffer.concat([Buffer.from([0x04]), Buffer.alloc(64)])
176+
t.false(secp256k1.publicKeyVerify(zeroUncompressed), 'zero uncompressed')
177+
178+
const zeroCompressed = Buffer.concat([Buffer.from([0x02]), Buffer.alloc(32)])
179+
t.false(secp256k1.publicKeyVerify(zeroCompressed), 'zero compressed')
180+
181+
t.end();
182+
})
183+
174184
util.repeat(t, 'random tests', util.env.repeat, function (t) {
175185
var privateKey = util.getPrivateKey()
176186
var publicKey = util.getPublicKey(privateKey)

0 commit comments

Comments
 (0)