Example usage for java.math BigInteger bitLength

List of usage examples for java.math BigInteger bitLength

Introduction

In this page you can find the example usage for java.math BigInteger bitLength.

Prototype

public int bitLength() 

Source Link

Document

Returns the number of bits in the minimal two's-complement representation of this BigInteger, excluding a sign bit.

Usage

From source file:cc.redberry.core.number.Exponentiation.java

static BigInteger findIntegerRoot(BigInteger base, BigInteger power) {
    BigInteger maxBits = BigInteger.valueOf(base.bitLength() + 1); // base < 2 ^ (maxBits + 1)
    // => base ^ ( 1 / power ) < 2 ^ ( (maxBits + 1) / power )

    BigInteger[] divResult = maxBits.divideAndRemainder(power);
    if (divResult[1].signum() == 0) // i.e. divResult[1] == 0
        maxBits = divResult[0];//from  w w  w.  j a  va2 s. c  o m
    else
        maxBits = divResult[0].add(BigInteger.ONE);

    if (maxBits.bitLength() > 31)
        throw new RuntimeException("Too many bits...");

    int targetBitsNumber = maxBits.intValue();
    int resultLengthM1 = targetBitsNumber / 8 + 1; //resultLength minus one
    byte[] result = new byte[resultLengthM1];
    resultLengthM1--;

    int bitNumber = targetBitsNumber;

    int cValue;
    BigInteger testValue;

    while ((--bitNumber) >= 0) {
        //setting bit
        result[resultLengthM1 - (bitNumber >> 3)] |= 1 << (bitNumber & 0x7);

        //Testing
        testValue = new BigInteger(result);
        cValue = ArithmeticUtils.pow(testValue, power).compareTo(base);
        if (cValue == 0)
            return testValue;
        if (cValue > 0)
            result[resultLengthM1 - (bitNumber >> 3)] &= ~(1 << (bitNumber & 0x7));
    }

    return null;
}

From source file:org.mozilla.gecko.sync.Utils.java

public static BigInteger generateBigIntegerLessThan(BigInteger r) {
    int maxBytes = (int) Math.ceil(((double) r.bitLength()) / 8);
    BigInteger randInt = new BigInteger(generateRandomBytes(maxBytes));
    return randInt.mod(r);
}

From source file:Main.java

public static int[] generateCompactWindowNaf(int width, BigInteger k) {
    if (width == 2) {
        return generateCompactNaf(k);
    }//  w  w  w.j  av a2s .  c o  m

    if (width < 2 || width > 16) {
        throw new IllegalArgumentException("'width' must be in the range [2, 16]");
    }
    if ((k.bitLength() >>> 16) != 0) {
        throw new IllegalArgumentException("'k' must have bitlength < 2^16");
    }

    int[] wnaf = new int[k.bitLength() / width + 1];

    // 2^width and a mask and sign bit set accordingly
    int pow2 = 1 << width;
    int mask = pow2 - 1;
    int sign = pow2 >>> 1;

    boolean carry = false;
    int length = 0, pos = 0;

    while (pos <= k.bitLength()) {
        if (k.testBit(pos) == carry) {
            ++pos;
            continue;
        }

        k = k.shiftRight(pos);

        int digit = k.intValue() & mask;
        if (carry) {
            ++digit;
        }

        carry = (digit & sign) != 0;
        if (carry) {
            digit -= pow2;
        }

        int zeroes = length > 0 ? pos - 1 : pos;
        wnaf[length++] = (digit << 16) | zeroes;
        pos = width;
    }

    // Reduce the WNAF array to its actual length
    if (wnaf.length > length) {
        wnaf = trim(wnaf, length);
    }

    return wnaf;
}

From source file:Main.java

/**
 * Computes the Window NAF (non-adjacent Form) of an integer.
 * @param width The width <code>w</code> of the Window NAF. The width is
 * defined as the minimal number <code>w</code>, such that for any
 * <code>w</code> consecutive digits in the resulting representation, at
 * most one is non-zero./*from   ww  w .  ja  va  2 s. c o m*/
 * @param k The integer of which the Window NAF is computed.
 * @return The Window NAF of the given width, such that the following holds:
 * <code>k = &sum;<sub>i=0</sub><sup>l-1</sup> k<sub>i</sub>2<sup>i</sup>
 * </code>, where the <code>k<sub>i</sub></code> denote the elements of the
 * returned <code>byte[]</code>.
 */
public static byte[] generateWindowNaf(int width, BigInteger k) {
    if (width == 2) {
        return generateNaf(k);
    }

    if (width < 2 || width > 8) {
        throw new IllegalArgumentException("'width' must be in the range [2, 8]");
    }

    byte[] wnaf = new byte[k.bitLength() + 1];

    // 2^width and a mask and sign bit set accordingly
    int pow2 = 1 << width;
    int mask = pow2 - 1;
    int sign = pow2 >>> 1;

    boolean carry = false;
    int length = 0, pos = 0;

    while (pos <= k.bitLength()) {
        if (k.testBit(pos) == carry) {
            ++pos;
            continue;
        }

        k = k.shiftRight(pos);

        int digit = k.intValue() & mask;
        if (carry) {
            ++digit;
        }

        carry = (digit & sign) != 0;
        if (carry) {
            digit -= pow2;
        }

        length += (length > 0) ? pos - 1 : pos;
        wnaf[length++] = (byte) digit;
        pos = width;
    }

    // Reduce the WNAF array to its actual length
    if (wnaf.length > length) {
        wnaf = trim(wnaf, length);
    }

    return wnaf;
}

From source file:Main.java

public static int[] generateCompactWindowNaf(int width, BigInteger k) {
    if (width == 2) {
        return generateCompactNaf(k);
    }/*ww w. j ava 2  s.c om*/

    if (width < 2 || width > 16) {
        throw new IllegalArgumentException("'width' must be in the range [2, 16]");
    }
    if ((k.bitLength() >>> 16) != 0) {
        throw new IllegalArgumentException("'k' must have bitlength < 2^16");
    }
    if (k.signum() == 0) {
        return EMPTY_INTS;
    }

    int[] wnaf = new int[k.bitLength() / width + 1];

    // 2^width and a mask and sign bit set accordingly
    int pow2 = 1 << width;
    int mask = pow2 - 1;
    int sign = pow2 >>> 1;

    boolean carry = false;
    int length = 0, pos = 0;

    while (pos <= k.bitLength()) {
        if (k.testBit(pos) == carry) {
            ++pos;
            continue;
        }

        k = k.shiftRight(pos);

        int digit = k.intValue() & mask;
        if (carry) {
            ++digit;
        }

        carry = (digit & sign) != 0;
        if (carry) {
            digit -= pow2;
        }

        int zeroes = length > 0 ? pos - 1 : pos;
        wnaf[length++] = (digit << 16) | zeroes;
        pos = width;
    }

    // Reduce the WNAF array to its actual length
    if (wnaf.length > length) {
        wnaf = trim(wnaf, length);
    }

    return wnaf;
}

From source file:Main.java

/**
 * Computes the Window NAF (non-adjacent Form) of an integer.
 * @param width The width <code>w</code> of the Window NAF. The width is
 * defined as the minimal number <code>w</code>, such that for any
 * <code>w</code> consecutive digits in the resulting representation, at
 * most one is non-zero.//from www  .  ja v  a2s . c om
 * @param k The integer of which the Window NAF is computed.
 * @return The Window NAF of the given width, such that the following holds:
 * <code>k = &sum;<sub>i=0</sub><sup>l-1</sup> k<sub>i</sub>2<sup>i</sup>
 * </code>, where the <code>k<sub>i</sub></code> denote the elements of the
 * returned <code>byte[]</code>.
 */
public static byte[] generateWindowNaf(int width, BigInteger k) {
    if (width == 2) {
        return generateNaf(k);
    }

    if (width < 2 || width > 8) {
        throw new IllegalArgumentException("'width' must be in the range [2, 8]");
    }
    if (k.signum() == 0) {
        return EMPTY_BYTES;
    }

    byte[] wnaf = new byte[k.bitLength() + 1];

    // 2^width and a mask and sign bit set accordingly
    int pow2 = 1 << width;
    int mask = pow2 - 1;
    int sign = pow2 >>> 1;

    boolean carry = false;
    int length = 0, pos = 0;

    while (pos <= k.bitLength()) {
        if (k.testBit(pos) == carry) {
            ++pos;
            continue;
        }

        k = k.shiftRight(pos);

        int digit = k.intValue() & mask;
        if (carry) {
            ++digit;
        }

        carry = (digit & sign) != 0;
        if (carry) {
            digit -= pow2;
        }

        length += (length > 0) ? pos - 1 : pos;
        wnaf[length++] = (byte) digit;
        pos = width;
    }

    // Reduce the WNAF array to its actual length
    if (wnaf.length > length) {
        wnaf = trim(wnaf, length);
    }

    return wnaf;
}

From source file:Main.java

private static boolean passesMillerRabin(BigInteger us, int iterations, Random rnd) {
    final BigInteger ONE = BigInteger.ONE;
    final BigInteger TWO = BigInteger.valueOf(2);
    // Find a and m such that m is odd and this == 1 + 2**a * m
    BigInteger thisMinusOne = us.subtract(ONE);
    BigInteger m = thisMinusOne;// www.  j  a  v  a 2  s.  c  om
    int a = m.getLowestSetBit();
    m = m.shiftRight(a);

    // Do the tests
    if (rnd == null) {
        rnd = new SecureRandom();
    }
    for (int i = 0; i < iterations; i++) {
        // Generate a uniform random on (1, this)
        BigInteger b;
        do {
            b = new BigInteger(us.bitLength(), rnd);
        } while (b.compareTo(ONE) <= 0 || b.compareTo(us) >= 0);

        int j = 0;
        BigInteger z = b.modPow(m, us);
        while (!((j == 0 && z.equals(ONE)) || z.equals(thisMinusOne))) {
            if (j > 0 && z.equals(ONE) || ++j == a)
                return false;
            z = z.modPow(TWO, us);
        }
    }
    return true;
}

From source file:com.d2lvalence.idkeyauth.codec.binary.Base64.java

/**
 * Returns a byte-array representation of a <code>BigInteger</code> without sign bit.
 *
 * @param bigInt//from w  w w .j av  a2 s.  com
 *            <code>BigInteger</code> to be converted
 * @return a byte array representation of the BigInteger parameter
 */
static byte[] toIntegerBytes(final BigInteger bigInt) {
    int bitlen = bigInt.bitLength();
    // round bitlen
    bitlen = ((bitlen + 7) >> 3) << 3;
    final byte[] bigBytes = bigInt.toByteArray();

    if (((bigInt.bitLength() % 8) != 0) && (((bigInt.bitLength() / 8) + 1) == (bitlen / 8))) {
        return bigBytes;
    }
    // set up params for copying everything but sign bit
    int startSrc = 0;
    int len = bigBytes.length;

    // if bigInt is exactly byte-aligned, just skip signbit in copy
    if ((bigInt.bitLength() % 8) == 0) {
        startSrc = 1;
        len--;
    }
    final int startDst = bitlen / 8 - len; // to pad w/ nulls as per spec
    final byte[] resizedBytes = new byte[bitlen / 8];
    System.arraycopy(bigBytes, startSrc, resizedBytes, startDst, len);
    return resizedBytes;
}

From source file:com.android.email.codec.binary.Base64.java

/**
 * Returns a byte-array representation of a <code>BigInteger</code>
 * without sign bit./*  ww  w.j av  a  2  s .c o  m*/
 *
 * @param bigInt <code>BigInteger</code> to be converted
 * @return a byte array representation of the BigInteger parameter
 */
static byte[] toIntegerBytes(BigInteger bigInt) {
    int bitlen = bigInt.bitLength();
    // round bitlen
    bitlen = ((bitlen + 7) >> 3) << 3;
    byte[] bigBytes = bigInt.toByteArray();

    if (((bigInt.bitLength() % 8) != 0) && (((bigInt.bitLength() / 8) + 1) == (bitlen / 8))) {
        return bigBytes;
    }

    // set up params for copying everything but sign bit
    int startSrc = 0;
    int len = bigBytes.length;

    // if bigInt is exactly byte-aligned, just skip signbit in copy
    if ((bigInt.bitLength() % 8) == 0) {
        startSrc = 1;
        len--;
    }

    int startDst = bitlen / 8 - len; // to pad w/ nulls as per spec
    byte[] resizedBytes = new byte[bitlen / 8];

    System.arraycopy(bigBytes, startSrc, resizedBytes, startDst, len);

    return resizedBytes;
}

From source file:com.cinnober.msgcodec.json.JsonValueHandler.java

static boolean isJavaScriptSafeSigned(BigInteger value) {
    int bitlen = value.bitLength();
    return bitlen <= 52 || (bitlen == 53 && isJavaScriptSafeSigned(value.longValue()));
}