@@ -2,8 +2,8 @@ import { sha256 } from "@noble/hashes/sha256";
22import * as secp from "./secp256k1" ;
33import { assertBool , assertBytes , hexToBytes , toHex } from "./utils" ;
44
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 the npm package `secp256k1` via noble-secp256k1
77
88// Copy-paste from secp256k1, maybe export it?
99const bytesToNumber = ( bytes : Uint8Array ) => hexToNumber ( toHex ( bytes ) ) ;
@@ -116,7 +116,6 @@ export function ecdsaSign(
116116 }
117117 const [ signature , recid ] = secp . signSync ( msgHash , privateKey , {
118118 recovered : true ,
119- canonical : true ,
120119 der : false
121120 } ) ;
122121 return { signature : output ( out , 64 , signature ) , recid } ;
@@ -150,13 +149,14 @@ export function ecdsaVerify(
150149 if ( r >= ORDER || s >= ORDER ) {
151150 throw new Error ( "Cannot parse signature" ) ;
152151 }
152+ const pub = secp . Point . fromHex ( publicKey ) ; // should not throw error
153153 let sig ;
154154 try {
155155 sig = getSignature ( signature ) ;
156156 } catch ( error ) {
157157 return false ;
158158 }
159- return secp . verify ( sig , msgHash , publicKey ) ;
159+ return secp . verify ( sig , msgHash , pub ) ;
160160}
161161
162162export function privateKeyTweakAdd (
@@ -234,10 +234,10 @@ export function publicKeyTweakAdd(
234234 assertBool ( compressed ) ;
235235 const p1 = secp . Point . fromHex ( publicKey ) ;
236236 const p2 = secp . Point . fromPrivateKey ( tweak ) ;
237- if ( p2 . equals ( secp . Point . ZERO ) ) {
237+ const point = p1 . add ( p2 ) ;
238+ if ( p2 . equals ( secp . Point . ZERO ) || point . equals ( secp . Point . ZERO ) ) {
238239 throw new Error ( "Tweak must not be zero" ) ;
239240 }
240- const point = p1 . add ( p2 ) ;
241241 return output ( out , compressed ? 33 : 65 , point . toRawBytes ( compressed ) ) ;
242242}
243243
@@ -254,7 +254,7 @@ export function publicKeyTweakMul(
254254 if ( bn === 0n ) {
255255 throw new Error ( "Tweak must not be zero" ) ;
256256 }
257- if ( bn <= 0 || bn >= ORDER ) {
257+ if ( bn <= 1 || bn >= ORDER ) {
258258 throw new Error ( "Tweak is zero or bigger than curve order" ) ;
259259 }
260260 const point = secp . Point . fromHex ( publicKey ) . multiply ( bn ) ;
@@ -267,23 +267,17 @@ export function privateKeyTweakMul(
267267) : Uint8Array {
268268 assertBytes ( privateKey , 32 ) ;
269269 assertBytes ( tweak , 32 ) ;
270- let bn = bytesToNumber ( tweak ) ;
271- if ( bn === 0n ) {
272- throw new Error ( "Tweak must not be zero" ) ;
273- }
274- if ( bn >= ORDER ) {
275- throw new Error ( "Tweak bigger than curve order" ) ;
276- }
277- bn = mod ( bn * bytesToNumber ( privateKey ) , ORDER ) ;
278- if ( bn >= ORDER ) {
279- bn -= ORDER ;
270+ const bn = bytesToNumber ( tweak ) ;
271+ if ( bn <= 1 || bn >= ORDER ) {
272+ throw new Error ( "Tweak is zero or bigger than curve order" ) ;
280273 }
281- if ( bn === 0n ) {
274+ const res = mod ( bn * bytesToNumber ( privateKey ) , ORDER ) ;
275+ if ( res === 0n ) {
282276 throw new Error (
283277 "The tweak was out of range or the resulted private key is invalid"
284278 ) ;
285279 }
286- privateKey . set ( hexToBytes ( numberToHex ( bn ) ) ) ;
280+ privateKey . set ( hexToBytes ( numberToHex ( res ) ) ) ;
287281 return privateKey ;
288282}
289283// internal -> DER
0 commit comments