List of usage examples for java.math BigInteger bitLength
public int bitLength()
From source file:Main.java
public static BigInteger sqrt(final BigInteger val) { final BigInteger two = BigInteger.valueOf(2); BigInteger a = BigInteger.ONE.shiftLeft(val.bitLength() / 2); BigInteger b;//from w w w . j a v a2 s.c om do { b = val.divide(a); a = (a.add(b)).divide(two); } while (a.subtract(b).abs().compareTo(two) >= 0); return a; }
From source file:com.cliqset.magicsig.util.KeyGen.java
/** * Returns a byte-array representation of a <code>{@link BigInteger}<code>. * No sign-bit is outputed./*from w ww.ja va2s . c om*/ * * <b>N.B.:</B> <code>{@link BigInteger}<code>'s toByteArray * retunrs 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 = big.bitLength(); //round bitlen bitlen = ((bitlen + 7) >> 3) << 3; if (bitlen < big.bitLength()) { throw new IllegalArgumentException("Illegal bit len."); } 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; }
From source file:it.zero11.acme.utils.JWKUtils.java
private 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; }/* w w w. j a va2 s .com*/ // 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:Main.java
public static byte[] generateJSF(BigInteger g, BigInteger h) { int digits = Math.max(g.bitLength(), h.bitLength()) + 1; byte[] jsf = new byte[digits]; BigInteger k0 = g, k1 = h;//from w w w.j a v a2 s .c om int j = 0, d0 = 0, d1 = 0; while (k0.signum() > 0 || k1.signum() > 0 || d0 > 0 || d1 > 0) { int n0 = (k0.intValue() + d0) & 7, n1 = (k1.intValue() + d1) & 7; int u0 = n0 & 1; if (u0 != 0) { u0 -= (n0 & 2); if ((n0 + u0) == 4 && (n1 & 3) == 2) { u0 = -u0; } } int u1 = n1 & 1; if (u1 != 0) { u1 -= (n1 & 2); if ((n1 + u1) == 4 && (n0 & 3) == 2) { u1 = -u1; } } if ((d0 << 1) == 1 + u0) { d0 = 1 - d0; } if ((d1 << 1) == 1 + u1) { d1 = 1 - d1; } k0 = k0.shiftRight(1); k1 = k1.shiftRight(1); jsf[j++] = (byte) ((u0 << 4) | (u1 & 0xF)); } // Reduce the JSF array to its actual length if (jsf.length > j) { jsf = trim(jsf, j); } return jsf; }
From source file:Main.java
public static int[] generateCompactNaf(BigInteger k) { if ((k.bitLength() >>> 16) != 0) { throw new IllegalArgumentException("'k' must have bitlength < 2^16"); }//from w w w. jav a 2 s.co m if (k.signum() == 0) { return EMPTY_INTS; } BigInteger _3k = k.shiftLeft(1).add(k); int bits = _3k.bitLength(); int[] naf = new int[bits >> 1]; BigInteger diff = _3k.xor(k); int highBit = bits - 1, length = 0, zeroes = 0; for (int i = 1; i < highBit; ++i) { if (!diff.testBit(i)) { ++zeroes; continue; } int digit = k.testBit(i) ? -1 : 1; naf[length++] = (digit << 16) | zeroes; zeroes = 1; ++i; } naf[length++] = (1 << 16) | zeroes; if (naf.length > length) { naf = trim(naf, length); } return naf; }
From source file:Main.java
public static byte[] generateJSF(BigInteger g, BigInteger h) { int digits = Math.max(g.bitLength(), h.bitLength()) + 1; byte[] jsf = new byte[digits]; BigInteger k0 = g, k1 = h;// ww w. j a v a 2 s. co m int j = 0, d0 = 0, d1 = 0; int offset = 0; while ((d0 | d1) != 0 || k0.bitLength() > offset || k1.bitLength() > offset) { int n0 = ((k0.intValue() >>> offset) + d0) & 7, n1 = ((k1.intValue() >>> offset) + d1) & 7; int u0 = n0 & 1; if (u0 != 0) { u0 -= (n0 & 2); if ((n0 + u0) == 4 && (n1 & 3) == 2) { u0 = -u0; } } int u1 = n1 & 1; if (u1 != 0) { u1 -= (n1 & 2); if ((n1 + u1) == 4 && (n0 & 3) == 2) { u1 = -u1; } } if ((d0 << 1) == 1 + u0) { d0 ^= 1; } if ((d1 << 1) == 1 + u1) { d1 ^= 1; } if (++offset == 30) { offset = 0; k0 = k0.shiftRight(30); k1 = k1.shiftRight(30); } jsf[j++] = (byte) ((u0 << 4) | (u1 & 0xF)); } // Reduce the JSF array to its actual length if (jsf.length > j) { jsf = trim(jsf, j); } return jsf; }
From source file:Main.java
public static byte[] generateJSF(BigInteger g, BigInteger h) { byte[] jsf = new byte[(Math.max(g.bitLength(), h.bitLength()) + 1)]; BigInteger k0 = g;//from w w w .j ava2 s . co m BigInteger k1 = h; int d0 = 0; int d1 = 0; int offset = 0; int j = 0; while (true) { if ((d0 | d1) == 0 && k0.bitLength() <= offset && k1.bitLength() <= offset) { break; } int n0 = ((k0.intValue() >>> offset) + d0) & 7; int n1 = ((k1.intValue() >>> offset) + d1) & 7; int u0 = n0 & 1; if (u0 != 0) { u0 -= n0 & 2; if (n0 + u0 == 4 && (n1 & 3) == 2) { u0 = -u0; } } int u1 = n1 & 1; if (u1 != 0) { u1 -= n1 & 2; if (n1 + u1 == 4 && (n0 & 3) == 2) { u1 = -u1; } } if ((d0 << 1) == u0 + 1) { d0 ^= 1; } if ((d1 << 1) == u1 + 1) { d1 ^= 1; } offset++; if (offset == 30) { offset = 0; k0 = k0.shiftRight(30); k1 = k1.shiftRight(30); } int j2 = j + 1; jsf[j] = (byte) ((u0 << 4) | (u1 & 15)); j = j2; } if (jsf.length > j) { return trim(jsf, j); } return jsf; }
From source file:org.keycloak.jose.jwk.JWKBuilder.java
/** * Copied from org.apache.commons.codec.binary.Base64 *//*from w w w .j av a 2 s . co m*/ private 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:Main.java
/** * Compute the square root of x to a given scale, x >= 0. Use Newton's * algorithm./*from w w w. j av a 2 s .com*/ * * @param x * the value of x * @return the result value */ public static BigDecimal sqrt(BigDecimal x) { // Check that x >= 0. if (x.signum() < 0) { throw new ArithmeticException("x < 0"); } // n = x*(10^(2*SCALE)) BigInteger n = x.movePointRight(SCALE << 1).toBigInteger(); // The first approximation is the upper half of n. int bits = (n.bitLength() + 1) >> 1; BigInteger ix = n.shiftRight(bits); BigInteger ixPrev; // Loop until the approximations converge // (two successive approximations are equal after rounding). do { ixPrev = ix; // x = (x + n/x)/2 ix = ix.add(n.divide(ix)).shiftRight(1); Thread.yield(); } while (ix.compareTo(ixPrev) != 0); return new BigDecimal(ix, SCALE); }
From source file:cc.redberry.core.number.NumberUtils.java
/** * Computes the integer square root of a number. * * @param n The number./* ww w. j a v a 2 s. c o m*/ * @return The integer square root, i.e. the largest number whose square * doesn't exceed n. */ public static BigInteger sqrt(BigInteger n) { if (n.signum() >= 0) { final int bitLength = n.bitLength(); BigInteger root = BigInteger.ONE.shiftLeft(bitLength / 2); while (!isSqrtXXX(n, root)) root = root.add(n.divide(root)).divide(TWO); return root; } else throw new ArithmeticException("square root of negative number"); }