当前位置: 首页>>代码示例>>Java>>正文


Java BigInteger.bitLength方法代码示例

本文整理汇总了Java中java.math.BigInteger.bitLength方法的典型用法代码示例。如果您正苦于以下问题:Java BigInteger.bitLength方法的具体用法?Java BigInteger.bitLength怎么用?Java BigInteger.bitLength使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.math.BigInteger的用法示例。


在下文中一共展示了BigInteger.bitLength方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: bigIntegerToNullPaddedByteArray

import java.math.BigInteger; //导入方法依赖的package包/类
/**
 * Converts a BigInteger into a byte array of given size. If the BigInteger
 * doesn't fit into the byte array, bits of the BigInteger will simply be
 * truncated, starting with the most significant bit. If the array is larger
 * than the BigInteger, prepending bytes in the array will be 0x00.
 *
 * @param input
 *            big integer
 * @param outputSizeInBytes
 *            output size
 * @return big integer converted to a byte array
 */
public static byte[] bigIntegerToNullPaddedByteArray(BigInteger input, int outputSizeInBytes) {
    if (input == null) {
        throw new IllegalArgumentException("'input' must not be null.");
    }
    byte[] output = new byte[outputSizeInBytes];

    int numByteBlocks = input.bitLength() / 8;
    int remainingBits;

    if (numByteBlocks < output.length) {
        remainingBits = input.bitLength() % 8;
    } else {
        remainingBits = 0;
        numByteBlocks = output.length;
    }

    int i;
    for (i = 0; i < numByteBlocks; i++) {
        output[output.length - 1 - i] = input.shiftRight(i * 8).byteValue();
    }
    if (remainingBits > 0) {
        output[output.length - 1 - i] = input.shiftRight(i * 8).byteValue();
    }
    return output;
}
 
开发者ID:RUB-NDS,项目名称:ModifiableVariable,代码行数:38,代码来源:ArrayConverter.java

示例2: bitLength

import java.math.BigInteger; //导入方法依赖的package包/类
public static void bitLength() {
    int failCount = 0;

    for (int i=0; i<SIZE*10; i++) {
        int x = rnd.nextInt();
        BigInteger bigX = BigInteger.valueOf((long)x);
        int signBit = (x < 0 ? 0x80000000 : 0);
        int tmp = x, bitLength, j;
        for (j=0; j<32 && (tmp & 0x80000000)==signBit; j++)
            tmp <<= 1;
        bitLength = 32 - j;

        if (bigX.bitLength() != bitLength) {
            //System.err.println(x+": "+bitLength+", "+bigX.bitLength());
            failCount++;
        }
    }

    report("BitLength", failCount);
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:21,代码来源:BigIntegerTest.java

示例3: generateS

import java.math.BigInteger; //导入方法依赖的package包/类
private BigInteger generateS(BigInteger x, BigInteger q,
        BigInteger r, BigInteger k) throws SignatureException {

    byte[] s2;
    try {
        s2 = md.digest();
    } catch (RuntimeException re) {
        // Only for RawDSA due to its 20-byte length restriction
        throw new SignatureException(re.getMessage());
    }
    // get the leftmost min(N, outLen) bits of the digest value
    int nBytes = q.bitLength()/8;
    if (nBytes < s2.length) {
        s2 = Arrays.copyOfRange(s2, 0, nBytes);
    }
    BigInteger z = new BigInteger(1, s2);
    BigInteger k1 = k.modInverse(q);

    return x.multiply(r).add(z).multiply(k1).mod(q);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:21,代码来源:DSA.java

示例4: bitLength

import java.math.BigInteger; //导入方法依赖的package包/类
public static void bitLength() {
    int failCount = 0;

    for (int i=0; i<SIZE*10; i++) {
        int x = random.nextInt();
        BigInteger bigX = BigInteger.valueOf((long)x);
        int signBit = (x < 0 ? 0x80000000 : 0);
        int tmp = x, bitLength, j;
        for (j=0; j<32 && (tmp & 0x80000000)==signBit; j++)
            tmp <<= 1;
        bitLength = 32 - j;

        if (bigX.bitLength() != bitLength) {
            //System.err.println(x+": "+bitLength+", "+bigX.bitLength());
            failCount++;
        }
    }

    report("BitLength", failCount);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:21,代码来源:BigIntegerTest.java

示例5: toIntegerBytes

import java.math.BigInteger; //导入方法依赖的package包/类
/**
 * Returns a byte-array representation of a <code>BigInteger</code> without sign bit.
 * 
 * @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;
}
 
开发者ID:wso2-incubator,项目名称:scim2-compliance-test-suite,代码行数:31,代码来源:Base64.java

示例6: writeField

import java.math.BigInteger; //导入方法依赖的package包/类
private void writeField(
    OutputStream    out,
    BigInteger      fieldValue)
    throws IOException
{
    int byteCount = (fieldValue.bitLength()+6)/7;
    if (byteCount == 0) 
    {
        out.write(0);
    }  
    else 
    {
        BigInteger tmpValue = fieldValue;
        byte[] tmp = new byte[byteCount];
        for (int i = byteCount-1; i >= 0; i--) 
        {
            tmp[i] = (byte) ((tmpValue.intValue() & 0x7f) | 0x80);
            tmpValue = tmpValue.shiftRight(7); 
        }
        tmp[byteCount-1] &= 0x7f;
        out.write(tmp);
    }

}
 
开发者ID:PhilippC,项目名称:keepass2android,代码行数:25,代码来源:DERObjectIdentifier.java

示例7: writeBigDecimal

import java.math.BigInteger; //导入方法依赖的package包/类
/**
 * Writes {@link BigDecimal} value. The written value can be read via {@link #readBigDecimal(DataInput)}.
 *
 * @param val Value.
 * @param out Data output.
 *
 * @throws IOException if failed to write value.
 */
public static void writeBigDecimal(BigDecimal val, DataOutput out) throws IOException {
    if (val.compareTo(BigDecimal.ZERO) == 0) {
        out.writeByte(DECIMAL_ZERO);
    } else if (val.compareTo(BigDecimal.ONE) == 0) {
        out.writeByte(DECIMAL_ONE);
    } else if (val.compareTo(BigDecimal.TEN) == 0) {
        out.writeByte(DECIMAL_TEN);
    } else {
        int scale = val.scale();

        BigInteger unscaled = val.unscaledValue();

        int bits = unscaled.bitLength();

        if (bits <= 63) {
            if (scale == 0) {
                out.writeByte(DECIMAL_SMALL_UNSCALED);
                writeVarLong(unscaled.longValue(), out);
            } else {
                out.writeByte(DECIMAL_SMALL_SCALED);
                writeVarIntUnsigned(scale, out);
                writeVarLong(unscaled.longValue(), out);
            }
        } else {
            byte[] bytes = unscaled.toByteArray();

            out.writeByte(DECIMAL_BIG);
            writeVarIntUnsigned(scale, out);
            writeVarIntUnsigned(bytes.length, out);
            out.write(bytes, 0, bytes.length);
        }
    }
}
 
开发者ID:hekate-io,项目名称:hekate,代码行数:42,代码来源:CodecUtils.java

示例8: multiply

import java.math.BigInteger; //导入方法依赖的package包/类
/**
 * D.3.2 pg 101
 * @see org.bouncycastle.math.ec.ECMultiplier#multiply(org.bouncycastle.math.ec.ECPoint, java.math.BigInteger)
 */
public ECPoint multiply(ECPoint p, BigInteger k, PreCompInfo preCompInfo)
{
    // TODO Probably should try to add this
    // BigInteger e = k.mod(n); // n == order of p
    BigInteger e = k;
    BigInteger h = e.multiply(BigInteger.valueOf(3));

    ECPoint neg = p.negate();
    ECPoint R = p;

    for (int i = h.bitLength() - 2; i > 0; --i)
    {             
        R = R.twice();

        boolean hBit = h.testBit(i);
        boolean eBit = e.testBit(i);

        if (hBit != eBit)
        {
            R = R.add(hBit ? p : neg);
        }
    }

    return R;
}
 
开发者ID:Appdome,项目名称:ipack,代码行数:30,代码来源:FpNafMultiplier.java

示例9: logBigInteger

import java.math.BigInteger; //导入方法依赖的package包/类
public static double logBigInteger(BigInteger val) {
    int blex = val.bitLength() - 1022; // any value in 60..1023 is ok
    if(blex > 0) {
        val = val.shiftRight(blex);
    }
    double res = Math.log(val.doubleValue());
    return blex > 0 ? res + blex * LOG2 : res;
}
 
开发者ID:Panzer1119,项目名称:JAddOn,代码行数:9,代码来源:JBigNumber.java

示例10: publicPointFromPrivate

import java.math.BigInteger; //导入方法依赖的package包/类
/**
 * Returns public key point from the given private key. To convert a byte array into a BigInteger, use <tt>
 * new BigInteger(1, bytes);</tt>
 */
public static ECPoint publicPointFromPrivate(BigInteger privKey) {
    /*
     * TODO: FixedPointCombMultiplier currently doesn't support scalars longer than the group order,
     * but that could change in future versions.
     */
    if (privKey.bitLength() > CURVE.getN().bitLength()) {
        privKey = privKey.mod(CURVE.getN());
    }
    return new FixedPointCombMultiplier().multiply(CURVE.getG(), privKey);
}
 
开发者ID:rsksmart,项目名称:bitcoinj-thin,代码行数:15,代码来源:BtcECKey.java

示例11: testAdd

import java.math.BigInteger; //导入方法依赖的package包/类
private static void testAdd() {
    System.out.println("Testing BigInteger.add");
    try {
        BigInteger actual = MAX_VALUE.add(BigInteger.ONE);
        throw new RuntimeException("BigInteger.MAX_VALUE.add(BigInteger.ONE).bitLength()=" + actual.bitLength());
    } catch (ArithmeticException e) {
        // expected
    }
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:10,代码来源:SymmetricRangeTests.java

示例12: checkKeyPair

import java.math.BigInteger; //导入方法依赖的package包/类
private static void checkKeyPair(KeyPair kp, int pSize) throws Exception {

        DHPrivateKey privateKey = (DHPrivateKey)kp.getPrivate();
        BigInteger p = privateKey.getParams().getP();
        if (p.bitLength() != pSize) {
            throw new Exception(
                "Invalid modulus size: " + p.bitLength() + "/" + pSize);
        }

        if (!p.isProbablePrime(128)) {
            throw new Exception("Good luck, the modulus is composite!");
        }

        DHPublicKey publicKey = (DHPublicKey)kp.getPublic();
        p = publicKey.getParams().getP();
        if (p.bitLength() != pSize) {
            throw new Exception(
                "Invalid modulus size: " + p.bitLength() + "/" + pSize);
        }

        BigInteger leftOpen = BigInteger.ONE;
        BigInteger rightOpen = p.subtract(BigInteger.ONE);

        BigInteger x = privateKey.getX();
        if ((x.compareTo(leftOpen) <= 0) ||
                (x.compareTo(rightOpen) >= 0)) {
            throw new Exception(
                "X outside range [2, p - 2]:  x: " + x + " p: " + p);
        }

        BigInteger y = publicKey.getY();
        if ((y.compareTo(leftOpen) <= 0) ||
                (y.compareTo(rightOpen) >= 0)) {
            throw new Exception(
                "Y outside range [2, p - 2]:  x: " + x + " p: " + p);
        }
    }
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:38,代码来源:SupportedDHParamGens.java

示例13: encode

import java.math.BigInteger; //导入方法依赖的package包/类
/**
 * Returns a byte-array representation of a <code>{@link BigInteger}<code>.
 * No sign-bit is output.
 *
 * <b>N.B.:</B> <code>{@link BigInteger}<code>'s toByteArray
 * returns eventually longer arrays because of the leading sign-bit.
 *
 * @param big <code>BigInteger<code> to be converted
 * @param bitlen <code>int<code> the desired length in bits of the representation
 * @return a byte array with <code>bitlen</code> bits of <code>big</code>
 */
public static final  byte[] encode(BigInteger big, int bitlen) {

    //round bitlen
    bitlen = ((bitlen + 7) >> 3) << 3;

    if (bitlen < big.bitLength()) {
        throw new IllegalArgumentException(I18n.translate("utils.Base64.IllegalBitlength"));
    }

    byte[] bigBytes = big.toByteArray();

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

    // some copying needed
    int startSrc = 0;    // no need to skip anything
    int bigLen = bigBytes.length;    //valid length of the string

    if ((big.bitLength() % 8) == 0) {    // correct values
        startSrc = 1;    // skip sign bit

        bigLen--;    // valid length of the string
    }

    int startDst = bitlen / 8 - bigLen;    //pad with leading nulls
    byte[] resizedBytes = new byte[bitlen / 8];

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

    return resizedBytes;
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:45,代码来源:Base64.java

示例14: hash2FieldElement

import java.math.BigInteger; //导入方法依赖的package包/类
private static ECFieldElement hash2FieldElement(ECCurve curve, byte[] hash)
{
    byte[] data = Arrays.clone(hash);
    reverseBytes(data);
    BigInteger num = new BigInteger(1, data);
    while (num.bitLength() >= curve.getFieldSize())
    {
        num = num.clearBit(num.bitLength() - 1);
    }

    return curve.fromBigInteger(num);
}
 
开发者ID:Appdome,项目名称:ipack,代码行数:13,代码来源:DSTU4145Signer.java

示例15: getBytes

import java.math.BigInteger; //导入方法依赖的package包/类
/**
 * Returns a byte-array representation of a <code>{@link BigInteger}<code>.
 * No sign-bit is output.
 *
 * <b>N.B.:</B> <code>{@link BigInteger}<code>'s toByteArray
 * returns eventually longer arrays because of the leading sign-bit.
 *
 * @param big <code>BigInteger<code> to be converted
 * @param bitlen <code>int<code> the desired length in bits of the representation
 * @return a byte array with <code>bitlen</code> bits of <code>big</code>
 */
static final byte[] getBytes(BigInteger big, int bitlen) {

    //round bitlen
    bitlen = ((bitlen + 7) >> 3) << 3;

    if (bitlen < big.bitLength()) {
        throw new IllegalArgumentException(I18n.translate("utils.Base64.IllegalBitlength"));
    }

    byte[] bigBytes = big.toByteArray();

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

    // some copying needed
    int startSrc = 0;    // no need to skip anything
    int bigLen = bigBytes.length;    //valid length of the string

    if ((big.bitLength() % 8) == 0) {    // correct values
        startSrc = 1;    // skip sign bit

        bigLen--;    // valid length of the string
    }

    int startDst = bitlen / 8 - bigLen;    //pad with leading nulls
    byte[] resizedBytes = new byte[bitlen / 8];

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

    return resizedBytes;
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:45,代码来源:Base64.java


注:本文中的java.math.BigInteger.bitLength方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。