@@ -173,7 +173,7 @@ public synchronized IRubyObject initialize_copy(final IRubyObject that) {
173
173
@ JRubyMethod (name = "copy" )
174
174
public IRubyObject copy (IRubyObject other ) {
175
175
if (this != other ) {
176
- this .value = getBigInteger (other );
176
+ this .value = asBigInteger (other );
177
177
}
178
178
return this ;
179
179
}
@@ -271,8 +271,23 @@ public IRubyObject inspect() {
271
271
return ObjectSupport .inspect (this , Collections .EMPTY_LIST );
272
272
}
273
273
274
+ @ Override
275
+ public boolean equals (Object other ) {
276
+ return (other instanceof BN ) ? this .value .equals (((BN ) other ).value ) : false ;
277
+ }
278
+
279
+ @ Override
280
+ public int hashCode () {
281
+ return 997 * value .hashCode ();
282
+ }
283
+
284
+ @ JRubyMethod (name = "hash" )
285
+ public RubyInteger hash (final ThreadContext context ) {
286
+ return context .runtime .newFixnum (hashCode ());
287
+ }
288
+
274
289
@ JRubyMethod (name = "to_i" )
275
- public IRubyObject to_i () {
290
+ public RubyInteger to_i () {
276
291
if ( value .compareTo ( MAX_LONG ) > 0 || value .compareTo ( MIN_LONG ) < 0 ) {
277
292
return RubyBignum .newBignum (getRuntime (), value );
278
293
}
@@ -321,17 +336,17 @@ public RubyBoolean odd_p(final ThreadContext context) {
321
336
322
337
@ JRubyMethod (name ={"cmp" , "<=>" })
323
338
public IRubyObject cmp (final ThreadContext context , IRubyObject other ) {
324
- return context .runtime .newFixnum ( value .compareTo ( getBigInteger (other ) ) );
339
+ return context .runtime .newFixnum ( value .compareTo ( asBigInteger (other ) ) );
325
340
}
326
341
327
342
@ JRubyMethod (name ="ucmp" )
328
343
public IRubyObject ucmp (final ThreadContext context , IRubyObject other ) {
329
- return context .runtime .newFixnum ( value .abs ().compareTo ( getBigInteger (other ).abs () ) );
344
+ return context .runtime .newFixnum ( value .abs ().compareTo ( asBigInteger (other ).abs () ) );
330
345
}
331
346
332
347
@ JRubyMethod (name ={"eql?" , "==" , "===" })
333
348
public RubyBoolean eql_p (final ThreadContext context , IRubyObject other ) {
334
- return context .runtime .newBoolean ( value .equals ( getBigInteger (other ) ) );
349
+ return context .runtime .newBoolean ( value .equals ( asBigInteger (other ) ) );
335
350
}
336
351
337
352
@ JRubyMethod (name ="sqr" )
@@ -347,23 +362,23 @@ public BN not(final ThreadContext context) {
347
362
348
363
@ JRubyMethod (name ="+" )
349
364
public BN add (final ThreadContext context , IRubyObject other ) {
350
- return newBN (context .runtime , value .add (getBigInteger (other )));
365
+ return newBN (context .runtime , value .add (asBigInteger (other )));
351
366
}
352
367
353
368
@ JRubyMethod (name ="-" )
354
369
public BN sub (final ThreadContext context , IRubyObject other ) {
355
- return newBN (context .runtime , value .subtract (getBigInteger (other )));
370
+ return newBN (context .runtime , value .subtract (asBigInteger (other )));
356
371
}
357
372
358
373
@ JRubyMethod (name ="*" )
359
374
public BN mul (final ThreadContext context , IRubyObject other ) {
360
- return newBN (context .runtime , value .multiply (getBigInteger (other )));
375
+ return newBN (context .runtime , value .multiply (asBigInteger (other )));
361
376
}
362
377
363
378
@ JRubyMethod (name ="%" )
364
379
public BN mod (final ThreadContext context , IRubyObject other ) {
365
380
try {
366
- return newBN (context .runtime , value .mod (getBigInteger (other )));
381
+ return newBN (context .runtime , value .mod (asBigInteger (other )));
367
382
}
368
383
catch (ArithmeticException e ) {
369
384
throw context .runtime .newZeroDivisionError ();
@@ -374,7 +389,7 @@ public BN mod(final ThreadContext context, IRubyObject other) {
374
389
public IRubyObject div (final ThreadContext context , IRubyObject other ) {
375
390
final Ruby runtime = context .runtime ;
376
391
try {
377
- BigInteger [] result = value .divideAndRemainder (getBigInteger (other ));
392
+ BigInteger [] result = value .divideAndRemainder (asBigInteger (other ));
378
393
return runtime .newArray (newBN (runtime , result [0 ]), newBN (runtime , result [1 ]));
379
394
}
380
395
catch (ArithmeticException e ) {
@@ -384,17 +399,17 @@ public IRubyObject div(final ThreadContext context, IRubyObject other) {
384
399
385
400
@ JRubyMethod (name ="&" )
386
401
public BN and (final ThreadContext context , IRubyObject other ) {
387
- return newBN (context .runtime , value .and (getBigInteger (other )));
402
+ return newBN (context .runtime , value .and (asBigInteger (other )));
388
403
}
389
404
390
405
@ JRubyMethod (name ="|" )
391
406
public BN or (final ThreadContext context , IRubyObject other ) {
392
- return newBN (context .runtime , value .or (getBigInteger (other )));
407
+ return newBN (context .runtime , value .or (asBigInteger (other )));
393
408
}
394
409
395
410
@ JRubyMethod (name ="^" )
396
411
public BN xor (final ThreadContext context , IRubyObject other ) {
397
- return newBN (context .runtime , value .xor (getBigInteger (other )));
412
+ return newBN (context .runtime , value .xor (asBigInteger (other )));
398
413
}
399
414
400
415
@ JRubyMethod (name ="**" )
@@ -439,13 +454,13 @@ else if ( other instanceof RubyBignum ) { // inherently too big
439
454
440
455
@ JRubyMethod (name ="gcd" )
441
456
public BN gcd (final ThreadContext context , IRubyObject other ) {
442
- return newBN (context .runtime , value .gcd (getBigInteger (other )));
457
+ return newBN (context .runtime , value .gcd (asBigInteger (other )));
443
458
}
444
459
445
460
@ JRubyMethod (name ="mod_sqr" )
446
461
public BN mod_sqr (final ThreadContext context , IRubyObject other ) {
447
462
try {
448
- return newBN (context .runtime , value .modPow (TWO , getBigInteger (other )));
463
+ return newBN (context .runtime , value .modPow (TWO , asBigInteger (other )));
449
464
}
450
465
catch (ArithmeticException e ) {
451
466
throw context .runtime .newZeroDivisionError ();
@@ -455,7 +470,7 @@ public BN mod_sqr(final ThreadContext context, IRubyObject other) {
455
470
@ JRubyMethod (name ="mod_inverse" )
456
471
public BN mod_inverse (final ThreadContext context , IRubyObject other ) {
457
472
try {
458
- return newBN (context .runtime , value .modInverse (getBigInteger (other )));
473
+ return newBN (context .runtime , value .modInverse (asBigInteger (other )));
459
474
}
460
475
catch (ArithmeticException e ) {
461
476
throw context .runtime .newZeroDivisionError ();
@@ -465,7 +480,7 @@ public BN mod_inverse(final ThreadContext context, IRubyObject other) {
465
480
@ JRubyMethod (name ="mod_add" )
466
481
public BN mod_add (final ThreadContext context , IRubyObject other , IRubyObject mod ) {
467
482
try {
468
- return newBN (context .runtime , value .add (getBigInteger (other )).mod (getBigInteger (mod )));
483
+ return newBN (context .runtime , value .add (asBigInteger (other )).mod (asBigInteger (mod )));
469
484
}
470
485
catch (ArithmeticException e ) {
471
486
throw context .runtime .newZeroDivisionError ();
@@ -475,7 +490,7 @@ public BN mod_add(final ThreadContext context, IRubyObject other, IRubyObject mo
475
490
@ JRubyMethod (name ="mod_sub" )
476
491
public BN mod_sub (final ThreadContext context , IRubyObject other , IRubyObject mod ) {
477
492
try {
478
- return newBN (context .runtime , value .subtract (getBigInteger (other )).mod (getBigInteger (mod )));
493
+ return newBN (context .runtime , value .subtract (asBigInteger (other )).mod (asBigInteger (mod )));
479
494
}
480
495
catch (ArithmeticException e ) {
481
496
throw context .runtime .newZeroDivisionError ();
@@ -485,7 +500,7 @@ public BN mod_sub(final ThreadContext context, IRubyObject other, IRubyObject mo
485
500
@ JRubyMethod (name ="mod_mul" )
486
501
public BN mod_mul (final ThreadContext context , IRubyObject other , IRubyObject mod ) {
487
502
try {
488
- return newBN (context .runtime , value .multiply (getBigInteger (other )).mod (getBigInteger (mod )));
503
+ return newBN (context .runtime , value .multiply (asBigInteger (other )).mod (asBigInteger (mod )));
489
504
}
490
505
catch (ArithmeticException e ) {
491
506
throw context .runtime .newZeroDivisionError ();
@@ -495,7 +510,7 @@ public BN mod_mul(final ThreadContext context, IRubyObject other, IRubyObject mo
495
510
@ JRubyMethod (name ="mod_exp" )
496
511
public BN mod_exp (final ThreadContext context , IRubyObject other , IRubyObject mod ) {
497
512
try {
498
- return newBN (context .runtime , value .modPow (getBigInteger (other ), getBigInteger (mod )));
513
+ return newBN (context .runtime , value .modPow (asBigInteger (other ), asBigInteger (mod )));
499
514
}
500
515
catch (ArithmeticException e ) {
501
516
throw context .runtime .newZeroDivisionError ();
@@ -657,8 +672,8 @@ public static IRubyObject generate_prime(IRubyObject recv, IRubyObject[] args) {
657
672
int argc = Arity .checkArgumentCount (runtime , args , 1 , 4 );
658
673
int bits = RubyNumeric .num2int (args [0 ]);
659
674
boolean safe = argc > 1 ? args [1 ] != runtime .getFalse () : true ;
660
- BigInteger add = argc > 2 ? getBigInteger (args [2 ]) : null ;
661
- BigInteger rem = argc > 3 ? getBigInteger (args [3 ]) : null ;
675
+ BigInteger add = argc > 2 ? asBigInteger (args [2 ]) : null ;
676
+ BigInteger rem = argc > 3 ? asBigInteger (args [3 ]) : null ;
662
677
if (bits < 3 ) {
663
678
if (safe ) throw runtime .newArgumentError ("bits < 3" );
664
679
if (bits < 2 ) throw runtime .newArgumentError ("bits < 2" );
@@ -794,12 +809,12 @@ public static BigInteger getRandomBI(int bits, int top, boolean bottom, Random r
794
809
795
810
@ JRubyMethod (name = "rand_range" , meta = true )
796
811
public static IRubyObject rand_range (IRubyObject recv , IRubyObject arg ) {
797
- return randomValueInRange (recv .getRuntime (), getBigInteger (arg ), getSecureRandom ());
812
+ return randomValueInRange (recv .getRuntime (), asBigInteger (arg ), getSecureRandom ());
798
813
}
799
814
800
815
@ JRubyMethod (name = "pseudo_rand_range" , meta = true )
801
816
public static IRubyObject pseudo_rand_range (IRubyObject recv , IRubyObject arg ) {
802
- return randomValueInRange (recv .getRuntime (), getBigInteger (arg ), getRandom ());
817
+ return randomValueInRange (recv .getRuntime (), asBigInteger (arg ), getRandom ());
803
818
}
804
819
805
820
private static BN randomValueInRange (Ruby runtime , BigInteger limit , Random random ) {
@@ -850,7 +865,7 @@ public static RaiseException newBNError(Ruby runtime, String message) {
850
865
return new RaiseException (runtime , runtime .getModule ("OpenSSL" ).getClass ("BNError" ), message , true );
851
866
}
852
867
853
- public static BigInteger getBigInteger (final IRubyObject arg ) {
868
+ public static BigInteger asBigInteger (final IRubyObject arg ) {
854
869
if ( arg .isNil () ) return null ;
855
870
856
871
if ( arg instanceof RubyInteger ) {
@@ -862,6 +877,15 @@ public static BigInteger getBigInteger(final IRubyObject arg) {
862
877
throw arg .getRuntime ().newTypeError ("Cannot convert into OpenSSL::BN" );
863
878
}
864
879
880
+ public static BigInteger asBigInteger (final BN arg ) {
881
+ return arg .isNil () ? null : arg .value ;
882
+ }
883
+
884
+ @ Deprecated
885
+ public static BigInteger getBigInteger (final IRubyObject arg ) {
886
+ return asBigInteger (arg );
887
+ }
888
+
865
889
@ Override
866
890
public Object toJava (Class target ) {
867
891
if ( target .isAssignableFrom (BigInteger .class ) || target == Number .class ) return value ;
0 commit comments