本文整理汇总了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;
}
示例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);
}
示例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);
}
示例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);
}
示例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;
}
示例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);
}
}
示例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);
}
}
}
示例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;
}
示例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;
}
示例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);
}
示例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
}
}
示例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);
}
}
示例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;
}
示例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);
}
示例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;
}