Skip to content

Commit 5f8fa24

Browse files
committed
unify code with elliptic.js more
1 parent 99a398c commit 5f8fa24

File tree

1 file changed

+27
-51
lines changed

1 file changed

+27
-51
lines changed

lib/noble.js

+27-51
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ if (!secp256k1.utils.sha256Sync) {
1111
secp256k1.utils.sha256Sync = (...msgs) => sha256(secp256k1.utils.concatBytes(...msgs))
1212
}
1313

14+
function readPublicKey (pubkey) {
15+
try {
16+
return secp256k1.Point.fromHex(pubkey)
17+
} catch (err) {
18+
return undefined
19+
}
20+
}
21+
1422
function writePublicKey (output, point) {
1523
const buf = point.toRawBytes(output.length === 33)
1624
if (output.length !== buf.length) return 1
@@ -76,11 +84,8 @@ module.exports = {
7684
},
7785

7886
publicKeyVerify (pubkey) {
79-
try {
80-
return secp256k1.Point.fromHex(pubkey) ? 0 : 1
81-
} catch (err) {
82-
return 1
83-
}
87+
const P = readPublicKey(pubkey)
88+
return P ? 0 : 1
8489
},
8590

8691
publicKeyCreate (output, seckey) {
@@ -95,36 +100,23 @@ module.exports = {
95100
},
96101

97102
publicKeyConvert (output, pubkey) {
98-
try {
99-
const publicKey = secp256k1.Point.fromHex(pubkey).toRawBytes(output.length === 33)
100-
if (output.length !== publicKey.length) return 1
101-
output.set(publicKey)
102-
return 0
103-
} catch (err) {
104-
return 1
105-
}
103+
const P = readPublicKey(pubkey)
104+
if (!P) return 1
105+
return writePublicKey(output, P)
106106
},
107107

108108
publicKeyNegate (output, pubkey) {
109-
let P
110-
try {
111-
P = secp256k1.Point.fromHex(pubkey)
112-
} catch (err) {
113-
return 1
114-
}
115-
109+
const P = readPublicKey(pubkey)
110+
if (!P) return 1
116111
const point = P.negate()
117112
return writePublicKey(output, point)
118113
},
119114

120115
publicKeyCombine (output, pubkeys) {
121116
const points = new Array(pubkeys.length)
122117
for (let i = 0; i < pubkeys.length; ++i) {
123-
try {
124-
points[i] = secp256k1.Point.fromHex(pubkeys[i])
125-
} catch (err) {
126-
return 1
127-
}
118+
points[i] = readPublicKey(pubkeys[i])
119+
if (!points[i]) return 1
128120
}
129121

130122
let point = points[0]
@@ -134,12 +126,8 @@ module.exports = {
134126
},
135127

136128
publicKeyTweakAdd (output, pubkey, tweak) {
137-
let P
138-
try {
139-
P = secp256k1.Point.fromHex(pubkey)
140-
} catch (err) {
141-
return 1
142-
}
129+
const P = readPublicKey(pubkey)
130+
if (!P) return 1
143131

144132
tweak = toBig(tweak)
145133
if (tweak >= secp256k1.CURVE.n) return 2
@@ -151,12 +139,8 @@ module.exports = {
151139
},
152140

153141
publicKeyTweakMul (output, pubkey, tweak) {
154-
let P
155-
try {
156-
P = secp256k1.Point.fromHex(pubkey)
157-
} catch (err) {
158-
return 1
159-
}
142+
const P = readPublicKey(pubkey)
143+
if (!P) return 1
160144

161145
tweak = toBig(tweak)
162146
if (tweak >= secp256k1.CURVE.n || tweak === _0n) return 2
@@ -241,14 +225,10 @@ module.exports = {
241225
}
242226
if (signature.hasHighS()) return 3
243227

244-
let pub
245-
try {
246-
pub = secp256k1.Point.fromHex(pubkey)
247-
} catch (err) {
248-
return 2
249-
}
228+
const P = readPublicKey(pubkey)
229+
if (!P) return 2
250230

251-
return secp256k1.verify(sig, msg32, pub) ? 0 : 3
231+
return secp256k1.verify(sig, msg32, P) ? 0 : 3
252232
},
253233

254234
// Complex logic to return correct error codes
@@ -276,18 +256,14 @@ module.exports = {
276256
},
277257

278258
ecdh (output, pubkey, seckey, data, hashfn, xbuf, ybuf) {
279-
let pub
280-
try {
281-
pub = secp256k1.Point.fromHex(pubkey)
282-
} catch (err) {
283-
return 1
284-
}
259+
const P = readPublicKey(pubkey)
260+
if (!P) return 1
285261

286262
const compressed = hashfn === undefined
287263

288264
let point
289265
try {
290-
point = secp256k1.getSharedSecret(seckey, pub, compressed)
266+
point = secp256k1.getSharedSecret(seckey, P, compressed)
291267
} catch (err) {
292268
return 2
293269
}

0 commit comments

Comments
 (0)