Skip to content

Commit

Permalink
elliptic: fix key verification in loadCompressedPublicKey
Browse files Browse the repository at this point in the history
  • Loading branch information
ChALkeR authored and fanatid committed Oct 18, 2024
1 parent dc37f41 commit 9a15fff
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/elliptic.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ function loadCompressedPublicKey (first, xbuf) {
let y = x.redSqr().redIMul(x).redIAdd(ecparams.b).redSqrt()
if ((first === 0x03) !== y.isOdd()) y = y.redNeg()

// x*x*x + b = y*y
const x3 = x.redSqr().redIMul(x)
if (!y.redSqr().redISub(x3.redIAdd(ecparams.b)).isZero()) return null

return ec.keyPair({ pub: { x: x, y: y } })
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"node-gyp": "=10.1.0",
"nyc": "^15.0.0",
"prebuildify": "^6.0.1",
"prebuildify-cross": "github:fanatid/prebuildify-cross#9f7af67698f06e07d42304d9813a6f19aee5812c",
"prebuildify-cross": "^5.1.1",
"standard": "^14.3.1",
"tap-dot": "^2.0.0",
"tape": "^4.10.1",
Expand Down
6 changes: 6 additions & 0 deletions test/publickey.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ module.exports = (t, secp256k1) => {
invalidLength[0] = publicKey.compressed[0]
t.false(secp256k1.publicKeyVerify(invalidLength), 'invalid length')

const zeroUncompressed = Buffer.concat([Buffer.from([0x04]), Buffer.alloc(64)])
t.false(secp256k1.publicKeyVerify(zeroUncompressed), 'zero uncompressed')

const zeroCompressed = Buffer.concat([Buffer.from([0x02]), Buffer.alloc(32)])
t.false(secp256k1.publicKeyVerify(zeroCompressed), 'zero compressed')

t.end()
})

Expand Down

0 comments on commit 9a15fff

Please sign in to comment.