@@ -2,8 +2,8 @@ import { sha256 } from "@noble/hashes/sha256";
2
2
import * as secp from "./secp256k1" ;
3
3
import { assertBool , assertBytes , hexToBytes , toHex } from "./utils" ;
4
4
5
- // Legacy compatibility layer for elliptic via noble-secp256k1
6
- // Use ` secp256k1` module directly instead
5
+ // Use `secp256k1` module directly.
6
+ // This is a legacy compatibility layer for `tiny- secp256k1` via noble-secp256k1
7
7
8
8
// Copy-paste from secp256k1, maybe export it?
9
9
const bytesToNumber = ( bytes : Uint8Array ) => hexToNumber ( toHex ( bytes ) ) ;
@@ -116,7 +116,6 @@ export function ecdsaSign(
116
116
}
117
117
const [ signature , recid ] = secp . signSync ( msgHash , privateKey , {
118
118
recovered : true ,
119
- canonical : true ,
120
119
der : false
121
120
} ) ;
122
121
return { signature : output ( out , 64 , signature ) , recid } ;
@@ -235,10 +234,10 @@ export function publicKeyTweakAdd(
235
234
assertBool ( compressed ) ;
236
235
const p1 = secp . Point . fromHex ( publicKey ) ;
237
236
const p2 = secp . Point . fromPrivateKey ( tweak ) ;
238
- if ( p2 . equals ( secp . Point . ZERO ) ) {
237
+ const point = p1 . add ( p2 ) ;
238
+ if ( p2 . equals ( secp . Point . ZERO ) || point . equals ( secp . Point . ZERO ) ) {
239
239
throw new Error ( "Tweak must not be zero" ) ;
240
240
}
241
- const point = p1 . add ( p2 ) ;
242
241
return output ( out , compressed ? 33 : 65 , point . toRawBytes ( compressed ) ) ;
243
242
}
244
243
@@ -255,7 +254,7 @@ export function publicKeyTweakMul(
255
254
if ( bn === 0n ) {
256
255
throw new Error ( "Tweak must not be zero" ) ;
257
256
}
258
- if ( bn <= 0 || bn >= ORDER ) {
257
+ if ( bn <= 1 || bn >= ORDER ) {
259
258
throw new Error ( "Tweak is zero or bigger than curve order" ) ;
260
259
}
261
260
const point = secp . Point . fromHex ( publicKey ) . multiply ( bn ) ;
@@ -269,22 +268,16 @@ export function privateKeyTweakMul(
269
268
assertBytes ( privateKey , 32 ) ;
270
269
assertBytes ( tweak , 32 ) ;
271
270
let bn = bytesToNumber ( tweak ) ;
272
- if ( bn === 0n ) {
273
- throw new Error ( "Tweak must not be zero" ) ;
274
- }
275
- if ( bn >= ORDER ) {
276
- throw new Error ( "Tweak bigger than curve order" ) ;
277
- }
278
- bn = mod ( bn * bytesToNumber ( privateKey ) , ORDER ) ;
279
- if ( bn >= ORDER ) {
280
- bn -= ORDER ;
271
+ if ( bn <= 1 || bn >= ORDER ) {
272
+ throw new Error ( "Tweak is zero or bigger than curve order" ) ;
281
273
}
282
- if ( bn === 0n ) {
274
+ const res = mod ( bn * bytesToNumber ( privateKey ) , ORDER ) ;
275
+ if ( res === 0n ) {
283
276
throw new Error (
284
277
"The tweak was out of range or the resulted private key is invalid"
285
278
) ;
286
279
}
287
- privateKey . set ( hexToBytes ( numberToHex ( bn ) ) ) ;
280
+ privateKey . set ( hexToBytes ( numberToHex ( res ) ) ) ;
288
281
return privateKey ;
289
282
}
290
283
// internal -> DER
0 commit comments