private void mulShift(BigInteger multiplicand, int multiplierShift) { _significand = _significand.multiply(multiplicand); _binaryExponent += multiplierShift; // check for too much precision int sc = (_significand.bitLength() - MIN_PRECISION) & 0xFFFFFFE0; // mask makes multiples of 32 which optimises BigInteger.shiftRight if (sc > 0) { // no need to round because we have at least 8 bits of extra precision _significand = _significand.shiftRight(sc); _binaryExponent += sc; } }
int nlen = n.bitLength(); BigInteger nm1 = n.subtract(BigInteger.ONE); BigInteger r, s; do { s = new BigInteger(nlen + 100, rnd); r = s.mod(n); } while (s.subtract(r).add(nm1).bitLength() >= nlen + 100); // result is in 'r'
private static long parseBinaryBigInteger(final byte[] buffer, final int offset, final int length, final boolean negative) { final byte[] remainder = new byte[length - 1]; System.arraycopy(buffer, offset + 1, remainder, 0, length - 1); BigInteger val = new BigInteger(remainder); if (negative) { // 2's complement val = val.add(BigInteger.valueOf(-1)).not(); } if (val.bitLength() > 63) { throw new IllegalArgumentException(String.format( "At offset %d, %d byte binary number exceeds maximum signed long value", offset, length)); } return negative ? -val.longValue() : val.longValue(); }
public BigInteger nextRandomBigInteger(BigInteger n) { Random rand = new Random(); BigInteger result = new BigInteger(n.bitLength(), rand); while( result.compareTo(n) >= 0 ) { result = new BigInteger(n.bitLength(), rand); } return result; }
public static BigInteger random(Random rand, BigInteger minValue, BigInteger maxValue) { BigInteger range = maxValue.subtract(minValue).add(BigInteger.ONE); int bits = range.bitLength(); BigInteger ret; do { ret = new BigInteger(bits, rand); } while(ret.compareTo(range) >= 0); return ret.add(minValue); }
/** @return 0 to max-1 or -1 for us */ public int getRange(T key) { Integer rv; synchronized (_distanceCache) { rv = _distanceCache.get(key); if (rv == null) { // easy way when _bValue == 1 //rv = Integer.valueOf(_bigUs.xor(new BigInteger(1, key.getData())).bitLength() - 1); BigInteger xor = _bigUs.xor(new BigInteger(1, key.getData())); int range = xor.bitLength() - 1; if (_bValue > 1) { int toShift = range + 1 - _bValue; int highbit = range; range <<= _bValue - 1; if (toShift >= 0) { int extra = xor.clearBit(highbit).shiftRight(toShift).intValue(); range += extra; //Log log = I2PAppContext.getGlobalContext().logManager().getLog(KBucketSet.class); //if (log.shouldLog(Log.DEBUG)) // log.debug("highbit " + highbit + " toshift " + toShift + " extra " + extra + " new " + range); } } rv = Integer.valueOf(range); _distanceCache.put(key, rv); } } return rv.intValue(); }
public BigDecimal eval(List<? extends Number> parameters) { assertNotNull(parameters.get(0)); /* * From The Java Programmers Guide To numerical Computing (Ronald Mak, 2003) */ BigDecimal x = (BigDecimal) parameters.get(0); if (x.compareTo(BigDecimal.ZERO) == 0) { return new BigDecimal(0); } if (x.signum() < 0) { throw new ExpressionException("Argument to SQRT() function must not be negative"); } BigInteger n = x.movePointRight(mc.getPrecision() << 1).toBigInteger(); int bits = (n.bitLength() + 1) >> 1; BigInteger ix = n.shiftRight(bits); BigInteger ixPrev; BigInteger test; do { ixPrev = ix; ix = ix.add(n.divide(ix)).shiftRight(1); // Give other threads a chance to work; Thread.yield(); test = ix.subtract(ixPrev).abs(); } while (test.compareTo(BigInteger.ZERO) != 0 && test.compareTo(BigInteger.ONE) != 0); return new BigDecimal(ix, mc.getPrecision()); } });
if (this.a.signum() < 0 || this.a.compareTo(p) >= 0) { throw new IllegalArgumentException("the a is not in the field"); if (this.b.signum() < 0 || this.b.compareTo(p) >= 0) { throw new IllegalArgumentException("the b is not in the field"); if (!(this.a.bitLength() <= fieldSizeInBits)) { throw new IllegalArgumentException("the a is not in the field"); if (!(this.b.bitLength() <= fieldSizeInBits)) { throw new IllegalArgumentException("the b is not in the field");
int logFloor = x.bitLength() - 1; switch (mode) { case UNNECESSARY: if (logFloor < SQRT2_PRECOMPUTE_THRESHOLD) { BigInteger halfPower = SQRT2_PRECOMPUTED_BITS.shiftRight(SQRT2_PRECOMPUTE_THRESHOLD - logFloor); if (x.compareTo(halfPower) <= 0) { return logFloor; } else { int logX2Floor = x2.bitLength() - 1; return (logX2Floor < 2 * logFloor + 1) ? logFloor : logFloor + 1;
public static BigInteger choose( SourceOfRandomness random, BigInteger min, BigInteger max) { BigInteger range = max.subtract(min).add(BigInteger.ONE); BigInteger generated; do { generated = random.nextBigInteger(range.bitLength()); } while (generated.compareTo(range) >= 0); return generated.add(min); }
static double bigToDouble(BigInteger x) { int exponent = absX.bitLength() - 1; return x.signum() * POSITIVE_INFINITY; long twiceSignifFloor = absX.shiftRight(shift).longValue(); long signifFloor = twiceSignifFloor >> 1; bits |= x.signum() & SIGN_MASK; return longBitsToDouble(bits);
@Override protected void primeSourceOfRandomness() { numberOfBits = max.movePointRight(3).subtract(min.movePointRight(3)).toBigInteger().bitLength(); when(randomForParameterGenerator.nextBigInteger(numberOfBits)) .thenReturn(new BigInteger("2").pow(numberOfBits).subtract(ONE)) .thenReturn(ONE) .thenReturn(TEN) .thenReturn(ZERO) .thenReturn(new BigInteger("234234234234")); }
private static long parseBinaryBigInteger(final byte[] buffer, final int offset, final int length, final boolean negative) { final byte[] remainder = new byte[length - 1]; System.arraycopy(buffer, offset + 1, remainder, 0, length - 1); BigInteger val = new BigInteger(remainder); if (negative) { // 2's complement val = val.add(BigInteger.valueOf(-1)).not(); } if (val.bitLength() > 63) { throw new IllegalArgumentException("At offset " + offset + ", " + length + " byte binary number" + " exceeds maximum signed long" + " value"); } return negative ? -val.longValue() : val.longValue(); }
X9ECParameters x9 = NISTNamedCurves.getByName("P-224"); // or whatever curve you want to use ECPoint g = x9.getG(); BigInteger n = x9.getN(); int nBitLength = n.bitLength(); BigInteger x; do { x = new BigInteger(nBitLength, random); } while (x.equals(ZERO) || (x.compareTo(n) >= 0)); ECPoint randomPoint = g.multiply(x);
public static int myRandom(int min, int max, Random r){ if (max <= min) throw new RuntimeException("max value must be greater than min value: max="+max +", min="+min); BigInteger maxB = BigInteger.valueOf(max); BigInteger minB = BigInteger.valueOf(min); BigInteger range = maxB.subtract(minB); do{ BigInteger result = new BigInteger(range.bitLength(), r); if (result.compareTo(range)<=0) return result.add(minB).intValueExact(); }while(true); }
private BigInteger bitsToInt(byte[] t) { BigInteger v = new BigInteger(1, t); if (t.length * 8 > n.bitLength()) { v = v.shiftRight(t.length * 8 - n.bitLength()); } return v; } }
private static void writeBigInteger(OutputStream output, BigInteger value) throws IOException { // encode the signed number as a positive integer value = value.shiftLeft(1); int sign = value.signum(); if (sign < 0) { value = value.negate(); value = value.subtract(ONE); } int length = value.bitLength(); while (true) { long lowBits = value.longValue() & 0x7fffffffffffffffL; length -= 63; // write out the next 63 bits worth of data for (int i = 0; i < 9; ++i) { // if this is the last byte, leave the high bit off if (length <= 0 && (lowBits & ~0x7f) == 0) { output.write((byte) lowBits); return; } else { output.write((byte) (0x80 | (lowBits & 0x7f))); lowBits >>>= 7; } } value = value.shiftRight(63); } } }