@Override public BigInteger getTokenCountInRange(String startToken, String endToken) { BigInteger start = new BigInteger(startToken); checkTokenBounds(start); BigInteger end = new BigInteger(endToken); checkTokenBounds(end); if (start.equals(end)) { if (start.equals(MIN_TOKEN)) { return TOTAL_TOKEN_COUNT; } else { return ZERO; } } BigInteger result = end.subtract(start); if (end.compareTo(start) <= 0) { result = result.add(TOTAL_TOKEN_COUNT); } return result; }
checkNonNegative("x", x); if (fitsInLong(x)) { return BigInteger.valueOf(LongMath.sqrt(x.longValue(), mode)); checkRoundingUnnecessary(sqrtFloor.pow(2).equals(x)); // fall through case FLOOR: case DOWN: boolean sqrtFloorIsExact = && sqrtFloor.pow(2).equals(x); // slow exact check return sqrtFloorIsExact ? sqrtFloor : sqrtFloor.add(BigInteger.ONE); case HALF_DOWN: case HALF_UP: case HALF_EVEN: BigInteger halfSquare = sqrtFloor.pow(2).add(sqrtFloor); return (halfSquare.compareTo(x) >= 0) ? sqrtFloor : sqrtFloor.add(BigInteger.ONE); default: throw new AssertionError();
int approxCmp = approxPow.compareTo(x); approxLog10--; approxPow = approxPow.divide(BigInteger.TEN); approxCmp = approxPow.compareTo(x); } while (approxCmp > 0); } else { BigInteger nextPow = BigInteger.TEN.multiply(approxPow); int nextCmp = nextPow.compareTo(x); while (nextCmp <= 0) { approxLog10++; approxPow = nextPow; approxCmp = nextCmp; nextPow = BigInteger.TEN.multiply(approxPow); nextCmp = nextPow.compareTo(x); return floorPow.equals(x) ? floorLog : floorLog + 1; BigInteger halfPowerSquared = floorPow.pow(2).multiply(BigInteger.TEN); return (x2.compareTo(halfPowerSquared) <= 0) ? floorLog : floorLog + 1; default:
if (listSpec==null || (numId!=null && !numId.equals(listSpec.numId))) { listSpec = new ListSpec(numId, BigInteger.valueOf(i)); listSpec.sdtList = new SdtBlock(); setTag(listSpec.sdtList, numId, ilvl); if (ilvl.equals(listSpec.ilvl)) { } else if (ilvl.compareTo(listSpec.ilvl)>0) { listSpec = new ListSpec(numId, BigInteger.valueOf(i)); listSpec.sdtList = new SdtBlock(); setTag(listSpec.sdtList, numId, ilvl);
final BigInteger sizeNum = toNum.subtract(fromNum).add(new BigInteger("1")); tempsize = sizeNum.intValue(); if (!BigInteger.valueOf(tempsize).equals(sizeNum)) { tempsize = Integer.MAX_VALUE; final BigInteger sizeNum = toNum.subtract(fromNum).add(new BigDecimal(1.0)).toBigInteger(); tempsize = sizeNum.intValue(); if (!BigInteger.valueOf(tempsize).equals(sizeNum)) { tempsize = Integer.MAX_VALUE;
public int compareTo(Item item) { if (item == null) { return BIG_INTEGER_ZERO.equals(value) ? 0 : 1; // 1.0 == 1, 1.1 > 1 } switch (item.getType()) { case INTEGER_ITEM: return value.compareTo(((IntegerItem) item).value); case STRING_ITEM: return 1; // 1.1 > 1-sp case LIST_ITEM: return 1; // 1.1 > 1-1 default: throw new RuntimeException("invalid item: " + item.getClass()); } }
protected byte[] engineSign() throws SignatureException { BigInteger elgp = key.getParams().getP(); BigInteger pm1 = elgp.subtract(BigInteger.ONE); BigInteger elgg = key.getParams().getG(); BigInteger x = ((ElGamalPrivateKey) key).getX(); do { k = new BigInteger(2048, RandomSource.getInstance()); ok = k.compareTo(pm1) == -1; ok = ok && k.compareTo(BigInteger.ONE) == 1; ok = ok && k.gcd(pm1).equals(BigInteger.ONE); } while (!ok); BigInteger kinv = k.modInverse(pm1); BigInteger h = new NativeBigInteger(1, data); BigInteger s = (kinv.multiply(h.subtract(x.multiply(r)))).mod(pm1);
/** Leaves while, do, for, if, ternary (?::), return or switch. */ private void leaveConditional() { final Values valuePair = popValue(); final BigInteger expressionValue = valuePair.getExpressionValue(); BigInteger basicRangeValue = valuePair.getRangeValue(); if (currentRangeValue.equals(BigInteger.ZERO)) { currentRangeValue = BigInteger.ONE; } if (basicRangeValue.equals(BigInteger.ZERO)) { basicRangeValue = BigInteger.ONE; } currentRangeValue = currentRangeValue.add(expressionValue).multiply(basicRangeValue); }
messageDigest.update(A.toByteArray()); final BigInteger u = new BigInteger(1, messageDigest.digest(B.toByteArray())); if (u.equals(BigInteger.ZERO)) { throw new CognitoInternalErrorException("Hash of A and B cannot be zero"); messageDigest.update(salt.toByteArray()); final BigInteger x = new BigInteger(1, messageDigest.digest(userIdHash)); final BigInteger s = (B.subtract(KK.multiply(GG.modPow(x, N))) .modPow(a.add(u.multiply(x)), N)).mod(N);
/** * @param from never null, inclusive minimum * @param to never null, exclusive maximum, {@code >= from} * @param incrementUnit never null, {@code > 0} */ public BigIntegerValueRange(BigInteger from, BigInteger to, BigInteger incrementUnit) { this.from = from; this.to = to; this.incrementUnit = incrementUnit; if (to.compareTo(from) < 0) { throw new IllegalArgumentException("The " + getClass().getSimpleName() + " cannot have a from (" + from + ") which is strictly higher than its to (" + to + ")."); } if (incrementUnit.compareTo(BigInteger.ZERO) <= 0) { throw new IllegalArgumentException("The " + getClass().getSimpleName() + " must have strictly positive incrementUnit (" + incrementUnit + ")."); } if (!to.subtract(from).remainder(incrementUnit).equals(BigInteger.ZERO)) { throw new IllegalArgumentException("The " + getClass().getSimpleName() + "'s incrementUnit (" + incrementUnit + ") must fit an integer number of times between from (" + from + ") and to (" + to + ")."); } }
/** Leaves else, default or case group tokens. */ private void leaveBranch() { final Values valuePair = popValue(); final BigInteger basicRangeValue = valuePair.getRangeValue(); final BigInteger expressionValue = valuePair.getExpressionValue(); if (branchVisited && currentRangeValue.equals(BigInteger.ZERO)) { currentRangeValue = BigInteger.ONE; } currentRangeValue = currentRangeValue.subtract(BigInteger.ONE) .add(basicRangeValue) .add(expressionValue); }
@GwtIncompatible // TODO @AndroidIncompatible // slow public void testDivNonZeroExact() { boolean isAndroid = System.getProperties().getProperty("java.runtime.name").contains("Android"); for (BigInteger p : NONZERO_BIGINTEGER_CANDIDATES) { for (BigInteger q : NONZERO_BIGINTEGER_CANDIDATES) { if (isAndroid && p.equals(BAD_FOR_ANDROID_P) && q.equals(BAD_FOR_ANDROID_Q)) { // https://code.google.com/p/android/issues/detail?id=196555 continue; } if (isAndroid && p.equals(BAD_FOR_GINGERBREAD_P) && q.equals(BAD_FOR_GINGERBREAD_Q)) { // Works fine under Marshmallow, so I haven't filed a bug. continue; } boolean dividesEvenly = p.remainder(q).equals(ZERO); try { BigInteger quotient = BigIntegerMath.divide(p, q, UNNECESSARY); BigInteger undone = quotient.multiply(q); if (!p.equals(undone)) { failFormat("expected %s.multiply(%s) = %s; got %s", quotient, q, p, undone); } assertTrue(dividesEvenly); } catch (ArithmeticException e) { assertFalse(dividesEvenly); } } } }
expectedNonce = repo.getNonce(txSender); curNonce.put(key, expectedNonce.add(ONE)); BigInteger txNonce = new BigInteger(1, tx.getNonce()); if (!expectedNonce.equals(txNonce)) { logger.warn("Invalid transaction: Tx nonce {} != expected nonce {} (parent nonce: {}): {}", txNonce, expectedNonce, repo.getNonce(txSender), tx);
public static void main(String... args) { BigInteger fact = fact(100); System.out.println("fact(100) = " + fact); System.out.println("fact(100).longValue() = " + fact.longValue()); System.out.println("fact(100).intValue() = " + fact.intValue()); int powerOfTwoCount = 0; BigInteger two = BigInteger.valueOf(2); while (fact.compareTo(BigInteger.ZERO) > 0 && fact.mod(two).equals(BigInteger.ZERO)) { powerOfTwoCount++; fact = fact.divide(two); } System.out.println("fact(100) powers of two = " + powerOfTwoCount); } private static BigInteger fact(long n) { BigInteger result = BigInteger.ONE; for (long i = 2; i <= n; i++) result = result.multiply(BigInteger.valueOf(i)); return result; }
/** * Raise a BigInteger to a BigInteger power. * * @param k Number to raise. * @param e Exponent (must be positive or zero). * @return k<sup>e</sup> * @throws NotPositiveException if {@code e < 0}. */ public static BigInteger pow(final BigInteger k, BigInteger e) throws NotPositiveException { if (e.compareTo(BigInteger.ZERO) < 0) { throw new NotPositiveException(LocalizedFormats.EXPONENT, e); } BigInteger result = BigInteger.ONE; BigInteger k2p = k; while (!BigInteger.ZERO.equals(e)) { if (e.testBit(0)) { result = result.multiply(k2p); } k2p = k2p.multiply(k2p); e = e.shiftRight(1); } return result; }