@@ -2252,8 +2252,27 @@ EC.prototype.genKeyPair = function genKeyPair(options) {
2252
2252
}
2253
2253
} ;
2254
2254
2255
- EC . prototype . _truncateToN = function _truncateToN ( msg , truncOnly ) {
2256
- var delta = msg . byteLength ( ) * 8 - this . n . bitLength ( ) ;
2255
+ EC . prototype . _truncateToN = function _truncateToN ( msg , truncOnly , bitLength ) {
2256
+ var byteLength ;
2257
+ if ( BN . isBN ( msg ) || typeof msg === 'number' ) {
2258
+ msg = new BN ( msg , 16 ) ;
2259
+ byteLength = msg . byteLength ( ) ;
2260
+ } else if ( typeof msg === 'object' ) {
2261
+ // BN assumes an array-like input and asserts length
2262
+ byteLength = msg . length ;
2263
+ msg = new BN ( msg , 16 ) ;
2264
+ } else {
2265
+ // BN converts the value to string
2266
+ var str = msg . toString ( ) ;
2267
+ // HEX encoding
2268
+ byteLength = ( str . length + 1 ) >>> 1 ;
2269
+ msg = new BN ( str , 16 ) ;
2270
+ }
2271
+ // Allow overriding
2272
+ if ( typeof bitLength !== 'number' ) {
2273
+ bitLength = byteLength * 8 ;
2274
+ }
2275
+ var delta = bitLength - this . n . bitLength ( ) ;
2257
2276
if ( delta > 0 )
2258
2277
msg = msg . ushrn ( delta ) ;
2259
2278
if ( ! truncOnly && msg . cmp ( this . n ) >= 0 )
@@ -2271,7 +2290,7 @@ EC.prototype.sign = function sign(msg, key, enc, options) {
2271
2290
options = { } ;
2272
2291
2273
2292
key = this . keyFromPrivate ( key , enc ) ;
2274
- msg = this . _truncateToN ( new BN ( msg , 16 ) ) ;
2293
+ msg = this . _truncateToN ( msg , false , options . msgBitLength ) ;
2275
2294
2276
2295
// Zero-extend key to provide enough entropy
2277
2296
var bytes = this . n . byteLength ( ) ;
@@ -2327,8 +2346,11 @@ EC.prototype.sign = function sign(msg, key, enc, options) {
2327
2346
}
2328
2347
} ;
2329
2348
2330
- EC . prototype . verify = function verify ( msg , signature , key , enc ) {
2331
- msg = this . _truncateToN ( new BN ( msg , 16 ) ) ;
2349
+ EC . prototype . verify = function verify ( msg , signature , key , enc , options ) {
2350
+ if ( ! options )
2351
+ options = { } ;
2352
+
2353
+ msg = this . _truncateToN ( msg , false , options . msgBitLength ) ;
2332
2354
key = this . keyFromPublic ( key , enc ) ;
2333
2355
signature = new Signature ( signature , 'hex' ) ;
2334
2356
@@ -2530,8 +2552,8 @@ KeyPair.prototype.sign = function sign(msg, enc, options) {
2530
2552
return this . ec . sign ( msg , this , enc , options ) ;
2531
2553
} ;
2532
2554
2533
- KeyPair . prototype . verify = function verify ( msg , signature ) {
2534
- return this . ec . verify ( msg , signature , this ) ;
2555
+ KeyPair . prototype . verify = function verify ( msg , signature , options ) {
2556
+ return this . ec . verify ( msg , signature , this , undefined , options ) ;
2535
2557
} ;
2536
2558
2537
2559
KeyPair . prototype . inspect = function inspect ( ) {
@@ -8867,7 +8889,7 @@ utils.encode = function encode(arr, enc) {
8867
8889
} , { } ] , 35 :[ function ( require , module , exports ) {
8868
8890
module . exports = {
8869
8891
"name" : "elliptic" ,
8870
- "version" : "6.5.7 " ,
8892
+ "version" : "6.6.0 " ,
8871
8893
"description" : "EC cryptography" ,
8872
8894
"main" : "lib/elliptic.js" ,
8873
8895
"files" : [
0 commit comments