@@ -9,7 +9,7 @@ module.exports = BigNum;
9
9
10
10
BigNum . conditionArgs = function ( num , base ) {
11
11
if ( typeof num !== 'string' ) num = num . toString ( base || 10 ) ;
12
-
12
+
13
13
if ( num . match ( / e \+ / ) ) { // positive exponent
14
14
if ( ! Number ( num ) . toString ( ) . match ( / e \+ / ) ) {
15
15
return {
@@ -23,7 +23,7 @@ BigNum.conditionArgs = function(num, base) {
23
23
. replace ( / ^ 0 / , '' ) ;
24
24
var i = n . length - n . indexOf ( '.' ) ;
25
25
n = n . replace ( / \. / , '' ) ;
26
-
26
+
27
27
for ( ; i <= pow ; i ++ ) n += '0' ;
28
28
return {
29
29
num : n ,
@@ -116,7 +116,7 @@ BigNum.prototype.powm = function (num, mod) {
116
116
else if ( mod instanceof BigNum ) {
117
117
m = mod ;
118
118
}
119
-
119
+
120
120
if ( ( typeof num ) === 'number' ) {
121
121
return this . upowm ( num , m ) ;
122
122
}
@@ -131,14 +131,14 @@ BigNum.prototype.powm = function (num, mod) {
131
131
132
132
BigNum . prototype . mod = function ( num , mod ) {
133
133
var m , res ;
134
-
134
+
135
135
if ( ( typeof mod ) === 'number' || ( typeof mod ) === 'string' ) {
136
136
m = BigNum ( mod ) ;
137
137
}
138
138
else if ( mod instanceof BigNum ) {
139
139
m = mod ;
140
140
}
141
-
141
+
142
142
if ( ( typeof num ) === 'number' ) {
143
143
return this . umod ( num , m ) ;
144
144
}
@@ -307,19 +307,19 @@ BigNum.prototype.nextPrime = function () {
307
307
308
308
BigNum . fromBuffer = function ( buf , opts ) {
309
309
if ( ! opts ) opts = { } ;
310
-
310
+
311
311
var endian = { 1 : 'big' , '-1' : 'little' } [ opts . endian ]
312
312
|| opts . endian || 'big'
313
313
;
314
-
314
+
315
315
var size = opts . size || 1 ;
316
-
316
+
317
317
if ( buf . length % size !== 0 ) {
318
318
throw new RangeError ( 'Buffer length (' + buf . length + ')'
319
319
+ ' must be a multiple of size (' + size + ')'
320
320
) ;
321
321
}
322
-
322
+
323
323
var hex = [ ] ;
324
324
for ( var i = 0 ; i < buf . length ; i += size ) {
325
325
var chunk = [ ] ;
@@ -328,36 +328,36 @@ BigNum.fromBuffer = function (buf, opts) {
328
328
i + ( endian === 'big' ? j : ( size - j - 1 ) )
329
329
] ) ;
330
330
}
331
-
331
+
332
332
hex . push ( chunk
333
333
. map ( function ( c ) {
334
334
return ( c < 16 ? '0' : '' ) + c . toString ( 16 ) ;
335
335
} )
336
336
. join ( '' )
337
337
) ;
338
338
}
339
-
339
+
340
340
return BigNum ( hex . join ( '' ) , 16 ) ;
341
341
} ;
342
342
343
343
BigNum . prototype . toBuffer = function ( opts ) {
344
344
if ( typeof opts === 'string' ) {
345
345
if ( opts !== 'mpint' ) return 'Unsupported Buffer representation' ;
346
-
346
+
347
347
var abs = this . abs ( ) ;
348
348
var buf = abs . toBuffer ( { size : 1 , endian : 'big' } ) ;
349
349
var len = buf . length === 1 && buf [ 0 ] === 0 ? 0 : buf . length ;
350
350
if ( buf [ 0 ] & 0x80 ) len ++ ;
351
-
351
+
352
352
var ret = new Buffer ( 4 + len ) ;
353
353
if ( len > 0 ) buf . copy ( ret , 4 + ( buf [ 0 ] & 0x80 ? 1 : 0 ) ) ;
354
354
if ( buf [ 0 ] & 0x80 ) ret [ 4 ] = 0 ;
355
-
355
+
356
356
ret [ 0 ] = len & ( 0xff << 24 ) ;
357
357
ret [ 1 ] = len & ( 0xff << 16 ) ;
358
358
ret [ 2 ] = len & ( 0xff << 8 ) ;
359
359
ret [ 3 ] = len & ( 0xff << 0 ) ;
360
-
360
+
361
361
// two's compliment for negative integers:
362
362
var isNeg = this . lt ( 0 ) ;
363
363
if ( isNeg ) {
@@ -367,49 +367,49 @@ BigNum.prototype.toBuffer = function (opts) {
367
367
}
368
368
ret [ 4 ] = ( ret [ 4 ] & 0x7f ) | ( isNeg ? 0x80 : 0 ) ;
369
369
if ( isNeg ) ret [ ret . length - 1 ] ++ ;
370
-
370
+
371
371
return ret ;
372
372
}
373
-
373
+
374
374
if ( ! opts ) opts = { } ;
375
-
375
+
376
376
var endian = { 1 : 'big' , '-1' : 'little' } [ opts . endian ]
377
377
|| opts . endian || 'big'
378
378
;
379
379
var size = opts . size || 1 ;
380
-
380
+
381
381
var hex = this . toString ( 16 ) ;
382
382
if ( hex . charAt ( 0 ) === '-' ) throw new Error (
383
383
'converting negative numbers to Buffers not supported yet'
384
384
) ;
385
-
385
+
386
386
var len = Math . ceil ( hex . length / ( 2 * size ) ) * size ;
387
387
var buf = new Buffer ( len ) ;
388
-
388
+
389
389
// zero-pad the hex string so the chunks are all `size` long
390
390
while ( hex . length < 2 * len ) hex = '0' + hex ;
391
-
391
+
392
392
var hx = hex
393
393
. split ( new RegExp ( '(.{' + ( 2 * size ) + '})' ) )
394
394
. filter ( function ( s ) { return s . length > 0 } )
395
395
;
396
-
396
+
397
397
hx . forEach ( function ( chunk , i ) {
398
398
for ( var j = 0 ; j < size ; j ++ ) {
399
399
var ix = i * size + ( endian === 'big' ? j : size - j - 1 ) ;
400
400
buf [ ix ] = parseInt ( chunk . slice ( j * 2 , j * 2 + 2 ) , 16 ) ;
401
401
}
402
402
} ) ;
403
-
403
+
404
404
return buf ;
405
405
} ;
406
406
407
407
Object . keys ( BigNum . prototype ) . forEach ( function ( name ) {
408
408
if ( name === 'inspect' || name === 'toString' ) return ;
409
-
409
+
410
410
BigNum [ name ] = function ( num ) {
411
411
var args = [ ] . slice . call ( arguments , 1 ) ;
412
-
412
+
413
413
if ( num instanceof BigNum ) {
414
414
return num [ name ] . apply ( num , args ) ;
415
415
}
0 commit comments