use of org.jruby.RubyBignum in project jruby-openssl by jruby.
the class BN method exp.
@JRubyMethod(name = "**")
public BN exp(final ThreadContext context, IRubyObject other) {
// somewhat strangely, BigInteger takes int rather than BigInteger
// as the argument to pow. so we'll have to narrow the value, and
// raise an exception if data would be lost. (on the other hand, an
// exponent even approaching Integer.MAX_VALUE would be silly big, and
// the value would take a very, very long time to calculate.)
// we'll check for values < 0 (illegal) while we're at it
int exp = -1;
if (other instanceof RubyInteger) {
long val = ((RubyInteger) other).getLongValue();
if (val >= 0 && val <= Integer.MAX_VALUE) {
exp = (int) val;
} else if (other instanceof RubyBignum) {
// inherently too big
throw newBNError(context.runtime, "invalid exponent");
}
}
if (exp == -1) {
if (!(other instanceof BN)) {
throw context.runtime.newTypeError("Cannot convert into " + other.getMetaClass().getName());
}
BigInteger val = ((BN) other).value;
if (val.compareTo(BigInteger.ZERO) < 0 || val.compareTo(MAX_INT) > 0) {
throw newBNError(context.runtime, "invalid exponent");
}
exp = val.intValue();
}
try {
return newBN(context.runtime, value.pow(exp));
} catch (ArithmeticException e) {
// shouldn't happen, we've already checked for < 0
throw newBNError(context.runtime, "invalid exponent");
}
}
Aggregations