@@ -23,6 +23,7 @@ function mod(a: bigint, b: bigint = secp.CURVE.P): bigint {
23
23
const result = a % b ;
24
24
return result >= 0 ? result : b + result ;
25
25
}
26
+ const ORDER = secp . CURVE . n ;
26
27
27
28
type Output = Uint8Array | ( ( len : number ) => Uint8Array ) ;
28
29
interface Signature {
@@ -146,7 +147,7 @@ export function ecdsaVerify(
146
147
assertBytes ( signature , 64 ) ;
147
148
const r = bytesToNumber ( signature . slice ( 0 , 32 ) ) ;
148
149
const s = bytesToNumber ( signature . slice ( 32 , 64 ) ) ;
149
- if ( r >= secp . CURVE . n || s >= secp . CURVE . n ) {
150
+ if ( r >= ORDER || s >= ORDER ) {
150
151
throw new Error ( "Cannot parse signature" ) ;
151
152
}
152
153
let sig ;
@@ -165,12 +166,12 @@ export function privateKeyTweakAdd(
165
166
assertBytes ( privateKey , 32 ) ;
166
167
assertBytes ( tweak , 32 ) ;
167
168
let bn = bytesToNumber ( tweak ) ;
168
- if ( bn >= secp . CURVE . n ) {
169
+ if ( bn >= ORDER ) {
169
170
throw new Error ( "Tweak bigger than curve order" ) ;
170
171
}
171
172
bn += bytesToNumber ( privateKey ) ;
172
- if ( bn >= secp . CURVE . n ) {
173
- bn -= secp . CURVE . n ;
173
+ if ( bn >= ORDER ) {
174
+ bn -= ORDER ;
174
175
}
175
176
if ( bn === 0n ) {
176
177
throw new Error (
@@ -183,7 +184,7 @@ export function privateKeyTweakAdd(
183
184
184
185
export function privateKeyNegate ( privateKey : Uint8Array ) : Uint8Array {
185
186
assertBytes ( privateKey , 32 ) ;
186
- const bn = mod ( - bytesToNumber ( privateKey ) , secp . CURVE . n ) ;
187
+ const bn = mod ( - bytesToNumber ( privateKey ) , ORDER ) ;
187
188
privateKey . set ( hexToBytes ( numberToHex ( bn ) ) ) ;
188
189
return privateKey ;
189
190
}
@@ -244,7 +245,7 @@ export function publicKeyTweakMul(
244
245
assertBytes ( tweak , 32 ) ;
245
246
assertBool ( compressed ) ;
246
247
const bn = bytesToNumber ( tweak ) ;
247
- if ( bn <= 0 || bn >= secp . CURVE . n ) {
248
+ if ( bn <= 0 || bn >= ORDER ) {
248
249
throw new Error ( "Tweak is zero or bigger than curve order" ) ;
249
250
}
250
251
const point = secp . Point . fromHex ( publicKey ) . multiply ( bn ) ;
@@ -258,12 +259,12 @@ export function privateKeyTweakMul(
258
259
assertBytes ( privateKey , 32 ) ;
259
260
assertBytes ( tweak , 32 ) ;
260
261
let bn = bytesToNumber ( tweak ) ;
261
- if ( bn >= secp . CURVE . n ) {
262
+ if ( bn >= ORDER ) {
262
263
throw new Error ( "Tweak bigger than curve order" ) ;
263
264
}
264
- bn = mod ( bn * bytesToNumber ( privateKey ) , secp . CURVE . n ) ;
265
- if ( bn >= secp . CURVE . n ) {
266
- bn -= secp . CURVE . n ;
265
+ bn = mod ( bn * bytesToNumber ( privateKey ) , ORDER ) ;
266
+ if ( bn >= ORDER ) {
267
+ bn -= ORDER ;
267
268
}
268
269
if ( bn === 0n ) {
269
270
throw new Error (
@@ -296,8 +297,8 @@ export function signatureImport(
296
297
297
298
export function signatureNormalize ( signature : Uint8Array ) : Uint8Array {
298
299
const res = getSignature ( signature ) ;
299
- if ( res . s > secp . CURVE . n / 2n ) {
300
- signature . set ( numberToBytes ( secp . CURVE . n - res . s ) , 32 ) ;
300
+ if ( res . s > ORDER / 2n ) {
301
+ signature . set ( numberToBytes ( ORDER - res . s ) , 32 ) ;
301
302
}
302
303
return signature ;
303
304
}
0 commit comments