FileDocCategorySizeDatePackage
BigInt.javaAPI DocAndroid 1.5 API11550Wed May 06 22:41:04 BST 2009java.math

BigInt

public class BigInt extends Object

Fields Summary
static BigInt
dummy
static Context
defaultContext
private static final long
serialVersionUID
This is the serialVersionUID used by the sun implementation
transient int
bignum
Constructors Summary
Methods Summary
private static voidCheck(boolean success)

        if (!success) {
            StringBuilder sb = new StringBuilder("(openssl)ERR: ");
            int cnt = consumeErrors(sb);
            if (cnt > 0)
                throw new ArithmeticException(sb.toString());
        }
    
public voidadd(java.math.BigInt a)

        Check(NativeBN.BN_add(this.bignum, this.bignum, a.bignum));
    
public voidaddPositiveInt(int w)

        Check(NativeBN.BN_add_word(this.bignum, w));
    
public static java.math.BigIntaddition(java.math.BigInt a, java.math.BigInt b)

        BigInt r = newBigInt();
        Check(NativeBN.BN_add(r.bignum, a.bignum, b.bignum));
        return r;
    
public byte[]bigEndianMagnitude()

        byte[] a = NativeBN.BN_bn2bin(this.bignum, null);
        return a;
    
public byte[]bigEndianTwosComplement()

        byte[] a = NativeBN.bn2twosComp(this.bignum, null);
        return a;
    
public static java.math.BigIntbigExp(java.math.BigInt a, java.math.BigInt p, java.math.BigInt$Context t)

        // Sign of p is ignored!
        BigInt r = newBigInt();
        Check(NativeBN.BN_exp(r.bignum, a.bignum, p.bignum, getCtx(t)));
        return r;
    
public intbitLength()

        return NativeBN.bitLength(this.bignum);
    
public static intcmp(java.math.BigInt a, java.math.BigInt b)

        return NativeBN.BN_cmp(a.bignum, b.bignum);
    
public static intconsumeErrors(java.lang.StringBuilder sb)

        int cnt = 0;
        int e, reason;
        boolean first = true;
        while ((e = NativeBN.ERR_get_error()) != 0) {
            reason = e & 255;
            if (reason == 103) {
                // math.17=BigInteger divide by zero
                throw new ArithmeticException(Messages.getString("math.17")); //$NON-NLS-1$
            }
            if (reason == 108) {
                // math.19=BigInteger not invertible.
                throw new ArithmeticException(Messages.getString("math.19")); //$NON-NLS-1$
            }
            if (reason == 65) {
                throw new OutOfMemoryError();
            }
            if (!first) { sb.append(" *** "); first = false; }
            sb.append(e).append(": ");
            String s = NativeBN.ERR_error_string(e);
            sb.append(s);
            cnt++;
        }
        return cnt;
    
public java.math.BigIntcopy()

        BigInt bi = new BigInt();
        bi.putCopy(this);
        return bi;
    
public java.lang.StringdecString()

        String str = NativeBN.BN_bn2dec(this.bignum);
        return str;
    
public voiddispose()



       
        if (this.bignum != 0) {
            NativeBN.BN_free(this.bignum);
            this.bignum = 0;
        }
    
public intdivideByPositiveInt(int w)

        int rem = NativeBN.BN_div_word(this.bignum, w);
        Check(rem != -1);
        return rem;
    
public static voiddivision(java.math.BigInt dividend, java.math.BigInt divisor, java.math.BigInt$Context t, java.math.BigInt quotient, java.math.BigInt remainder)

        int quot, rem;
        if (quotient != null) {
            quotient.makeValid();
            quot = quotient.bignum;
        }
        else quot = 0;
        if (remainder != null) {
            remainder.makeValid();
            rem = remainder.bignum;
        }
        else rem = 0;
        Check(NativeBN.BN_div(quot, rem, dividend.bignum, divisor.bignum, getCtx(t)));
    
public static java.math.BigIntexp(java.math.BigInt a, int p, java.math.BigInt$Context t)

        // Sign of p is ignored!
        BigInt power = new BigInt();
        power.putLongInt(p);
        return bigExp(a, power, t);
        // OPTIONAL:
//      public int BN_sqr(BigInteger r, BigInteger a, BN_CTX ctx);
      // int BN_sqr(BIGNUM *r, const BIGNUM *a,BN_CTX *ctx);
    
protected voidfinalize()

        dispose();
    
public static java.math.BigIntgcd(java.math.BigInt a, java.math.BigInt b, java.math.BigInt$Context t)

        BigInt r = newBigInt();
        Check(NativeBN.BN_gcd(r.bignum, a.bignum, b.bignum, getCtx(t)));
        return r;
    
public static java.math.BigIntgeneratePrimeDefault(int bitLength, java.util.Random rnd, java.math.BigInt$Context t)

        BigInt r = newBigInt();
        Check(NativeBN.BN_generate_prime_ex(r.bignum, bitLength, false, 0, 0, 0));
        return r;
    
public static java.math.BigIntgeneratePrimeSafe(int bitLength, java.util.Random rnd, java.math.BigInt$Context t)

        BigInt r = newBigInt();
        Check(NativeBN.BN_generate_prime_ex(r.bignum, bitLength, true, 0, 0, 0));
        return r;
    
static intgetCtx(java.math.BigInt$Context t)

        dummy = new BigInt();
        defaultContext = dummy.new Context();
    
        return (t != null) ? t.bnctx : defaultContext.bnctx;
    
public intgetNativeBIGNUM()

        return this.bignum;
    
public java.lang.StringhexString()

        String str = NativeBN.BN_bn2hex(this.bignum);
        return str;
    
public booleanisBitSet(int n)

        return NativeBN.BN_is_bit_set(this.bignum, n);
    
public booleanisPrime(int certainty, java.util.Random rnd, java.math.BigInt$Context t)

        return NativeBN.BN_is_prime_ex(bignum, certainty, getCtx(t), 0);
    
public int[]littleEndianIntsMagnitude()

        int[] a = NativeBN.bn2litEndInts(this.bignum, null);
        return a;
    
public longlongInt()

        return NativeBN.longInt(this.bignum);
    
private voidmakeValid()

        if (this.bignum == 0) {
            this.bignum = NativeBN.BN_new();
            Check(this.bignum != 0);
        }
    
public static java.math.BigIntmodExp(java.math.BigInt a, java.math.BigInt p, java.math.BigInt m, java.math.BigInt$Context t)

        // Sign of p is ignored!
        BigInt r = newBigInt();
        Check(NativeBN.BN_mod_exp(r.bignum, a.bignum, p.bignum, m.bignum, getCtx(t)));

        // OPTIONAL:
        // int BN_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx);
        return r;
    
public static java.math.BigIntmodInverse(java.math.BigInt a, java.math.BigInt m, java.math.BigInt$Context t)

        BigInt r = newBigInt();
        Check(NativeBN.BN_mod_inverse(r.bignum, a.bignum, m.bignum, getCtx(t)));
        return r;
    
public voidmodifyBit(int n, int op)

        // op: 0 = reset; 1 = set; -1 = flip
        Check(NativeBN.modifyBit(this.bignum, n, op));
    
public static java.math.BigIntmodulus(java.math.BigInt a, java.math.BigInt m, java.math.BigInt$Context t)

        // Sign of p is ignored! ?
        BigInt r = newBigInt();
        Check(NativeBN.BN_nnmod(r.bignum, a.bignum, m.bignum, getCtx(t)));
        return r;
    
public voidmultiplyBy(java.math.BigInt a, java.math.BigInt$Context t)

        Check(NativeBN.BN_mul(this.bignum, this.bignum, a.bignum, getCtx(t)));
    
public voidmultiplyByPositiveInt(int w)

        Check(NativeBN.BN_mul_word(this.bignum, w));
    
private static java.math.BigIntnewBigInt()

        BigInt bi = new BigInt();
        bi.bignum = NativeBN.BN_new();
        Check(bi.bignum != 0);
        return bi;
    
public static java.math.BigIntproduct(java.math.BigInt a, java.math.BigInt b, java.math.BigInt$Context t)

        BigInt r = newBigInt();
        Check(NativeBN.BN_mul(r.bignum, a.bignum, b.bignum, getCtx(t)));
        return r;
    
public voidputBigEndian(byte[] a, boolean neg)

        this.makeValid();
        Check(NativeBN.BN_bin2bn(a, a.length, neg, this.bignum));
    
public voidputBigEndianTwosComplement(byte[] a)

        this.makeValid();
        Check(NativeBN.twosComp2bn(a, a.length, this.bignum));
    
public voidputCopy(java.math.BigInt from)

        this.makeValid();
        Check(NativeBN.BN_copy(this.bignum, from.bignum));
    
public voidputDecString(java.lang.String str)

        if (str == null) throw new NullPointerException();
        if (str.length() == 0) {
            // math.12=Zero length BigInteger
            throw new NumberFormatException(Messages.getString("math.12")); //$NON-NLS-1$
        }
        this.makeValid();
        int usedLen = NativeBN.BN_dec2bn(this.bignum, str);
        Check((usedLen > 0));
        if (usedLen < str.length()) {
            throw new NumberFormatException(str);
        }
    
public voidputHexString(java.lang.String str)

        if (str == null) throw new NullPointerException();
        if (str.length() == 0) {
            // math.12=Zero length BigInteger
            throw new NumberFormatException(Messages.getString("math.12")); //$NON-NLS-1$
        }
        this.makeValid();
        int usedLen = NativeBN.BN_hex2bn(this.bignum, str);
        Check((usedLen > 0));
        if (usedLen < str.length()) {
            throw new NumberFormatException(str);
        }
    
public voidputLittleEndianInts(int[] a, boolean neg)

        this.makeValid();
        Check(NativeBN.litEndInts2bn(a, a.length, neg, this.bignum));
    
public voidputLongInt(long val)

        this.makeValid();
        Check(NativeBN.putLongInt(this.bignum, val));
    
public voidputULongInt(long val, boolean neg)

        this.makeValid();
        Check(NativeBN.putULongInt(this.bignum, val, neg));
    
public static intremainderByPositiveInt(java.math.BigInt a, int w)

        int rem = NativeBN.BN_mod_word(a.bignum, w);
        Check(rem != -1);
        return rem;
    
public voidsetSign(int val)

        if (val > 0) NativeBN.BN_set_negative(this.bignum, 0);
        else if (val < 0) NativeBN.BN_set_negative(this.bignum, 1);
    
public static java.math.BigIntshift(java.math.BigInt a, int n)

        BigInt r = newBigInt();
        Check(NativeBN.BN_lshift(r.bignum, a.bignum, n));
        return r;
    
public voidshift(int n)

        Check(NativeBN.BN_lshift(this.bignum, this.bignum, n));
    
public intsign()

        return NativeBN.sign(this.bignum);
    
public voidsubtractPositiveInt(int w)

        Check(NativeBN.BN_sub_word(this.bignum, w));
    
public static java.math.BigIntsubtraction(java.math.BigInt a, java.math.BigInt b)

        BigInt r = newBigInt();
        Check(NativeBN.BN_sub(r.bignum, a.bignum, b.bignum));
        return r;
    
public java.lang.StringtoString()

        return this.decString();
    
public booleantwosCompFitsIntoBytes(int byteCnt)

        return NativeBN.twosCompFitsIntoBytes(this.bignum, byteCnt);