Skip to content

Commit

Permalink
Negative values are always considered not prime.
Browse files Browse the repository at this point in the history
Fixes jruby/jruby#4193.
Fixes #107.
  • Loading branch information
headius committed Sep 30, 2016
1 parent 4458259 commit 2678bab
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/main/java/org/jruby/ext/openssl/BN.java
Expand Up @@ -623,9 +623,14 @@ public RubyFixnum num_bits_set(final ThreadContext context) {
public IRubyObject prime_p(IRubyObject[] args) {
final Ruby runtime = getRuntime();
int argc = Arity.checkArgumentCount(runtime, args, 0, 1);

// negative numbers are always considered non-prime
if (this.value.signum() < 0) return runtime.getFalse();

int certainty = argc == 0 ? DEFAULT_CERTAINTY : RubyNumeric.fix2int(args[0]);

// BigInteger#isProbablePrime will actually limit checks to a maximum of 50,
// depending on bit count.
int certainty = argc == 0 ? DEFAULT_CERTAINTY : RubyNumeric.fix2int(args[0]);
return runtime.newBoolean(this.value.isProbablePrime(certainty));
}

Expand All @@ -635,9 +640,14 @@ public IRubyObject prime_p(IRubyObject[] args) {
public IRubyObject prime_fasttest_p(IRubyObject[] args) {
final Ruby runtime = getRuntime();
int argc = Arity.checkArgumentCount(runtime, args, 0, 2);

// negative numbers are always considered non-prime
if (this.value.signum() < 0) return runtime.getFalse();

int certainty = argc == 0 ? DEFAULT_CERTAINTY : RubyNumeric.fix2int(args[0]);

// BigInteger#isProbablePrime will actually limit checks to a maximum of 50,
// depending on bit count.
int certainty = argc == 0 ? DEFAULT_CERTAINTY : RubyNumeric.fix2int(args[0]);
return runtime.newBoolean(this.value.isProbablePrime(certainty));
}

Expand Down

0 comments on commit 2678bab

Please sign in to comment.