@@ -3,7 +3,7 @@ import * as secp from "./secp256k1";
3
3
import { assertBool , assertBytes , hexToBytes , toHex } from "./utils" ;
4
4
5
5
// Legacy compatibility layer for elliptic via noble-secp256k1
6
- // Use `curve- secp256k1` module directly instead
6
+ // Use `secp256k1` module directly instead
7
7
8
8
// Copy-paste from secp256k1, maybe export it?
9
9
const bytesToNumber = ( bytes : Uint8Array ) => hexToNumber ( toHex ( bytes ) ) ;
@@ -166,6 +166,9 @@ export function privateKeyTweakAdd(
166
166
assertBytes ( privateKey , 32 ) ;
167
167
assertBytes ( tweak , 32 ) ;
168
168
let bn = bytesToNumber ( tweak ) ;
169
+ if ( bn === 0n ) {
170
+ throw new Error ( "Tweak must not be zero" ) ;
171
+ }
169
172
if ( bn >= ORDER ) {
170
173
throw new Error ( "Tweak bigger than curve order" ) ;
171
174
}
@@ -231,6 +234,9 @@ export function publicKeyTweakAdd(
231
234
assertBool ( compressed ) ;
232
235
const p1 = secp . Point . fromHex ( publicKey ) ;
233
236
const p2 = secp . Point . fromPrivateKey ( tweak ) ;
237
+ if ( p2 . equals ( secp . Point . ZERO ) ) {
238
+ throw new Error ( "Tweak must not be zero" ) ;
239
+ }
234
240
const point = p1 . add ( p2 ) ;
235
241
return output ( out , compressed ? 33 : 65 , point . toRawBytes ( compressed ) ) ;
236
242
}
@@ -245,6 +251,9 @@ export function publicKeyTweakMul(
245
251
assertBytes ( tweak , 32 ) ;
246
252
assertBool ( compressed ) ;
247
253
const bn = bytesToNumber ( tweak ) ;
254
+ if ( bn === 0n ) {
255
+ throw new Error ( "Tweak must not be zero" ) ;
256
+ }
248
257
if ( bn <= 0 || bn >= ORDER ) {
249
258
throw new Error ( "Tweak is zero or bigger than curve order" ) ;
250
259
}
@@ -259,6 +268,9 @@ export function privateKeyTweakMul(
259
268
assertBytes ( privateKey , 32 ) ;
260
269
assertBytes ( tweak , 32 ) ;
261
270
let bn = bytesToNumber ( tweak ) ;
271
+ if ( bn === 0n ) {
272
+ throw new Error ( "Tweak must not be zero" ) ;
273
+ }
262
274
if ( bn >= ORDER ) {
263
275
throw new Error ( "Tweak bigger than curve order" ) ;
264
276
}
0 commit comments