Search in sources :

Example 36 with JRubyMethod

use of org.jruby.anno.JRubyMethod in project jruby-openssl by jruby.

the class BN method mask_bits.

/**
 * Truncates value to n bits
 */
@JRubyMethod(name = "mask_bits!")
public synchronized IRubyObject mask_bits(IRubyObject n) {
    // evil mutable BN
    int pos = RubyNumeric.num2int(n);
    if (pos < 0)
        throw newBNError(getRuntime(), "invalid pos");
    BigInteger oldValue = this.value;
    // TODO: cache 2 ** n values?
    if (oldValue.signum() >= 0) {
        if (oldValue.bitLength() < pos)
            throw newBNError(getRuntime(), "invalid pos");
        this.value = oldValue.mod(TWO.pow(pos));
    } else {
        BigInteger absValue = oldValue.abs();
        if (absValue.bitLength() < pos)
            throw newBNError(getRuntime(), "invalid pos");
        this.value = absValue.mod(TWO.pow(pos)).negate();
    }
    return this;
}
Also used : BigInteger(java.math.BigInteger) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 37 with JRubyMethod

use of org.jruby.anno.JRubyMethod in project jruby-openssl by jruby.

the class BN method prime_p.

// note that there is a bug in the MRI version, in argument handling,
// so apparently no one ever calls this...
@JRubyMethod(name = "prime?", rest = true)
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]);
    // depending on bit count.
    return runtime.newBoolean(this.value.isProbablePrime(certainty));
}
Also used : Ruby(org.jruby.Ruby) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 38 with JRubyMethod

use of org.jruby.anno.JRubyMethod in project jruby-openssl by jruby.

the class BN method prime_fasttest_p.

// NOTE: BigInteger doesn't supply this, so right now this is
// ... (essentially) the same as prime?
@JRubyMethod(name = "prime_fasttest?", rest = true)
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]);
    // depending on bit count.
    return runtime.newBoolean(this.value.isProbablePrime(certainty));
}
Also used : Ruby(org.jruby.Ruby) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 39 with JRubyMethod

use of org.jruby.anno.JRubyMethod in project jruby-openssl by jruby.

the class BN method initialize.

@JRubyMethod(name = "initialize", required = 1, optional = 1, visibility = Visibility.PRIVATE)
public IRubyObject initialize(final ThreadContext context, final IRubyObject[] args) {
    final Ruby runtime = context.runtime;
    if (this.value != BigInteger.ZERO) {
        // already initialized
        throw newBNError(runtime, "illegal initialization");
    }
    int argc = Arity.checkArgumentCount(runtime, args, 1, 2);
    int base = argc == 2 ? RubyNumeric.num2int(args[1]) : 10;
    final RubyString str = args[0].asString();
    switch(base) {
        case 0:
            this.value = initBigIntegerMPI(runtime, str);
            break;
        case 2:
            // this seems wrong to me, but is the behavior of the
            // MRI implementation. rather than interpreting the string
            // as ASCII-encoded binary digits, the raw binary value of
            // the string is used instead. the value is always interpreted
            // as positive, hence the use of the signum version of the BI
            // constructor here:
            this.value = new BigInteger(1, str.getBytes());
            break;
        case 10:
        case 16:
            // here, the ASCII-encoded decimal or hex string is used
            this.value = initBigIntegerBase(runtime, str, base);
            break;
        default:
            throw runtime.newArgumentError("illegal radix: " + base);
    }
    return this;
}
Also used : RubyString(org.jruby.RubyString) BigInteger(java.math.BigInteger) Ruby(org.jruby.Ruby) JRubyMethod(org.jruby.anno.JRubyMethod)

Example 40 with JRubyMethod

use of org.jruby.anno.JRubyMethod 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");
    }
}
Also used : RubyInteger(org.jruby.RubyInteger) BigInteger(java.math.BigInteger) RubyBignum(org.jruby.RubyBignum) JRubyMethod(org.jruby.anno.JRubyMethod)

Aggregations

JRubyMethod (org.jruby.anno.JRubyMethod)304 Ruby (org.jruby.Ruby)157 RubyString (org.jruby.RubyString)83 IRubyObject (org.jruby.runtime.builtin.IRubyObject)77 IOException (java.io.IOException)49 RubyArray (org.jruby.RubyArray)34 Node (org.w3c.dom.Node)30 RaiseException (org.jruby.exceptions.RaiseException)27 BigInteger (java.math.BigInteger)24 GeneralSecurityException (java.security.GeneralSecurityException)24 NokogiriHelpers.rubyStringToString (nokogiri.internals.NokogiriHelpers.rubyStringToString)24 Document (org.w3c.dom.Document)20 ByteList (org.jruby.util.ByteList)19 RubyClass (org.jruby.RubyClass)16 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)14 RubyFloat (org.jruby.RubyFloat)13 JRubyClass (org.jruby.anno.JRubyClass)13 NokogiriHelpers.clearCachedNode (nokogiri.internals.NokogiriHelpers.clearCachedNode)12 RubyFixnum (org.jruby.RubyFixnum)12 Element (org.w3c.dom.Element)12